All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.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/Utils.cpp
22 
23  \brief This file contains utility functions for dealing with Symbology Enconding and Qt concepts.
24  */
25 
26 // TerraLib
27 #include "../../../color/RGBAColor.h"
28 #include "../../../maptools/Utils.h"
29 #include "../../../se/serialization/xml/Symbolizer.h"
30 #include "../../../se/Stroke.h"
31 #include "../../../se/SvgParameter.h"
32 #include "../../../se/Utils.h"
33 #include "../../../xml/Reader.h"
34 #include "../../../xml/ReaderFactory.h"
35 #include "../Exception.h"
36 #include "Symbol.h"
37 #include "SymbolLibrary.h"
38 #include "SymbolLibraryManager.h"
39 #include "Utils.h"
40 
41 // Boost
42 #include <boost/format.hpp>
43 
44 // Qt
45 #include <QtCore/QVector>
46 #include <QBrush>
47 #include <QPen>
48 
49 // STL
50 #include <cassert>
51 
52 void te::qt::widgets::Config(QPen& pen, const te::se::Stroke* stroke)
53 {
54  if(stroke == 0)
55  {
56  pen.setColor(Qt::transparent);
57  return;
58  }
59 
60  // Color
62  te::se::GetColor(stroke, rgba);
63  QColor qrgba(rgba.getRgba());
64  qrgba.setAlpha(rgba.getAlpha());
65  pen.setColor(qrgba);
66 
67  // Width
68  const te::se::SvgParameter* width = stroke->getWidth();
69  if(width)
70  pen.setWidth(te::se::GetInt(width));
71 
72  // Cap Style
73  const te::se::SvgParameter* linecap = stroke->getLineCap();
74  if(linecap)
75  {
76  std::string capValue = te::se::GetString(linecap);
77 
78  Qt::PenCapStyle capStyle = Qt::FlatCap;
79  capValue == TE_SE_ROUND_CAP ? capStyle = Qt::RoundCap : capStyle = Qt::SquareCap;
80 
81  pen.setCapStyle(capStyle);
82  }
83 
84  // Join Style
85  const te::se::SvgParameter* linejoin = stroke->getLineJoin();
86  if(linejoin)
87  {
88  std::string joinValue = te::se::GetString(linejoin);
89 
90  Qt::PenJoinStyle joinStyle = Qt::MiterJoin;
91  joinValue == TE_SE_ROUND_JOIN ? joinStyle = Qt::RoundJoin : joinStyle = Qt::BevelJoin;
92 
93  pen.setJoinStyle(joinStyle);
94  }
95 
96  // Dash Style
97  const te::se::SvgParameter* dasharray = stroke->getDashArray();
98  if(dasharray)
99  {
100  std::string value = te::se::GetString(dasharray);
101 
102  std::vector<double> pattern;
103  te::map::GetDashStyle(value, pattern);
104 
105  pen.setDashPattern(QVector<qreal>::fromStdVector(pattern));
106  }
107 
108  /* TODO: Is necessary verify stroke-dashoffset, <GraphicStroke>, and <GraphicFill> elements here ?! */
109 }
110 
111 void te::qt::widgets::Config(QBrush& brush, const te::se::Fill* fill)
112 {
113  if(fill == 0)
114  {
115  brush.setColor(Qt::transparent);
116  return;
117  }
118 
120  te::se::GetColor(fill, rgba);
121  QColor qrgba(rgba.getRgba());
122  qrgba.setAlpha(rgba.getAlpha());
123  brush.setColor(qrgba);
124 
125  /* TODO: Is necessary verify <GraphicFill> element here ?! */
126 }
127 
128 void te::qt::widgets::ReadSymbolLibrary(const std::string& path)
129 {
130  std::auto_ptr<te::xml::Reader> reader(te::xml::ReaderFactory::make("XERCES"));
131  reader->read(path);
132 
133  if(!reader->next())
134  throw Exception((boost::format(TE_TR("Could not read symbol library information in file: %1%.")) % path).str());
135 
136  if(reader->getNodeType() != te::xml::START_ELEMENT)
137  throw Exception((boost::format(TE_TR("Error reading the document %1%, the start element wasn't found.")) % path).str());
138 
139  if(reader->getElementLocalName() != "SymbolLibrary")
140  throw Exception((boost::format(TE_TR("The first tag in the document %1% is not 'SymbolLibrary'.")) % path).str());
141 
142  std::string name = reader->getAttr("name");
143 
144  std::auto_ptr<SymbolLibrary> library(new SymbolLibrary(name));
145 
146  reader->next();
147 
148  while((reader->getNodeType() == te::xml::START_ELEMENT) &&
149  (reader->getElementLocalName() == "Symbol"))
150  {
151  Symbol* symbol = ReadSymbol(*reader);
152  library->insert(symbol);
153  }
154 
155  SymbolLibraryManager::getInstance().insert(library.release());
156 
157  assert(reader->getNodeType() == te::xml::END_DOCUMENT);
158 }
159 
161 {
162  std::auto_ptr<te::qt::widgets::Symbol> symbol(new te::qt::widgets::Symbol);
163 
165 
166  // Symbol Id
167  reader.next();
168  assert(reader.getNodeType() == te::xml::START_ELEMENT);
169  assert(reader.getElementLocalName() == "Id");
170  reader.next();
171  info.m_id = reader.getElementValue();
172  reader.next();
173  assert(reader.getNodeType() == te::xml::END_ELEMENT);
174 
175  // Symbol Name
176  reader.next();
177  assert(reader.getNodeType() == te::xml::START_ELEMENT);
178  assert(reader.getElementLocalName() == "Name");
179  reader.next();
180  info.m_name = reader.getElementValue();
181  reader.next();
182 
183  assert(reader.getNodeType() == te::xml::END_ELEMENT);
184  reader.next();
185 
186  // Symbol Author
187  if(reader.getElementLocalName() == "Author")
188  {
189  assert(reader.getNodeType() == te::xml::START_ELEMENT);
190  reader.next();
191  info.m_author = reader.getElementValue();
192  reader.next();
193 
194  assert(reader.getNodeType() == te::xml::END_ELEMENT);
195  reader.next();
196  }
197 
198  // Symbol Tags
199  if(reader.getElementLocalName() == "Tags")
200  {
201  assert(reader.getNodeType() == te::xml::START_ELEMENT);
202  reader.next();
203  info.m_tags = reader.getElementValue();
204  reader.next();
205 
206  assert(reader.getNodeType() == te::xml::END_ELEMENT);
207  reader.next();
208  }
209 
210  // Symbol Description
211  if(reader.getElementLocalName() == "Description")
212  {
213  assert(reader.getNodeType() == te::xml::START_ELEMENT);
214  reader.next();
215  info.m_description = reader.getElementValue();
216  reader.next();
217 
218  assert(reader.getNodeType() == te::xml::END_ELEMENT);
219  reader.next();
220  }
221 
222  symbol->setInfo(info);
223 
224  assert(reader.getNodeType() == te::xml::START_ELEMENT);
225  assert(reader.getElementLocalName().find("Symbolizer") != std::string::npos);
226 
227  /* TODO: *** Need move the OGC Symbology Enconding serialization methods
228  from <te::serialization> module to <te::se> module to avoid dependency with serialization module! ***/
229 
230  while((reader.getNodeType() == te::xml::START_ELEMENT) &&
231  (reader.getElementLocalName().find("Symbolizer") != std::string::npos))
232  symbol->addSymbolizer(te::se::serialize::Symbolizer::getInstance().read(reader));
233 
234  assert(reader.getNodeType() == te::xml::END_ELEMENT);
235  reader.next();
236 
237  return symbol.release();
238 }
void getRgba(int *r, int *g, int *b, int *a=0) const
It gets the color value.
Definition: RGBAColor.h:315
const SvgParameter * getLineCap() const
Definition: Stroke.cpp:143
TESEEXPORT void GetColor(const te::se::Stroke *stroke, te::color::RGBAColor &color)
It gets the RGBA color from the Stroke element.
Definition: Utils.cpp:404
This class models a XML reader object.
Definition: Reader.h:55
This class represents a symbol.
const SvgParameter * getLineJoin() const
Definition: Stroke.cpp:138
TEQTWIDGETSEXPORT Symbol * ReadSymbol(te::xml::Reader &reader)
It reads a symbol using the given reader.
Definition: Utils.cpp:160
static te::xml::Reader * make()
It creates a new XML reader using the dafault implementation.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
virtual std::string getElementLocalName() const =0
It returns the local part of the element name in the case of an element node.
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
TEMAPEXPORT void GetDashStyle(const std::string &dasharray, std::vector< double > &style)
Converts a dasharray pattern coded by a string to a vector of double.
Definition: Utils.cpp:155
The SymbolLibraryManager is a singleton that can be used to manage all loaded symbol libraries in Ter...
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:120
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
Definition: Config.h:86
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
#define TE_SE_ROUND_JOIN
It specifies the value "round" for stroke-linejoin parameter.
Definition: Config.h:149
This class represents a library of symbols.
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
This class represents a library of symbols.
Definition: SymbolLibrary.h:52
#define TE_SE_ROUND_CAP
It specifies the value "round" for stroke-linecap parameter.
Definition: Config.h:128
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
Information about a given Symbol.
Definition: SymbolInfo.h:60
This class represents a symbol. TODO: More description!
Definition: Symbol.h:54
Utility functions for MapTools module.
#define TE_SE_DEFAULT_FILL_BASIC_COLOR
It specifies the default color used by basic fill (solid colors).
Definition: Config.h:79
const SvgParameter * getDashArray() const
Definition: Stroke.cpp:148
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
virtual NodeType getNodeType() const =0
It return the type of node read.
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.
Definition: Utils.cpp:443
virtual std::string getElementValue() const =0
It returns the element data value in the case of VALUE node.
A SvgParameter refers to an SVG/CSS graphical-formatting parameter.
Definition: SvgParameter.h:48
TEQTWIDGETSEXPORT void ReadSymbolLibrary(const std::string &path)
It reads a symbol library file and put the result on SymbolLibraryManager.
Definition: Utils.cpp:128
virtual bool next()=0
It gets the next event to be read.
TESEEXPORT std::string GetString(const te::se::ParameterValue *param)
It gets the parameter value as a string.
Definition: Utils.cpp:453
const SvgParameter * getWidth() const
Definition: Stroke.cpp:133