All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LayerItem.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/vp/qt/LayerItem.cpp
22 
23  \brief A class that represents a Layer in a LayerTreeModel.
24 */
25 
26 // TerraLib
27 #include "../../dataaccess/dataset/DataSetType.h"
28 #include "../../maptools/DataSetLayer.h"
29 #include "../../se/Style.h"
30 #include "LayerItem.h"
31 #include "PropertyGroupItem.h"
32 #include "PropertyItem.h"
33 #include "LegendGroupItem.h"
34 
35 // Qt
36 #include <QWidget>
37 
38 // STL
39 #include <memory>
40 
42  : te::qt::widgets::AbstractTreeItem(parent),
43  m_layer(layer),
44  m_propertyGroup(0),
45  m_legendGroup(0),
46  m_selected(false),
47  m_onlySelecteds(false),
48  m_OnlyLegend(false)
49 {
50  if(layer->hasChildren())
51  {
52  for(size_t i = 0; i < layer->getChildrenCount(); ++i)
53  {
54  te::map::AbstractLayerPtr layerChild = boost::dynamic_pointer_cast<te::map::AbstractLayer>(layer->getChild(i));
55 
56  if(layerChild->isValid())
57  {
58  te::qt::widgets::AbstractTreeItem* litem = new te::vp::LayerItem(layerChild, this);
59  m_items.push_back(litem);
60  }
61  }
62  }
63  else if(m_layer->getSchema()->getProperties().size() > 0)
64  {
65  std::auto_ptr<te::map::LayerSchema> schema = m_layer->getSchema();
66 
67  if(schema.get() == 0)
68  return;
69 
70  std::vector<te::dt::Property*> properties = schema->getProperties();
71 
72  m_propertyGroup = new PropertyGroupItem(properties, this);
73  }
74 
75  te::map::DataSetLayer* dataSetLayer = 0;
76  dataSetLayer = dynamic_cast<te::map::DataSetLayer*>(m_layer.get());
77 
78  if(dataSetLayer != 0)
79  {
80  if(dataSetLayer->getStyle())
81  {
82  const std::vector<te::se::Rule*>& rules = dataSetLayer->getStyle()->getRules();
83 
84  m_legendGroup = new LegendGroupItem(rules, this);
85  }
86  }
87 
88 }
89 
91 {
92 
93 }
94 
96 {
97  return 2;
98 }
99 
100 QVariant te::vp::LayerItem::data(int column, int role) const
101 {
102  if(role == Qt::DisplayRole && column == 0)
103  return QVariant(QString::fromStdString(m_layer->getTitle()));
104 
105  if(role == Qt::CheckStateRole && column == 0)
106  return (m_selected ? Qt::Checked : Qt::Unchecked);
107 
108  if(role == Qt::CheckStateRole && column == 1)
109  return (m_onlySelecteds ? Qt::Checked : Qt::Unchecked);
110 
111  return QVariant();
112 }
113 
114 QMenu* te::vp::LayerItem::getMenu(QWidget* parent) const
115 {
116  //QMenu* m = new QMenu(parent);
117 
118  //QAction* aOpenDataSource = m->addAction(tr("&Open layer"));
119 
120  //connect(aOpenDataSource, SIGNAL(triggered()), this, SLOT(openDataSource()));
121 
122  //return m;
123  return 0;
124 }
125 
127 {
128  return m_layer->hasChildren() || (m_layer->getSchema()->getProperties().size() > 0);
129 }
130 
132 {
133  if(parent() == 0)
134  return;
135 
136  for(size_t i = 0; i < m_items.size(); ++i)
137  {
138  m_items[i]->fetchMore();
139  }
140 }
141 
142 Qt::ItemFlags te::vp::LayerItem::flags() const
143 {
144  return Qt::ItemIsUserCheckable;
145 }
146 
148 {
149  return m_layer->hasChildren() || !children().isEmpty();
150 }
151 
152 bool te::vp::LayerItem::setData(int column, const QVariant& value, int role)
153 {
154  if(role == Qt::CheckStateRole)
155  {
156  bool ok = false;
157  Qt::CheckState checkState = static_cast<Qt::CheckState>(value.toInt(&ok));
158 
159  if(!ok)
160  return false;
161 
162  if(column == 0)
163  {
164  if(checkState == Qt::Checked)
165  m_selected = true;
166  else if(checkState == Qt::Unchecked)
167  m_selected = false;
168 
169  std::vector<te::qt::widgets::AbstractTreeItem*> items = m_propertyGroup->getItems();
170 
171  for(size_t i = 0; i < items.size(); ++i)
172  {
173  PropertyItem* pItem = dynamic_cast<PropertyItem*>(items[i]);
174  pItem->setSelected(m_selected);
175  }
176  }
177  else if(column == 1)
178  {
179  if(checkState == Qt::Checked)
180  m_onlySelecteds = true;
181  else if(checkState == Qt::Unchecked)
182  m_onlySelecteds = false;
183  }
184  return true;
185  }
186 
187  return false;
188 }
189 
191 {
192  return m_layer;
193 }
194 
195 void te::vp::LayerItem::isSelected(bool selected)
196 {
197  m_selected = selected;
198 }
199 
201 {
202  return m_selected;
203 }
204 
205 std::vector<te::dt::Property*> te::vp::LayerItem::getSelected()
206 {
207  std::vector<te::qt::widgets::AbstractTreeItem*> propItems = m_propertyGroup->getItems();
208 
209  std::vector<te::dt::Property*> selected;
210 
211  for(size_t i = 0; i < propItems.size(); ++i)
212  {
213  PropertyItem* pitem = dynamic_cast<PropertyItem*>(propItems[i]);
214 
215  if(pitem->isSelected())
216  {
217  selected.push_back(pitem->getProperty());
218  }
219  }
220 
221  return selected;
222 }
223 
224 const std::string te::vp::LayerItem::getItemType() const
225 {
226  return "LAYER_ITEM";
227 }
bool setData(int column, const QVariant &value, int role=Qt::EditRole)
Definition: LayerItem.cpp:152
This is the base class for layers.
Definition: AbstractLayer.h:76
int columnCount() const
Definition: LayerItem.cpp:95
The class that represents an item in a LayerTreeModel.
A class that represents a group of legends in a LayerTreeModel.
QMenu * getMenu(QWidget *parent=0) const
Definition: LayerItem.cpp:114
te::dt::Property * getProperty() const
const std::vector< Rule * > & getRules() const
Definition: Style.cpp:94
std::vector< te::dt::Property * > getSelected()
Get the selected Properties of the Layer.
Definition: LayerItem.cpp:205
void setSelected(bool selected)
Set the Property as selected.
A class that represents a group of Properties in a LayerTreeModel.
std::vector< te::qt::widgets::AbstractTreeItem * > m_items
Definition: LayerItem.h:93
bool isSelected()
Check if the Property is selected.
virtual te::se::Style * getStyle() const
It returns the Style associated to the layer.
bool canFetchMore() const
Definition: LayerItem.cpp:126
bool hasChildren() const
Definition: LayerItem.cpp:147
A class that represents a Property in a LayerTreeModel.
LayerItem(te::map::AbstractLayerPtr layer, QObject *parent=0)
Definition: LayerItem.cpp:41
te::map::AbstractLayerPtr getLayer() const
Definition: LayerItem.cpp:190
Qt::ItemFlags flags() const
Definition: LayerItem.cpp:142
QVariant data(int column, int role) const
Definition: LayerItem.cpp:100
te::vp::LegendGroupItem * m_legendGroup
Definition: LayerItem.h:96
A class that represents a Layer in a LayerTreeModel.
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
te::map::AbstractLayerPtr m_layer
Terralib Layer of the LayerItem.
Definition: LayerItem.h:94
bool isSelected()
Check if the Layer is selected.
Definition: LayerItem.cpp:200
te::vp::PropertyGroupItem * m_propertyGroup
Definition: LayerItem.h:95
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
const std::string getItemType() const
It returns the item type.
Definition: LayerItem.cpp:224