All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  : te::map::AbstractMarkRenderer()
42 {
43  m_brush.setStyle(Qt::SolidPattern);
44  m_brush.setColor(QColor(TE_SE_DEFAULT_FILL_BASIC_COLOR));
45 }
46 
48 {
49 }
50 
52 {
53  // Decoding...
54  QChar ch;
55  QString fontName;
56  QString name(mark->getWellKnownName()->c_str());
57  te::qt::widgets::GlyphMarkRenderer::decode(name, fontName, ch); // TODO: Can throw exceptions!
58 
59  // Configuring the font
60  QFont font;
61  font.setFamily(fontName);
62  font.setPixelSize(size);
63 
64  // Bulding the character path
65  QPainterPath charPath;
66  charPath.addText(0.0, 0.0, font, ch);
67  // Adjusting...
68  QRectF bounds = charPath.boundingRect();
69  double invMax = 1 / std::max(bounds.width(), bounds.height());
70  QTransform t;
71  t.scale(invMax, invMax);
72  t.translate(-bounds.center().x(), -bounds.center().y());
73  charPath = t.map(charPath);
74 
75  // Creates the image that will represent the graphical mark pattern
76  QImage* img = new QImage(size, size, QImage::Format_ARGB32_Premultiplied);
77  img->fill(Qt::transparent);
78 
79  // Configuring visual...
80  te::qt::widgets::Config(m_pen, mark->getStroke());
81  te::qt::widgets::Config(m_brush, mark->getFill());
82 
83  // Let's draw the char path!
84  draw(img, charPath);
85 
86  // Converts QImage to te::color::RGBA**
88 
89  delete img;
90 
91  return rgba;
92 }
93 
94 void te::qt::widgets::GlyphMarkRenderer::getSupportedMarks(std::vector<std::string>& marks) const
95 {
96 }
97 
98 QString te::qt::widgets::GlyphMarkRenderer::encode(const QString& font, const int& charCode)
99 {
100  QString result = QString::fromStdString(sm_rendererKey);
101  result += "://" + font + "#0x" + QString::number(charCode, 16);
102  return result;
103 }
104 
105 void te::qt::widgets::GlyphMarkRenderer::decode(QString& name, QString& font, QChar& charCode)
106 {
107  // Extract the part important to this renderer!
108  QString pattern(name);
109  pattern.remove(0, sm_rendererKey.size() + 3); // removing "ttf://"
110  if(!pattern.contains("#"))
111  return; // TODO: Exception: Bad format.
112 
113  // Tokenizes based on '#" separator. The first value is the font and the second is the char code...
114  QStringList myParams = pattern.split("#", QString::SkipEmptyParts);
115  if(myParams.size() < 2)
116  return; // TODO: Exception: Bad format.
117 
118  // Getting char
119  bool wasConverted = false;
120  // 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.
121  charCode = myParams[1].toInt(&wasConverted, 0);
122  if(!wasConverted)
123  return; // TODO: Exception: Invalid char code.
124 
125  // The font name
126  font = myParams[0];
127 }
128 
130 {
131  m_painter.begin(img);
132  m_painter.setRenderHints(QPainter::Antialiasing);
133  m_painter.setPen(m_pen);
134  m_painter.setBrush(m_brush);
135 }
136 
138 {
139  m_painter.end();
140  m_pen = QPen(QColor(TE_SE_DEFAULT_STROKE_BASIC_COLOR));
141  m_brush = QBrush(QColor(TE_SE_DEFAULT_FILL_BASIC_COLOR), Qt::SolidPattern);
142 }
143 
144 void te::qt::widgets::GlyphMarkRenderer::draw(QImage* img, QPainterPath& path)
145 {
146  setup(img);
147 
148  // Transformation parameters
149  double s = img->width() - m_pen.width() - 1;
150  double t = img->width() * 0.5;
151 
152  QTransform transform;
153  QPainterPath transformedPath = transform.translate(t, t).scale(s, s).map(path);
154 
155  m_painter.drawPath(transformedPath);
156 
157  end();
158 }
159 
160 bool te::qt::widgets::GlyphMarkRenderer::getChar(QString& charCode, QChar& ch)
161 {
162  bool isOk = false;
163  // 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.
164  ch = charCode.toInt(&isOk, 0);
165  return isOk;
166 }
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.
Definition: Utils.cpp:52
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
Definition: Config.h:86
const std::string * getWellKnownName() const
Definition: Mark.cpp:60
const Stroke * getStroke() const
Definition: Mark.cpp:125
Utility functions for MapTools module.
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.
#define TE_SE_DEFAULT_FILL_BASIC_COLOR
It specifies the default color used by basic fill (solid colors).
Definition: Config.h:79
TEQTWIDGETSEXPORT QImage * GetImage(te::color::RGBAColor **img, int width, int height)
It creates a QImage from an RGBA color array.
Definition: Utils.cpp:69
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
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.
const Fill * getFill() const
Definition: Mark.cpp:114