| 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(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) : parent(_parent), sub(NULL){} | 
|---|
| 110 | virtual void update() | 
|---|
| 111 | { | 
|---|
| 112 | if (sub) | 
|---|
| 113 | if (sub->isValid()) | 
|---|
| 114 | sub->setResult(); | 
|---|
| 115 | parent->onSubUpdate(); | 
|---|
| 116 | } | 
|---|
| 117 | void setSubQuery(Query *_sub){  sub = _sub; } | 
|---|
| 118 |  | 
|---|
| 119 | virtual void queryEmpty(const char*, std::string){} | 
|---|
| 120 | virtual void queryBoolean(Parameter<bool> &, const char *, std::string = ""){} | 
|---|
| 121 | virtual void queryInt(Parameter<int> &, const char *,std::string = ""){} | 
|---|
| 122 | virtual void queryInts(Parameter<std::vector<int> > &, const char *,std::string = ""){} | 
|---|
| 123 | virtual void queryUnsignedInt(Parameter<unsigned int> &, const char *,std::string = ""){} | 
|---|
| 124 | virtual void queryUnsignedInts(Parameter<std::vector<unsigned int> > &, const char *,std::string = ""){} | 
|---|
| 125 | virtual void queryDouble(Parameter<double> &, const char*,std::string = ""){} | 
|---|
| 126 | virtual void queryDoubles(Parameter<std::vector<double> > &, const char*,std::string = ""){} | 
|---|
| 127 | virtual void queryString(Parameter<std::string> &, const char*,std::string = ""){} | 
|---|
| 128 | virtual void queryStrings(Parameter<std::vector<std::string> > &, const char*,std::string = ""){} | 
|---|
| 129 | virtual void queryAtom(Parameter<const atom *> &, const char*,std::string = ""){} | 
|---|
| 130 | virtual void queryAtoms(Parameter<std::vector<const atom *> > &, const char*,std::string = ""){} | 
|---|
| 131 | virtual void queryMolecule(Parameter<const molecule *> &, const char*,std::string = ""){} | 
|---|
| 132 | virtual void queryMolecules(Parameter<std::vector<const molecule *> > &, const char*,std::string = ""){} | 
|---|
| 133 | virtual void queryVector(Parameter<Vector> &, const char*,std::string = ""){} | 
|---|
| 134 | virtual void queryVectors(Parameter<std::vector<Vector> > &, const char*,std::string = ""){} | 
|---|
| 135 | virtual void queryRealSpaceMatrix(Parameter<RealSpaceMatrix> &, const char*, std::string = ""){} | 
|---|
| 136 | virtual void queryElement(Parameter<const element *> &, const char*,std::string = ""){} | 
|---|
| 137 | virtual void queryElements(Parameter<std::vector<const element *> > &, const char*,std::string = ""){} | 
|---|
| 138 | virtual void queryFile(Parameter<boost::filesystem::path> &, const char*,std::string = ""){} | 
|---|
| 139 | virtual void queryFiles(Parameter<std::vector< boost::filesystem::path> > &, const char*,std::string = ""){} | 
|---|
| 140 | virtual void queryRandomNumberDistribution_Parameters(Parameter<RandomNumberDistribution_Parameters> &, const char*,std::string = ""){} | 
|---|
| 141 | private: | 
|---|
| 142 | QtQueryListUntyped *parent; | 
|---|
| 143 | Query *sub; | 
|---|
| 144 | }; | 
|---|
| 145 |  | 
|---|
| 146 |  | 
|---|
| 147 | #endif /* QTQUERYLIST_HPP_ */ | 
|---|