All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSetCategoryItem.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/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 "../../../../common/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 <QtGui/QMenu>
48 #include <QtGui/QMessageBox>
49 #include <QtGui/QWidget>
50 
53  m_ds(ds),
54  m_checked(false),
55  m_isInvalid(false),
56  m_category(category)
57 {
58 }
59 
61 {
62 }
63 
65 {
66  return 1;
67 }
68 
69 QVariant te::qt::widgets::DataSetCategoryItem::data(int /*column*/, int role) const
70 {
71  if(role == Qt::DecorationRole)
72  return QVariant(QIcon::fromTheme(te::da::GetDataSetCategoryName(m_category).c_str()));
73 
74  if(role == Qt::DisplayRole)
75  return QVariant(QString(te::da::GetDataSetCategoryName(m_category).c_str()));
76 
77  if(role == Qt::CheckStateRole)
78  return QVariant(m_checked ? Qt::Checked : Qt::Unchecked);
79 
80  return QVariant();
81 }
82 
83 QMenu* te::qt::widgets::DataSetCategoryItem::getMenu(QWidget* parent) const
84 {
85  QMenu* m = new QMenu(parent);
86 
87  QAction* aOpenDataSets = m->addAction(tr("&Open datasets"));
88 
89  connect(aOpenDataSets, SIGNAL(triggered()), this, SLOT(openDataSets()));
90 
91  return m;
92 }
93 
95 {
96  if(!children().empty ())
97  return false;
98 
99  return hasChildren();
100 }
101 
103 {
104  return Qt::ItemIsUserCheckable;
105 }
106 
108 {
109  if(m_ds == 0)
110  return;
111 
112  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
113 
114  if(ds.get() == 0)
115  return;
116 
117  std::vector<std::string> datasetNames = ds->getDataSetNames();
118 
119  const std::size_t ndatasets = datasetNames.size();
120 
121  for(std::size_t i = 0; i < ndatasets; ++i)
122  {
123  te::da::DataSetTypePtr dt(new te::da::DataSetType(datasetNames[i]));
124 
125  if(dt->getCategory() == m_category)
126  new DataSetItem(dt, ds.get(), this);
127  }
128 }
129 
131 {
132  if(m_isInvalid)
133  return false;
134 
135  if(m_ds.get() == 0)
136  return false;
137 
138  try
139  {
140  te::da::DataSourcePtr ds = te::da::DataSourceManager::getInstance().get(m_ds->getId(), m_ds->getAccessDriver(), m_ds->getConnInfo());
141 
142  if(ds.get() == 0)
143  return false;
144 
145  return ds->hasDataSets();
146  }
147  catch(...)
148  {
149  m_isInvalid = true;
150  throw;
151  }
152 }
153 
154 bool te::qt::widgets::DataSetCategoryItem::setData(const QVariant& value, int role)
155 {
156  if(role == Qt::CheckStateRole)
157  {
158  m_checked = value.toBool();
159  return true;
160  }
161 
162  return false;
163 }
164 
166 {
167  return m_checked;
168 }
169 
A class used to group a set of dataset items in a DataSourceTreeModel.
A class that represents a data source in a DataSourceTreeModel.
A class that represents a dataset in a TreeModel.
QVariant data(int column, int role) const
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
DataSetCategoryItem(const te::da::DataSourceInfoPtr &ds, int category, AbstractDataSourceTreeItem *parent=0)
QMenu * getMenu(QWidget *parent=0) const
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
Definition: Utils.cpp:160
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
bool setData(const QVariant &value, int role=Qt::EditRole)