1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 //
4 // This file is part of g2o.
5 //
6 // g2o is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // g2o is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with g2o.  If not, see <http://www.gnu.org/licenses/>.
18 
19 #include "properties_widget.h"
20 
21 #include <QLineEdit>
22 
23 #include <iostream>
24 #include <cassert>
25 
26 #include "g2o/stuff/property.h"
27 
28 using namespace std;
29 
30 using namespace g2o;
31 
PropertiesWidget(QWidget * parent,Qt::WindowFlags f)32 PropertiesWidget::PropertiesWidget(QWidget * parent, Qt::WindowFlags f) :
33   QDialog(parent, f),
34   _properties(0)
35 {
36   setupUi(this);
37 }
38 
~PropertiesWidget()39 PropertiesWidget::~PropertiesWidget()
40 {
41 }
42 
updateDisplayedProperties()43 void PropertiesWidget::updateDisplayedProperties()
44 {
45   tableWidget->clear();
46   _propNames.clear();
47 
48   tableWidget->setColumnCount(2);
49 
50   QStringList horizontalHeaders;
51   horizontalHeaders.append("Name");
52   horizontalHeaders.append("Value");
53   tableWidget->setHorizontalHeaderLabels(horizontalHeaders);
54 
55   tableWidget->verticalHeader()->hide();
56 
57   PropertyMap* properties = _properties;
58   if (! properties)
59     return;
60   tableWidget->setRowCount(properties->size());
61 
62   int r = 0;
63   for (PropertyMap::PropertyMapIterator it = properties->begin(); it != properties->end(); ++it, ++r) {
64 
65     QTableWidgetItem* textItem = new QTableWidgetItem;
66     textItem->setText(QString::fromStdString(humanReadablePropName(it->first)));
67     textItem->setFlags(textItem->flags() & ~Qt::ItemIsEditable);
68     tableWidget->setItem(r, 0, textItem);
69     _propNames.push_back(it->first);
70 
71     if (dynamic_cast<Property<bool>*>(it->second)) {
72       Property<bool>* prop = static_cast<Property<bool>*>(it->second);
73       QTableWidgetItem* checkItem = new QTableWidgetItem;
74       checkItem->setText("enabled");
75       checkItem->setFlags(checkItem->flags() | Qt::ItemIsUserCheckable);
76       if (prop->value())
77         checkItem->setCheckState(Qt::Checked);
78       else
79         checkItem->setCheckState(Qt::Unchecked);
80       tableWidget->setItem(r, 1, checkItem);
81     } else {
82       QLineEdit* editor = new QLineEdit(tableWidget);
83       editor->setText(QString::fromStdString(it->second->toString()));
84       if (dynamic_cast<Property<int>*>(it->second)) {
85         editor->setValidator(new QIntValidator(editor));
86       }
87       else if (dynamic_cast<Property<float>*>(it->second)) {
88         editor->setValidator(new QDoubleValidator(editor));
89       }
90       else if (dynamic_cast<Property<double>*>(it->second)) {
91         editor->setValidator(new QDoubleValidator(editor));
92       }
93       tableWidget->setCellWidget(r, 1, editor);
94     }
95 
96   }
97   tableWidget->resizeColumnToContents(0);
98 }
99 
applyProperties()100 void PropertiesWidget::applyProperties()
101 {
102   assert(tableWidget->rowCount() == (int) _propNames.size());
103   PropertyMap* properties = _properties;
104   for (int r = 0; r < tableWidget->rowCount(); ++r) {
105     const std::string& propName = _propNames[r];
106     BaseProperty* baseProp = properties->getProperty<BaseProperty>(propName);
107     if (! baseProp)
108       continue;
109 
110     if (dynamic_cast<Property<bool>*>(baseProp)) {
111       Property<bool>* prop = static_cast<Property<bool>*>(baseProp);
112       QTableWidgetItem* checkItem = tableWidget->item(r, 1);
113       prop->setValue(checkItem->checkState() == Qt::Checked);
114     } else {
115       QLineEdit* editor = dynamic_cast<QLineEdit*>(tableWidget->cellWidget(r, 1));
116       bool status = baseProp->fromString(editor->text().toStdString());
117       if (! status) {
118         cerr << "Warning: unable to set property " << baseProp->name() << endl;
119       }
120     }
121   }
122 }
123 
on_btnApply_clicked()124 void PropertiesWidget::on_btnApply_clicked()
125 {
126   applyProperties();
127 }
128 
on_btnOK_clicked()129 void PropertiesWidget::on_btnOK_clicked()
130 {
131   applyProperties();
132   close();
133 }
134 
humanReadablePropName(const std::string & propertyName) const135 std::string PropertiesWidget::humanReadablePropName(const std::string& propertyName) const
136 {
137   return propertyName;
138 }
139 
setProperties(PropertyMap * properties)140 void PropertiesWidget::setProperties(PropertyMap* properties)
141 {
142   _properties = properties;
143   updateDisplayedProperties();
144 }
145