DataSetWidget.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/query/DataSetWidget.cpp
22 
23  \brief This file has the DataSetWidget class.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/query/DataSetName.h"
28 #include "DataSetWidget.h"
29 #include "ui_DataSetWidgetForm.h"
30 
31 // Qt
32 #include <QIcon>
33 #include <QMessageBox>
34 #include <QToolButton>
35 
36 
38  : QWidget(parent, f),
39  m_ui(new Ui::DataSetWidgetForm)
40 {
41  m_ui->setupUi(this);
42 
43  // set icons
44  m_ui->m_addDataSetPushButton->setIcon(QIcon::fromTheme("list-add"));
45 
46  //connects
47  connect(m_ui->m_dataSetComboBox, SIGNAL(activated(const QString&)), this, SLOT(onDataSetComboBoxActivated(const QString&)));
48  connect(m_ui->m_addDataSetPushButton, SIGNAL(clicked()), this, SLOT(onAddDataSetPushButtonClicked()));
49 }
50 
52 
53 Ui::DataSetWidgetForm* te::qt::widgets::DataSetWidget::getForm() const
54 {
55  return m_ui.get();
56 }
57 
59 {
60  te::da::From* from = new te::da::From;
61 
62  std::vector<std::pair<std::string, std::string> > vec;
63 
64  getDataSetNames(vec);
65 
66  for(size_t t = 0; t < vec.size(); ++t)
67  {
68  te::da::FromItem* fi = new te::da::DataSetName(vec[t].first, vec[t].second);
69 
70  from->push_back(fi);
71  }
72 
73  return from;
74 }
75 
77 {
78  std::vector<std::pair<std::string, std::string> > list;
79 
80  for(std::size_t i = 0; i < from->size(); ++i)
81  {
82  const te::da::FromItem& item = from->at(i);
83 
84  const te::da::DataSetName* dsName = dynamic_cast<const te::da::DataSetName*>(&item);
85 
86  std::pair<std::string, std::string> p;
87  p.first = dsName->getName();
88  p.second = dsName->getAlias();
89  list.push_back(p);
90  }
91 
92  setDataSetNames(list);
93 }
94 
95 void te::qt::widgets::DataSetWidget::setDataSetNames(const std::vector<std::string>& names)
96 {
97  QStringList list;
98 
99  for(size_t t = 0; t < names.size(); ++t)
100  {
101  list.push_back(names[t].c_str());
102  }
103 
104  m_ui->m_dataSetComboBox->clear();
105  m_ui->m_dataSetComboBox->addItems(list);
106 
107  if(names.empty() == false)
108  onDataSetComboBoxActivated(m_ui->m_dataSetComboBox->currentText());
109 }
110 
111 void te::qt::widgets::DataSetWidget::setDataSetNames(std::vector<std::pair<std::string, std::string> >& list)
112 {
113  for(std::size_t i = 0; i < list.size(); ++i)
114  {
115  int newrow = m_ui->m_dataSetTableWidget->rowCount();
116 
117  //check if already exist an alias with this name
118  for(int j = 0; j < newrow; ++j)
119  {
120  QTableWidgetItem* itemName = m_ui->m_dataSetTableWidget->item(j, 2);
121 
122  if(itemName->text().toUtf8().data() == list[j].second)
123  {
124  QMessageBox::warning(this, tr("Query Editor"), tr("Data Set Alias already defined!"));
125  return;
126  }
127  }
128 
129  //new entry
130  m_ui->m_dataSetTableWidget->insertRow(newrow);
131 
132  //remove button
133  QToolButton* removeBtn = new QToolButton(m_ui->m_dataSetTableWidget);
134  removeBtn->setIcon(QIcon::fromTheme("list-remove"));
135  connect(removeBtn, SIGNAL(clicked()), this, SLOT(onRemoveDataSetPushButtonClicked()));
136  m_ui->m_dataSetTableWidget->setCellWidget(newrow, 0, removeBtn);
137 
138  QTableWidgetItem* itemDataSet = new QTableWidgetItem(QString::fromUtf8(list[i].first.c_str()));
139  m_ui->m_dataSetTableWidget->setItem(newrow, 1, itemDataSet);
140 
141  QTableWidgetItem* itemAlias = new QTableWidgetItem(QString::fromUtf8(list[i].second.c_str()));
142  m_ui->m_dataSetTableWidget->setItem(newrow, 2, itemAlias);
143 
144  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
145 
146  emit itemChanged();
147  }
148 }
149 
150 void te::qt::widgets::DataSetWidget::getDataSetNames(std::vector<std::pair<std::string, std::string> >& list)
151 {
152  int row = m_ui->m_dataSetTableWidget->rowCount();
153 
154  //get properties for each data set
155  for(int i = 0; i < row; ++i)
156  {
157  //alias name
158  QTableWidgetItem* itemAlias = m_ui->m_dataSetTableWidget->item(i, 2);
159  std::string alias = itemAlias->text().toUtf8().data();
160 
161  //data set name
162  QTableWidgetItem* itemDataSet = m_ui->m_dataSetTableWidget->item(i, 1);
163  std::string dataSetName = itemDataSet->text().toUtf8().data();
164 
165  list.push_back(std::pair<std::string, std::string> (dataSetName, alias));
166  }
167 }
168 
170 {
171  if(value.isEmpty() == false)
172  {
173  std::string dataSetName = value.toUtf8().data();
174  std::string aliasName = value.toUtf8().data();
175 
176  std::size_t pos = dataSetName.find(".");
177  if(pos != std::string::npos)
178  {
179  aliasName = dataSetName.substr(pos + 1, dataSetName.size() - 1);
180  }
181 
182  m_ui->m_dataSetAliasLineEdit->setText(aliasName.c_str());
183  }
184 }
185 
187 {
188  if(m_ui->m_dataSetComboBox->currentText().isEmpty())
189  {
190  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set not selected."));
191  return;
192  }
193 
194  if(m_ui->m_dataSetAliasLineEdit->text().isEmpty())
195  {
196  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set Alias not defined."));
197  return;
198  }
199 
200  std::string dataSetName = m_ui->m_dataSetComboBox->currentText().toUtf8().data();
201  std::string aliasName = m_ui->m_dataSetAliasLineEdit->text().toUtf8().data();
202 
203  int newrow = m_ui->m_dataSetTableWidget->rowCount();
204 
205  //check if already exist an alias with this name
206  for(int i = 0; i < newrow; ++i)
207  {
208  QTableWidgetItem* itemName = m_ui->m_dataSetTableWidget->item(i, 2);
209 
210  if(itemName->text().toUtf8().data() == aliasName)
211  {
212  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set Alias already defined."));
213  return;
214  }
215  }
216 
217  //new entry
218  m_ui->m_dataSetTableWidget->insertRow(newrow);
219 
220  //remove button
221  QToolButton* removeBtn = new QToolButton(m_ui->m_dataSetTableWidget);
222  removeBtn->setIcon(QIcon::fromTheme("list-remove"));
223  connect(removeBtn, SIGNAL(clicked()), this, SLOT(onRemoveDataSetPushButtonClicked()));
224  m_ui->m_dataSetTableWidget->setCellWidget(newrow, 0, removeBtn);
225 
226  QTableWidgetItem* itemDataSet = new QTableWidgetItem(QString::fromUtf8(dataSetName.c_str()));
227  m_ui->m_dataSetTableWidget->setItem(newrow, 1, itemDataSet);
228 
229  QTableWidgetItem* itemAlias = new QTableWidgetItem(QString::fromUtf8(aliasName.c_str()));
230  m_ui->m_dataSetTableWidget->setItem(newrow, 2, itemAlias);
231 
232  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
233 
234  emit itemChanged();
235 }
236 
238 {
239  QToolButton* button = dynamic_cast<QToolButton*>(sender());
240  if(button)
241  {
242  int row = -1;
243 
244  for(int i = 0; i < m_ui->m_dataSetTableWidget->rowCount(); ++i)
245  {
246  QWidget* w = m_ui->m_dataSetTableWidget->cellWidget(i, 0);
247 
248  if(button == w)
249  {
250  row = i;
251  break;
252  }
253  }
254 
255  if(row >= 0)
256  m_ui->m_dataSetTableWidget->removeRow(row);
257 
258  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
259  }
260  emit itemChanged();
261 }
void getDataSetNames(std::vector< std::pair< std::string, std::string > > &list)
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
void setDataSetNames(const std::vector< std::string > &names)
void setFrom(const te::da::From *from)
std::unique_ptr< Ui::DataSetWidgetForm > m_ui
const std::string & getAlias() const
It returns the alias associated to the source item.
Definition: FromItem.cpp:47
void onDataSetComboBoxActivated(const QString &value)
te::gm::Polygon * p
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
This file has the DataSetWidget class.
DataSetWidget(QWidget *parent=0, Qt::WindowFlags f=0)
const std::string & getName() const
It returns the dataset name.
Definition: DataSetName.cpp:55
Ui::DataSetWidgetForm * getForm() const