All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSetGroupItem.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/datasource/explorer/DataSetGroupItem.cpp
22 
23  \brief A class used to group a set of dataset items in a DataSourceTreeModel.
24 */
25 
26 // TerraLib
27 #include "../../../../common/STLUtils.h"
28 #include "../../../../common/Translator.h"
29 #include "../../../../dataaccess/dataset/DataSetType.h"
30 #include "../../../../dataaccess/datasource/DataSource.h"
31 //#include "../../../../dataaccess/datasource/DataSourceCatalog.h"
32 //#include "../../../../dataaccess/datasource/DataSourceCatalogLoader.h"
33 #include "../../../../dataaccess/datasource/DataSourceManager.h"
34 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
35 //#include "../../../../dataaccess/datasource/DataSourceTransactor.h"
36 #include "../../../../dataaccess/utils/Utils.h"
37 #include "../../Exception.h"
38 #include "DataSetGroupItem.h"
39 #include "DataSetItem.h"
40 #include "DataSourceItem.h"
41 
42 // STL
43 #include <memory>
44 
45 // Qt
46 #include <QAbstractItemModel>
47 #include <QMenu>
48 #include <QMessageBox>
49 #include <QWidget>
50 
53  m_ds(ds),
54  m_checked(false),
55  m_isInvalid(false)
56 {
57 }
58 
60 {
61 }
62 
64 {
65  return 1;
66 }
67 
68 QVariant te::qt::widgets::DataSetGroupItem::data(int /*column*/, int role) const
69 {
70  if(role == Qt::DecorationRole)
71  return QVariant(QIcon::fromTheme("datasets"));
72 
73  if(role == Qt::DisplayRole)
74  return QVariant(QString("datasets"));
75 
76  if(role == Qt::CheckStateRole)
77  return QVariant(m_checked ? Qt::Checked : Qt::Unchecked);
78 
79  return QVariant();
80 }
81 
82 QMenu* te::qt::widgets::DataSetGroupItem::getMenu(QWidget* parent) const
83 {
84  QMenu* m = new QMenu(parent);
85 
86  QAction* aOpenDataSets = m->addAction(tr("&Open datasets"));
87 
88  connect(aOpenDataSets, SIGNAL(triggered()), this, SLOT(openDataSets()));
89 
90  return m;
91 }
92 
94 {
95  if(!children().empty ())
96  return false;
97 
98  return hasChildren();
99 }
100 
102 {
103  return Qt::ItemIsUserCheckable;
104 }
105 
107 {
108  if(m_ds == 0)
109  return;
110 
111  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
112 
113  if(ds.get() == 0)
114  return;
115 
116  std::vector<std::string> datasetNames = ds->getDataSetNames();
117 
118  const std::size_t ndatasets = datasetNames.size();
119 
120  for(std::size_t i = 0; i < ndatasets; ++i)
121  {
122  //te::da::DataSetTypePtr dt(new te::da::DataSetType(datasetNames[i].c_str()));
123  te::da::DataSetTypePtr dt(ds->getDataSetType(datasetNames[i].c_str()).release());
124 
125  for(std::size_t i = 0; i < dt->size(); ++i)
126  {
127  te::dt::Property* p = dt->getProperty(i);
128  if(p->getType() == te::dt::GEOMETRY_TYPE || dt->getProperty(i)->getType() == te::dt::RASTER_TYPE)
129  m_items.push_back(new DataSetItem(dt, p->getName(), ds.get(), this));
130  }
131  }
132 
133  if(m_items.size() == 1)
134  {
135  m_items[0]->setData(true, Qt::CheckStateRole);
136  }
137 }
138 
140 {
141  if(m_isInvalid)
142  return false;
143 
144  if(m_ds.get() == 0)
145  return false;
146 
147  try
148  {
149  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
150 
151  if(ds.get() == 0)
152  return false;
153 
154  return ds->hasDataSets();
155  }
156  catch(...)
157  {
158  m_isInvalid = true;
159  throw;
160  }
161 }
162 
163 bool te::qt::widgets::DataSetGroupItem::setData(const QVariant& value, int role)
164 {
165  if(role == Qt::CheckStateRole)
166  {
167  m_checked = value.toBool();
168 
169  for(std::size_t i = 0; i < m_items.size(); ++i)
170  {
171  m_items[i]->setData(value, role);
172  }
173  return true;
174  }
175 
176  return false;
177 }
178 
180 {
181  return m_checked;
182 }
183 
185 {
186  bool check = false;
187 
188  for(std::size_t i = 0; i < m_items.size(); ++i)
189  {
190  if(m_items[i]->isChecked())
191  {
192  check = true;
193  break;
194  }
195  }
196 
197  m_checked = check;
198 }
199 
200 const std::vector<te::qt::widgets::DataSetItem*>& te::qt::widgets::DataSetGroupItem::getDataSetItems() const
201 {
202  return m_items;
203 }
A class that represents a dataset in a TreeModel.
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
QVariant data(int column, int role) const
bool setData(const QVariant &value, int role=Qt::EditRole)
const std::vector< DataSetItem * > & getDataSetItems() const
It models a property definition.
Definition: Property.h:59
QMenu * getMenu(QWidget *parent=0) const
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
int getType() const
It returns the property data type.
Definition: Property.h:143
A class that represents a data source in a DataSourceTreeModel.
A class used to group a set of dataset items in a DataSourceTreeModel.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
DataSetGroupItem(const te::da::DataSourceInfoPtr &ds, AbstractDataSourceTreeItem *parent=0)
const std::string & getName() const
It returns the property name.
Definition: Property.h:126