| 1 | /*
 | 
|---|
| 2 |  * QtQueryList.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jul 24, 2012
 | 
|---|
| 5 |  *      Author: ankele
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef QTQUERYLIST_HPP_
 | 
|---|
| 9 | #define QTQUERYLIST_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | // include config.h
 | 
|---|
| 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 14 | #include <config.h>
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 18 | #include "Parameters/Parameter.hpp"
 | 
|---|
| 19 | #include "Parameters/Validators/STLVectorValidator.hpp"
 | 
|---|
| 20 | #include "Parameters/Validators/Ops_Validator_impl.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | class QListWidget;
 | 
|---|
| 23 | class QPushButton;
 | 
|---|
| 24 | class QVBoxLayout;
 | 
|---|
| 25 | class QHBoxLayout;
 | 
|---|
| 26 | class QBoxLayout;
 | 
|---|
| 27 | 
 | 
|---|
| 28 | class QtQueryListUntyped {
 | 
|---|
| 29 | public:
 | 
|---|
| 30 |   QtQueryListUntyped(QBoxLayout *parent, Dialog *_dialog);
 | 
|---|
| 31 |   virtual ~QtQueryListUntyped(){}
 | 
|---|
| 32 | 
 | 
|---|
| 33 |   virtual void onSubUpdate() = 0;
 | 
|---|
| 34 | 
 | 
|---|
| 35 |   void onUpdate();
 | 
|---|
| 36 |   void elementSelected();
 | 
|---|
| 37 |   void addElementToListWidget(const std::string &str);
 | 
|---|
| 38 |   std::vector<int> getSelectedRows();
 | 
|---|
| 39 |   void removeSelectedRows(const std::vector<int> &rows);
 | 
|---|
| 40 | 
 | 
|---|
| 41 | protected:
 | 
|---|
| 42 |   QListWidget *inputList;
 | 
|---|
| 43 |   QPushButton *addButton;
 | 
|---|
| 44 |   QPushButton *removeButton;
 | 
|---|
| 45 |   QVBoxLayout *thisVLayout;
 | 
|---|
| 46 |   QHBoxLayout *thisHLayout;
 | 
|---|
| 47 |   QVBoxLayout *buttonBox;
 | 
|---|
| 48 |   Dialog *dialog;
 | 
|---|
| 49 | };
 | 
|---|
| 50 | 
 | 
|---|
| 51 | template<class T>
 | 
|---|
| 52 | class QtQueryList : public QtQueryListUntyped {
 | 
|---|
| 53 | public:
 | 
|---|
| 54 |   QtQueryList(Parameter<std::vector<T> > &parentParam, QBoxLayout *parent, Dialog *_dialog, std::vector<T> &_temp) : QtQueryListUntyped(parent, _dialog), tempRef(_temp)
 | 
|---|
| 55 |   {
 | 
|---|
| 56 |     // do we have an STLVectorValidator?
 | 
|---|
| 57 |     Validator<std::vector<T> > *val = &parentParam.getValidator();
 | 
|---|
| 58 |     STLVectorValidator<std::vector<T> > *vector_val = NULL;
 | 
|---|
| 59 | 
 | 
|---|
| 60 |     // might be hidden inside an And_Validator
 | 
|---|
| 61 |     And_Validator<std::vector<T> > * and_val = dynamic_cast<And_Validator<std::vector<T> > *>(val);
 | 
|---|
| 62 |     if (and_val){
 | 
|---|
| 63 |       if (dynamic_cast<STLVectorValidator<std::vector<T> > *>(and_val->getA()))
 | 
|---|
| 64 |         vector_val = dynamic_cast<STLVectorValidator<std::vector<T> > *>(and_val->getA());
 | 
|---|
| 65 |       else if (dynamic_cast<STLVectorValidator<std::vector<T> > *>(and_val->getB()))
 | 
|---|
| 66 |         vector_val = dynamic_cast<STLVectorValidator<std::vector<T> > *>(and_val->getB());
 | 
|---|
| 67 |     }else{
 | 
|---|
| 68 |       vector_val = dynamic_cast<STLVectorValidator<std::vector<T> > *>(val);
 | 
|---|
| 69 |     }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |     if (vector_val){
 | 
|---|
| 72 |       // if so, try to use its ElementwiseValidator
 | 
|---|
| 73 |       subParam = new Parameter<T>("sub-param", *(vector_val->getElementwiseValidator()));
 | 
|---|
| 74 |     }else{
 | 
|---|
| 75 |       subParam = new Parameter<T>("sub-param");
 | 
|---|
| 76 |     }
 | 
|---|
| 77 |   }
 | 
|---|
| 78 |   virtual ~QtQueryList()
 | 
|---|
| 79 |   {
 | 
|---|
| 80 |     delete(subParam);
 | 
|---|
| 81 |   }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   void addElement() {
 | 
|---|
| 84 |     // add item to both
 | 
|---|
| 85 |     addElementToListWidget(subParam->getAsString());
 | 
|---|
| 86 |     tempRef.push_back(subParam->get());
 | 
|---|
| 87 |     onUpdate();
 | 
|---|
| 88 |   }
 | 
|---|
| 89 |   void removeElements()
 | 
|---|
| 90 |   {
 | 
|---|
| 91 |     std::vector<int> rows = getSelectedRows();
 | 
|---|
| 92 |     removeSelectedRows(rows);
 | 
|---|
| 93 |     for (int i = rows.size() - 1; i >= 0; i --){
 | 
|---|
| 94 |       ASSERT((size_t)(rows[i]) < tempRef.size(), "QtQueryList<T>::removeElements() trying to remove invalid element.");
 | 
|---|
| 95 |       tempRef.erase(tempRef.begin() + rows[i]);
 | 
|---|
| 96 |     }
 | 
|---|
| 97 |     onUpdate();
 | 
|---|
| 98 |   }
 | 
|---|
| 99 | protected:
 | 
|---|
| 100 |   std::vector<T> &tempRef;
 | 
|---|
| 101 |   Parameter<T> *subParam;
 | 
|---|
| 102 | };
 | 
|---|
| 103 | 
 | 
|---|
| 104 | 
 | 
|---|
| 105 | 
 | 
|---|
| 106 | class ListQuerySubDialog : public Dialog
 | 
|---|
| 107 | {
 | 
|---|
| 108 | public:
 | 
|---|
| 109 |   ListQuerySubDialog(QtQueryListUntyped *_parent, const std::string &_title) :
 | 
|---|
| 110 |     Dialog(_title),
 | 
|---|
| 111 |     parent(_parent),
 | 
|---|
| 112 |     sub(NULL)
 | 
|---|
| 113 |   {}
 | 
|---|
| 114 |   virtual void update()
 | 
|---|
| 115 |   {
 | 
|---|
| 116 |     if (sub)
 | 
|---|
| 117 |       if (sub->isValid())
 | 
|---|
| 118 |         sub->setResult();
 | 
|---|
| 119 |     parent->onSubUpdate();
 | 
|---|
| 120 |   }
 | 
|---|
| 121 |   void setSubQuery(Query *_sub){  sub = _sub; }
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   virtual void queryEmpty(const std::string = "", const std::string = ""){}
 | 
|---|
| 124 |   virtual void queryBoolean(Parameter<bool> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 125 |   virtual void queryInt(Parameter<int> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 126 |   virtual void queryInts(Parameter<std::vector<int> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 127 |   virtual void queryUnsignedInt(Parameter<unsigned int> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 128 |   virtual void queryUnsignedInts(Parameter<std::vector<unsigned int> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 129 |   virtual void queryDouble(Parameter<double> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 130 |   virtual void queryDoubles(Parameter<std::vector<double> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 131 |   virtual void queryString(Parameter<std::string> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 132 |   virtual void queryStrings(Parameter<std::vector<std::string> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 133 |   virtual void queryAtom(Parameter<const atom *> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 134 |   virtual void queryAtoms(Parameter<std::vector<const atom *> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 135 |   virtual void queryMolecule(Parameter<const molecule *> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 136 |   virtual void queryMolecules(Parameter<std::vector<const molecule *> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 137 |   virtual void queryVector(Parameter<Vector> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 138 |   virtual void queryVectors(Parameter<std::vector<Vector> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 139 |   virtual void queryRealSpaceMatrix(Parameter<RealSpaceMatrix> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 140 |   virtual void queryElement(Parameter<const element *> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 141 |   virtual void queryElements(Parameter<std::vector<const element *> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 142 |   virtual void queryFile(Parameter<boost::filesystem::path> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 143 |   virtual void queryFiles(Parameter<std::vector< boost::filesystem::path> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 144 |   virtual void queryKeyValuePair(Parameter<KeyValuePair> &, const std::string = "", const std::string = ""){}
 | 
|---|
| 145 |   virtual void queryKeyValuePairs(Parameter<std::vector<KeyValuePair> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| 146 | 
 | 
|---|
| 147 | private:
 | 
|---|
| 148 |   QtQueryListUntyped *parent;
 | 
|---|
| 149 |   Query *sub;
 | 
|---|
| 150 | };
 | 
|---|
| 151 | 
 | 
|---|
| 152 | 
 | 
|---|
| 153 | #endif /* QTQUERYLIST_HPP_ */
 | 
|---|