| [7b8a8e] | 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"
 | 
|---|
| [713a43] | 19 | #include "Parameters/Validators/STLVectorValidator.hpp"
 | 
|---|
| [e59e8e] | 20 | #include "Parameters/Validators/Ops_Validator_impl.hpp"
 | 
|---|
| [7b8a8e] | 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:
 | 
|---|
| [713a43] | 54 |   QtQueryList(Parameter<std::vector<T> > &parentParam, QBoxLayout *parent, Dialog *_dialog, std::vector<T> &_temp) : QtQueryListUntyped(parent, _dialog), tempRef(_temp)
 | 
|---|
| [7b8a8e] | 55 |   {
 | 
|---|
| [713a43] | 56 |     // do we have an STLVectorValidator?
 | 
|---|
| [e59e8e] | 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){
 | 
|---|
| [713a43] | 72 |       // if so, try to use its ElementwiseValidator
 | 
|---|
| [e59e8e] | 73 |       subParam = new Parameter<T>("sub-param", *(vector_val->getElementwiseValidator()));
 | 
|---|
| [713a43] | 74 |     }else{
 | 
|---|
 | 75 |       subParam = new Parameter<T>("sub-param");
 | 
|---|
 | 76 |     }
 | 
|---|
| [7b8a8e] | 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 --){
 | 
|---|
| [a2a2f7] | 94 |       ASSERT((size_t)(rows[i]) < tempRef.size(), "QtQueryList<T>::removeElements() trying to remove invalid element.");
 | 
|---|
| [7b8a8e] | 95 |       tempRef.erase(tempRef.begin() + rows[i]);
 | 
|---|
 | 96 |     }
 | 
|---|
 | 97 |     onUpdate();
 | 
|---|
 | 98 |   }
 | 
|---|
 | 99 | protected:
 | 
|---|
 | 100 |   std::vector<T> &tempRef;
 | 
|---|
 | 101 |   Parameter<T> *subParam;
 | 
|---|
 | 102 | };
 | 
|---|
 | 103 | 
 | 
|---|
| [6af6470] | 104 | template<>
 | 
|---|
 | 105 | class QtQueryList<Vector> : public QtQueryListUntyped {
 | 
|---|
 | 106 | public:
 | 
|---|
| [f79d65] | 107 |   QtQueryList(Parameter<std::vector<Vector> > &parentParam, QBoxLayout *parent, Dialog *_dialog, std::vector<std::string> &_temp) : QtQueryListUntyped(parent, _dialog), tempRef(_temp)
 | 
|---|
| [6af6470] | 108 |   {
 | 
|---|
 | 109 |     // do we have an STLVectorValidator?
 | 
|---|
 | 110 |     Validator<std::vector<Vector> > *val = &parentParam.getValidator();
 | 
|---|
 | 111 |     STLVectorValidator<std::vector<Vector> > *vector_val = NULL;
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |     // might be hidden inside an And_Validator
 | 
|---|
 | 114 |     And_Validator<std::vector<Vector> > * and_val = dynamic_cast<And_Validator<std::vector<Vector> > *>(val);
 | 
|---|
 | 115 |     if (and_val){
 | 
|---|
 | 116 |       if (dynamic_cast<STLVectorValidator<std::vector<Vector> > *>(and_val->getA()))
 | 
|---|
 | 117 |         vector_val = dynamic_cast<STLVectorValidator<std::vector<Vector> > *>(and_val->getA());
 | 
|---|
 | 118 |       else if (dynamic_cast<STLVectorValidator<std::vector<Vector> > *>(and_val->getB()))
 | 
|---|
 | 119 |         vector_val = dynamic_cast<STLVectorValidator<std::vector<Vector> > *>(and_val->getB());
 | 
|---|
 | 120 |     }else{
 | 
|---|
 | 121 |       vector_val = dynamic_cast<STLVectorValidator<std::vector<Vector> > *>(val);
 | 
|---|
 | 122 |     }
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 |     if (vector_val){
 | 
|---|
 | 125 |       // if so, try to use its ElementwiseValidator
 | 
|---|
 | 126 |       subParam = new Parameter<Vector>("sub-param", *(vector_val->getElementwiseValidator()));
 | 
|---|
 | 127 |     }else{
 | 
|---|
 | 128 |       subParam = new Parameter<Vector>("sub-param");
 | 
|---|
 | 129 |     }
 | 
|---|
 | 130 |   }
 | 
|---|
 | 131 |   virtual ~QtQueryList()
 | 
|---|
 | 132 |   {
 | 
|---|
 | 133 |     delete(subParam);
 | 
|---|
 | 134 |   }
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |   void addElement() {
 | 
|---|
 | 137 |     // add item to both
 | 
|---|
 | 138 |     addElementToListWidget(subParam->getAsString());
 | 
|---|
| [f79d65] | 139 |     tempRef.push_back(subParam->getAsString());
 | 
|---|
| [6af6470] | 140 |     onUpdate();
 | 
|---|
 | 141 |   }
 | 
|---|
 | 142 |   void removeElements()
 | 
|---|
 | 143 |   {
 | 
|---|
 | 144 |     std::vector<int> rows = getSelectedRows();
 | 
|---|
 | 145 |     removeSelectedRows(rows);
 | 
|---|
 | 146 |     for (int i = rows.size() - 1; i >= 0; i --){
 | 
|---|
 | 147 |       ASSERT((size_t)(rows[i]) < tempRef.size(), "QtQueryList<Vector>::removeElements() trying to remove invalid element.");
 | 
|---|
 | 148 |       tempRef.erase(tempRef.begin() + rows[i]);
 | 
|---|
 | 149 |     }
 | 
|---|
 | 150 |     onUpdate();
 | 
|---|
 | 151 |   }
 | 
|---|
 | 152 | protected:
 | 
|---|
| [f79d65] | 153 |   std::vector<std::string> &tempRef;
 | 
|---|
| [6af6470] | 154 |   Parameter<Vector> *subParam;
 | 
|---|
 | 155 | };
 | 
