From 72cea006a29539ac064a40198ce9699e28b51679 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 20 Jun 2008 14:55:06 +0000 Subject: Modified combobox to use more complex search and an overall nicer behaviour... testing needed, and maybe more changes. --- client/widgets/combobox.cc | 110 ++++++++++++++++++++++++++++++++++++++------- client/widgets/combobox.h | 6 ++- 2 files changed, 100 insertions(+), 16 deletions(-) diff --git a/client/widgets/combobox.cc b/client/widgets/combobox.cc index aa2d78b..a60f1ed 100644 --- a/client/widgets/combobox.cc +++ b/client/widgets/combobox.cc @@ -26,51 +26,131 @@ */ #include "combobox.h" #include +#include +#include +#include + +typedef enum { + SELECT, + EDIT, + SEARCH +} types_t; ComboBox::ComboBox(QDomNode &node, MacroWindow *macrowindow) : QComboBox(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); + setInsertPolicy(QComboBox::InsertAlphabetically); + if(elem.hasAttribute("width")) { - //resize(elem.attribute("width").toInt(), height()); setMinimumWidth(elem.attribute("width").toInt()); } if(elem.hasAttribute("height")) { - //resize(width(), elem.attribute("height").toInt()); setMinimumHeight(elem.attribute("height").toInt()); } QDomNodeList children = node.childNodes(); - int default_found = 0; + QStringList itemlist; + + setCurrentIndex(-1); // -1 is default for none selected for (int i=0; isetCaseSensitivity(Qt::CaseInsensitive); + completer->setCompletionMode(QCompleter::PopupCompletion); + setCompleter(completer); + setValidator(new QRegExpValidator(rx, this)); + } + connect(this, SIGNAL(editTextChanged(QString)), this, SLOT(changed())); + //setEditText(elem.attribute("value")); + break; + } } QString ComboBox::getValue() { QString value; - if(currentIndex() != -1) value = itemData(currentIndex()).toString(); + + int idx = currentIndex(); + + if(idx != -1 && itemText(idx) == currentText()) value = itemData(idx).toString(); + else value = currentText(); + return value; } + +void ComboBox::setValue(QString value) +{ + int idx = findData(value); + if(idx != -1) setCurrentIndex(idx); +} + +bool ComboBox::isValid() +{ + return rx.exactMatch(currentText()); +} + +void ComboBox::changed() +{ + QPalette palette; + + if(isValid()) { + // valid string + palette.setBrush(QPalette::Base, QBrush(QColor(255, 255, 255))); + } else { + // invalid string + palette.setBrush(QPalette::Base, QBrush(QColor(200, 230, 200))); + } + + setPalette(palette); +} diff --git a/client/widgets/combobox.h b/client/widgets/combobox.h index a79b2aa..9ad2323 100644 --- a/client/widgets/combobox.h +++ b/client/widgets/combobox.h @@ -30,19 +30,23 @@ #include "widget.h" #include #include +#include class ComboBox : public QComboBox, public Widget { +Q_OBJECT public: ComboBox(QDomNode &node, MacroWindow *macrowindow); public slots: bool isValid(); QString getValue(); + void setValue(QString value); + void changed(); private: + QRegExp rx; QString combo_value; - }; #endif/*__PRACRO_COMBOBOX_H__*/ -- cgit v1.2.3