All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSetDisplay.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/myqt/widgets/mapdisplay/DataSetDisplay.cpp
22 
23  \brief A map display for a dataset.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSet.h"
28 #include "../../../dataaccess/dataset/DataSetType.h"
29 #include "../../../dataaccess/datasource/DataSource.h"
30 #include "../../../dataaccess/datasource/DataSourceManager.h"
31 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
32 #include "../../../dataaccess/utils/Utils.h"
33 #include "../../../geometry/Envelope.h"
34 #include "../../../geometry/Geometry.h"
35 #include "../../../geometry/GeometryProperty.h"
36 #include "../canvas/Canvas.h"
37 #include "../Utils.h"
38 #include "DataSetDisplay.h"
39 
40 // STL
41 #include <cassert>
42 
43 // Qt
44 #include <QMessageBox>
45 #include <QResizeEvent>
46 
47 te::qt::widgets::DataSetDisplay::DataSetDisplay(QWidget * parent, Qt::WindowFlags f)
48  : QFrame(parent, f)
49 {
50 }
51 
53 {
54 }
55 
57 {
58  if(ds.get() == 0)
59  return;
60 
61  try
62  {
63  te::da::DataSourcePtr auxDs = te::da::DataSourceManager::getInstance().get(ds->getId(), ds->getType(), ds->getConnInfo());
64 
65  draw(dataset, auxDs);
66  }
67  catch(const std::exception& e)
68  {
69  QMessageBox::warning(this,
70  tr("TerraLib Qt Components"),
71  tr(e.what()));
72  }
73  catch(...)
74  {
75  QMessageBox::warning(this,
76  tr("TerraLib Qt Components"),
77  tr("Unknown error when displaying dataset!"));
78  }
79 }
80 
82 {
83  if((dataset.get() == 0) || (ds.get() == 0))
84  return;
85 
86  if(m_canvas.get() == 0)
87  m_canvas.reset(new Canvas(width(), height()));
88 
89  if(dataset->size() == 0)
90  te::da::LoadProperties(dataset.get(), ds->getId());
91 
92  if(!dataset->hasGeom())
93  {
94  m_canvas->setBackgroundColor(te::color::RGBAColor(0, 0, 0, 255)); // fill black
95  repaint();
96  return;
97  }
98 
100 
101  std::auto_ptr<te::gm::Envelope> mbr(te::da::GetExtent(dataset->getName(), gp->getName(), ds->getId()));
102 
103  if(mbr.get() == 0)
104  return;
105 
106  m_canvas->calcAspectRatio(mbr.get());
107 
108  m_canvas->setWindow(mbr->getLowerLeftX(), mbr->getLowerLeftY(), mbr->getUpperRightX(), mbr->getUpperRightY());
109 
110  switch(gp->getGeometryType())
111  {
112  case te::gm::PolygonType:
120  {
121  Config2DrawPolygons(m_canvas.get(), Qt::red, Qt::black);
122  }
123  break;
124 
133  {
134  Config2DrawLines(m_canvas.get(), Qt::black);
135  }
136  break;
137 
138  case te::gm::PointType:
139  case te::gm::PointZType:
140  case te::gm::PointMType:
141  case te::gm::PointZMType:
146  {
147  Config2DrawPoints(m_canvas.get(), "circle", 1, Qt::black, Qt::transparent, 1);
148  }
149  break;
150 
151  default:
152  break;
153  }
154 
155  if(datasetData)
156  {
157  std::size_t gpos = te::da::GetFirstPropertyPos(datasetData, te::dt::GEOMETRY_TYPE);
158 
159  while(datasetData->moveNext())
160  {
161  std::auto_ptr<te::gm::Geometry> g(datasetData->getGeometry(gpos));
162  m_canvas->draw(g.get());
163  }
164  }
165  else
166  {
167  std::auto_ptr<te::da::DataSet> feature(ds->getDataSet(dataset->getName()));
168 
169  std::size_t gpos = te::da::GetFirstPropertyPos(feature.get(), te::dt::GEOMETRY_TYPE);
170 
171  while(feature->moveNext())
172  {
173  std::auto_ptr<te::gm::Geometry> g(feature->getGeometry(gpos));
174  m_canvas->draw(g.get());
175  }
176  }
177 
178  repaint();
179 }
180 
182 {
183  if(m_canvas.get() == 0)
184  m_canvas.reset(new Canvas(width(), height()));
185 
186  m_canvas->setBackgroundColor(te::color::RGBAColor(255, 255, 255, 0));
187  m_canvas->clear();
188 
189  repaint();
190 }
191 
193 {
194  if(m_canvas.get() == 0)
195  return;
196 
197  QPainter painter(this);
198 
199  painter.drawPixmap(0, 0, *(m_canvas->getPixmap()));
200 
201  painter.end();
202 
203 }
204 
206 {
207  assert(e);
208 
209  if(m_canvas.get() == 0)
210  return;
211 
212  m_canvas->resize(e->size().width(), e->size().height());
213 
214  QFrame::resizeEvent(e);
215 }
216 
Geometric property.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Definition: Utils.cpp:133
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:117
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
TEQTWIDGETSEXPORT void Config2DrawLines(te::map::Canvas *canvas, const QColor &color, const std::size_t &width=1)
It configs (i.e. prepares) the given canvas to draw lines.
Definition: Utils.cpp:230
void draw(const te::da::DataSetTypePtr &dataset, const te::da::DataSourcePtr &ds, te::da::DataSet *datasetData=0)
DataSetDisplay(QWidget *parent=0, Qt::WindowFlags f=0)
TEQTWIDGETSEXPORT void Config2DrawPolygons(te::map::Canvas *canvas, const QColor &fillColor, const QColor &contourColor, const std::size_t &contourWidth=1)
It configs (i.e. prepares) the given canvas to draw polygons.
Definition: Utils.cpp:223
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
TEQTWIDGETSEXPORT void Config2DrawPoints(te::map::Canvas *canvas, const QColor &color, const std::size_t &width=1)
It configs (i.e. prepares) the given canvas to draw points.
Definition: Utils.cpp:236
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
A canvas built on top of Qt.
Definition: Canvas.h:54
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void resizeEvent(QResizeEvent *e)
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:432
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:508
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
const std::string & getName() const
It returns the property name.
Definition: Property.h:126