QueryLayerItem.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/layer/explorer/QueryLayerItem.cpp
22 
23  \brief The class that represents a query layer item in a LayerTreeModel.
24 */
25 
26 // TerraLib
27 #include "../../../../common/Translator.h"
28 #include "../../../../dataaccess/datasource/DataSourceInfoManager.h"
29 #include "../../../../dataaccess/query/Expression.h"
30 #include "../../../../dataaccess/query/Select.h"
31 #include "../../../../dataaccess/query/Where.h"
32 #include "../../../../maptools/QueryLayer.h"
33 #include "../../../../se/Style.h"
34 #include "../../Exception.h"
35 #include "ChartItem.h"
36 #include "GroupingItem.h"
37 #include "LegendItem.h"
38 #include "QueryLayerItem.h"
39 
40 //Boost
41 #include <boost/algorithm/string/replace.hpp>
42 
43 // STL
44 #include <cassert>
45 
46 // Qt
47 #include <QMenu>
48 #include <QWidget>
49 
51  : AbstractTreeItem(parent)
52 {
53  m_layer = boost::dynamic_pointer_cast<te::map::QueryLayer>(l);
54 }
55 
57 {
58 }
59 
61 {
62  return 1;
63 }
64 
65 QVariant te::qt::widgets::QueryLayerItem::data(int /*column*/, int role) const
66 {
67  if(role == Qt::DecorationRole)
68  return QVariant(QIcon::fromTheme("query-layer"));
69 
70  if(role == Qt::DisplayRole)
71  return QVariant(QString::fromUtf8(m_layer->getTitle().c_str()));
72 
73  if(role == Qt::CheckStateRole)
74  return QVariant(m_layer->getVisibility() == te::map::VISIBLE ? Qt::Checked : Qt::Unchecked);
75 
76  if (role == Qt::ToolTipRole)
77  return buildToolTip();
78 
79  return QVariant();
80 }
81 
82 QMenu* te::qt::widgets::QueryLayerItem::getMenu(QWidget* /*parent*/) const
83 {
84  //QMenu* m = new QMenu(parent);
85 
86  //QAction* aOpenDataSource = m->addAction(tr("&Open data source"));
87 
88  //connect(aOpenDataSource, SIGNAL(triggered()), this, SLOT(openDataSource()));
89 
90  //return m;
91  return 0;
92 }
93 
95 {
96  return (((m_layer->getStyle() != 0) && (!m_layer->getStyle()->getRules().empty())) || m_layer->getGrouping() != 0 || m_layer->getChart() != 0);
97 }
98 
100 {
101  return Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;;
102 }
103 
105 {
106  if(m_layer->getStyle() && children().empty())
107  {
108  const std::vector<te::se::Rule*>& rules = m_layer->getStyle()->getRules();
109 
110  for(std::size_t i = 0; i != rules.size(); ++i)
111  new LegendItem(rules[i], this);
112  }
113 
114  if(m_layer->getGrouping() && !hasGroupingItem())
115  new GroupingItem(m_layer->getGrouping(), this);
116 
117  if(m_layer->getChart() && !hasChartItem())
118  new ChartItem(m_layer->getChart(), this);
119 }
120 
122 {
123  return (m_layer->getStyle() != 0) && (m_layer->getStyle()->getRules().empty() == false);
124 }
125 
126 bool te::qt::widgets::QueryLayerItem::setData(int column, const QVariant& value, int role)
127 {
128  if(role == Qt::CheckStateRole)
129  {
130  Qt::CheckState checkState = static_cast<Qt::CheckState>(value.toInt());
131  if(checkState == Qt::Checked)
132  m_layer->setVisibility(te::map::VISIBLE);
133  else if(checkState == Qt::Unchecked)
134  m_layer->setVisibility(te::map::NOT_VISIBLE);
135 
136  m_layer->updateVisibilityOfAncestors();
137 
138  return true;
139  }
140 
141  return false;
142 }
143 
145 {
146  return m_layer;
147 }
148 
150 {
151  return "QUERY_LAYER_ITEM";
152 }
153 
155 {
156  GroupingItem* groupingItem = findChild<GroupingItem*>();
157 
158  return groupingItem != 0;
159 }
160 
162 {
163  ChartItem* chartItem = findChild<ChartItem*>();
164 
165  return chartItem != 0;
166 }
167 
169 {
170  if (!m_layer->isValid())
171  return tr("Invalid Layer");
172 
173  QString toolTip;
174 
175  // Gets the connection info
176  const std::string& id = m_layer->getDataSourceId();
178  const std::map<std::string, std::string>& connInfo = info->getConnInfo();
179 
180  toolTip += tr("Connection Info") + ":\n";
181 
182  std::size_t i = 0;
183  std::map<std::string, std::string>::const_iterator it;
184  for (it = connInfo.begin(); it != connInfo.end(); ++it)
185  {
186  toolTip += it->first.c_str();
187  toolTip += ": ";
188  toolTip += it->second.c_str();
189  ++i;
190  if (i != connInfo.size())
191  toolTip += "\n";
192  }
193 
194  toolTip += '\n';
195 
196  toolTip += tr("SRID: ");
197  toolTip += QString::number(m_layer->getSRID());
198 
199  toolTip += '\n';
200 
201  //The restriction
202  toolTip += tr("Restriction: ");
203  std::string query = m_layer->getQueryAsString();
204  boost::replace_all(query, ", ", ",\n");
205 
206  toolTip += QString::fromStdString(query);
207  return toolTip;
208 }
209 
A class that represents a grouping of a layer in a LayerTreeModel.
const std::string getItemType() const
It returns the item type: "QUERY_LAYER_ITEM".
QVariant data(int column, int role) const
Qt::ItemFlags flags() const
The class that represents an item in a LayerTreeModel.
A layer resulting from a query.
Definition: QueryLayer.h:50
A class that represents a legend item of a layer in a LayerTreeModel.
bool setData(int column, const QVariant &value, int role=Qt::EditRole)
static DataSourceInfoManager & getInstance()
It returns a reference to the singleton instance.
A class that represents a chart of a layer in a LayerTreeModel.
The class that represents a query layer item in a LayerTreeModel.
te::map::QueryLayerPtr m_layer
QueryLayerItem(const te::map::AbstractLayerPtr &l, QObject *parent=0)
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
te::map::AbstractLayerPtr getLayer() const
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
QMenu * getMenu(QWidget *parent=0) const