From 5151bc32413efa8d34a21be21fa10f3bf62517c2 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 30 Jun 2008 11:38:19 +0000 Subject: Added setValue and cleaned up the code in general. --- client/widgets/listbox.cc | 98 ++++++++++++++++++++++++++++------------------- client/widgets/listbox.h | 5 +-- 2 files changed, 59 insertions(+), 44 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 + +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); +} diff --git a/client/widgets/listbox.h b/client/widgets/listbox.h index b283bcc..627d31e 100644 --- a/client/widgets/listbox.h +++ b/client/widgets/listbox.h @@ -39,10 +39,7 @@ public: public slots: bool isValid(); QString getValue(); - -private: - QString list_value; - + void setValue(QString value); }; #endif/*__PRACRO_LISTBOX_H__*/ -- cgit v1.2.3