diff options
Diffstat (limited to 'client/widgets/listbox.cc')
-rw-r--r-- | client/widgets/listbox.cc | 98 |
1 files changed, 58 insertions, 40 deletions
diff --git a/client/widgets/listbox.cc b/client/widgets/listbox.cc index 3021bd9..4af7952 100644 --- a/client/widgets/listbox.cc +++ b/client/widgets/listbox.cc @@ -27,64 +27,70 @@ #include "listbox.h" #include <QListWidgetItem> + +static QListWidgetItem *createItem(QDomElement &elem) +{ + QListWidgetItem *item = new QListWidgetItem(); + + if(elem.hasAttribute("caption") && + elem.hasAttribute("value")) { + item->setText(elem.attribute("caption")); + item->setData(Qt::UserRole, elem.attribute("value")); + } else { + printf("XML Error!!! Listbox item is missing one or more " + "attributes...\n"); + } + + if(elem.hasAttribute("type")) { + + if(elem.attribute("type") == "header") { + item->setFlags(0); + + QFont headerfont; + headerfont.setBold(true); + headerfont.setItalic(true); + item->setFont(headerfont); + + QBrush headerbrush(Qt::SolidPattern); + headerbrush.setColor(Qt::lightGray); + item->setBackground(headerbrush); + + } + + if(elem.attribute("type") == "separator") { + item->setText(""); + item->setFlags(0); + } + + } + + return item; +} + ListBox::ListBox(QDomNode &node, MacroWindow *macrowindow) : QListWidget(), Widget(node, macrowindow) { QDomElement elem = node.toElement(); 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; for (int i=0; i < children.count(); i++) { QDomNode child = children.at(i); QDomElement list_elem = child.toElement(); - - if(list_elem.hasAttribute("type")) { - if(list_elem.attribute("type") == "header") { - // create header element - QListWidgetItem *header = new - QListWidgetItem(list_elem.attribute("caption")); - //header->setBackground(Qt::HorPattern); - header->setFlags(0); - QFont headerfont; - headerfont.setBold(true); - headerfont.setItalic(true); - header->setFont(headerfont); - QBrush headerbrush(Qt::SolidPattern); - headerbrush.setColor(Qt::lightGray); - header->setBackground(headerbrush); - addItem(header); - } else if (list_elem.attribute("type") == "separator") { - // insert separator - QListWidgetItem *separator = new QListWidgetItem(" "); - //separator->setBackground(Qt::HorPattern); - separator->setFlags(0); - addItem(separator); - } - } else if(list_elem.hasAttribute("caption") && - list_elem.hasAttribute("value")) { - // insert item into current listbox - addItem(list_elem.attribute("caption")); - if(default_found == 0 && elem.attribute("value") == list_elem.attribute("value")) { - //setCurrentRow(count() - 1); - //default_found = 1; - } - } else { - printf("XML Error!!! Listbox item is missing one or more " - "attributes...\n"); - } + addItem(createItem(list_elem)); + } + + if(elem.hasAttribute("value")) { + setValue(elem.attribute("value")); } - if(default_found == 0) setCurrentRow(-1); // -1 is default for none selected } bool ListBox::isValid() @@ -95,6 +101,18 @@ bool ListBox::isValid() QString ListBox::getValue() { QString value = "none"; - if(currentRow() != -1) value = currentItem()->text(); + if(currentRow() != -1) value = currentItem()->data(Qt::UserRole).toString(); return value; } + +void ListBox::setValue(QString value) +{ + int sel = -1; // -1 is default for none selected + + for(int i = 0; i < count(); i++) { + QListWidgetItem *listitem = item(i); + if(listitem->data(Qt::UserRole).toString() == value) sel = i; + } + + setCurrentRow(sel); +} |