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 "../../../core/translator/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 Ui::ParameterTableWidgetForm* te::qt::widgets::ParameterTableWidget::getForm() const
55 {
56  return m_ui.get();
57 }
58 
59 void te::qt::widgets::ParameterTableWidget::add(const std::string& name, const std::string& value)
60 {
61  int newrow = m_ui->m_paramTableWidget->rowCount();
62 
63  //check if already exist a key with this name
64  for(int i = 0; i < newrow; ++i)
65  {
66  QTableWidgetItem* itemName = m_ui->m_paramTableWidget->item(i, 0);
67 
68  if(itemName->text().toUtf8().data() == name)
69  {
70  QTableWidgetItem* itemValue = new QTableWidgetItem(QString::fromUtf8(value.c_str()));
71  m_ui->m_paramTableWidget->setItem(i, 1, itemValue);
72 
73  return;
74  }
75  }
76 
77  //new entry
78  m_ui->m_paramTableWidget->insertRow(newrow);
79 
80  QTableWidgetItem* itemName = new QTableWidgetItem(QString::fromUtf8(name.c_str()));
81  m_ui->m_paramTableWidget->setItem(newrow, 0, itemName);
82 
83  QTableWidgetItem* itemValue = new QTableWidgetItem(QString::fromUtf8(value.c_str()));
84  m_ui->m_paramTableWidget->setItem(newrow, 1, itemValue);
85 
86  m_ui->m_paramTableWidget->resizeRowsToContents();
87  m_ui->m_paramTableWidget->resizeColumnToContents(0);
88 }
89 
90 std::map<std::string, std::string> te::qt::widgets::ParameterTableWidget::getMap() const
91 {
92  std::map<std::string, std::string> kvpairs;
93 
94  if(m_ui->m_paramTableWidget->columnCount() < 2)
95  throw Exception(TE_TR("Can not get key-value pairs from a table with less than 2 columns!"));
96 
97  int nrows = m_ui->m_paramTableWidget->rowCount();
98 
99  for(int i = 0; i < nrows; ++i)
100  {
101  QTableWidgetItem* itemName = m_ui->m_paramTableWidget->item(i, 0);
102 
103  QTableWidgetItem* itemValue = m_ui->m_paramTableWidget->item(i, 1);
104 
105  kvpairs[itemName->text().toUtf8().data()] = itemValue->text().toUtf8().data();
106  }
107 
108  return kvpairs;
109 }
110 
112 {
113  std::string parameters;
114 
115  if (m_ui->m_paramTableWidget->columnCount() < 2)
116  throw Exception(TE_TR("Can not get key-value pairs from a table with less than 2 columns!"));
117 
118  int nrows = m_ui->m_paramTableWidget->rowCount();
119 
120  for (int i = 0; i < nrows; ++i)
121  {
122  QTableWidgetItem* itemName = m_ui->m_paramTableWidget->item(i, 0);
123 
124  QTableWidgetItem* itemValue = m_ui->m_paramTableWidget->item(i, 1);
125 
126  parameters += std::string(itemName->text().toUtf8().data()) + "=" + std::string(itemValue->text().toUtf8().data()) + "&";
127  }
128 
129  return parameters;
130 }
131 
133 {
134  std::unique_ptr<ParameterDialog> pdialog(new ParameterDialog(this));
135 
136  pdialog->setWindowTitle(tr("Add new parameter"));
137 
138  int retval = pdialog->exec();
139 
140  if(retval == QDialog::Rejected)
141  return;
142 
143  int newrow = m_ui->m_paramTableWidget->rowCount();
144 
145  m_ui->m_paramTableWidget->insertRow(newrow);
146 
147  QTableWidgetItem* itemName = new QTableWidgetItem(pdialog->getName());
148  m_ui->m_paramTableWidget->setItem(newrow, 0, itemName);
149 
150  QTableWidgetItem* itemValue = new QTableWidgetItem(pdialog->getValue());
151  m_ui->m_paramTableWidget->setItem(newrow, 1, itemValue);
152 
153  m_ui->m_paramTableWidget->resizeRowsToContents();
154  m_ui->m_paramTableWidget->resizeColumnToContents(0);
155 }
156 
158 {
159  int row = m_ui->m_paramTableWidget->currentRow();
160 
161  if(row >= 0)
162  m_ui->m_paramTableWidget->removeRow(row);
163 
164  m_ui->m_paramTableWidget->resizeRowsToContents();
165  m_ui->m_paramTableWidget->resizeColumnToContents(0);
166 }
167 
169 {
170  int row = m_ui->m_paramTableWidget->currentRow();
171 
172  if(row < 0)
173  return;
174 
175  std::unique_ptr<ParameterDialog> pdialog(new ParameterDialog(this));
176 
177  pdialog->setWindowTitle(tr("Edit parameter"));
178 
179  QTableWidgetItem* itemName = m_ui->m_paramTableWidget->item(row, 0);
180 
181  if(itemName != nullptr)
182  pdialog->setName(itemName->text());
183 
184  QTableWidgetItem* itemValue = m_ui->m_paramTableWidget->item(row, 1);
185 
186  if(itemValue != nullptr)
187  pdialog->setValue(itemValue->text());
188 
189  int retval = pdialog->exec();
190 
191  if(retval == QDialog::Rejected)
192  return;
193 
194  itemName->setText(pdialog->getName());
195  itemValue->setText(pdialog->getValue());
196 
197  m_ui->m_paramTableWidget->update();
198 
199  m_ui->m_paramTableWidget->resizeRowsToContents();
200  m_ui->m_paramTableWidget->resizeColumnToContents(0);
201 }
202 
A dialog to be used as input of key/value pairs.
Base exception class for plugin module.
Ui::ParameterTableWidgetForm * getForm() const
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::map< std::string, std::string > getMap() const
void add(const std::string &name, const std::string &value)
ParameterTableWidget(QWidget *parent=0, Qt::WindowFlags f=0)
std::unique_ptr< Ui::ParameterTableWidgetForm > m_ui
A dialog to be used as input of key/value pairs.