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

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

Management of lists using red/black colors

File size: 3.4 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 : FormLayout(parent, row, col, name, "ListWidget") {
18 listwidget = new QListWidget();
19 object_layout->addRow(new QLabel(name));
20 object_layout->addRow(listwidget);
21
22 list_red_pal.setColor(QPalette::HighlightedText, QColor(255, 0, 0));
23 list_black_pal.setColor(QPalette::HighlightedText, QColor(255, 255, 255));
24
25 current_item_row = -1;
26 is_up_to_date = true;
27
28 connect(listwidget, SIGNAL(currentRowChanged(int)), this,
29 SLOT(SelectedItemChanged(int)));
30
31 // to avoid duplicates when adding items
32 SetIsExpandable(true);
33}
34
35ListWidget::ListWidget(Layout *parent, int row, int col, QString name, QStringList& items)
36 : ListWidget(parent, row, col, name) {
37 if (!items.isEmpty()) {
38 listwidget->addItems(items);
39 }
40}
41
42ListWidget::~ListWidget() { delete listwidget; }
43
44//received from the UAV / FlairCore
45void ListWidget::XmlEvent(QDomElement dom) {
46 is_up_to_date = false;
47 // if (dom.attribute("value") != "") {
48 // int new_row = dom.attribute("value");
49 // listwidget->setCurrentRow(new_row);
50 // }
51 if (dom.attribute("item") != "") {
52 QString item = dom.attribute("item");
53 QListWidgetItem* widget_item = new QListWidgetItem(item);
54 widget_item->setForeground(Qt::red);
55 listwidget->addItem(widget_item);
56 }
57 if (dom.attribute("delete") != "") {
58 int row_to_del = dom.attribute("delete").toInt();
59 QListWidgetItem* item_to_del = listwidget->takeItem(row_to_del);
60 if (item_to_del) {
61 delete item_to_del;
62 }
63 }
64}
65
66void ListWidget::SetUptodate(void) {
67 ui_to_var();
68 ui_to_xml();
69 listwidget->setPalette(list_black_pal);
70 is_up_to_date = true;
71}
72
73void ListWidget::ui_to_var(void) { current_item_row = listwidget->currentRow(); }
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()), listwidget->item(count)->text());
83 listwidget->item(count)->setForeground(Qt::black);
84 }
85}
86
87void ListWidget::Reset(void) {
88 if (current_item_row != -1) {
89 listwidget->setCurrentRow(current_item_row);
90 }
91}
92
93void ListWidget::LoadEvent(QDomElement dom) {
94 std::cout << "In LoadEvent" << std::endl;
95 // Parse the item list
96 // QStringList items;
97 // int count = 0;
98 // while (dom.hasAttribute("item" + QString::number(count))) {
99 // QString item = dom.attribute("item" + QString::number(count));
100 // std::cout << "Item" << count << " : " << item.toStdString() << std::endl;
101 // items.append(item);
102 // count++;
103 // }
104
105 // listwidget->addItems(items);
106
107 // if (listwidget->isEnabled() == true) {
108 // listwidget->setCurrentIndex((dom.attribute("value")).toInt());
109 // }
110}
111
112void ListWidget::SelectedItemChanged(int current_row) {
113 if (current_row != current_item_row) {
114 is_up_to_date = false;
115 listwidget->setPalette(list_red_pal);
116 } else {
117 listwidget->setPalette(list_black_pal);
118 }
119}
120
121bool ListWidget::IsUptodate(void) {
122 return is_up_to_date;
123}
Note: See TracBrowser for help on using the repository browser.