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

Last change on this file since 98 was 66, checked in by Thomas Fuhrmann, 8 years ago

Add the reset functionality

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