source: flair-src/trunk/tools/FlairGCS/src/ListWidget.cpp@ 218

Last change on this file since 218 was 88, checked in by Sanahuja Guillaume, 8 years ago

m

File size: 4.5 KB
Line 
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 <QStringList>
13#include <sstream>
14
15//to remove, for tests only
16#include <iostream>
17
18ListWidget::ListWidget(Layout *parent, int row, int col, QString name)
19 : currentItemRow(-1), isUpToDate(true),
20 FormLayout(parent, row, col, name, "ListWidget") {
21 listwidget = new QListWidget();
22 if (internalItemsList == NULL) {
23 internalItemsList = new QStringList();
24 }
25 // construct the object in 2 times, to have the label above the list
26 object_layout->addRow(new QLabel(name));
27 object_layout->addRow(listwidget);
28 // modify the color palette to have the selected item text in red
29 palListRed.setColor(QPalette::HighlightedText, QColor(255, 0, 0));
30 palListDefault.setColor(QPalette::HighlightedText, QColor(255, 255, 255));
31 connect(listwidget, SIGNAL(currentRowChanged(int)), this,
32 SLOT(SelectedItemChanged(int)));
33 // to avoid duplicates when adding items
34 SetIsExpandable(true);
35}
36
37ListWidget::ListWidget(Layout *parent, int row, int col, QString name,
38 QStringList &items)
39 : ListWidget(parent, row, col, name) {
40 if (!items.isEmpty()) {
41 listwidget->addItems(items);
42 internalItemsList = new QStringList(items);
43 }
44
45}
46
47ListWidget::~ListWidget() {
48 if (listwidget) {
49 delete listwidget;
50 }
51}
52
53void ListWidget::XmlEvent(QDomElement dom) {
54 isUpToDate = false;
55 if (dom.attribute("item") != "") {
56 QString item = dom.attribute("item");
57 QListWidgetItem *widget_item = new QListWidgetItem(item);
58 // new item, so the text is red
59 widget_item->setForeground(Qt::red);
60 listwidget->addItem(widget_item);
61 }
62 if (dom.attribute("delete") != "") {
63 int row_to_del = dom.attribute("delete").toInt();
64 RemoveItemFromXml(row_to_del);
65 QListWidgetItem *item_to_del = listwidget->takeItem(row_to_del);
66 if (item_to_del) {
67 delete item_to_del;
68 }
69 }
70}
71
72void ListWidget::SetUptodate(void) {
73 ui_to_var();
74 ui_to_xml();
75 listwidget->setPalette(palListDefault);
76 isUpToDate = true;
77}
78
79void ListWidget::ui_to_var(void) {
80 currentItemRow = listwidget->currentRow();
81 internalItemsList->clear();
82 for (int count = 0; count < listwidget->count(); count++) {
83 internalItemsList->append(listwidget->item(count)->text());
84 }
85}
86
87void ListWidget::ui_to_xml(void) {
88 SetValue(QString::number(listwidget->currentRow()));
89 // add the list of items
90 for (int count = 0; count < listwidget->count(); count++) {
91 std::string item;
92 std::ostringstream item_prop;
93 item_prop << "item" << count;
94 SetAttribute(QString::fromStdString(item_prop.str()),
95 listwidget->item(count)->text());
96 listwidget->item(count)->setForeground(Qt::black);
97 }
98}
99
100void ListWidget::Reset(void) {
101 if (currentItemRow != -1) {
102 listwidget->setCurrentRow(currentItemRow);
103 }
104 listwidget->clear();
105 listwidget->addItems(*internalItemsList);
106}
107
108void ListWidget::LoadEvent(QDomElement dom) {
109 //function not tested
110 std::cout << "FlairGCS LoadEvent" << std::endl;
111 if (listwidget->isEnabled() == true) {
112 std::cout << "FlairGCS LoadEvent loading" << std::endl;
113 listwidget->clear();
114 QStringList items;
115 int count = 0;
116 while (dom.hasAttribute("item" + QString::number(count))) {
117 std::cout << "FlairGCS LoadEvent boucle" << std::endl;
118 listwidget->addItem(dom.attribute("item" + QString::number(count)));
119 count++;
120 }
121 }
122}
123
124void ListWidget::SelectedItemChanged(int current_row) {
125 if (current_row != currentItemRow) {
126 isUpToDate = false;
127 listwidget->setPalette(palListRed);
128 } else {
129 listwidget->setPalette(palListDefault);
130 }
131}
132
133bool ListWidget::IsUptodate(void) { return isUpToDate; }
134
135void ListWidget::RemoveItemFromXml(int row_to_del) {
136 // Shift the elements in the XML file to have the one to delete at the end
137 std::ostringstream item_prop;
138 for (int count = row_to_del; count < listwidget->count() - 1; count++) {
139 item_prop << "item" << count;
140 SetAttribute(QString::fromStdString(item_prop.str()),
141 listwidget->item(count + 1)->text());
142 // Clear the value of the stringstream
143 item_prop.str(std::string());
144 }
145 // Delete the last element from the XML file
146 item_prop << "item" << (listwidget->count() - 1);
147 RemoveAttribute(QString::fromStdString(item_prop.str()));
148}
Note: See TracBrowser for help on using the repository browser.