|---|
 | 156 | 
 | 
|---|
| [7b8a8e] | 157 | 
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | class ListQuerySubDialog : public Dialog
 | 
|---|
 | 160 | {
 | 
|---|
 | 161 | public:
 | 
|---|
| [163110] | 162 |   ListQuerySubDialog(QtQueryListUntyped *_parent, const std::string &_title) :
 | 
|---|
 | 163 |     Dialog(_title),
 | 
|---|
 | 164 |     parent(_parent),
 | 
|---|
 | 165 |     sub(NULL)
 | 
|---|
 | 166 |   {}
 | 
|---|
| [7b8a8e] | 167 |   virtual void update()
 | 
|---|
 | 168 |   {
 | 
|---|
 | 169 |     if (sub)
 | 
|---|
| [7e6a1b] | 170 |       if (sub->isValid())
 | 
|---|
 | 171 |         sub->setResult();
 | 
|---|
| [7b8a8e] | 172 |     parent->onSubUpdate();
 | 
|---|
 | 173 |   }
 | 
|---|
 | 174 |   void setSubQuery(Query *_sub){  sub = _sub; }
 | 
|---|
 | 175 | 
 | 
|---|
| [f130d4] | 176 |   virtual void queryEmpty(const std::string = "", const std::string = ""){}
 | 
|---|
 | 177 |   virtual void queryBoolean(Parameter<bool> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 178 |   virtual void queryInt(Parameter<int> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 179 |   virtual void queryInts(Parameter<std::vector<int> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 180 |   virtual void queryUnsignedInt(Parameter<unsigned int> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 181 |   virtual void queryUnsignedInts(Parameter<std::vector<unsigned int> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 182 |   virtual void queryDouble(Parameter<double> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 183 |   virtual void queryDoubles(Parameter<std::vector<double> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 184 |   virtual void queryString(Parameter<std::string> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 185 |   virtual void queryStrings(Parameter<std::vector<std::string> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 186 |   virtual void queryAtom(Parameter<const atom *> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 187 |   virtual void queryAtoms(Parameter<std::vector<const atom *> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 188 |   virtual void queryMolecule(Parameter<const molecule *> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 189 |   virtual void queryMolecules(Parameter<std::vector<const molecule *> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 190 |   virtual void queryVector(Parameter<Vector> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 191 |   virtual void queryVectors(Parameter<std::vector<Vector> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 192 |   virtual void queryRealSpaceMatrix(Parameter<RealSpaceMatrix> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 193 |   virtual void queryElement(Parameter<const element *> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 194 |   virtual void queryElements(Parameter<std::vector<const element *> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 195 |   virtual void queryFile(Parameter<boost::filesystem::path> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 196 |   virtual void queryFiles(Parameter<std::vector< boost::filesystem::path> > &, const std::string = "", const std::string = ""){}
 | 
|---|
| [33e801] | 197 |   virtual void queryKeyValuePair(Parameter<KeyValuePair> &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 198 |   virtual void queryKeyValuePairs(Parameter<std::vector<KeyValuePair> > &, const std::string = "", const std::string = ""){}
 | 
|---|
 | 199 | 
 | 
|---|
| [7b8a8e] | 200 | private:
 | 
|---|
 | 201 |   QtQueryListUntyped *parent;
 | 
|---|
 | 202 |   Query *sub;
 | 
|---|
 | 203 | };
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 | #endif /* QTQUERYLIST_HPP_ */
 | 
|---|