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

Last change on this file since 64 was 64, checked in by Thomas Fuhrmann, 5 years ago

Save config on target and save all locally are now working

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