DataSetCategoryItem.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/DataSetCategoryItem.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 "../../../../core/translator/Translator.h"
29 #include "../../../../dataaccess/Enums.h"
30 #include "../../../../dataaccess/dataset/DataSetType.h"
31 #include "../../../../dataaccess/datasource/DataSource.h"
32 //#include "../../../../dataaccess/datasource/DataSourceCatalog.h"
33 //#include "../../../../dataaccess/datasource/DataSourceCatalogLoader.h"
34 #include "../../../../dataaccess/datasource/DataSourceManager.h"
35 #include "../../../../dataaccess/datasource/DataSourceInfo.h"
36 //#include "../../../../dataaccess/datasource/DataSourceTransactor.h"
37 #include "../../../../dataaccess/utils/Utils.h"
38 #include "../../Exception.h"
39 #include "DataSetCategoryItem.h"
40 #include "DataSetItem.h"
41 #include "DataSourceItem.h"
42 
43 // STL
44 #include <memory>
45 
46 // Qt
47 #include <QMenu>
48 #include <QMessageBox>
49 #include <QWidget>
50 
53  m_ds(ds),
54  m_checked(false),
55  m_isInvalid(false),
56  m_category(category)
57 {
58 }
59 
61 
63 {
64  return 1;
65 }
66 
67 QVariant te::qt::widgets::DataSetCategoryItem::data(int /*column*/, int role) const
68 {
69  if(role == Qt::DecorationRole)
70  return QVariant(QIcon::fromTheme(te::da::GetDataSetCategoryName(m_category).c_str()));
71 
72  if(role == Qt::DisplayRole)
73  return QVariant(QString(te::da::GetDataSetCategoryName(m_category).c_str()));
74 
75  if(role == Qt::CheckStateRole)
76  return QVariant(m_checked ? Qt::Checked : Qt::Unchecked);
77 
78  return QVariant();
79 }
80 
82 {
83  QMenu* m = new QMenu(parent);
84 
85  QAction* aOpenDataSets = m->addAction(tr("&Open datasets"));
86 
87  connect(aOpenDataSets, SIGNAL(triggered()), this, SLOT(openDataSets()));
88 
89  return m;
90 }
91 
93 {
94  if(!children().empty ())
95  return false;
96 
97  return hasChildren();
98 }
99 
101 {
102  return Qt::ItemIsUserCheckable;
103 }
104 
106 {
107  if(m_ds == nullptr)
108  return;
109 
110  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
111 
112  if(ds.get() == nullptr)
113  return;
114 
115  std::vector<std::string> datasetNames = ds->getDataSetNames();
116 
117  const std::size_t ndatasets = datasetNames.size();
118 
119  for(std::size_t i = 0; i < ndatasets; ++i)
120  {
121  //te::da::DataSetTypePtr dt(new te::da::DataSetType(datasetNames[i]));
122  te::da::DataSetTypePtr dt(ds->getDataSetType(datasetNames[i]).release());
123 
124  for(std::size_t i = 0; i < dt->size(); ++i)
125  {
126  if(dt->getCategory() == m_category)
127  {
128  te::dt::Property* p = dt->getProperty(i);
129  if(p->getType() == te::dt::GEOMETRY_TYPE || dt->getProperty(i)->getType() == te::dt::RASTER_TYPE)
130  new DataSetItem(dt->getName(), ds.get(), this);
131  }
132  }
133  }
134 }
135 
137 {
138  if(m_isInvalid)
139  return false;
140 
141  if(m_ds.get() == nullptr)
142  return false;
143 
144  try
145  {
146  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
147 
148  if(ds.get() == nullptr)
149  return false;
150 
151  return ds->hasDataSets();
152  }
153  catch(...)
154  {
155  m_isInvalid = true;
156  throw;
157  }
158 }
159 
160 bool te::qt::widgets::DataSetCategoryItem::setData(const QVariant& value, int role)
161 {
162  if(role == Qt::CheckStateRole)
163  {
164  m_checked = value.toBool();
165  return true;
166  }
167 
168  return false;
169 }
170 
172 {
173  return m_checked;
174 }
175 
A class that represents a dataset in a TreeModel.
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
A class used to group a set of dataset items in a DataSourceTreeModel.
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
static te::dt::Date ds(2010, 01, 01)
It models a property definition.
Definition: Property.h:59
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
static te::dt::TimeDuration dt(20, 30, 50, 11)
bool setData(const QVariant &value, int role=Qt::EditRole)
te::gm::Polygon * p
QMenu * getMenu(QWidget *parent=0) const
int getType() const
It returns the property data type.
Definition: Property.h:161
A class that represents a data source in a DataSourceTreeModel.
QVariant data(int column, int role) const
DataSetCategoryItem(const te::da::DataSourceInfoPtr &ds, int category, AbstractDataSourceTreeItem *parent=0)
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr