All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 <QtGui/QMessageBox>
45 #include <QtGui/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  std::auto_ptr<te::da::DataSet> feature(ds->getDataSet(dataset->getName()));
156 
157  std::size_t gpos = te::da::GetFirstPropertyPos(feature.get(), te::dt::GEOMETRY_TYPE);
158 
159  while(feature->moveNext())
160  {
161  std::auto_ptr<te::gm::Geometry> g(feature->getGeometry(gpos));
162  m_canvas->draw(g.get());
163  }
164 
165  repaint();
166 }
167 
169 {
170  if(m_canvas.get() == 0)
171  m_canvas.reset(new Canvas(width(), height()));
172 
173  m_canvas->setBackgroundColor(te::color::RGBAColor(255, 255, 255, 0));
174  m_canvas->clear();
175 
176  repaint();
177 }
178 
180 {
181  if(m_canvas.get() == 0)
182  return;
183 
184  QPainter painter(this);
185 
186  painter.drawPixmap(0, 0, *(m_canvas->getPixmap()));
187 
188  painter.end();
189 
190 }
191 
193 {
194  assert(e);
195 
196  if(m_canvas.get() == 0)
197  return;
198 
199  m_canvas->resize(e->size().width(), e->size().height());
200 
201  QFrame::resizeEvent(e);
202 }
203 
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 resizeEvent(QResizeEvent *e)
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:504
const std::string & getName() const
It returns the property name.
Definition: Property.h:126
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Definition: Utils.cpp:132
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A canvas built on top of Qt.
Definition: Canvas.h:54
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:428
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
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
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:116
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
Geometric property.
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
DataSetDisplay(QWidget *parent=0, Qt::WindowFlags f=0)
void draw(const te::da::DataSetTypePtr &dataset, const te::da::DataSourcePtr &ds)