| [8df74d] | 1 | /*
 | 
|---|
 | 2 |  * QtQueryListPipe.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Oct 25, 2010
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef QTQUERYLISTPIPE_HPP_
 | 
|---|
 | 9 | #define QTQUERYLISTPIPE_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
| [56f73b] | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | 
 | 
|---|
| [f10b0c] | 17 | #include "Parameters/Parameter.hpp"
 | 
|---|
| [a5ddf0] | 18 | #include "QtQueryPipe.hpp"
 | 
|---|
 | 19 | 
 | 
|---|
| [2aafe8] | 20 | // Qt moc needs these because it generates its own .cpp file from QtQueryListPipe.hpp.
 | 
|---|
 | 21 | #include <Qt/qboxlayout.h>
 | 
|---|
 | 22 | #include <Qt/qlabel.h>
 | 
|---|
 | 23 | #include <Qt/qlineedit.h>
 | 
|---|
 | 24 | #include <Qt/qlistwidget.h>
 | 
|---|
 | 25 | #include <Qt/qpushbutton.h>
 | 
|---|
 | 26 | 
 | 
|---|
| [5813ac] | 27 | #include <boost/lexical_cast.hpp>
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | using boost::lexical_cast;
 | 
|---|
 | 30 | using boost::bad_lexical_cast;
 | 
|---|
 | 31 | 
 | 
|---|
| [a5ddf0] | 32 | 
 | 
|---|
| [f10b0c] | 33 | template<typename T> QtQueryListPipe<T>::QtQueryListPipe(Parameter<std::vector<T> > &_content, QtDialog *_dialog, QLineEdit *_inputBox, QListWidget *_inputList, QPushButton *_AddButton, QPushButton *_RemoveButton) :
 | 
|---|
| [8df74d] | 34 |   content(_content),
 | 
|---|
 | 35 |   dialog(_dialog),
 | 
|---|
 | 36 |   inputBox(_inputBox),
 | 
|---|
 | 37 |   inputList(_inputList),
 | 
|---|
 | 38 |   AddButton(_AddButton),
 | 
|---|
 | 39 |   RemoveButton(_RemoveButton)
 | 
|---|
 | 40 | {}
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | template<typename T> QtQueryListPipe<T>::~QtQueryListPipe()
 | 
|---|
 | 43 | {}
 | 
|---|
 | 44 | 
 | 
|---|
| [2aafe8] | 45 | template<typename T> void QtQueryListPipe<T>::elementEntered(const QString& t)
 | 
|---|
| [8df74d] | 46 | {
 | 
|---|
| [2aafe8] | 47 |   AddButton->setEnabled(t.length() > 0);
 | 
|---|
| [8df74d] | 48 | }
 | 
|---|
 | 49 | 
 | 
|---|
| [2aafe8] | 50 | template<typename T> void QtQueryListPipe<T>::elementSelected()
 | 
|---|
| [8df74d] | 51 | {
 | 
|---|
 | 52 |   if (inputList->selectedItems().empty())
 | 
|---|
 | 53 |     RemoveButton->setEnabled(false);
 | 
|---|
 | 54 |   else
 | 
|---|
 | 55 |     RemoveButton->setEnabled(true);
 | 
|---|
 | 56 | }
 | 
|---|
 | 57 | 
 | 
|---|
| [2aafe8] | 58 | template<typename T> void QtQueryListPipe<T>::addElement() {
 | 
|---|
| [8df74d] | 59 |   // type-check
 | 
|---|
 | 60 |   std::string text = inputBox->text().toStdString();
 | 
|---|
| [2aafe8] | 61 |   T value;
 | 
|---|
| [8df74d] | 62 |   try {
 | 
|---|
| [2aafe8] | 63 |     value = boost::lexical_cast<T>(text);
 | 
|---|
| [8df74d] | 64 |   } catch (boost::bad_lexical_cast&) {
 | 
|---|
 | 65 |     return;
 | 
|---|
 | 66 |   };
 | 
|---|
 | 67 |   // add item to both
 | 
|---|
| [2aafe8] | 68 |   inputList->addItem(QString(toString(value).c_str()));
 | 
|---|
 | 69 |   addValue(value);
 | 
|---|
| [8df74d] | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
| [2aafe8] | 72 | template<typename T> void QtQueryListPipe<T>::addValue(T item) {
 | 
|---|
 | 73 |   std::vector<T> temp = content.get();
 | 
|---|
 | 74 |   temp.push_back(item);
 | 
|---|
 | 75 |   content.set(temp);
 | 
|---|
| [8df74d] | 76 | 
 | 
|---|
 | 77 |   dialog->update();
 | 
|---|
 | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
| [2aafe8] | 80 | template<typename T> void QtQueryListPipe<T>::removeElement() {
 | 
|---|
| [8df74d] | 81 |   QList<QListWidgetItem *> items = inputList->selectedItems();
 | 
|---|
 | 82 |   for (QList<QListWidgetItem *>::iterator iter = items.begin(); !items.empty(); iter = items.begin()) {
 | 
|---|
 | 83 |     // obtain which position item has (by making it current item)
 | 
|---|
 | 84 |     inputList->setCurrentItem(*iter);
 | 
|---|
 | 85 |     // remove
 | 
|---|
| [2aafe8] | 86 |     QtQueryListPipe<T>::removeRow(inputList->currentRow()); // template parameters needs to be known, such that compiler knows which to call
 | 
|---|
 | 87 |     inputList->takeItem(inputList->currentRow());
 | 
|---|
 | 88 |     items.erase(iter);
 | 
|---|
| [8df74d] | 89 |   }
 | 
|---|
 | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
| [2aafe8] | 92 | template<typename T> void QtQueryListPipe<T>::removeRow(int row) {
 | 
|---|
| [8df74d] | 93 |   int counter = 0;
 | 
|---|
| [2aafe8] | 94 |   std::vector<T> temp = content.get();
 | 
|---|
 | 95 |   typename std::vector<T>::iterator iter = temp.begin();
 | 
|---|
 | 96 |   for (; iter != temp.end(); ++iter)
 | 
|---|
| [8df74d] | 97 |     if (counter++ == row)
 | 
|---|
 | 98 |       break;
 | 
|---|
| [2aafe8] | 99 |   if (iter != temp.end())
 | 
|---|
 | 100 |     temp.erase(iter);
 | 
|---|
 | 101 |   content.set(temp);
 | 
|---|
| [8df74d] | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
| [2aafe8] | 104 | class StringsQtQueryPipe : public QtQueryListPipe<std::string>
 | 
|---|
 | 105 | {
 | 
|---|
 | 106 |   Q_OBJECT
 | 
|---|
 | 107 | public:
 | 
|---|
 | 108 |   StringsQtQueryPipe(Parameter<std::vector<std::string> > &_content, QtDialog *_dialog, QLineEdit *_inputBox, QListWidget *_inputList, QPushButton *_AddButton, QPushButton *_RemoveButton) :
 | 
|---|
 | 109 |     QtQueryListPipe<std::string>(_content, _dialog, _inputBox, _inputList, _AddButton, _RemoveButton) { }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | public slots:
 | 
|---|
 | 112 |   virtual void elementEntered(const QString& t)
 | 
|---|
 | 113 |   {
 | 
|---|
 | 114 |     QtQueryListPipe<std::string>::elementEntered(t);
 | 
|---|
 | 115 |   }
 | 
|---|
 | 116 |   virtual void elementSelected()
 | 
|---|
 | 117 |   {
 | 
|---|
 | 118 |     QtQueryListPipe<std::string>::elementSelected();
 | 
|---|
 | 119 |   }
 | 
|---|
 | 120 |   virtual void addElement()
 | 
|---|
 | 121 |   {
 | 
|---|
 | 122 |     QtQueryListPipe<std::string>::addElement();
 | 
|---|
 | 123 |   }
 | 
|---|
 | 124 |   virtual void removeElement()
 | 
|---|
 | 125 |   {
 | 
|---|
 | 126 |     QtQueryListPipe<std::string>::removeElement();
 | 
|---|
 | 127 |   }
 | 
|---|
 | 128 | };
 | 
|---|
 | 129 | 
 | 
|---|
| [8df74d] | 130 | 
 | 
|---|
 | 131 | #endif /*  QTQUERYLISTPIPE_HPP_  */
 | 
|---|