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