source: flair-src/branches/mavlink/tools/FlairGCS/src/ListWidget.cpp@ 60

Last change on this file since 60 was 60, checked in by Thomas Fuhrmann, 6 years ago

Improve ListWidget + code cleanup + doxygen comments

File size: 3.3 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
13#include <iostream>
14#include <sstream>
15
16ListWidget::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
32ListWidget::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
40ListWidget::~ListWidget() {
41 if (listwidget) {
42 delete listwidget;
43 }
44}
45
46void 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
64void ListWidget::SetUptodate(void) {
65 ui_to_var();
66 ui_to_xml();
67 listwidget->setPalette(palListDefault);
68 isUpToDate = true;
69}
70
71void ListWidget::ui_to_var(void) {
72 currentItemRow = listwidget->currentRow();
73}
74
75void 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
88void ListWidget::Reset(void) {
89 if (currentItemRow != -1) {
90 listwidget->setCurrentRow(currentItemRow);
91 }
92}
93
94void ListWidget::LoadEvent(QDomElement dom) {
95 std::cout << "FlairGCS LoadEvent" << std::endl;
96 if (listwidget->isEnabled() == true) {
97 std::cout << "FlairGCS LoadEvent loading" << std::endl;
98 listwidget->clear();
99 QStringList items;
100 int count = 0;
101 while (dom.hasAttribute("item" + QString::number(count))) {
102 listwidget->addItem(dom.attribute("item" + QString::number(count)));
103 count++;
104 }
105 }
106}
107
108void ListWidget::SelectedItemChanged(int current_row) {
109 if (current_row != currentItemRow) {
110 isUpToDate = false;
111 listwidget->setPalette(palListRed);
112 } else {
113 listwidget->setPalette(palListDefault);
114 }
115}
116
117bool ListWidget::IsUptodate(void) { return isUpToDate; }
Note: See TracBrowser for help on using the repository browser.