DataSetTreeView.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/DataSetTreeView.cpp
22 
23  \brief A tree view for datasets of a data source.
24 */
25 
26 // TerraLib
27 #include "../../datasource/explorer/DataSetGroupItem.h"
28 #include "../../datasource/explorer/DataSetCategoryGroupItem.h"
29 #include "../../datasource/explorer/DataSetItem.h"
30 //#include "DataSetTreeModel.h"
31 #include "DataSetCategoryModel.h"
32 #include "DataSetTreeModel.h"
33 #include "DataSetTreeView.h"
34 
35 // STL
36 #include <memory>
37 
38 // Qt
39 #include <QMenu>
40 
42  : QTreeView(parent),
43 
44  m_ds(te::da::DataSourceInfoPtr()),
45  m_isCategoryModel(true),
46  m_useCheckableItems(true)
47 {
48  this->setContextMenuPolicy(Qt::CustomContextMenu);
49 
50  connect(this, SIGNAL(activated(const QModelIndex&)), this, SLOT(onItemActivated(const QModelIndex&)));
51  connect(this, SIGNAL(clicked(const QModelIndex&)), this, SLOT(onItemClicked(const QModelIndex&)));
52  connect(this, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(onItemDoubleClicked(const QModelIndex&)));
53  connect(this, SIGNAL(entered(const QModelIndex&)), this, SLOT(onItemEntered(const QModelIndex&)));
54  connect(this, SIGNAL(pressed(const QModelIndex&)), this, SLOT(onItemPressed(const QModelIndex&)));
55  connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(customContextMenu(const QPoint&)));
56 }
57 
59 
61 {
62  m_ds = ds;
63 
64  m_treeModel.reset(new DataSetTreeModel(ds, this));
65  m_categoryModel.reset(new DataSetCategoryModel(ds, this));
66 
67  m_useCheckableItems = useCheckableItems;
68 
69  m_treeModel->setCheckable(m_useCheckableItems);
70  m_categoryModel->setCheckable(m_useCheckableItems);
71 
72  this->setModel(m_treeModel.get());
73 
74  m_isCategoryModel = true;
75 }
76 
77 std::list<te::qt::widgets::DataSetItem*> te::qt::widgets::DataSetTreeView::getSelectedDataSets() const
78 {
79  std::list<DataSetItem*> ditems;
80 
81  QModelIndexList items = selectedIndexes();
82 
83  for(QModelIndexList::iterator it = items.begin(); it != items.end(); ++it)
84  {
85  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(it->internalPointer());
86 
87  if(item == nullptr)
88  continue;
89 
90  DataSetItem* ditem = dynamic_cast<DataSetItem*>(item);
91 
92  if(ditem)
93  ditems.push_back(ditem);
94  }
95 
96  return ditems;
97 }
98 
100 {
101  QModelIndexList items = selectedIndexes();
102 
103  for(QModelIndexList::iterator it = items.begin(); it != items.end(); ++it)
104  {
105  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(it->internalPointer());
106 
107  if(item == nullptr)
108  continue;
109 
110  DataSetItem* ditem = dynamic_cast<DataSetItem*>(item);
111 
112  if(ditem)
113  return true;
114  }
115 
116  return false;
117 }
118 
120 {
121  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
122 
123  if(item == nullptr)
124  return;
125 
126  DataSetItem* ditem = dynamic_cast<DataSetItem*>(item);
127 
128  if(ditem != nullptr)
129  {
130  emit activated(ditem);
131  return;
132  }
133 
134  DataSetCategoryGroupItem* categoryItem = dynamic_cast<DataSetCategoryGroupItem*>(item);
135 
136  if(categoryItem != nullptr)
137  {
138  emit activated(categoryItem);
139  return;
140  }
141 
142  DataSetGroupItem* groupItem = dynamic_cast<DataSetGroupItem*>(item);
143 
144  if(groupItem != nullptr)
145  emit activated(groupItem);
146 }
147 
148 void te::qt::widgets::DataSetTreeView::onItemClicked(const QModelIndex & index)
149 {
150  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
151 
152  if(item == nullptr)
153  return;
154 
155  QAbstractItemModel* model = this->model();
156 
157  DataSetItem* ditem = dynamic_cast<DataSetItem*>(item);
158 
159  if(ditem != nullptr)
160  {
161  emit clicked(ditem);
162 
163  if(model && !m_useCheckableItems)
164  return;
165 
166  QVariant value = item->data(0, Qt::CheckStateRole);
167 
168  if(value.isNull())
169  return;
170 
171  emit toggled(ditem);
172 
173  return;
174  }
175 
176  DataSetCategoryGroupItem* categoryItem = dynamic_cast<DataSetCategoryGroupItem*>(item);
177 
178  if(categoryItem != nullptr)
179  {
180  emit clicked(categoryItem);
181 
182  if(model && !m_useCheckableItems)
183  return;
184 
185  if(model && !m_useCheckableItems)
186  return;
187 
188  QVariant value = item->data(0, Qt::CheckStateRole);
189 
190  if(value.isNull())
191  return;
192 
193  emit toggled(categoryItem);
194 
195  return;
196  }
197 
198  DataSetGroupItem* groupItem = dynamic_cast<DataSetGroupItem*>(item);
199 
200  if(groupItem != nullptr)
201  {
202  emit clicked(groupItem);
203 
204  if(model && !m_useCheckableItems)
205  return;
206 
207  if(model && !m_useCheckableItems)
208  return;
209 
210  QVariant value = item->data(0, Qt::CheckStateRole);
211 
212  if(value.isNull())
213  return;
214 
215  emit toggled(groupItem);
216 
217  return;
218  }
219 }
220 
222 {
223  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
224 
225  if(item == nullptr)
226  return;
227 
228  DataSetItem* ditem = dynamic_cast<DataSetItem*>(item);
229 
230  if(ditem != nullptr)
231  {
232  emit doubleClicked(ditem);
233  return;
234  }
235 
236  DataSetCategoryGroupItem* categoryItem = dynamic_cast<DataSetCategoryGroupItem*>(item);
237 
238  if(categoryItem != nullptr)
239  {
240  emit doubleClicked(categoryItem);
241  return;
242  }
243 
244  DataSetGroupItem* groupItem = dynamic_cast<DataSetGroupItem*>(item);
245 
246  if(groupItem != nullptr)
247  emit doubleClicked(groupItem);
248 }
249 
250 void te::qt::widgets::DataSetTreeView::onItemEntered(const QModelIndex & index)
251 {
252  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
253 
254  if(item == nullptr)
255  return;
256 
257  DataSetItem* ditem = dynamic_cast<DataSetItem*>(item);
258 
259  if(ditem != nullptr)
260  {
261  emit entered(ditem);
262  return;
263  }
264 
265  DataSetCategoryGroupItem* categoryItem = dynamic_cast<DataSetCategoryGroupItem*>(item);
266 
267  if(categoryItem != nullptr)
268  {
269  emit entered(categoryItem);
270  return;
271  }
272 
273  DataSetGroupItem* groupItem = dynamic_cast<DataSetGroupItem*>(item);
274 
275  if(groupItem != nullptr)
276  emit entered(groupItem);
277 }
278 
279 void te::qt::widgets::DataSetTreeView::onItemPressed(const QModelIndex & index)
280 {
281  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(index.internalPointer());
282 
283  if(item == nullptr)
284  return;
285 
286  DataSetItem* ditem = dynamic_cast<DataSetItem*>(item);
287 
288  if(ditem != nullptr)
289  {
290  emit pressed(ditem);
291  return;
292  }
293 
294  DataSetCategoryGroupItem* categoryItem = dynamic_cast<DataSetCategoryGroupItem*>(item);
295 
296  if(categoryItem != nullptr)
297  {
298  emit pressed(categoryItem);
299  return;
300  }
301 
302  DataSetGroupItem* groupItem = dynamic_cast<DataSetGroupItem*>(item);
303 
304  if(groupItem != nullptr)
305  emit pressed(groupItem);
306 }
307 
309  const QPoint& /*point*/)
310 {
311  QMenu *menu = new QMenu("", this);
312 
313  QAction * organize = new QAction(tr("Organize by category"), menu);
314  organize->setCheckable(true);
315 
317  organize->setChecked(true);
318 
319  connect(organize, SIGNAL(toggled(bool)), this, SLOT(onModelToggled(bool)));
320  menu->addAction(organize);
321  menu->exec(QCursor::pos());
322 }
323 
325 {
326  if(checked)
327  this->setModel(m_categoryModel.get());
328  else
329  this->setModel(m_treeModel.get());
330 
331  m_isCategoryModel = checked;
332 
333  QAbstractItemModel* nmodel = model();
334 
335  QModelIndex idx = nmodel->index(0, 0);
336 
337  expand(idx);
338 
339  if(m_isCategoryModel)
340  {
341  AbstractDataSourceTreeItem* item = static_cast<AbstractDataSourceTreeItem*>(idx.internalPointer());
342 
343  for(int i = 0; i < item->children().size(); i++)
344  {
345  QModelIndex idxChild = nmodel->index(i, 0, idx);
346  expand(idxChild);
347  }
348  }
349 }
void pressed(DataSetItem *item)
A tree view for datasets of a data source.
void onItemDoubleClicked(const QModelIndex &index)
void toggled(DataSetItem *item)
void onItemEntered(const QModelIndex &index)
std::unique_ptr< DataSetCategoryModel > m_categoryModel
static te::dt::Date ds(2010, 01, 01)
void onItemActivated(const QModelIndex &index)
A simple model for datasets belonging to a data source.
void entered(DataSetItem *item)
void clicked(DataSetItem *item)
void customContextMenu(const QPoint &point)
void doubleClicked(DataSetItem *item)
virtual QVariant data(int column, int role) const =0
te::da::DataSourceInfoPtr m_ds
void onItemClicked(const QModelIndex &index)
URI C++ Library.
Definition: Attributes.h:37
void set(const te::da::DataSourceInfoPtr &ds, bool useCheckableItems=false)
void onItemPressed(const QModelIndex &index)
void activated(DataSetItem *item)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
std::unique_ptr< DataSetTreeModel > m_treeModel
std::list< DataSetItem * > getSelectedDataSets() const