All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConstraintsIndexesListWidget.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/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_tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(onCellClicked(int, int)));
58 }
59 
61 {
62 }
63 
64 Ui::ConstraintsIndexesListWidgetForm* te::qt::widgets::ConstraintsIndexesListWidget::getForm() const
65 {
66  return m_ui.get();
67 }
68 
70 {
71  m_dsType = dsType;
72 
73  listDataSetProperties();
74 }
75 
77 {
78  if(m_dsType == 0)
79  return;
80 
82 
83  if(w.exec() == QDialog::Accepted)
84  {
85  listDataSetProperties();
86  }
87 }
88 
90 {
91  int row = m_ui->m_tableWidget->currentRow();
92 
93  if(row < 0)
94  return;
95 
96  std::string type = m_ui->m_tableWidget->item(row, 1)->text().toStdString();
97  std::string name = m_ui->m_tableWidget->item(row, 0)->text().toStdString();
98 
99  if(type == CONSTRAINT_PK_TYPE)
100  {
101  removePrimaryKey(name);
102  }
103  else if(type == CONSTRAINT_UK_TYPE)
104  {
105  removeUniqueKey(name);
106  }
107  else if(type == INDEX_TYPE)
108  {
109  removeIndex(name);
110  }
111 
112  m_ui->m_removeToolButton->setEnabled(false);
113 
114  listDataSetProperties();
115 }
116 
118 {
119  //TODO
120 }
121 
123 {
124  m_ui->m_removeToolButton->setEnabled(true);
125 }
126 
128 {
129  m_ui->m_tableWidget->setRowCount(0);
130 
131  //add primary key info
132  if(m_dsType->getPrimaryKey())
133  {
134  addConstraint(m_dsType->getPrimaryKey());
135  }
136 
137  //add unique keys info
138  size_t size = m_dsType->getNumberOfUniqueKeys();
139 
140  for(size_t t = 0; t < size; ++t)
141  {
142  addConstraint(m_dsType->getUniqueKey(t));
143  }
144 
145  //add index info
146  size = m_dsType->getNumberOfIndexes();
147 
148  for(size_t t = 0; t < size; ++t)
149  {
150  addIndex(m_dsType->getIndex(t));
151  }
152 
153  m_ui->m_tableWidget->resizeColumnsToContents();
154 }
155 
157 {
158  std::string name = c->getName();
159  std::string type = "";
160  std::string properties = "";
161 
162  if(c->getType() == te::da::PRIMARY_KEY)
163  {
164  type = tr(CONSTRAINT_PK_TYPE).toStdString();
165  properties = getPropertiesStr(dynamic_cast<te::da::PrimaryKey*>(c)->getProperties());
166  }
167  else if(c->getType() == te::da::UNIQUE_KEY)
168  {
169  type = tr(CONSTRAINT_UK_TYPE).toStdString();
170  properties = getPropertiesStr(dynamic_cast<te::da::UniqueKey*>(c)->getProperties());
171  }
172  else
173  {
174  type = tr(CONSTRAINT_UNKNOWN_TYPE).toStdString();
175  }
176 
177  addTableItem(name, type, properties);
178 }
179 
181 {
182  std::string name = i->getName();
183  std::string type = tr(INDEX_TYPE).toStdString();
184  std::string properties = getPropertiesStr(i->getProperties());
185 
186  addTableItem(name, type, properties);
187 }
188 
190 {
191  te::da::PrimaryKey* pk = m_dsType->getPrimaryKey();
192 
193  if(pk && pk->getName() == name)
194  {
195  m_dsType->remove(pk);
196  }
197 }
198 
200 {
201  size_t size = m_dsType->getNumberOfUniqueKeys();
202 
203  for(size_t t = 0; t < size; ++t)
204  {
205  te::da::UniqueKey* uk = m_dsType->getUniqueKey(t);
206 
207  if(uk->getName() == name)
208  {
209  m_dsType->remove(uk);
210  break;
211  }
212  }
213 }
214 
216 {
217  size_t size = m_dsType->getNumberOfIndexes();
218 
219  for(size_t t = 0; t < size; ++t)
220  {
221  te::da::Index* i = m_dsType->getIndex(t);
222 
223  if(i->getName() == name)
224  {
225  m_dsType->remove(i);
226  break;
227  }
228  }
229 }
230 
231 void te::qt::widgets::ConstraintsIndexesListWidget::addTableItem(std::string name, std::string type, std::string properties)
232 {
233  //new entry
234  int newrow = m_ui->m_tableWidget->rowCount();
235 
236  m_ui->m_tableWidget->insertRow(newrow);
237 
238  QTableWidgetItem* itemName = new QTableWidgetItem(QString::fromStdString(name));
239  m_ui->m_tableWidget->setItem(newrow, 0, itemName);
240 
241  QTableWidgetItem* itemType = new QTableWidgetItem(QString::fromStdString(type));
242  m_ui->m_tableWidget->setItem(newrow, 1, itemType);
243 
244  QTableWidgetItem* itemProp = new QTableWidgetItem(QString::fromStdString(properties));
245  m_ui->m_tableWidget->setItem(newrow, 2, itemProp);
246 }
247 
248 std::string te::qt::widgets::ConstraintsIndexesListWidget::getPropertiesStr(std::vector<te::dt::Property*> vec)
249 {
250  std::string str = "";
251 
252  for(size_t t = 0; t < vec.size(); ++t)
253  {
254  str += vec[t]->getName();
255 
256  str += "; ";
257  }
258 
259  return str;
260 }
It describes an index associated to a DataSetType.
Definition: Index.h:54
std::string getPropertiesStr(std::vector< te::dt::Property * > vec)
A class used to define a constraint or index property creator.
#define CONSTRAINT_UK_TYPE
#define CONSTRAINT_UNKNOWN_TYPE
void addTableItem(std::string name, std::string type, std::string properties)
It describes a primary key (pk) constraint.
Definition: PrimaryKey.h:52
const std::vector< te::dt::Property * > & getProperties() const
It returns the properties that take part of the index.
Definition: Index.h:183
#define INDEX_TYPE
ConstraintsIndexesListWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Ui::ConstraintsIndexesListWidgetForm * getForm() const
#define CONSTRAINT_PK_TYPE
const std::string & getName() const
It returns the index name.
Definition: Index.h:155
It describes a unique key (uk) constraint.
Definition: UniqueKey.h:53
virtual const std::string & getName() const
It returns the constraint name.
Definition: Constraint.h:119
This file has the ConstraintsIndexesListWidget class.
A class that models the description of a dataset.
Definition: DataSetType.h:72
A dialog for creating a constraint or a index property.
virtual ConstraintType getType() const =0
It returns the constraint type.
std::auto_ptr< Ui::ConstraintsIndexesListWidgetForm > m_ui