All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 <QtCore/QAbstractItemModel>
47 #include <QtGui/QMenu>
48 #include <QtGui/QMessageBox>
49 #include <QtGui/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 
124  m_items.push_back(new DataSetItem(dt, ds.get(), this));
125  }
126 
127  if(m_items.size() == 1)
128  {
129  m_items[0]->setData(true, Qt::CheckStateRole);
130  }
131 }
132 
134 {
135  if(m_isInvalid)
136  return false;
137 
138  if(m_ds.get() == 0)
139  return false;
140 
141  try
142  {
143  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
144 
145  if(ds.get() == 0)
146  return false;
147 
148  return ds->hasDataSets();
149  }
150  catch(...)
151  {
152  m_isInvalid = true;
153  throw;
154  }
155 }
156 
157 bool te::qt::widgets::DataSetGroupItem::setData(const QVariant& value, int role)
158 {
159  if(role == Qt::CheckStateRole)
160  {
161  m_checked = value.toBool();
162 
163  for(std::size_t i = 0; i < m_items.size(); ++i)
164  {
165  m_items[i]->setData(value, role);
166  }
167  return true;
168  }
169 
170  return false;
171 }
172 
174 {
175  return m_checked;
176 }
177 
179 {
180  bool check = false;
181 
182  for(std::size_t i = 0; i < m_items.size(); ++i)
183  {
184  if(m_items[i]->isChecked())
185  {
186  check = true;
187  break;
188  }
189  }
190 
191  m_checked = check;
192 }
193 
194 const std::vector<te::qt::widgets::DataSetItem*>& te::qt::widgets::DataSetGroupItem::getDataSetItems() const
195 {
196  return m_items;
197 }
QVariant data(int column, int role) const
const std::vector< DataSetItem * > & getDataSetItems() const
A class that represents a data source in a DataSourceTreeModel.
A class that represents a dataset in a TreeModel.
A class used to group a set of dataset items in a DataSourceTreeModel.
bool setData(const QVariant &value, int role=Qt::EditRole)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
A class that models the description of a dataset.
Definition: DataSetType.h:72
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
QMenu * getMenu(QWidget *parent=0) const
DataSetGroupItem(const te::da::DataSourceInfoPtr &ds, AbstractDataSourceTreeItem *parent=0)