/* * QtQueryListPipe.hpp * * Created on: Oct 25, 2010 * Author: heber */ #ifndef QTQUERYLISTPIPE_HPP_ #define QTQUERYLISTPIPE_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Parameters/Parameter.hpp" #include "QtQueryPipe.hpp" // Qt moc needs these because it generates its own .cpp file from QtQueryListPipe.hpp. #include #include #include #include #include #include using boost::lexical_cast; using boost::bad_lexical_cast; template QtQueryListPipe::QtQueryListPipe(std::vector &_content, QtDialog *_dialog, QLineEdit *_inputBox, QListWidget *_inputList, QPushButton *_AddButton, QPushButton *_RemoveButton) : content(_content), dialog(_dialog), inputBox(_inputBox), inputList(_inputList), AddButton(_AddButton), RemoveButton(_RemoveButton) {} template QtQueryListPipe::~QtQueryListPipe() {} template void QtQueryListPipe::elementEntered(const QString& t) { AddButton->setEnabled(t.length() > 0); } template void QtQueryListPipe::elementSelected() { if (inputList->selectedItems().empty()) RemoveButton->setEnabled(false); else RemoveButton->setEnabled(true); } template void QtQueryListPipe::addElement() { // type-check std::string text = inputBox->text().toStdString(); T value; try { value = boost::lexical_cast(text); } catch (boost::bad_lexical_cast&) { return; }; // add item to both inputList->addItem(QString(toString(value).c_str())); addValue(value); } template void QtQueryListPipe::addValue(T item) { content.push_back(item); dialog->update(); } template void QtQueryListPipe::removeElement() { QList items = inputList->selectedItems(); for (QList::iterator iter = items.begin(); !items.empty(); iter = items.begin()) { // obtain which position item has (by making it current item) inputList->setCurrentItem(*iter); // remove QtQueryListPipe::removeRow(inputList->currentRow()); // template parameters needs to be known, such that compiler knows which to call inputList->takeItem(inputList->currentRow()); items.erase(iter); } } template void QtQueryListPipe::removeRow(int row) { int counter = 0; typename std::vector::iterator iter = content.begin(); for (; iter != content.end(); ++iter) if (counter++ == row) break; if (iter != content.end()) content.erase(iter); } class StringsQtQueryPipe : public QtQueryListPipe { Q_OBJECT public: StringsQtQueryPipe(std::vector &_content, QtDialog *_dialog, QLineEdit *_inputBox, QListWidget *_inputList, QPushButton *_AddButton, QPushButton *_RemoveButton) : QtQueryListPipe(_content, _dialog, _inputBox, _inputList, _AddButton, _RemoveButton) { } public slots: virtual void elementEntered(const QString& t) { QtQueryListPipe::elementEntered(t); } virtual void elementSelected() { QtQueryListPipe::elementSelected(); } virtual void addElement() { QtQueryListPipe::addElement(); } virtual void removeElement() { QtQueryListPipe::removeElement(); } }; #endif /* QTQUERYLISTPIPE_HPP_ */