summaryrefslogtreecommitdiff
path: root/client/widgets
diff options
context:
space:
mode:
Diffstat (limited to 'client/widgets')
-rw-r--r--client/widgets/combobox.cc110
-rw-r--r--client/widgets/combobox.h6
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 <QDomNodeList>
+#include <QCompleter>
+#include <QRegExpValidator>
+#include <QRegExp>
+
+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; i<children.count();i++) {
QDomNode child = children.at(i);
QDomElement combo_elem = child.toElement();
-
if(combo_elem.hasAttribute("caption") && combo_elem.hasAttribute("value")) {
- // insert item into current combobox
addItem(combo_elem.attribute("caption"), combo_elem.attribute("value"));
- if(elem.attribute("value") == combo_elem.attribute("value")) {
- setCurrentIndex(count() - 1);
- default_found = 1;
- }
+ itemlist << combo_elem.attribute("caption");
} else {
printf("XML Error!!! Combobox item is missing one or more attributes...\n");
}
}
- if(default_found == 0) setCurrentIndex(-1); // -1 is default for none selected
-}
-bool ComboBox::isValid()
-{
- return currentIndex() != -1;
+ if(elem.hasAttribute("value")) {
+ setValue(elem.attribute("value"));
+ }
+
+
+ // addItems(itemlist);
+
+ types_t combotype = SELECT;
+ if(elem.hasAttribute("type")) {
+ if(elem.attribute("type") == "select") combotype = SELECT;
+ if(elem.attribute("type") == "edit") combotype = EDIT;
+ if(elem.attribute("type") == "search") combotype = SEARCH;
+ }
+
+ switch(combotype) {
+ case SELECT:
+ setEditable(false);
+ //connect(this, SIGNAL(currentIndexChanged(QString)), this, SLOT(changed()));
+ break;
+
+ case EDIT:
+ setEditable(true);
+ connect(this, SIGNAL(editTextChanged(QString)), this, SLOT(changed()));
+ //setEditText(elem.attribute("value"));
+ break;
+
+ case SEARCH:
+ setEditable(true);
+ {
+ QString rxs = "(";
+ for(int i = 0; i < itemlist.size(); i++) {
+ if(rxs != "(") rxs += "|";
+ rxs += itemlist.at(i);
+ }
+ rxs += ")";
+ rx = QRegExp(rxs);
+ }
+ {
+ QCompleter *completer = new QCompleter(itemlist, this);
+ completer->setCaseSensitivity(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 <QDomNode>
#include <QComboBox>
+#include <QRegExp>
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__*/