1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | #include "ListWidget.h"
|
---|
6 | #include "Layout.h"
|
---|
7 | #include <QListWidget>
|
---|
8 | #include <QFormLayout>
|
---|
9 | #include <QLabel>
|
---|
10 | #include <QListWidgetItem>
|
---|
11 | #include <QPalette>
|
---|
12 | #include <sstream>
|
---|
13 |
|
---|
14 | // #include <iostream>
|
---|
15 |
|
---|
16 | ListWidget::ListWidget(Layout *parent, int row, int col, QString name)
|
---|
17 | : currentItemRow(-1), isUpToDate(true),
|
---|
18 | FormLayout(parent, row, col, name, "ListWidget") {
|
---|
19 | listwidget = new QListWidget();
|
---|
20 | // construct the object in 2 times, to have the label above the list
|
---|
21 | object_layout->addRow(new QLabel(name));
|
---|
22 | object_layout->addRow(listwidget);
|
---|
23 | // modify the color palette to have the selected item text in red
|
---|
24 | palListRed.setColor(QPalette::HighlightedText, QColor(255, 0, 0));
|
---|
25 | palListDefault.setColor(QPalette::HighlightedText, QColor(255, 255, 255));
|
---|
26 | connect(listwidget, SIGNAL(currentRowChanged(int)), this,
|
---|
27 | SLOT(SelectedItemChanged(int)));
|
---|
28 | // to avoid duplicates when adding items
|
---|
29 | SetIsExpandable(true);
|
---|
30 | }
|
---|
31 |
|
---|
32 | ListWidget::ListWidget(Layout *parent, int row, int col, QString name,
|
---|
33 | QStringList &items)
|
---|
34 | : ListWidget(parent, row, col, name) {
|
---|
35 | if (!items.isEmpty()) {
|
---|
36 | listwidget->addItems(items);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | ListWidget::~ListWidget() {
|
---|
41 | if (listwidget) {
|
---|
42 | delete listwidget;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | void ListWidget::XmlEvent(QDomElement dom) {
|
---|
47 | isUpToDate = false;
|
---|
48 | if (dom.attribute("item") != "") {
|
---|
49 | QString item = dom.attribute("item");
|
---|
50 | QListWidgetItem *widget_item = new QListWidgetItem(item);
|
---|
51 | // new item, so the text is red
|
---|
52 | widget_item->setForeground(Qt::red);
|
---|
53 | listwidget->addItem(widget_item);
|
---|
54 | }
|
---|
55 | if (dom.attribute("delete") != "") {
|
---|
56 | int row_to_del = dom.attribute("delete").toInt();
|
---|
57 | QListWidgetItem *item_to_del = listwidget->takeItem(row_to_del);
|
---|
58 | if (item_to_del) {
|
---|
59 | delete item_to_del;
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | void ListWidget::SetUptodate(void) {
|
---|
65 | ui_to_var();
|
---|
66 | ui_to_xml();
|
---|
67 | listwidget->setPalette(palListDefault);
|
---|
68 | isUpToDate = true;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void ListWidget::ui_to_var(void) {
|
---|
72 | currentItemRow = listwidget->currentRow();
|
---|
73 | }
|
---|
74 |
|
---|
75 | void ListWidget::ui_to_xml(void) {
|
---|
76 | SetValue(QString::number(listwidget->currentRow()));
|
---|
77 | // add the list of items
|
---|
78 | for (int count = 0; count < listwidget->count(); count++) {
|
---|
79 | std::string item;
|
---|
80 | std::ostringstream item_prop;
|
---|
81 | item_prop << "item" << count;
|
---|
82 | SetAttribute(QString::fromStdString(item_prop.str()),
|
---|
83 | listwidget->item(count)->text());
|
---|
84 | listwidget->item(count)->setForeground(Qt::black);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | void ListWidget::Reset(void) {
|
---|
89 | if (currentItemRow != -1) {
|
---|
90 | listwidget->setCurrentRow(currentItemRow);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | void ListWidget::LoadEvent(QDomElement dom) {
|
---|
95 | //function not tested
|
---|
96 | // std::cout << "FlairGCS LoadEvent" << std::endl;
|
---|
97 | if (listwidget->isEnabled() == true) {
|
---|
98 | // std::cout << "FlairGCS LoadEvent loading" << std::endl;
|
---|
99 | listwidget->clear();
|
---|
100 | QStringList items;
|
---|
101 | int count = 0;
|
---|
102 | while (dom.hasAttribute("item" + QString::number(count))) {
|
---|
103 | listwidget->addItem(dom.attribute("item" + QString::number(count)));
|
---|
104 | count++;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void ListWidget::SelectedItemChanged(int current_row) {
|
---|
110 | if (current_row != currentItemRow) {
|
---|
111 | isUpToDate = false;
|
---|
112 | listwidget->setPalette(palListRed);
|
---|
113 | } else {
|
---|
114 | listwidget->setPalette(palListDefault);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | bool ListWidget::IsUptodate(void) { return isUpToDate; }
|
---|