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