All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSetWidget.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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 <QtGui/QIcon>
33 #include <QtGui/QMessageBox>
34 
35 
36 te::qt::widgets::DataSetWidget::DataSetWidget(QWidget* parent, Qt::WindowFlags f)
37  : QWidget(parent, f),
38  m_ui(new Ui::DataSetWidgetForm)
39 {
40  m_ui->setupUi(this);
41 
42  // set icons
43  m_ui->m_addDataSetPushButton->setIcon(QIcon::fromTheme("list-add"));
44  m_ui->m_removeDataSetPushButton->setIcon(QIcon::fromTheme("list-remove"));
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  connect(m_ui->m_removeDataSetPushButton, SIGNAL(clicked()), this, SLOT(onRemoveDataSetPushButtonClicked()));
50 }
51 
53 {
54 }
55 
56 Ui::DataSetWidgetForm* te::qt::widgets::DataSetWidget::getForm() const
57 {
58  return m_ui.get();
59 }
60 
62 {
63  te::da::From* from = new te::da::From;
64 
65  std::vector<std::pair<std::string, std::string> > vec;
66 
67  getDataSetNames(vec);
68 
69  for(size_t t = 0; t < vec.size(); ++t)
70  {
71  te::da::FromItem* fi = new te::da::DataSetName(vec[t].first, vec[t].second);
72 
73  from->push_back(fi);
74  }
75 
76  return from;
77 }
78 
80 {
81  std::vector<std::pair<std::string, std::string> > list;
82 
83  for(std::size_t i = 0; i < from->size(); ++i)
84  {
85  const te::da::FromItem& item = from->at(i);
86 
87  const te::da::DataSetName* dsName = dynamic_cast<const te::da::DataSetName*>(&item);
88 
89  std::pair<std::string, std::string> p;
90  p.first = dsName->getName();
91  p.second = dsName->getAlias();
92  list.push_back(p);
93  }
94 
95  setDataSetNames(list);
96 }
97 
98 void te::qt::widgets::DataSetWidget::setDataSetNames(const std::vector<std::string>& names)
99 {
100  QStringList list;
101 
102  for(size_t t = 0; t < names.size(); ++t)
103  {
104  list.push_back(names[t].c_str());
105  }
106 
107  m_ui->m_dataSetComboBox->clear();
108  m_ui->m_dataSetComboBox->addItems(list);
109 
110  if(names.empty() == false)
111  onDataSetComboBoxActivated(m_ui->m_dataSetComboBox->currentText());
112 }
113 
114 void te::qt::widgets::DataSetWidget::setDataSetNames(std::vector<std::pair<std::string, std::string> >& list)
115 {
116  for(std::size_t i = 0; i < list.size(); ++i)
117  {
118  int newrow = m_ui->m_dataSetTableWidget->rowCount();
119 
120  //check if already exist an alias with this name
121  for(int j = 0; j < newrow; ++j)
122  {
123  QTableWidgetItem* itemName = m_ui->m_dataSetTableWidget->item(j, 1);
124 
125  if(itemName->text().toStdString() == list[j].second)
126  {
127  QMessageBox::warning(this, tr("Query Editor"), tr("Data Set Alias already defined!"));
128  return;
129  }
130  }
131 
132  //new entry
133  m_ui->m_dataSetTableWidget->insertRow(newrow);
134 
135  QTableWidgetItem* itemDataSet = new QTableWidgetItem(QString::fromStdString(list[i].first));
136  m_ui->m_dataSetTableWidget->setItem(newrow, 0, itemDataSet);
137 
138  QTableWidgetItem* itemAlias = new QTableWidgetItem(QString::fromStdString(list[i].second));
139  m_ui->m_dataSetTableWidget->setItem(newrow, 1, itemAlias);
140 
141  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
142 
143  emit itemChanged();
144  }
145 }
146 
147 void te::qt::widgets::DataSetWidget::getDataSetNames(std::vector<std::pair<std::string, std::string> >& list)
148 {
149  int row = m_ui->m_dataSetTableWidget->rowCount();
150 
151  //get properties for each data set
152  for(int i = 0; i < row; ++i)
153  {
154  //alias name
155  QTableWidgetItem* itemAlias = m_ui->m_dataSetTableWidget->item(i, 1);
156  std::string alias = itemAlias->text().toStdString();
157 
158  //data set name
159  QTableWidgetItem* itemDataSet = m_ui->m_dataSetTableWidget->item(i, 0);
160  std::string dataSetName = itemDataSet->text().toStdString();
161 
162  list.push_back(std::pair<std::string, std::string> (dataSetName, alias));
163  }
164 }
165 
167 {
168  if(value.isEmpty() == false)
169  {
170  std::string dataSetName = value.toStdString();
171  std::string aliasName = value.toStdString();
172 
173  int pos = dataSetName.find(".");
174  if(pos != std::string::npos)
175  {
176  aliasName = dataSetName.substr(pos + 1, dataSetName.size() - 1);
177  }
178 
179  m_ui->m_dataSetAliasLineEdit->setText(aliasName.c_str());
180  }
181 }
182 
184 {
185  if(m_ui->m_dataSetComboBox->currentText().isEmpty())
186  {
187  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set not selected."));
188  return;
189  }
190 
191  if(m_ui->m_dataSetAliasLineEdit->text().isEmpty())
192  {
193  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set Alias not defined."));
194  return;
195  }
196 
197  std::string dataSetName = m_ui->m_dataSetComboBox->currentText().toStdString();
198  std::string aliasName = m_ui->m_dataSetAliasLineEdit->text().toStdString();
199 
200  int newrow = m_ui->m_dataSetTableWidget->rowCount();
201 
202  //check if already exist an alias with this name
203  for(int i = 0; i < newrow; ++i)
204  {
205  QTableWidgetItem* itemName = m_ui->m_dataSetTableWidget->item(i, 1);
206 
207  if(itemName->text().toStdString() == aliasName)
208  {
209  QMessageBox::warning(this, tr("Query Builder"), tr("Data Set Alias already defined."));
210  return;
211  }
212  }
213 
214  //new entry
215  m_ui->m_dataSetTableWidget->insertRow(newrow);
216 
217  QTableWidgetItem* itemDataSet = new QTableWidgetItem(QString::fromStdString(dataSetName));
218  m_ui->m_dataSetTableWidget->setItem(newrow, 0, itemDataSet);
219 
220  QTableWidgetItem* itemAlias = new QTableWidgetItem(QString::fromStdString(aliasName));
221  m_ui->m_dataSetTableWidget->setItem(newrow, 1, itemAlias);
222 
223  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
224 
225  emit itemChanged();
226 }
227 
229 {
230  int row = m_ui->m_dataSetTableWidget->currentRow();
231 
232  if(row >= 0)
233  m_ui->m_dataSetTableWidget->removeRow(row);
234 
235  m_ui->m_dataSetTableWidget->resizeColumnToContents(0);
236 
237  emit itemChanged();
238 }
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
Ui::DataSetWidgetForm * getForm() const
const std::string & getName() const
It returns the dataset name.
Definition: DataSetName.cpp:57
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
void setFrom(const te::da::From *from)
void getDataSetNames(std::vector< std::pair< std::string, std::string > > &list)
This file has the DataSetWidget class.
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
const std::string & getAlias() const
It returns the alias associated to the source item.
Definition: FromItem.cpp:47
void onDataSetComboBoxActivated(const QString &value)
DataSetWidget(QWidget *parent=0, Qt::WindowFlags f=0)
std::auto_ptr< Ui::DataSetWidgetForm > m_ui
Definition: DataSetWidget.h:98
void setDataSetNames(const std::vector< std::string > &names)