SymbologyPreview.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/se/SymbologyPreview.cpp
22 
23  \brief Static class used to generate preview of Symbology elements.
24 */
25 
26 // TerraLib
27 #include "../../../geometry/Envelope.h"
28 #include "../../../geometry/Polygon.h"
29 #include "../../../geometry/LinearRing.h"
30 #include "../../../geometry/Point.h"
31 #include "../../../maptools/CanvasConfigurer.h"
32 #include "../../../se/AnchorPoint.h"
33 #include "../../../se/Displacement.h"
34 #include "../../../se/LabelPlacement.h"
35 #include "../../../se/PointPlacement.h"
36 #include "../../../se/Rule.h"
37 #include "../../../se/Symbolizer.h"
38 #include "../../../se/TextSymbolizer.h"
39 #include "../../../se/Utils.h"
40 #include "../canvas/Canvas.h"
41 #include "SymbologyPreview.h"
42 #include "Symbol.h"
43 
44 // Qt
45 #include <QIcon>
46 
47 // STL
48 #include <cassert>
49 
50 QPixmap te::qt::widgets::SymbologyPreview::build(const te::se::Symbolizer* symb, const QSize& size)
51 {
52  double offset = 2.0;
53 
54  te::gm::Geometry* geom = nullptr;
55  if(symb->getType() == "PolygonSymbolizer")
56  {
59  ring->setPoint(0, offset, offset);
60  ring->setPoint(1, size.width() - offset, offset);
61  ring->setPoint(2, size.width() - offset, size.height() - offset);
62  ring->setPoint(3, offset, size.height() - offset);
63  ring->setPoint(4, offset, offset);
64  polygon->setRingN(0, ring);
65  geom = polygon;
66  }
67  else if(symb->getType() == "LineSymbolizer")
68  {
70  line->setPoint(0, offset, size.height() * 0.5);
71  line->setPoint(1, size.width() - offset, size.height() * 0.5);
72  geom = line;
73  }
74  else if(symb->getType() == "PointSymbolizer")
75  {
76  geom = new te::gm::Point(size.width() * 0.5, size.height() * 0.5);
77  }
78  else if(symb->getType() == "RasterSymbolizer")
79  {
80  QIcon raster = QIcon::fromTheme("raster-symbolizer");
81  return raster.pixmap(size);
82  }
83  else if (symb->getType() == "TextSymbolizer")
84  {
85  QPixmap result = buildText(symb, size);
86 
87  return result;
88  }
89 
90  QPixmap result = build(symb, geom, size);
91 
92  delete geom;
93 
94  return result;
95 }
96 
97 QPixmap te::qt::widgets::SymbologyPreview::build(const std::vector<te::se::Symbolizer*>& symbs, const QSize& size)
98 {
99  QPixmap result(size);
100  result.fill(Qt::transparent);
101 
102  // Let's draw!
103  QPainter painter(&result);
104  for(std::size_t i = 0; i < symbs.size(); ++i)
105  {
106  QPixmap pix = build(symbs[i], size);
107  painter.drawPixmap(0, 0, pix);
108  }
109 
110  return result;
111 }
112 
113 QPixmap te::qt::widgets::SymbologyPreview::build(const te::se::Rule* rule, const QSize& size)
114 {
115  assert(rule);
116 
117  return build(rule->getSymbolizers(), size);
118 }
119 
120 QPixmap te::qt::widgets::SymbologyPreview::build(const te::se::Symbolizer* symb, const te::gm::Geometry* geom, const QSize& size)
121 {
122  assert(symb);
123 
124  if(symb->getType() == "RasterSymbolizer")
125  return QPixmap();
126 
127  assert(geom);
128  assert(!size.isEmpty());
129 
130  // Creating a canvas...
131  te::qt::widgets::Canvas canvas(size.width(), size.height());
132  canvas.setWindow(0.0, 0.0, size.width(), size.height());
133  canvas.setBackgroundColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT));
134 
135  // Configuring...
136  te::map::CanvasConfigurer cc(&canvas);
137  cc.config(symb);
138 
139  // Let's draw!
140  canvas.draw(geom);
141 
142  return *canvas.getPixmap();
143 }
144 
146 {
147  assert(symb);
148 
149  if (symb->getType() != "TextSymbolizer")
150  return QPixmap();
151 
152  assert(!size.isEmpty());
153 
154  // Creating a canvas...
155  te::qt::widgets::Canvas canvas(size.width(), size.height());
156  canvas.setWindow(0.0, size.height(), size.width(), 0);
157  canvas.setBackgroundColor(te::color::RGBAColor(0, 0, 255, TE_TRANSPARENT));
158 
159  // Configuring...
160  te::map::CanvasConfigurer cc(&canvas);
161  cc.config(symb);
162 
163  // Let's draw!
164  double posX = size.width() / 2.;
165  double posY = size.height() / 2.;
166 
167  float angle = 0.0;
168  double anchorX = 0.5;
169  double anchorY = 0.5;
170  int displacementX = 0;
171  int displacementY = 0;
172 
173  const te::se::TextSymbolizer* ts = dynamic_cast<const te::se::TextSymbolizer*>(symb);
174 
175  if (ts && ts->getLabelPlacement() && ts->getLabelPlacement()->getPointPlacement())
176  {
178 
179  if (pp->getRotation())
180  angle = te::se::GetDouble(pp->getRotation());
181 
182  if (pp->getAnchorPoint())
183  {
186  }
187 
188  if (pp->getDisplacement())
189  {
190  displacementX = te::se::GetInt(pp->getDisplacement()->getDisplacementX());
191  displacementY = te::se::GetInt(pp->getDisplacement()->getDisplacementY());
192  }
193  }
194 
195  canvas.drawText(posX, posY, "ABC", angle, anchorX, anchorY, displacementX, displacementY);
196 
197  return *canvas.getPixmap();
198 }
199 
200 QPixmap te::qt::widgets::SymbologyPreview::build(const std::vector<te::se::Symbolizer*>& symbs, const te::gm::Geometry* geom, const QSize& size)
201 {
202  QPixmap result(size);
203  result.fill(Qt::transparent);
204 
205  // Let's draw!
206  QPainter painter(&result);
207  for(std::size_t i = 0; i < symbs.size(); ++i)
208  {
209  QPixmap pix = build(symbs[i], geom, size);
210  painter.drawPixmap(0, 0, pix);
211  }
212 
213  return result;
214 }
215 
216 QPixmap te::qt::widgets::SymbologyPreview::build(const te::se::Rule* rule, const te::gm::Geometry* geom, const QSize& size)
217 {
218  assert(rule);
219 
220  return build(rule->getSymbolizers(), geom, size);
221 }
222 
223 QPixmap te::qt::widgets::SymbologyPreview::build(const te::qt::widgets::Symbol* symbol, const te::gm::Geometry* geom, const QSize& size)
224 {
225  assert(symbol);
226 
227  return build(symbol->getSymbolizers(), geom, size);
228 }
A TextSymbolizer is used to render text labels according to various graphical parameters.
const ParameterValue * getDisplacementX() const
const AnchorPoint * getAnchorPoint() const
const LabelPlacement * getLabelPlacement() const
This class represents a symbol.
const Displacement * getDisplacement() const
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
Static class used to generate preview of Symbology elements.
TESEEXPORT double GetDouble(const te::se::ParameterValue *param)
It gets the parameter value as a double.
static QPixmap buildText(const te::se::Symbolizer *symb, const QSize &size)
Generates the preview of given text symbolizer element.
const ParameterValue * getRotation() const
const ParameterValue * getDisplacementY() const
unsigned int line
A PointPlacement specifies how a text label should be rendered relative to a geometric point...
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
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).
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
A point with x and y coordinate values.
Definition: Point.h:50
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
const ParameterValue * getAnchorPointX() const
Definition: AnchorPoint.cpp:48
This class represents a symbol. TODO: More description!
Definition: Symbol.h:54
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
const std::vector< Symbolizer * > & getSymbolizers() const
Definition: Rule.cpp:158
const std::vector< te::se::Symbolizer * > & getSymbolizers() const
It returns the list of Symbolizers that compose the symbol.
Definition: Symbol.cpp:75
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:76
virtual const std::string & getType() const =0
It returns the symbolizer type.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
TESEEXPORT int GetInt(const te::se::ParameterValue *param)
It gets the parameter value as an integer.
const ParameterValue * getAnchorPointY() const
Definition: AnchorPoint.cpp:59
void config(const te::se::Symbolizer *symbolizer)
It configs the canvas based on given symbolizer.
#define TE_TRANSPARENT
For an RGBA color this is the value of the alpha-channel for totally transparent. ...
const PointPlacement * getPointPlacement() const
static QPixmap build(const te::se::Symbolizer *symb, const QSize &size)
Generates the preview of given symbolizer element.
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
A Symbology Enconding visitor that configures a given canvas based on symbolizers elements...