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) 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/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  bool hasGeom = false;
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  {
130  m_items.push_back(new DataSetItem(dt, p->getName(), ds.get(), this));
131  hasGeom = true;
132  }
133  }
134 
135  if(!hasGeom)
136  m_items.push_back(new DataSetItem(dt, "Tabular", ds.get(), this));
137  }
138 
139  if(m_items.size() == 1)
140  {
141  m_items[0]->setData(true, Qt::CheckStateRole);
142  }
143 }
144 
146 {
147  if(m_isInvalid)
148  return false;
149 
150  if(m_ds.get() == 0)
151  return false;
152 
153  try
154  {
155  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
156 
157  if(ds.get() == 0)
158  return false;
159 
160  return ds->hasDataSets();
161  }
162  catch(...)
163  {
164  m_isInvalid = true;
165  throw;
166  }
167 }
168 
169 bool te::qt::widgets::DataSetGroupItem::setData(const QVariant& value, int role)
170 {
171  if(role == Qt::CheckStateRole)
172  {
173  m_checked = value.toBool();
174 
175  for(std::size_t i = 0; i < m_items.size(); ++i)
176  {
177  m_items[i]->setData(value, role);
178  }
179  return true;
180  }
181 
182  return false;
183 }
184 
186 {
187  return m_checked;
188 }
189 
191 {
192  bool check = false;
193 
194  for(std::size_t i = 0; i < m_items.size(); ++i)
195  {
196  if(m_items[i]->isChecked())
197  {
198  check = true;
199  break;
200  }
201  }
202 
203  m_checked = check;
204 }
205 
206 const std::vector<te::qt::widgets::DataSetItem*>& te::qt::widgets::DataSetGroupItem::getDataSetItems() const
207 {
208  return m_items;
209 }
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:161
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:127