All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ConstraintsIndexesListWidget.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/property/ConstraintsIndexesListWidget.cpp
22 
23  \brief This file has the ConstraintsIndexesListWidget class.
24 */
25 
26 // TerraLib
27 #include "../../../common/STLUtils.h"
28 #include "../../../dataaccess/dataset/Constraint.h"
29 #include "../../../dataaccess/dataset/DataSetType.h"
30 #include "../../../dataaccess/dataset/Index.h"
31 #include "../../../dataaccess/dataset/PrimaryKey.h"
32 #include "../../../dataaccess/dataset/UniqueKey.h"
33 #include "../../../datatype/Property.h"
36 #include "ui_ConstraintsIndexesListWidgetForm.h"
37 
38 #define CONSTRAINT_PK_TYPE "Primary Key"
39 #define CONSTRAINT_UK_TYPE "Unique Key"
40 #define CONSTRAINT_UNKNOWN_TYPE "Unknown"
41 #define INDEX_TYPE "Index"
42 
43 
45  : QWidget(parent, f),
46  m_ui(new Ui::ConstraintsIndexesListWidgetForm)
47 {
48  m_ui->setupUi(this);
49 
50 // button icons
51  m_ui->m_addToolButton->setIcon(QIcon::fromTheme("list-add"));
52  m_ui->m_removeToolButton->setIcon(QIcon::fromTheme("list-remove"));
53  m_ui->m_editToolButton->setIcon(QIcon::fromTheme("preferences-system"));
54 
55  connect(m_ui->m_addToolButton, SIGNAL(clicked()), this, SLOT(onAddToolButtonClicked()));
56  connect(m_ui->m_removeToolButton, SIGNAL(clicked()), this, SLOT(onRemoveToolButtonClicked()));
57  connect(m_ui->m_editToolButton, SIGNAL(clicked()), this, SLOT(onEditToolButtonClicked()));
58  connect(m_ui->m_tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(onCellClicked(int, int)));
59 }
60 
62 {
63 }
64 
65 Ui::ConstraintsIndexesListWidgetForm* te::qt::widgets::ConstraintsIndexesListWidget::getForm() const
66 {
67  return m_ui.get();
68 }
69 
71 {
72  m_dsType = dsType;
73 
74  listDataSetProperties();
75 }
76 
78 {
79  if(m_dsType == 0)
80  return;
81 
83 
84  if(w.exec() == QDialog::Accepted)
85  {
86  listDataSetProperties();
87  }
88 }
89 
91 {
92  int row = m_ui->m_tableWidget->currentRow();
93 
94  if(row < 0)
95  return;
96 
97  std::string type = m_ui->m_tableWidget->item(row, 1)->text().toStdString();
98  std::string name = m_ui->m_tableWidget->item(row, 0)->text().toStdString();
99 
100  if(type == CONSTRAINT_PK_TYPE)
101  {
102  removePrimaryKey(name);
103  }
104  else if(type == CONSTRAINT_UK_TYPE)
105  {
106  removeUniqueKey(name);
107  }
108  else if(type == INDEX_TYPE)
109  {
110  removeIndex(name);
111  }
112 
113  m_ui->m_removeToolButton->setEnabled(false);
114 
115  listDataSetProperties();
116 }
117 
119 {
120  if(!m_ui->m_tableWidget->currentItem())
121  return;
122 
123  std::string constIndxName = m_ui->m_tableWidget->item(m_ui->m_tableWidget->currentRow(), 0)->text().toStdString();
124 
126 
127  te::da::PrimaryKey* pk = m_dsType->getPrimaryKey();
128  te::da::UniqueKey* uk = m_dsType->getUniqueKey(constIndxName);
129  te::da::Index* idx = m_dsType->getIndex(constIndxName);
130 
131  if(pk->getName() == constIndxName)
132  {
133  w.setConstraint(m_dsType->getPrimaryKey());
134  }
135  else if(uk)
136  {
137  w.setConstraint(uk);
138  }
139  else if(idx)
140  {
141  w.setIndex(idx);
142  }
143 
144  if(w.exec() == QDialog::Accepted)
145  {
146  listDataSetProperties();
147  }
148 
149 }
150 
152 {
153  m_ui->m_removeToolButton->setEnabled(true);
154 }
155 
157 {
158  bool isEmpty = true;
159 
160  m_ui->m_tableWidget->setRowCount(0);
161 
162  //add primary key info
163  if(m_dsType->getPrimaryKey())
164  {
165  addConstraint(m_dsType->getPrimaryKey());
166  isEmpty = false;
167  }
168 
169  //add unique keys info
170  size_t size = m_dsType->getNumberOfUniqueKeys();
171 
172  for(size_t t = 0; t < size; ++t)
173  {
174  addConstraint(m_dsType->getUniqueKey(t));
175  isEmpty = false;
176  }
177 
178  //add index info
179  size = m_dsType->getNumberOfIndexes();
180 
181  for(size_t t = 0; t < size; ++t)
182  {
183  addIndex(m_dsType->getIndex(t));
184  isEmpty = false;
185  }
186 
187  m_ui->m_tableWidget->resizeColumnsToContents();
188 
189  if(!isEmpty)
190  {
191  m_ui->m_editToolButton->setEnabled(true);
192  }
193 }
194 
196 {
197  std::string name = c->getName();
198  std::string type = "";
199  std::string properties = "";
200 
201  if(c->getType() == te::da::PRIMARY_KEY)
202  {
203  type = tr(CONSTRAINT_PK_TYPE).toStdString();
204  properties = getPropertiesStr(dynamic_cast<te::da::PrimaryKey*>(c)->getProperties());
205  }
206  else if(c->getType() == te::da::UNIQUE_KEY)
207  {
208  type = tr(CONSTRAINT_UK_TYPE).toStdString();
209  properties = getPropertiesStr(dynamic_cast<te::da::UniqueKey*>(c)->getProperties());
210  }
211  else
212  {
213  type = tr(CONSTRAINT_UNKNOWN_TYPE).toStdString();
214  }
215 
216  addTableItem(name, type, properties);
217 }
218 
220 {
221  std::string name = i->getName();
222  std::string type = tr(INDEX_TYPE).toStdString();
223  std::string properties = getPropertiesStr(i->getProperties());
224 
225  addTableItem(name, type, properties);
226 }
227 
229 {
230  te::da::PrimaryKey* pk = m_dsType->getPrimaryKey();
231 
232  if(pk && pk->getName() == name)
233  {
234  m_dsType->remove(pk);
235  }
236 }
237 
239 {
240  size_t size = m_dsType->getNumberOfUniqueKeys();
241 
242  for(size_t t = 0; t < size; ++t)
243  {
244  te::da::UniqueKey* uk = m_dsType->getUniqueKey(t);
245 
246  if(uk->getName() == name)
247  {
248  m_dsType->remove(uk);
249  break;
250  }
251  }
252 }
253 
255 {
256  size_t size = m_dsType->getNumberOfIndexes();
257 
258  for(size_t t = 0; t < size; ++t)
259  {
260  te::da::Index* i = m_dsType->getIndex(t);
261 
262  if(i->getName() == name)
263  {
264  m_dsType->remove(i);
265  break;
266  }
267  }
268 }
269 
270 void te::qt::widgets::ConstraintsIndexesListWidget::addTableItem(std::string name, std::string type, std::string properties)
271 {
272  //new entry
273  int newrow = m_ui->m_tableWidget->rowCount();
274 
275  m_ui->m_tableWidget->insertRow(newrow);
276 
277  QTableWidgetItem* itemName = new QTableWidgetItem(QString::fromStdString(name));
278  m_ui->m_tableWidget->setItem(newrow, 0, itemName);
279  itemName->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
280 
281  QTableWidgetItem* itemType = new QTableWidgetItem(QString::fromStdString(type));
282  m_ui->m_tableWidget->setItem(newrow, 1, itemType);
283  itemType->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
284 
285  QTableWidgetItem* itemProp = new QTableWidgetItem(QString::fromStdString(properties));
286  m_ui->m_tableWidget->setItem(newrow, 2, itemProp);
287  itemProp->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
288 }
289 
290 std::string te::qt::widgets::ConstraintsIndexesListWidget::getPropertiesStr(std::vector<te::dt::Property*> vec)
291 {
292  std::string str = "";
293 
294  for(size_t t = 0; t < vec.size(); ++t)
295  {
296  str += vec[t]->getName();
297 
298  str += "; ";
299  }
300 
301  return str;
302 }
Ui::ConstraintsIndexesListWidgetForm * getForm() const
std::string getPropertiesStr(std::vector< te::dt::Property * > vec)
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::auto_ptr< Ui::ConstraintsIndexesListWidgetForm > m_ui
#define CONSTRAINT_PK_TYPE
#define CONSTRAINT_UK_TYPE
A class used to define a constraint or index property creator.
virtual ConstraintType getType() const =0
It returns the constraint type.
A dialog for creating a constraint or a index property.
ConstraintsIndexesListWidget(QWidget *parent=0, Qt::WindowFlags f=0)
This file has the ConstraintsIndexesListWidget class.
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
void addTableItem(std::string name, std::string type, std::string properties)
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the index.
Definition: Index.h:183
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
#define INDEX_TYPE
#define CONSTRAINT_UNKNOWN_TYPE
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
It describes an index associated to a DataSetType.
Definition: Index.h:54
const std::string & getName() const
It returns the index name.
Definition: Index.h:155