[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 |
|
---|
[9d5531] | 33 | template<typename T> QtQueryListPipe<T>::QtQueryListPipe(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) {
|
---|
[9d5531] | 73 | content.push_back(item);
|
---|
[8df74d] | 74 |
|
---|
| 75 | dialog->update();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[2aafe8] | 78 | template<typename T> void QtQueryListPipe<T>::removeElement() {
|
---|
[8df74d] | 79 | QList<QListWidgetItem *> items = inputList->selectedItems();
|
---|
| 80 | for (QList<QListWidgetItem *>::iterator iter = items.begin(); !items.empty(); iter = items.begin()) {
|
---|
| 81 | // obtain which position item has (by making it current item)
|
---|
| 82 | inputList->setCurrentItem(*iter);
|
---|
| 83 | // remove
|
---|
[2aafe8] | 84 | QtQueryListPipe<T>::removeRow(inputList->currentRow()); // template parameters needs to be known, such that compiler knows which to call
|
---|
| 85 | inputList->takeItem(inputList->currentRow());
|
---|
| 86 | items.erase(iter);
|
---|
[8df74d] | 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[2aafe8] | 90 | template<typename T> void QtQueryListPipe<T>::removeRow(int row) {
|
---|
[8df74d] | 91 | int counter = 0;
|
---|
[9d5531] | 92 | typename std::vector<T>::iterator iter = content.begin();
|
---|
| 93 | for (; iter != content.end(); ++iter)
|
---|
[8df74d] | 94 | if (counter++ == row)
|
---|
| 95 | break;
|
---|
[9d5531] | 96 | if (iter != content.end())
|
---|
| 97 | content.erase(iter);
|
---|
[8df74d] | 98 | }
|
---|
| 99 |
|
---|
[2aafe8] | 100 | class StringsQtQueryPipe : public QtQueryListPipe<std::string>
|
---|
| 101 | {
|
---|
| 102 | Q_OBJECT
|
---|
| 103 | public:
|
---|
[9d5531] | 104 | StringsQtQueryPipe(std::vector<std::string> &_content, QtDialog *_dialog, QLineEdit *_inputBox, QListWidget *_inputList, QPushButton *_AddButton, QPushButton *_RemoveButton) :
|
---|
[2aafe8] | 105 | QtQueryListPipe<std::string>(_content, _dialog, _inputBox, _inputList, _AddButton, _RemoveButton) { }
|
---|
| 106 |
|
---|
| 107 | public slots:
|
---|
| 108 | virtual void elementEntered(const QString& t)
|
---|
| 109 | {
|
---|
| 110 | QtQueryListPipe<std::string>::elementEntered(t);
|
---|
| 111 | }
|
---|
| 112 | virtual void elementSelected()
|
---|
| 113 | {
|
---|
| 114 | QtQueryListPipe<std::string>::elementSelected();
|
---|
| 115 | }
|
---|
| 116 | virtual void addElement()
|
---|
| 117 | {
|
---|
| 118 | QtQueryListPipe<std::string>::addElement();
|
---|
| 119 | }
|
---|
| 120 | virtual void removeElement()
|
---|
| 121 | {
|
---|
| 122 | QtQueryListPipe<std::string>::removeElement();
|
---|
| 123 | }
|
---|
| 124 | };
|
---|
| 125 |
|
---|
[8df74d] | 126 |
|
---|
| 127 | #endif /* QTQUERYLISTPIPE_HPP_ */
|
---|