All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSetTableModel.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/table/DataSetTableModel.cpp
22 
23  \brief A model based on te::da::DataSet.
24 */
25 
26 // TerraLib
27 #include "../../../common/Exception.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/dataset/ObjectIdSet.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 
32 #include "DataSetTableModel.h"
33 #include "Promoter.h"
34 
35 // Qt
36 #include <QtGui/QIcon>
37 
38 bool IsPkey(const int& column, const std::vector<size_t>& pkeys)
39 {
40  std::vector<size_t>::const_iterator it;
41 
42  for(it=pkeys.begin(); it!=pkeys.end(); ++it)
43  if(*it == column)
44  return true;
45 
46  return false;
47 }
48 
49 QString columnType(const int& type)
50 {
51 
52  switch(type)
53  {
54  case te::dt::VOID_TYPE:
55  return QObject::tr("Void");
56  break;
57 
58  case te::dt::BIT_TYPE:
59  return QObject::tr("Bit");
60  break;
61 
62  case te::dt::CHAR_TYPE:
63  return QObject::tr("Char");
64  break;
65 
66  case te::dt::UCHAR_TYPE:
67  return QObject::tr("Unsigned char");
68  break;
69 
70  case te::dt::INT16_TYPE:
71  case te::dt::INT32_TYPE:
72  case te::dt::INT64_TYPE:
73  return QObject::tr("Integer");
74  break;
75 
79  return QObject::tr("Unsigned integer");
80  break;
81 
83  return QObject::tr("Boolean");
84  break;
85 
86  case te::dt::FLOAT_TYPE:
87  return QObject::tr("Float");
88  break;
89 
91  return QObject::tr("Double");
92  break;
93 
95  return QObject::tr("Numeric");
96  break;
97 
99  return QObject::tr("String");
100  break;
101 
103  return QObject::tr("Byte array");
104  break;
105 
107  return QObject::tr("Geometry");
108  break;
109 
111  return QObject::tr("Date time");
112  break;
113 
114  default:
115  return QObject::tr("Unknown");
116  break;
117  }
118 }
119 
121  : QAbstractTableModel(parent),
122  m_dataset(0),
123  m_currentRow(-1),
124  m_OIdsVisible(true),
125  m_enabled(true)
126 {
127  m_promoter = new Promoter;
128 }
129 
131 {
132  delete m_dataset;
133  delete m_promoter;
134 }
135 
137 {
138  beginResetModel();
139 
140  delete m_dataset;
141 
142  m_dataset = dset;
143 
144  endResetModel();
145 }
146 
147 void te::qt::widgets::DataSetTableModel::setPkeysColumns(const std::vector<size_t>& pkeys)
148 {
149  m_pkeysColumns = pkeys;
150 }
151 
153 {
154  beginResetModel();
155 
156  m_promoter->promote(oids);
157 
158  endResetModel();
159 }
160 
162 {
163  return m_promoter;
164 }
165 
167 {
168  m_OIdsVisible = visible;
169 }
170 
172 {
174 
175  // Mounting oidset
176  std::vector<size_t>::iterator it;
177 
178  for(it=m_pkeysColumns.begin(); it!=m_pkeysColumns.end(); ++it)
179  oids->addProperty(m_dataset->getPropertyName(*it), *it, m_dataset->getPropertyDataType(*it));
180 
181  // Loading oid set.
182  int row;
183 
184  for(int i=initRow; i<=finalRow; i++)
185  {
186  row = (m_promoter == 0) ? i : (int)m_promoter->getLogicalRow(i);
187  m_dataset->move(row);
188 
189  oids->add(te::da::GenerateOID(m_dataset, oids->getPropertyNames()));
190  }
191 
192  return oids;
193 }
194 
196 {
197  beginResetModel();
198 
199  m_enabled = enabled;
200 
201  endResetModel();
202 }
203 
204 int te::qt::widgets::DataSetTableModel::rowCount(const QModelIndex & parent) const
205 {
206  return (m_dataset == 0 || !m_enabled) ? 0 : (int)m_dataset->size();
207 }
208 
209 int te::qt::widgets::DataSetTableModel::columnCount(const QModelIndex & parent) const
210 {
211  return (m_dataset == 0) ? 0 : (int) m_dataset->getNumProperties();
212 }
213 
214 QVariant te::qt::widgets::DataSetTableModel::data(const QModelIndex & index, int role) const
215 {
216  if(!index.isValid())
217  return QVariant();
218 
219  switch (role)
220  {
221  case Qt::TextAlignmentRole:
222  return (int)(Qt::AlignCenter);
223  break;
224 
225  case Qt::DisplayRole:
226  if(m_currentRow != index.row())
227  {
228  m_currentRow = index.row();
229 
230  int row = (m_promoter == 0) ? m_currentRow : (int)m_promoter->getLogicalRow(m_currentRow);
231  m_dataset->move(row);
232  }
233 
234  if(!m_dataset->isNull(index.column()))
235  return m_dataset->getAsString(index.column(), 6).c_str();
236 
237  break;
238 
239  default:
240  break;
241  }
242 
243  return QVariant();
244 }
245 
246 QVariant te::qt::widgets::DataSetTableModel::headerData(int section, Qt::Orientation orientation, int role) const
247 {
248  if(orientation == Qt::Horizontal)
249  {
250  switch(role)
251  {
252  case Qt::DisplayRole:
253  return m_dataset->getPropertyName(section).c_str();
254  break;
255 
256  case Qt::DecorationRole:
257  return (m_OIdsVisible && IsPkey(section, m_pkeysColumns)) ?
258  QIcon::fromTheme("key") :
259  QVariant();
260  break;
261 
262  case Qt::ToolTipRole:
263  return m_dataset->getPropertyName(section).c_str() + QString(" : ") + columnType(m_dataset->getPropertyDataType(section));
264  break;
265 
266  default:
267  return QAbstractTableModel::headerData(section, orientation, role);
268  break;
269  }
270  }
271  else
272  {
273  switch (role)
274  {
275  case Qt::DisplayRole:
276  return section+1;
277  break;
278 
279  default:
280  return QAbstractTableModel::headerData(section, orientation, role);
281  break;
282  }
283  }
284 
285  return QVariant();
286 }
287 
288 Qt::ItemFlags te::qt::widgets::DataSetTableModel::flags(const QModelIndex & index) const
289 {
290  if (index.isValid() == false)
291  return 0;
292 
293  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
294 
295  flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable /*| Qt::ItemIsEditable*/;
296 
297  return flags;
298 }
299 
300 bool te::qt::widgets::DataSetTableModel::setData (const QModelIndex & /*index*/, const QVariant & /*value*/, int /*role*/)
301 {
302  return true;
303 }
304 
305 bool te::qt::widgets::DataSetTableModel::insertColumns(int column, int count, const QModelIndex& parent)
306 {
307  beginInsertColumns(parent, column, column+count);
308 
309  endInsertColumns();
310 
311  return true;
312 }
313 
314 bool te::qt::widgets::DataSetTableModel::removeColumns(int column, int count, const QModelIndex& parent)
315 {
316  beginRemoveColumns(parent, column, column+count);
317 
318  endRemoveColumns();
319 
320  return true;
321 }
Defines an mechanism for logical ordering of rows.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
void showOIdsVisible(const bool &visible)
Shows an icon for indentify the columns that are used for identify objects.
int columnCount(const QModelIndex &parent) const
te::da::ObjectIdSet * getObjectIdSet(const int &initRow, const int &finalRow)
Returns the ObjectIdSet begining with row initRow and ending in finalRow.
Promoter * m_promoter
Promoter to be used.
bool removeColumns(int column, int count, const QModelIndex &parent=QModelIndex())
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
void setDataSet(te::da::DataSet *dset)
Updates the data being used.
const std::vector< std::string > & getPropertyNames() const
It returns the property names used to generated the oids.
Promoter * getPromoter()
Returns the pointer to the promoter being used.
void setEnabled(const bool &enabled)
Enable or disable the dataset presentation.
int rowCount(const QModelIndex &parent) const
void setPkeysColumns(const std::vector< size_t > &pkeys)
Sets the columns used as pkeys, for presentation purposes.
QString columnType(const int &type)
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:393
void promote(const te::da::ObjectIdSet *oids)
Promotes the rows identified by oids.
QVariant data(const QModelIndex &index, int role) const
DataSetTableModel(QObject *parent=0)
Constructor.
bool IsPkey(const int &column, const std::vector< size_t > &pkeys)
bool insertColumns(int column, int count, const QModelIndex &parent=QModelIndex())
Qt::ItemFlags flags(const QModelIndex &index) const
void addProperty(const std::string &name, std::size_t pos, int type)
It adds a property that will be used to generate the unique ids.
Definition: ObjectIdSet.cpp:63
virtual ~DataSetTableModel()
Virtual destructor.
void add(ObjectId *oid)
It adds an object id to this object id set.
Definition: ObjectIdSet.cpp:71
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:111
A model based on te::da::DataSet.
A class used for logical ordering of rows.
Definition: Promoter.h:69
QVariant headerData(int section, Qt::Orientation orientation, int role) const