WellKnownMarkRenderer.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/WellKnownMarkRenderer.cpp
22 
23  \brief A concrete renderer based on Qt4 for conversion of Symbology Enconding Mark elements to an image pattern.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "../../../maptools/Utils.h"
29 #include "../../../se/Mark.h"
30 #include "../Utils.h"
31 #include "WellKnownMarkRenderer.h"
32 #include "Utils.h"
33 
34 // STL
35 #include <vector>
36 
37 // MarkMap::<name -> MarkType>
38 std::map<std::string, te::qt::widgets::WellKnownMarkRenderer::MarkType> te::qt::widgets::WellKnownMarkRenderer::sm_markMap;
39 
41 
42 {
43  m_brush.setStyle(Qt::SolidPattern);
44  m_brush.setColor(QColor(TE_SE_DEFAULT_FILL_BASIC_COLOR));
45  buildMaps();
46  buildPaths();
47 }
48 
50 
52 {
53  // Supports the mark?
54  const std::string* name = mark->getWellKnownName();
55  std::string lname = te::common::Convert2LCase(*name);
56  std::map<std::string, MarkType>::iterator it = sm_markMap.find(lname);
57  if(it == sm_markMap.end()) // Not recognize mark!
58  return nullptr;
59 
60  // Gets the correct mark type from mark name
61  MarkType markType = it->second;
62 
63  // Creates the image that will represent the graphical mark pattern
64  QImage* img = new QImage(static_cast<int>(size), static_cast<int>(size), QImage::Format_ARGB32_Premultiplied);
65  img->fill(Qt::transparent);
66 
67  // Configuring visual...
70 
71  // Let's draw the mark!
72  switch(markType)
73  {
75  draw(img, m_squarePath);
76  break;
77 
79  draw(img, m_circlePath);
80  break;
81 
83  draw(img, m_trianglePath);
84  break;
85 
87  draw(img, m_starPath);
88  break;
89 
91  draw(img, m_crossPath);
92  break;
93 
95  draw(img, m_xPath);
96  break;
97 
99  draw(img, m_diamondPath);
100  break;
101 
103  draw(img, m_ellipsePath);
104  break;
105 
107  draw(img, m_semiCirclePath);
108  break;
109 
111  draw(img, m_pentagonPath);
112  break;
113 
115  draw(img, m_hexagonPath);
116  break;
117 
119  draw(img, m_octagonPath);
120  break;
121  }
122 
123  // Converts QImage to te::color::RGBA**
125 
126  delete img;
127 
128  return rgba;
129 }
130 
131 void te::qt::widgets::WellKnownMarkRenderer::getSupportedMarks(std::vector<std::string>& marks) const
132 {
133  std::map<std::string, MarkType>::const_iterator it;
134  for(it = sm_markMap.begin(); it != sm_markMap.end(); ++it)
135  marks.push_back(it->first);
136 }
137 
139 {
140  // MarkMap
153 }
154 
156 {
157  // Local transformation matrix
158  QTransform transform;
159 
160  // Square
161  m_squarePath.addRect(-0.5, -0.5, 1.0, 1.0);
162 
163  // Circle
164  m_circlePath.addEllipse(-0.5, -0.5, 1.0, 1.0);
165 
166  // Triangle
167  m_trianglePath.moveTo(0.0, 1.25);
168  m_trianglePath.lineTo(1.0, -0.75);
169  m_trianglePath.lineTo(-1.0, -0.75);
170  m_trianglePath.closeSubpath();
171  m_trianglePath.translate(0, -0.25);
172  m_trianglePath = transform.scale(0.5, 0.5).rotate(180).map(m_trianglePath);
173 
174  // Star
175  m_starPath.moveTo(0.191, 0.0);
176  m_starPath.lineTo(0.25, 0.344);
177  m_starPath.lineTo(0.0, 0.588);
178  m_starPath.lineTo(0.346, 0.638);
179  m_starPath.lineTo(0.5, 1.0);
180  m_starPath.lineTo(0.654, 0.638);
181  m_starPath.lineTo(1.0, 0.588);
182  m_starPath.lineTo(0.75, 0.344);
183  m_starPath.lineTo(0.8, 0.0);
184  m_starPath.lineTo(0.5, 0.162);
185  m_starPath.closeSubpath();
186  m_starPath.translate(-0.5, -0.5);
187  transform.reset();
188  m_starPath = transform.rotate(180).map(m_starPath);
189 
190  // Cross
191  m_crossPath.moveTo(0.5, 0.125);
192  m_crossPath.lineTo(0.125, 0.125);
193  m_crossPath.lineTo(0.125, 0.5);
194  m_crossPath.lineTo(-0.125, 0.5);
195  m_crossPath.lineTo(-0.125, 0.125);
196  m_crossPath.lineTo(-0.5, 0.125);
197  m_crossPath.lineTo(-0.5, -0.125);
198  m_crossPath.lineTo(-0.125, -0.125);
199  m_crossPath.lineTo(-0.125, -0.5);
200  m_crossPath.lineTo(0.125, -0.5);
201  m_crossPath.lineTo(0.125, -0.125);
202  m_crossPath.lineTo(0.5, -0.125);
203  m_crossPath.closeSubpath();
204 
205  // X
206  transform.reset();
207  m_xPath = transform.rotate(-45).map(m_crossPath);
208 
209  // Diamond
210  transform.reset();
211  m_diamondPath = transform.rotate(-45).scale(0.7, 0.7).map(m_squarePath);
212 
213  // Ellipse
214  m_ellipsePath.addEllipse(-0.5, -0.3, 1.0, 0.7);
215 
216  // Semicircle
217  m_semiCirclePath.arcTo(QRectF(-0.5, -0.5, 1.0, 1.0), 0.0, 180.0);
218  m_semiCirclePath.closeSubpath();
219 
220  // Pentagon
221  transform.reset();
222  m_pentagonPath.moveTo(-0.6, -0.75);
223  m_pentagonPath.lineTo(-1.0, 0.50);
224  m_pentagonPath.lineTo(0.0, 1.25);
225  m_pentagonPath.lineTo(1.0, 0.50);
226  m_pentagonPath.lineTo(0.6, -0.75);
227  m_pentagonPath.closeSubpath();
228  m_pentagonPath.translate(0, -0.25);
229  m_pentagonPath = transform.scale(0.5, 0.5).rotate(180).map(m_pentagonPath);
230 
231  // Hexagon
232  transform.reset();
233  m_hexagonPath.moveTo(-0.5, -0.5);
234  m_hexagonPath.lineTo(-1.0, 0.4);
235  m_hexagonPath.lineTo(-0.5, 1.1);
236  m_hexagonPath.lineTo(0.5, 1.1);
237  m_hexagonPath.lineTo(1.0, 0.4);
238  m_hexagonPath.lineTo(0.5, -0.5);
239  m_hexagonPath.closeSubpath();
240  m_hexagonPath.translate(0, -0.25);
241  m_hexagonPath = transform.scale(0.5, 0.5).rotate(180).map(m_hexagonPath);
242 
243  // Octagon
244  transform.reset();
245  m_octagonPath.moveTo(-0.5, -0.75);
246  m_octagonPath.lineTo(-1.0, -0.15);
247  m_octagonPath.lineTo(-1.0, 0.75);
248  m_octagonPath.lineTo(-0.5, 1.25);
249  m_octagonPath.lineTo(0.5, 1.25);
250  m_octagonPath.lineTo(1.0, 0.75);
251  m_octagonPath.lineTo(1.0, -0.15);
252  m_octagonPath.lineTo(0.5, -0.75);
253  m_octagonPath.closeSubpath();
254  m_octagonPath.translate(0, -0.25);
255  m_octagonPath = transform.scale(0.5, 0.5).rotate(180).map(m_octagonPath);
256 }
257 
259 {
260  m_painter.begin(img);
261  m_painter.setRenderHints(QPainter::Antialiasing);
262  m_painter.setPen(m_pen);
263  m_painter.setBrush(m_brush);
264 }
265 
267 {
268  m_painter.end();
269  m_pen = QPen(QColor(TE_SE_DEFAULT_STROKE_BASIC_COLOR));
270  m_brush = QBrush(QColor(TE_SE_DEFAULT_FILL_BASIC_COLOR), Qt::SolidPattern);
271 }
272 
273 void te::qt::widgets::WellKnownMarkRenderer::draw(QImage* img, QPainterPath& path)
274 {
275  setup(img);
276 
277  // Transformation parameters
278  double s = img->width() - m_pen.width() - 1;
279  double t = img->width() * 0.5;
280 
281  QTransform transform;
282  QPainterPath transformedPath = transform.translate(t, t).scale(s, s).map(path);
283 
284  m_painter.drawPath(transformedPath);
285 
286  end();
287 }
MarkType
This enum specifies mark types handle by this renderer.
QPainterPath m_starPath
Pre-defined path to star mark.
void buildPaths()
Builds the pre-defined marks paths.
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
QPen m_pen
The pen used to draw the mark patterns.
QPainterPath m_pentagonPath
Pre-defined path to pentagon mark.
std::string Convert2LCase(const std::string &value)
It converts a string to lower case.
Definition: StringUtils.h:202
QPainterPath m_xPath
Pre-defined path to x mark.
void setup(QImage *img)
Setups the internal QPainter and associates it with the given paint device (QImage).
void getSupportedMarks(std::vector< std::string > &marks) const
Method that informs the set of supported marks by a specific concrete mark renderer.
QBrush m_brush
The pen used to draw the mark patterns.
void end()
Finalizes the internal QPainter resources.
QPainterPath m_diamondPath
Pre-defined path to diamond mark.
TEQTWIDGETSEXPORT void Config(QPen &pen, const te::se::Stroke *stroke)
It configs the given pen based on Symbology Enconding Stroke element.
A concrete renderer based on Qt4 for conversion of Symbology Enconding Mark elements to an image patt...
te::color::RGBAColor ** render(const te::se::Mark *mark, std::size_t size)
It generates the image pattern from the given Symbology Enconding Mark element.
QPainterPath m_trianglePath
Pre-defined path to triangle mark.
QPainterPath m_ellipsePath
Pre-defined path to ellipse mark.
QPainterPath m_hexagonPath
Pre-defined path to hexagon mark.
const std::string * getWellKnownName() const
Definition: Mark.cpp:60
const Stroke * getStroke() const
Definition: Mark.cpp:125
static std::map< std::string, MarkType > sm_markMap
A map that associates a well-known mark name to the correct mark type.
void buildMaps()
Builds the internal auxiliary maps to map Symbology Enconding concepts to Qt.
QPainter m_painter
The painter used to draw the mark patterns.
QPainterPath m_crossPath
Pre-defined path to cross mark.
TEQTWIDGETSEXPORT QImage * GetImage(te::color::RGBAColor **img, int width, int height)
It creates a QImage from an RGBA color array.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
#define TE_SE_DEFAULT_FILL_BASIC_COLOR
It specifies the default color used by basic fill (solid colors).
QPainterPath m_semiCirclePath
Pre-defined path to semicircle mark.
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
Utility functions for MapTools module.
QPainterPath m_squarePath
Pre-defined path to square mark.
QPainterPath m_octagonPath
Pre-defined path to octagon mark.
QPainterPath m_circlePath
Pre-defined path to circle mark.
void draw(QImage *img, QPainterPath &path)
Draw the given path on the the given image using the internal pen and brush.
const Fill * getFill() const
Definition: Mark.cpp:114