All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSetCategoryModel.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/dataset/explorer/DataSetCategoryModel.cpp
22 
23  \brief ????
24 */
25 
26 // TerraLib
27 #include "../../../../dataaccess/Enums.h"
28 #include "../../datasource/explorer/DataSetCategoryGroupItem.h"
29 #include "DataSetCategoryModel.h"
30 
31 // STL
32 #include <memory>
33 
34 // Qt
35 #include <QMessageBox>
36 #include <QWidget>
37 
39  : QAbstractItemModel(parent),
40  m_datasets(0),
41  m_checkable(false),
42  m_forceCatalogCache(false)
43 {
44  if(datasource.get() != 0)
45  m_datasets = new DataSetCategoryGroupItem(datasource, 0);
46 }
47 
49 {
50  delete m_datasets;
51 }
52 
53 bool te::qt::widgets::DataSetCategoryModel::canFetchMore(const QModelIndex& parent) const
54 {
55  if(!parent.isValid())
56  return false;
57 
58  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(parent.internalPointer());
59 
60  if(item == 0)
61  return false;
62 
63  return item->canFetchMore();
64 }
65 
66 int te::qt::widgets::DataSetCategoryModel::columnCount(const QModelIndex& parent) const
67 {
68  return 1;
69 }
70 
71 QVariant te::qt::widgets::DataSetCategoryModel::data(const QModelIndex& index, int role) const
72 {
73  if(!index.isValid())
74  return QVariant();
75 
76  if(role == Qt::CheckStateRole && !m_checkable)
77  return QVariant();
78 
79  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
80 
81  if(item == 0)
82  return QVariant();
83 
84  try
85  {
86  return item->data(index.column(), role);
87  }
88  catch(const std::exception& e)
89  {
90  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
91  tr("TerraLib Qt Components"),
92  tr(e.what()));
93  }
94  catch(...)
95  {
96  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
97  tr("TerraLib Qt Components"),
98  tr("Unknown error in dataset explorer model!"));
99  }
100 
101  return QVariant();
102 }
103 
105 {
106  if(!parent.isValid())
107  return;
108 
109  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(parent.internalPointer());
110 
111  if(item == 0)
112  return;
113 
114  try
115  {
116  item->fetchMore();
117  }
118  catch(const std::exception& e)
119  {
120  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
121  tr("TerraLib Qt Components"),
122  tr(e.what()));
123  }
124  catch(...)
125  {
126  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
127  tr("TerraLib Qt Components"),
128  tr("Unknown error in dataset explorer model!"));
129  }
130 }
131 
132 Qt::ItemFlags te::qt::widgets::DataSetCategoryModel::flags(const QModelIndex& index) const
133 {
134  if(!index.isValid())
135  return QAbstractItemModel::flags(index);
136 
137  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
138 
139  if(item == 0)
140  return QAbstractItemModel::flags(index);
141 
142  try
143  {
144  return QAbstractItemModel::flags(index) | item->flags();
145  }
146  catch(const std::exception& e)
147  {
148  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
149  tr("TerraLib Qt Components"),
150  tr(e.what()));
151  }
152  catch(...)
153  {
154  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
155  tr("TerraLib Qt Components"),
156  tr("Unknown error in dataset explorer model!"));
157  }
158 
159  return QAbstractItemModel::flags(index);
160 }
161 
162 bool te::qt::widgets::DataSetCategoryModel::hasChildren(const QModelIndex& parent) const
163 {
164  try
165  {
166  if(!parent.isValid())
167  return true;
168 
169  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(parent.internalPointer());
170 
171  if(item == 0)
172  return false;
173 
174  return item->hasChildren();
175  }
176  catch(const std::exception& e)
177  {
178  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
179  tr("TerraLib Qt Components"),
180  tr(e.what()));
181  }
182  catch(...)
183  {
184  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
185  tr("TerraLib Qt Components"),
186  tr("Unknown error in dataset explorer model!"));
187  }
188 
189  return false;
190 }
191 
192 QModelIndex te::qt::widgets::DataSetCategoryModel::index(int row, int column, const QModelIndex& parent) const
193 {
194  if(m_datasets == 0)
195  return QModelIndex();
196 
197  if(!parent.isValid()) // is it the top-level item? we have just one descendent from root!
198  {
199  if(row != 0)
200  return QModelIndex();
201 
202  AbstractDataSourceTreeItem* item = m_datasets;
203 
204  return createIndex(row, column, item);
205  }
206 
207  AbstractDataSourceTreeItem* parentItem = static_cast<AbstractDataSourceTreeItem*>(parent.internalPointer());
208 
209  if(parentItem == 0 || parentItem->children().empty()) // if there isn't a child, return an invalid index
210  return QModelIndex();
211 
212  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(parentItem->children().at(row));
213 
214  if(item == 0)
215  return QModelIndex();
216 
217  return createIndex(row, column, item);
218 }
219 
220 QModelIndex te::qt::widgets::DataSetCategoryModel::parent(const QModelIndex& index) const
221 {
222  if(!index.isValid())
223  return QModelIndex();
224 
225  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
226 
227  if(item == 0 || item->parent() == 0)
228  return QModelIndex();
229 
230  AbstractDataSourceTreeItem* parentItem = dynamic_cast<AbstractDataSourceTreeItem*>(item->parent());
231 
232  if(parentItem == 0)
233  return QModelIndex();
234 
235  AbstractDataSourceTreeItem* grandParentItem = dynamic_cast<AbstractDataSourceTreeItem*>(parentItem->parent());
236 
237  if(grandParentItem == 0)
238  {
239 // the parent is a top level item
240  return createIndex(0, index.column(), parentItem);
241  }
242  else
243  {
244 // the parent has a grandparent
245  const QObjectList& items = grandParentItem->children();
246 
247  int i = 0;
248 
249  for(QObjectList::const_iterator it = items.begin(); it != items.end(); ++it, ++i)
250  {
251  if((*it) == parentItem)
252  return createIndex(i, index.column(), parentItem);
253  }
254  }
255 
256  return QModelIndex();
257 }
258 
259 int te::qt::widgets::DataSetCategoryModel::rowCount(const QModelIndex& parent) const
260 {
261  if(!parent.isValid()) // if parent index is not valid we assume we are asking for root item
262  return 1;
263 
264  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(parent.internalPointer());
265 
266  if(item == 0)
267  return 0;
268 
269  return item->children().count();
270 }
271 
272 bool te::qt::widgets::DataSetCategoryModel::setData(const QModelIndex& index, const QVariant& value, int role)
273 {
274  if(!index.isValid())
275  return false;
276 
277  if(role == Qt::CheckStateRole && !m_checkable)
278  return false;
279 
280  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
281 
282  if(item == 0)
283  return false;
284 
285  try
286  {
287  bool retval = item->setData(value, role);
288 
289  emit dataChanged(index, index);
290 
291  return retval;
292  }
293  catch(const std::exception& e)
294  {
295  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
296  tr("TerraLib Qt Components"),
297  tr(e.what()));
298  }
299  catch(...)
300  {
301  QMessageBox::warning(static_cast<QWidget*>(QObject::parent()),
302  tr("TerraLib Qt Components"),
303  tr("Unknown error in dataset explorer model!"));
304  }
305 
306  return false;
307 }
308 
310 {
311  m_checkable = checkable;
312 }
313 
315 {
316  return m_checkable;
317 }
318 
void fetchMore(const QModelIndex &parent)
int columnCount(const QModelIndex &parent=QModelIndex()) const
int rowCount(const QModelIndex &parent=QModelIndex()) const
Qt::ItemFlags flags(const QModelIndex &index) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
QModelIndex parent(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
virtual bool setData(const QVariant &value, int role=Qt::EditRole)=0
DataSetCategoryModel(const te::da::DataSourceInfoPtr &datasource, QWidget *parent=0)
bool canFetchMore(const QModelIndex &parent) const
virtual Qt::ItemFlags flags() const =0
virtual QVariant data(int column, int role) const =0
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
bool hasChildren(const QModelIndex &parent=QModelIndex()) const