All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
37 te::qt::widgets::DataSetWidget::DataSetWidget(QWidget* parent, Qt::WindowFlags f)
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 }
54 
55 Ui::DataSetWidgetForm* te::qt::widgets::DataSetWidget::getForm() const
56 {
57  return m_ui.get();
58 }
59 
61 {
62  te::da::From* from = new te::da::From;
63 
64  std::vector<std::pair<std::string, std::string> > vec;
65 
66  getDataSetNames(vec);
67 
68  for(size_t t = 0; t < vec.size(); ++t)
69  {
70  te::da::FromItem* fi = new te::da::DataSetName(vec[t].first, vec[t].second);
71 
72  from->push_back(fi);
73  }
74 
75  return from;
76 }
77 
79 {
80  std::vector<std::pair<std::string, std::string> > list;
81 
82  for(std::size_t i = 0; i < from->size(); ++i)
83  {
84  const te::da::FromItem& item = from->at(i);
85 
86  const te::da::DataSetName* dsName = dynamic_cast<const te::da::DataSetName*>(&item);
87 
88  std::pair<std::string, std::string> p;
89  p.first = dsName->getName();
90  p.second = dsName->getAlias();
91  list.push_back(p);
92  }
93 
94  setDataSetNames(list);
95 }
96 
97 void te::qt::widgets::DataSetWidget::setDataSetNames(const std::vector<std::string>& names)
98 {
99  QStringList list;
100 
101  for(size_t t = 0; t < names.size(); ++t)
102  {
103  list.push_back(names[t].c_str());
104  }
105 
106  m_ui->m_dataSetComboBox->clear();
107  m_ui->m_dataSetComboBox->addItems(list);
108 
109  if(names.empty() == false)
110  onDataSetComboBoxActivated(m_ui->m_dataSetComboBox->currentText());
111 }
112 
113 void te::qt::widgets::DataSetWidget::setDataSetNames(std::vector<std::pair<std::string, std::string> >& list)
114 {
115  for(std::size_t i = 0; i < list.size(); ++i)
116  {
117  int newrow = m_ui->m_dataSetTableWidget->rowCount();
118 
119  //check if already exist an alias with this name
120  for(int j = 0; j < newrow; ++j)
121  {
122  QTableWidgetItem* itemName = m_ui->m_dataSetTableWidget->item(j, 2);
123 
124  if(itemName->text().toStdString() == list[j].second)
125  {
126  QMessageBox::warning(this, tr("Query Editor"), tr("Data Set Alias already defined!"));
127  return;
128  }
129  }
130 
131  //new entry
132  m_ui->m_dataSetTableWidget->insertRow(newrow);
133 
134  //remove button
135  QToolButton* removeBtn = new QToolButton(m_ui->m_dataSetTableWidget);
136  removeBtn->setIcon(QIcon::fromTheme("list-remove"));
137  connect(removeBtn, SIGNAL(clicked()), this, SLOT(onRemoveDataSetPushButtonClicked()));
138  m_ui->m_dataSetTableWidget->setCellWidget(newrow, 0, removeBtn);
139 
140  QTableWidgetItem* itemDataSet = new QTableWidgetItem(QString::fromStdString(list[i].first));
141  m_ui->m_dataSetTableWidget->setItem(newrow, 1, itemDataSet);
142 
143  QTableWidgetItem* itemAlias = new QTableWidgetItem(QString::fromStdString(list[i].second));
144  m_ui->m_dataSetTableWidget->setItem(newrow, 2, itemAlias);
145 
146  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
147 
148  emit itemChanged();
149  }
150 }
151 
152 void te::qt::widgets::DataSetWidget::getDataSetNames(std::vector<std::pair<std::string, std::string> >& list)
153 {
154  int row = m_ui->m_dataSetTableWidget->rowCount();
155 
156  //get properties for each data set
157  for(int i = 0; i < row; ++i)
158  {
159  //alias name
160  QTableWidgetItem* itemAlias = m_ui->m_dataSetTableWidget->item(i, 2);
161  std::string alias = itemAlias->text().toStdString();
162 
163  //data set name
164  QTableWidgetItem* itemDataSet = m_ui->m_dataSetTableWidget->item(i, 1);
165  std::string dataSetName = itemDataSet->text().toStdString();
166 
167  list.push_back(std::pair<std::string, std::string> (dataSetName, alias));
168  }
169 }
170 
172 {
173  if(value.isEmpty() == false)
174  {
175  std::string dataSetName = value.toStdString();
176  std::string aliasName = value.toStdString();
177 
178  int pos = dataSetName.find(".");
179  if(pos != std::string::npos)
180  {
181  aliasName = dataSetName.substr(pos + 1, dataSetName.size() - 1);
182  }
183 
184  m_ui->m_dataSetAliasLineEdit->setText(aliasName.c_str());
185  }
186 }
187 
189 {
190  if(m_ui->m_dataSetComboBox->currentText().isEmpty())
191  {
192  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set not selected."));
193  return;
194  }
195 
196  if(m_ui->m_dataSetAliasLineEdit->text().isEmpty())
197  {
198  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set Alias not defined."));
199  return;
200  }
201 
202  std::string dataSetName = m_ui->m_dataSetComboBox->currentText().toStdString();
203  std::string aliasName = m_ui->m_dataSetAliasLineEdit->text().toStdString();
204 
205  int newrow = m_ui->m_dataSetTableWidget->rowCount();
206 
207  //check if already exist an alias with this name
208  for(int i = 0; i < newrow; ++i)
209  {
210  QTableWidgetItem* itemName = m_ui->m_dataSetTableWidget->item(i, 2);
211 
212  if(itemName->text().toStdString() == aliasName)
213  {
214  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set Alias already defined."));
215  return;
216  }
217  }
218 
219  //new entry
220  m_ui->m_dataSetTableWidget->insertRow(newrow);
221 
222  //remove button
223  QToolButton* removeBtn = new QToolButton(m_ui->m_dataSetTableWidget);
224  removeBtn->setIcon(QIcon::fromTheme("list-remove"));
225  connect(removeBtn, SIGNAL(clicked()), this, SLOT(onRemoveDataSetPushButtonClicked()));
226  m_ui->m_dataSetTableWidget->setCellWidget(newrow, 0, removeBtn);
227 
228  QTableWidgetItem* itemDataSet = new QTableWidgetItem(QString::fromStdString(dataSetName));
229  m_ui->m_dataSetTableWidget->setItem(newrow, 1, itemDataSet);
230 
231  QTableWidgetItem* itemAlias = new QTableWidgetItem(QString::fromStdString(aliasName));
232  m_ui->m_dataSetTableWidget->setItem(newrow, 2, itemAlias);
233 
234  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
235 
236  emit itemChanged();
237 }
238 
240 {
241  QToolButton* button = dynamic_cast<QToolButton*>(sender());
242  if(button)
243  {
244  int row = -1;
245 
246  for(int i = 0; i < m_ui->m_dataSetTableWidget->rowCount(); ++i)
247  {
248  QWidget* w = m_ui->m_dataSetTableWidget->cellWidget(i, 0);
249  QToolButton* btn = dynamic_cast<QToolButton*>(w);
250  if(button == w)
251  {
252  row = i;
253  break;
254  }
255  }
256 
257  if(row >= 0)
258  m_ui->m_dataSetTableWidget->removeRow(row);
259 
260  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
261  }
262  emit itemChanged();
263 }
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
std::auto_ptr< Ui::DataSetWidgetForm > m_ui
Definition: DataSetWidget.h:98
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)
const std::string & getAlias() const
It returns the alias associated to the source item.
Definition: FromItem.cpp:47
void onDataSetComboBoxActivated(const QString &value)
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:57
Ui::DataSetWidgetForm * getForm() const