Ignore:
Timestamp:
Jul 29, 2016, 3:14:55 PM (5 years ago)
Author:
Thomas Fuhrmann
Message:

Improve ListWidget? + code cleanup + doxygen comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/mavlink/tools/FlairGCS/src/ListWidget.cpp

    r56 r60  
    1515
    1616ListWidget::ListWidget(Layout *parent, int row, int col, QString name)
    17     : FormLayout(parent, row, col, name, "ListWidget") {
     17    : currentItemRow(-1), isUpToDate(true),
     18      FormLayout(parent, row, col, name, "ListWidget") {
    1819  listwidget = new QListWidget();
     20  // construct the object in 2 times, to have the label above the list
    1921  object_layout->addRow(new QLabel(name));
    2022  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 
     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));
    2826  connect(listwidget, SIGNAL(currentRowChanged(int)), this,
    2927          SLOT(SelectedItemChanged(int)));
    30 
    31   // to avoid duplicates when adding items
     28  // to avoid duplicates when adding items
    3229  SetIsExpandable(true);
    3330}
    3431
    35 ListWidget::ListWidget(Layout *parent, int row, int col, QString name, QStringList& items)
     32ListWidget::ListWidget(Layout *parent, int row, int col, QString name,
     33                       QStringList &items)
    3634    : ListWidget(parent, row, col, name) {
    3735  if (!items.isEmpty()) {
     
    4038}
    4139
    42 ListWidget::~ListWidget() { delete listwidget; }
     40ListWidget::~ListWidget() {
     41  if (listwidget) {
     42    delete listwidget;
     43  }
     44}
    4345
    44 //received from the UAV / FlairCore
    4546void 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   // }
     47  isUpToDate = false;
    5148  if (dom.attribute("item") != "") {
    5249    QString item = dom.attribute("item");
    53     QListWidgetItem* widget_item = new QListWidgetItem(item);
     50    QListWidgetItem *widget_item = new QListWidgetItem(item);
     51    // new item, so the text is red
    5452    widget_item->setForeground(Qt::red);
    5553    listwidget->addItem(widget_item);
     
    5755  if (dom.attribute("delete") != "") {
    5856    int row_to_del = dom.attribute("delete").toInt();
    59     QListWidgetItem* item_to_del = listwidget->takeItem(row_to_del);
     57    QListWidgetItem *item_to_del = listwidget->takeItem(row_to_del);
    6058    if (item_to_del) {
    61       delete item_to_del; 
     59      delete item_to_del;
    6260    }
    6361  }
     
    6765  ui_to_var();
    6866  ui_to_xml();
    69   listwidget->setPalette(list_black_pal);
    70   is_up_to_date = true;
     67  listwidget->setPalette(palListDefault);
     68  isUpToDate = true;
    7169}
    7270
    73 void ListWidget::ui_to_var(void) { current_item_row = listwidget->currentRow(); }
     71void ListWidget::ui_to_var(void) {
     72  currentItemRow = listwidget->currentRow();
     73}
    7474
    7575void ListWidget::ui_to_xml(void) {
    7676  SetValue(QString::number(listwidget->currentRow()));
    77   //add the list of items
    78   for(int count = 0; count < listwidget->count(); count++) {
     77  // add the list of items
     78  for (int count = 0; count < listwidget->count(); count++) {
    7979    std::string item;
    8080    std::ostringstream item_prop;
    8181    item_prop << "item" << count;
    82     SetAttribute(QString::fromStdString(item_prop.str()), listwidget->item(count)->text());
     82    SetAttribute(QString::fromStdString(item_prop.str()),
     83                 listwidget->item(count)->text());
    8384    listwidget->item(count)->setForeground(Qt::black);
    8485  }
     
    8687
    8788void ListWidget::Reset(void) {
    88   if (current_item_row != -1) {
    89     listwidget->setCurrentRow(current_item_row);
     89  if (currentItemRow != -1) {
     90    listwidget->setCurrentRow(currentItemRow);
    9091  }
    9192}
    9293
    9394void 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   // }
     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  }
    110106}
    111107
    112108void ListWidget::SelectedItemChanged(int current_row) {
    113   if (current_row != current_item_row) {
    114     is_up_to_date = false;
    115     listwidget->setPalette(list_red_pal);
     109  if (current_row != currentItemRow) {
     110    isUpToDate = false;
     111    listwidget->setPalette(palListRed);
    116112  } else {
    117     listwidget->setPalette(list_black_pal);
     113    listwidget->setPalette(palListDefault);
    118114  }
    119115}
    120116
    121 bool ListWidget::IsUptodate(void) {
    122   return is_up_to_date;
    123 }
     117bool ListWidget::IsUptodate(void) { return isUpToDate; }
Note: See TracChangeset for help on using the changeset viewer.