| 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 |
|
|---|
| 11 | // include config.h
|
|---|
| 12 | #ifdef HAVE_CONFIG_H
|
|---|
| 13 | #include <config.h>
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | #include "Parameters/Parameter.hpp"
|
|---|
| 18 | #include "QtQueryPipe.hpp"
|
|---|
| 19 |
|
|---|
| 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 |
|
|---|
| 27 | #include <boost/lexical_cast.hpp>
|
|---|
| 28 |
|
|---|
| 29 | using boost::lexical_cast;
|
|---|
| 30 | using boost::bad_lexical_cast;
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | template<typename T> QtQueryListPipe<T>::QtQueryListPipe(std::vector<T> &_content, QtDialog *_dialog, QLineEdit *_inputBox, QListWidget *_inputList, QPushButton *_AddButton, QPushButton *_RemoveButton) :
|
|---|
| 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 |
|
|---|
| 45 | template<typename T> void QtQueryListPipe<T>::elementEntered(const QString& t)
|
|---|
| 46 | {
|
|---|
| 47 | AddButton->setEnabled(t.length() > 0);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | template<typename T> void QtQueryListPipe<T>::elementSelected()
|
|---|
| 51 | {
|
|---|
| 52 | if (inputList->selectedItems().empty())
|
|---|
| 53 | RemoveButton->setEnabled(false);
|
|---|
| 54 | else
|
|---|
| 55 | RemoveButton->setEnabled(true);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | template<typename T> void QtQueryListPipe<T>::addElement() {
|
|---|
| 59 | // type-check
|
|---|
| 60 | std::string text = inputBox->text().toStdString();
|
|---|
| 61 | T value;
|
|---|
| 62 | try {
|
|---|
| 63 | value = boost::lexical_cast<T>(text);
|
|---|
| 64 | } catch (boost::bad_lexical_cast&) {
|
|---|
| 65 | return;
|
|---|
| 66 | };
|
|---|
| 67 | // add item to both
|
|---|
| 68 | inputList->addItem(QString(toString(value).c_str()));
|
|---|
| 69 | addValue(value);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | template<typename T> void QtQueryListPipe<T>::addValue(T item) {
|
|---|
| 73 | content.push_back(item);
|
|---|
| 74 |
|
|---|
| 75 | dialog->update();
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | template<typename T> void QtQueryListPipe<T>::removeElement() {
|
|---|
| 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
|
|---|
| 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);
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | template<typename T> void QtQueryListPipe<T>::removeRow(int row) {
|
|---|
| 91 | int counter = 0;
|
|---|
| 92 | typename std::vector<T>::iterator iter = content.begin();
|
|---|
| 93 | for (; iter != content.end(); ++iter)
|
|---|
| 94 | if (counter++ == row)
|
|---|
| 95 | break;
|
|---|
| 96 | if (iter != content.end())
|
|---|
| 97 | content.erase(iter);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | class StringsQtQueryPipe : public QtQueryListPipe<std::string>
|
|---|
| 101 | {
|
|---|
| 102 | Q_OBJECT
|
|---|
| 103 | public:
|
|---|
| 104 | StringsQtQueryPipe(std::vector<std::string> &_content, QtDialog *_dialog, QLineEdit *_inputBox, QListWidget *_inputList, QPushButton *_AddButton, QPushButton *_RemoveButton) :
|
|---|
| 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 |
|
|---|
| 126 |
|
|---|
| 127 | #endif /* QTQUERYLISTPIPE_HPP_ */
|
|---|