Renderer.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/edit/qt/Renderer.cpp
22 
23  \brief This is a singleton for rendering geometries and features.
24 */
25 
26 // TerraLib
27 #include "../../common/STLUtils.h"
28 #include "../../dataaccess/dataset/ObjectId.h"
29 #include "../../geometry/Envelope.h"
30 #include "../../geometry/Geometry.h"
31 #include "../../geometry/LineString.h"
32 #include "../../maptools/MarkRendererManager.h"
33 #include "../../se/Mark.h"
34 #include "../../se/Utils.h"
35 #include "../../qt/widgets/canvas/Canvas.h"
36 #include "../../qt/widgets/Utils.h"
37 #include "../../srs/Config.h"
38 #include "../Feature.h"
39 #include "../Repository.h"
40 #include "../RepositoryManager.h"
41 #include "Renderer.h"
42 
43 // STL
44 #include <map>
45 
47 : m_canvas(nullptr),
48  m_srid(TE_UNKNOWN_SRS),
49  m_currentGeomType(te::gm::UnknownGeometryType)
50 {
52 }
53 
55 {
56  delete m_canvas;
57 }
58 
59 void te::edit::Renderer::begin(QPaintDevice* device, const te::gm::Envelope& e, int srid)
60 {
61  delete m_canvas;
62  m_canvas = new te::qt::widgets::Canvas(device->width(), device->height());
63  m_canvas->setDevice(device, false);
64  m_canvas->setWindow(e.m_llx, e.m_lly, e.m_urx, e.m_ury);
65  m_canvas->setRenderHint(QPainter::Antialiasing, true);
66  m_srid = srid;
67 }
68 
70 {
71  const std::map<std::string, Repository*>& repositories = RepositoryManager::getInstance().getRepositories();
72 
73  std::map<std::string, Repository*>::const_iterator it;
74  for(it = repositories.begin(); it != repositories.end(); ++it)
75  drawRepository(it->first, e, srid);
76 }
77 
78 void te::edit::Renderer::drawRepository(const std::string& source, const te::gm::Envelope& e, int srid)
79 {
80  Repository* repository = RepositoryManager::getInstance().getRepository(source);
81 
82  if(repository == nullptr)
83  return;
84 
85  std::vector<Feature*> features = repository->getFeatures(e, srid);
86 
87  for (std::size_t i = 0; i < features.size(); ++i)
88  {
89  if (features[i]->getOperationTypeId() == DELETE || features[i]->hasColorChanged())
90  {
91  m_styleChanged = true;
92  break;
93  }
94  }
95 
96  for (std::size_t i = 0; i < features.size(); ++i)
97  draw(features[i], false);
98 }
99 
101 {
102  assert(m_canvas);
103  assert(feature);
104 
105  te::gm::GeomType type = feature->getGeometry()->getGeomTypeId();
106  te::edit::OperationType oType = feature->getOperationTypeId();
107 
108  if (m_currentGeomType == type && m_styleChanged == false)
109  return; // No need reconfigure the canvas
110 
111  m_currentGeomType = type;
112 
113  switch(type)
114  {
115  case te::gm::PolygonType:
123  {
126 
127  if (feature->hasColorChanged())
128  {
131  }
132 
133  if (oType == DELETE)
134  {
135  QBrush f;
136 
137  f.setColor(Qt::black);
138  f.setStyle(Qt::DiagCrossPattern);
139 
141  }
142  }
143  break;
144 
153  {
156 
157  if (feature->hasColorChanged())
158  m_canvas->setLineColor(feature->getFillColor());
159 
160  if (oType == DELETE)
162  }
163  break;
164 
165  case te::gm::PointType:
166  case te::gm::PointZType:
167  case te::gm::PointMType:
168  case te::gm::PointZMType:
173  {
175 
176  if (feature->hasColorChanged())
177  m_canvas->setPointColor(feature->getFillColor());
178 
179  if (oType == DELETE)
180  {
181  std::size_t size = 24;
182 
183  te::se::Stroke* stroke = te::se::CreateStroke("#FF0000", "2", "0.5");
184  te::se::Fill* fill = te::se::CreateFill("#FFFFFF", "0.5");
185  te::se::Mark* mark = te::se::CreateMark("square", stroke, fill);
186 
188 
190  m_canvas->setPointPattern(rgba, (int)size, (int)size);
191 
192  te::common::Free(rgba, size);
193  delete mark;
194  }
195  }
196  break;
197 
198  default:
199  return;
200  }
201 }
202 
204 {
205  assert(m_canvas);
206 
207  if (m_currentGeomType == type && m_styleChanged == false)
208  return; // No need reconfigure the canvas
209 
210  m_currentGeomType = type;
211 
212  switch(type)
213  {
214  case te::gm::PolygonType:
222 
225 
226  break;
227 
236 
239 
240  break;
241 
242  case te::gm::PointType:
243  case te::gm::PointZType:
244  case te::gm::PointMType:
245  case te::gm::PointZMType:
250 
252 
253  break;
254 
255  default:
256  return;
257  }
258 }
259 
260 void te::edit::Renderer::draw(te::gm::Geometry* geom, bool showVertexes)
261 {
262  assert(m_canvas);
263  assert(geom);
264 
265  if((geom->getSRID() != TE_UNKNOWN_SRS) && (m_srid != TE_UNKNOWN_SRS) && (geom->getSRID() != m_srid))
266  geom->transform(m_srid);
267 
268  prepare(geom->getGeomTypeId());
269 
270  m_canvas->draw(geom);
271 
272  if(showVertexes)
273  drawVertexes(geom);
274 }
275 
276 void te::edit::Renderer::draw(te::edit::Feature* feature, bool showVertexes)
277 {
278  assert(m_canvas);
279  assert(feature);
280 
281  if ((feature->getGeometry()->getSRID() != TE_UNKNOWN_SRS) && (m_srid != TE_UNKNOWN_SRS) && (feature->getGeometry()->getSRID() != m_srid))
282  feature->getGeometry()->transform(m_srid);
283 
284  prepare(feature);
285 
286  m_canvas->draw(feature->getGeometry());
287 
288  if (showVertexes)
289  drawVertexes(feature->getGeometry());
290 }
291 
293 {
294  assert(geom);
295 
296  std::vector<te::gm::LineString*> lines;
297  GetLines(geom, lines);
298 
300 
301  drawVertexes(lines);
302 }
303 
304 void te::edit::Renderer::drawVertexes(const std::vector<te::gm::LineString*>& lines)
305 {
306  for(std::size_t i = 0; i < lines.size(); ++i)
307  drawVertexes(lines[i]);
308 }
309 
311 {
312  assert(m_canvas);
313  assert(line);
314 
315  if((line->getSRID() != TE_UNKNOWN_SRS) && (m_srid != TE_UNKNOWN_SRS) && (line->getSRID() != m_srid))
316  line->transform(m_srid);
317 
318  for(std::size_t j = 0; j < line->getNPoints(); ++j)
319  {
320  std::unique_ptr<te::gm::Point> point(line->getPointN(j));
321  m_canvas->draw(point.get());
322  }
323 }
324 
325 void te::edit::Renderer::drawText(const std::string& text, te::gm::Point* p)
326 {
327  assert(m_canvas);
328  assert(p);
329 
332 
333  m_canvas->setFontFamily("Arial");
335 
336  m_canvas->drawText(p, text);
337 }
338 
340 {
341  delete m_canvas;
342  m_canvas = nullptr;
343 
345 
347 
349 }
350 
351 void te::edit::Renderer::setPolygonStyle(const QColor& fillColor, const QColor& contourColor, const std::size_t& contourWidth)
352 {
353  m_polygonFillColor = fillColor;
354  m_polygonContourColor = contourColor;
355  m_polygonContourWidth = contourWidth;
356 
357  m_styleChanged = true;
358 
359 }
360 
361 void te::edit::Renderer::setPointStyle(const QString& mark, const QColor& fillColor, const QColor& contourColor,
362  const std::size_t& contourWidth, const std::size_t& size)
363 {
364  m_pointMark = mark;
365  m_pointFillColor = fillColor;
366  m_pointContourColor = contourColor;
367  m_pointContourWidth = contourWidth;
368  m_pointSize = size;
369 
370  m_styleChanged = true;
371 }
372 
373 void te::edit::Renderer::setLineStyle(const QColor& lineColor, const std::size_t& lineWidth)
374 {
375  m_lineColor = lineColor;
376  m_lineWidth = lineWidth;
377 
378  m_styleChanged = true;
379 }
380 
381 void te::edit::Renderer::setTextStyle(const QColor& textColor, const std::size_t& textPointSize)
382 {
383  m_textFillColor = textColor;
384  m_textPointSize = textPointSize;
385 }
386 
388 {
389  QSettings sett(QSettings::IniFormat, QSettings::UserScope, qApp->organizationName(), qApp->applicationName());
390 
391  QString fillColor = sett.value("edit_config/defaultFillColor").toString();
392  QColor fColor(fillColor);
393 
394  if (!fColor.isValid())
395  {
398  }
399  else
400  {
401  m_polygonFillColor = fColor;
402  m_pointFillColor = fColor;
403  }
404 
405  QString lineColor = sett.value("edit_config/defaultLineColor").toString();
406  QColor lColor(lineColor);
407 
408  if (!lColor.isValid())
409  {
413  }
414  else
415  {
416  m_polygonContourColor = lColor;
417  m_lineColor = lColor;
418  m_pointContourColor = lColor;
419  }
420 
421  int width = sett.value("edit_config/defaultWidth").toInt();
422  if (width < 1)
423  {
427  }
428  else
429  {
430  m_polygonContourWidth = width;
431  m_lineWidth = width;
432  m_pointContourWidth = width;
433  }
434 
435  QString fillTextColor = sett.value("edit_config/defaultTextFillColor").toString();
436  QColor fTxtColor(fillTextColor);
437 
438  if (!fTxtColor.isValid())
439  m_textFillColor = QColor(0, 0, 0);
440  else
441  m_textFillColor = fTxtColor;
442 
443  int textPointSize = sett.value("edit_config/defaultTextPointSize").toInt();
444  if (textPointSize < 8)
445  m_textPointSize = 8;
446  else
447  m_textPointSize = textPointSize;
448 
449  m_pointMark = "circle";
450  m_pointSize = 8;
451 
452  int lineDashStyle = sett.value("edit_config/defaultLineDashStyle", te::map::SolidLine).toInt();
453  m_lineDashStyle = static_cast<te::map::LineDashStyle>(lineDashStyle);
454 
455  m_styleChanged = false;
456 }
te::gm::GeomType m_currentGeomType
Definition: Renderer.h:142
void setPolygonContourDashStyle(te::map::LineDashStyle style)
It sets the polygon contour dash style.
TEQTWIDGETSEXPORT te::color::RGBAColor Convert2TerraLib(const QColor &color)
It converts a Qt Color to TerraLib Color.
~Renderer()
Singleton destructor.
Definition: Renderer.cpp:54
#define TE_EDIT_FEATURE_FILL_COLOR
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
#define TE_EDIT_FEATURE_CONTOUR_WIDTH
void setDevice(QPaintDevice *device, bool takeOwnerShip)
It sets new device as QPrinter.
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
void drawText(int x, int y, const std::string &txt, float angle=0.0, double anchorX=0.5, double anchorY=0.5, int displacementX=0, int displacementY=0)
It draws a text.
void setLineStyle(const QColor &lineColor, const std::size_t &lineWidth)
Definition: Renderer.cpp:373
void setPointStyle(const QString &mark, const QColor &fillColor, const QColor &contourColor, const std::size_t &contourWidth, const std::size_t &size)
Definition: Renderer.cpp:361
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
QColor m_textFillColor
Definition: Renderer.h:139
std::size_t m_lineWidth
Definition: Renderer.h:131
bool hasColorChanged()
Definition: Feature.cpp:164
te::color::RGBAColor getFillColor() const
Definition: Feature.cpp:149
QString m_pointMark
Definition: Renderer.h:133
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
QColor m_lineColor
Definition: Renderer.h:130
std::size_t m_pointSize
Definition: Renderer.h:137
void setupDefaultStyle()
Definition: Renderer.cpp:387
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.
double m_urx
Upper right corner x-coordinate.
std::unique_ptr< Point > getPointN(std::size_t i) const
It returns the specified point in this LineString.
void setPolygonFillColor(const te::color::RGBAColor &color)
It sets the color used to fill the draw of polygon geometries.
void drawRepositories(const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:69
te::gm::Geometry * getGeometry() const
Definition: Feature.cpp:139
void setPolygonContourColor(const te::color::RGBAColor &color)
It sets the pen color used to draw the boundary of polygon geometries.
void draw(const te::gm::Geometry *geom)
It draws the geometry on canvas.
te::qt::widgets::Canvas * m_canvas
Definition: Renderer.h:123
te::edit::OperationType getOperationTypeId() const
Definition: Feature.cpp:159
unsigned int line
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.
std::size_t m_polygonContourWidth
Definition: Renderer.h:128
void setFontFamily(const std::string &family)
It sets the text font family.
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
te::color::RGBAColor getContourColor() const
Definition: Feature.cpp:154
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
void setRenderHint(QPainter::RenderHint hint, bool on=true)
Sets the given render hint on the canvas painter if on is true; otherwise clears the render hint...
void setWindow(const double &llx, const double &lly, const double &urx, const double &ury)
It sets the world (or window) coordinates area (supposing a cartesian reference system).
double m_llx
Lower left corner x-coordinate.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
std::size_t m_pointContourWidth
Definition: Renderer.h:136
static RepositoryManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
void setPointColor(const te::color::RGBAColor &color)
It sets the point drawing color.
void setLineColor(const te::color::RGBAColor &color)
It sets the pen color used to draw line geometries.
URI C++ Library.
Definition: Attributes.h:37
void setTextStyle(const QColor &textColor, const std::size_t &textPointSize)
Definition: Renderer.cpp:381
void draw(te::gm::Geometry *geom, bool showVertexes=false)
Definition: Renderer.cpp:260
te::gm::Polygon * p
std::size_t getNPoints() const
It returns the number of points (vertexes) in the linestring.
Definition: LineString.h:193
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
void drawText(const std::string &text, te::gm::Point *p)
Definition: Renderer.cpp:325
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.
TESEEXPORT Mark * CreateMark(const std::string &wellKnownName, Stroke *stroke, Fill *fill)
Creates a mark.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
void setTextWeight(te::se::Font::FontWeightType weight)
It sets the text weight.
std::size_t m_textPointSize
Definition: Renderer.h:140
#define TE_EDIT_FEATURE_CONTOUR_COLOR
double m_lly
Lower left corner y-coordinate.
void prepare(te::gm::GeomType type)
Definition: Renderer.cpp:203
TESEEXPORT Stroke * CreateStroke(const std::string &color, const std::string &width)
Creates a stroke.
Renderer()
It initializes the singleton instance of the renderer.
Definition: Renderer.cpp:46
std::vector< Feature * > getFeatures(const te::gm::Envelope &e, int srid) const
Definition: Repository.cpp:182
LineDashStyle
This enum encodes enumerated values telling how lines should be drawn. e.g. as a plain line or dash l...
double m_ury
Upper right corner y-coordinate.
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
void drawVertexes(te::gm::Geometry *geom)
Definition: Renderer.cpp:292
void begin(QPaintDevice *device, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:59
TEEDITEXPORT void GetLines(te::gm::Geometry *geom, std::vector< te::gm::LineString * > &lines)
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
QColor m_pointFillColor
Definition: Renderer.h:134
void drawRepository(const std::string &source, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:78
void setPointPattern(te::color::RGBAColor **pattern, int ncols, int nrows)
It sets the point pattern.
This class represents a repository of geometries and features.
Definition: Repository.h:63
QColor m_polygonContourColor
Definition: Renderer.h:127
This is a singleton for rendering geometries and features.
QColor m_pointContourColor
Definition: Renderer.h:135
QColor m_polygonFillColor
Definition: Renderer.h:126
void setLineDashStyle(te::map::LineDashStyle style)
It sets the line dash style.
void setPolygonStyle(const QColor &fillColor, const QColor &contourColor, const std::size_t &contourWidth)
Definition: Renderer.cpp:351
virtual void transform(int srid) _NOEXCEPT_OP(false)=0
It converts the coordinate values of the geometry to the new spatial reference system.
#define TE_TRANSPARENT
For an RGBA color this is the value of the alpha-channel for totally transparent. ...
void setTextColor(const te::color::RGBAColor &color)
It sets the text drawing color.
void setTextPointSize(double psize)
It sets the text point Size.
te::map::LineDashStyle m_lineDashStyle
Definition: Renderer.h:145
void transform(int srid)
It converts the coordinate values of the linestring to the new spatial reference system.
TESEEXPORT Fill * CreateFill(const std::string &color, const std::string &opacity)
Creates a fill.