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 "../../../../core/translator/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  m_filter("")
57 {
58 }
59 
61 
63 {
64  return 1;
65 }
66 
67 QVariant te::qt::widgets::DataSetGroupItem::data(int /*column*/, int role) const
68 {
69  if(role == Qt::DecorationRole)
70  return QVariant(QIcon::fromTheme("datasets"));
71 
72  if(role == Qt::DisplayRole)
73  return QVariant(QString("datasets"));
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  if (!m_filter.empty())
122  {
123  if(datasetNames[i].find(m_filter) != std::string::npos)
124  m_items.push_back(new DataSetItem(datasetNames[i], ds.get(), this));
125  }
126  else
127  {
128  m_items.push_back(new DataSetItem(datasetNames[i], ds.get(), this));
129  }
130  }
131 
132  if(m_items.size() == 1)
133  {
134  m_items[0]->setData(true, Qt::CheckStateRole);
135  }
136 }
137 
139 {
140  if(m_isInvalid)
141  return false;
142 
143  if(m_ds.get() == nullptr)
144  return false;
145 
146  try
147  {
148  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
149 
150  if(ds.get() == nullptr)
151  return false;
152 
153  return ds->hasDataSets();
154  }
155  catch(...)
156  {
157  m_isInvalid = true;
158  throw;
159  }
160 }
161 
162 bool te::qt::widgets::DataSetGroupItem::setData(const QVariant& value, int role)
163 {
164  if(role == Qt::CheckStateRole)
165  {
166  m_checked = value.toBool();
167 
168  for(std::size_t i = 0; i < m_items.size(); ++i)
169  {
170  m_items[i]->setData(value, role);
171  }
172  return true;
173  }
174 
175  return false;
176 }
177 
179 {
180  return m_checked;
181 }
182 
184 {
185  bool check = false;
186 
187  for(std::size_t i = 0; i < m_items.size(); ++i)
188  {
189  if(m_items[i]->isChecked())
190  {
191  check = true;
192  break;
193  }
194  }
195 
196  m_checked = check;
197 }
198 
199 const std::vector<te::qt::widgets::DataSetItem*>& te::qt::widgets::DataSetGroupItem::getDataSetItems() const
200 {
201  return m_items;
202 }
203 
204 void te::qt::widgets::DataSetGroupItem::setFilter(const std::string& filter)
205 {
206  m_filter = filter;
207 }
A class that represents a dataset in a TreeModel.
boost::shared_ptr< DataSource > DataSourcePtr
QVariant data(int column, int role) const
bool setData(const QVariant &value, int role=Qt::EditRole)
const std::vector< DataSetItem * > & getDataSetItems() const
static te::dt::Date ds(2010, 01, 01)
QMenu * getMenu(QWidget *parent=0) const
std::vector< DataSetItem * > m_items
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
te::da::DataSourceInfoPtr m_ds
A class that represents a data source in a DataSourceTreeModel.
void setFilter(const std::string &filter)
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)