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
RevLine 
[48]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>
[56]10#include <QListWidgetItem>
11#include <QPalette>
[48]12
13#include <iostream>
[56]14#include <sstream>
[48]15
16ListWidget::ListWidget(Layout *parent, int row, int col, QString name)
[60]17 : currentItemRow(-1), isUpToDate(true),
18 FormLayout(parent, row, col, name, "ListWidget") {
[48]19 listwidget = new QListWidget();
[60]20 // construct the object in 2 times, to have the label above the list
[48]21 object_layout->addRow(new QLabel(name));
22 object_layout->addRow(listwidget);
[60]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));
[56]26 connect(listwidget, SIGNAL(currentRowChanged(int)), this,
27 SLOT(SelectedItemChanged(int)));
[60]28 // to avoid duplicates when adding items
[48]29 SetIsExpandable(true);
30}
31
[60]32ListWidget::ListWidget(Layout *parent, int row, int col, QString name,
33 QStringList &items)
[54]34 : ListWidget(parent, row, col, name) {
35 if (!items.isEmpty()) {
36 listwidget->addItems(items);
37 }
38}
39
[60]40ListWidget::~ListWidget() {
41 if (listwidget) {
42 delete listwidget;
43 }
44}
[48]45
46void ListWidget::XmlEvent(QDomElement dom) {
[60]47 isUpToDate = false;
[48]48 if (dom.attribute("item") != "") {
49 QString item = dom.attribute("item");
[60]50 QListWidgetItem *widget_item = new QListWidgetItem(item);
51 // new item, so the text is red
[56]52 widget_item->setForeground(Qt::red);
53 listwidget->addItem(widget_item);
[48]54 }
55 if (dom.attribute("delete") != "") {
[56]56 int row_to_del = dom.attribute("delete").toInt();
[60]57 QListWidgetItem *item_to_del = listwidget->takeItem(row_to_del);
[56]58 if (item_to_del) {
[60]59 delete item_to_del;
[54]60 }
[48]61 }
62}
63
64void ListWidget::SetUptodate(void) {
[56]65 ui_to_var();
66 ui_to_xml();
[60]67 listwidget->setPalette(palListDefault);
68 isUpToDate = true;
[48]69}
70
[60]71void ListWidget::ui_to_var(void) {
72 currentItemRow = listwidget->currentRow();
73}
[48]74
[56]75void ListWidget::ui_to_xml(void) {
76 SetValue(QString::number(listwidget->currentRow()));
[60]77 // add the list of items
78 for (int count = 0; count < listwidget->count(); count++) {
[56]79 std::string item;
80 std::ostringstream item_prop;
81 item_prop << "item" << count;
[60]82 SetAttribute(QString::fromStdString(item_prop.str()),
83 listwidget->item(count)->text());
[56]84 listwidget->item(count)->setForeground(Qt::black);
85 }
86}
[48]87
[56]88void ListWidget::Reset(void) {
[60]89 if (currentItemRow != -1) {
90 listwidget->setCurrentRow(currentItemRow);
[56]91 }
[48]92}
93
94void ListWidget::LoadEvent(QDomElement dom) {
[60]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 }
[48]106}
107
[56]108void ListWidget::SelectedItemChanged(int current_row) {
[60]109 if (current_row != currentItemRow) {
110 isUpToDate = false;
111 listwidget->setPalette(palListRed);
[56]112 } else {
[60]113 listwidget->setPalette(palListDefault);
[56]114 }
115}
116
[60]117bool ListWidget::IsUptodate(void) { return isUpToDate; }
Note: See TracBrowser for help on using the repository browser.