All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ParameterTableWidget.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/utils/ParameterTableWidget.cpp
22 
23  \brief ...
24 */
25 
26 // TerraLib
27 #include "../../../common/Translator.h"
28 #include "../Exception.h"
29 #include "ParameterDialog.h"
30 #include "ParameterTableWidget.h"
31 #include "ui_ParameterDialogForm.h"
32 #include "ui_ParameterTableWidgetForm.h"
33 
34 // Qt
35 #include <QIcon>
36 
38  : QWidget(parent, f),
39  m_ui(new Ui::ParameterTableWidgetForm)
40 {
41  m_ui->setupUi(this);
42 
43  m_ui->m_addItemToolButton->setIcon(QIcon::fromTheme("list-add"));
44  m_ui->m_removeItemToolButton->setIcon(QIcon::fromTheme("list-remove"));
45  m_ui->m_editItemToolButton->setIcon(QIcon::fromTheme("preferences-system"));
46 
47  connect(m_ui->m_addItemToolButton, SIGNAL(pressed()), this, SLOT(addToolButtonPressed()));
48  connect(m_ui->m_removeItemToolButton, SIGNAL(pressed()), this, SLOT(removeToolButtonPressed()));
49  connect(m_ui->m_editItemToolButton, SIGNAL(pressed()), this, SLOT(editToolButtonPressed()));
50 }
51 
53 {
54 }
55 
56 Ui::ParameterTableWidgetForm* te::qt::widgets::ParameterTableWidget::getForm() const
57 {
58  return m_ui.get();
59 }
60 
61 void te::qt::widgets::ParameterTableWidget::add(const std::string& name, const std::string& value)
62 {
63  int newrow = m_ui->m_paramTableWidget->rowCount();
64 
65  //check if already exist a key with this name
66  for(int i = 0; i < newrow; ++i)
67  {
68  QTableWidgetItem* itemName = m_ui->m_paramTableWidget->item(i, 0);
69 
70  if(itemName->text().toStdString() == name)
71  {
72  QTableWidgetItem* itemValue = new QTableWidgetItem(QString::fromStdString(value));
73  m_ui->m_paramTableWidget->setItem(i, 1, itemValue);
74 
75  return;
76  }
77  }
78 
79  //new entry
80  m_ui->m_paramTableWidget->insertRow(newrow);
81 
82  QTableWidgetItem* itemName = new QTableWidgetItem(QString::fromStdString(name));
83  m_ui->m_paramTableWidget->setItem(newrow, 0, itemName);
84 
85  QTableWidgetItem* itemValue = new QTableWidgetItem(QString::fromStdString(value));
86  m_ui->m_paramTableWidget->setItem(newrow, 1, itemValue);
87 
88  m_ui->m_paramTableWidget->resizeRowsToContents();
89  m_ui->m_paramTableWidget->resizeColumnToContents(0);
90 }
91 
92 std::map<std::string, std::string> te::qt::widgets::ParameterTableWidget::getMap() const
93 {
94  std::map<std::string, std::string> kvpairs;
95 
96  if(m_ui->m_paramTableWidget->columnCount() < 2)
97  throw Exception(TE_TR("Can not get key-value pairs from a table with less than 2 columns!"));
98 
99  int nrows = m_ui->m_paramTableWidget->rowCount();
100 
101  for(int i = 0; i < nrows; ++i)
102  {
103  QTableWidgetItem* itemName = m_ui->m_paramTableWidget->item(i, 0);
104 
105  QTableWidgetItem* itemValue = m_ui->m_paramTableWidget->item(i, 1);
106 
107  kvpairs[itemName->text().toStdString()] = itemValue->text().toStdString();
108  }
109 
110  return kvpairs;
111 }
112 
114 {
115  std::auto_ptr<ParameterDialog> pdialog(new ParameterDialog(this));
116 
117  pdialog->setWindowTitle(tr("Add new parameter"));
118 
119  int retval = pdialog->exec();
120 
121  if(retval == QDialog::Rejected)
122  return;
123 
124  int newrow = m_ui->m_paramTableWidget->rowCount();
125 
126  m_ui->m_paramTableWidget->insertRow(newrow);
127 
128  QTableWidgetItem* itemName = new QTableWidgetItem(pdialog->getName());
129  m_ui->m_paramTableWidget->setItem(newrow, 0, itemName);
130 
131  QTableWidgetItem* itemValue = new QTableWidgetItem(pdialog->getValue());
132  m_ui->m_paramTableWidget->setItem(newrow, 1, itemValue);
133 
134  m_ui->m_paramTableWidget->resizeRowsToContents();
135  m_ui->m_paramTableWidget->resizeColumnToContents(0);
136 }
137 
139 {
140  int row = m_ui->m_paramTableWidget->currentRow();
141 
142  if(row >= 0)
143  m_ui->m_paramTableWidget->removeRow(row);
144 
145  m_ui->m_paramTableWidget->resizeRowsToContents();
146  m_ui->m_paramTableWidget->resizeColumnToContents(0);
147 }
148 
150 {
151  int row = m_ui->m_paramTableWidget->currentRow();
152 
153  if(row < 0)
154  return;
155 
156  std::auto_ptr<ParameterDialog> pdialog(new ParameterDialog(this));
157 
158  pdialog->setWindowTitle(tr("Edit parameter"));
159 
160  QTableWidgetItem* itemName = m_ui->m_paramTableWidget->item(row, 0);
161 
162  if(itemName != 0)
163  pdialog->setName(itemName->text());
164 
165  QTableWidgetItem* itemValue = m_ui->m_paramTableWidget->item(row, 1);
166 
167  if(itemValue != 0)
168  pdialog->setValue(itemValue->text());
169 
170  int retval = pdialog->exec();
171 
172  if(retval == QDialog::Rejected)
173  return;
174 
175  itemName->setText(pdialog->getName());
176  itemValue->setText(pdialog->getValue());
177 
178  m_ui->m_paramTableWidget->update();
179 
180  m_ui->m_paramTableWidget->resizeRowsToContents();
181  m_ui->m_paramTableWidget->resizeColumnToContents(0);
182 }
183 
A dialog to be used as input of key/value pairs.
Ui::ParameterTableWidgetForm * getForm() const
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
std::map< std::string, std::string > getMap() const
void add(const std::string &name, const std::string &value)
std::auto_ptr< Ui::ParameterTableWidgetForm > m_ui
ParameterTableWidget(QWidget *parent=0, Qt::WindowFlags f=0)
A dialog to be used as input of key/value pairs.