All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LegendModel.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2014 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 LegendModel.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "LegendModel.h"
30 #include "Context.h"
31 #include "MapModel.h"
32 #include "Property.h"
33 #include "Properties.h"
34 #include "SharedProperties.h"
35 #include "../../../maptools/CanvasConfigurer.h"
36 #include "../../../se/Symbolizer.h"
37 #include "../../../se/Style.h"
38 #include "../../../se/Rule.h"
39 #include "../../../geometry/Polygon.h"
40 #include "../../../geometry/Geometry.h"
41 #include "../../../geometry/Envelope.h"
42 
44  m_mapName(""),
45  m_layer(0),
46  m_borderDisplacement(2),
47  m_displacementBetweenSymbols(2),
48  m_displacementBetweenTitleAndSymbols(5),
49  m_displacementBetweenSymbolsAndText(3)
50 {
51  m_box = te::gm::Envelope(0., 0., 70., 50.);
52 }
53 
55 {
56 
57 }
58 
60 {
61  te::color::RGBAColor** pixmap = 0;
62 
64  Utils* utils = Context::getInstance()->getUtils();
65 
66  if(context.isResizeCanvas())
67  utils->configCanvas(m_box);
68 
69  if(m_border)
70  {
71  canvas->setPolygonContourWidth(2);
72  canvas->setPolygonContourColor(te::color::RGBAColor(0, 0, 0, 255));
73  canvas->setPolygonFillColor(m_backgroundColor);
74 
75  utils->drawRectW(m_box);
76  }
77 
78  drawLegend(canvas, utils);
79 
80  if(context.isResizeCanvas())
81  pixmap = utils->getImageW(m_box);
82 
83  ContextItem contextNotify;
84  contextNotify.setPixmap(pixmap);
85  notifyAll(contextNotify);
86 }
87 
89 {
90  if(!m_layer)
91  return;
92 
93  if(!m_layer->getStyle())
94  return;
95 
96  // Number of rules defined on feature type style
97  std::size_t nRules = m_layer->getStyle()->getRules().size();
98 
99  //Header
100  std::string layerName = m_layer->getTitle();
101 
102  double wtxt = 0;
103  double htxt = 0;
104 
105  utils->textBoundingBox(wtxt, htxt, layerName);
106 
107  canvas->setTextPointSize(12);
108  canvas->setTextColor(te::color::RGBAColor(0, 0, 0, 255));
109  canvas->drawText(m_box.getCenter().x - wtxt, m_box.getUpperRightY() - htxt, layerName, 0);
110 
111  // Creates a canvas configurer
112  te::map::CanvasConfigurer cc(canvas);
113 
114  for(std::size_t i = 0; i < nRules; ++i) // for each <Rule>
115  {
116  // The current rule
117  te::se::Rule* rule = m_layer->getStyle()->getRule(i);
118  assert(rule);
119 
120  // Gets the set of symbolizers defined on current rule
121  const std::vector<te::se::Symbolizer*>& symbolizers = rule->getSymbolizers();
122 
123  if(symbolizers.empty())
124  {
125  continue;
126  }
127 
128  std::size_t nSymbolizers = symbolizers.size();
129 
130  for(std::size_t j = 0; j < nSymbolizers; ++j) // for each <Symbolizer>
131  {
132  // The current symbolizer
133  te::se::Symbolizer* symb = symbolizers[j];
134 
135  // Let's config the canvas based on the current symbolizer
136  cc.config(symb);
137 
138  //Test
139  te::gm::Envelope box(m_box.getLowerLeftX(), m_box.getUpperRightY() - 20, m_box.getLowerLeftX() + 10, m_box.getUpperRightY() - 10);
140  utils->drawRectW(box);
141 
142  canvas->setTextPointSize(12);
143  canvas->setTextColor(te::color::RGBAColor(0, 0, 0, 255));
144  std::string* name = const_cast<std::string*>(rule->getName());
145  std::string n = symb->getName();
146  if(name)
147  canvas->drawText(m_box.getLowerLeftX() + 15, m_box.getUpperRightY() - 20, name->c_str(), 0);
148  } // end for each <Symbolizer>
149 
150  } // end for each <Rule>
151 
152 }
153 
155 {
157 
158  Property pro_mapName;
159  pro_mapName.setName(m_sharedProps->getMapName());
160  pro_mapName.setId("");
161  pro_mapName.setValue(m_mapName, DataTypeStringList);
162  Variant v;
163  v.setValue(m_mapName, DataTypeString);
164  pro_mapName.addOption(v);
165 
166  m_properties->addProperty(pro_mapName);
167 
168  return m_properties;
169 }
170 
172 {
174 
175  Properties* vectorProps = const_cast<Properties*>(properties);
176 
177  Property pro_mapName = vectorProps->contains(m_sharedProps->getMapName());
178 
179  if(!pro_mapName.isNull())
180  {
181  m_mapName = pro_mapName.getOptionByCurrentChoice().toString();
182  }
183 }
184 
186 {
187  MapModel* map = dynamic_cast<MapModel*>(m_visitable);
188 
189  if(map)
190  {
191  m_layer = map->getLayer();
192 
193  ContextItem contx;
194 
195  draw(contx);
196 
197  ContextItem contextNotify;
198  contextNotify.setWait(true);
199  notifyAll(contextNotify);
200  }
201 }
202 
204 {
205  m_borderDisplacement = value;
206 }
207 
209 {
210  return m_borderDisplacement;
211 }
212 
214 {
215  m_displacementBetweenSymbols = value;
216 }
217 
219 {
220  return m_displacementBetweenSymbols;
221 }
222 
224 {
225  m_displacementBetweenTitleAndSymbols = value;
226 }
227 
229 {
230  return m_displacementBetweenTitleAndSymbols;
231 }
232 
234 {
235  m_displacementBetweenSymbolsAndText = value;
236 }
237 
239 {
240  return m_displacementBetweenSymbolsAndText;
241 }
virtual double getDisplacementBetweenTitleAndSymbols()
void setPixmap(te::color::RGBAColor **pixmap)
Definition: ContextItem.cpp:77
void addOption(Variant variant)
Definition: Property.cpp:79
virtual void textBoundingBox(double &w, double &h, std::string txt)
Definition: Utils.cpp:284
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
virtual void setDisplacementBetweenTitleAndSymbols(double value)
virtual void setPolygonFillColor(const te::color::RGBAColor &color)=0
It sets the color used to fill the draw of polygon geometries.
virtual void drawRectW(te::gm::Envelope box)
Definition: Utils.cpp:49
virtual void drawText(int x, int y, const std::string &txt, float angle=0.0, te::at::HorizontalAlignment hAlign=te::at::Start, te::at::VerticalAlignment vAlign=te::at::Baseline)=0
It draws a text.
Variant getOptionByCurrentChoice()
Definition: Property.cpp:106
virtual void configCanvas(te::gm::Envelope box, bool resize=true)
Definition: Utils.cpp:124
virtual void updateProperties(te::layout::Properties *properties)
void setValue(typename ValueType value, LayoutPropertyDataType type)
Definition: Variant.h:107
void setValue(typename ValueType value, LayoutPropertyDataType type)
Definition: Property.h:106
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
const std::string & getName() const
Definition: Symbolizer.cpp:72
virtual double getDisplacementBetweenSymbols()
virtual te::map::AbstractLayerPtr getLayer()
Definition: MapModel.cpp:83
virtual void setTextColor(const te::color::RGBAColor &color)=0
It sets the text drawing color.
Utils * getUtils()
Definition: Context.cpp:126
virtual Properties * getProperties() const
virtual void setPolygonContourWidth(int w)=0
It sets the polygon contour width.
virtual void setDisplacementBetweenSymbolAndText(double value)
const std::vector< Symbolizer * > & getSymbolizers() const
Definition: Rule.cpp:158
virtual void draw(ContextItem context)
Definition: LegendModel.cpp:59
virtual void setDisplacementBetweenSymbols(double value)
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
virtual Properties * getProperties() const
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:78
virtual void drawLegend(te::map::Canvas *canvas, Utils *utils)
Definition: LegendModel.cpp:88
virtual double getBorderDisplacement()
virtual void visitDependent()
virtual te::color::RGBAColor ** getImageW(te::gm::Envelope boxmm)
Definition: Utils.cpp:97
virtual void setPolygonContourColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw the boundary of polygon geometries.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
std::string toString()
Definition: Variant.cpp:237
void setName(std::string name)
Definition: Property.cpp:54
void setWait(bool wait)
Definition: ContextItem.cpp:97
void config(const te::se::Symbolizer *symbolizer)
It configs the canvas based on given symbolizer.
static Context * getInstance()
This function is called to create an instance of the class.
Definition: Context.cpp:46
void setId(std::string id)
Definition: Property.cpp:69
virtual bool contains(Property property)
Definition: Properties.h:147
virtual double getDisplacementBetweenSymbolAndText()
te::map::Canvas * getCanvas()
Definition: Context.cpp:116
virtual void setBorderDisplacement(double value)
virtual void updateProperties(te::layout::Properties *properties)
virtual void setTextPointSize(double size)=0
It sets the text point Size.
A Symbology Enconding visitor that configures a given canvas based on symbolizers elements...
const std::string * getName() const
Definition: Rule.cpp:64