GlyphMarkRenderer.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/GlyphMarkRenderer.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 "GlyphMarkRenderer.h"
32 #include "Utils.h"
33 
34 // STL
35 #include <algorithm>
36 
37 // Renderer key
39 
41 
42 {
43  m_brush.setStyle(Qt::SolidPattern);
44  m_brush.setColor(QColor(TE_SE_DEFAULT_FILL_BASIC_COLOR));
45 }
46 
48 
50 {
51  // Decoding...
52  QChar ch;
53  QString fontName;
54  QString name(mark->getWellKnownName()->c_str());
55  te::qt::widgets::GlyphMarkRenderer::decode(name, fontName, ch); // TODO: Can throw exceptions!
56 
57  // Configuring the font
58  QFont font;
59  font.setFamily(fontName);
60  font.setPixelSize(static_cast<int>(size));
61 
62  // Bulding the character path
63  QPainterPath charPath;
64  charPath.addText(0.0, 0.0, font, ch);
65  // Adjusting...
66  QRectF bounds = charPath.boundingRect();
67  double invMax = 1 / std::max(bounds.width(), bounds.height());
68  QTransform t;
69  t.scale(invMax, invMax);
70  t.translate(-bounds.center().x(), -bounds.center().y());
71  charPath = t.map(charPath);
72 
73  // Creates the image that will represent the graphical mark pattern
74  QImage* img = new QImage(static_cast<int>(size), static_cast<int>(size), QImage::Format_ARGB32_Premultiplied);
75  img->fill(Qt::transparent);
76 
77  // Configuring visual...
80 
81  // Let's draw the char path!
82  draw(img, charPath);
83 
84  // Converts QImage to te::color::RGBA**
86 
87  delete img;
88 
89  return rgba;
90 }
91 
93  std::vector<std::string>& /*marks*/) const
94 {
95 }
96 
97 QString te::qt::widgets::GlyphMarkRenderer::encode(const QString& font, const int& charCode)
98 {
99  QString result = QString::fromUtf8(sm_rendererKey.c_str());
100  result += "://" + font + "#0x" + QString::number(charCode, 16);
101  return result;
102 }
103 
104 void te::qt::widgets::GlyphMarkRenderer::decode(QString& name, QString& font, QChar& charCode)
105 {
106  // Extract the part important to this renderer!
107  QString pattern(name);
108  pattern.remove(0, static_cast<int>(sm_rendererKey.size() + 3)); // removing "ttf://"
109  if(!pattern.contains("#"))
110  return; // TODO: Exception: Bad format.
111 
112  // Tokenizes based on '#" separator. The first value is the font and the second is the char code...
113  QStringList myParams = pattern.split("#", QString::SkipEmptyParts);
114  if(myParams.size() < 2)
115  return; // TODO: Exception: Bad format.
116 
117  // Getting char
118  bool wasConverted = false;
119  // Base 0: if the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
120  charCode = myParams[1].toInt(&wasConverted, 0);
121  if(!wasConverted)
122  return; // TODO: Exception: Invalid char code.
123 
124  // The font name
125  font = myParams[0];
126 }
127 
129 {
130  m_painter.begin(img);
131  m_painter.setRenderHints(QPainter::Antialiasing);
132  m_painter.setPen(m_pen);
133  m_painter.setBrush(m_brush);
134 }
135 
137 {
138  m_painter.end();
139  m_pen = QPen(QColor(TE_SE_DEFAULT_STROKE_BASIC_COLOR));
140  m_brush = QBrush(QColor(TE_SE_DEFAULT_FILL_BASIC_COLOR), Qt::SolidPattern);
141 }
142 
143 void te::qt::widgets::GlyphMarkRenderer::draw(QImage* img, QPainterPath& path)
144 {
145  setup(img);
146 
147  // Transformation parameters
148  double s = img->width() - m_pen.width() - 1;
149  double t = img->width() * 0.5;
150 
151  QTransform transform;
152  QPainterPath transformedPath = transform.translate(t, t).scale(s, s).map(path);
153 
154  m_painter.drawPath(transformedPath);
155 
156  end();
157 }
158 
159 bool te::qt::widgets::GlyphMarkRenderer::getChar(QString& charCode, QChar& ch)
160 {
161  bool isOk = false;
162  // Base 0: if the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
163  ch = charCode.toInt(&isOk, 0);
164  return isOk;
165 }
static QString encode(const QString &font, const int &charCode)
Encodes the given parameters to a format known by this renderer.
A concrete renderer based on Qt4 for conversion of Symbology Enconding Mark elements to an image patt...
A Mark specifies a geometric shape and applies coloring to it.
Definition: Mark.h:84
bool getChar(QString &charCode, QChar &ch)
Converts the character code to a char.
QBrush m_brush
The pen used to draw the mark patterns.
void end()
Finalizes the internal QPainter resources.
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.
static void decode(QString &name, QString &font, QChar &charCode)
Decodes the given name extracting the parameters known by this renderer.
TEQTWIDGETSEXPORT void Config(QPen &pen, const te::se::Stroke *stroke)
It configs the given pen based on Symbology Enconding Stroke element.
const std::string * getWellKnownName() const
Definition: Mark.cpp:60
const Stroke * getStroke() const
Definition: Mark.cpp:125
void getSupportedMarks(std::vector< std::string > &marks) const
Method that informs the set of supported marks by a specific concrete mark renderer.
static std::string sm_rendererKey
The renderer key.
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).
QPen m_pen
The pen used to draw the mark patterns.
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
void setup(QImage *img)
Setups the internal QPainter and associates it with the given paint device (QImage).
void draw(QImage *img, QPainterPath &path)
Draw the given path on the the given image using the internal pen and brush.
Utility functions for MapTools module.
QPainter m_painter
The painter used to draw the mark patterns.
const Fill * getFill() const
Definition: Mark.cpp:114