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  default;
63 
64 Ui::ConstraintsIndexesListWidgetForm* te::qt::widgets::ConstraintsIndexesListWidget::getForm() const
65 {
66  return m_ui.get();
67 }
68 
70 {
71  m_dsType = dsType;
72 
74 }
75 
77 {
78  if(m_dsType == nullptr)
79  return;
80 
82 
83  if(w.exec() == QDialog::Accepted)
84  {
86 
87  emit constraintsChanged();
88  }
89 }
90 
92 {
93  int row = m_ui->m_tableWidget->currentRow();
94 
95  if(row < 0)
96  return;
97 
98  std::string type = m_ui->m_tableWidget->item(row, 1)->text().toUtf8().data();
99  std::string name = m_ui->m_tableWidget->item(row, 0)->text().toUtf8().data();
100 
101  if(type == CONSTRAINT_PK_TYPE)
102  {
103  removePrimaryKey(name);
104  }
105  else if(type == CONSTRAINT_UK_TYPE)
106  {
107  removeUniqueKey(name);
108  }
109  else if(type == INDEX_TYPE)
110  {
111  removeIndex(name);
112  }
113 
114  m_ui->m_removeToolButton->setEnabled(false);
115 
117 
118  emit constraintsChanged();
119 }
120 
122 {
123  if(!m_ui->m_tableWidget->currentItem())
124  return;
125 
126  std::string constIndxName = m_ui->m_tableWidget->item(m_ui->m_tableWidget->currentRow(), 0)->text().toUtf8().data();
127 
129 
131  te::da::UniqueKey* uk = m_dsType->getUniqueKey(constIndxName);
132  te::da::Index* idx = m_dsType->getIndex(constIndxName);
133 
134  if(pk->getName() == constIndxName)
135  {
137  }
138  else if(uk)
139  {
140  w.setConstraint(uk);
141  }
142  else if(idx)
143  {
144  w.setIndex(idx);
145  }
146 
147  if(w.exec() == QDialog::Accepted)
148  {
150 
151  emit constraintsChanged();
152  }
153 
154 }
155 
157  int /*col*/)
158 {
159  m_ui->m_removeToolButton->setEnabled(true);
160 }
161 
163 {
164  bool isEmpty = true;
165 
166  m_ui->m_tableWidget->setRowCount(0);
167 
168  //add primary key info
169  if(m_dsType->getPrimaryKey())
170  {
172  isEmpty = false;
173  }
174 
175  //add unique keys info
176  size_t size = m_dsType->getNumberOfUniqueKeys();
177 
178  for(size_t t = 0; t < size; ++t)
179  {
181  isEmpty = false;
182  }
183 
184  //add index info
185  size = m_dsType->getNumberOfIndexes();
186 
187  for(size_t t = 0; t < size; ++t)
188  {
190  isEmpty = false;
191  }
192 
193  m_ui->m_tableWidget->resizeColumnsToContents();
194 
195  if(!isEmpty)
196  {
197  m_ui->m_editToolButton->setEnabled(true);
198  }
199 }
200 
202 {
203  std::string name = c->getName();
204  std::string type;
205  std::string properties;
206 
207  if(c->getType() == te::da::PRIMARY_KEY)
208  {
209  type = tr(CONSTRAINT_PK_TYPE).toUtf8().data();
210  properties = getPropertiesStr(dynamic_cast<te::da::PrimaryKey*>(c)->getProperties());
211  }
212  else if(c->getType() == te::da::UNIQUE_KEY)
213  {
214  type = tr(CONSTRAINT_UK_TYPE).toUtf8().data();
215  properties = getPropertiesStr(dynamic_cast<te::da::UniqueKey*>(c)->getProperties());
216  }
217  else
218  {
219  type = tr(CONSTRAINT_UNKNOWN_TYPE).toUtf8().data();
220  }
221 
222  addTableItem(name, type, properties);
223 }
224 
226 {
227  std::string name = i->getName();
228  std::string type = tr(INDEX_TYPE).toUtf8().data();
229  std::string properties = getPropertiesStr(i->getProperties());
230 
231  addTableItem(name, type, properties);
232 }
233 
235 {
237 
238  if(pk && pk->getName() == name)
239  {
240  m_dsType->remove(pk);
241  }
242 }
243 
245 {
246  size_t size = m_dsType->getNumberOfUniqueKeys();
247 
248  for(size_t t = 0; t < size; ++t)
249  {
251 
252  if(uk->getName() == name)
253  {
254  m_dsType->remove(uk);
255  break;
256  }
257  }
258 }
259 
261 {
262  size_t size = m_dsType->getNumberOfIndexes();
263 
264  for(size_t t = 0; t < size; ++t)
265  {
267 
268  if(i->getName() == name)
269  {
270  m_dsType->remove(i);
271  break;
272  }
273  }
274 }
275 
276 void te::qt::widgets::ConstraintsIndexesListWidget::addTableItem(std::string name, std::string type, std::string properties)
277 {
278  //new entry
279  int newrow = m_ui->m_tableWidget->rowCount();
280 
281  m_ui->m_tableWidget->insertRow(newrow);
282 
283  QTableWidgetItem* itemName = new QTableWidgetItem(QString::fromUtf8(name.c_str()));
284  m_ui->m_tableWidget->setItem(newrow, 0, itemName);
285  itemName->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
286 
287  QTableWidgetItem* itemType = new QTableWidgetItem(QString::fromUtf8(type.c_str()));
288  m_ui->m_tableWidget->setItem(newrow, 1, itemType);
289  itemType->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
290 
291  QTableWidgetItem* itemProp = new QTableWidgetItem(QString::fromUtf8(properties.c_str()));
292  m_ui->m_tableWidget->setItem(newrow, 2, itemProp);
293  itemProp->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
294 }
295 
296 std::string te::qt::widgets::ConstraintsIndexesListWidget::getPropertiesStr(std::vector<te::dt::Property*> vec)
297 {
298  std::string str;
299 
300  for(size_t t = 0; t < vec.size(); ++t)
301  {
302  str += vec[t]->getName();
303 
304  str += "; ";
305  }
306 
307  return str;
308 }
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::size_t getNumberOfUniqueKeys() const
It returns the number of unique keys defined for the dataset type.
Definition: DataSetType.h:269
PrimaryKey * getPrimaryKey() const
It returns the primary key associated to the dataset type.
Definition: DataSetType.h:214
#define CONSTRAINT_PK_TYPE
#define CONSTRAINT_UK_TYPE
A class used to define a constraint or index property creator.
Index * getIndex(std::size_t i) const
It returns the i-th index associated to the dataset type.
Definition: DataSetType.h:428
A dialog for creating a constraint or a index property.
virtual ConstraintType getType() const =0
It returns the constraint type.
void remove(Constraint *c)
It removes the constraint.
ConstraintsIndexesListWidget(QWidget *parent=0, Qt::WindowFlags f=0)
This file has the ConstraintsIndexesListWidget class.
std::size_t getNumberOfIndexes() const
It returns the number of indexes defined for the dataset type.
Definition: DataSetType.h:391
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.
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.
std::unique_ptr< Ui::ConstraintsIndexesListWidgetForm > m_ui
UniqueKey * getUniqueKey(std::size_t i) const
It returns the i-th unique key.
Definition: DataSetType.h:294
const std::string & getName() const
It returns the index name.