All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LegendChildModel.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 LegendChildModel.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "LegendChildModel.h"
30 #include "../core/enum/Enums.h"
31 #include "../core/property/Properties.h"
32 #include "../core/property/SharedProperties.h"
33 #include "../../maptools/CanvasConfigurer.h"
34 #include "../../se/Symbolizer.h"
35 #include "../../se/Style.h"
36 #include "../../se/Rule.h"
37 #include "MapModel.h"
38 
39 // STL
40 #include <string>
41 #include <sstream>
42 
44  m_rule(0)
45 {
46  m_type = Enums::getInstance().getEnumObjectType()->getLegendChildItem();
47 
48  m_box = te::gm::Envelope(0., 0., 70., 50.);
49 
51  m_fontColor.setColor(0,0,0);
52 }
53 
55 {
56 
57 }
58 
60 {
61  te::color::RGBAColor** pixmap = 0;
62 
63  te::map::Canvas* canvas = context.getCanvas();
64  Utils* utils = context.getUtils();
65 
66  if((!canvas) || (!utils))
67  return;
68 
69  if(context.isResizeCanvas())
70  utils->configCanvas(m_box);
71 
72  drawBackground(context);
73 
74  drawLegend(canvas, utils);
75 
76  if(context.isResizeCanvas())
77  pixmap = utils->getImageW(m_box);
78 
79  context.setPixmap(pixmap);
80  notifyAll(context);
81 }
82 
84 {
85  if(!m_rule)
86  return;
87 
88  double wtxt = 0;
89  double htxt = 0;
90 
91  double x1 = m_box.getLowerLeftX() + m_borderDisplacement;
92  double y1 = m_box.getUpperRightY() - m_borderDisplacement - htxt;
93 
94  // Creates a canvas configurer
95  te::map::CanvasConfigurer cc(canvas);
96 
97  // Gets the set of symbolizers defined on current rule
98  const std::vector<te::se::Symbolizer*>& symbolizers = m_rule->getSymbolizers();
99 
100  if(symbolizers.empty())
101  {
102  return;
103  }
104 
105  std::size_t nSymbolizers = symbolizers.size();
106 
107  for(std::size_t j = 0; j < nSymbolizers; ++j) // for each <Symbolizer>
108  {
109  // The current symbolizer
110  te::se::Symbolizer* symb = symbolizers[j];
111 
112  // Let's config the canvas based on the current symbolizer
113  cc.config(symb);
114 
115  //Test
116  te::gm::Envelope box(x1, y1 - m_displacementBetweenTitleAndSymbols - (m_displacementBetweenSymbols * j),
117  x1 + m_symbolsize, y1 - m_displacementBetweenTitleAndSymbols - (m_displacementBetweenSymbols * j) - m_symbolsize);
118  utils->drawRectW(box);
119 
120  canvas->setTextPointSize(m_font.getPointSize());
121  canvas->setTextUnderline(m_font.isUnderline());
122  canvas->setTextStrikeOut(m_font.isStrikeout());
123  canvas->setTextColor(m_fontColor);
124  std::string* nameRule = const_cast<std::string*>(m_rule->getName());
125  std::string n = symb->getName();
126  std::string name;
127  if(nameRule)
128  {
129  name = nameRule->c_str();
130  }
131  else
132  {
133  std::ostringstream convert;
134  convert << j;
135  name = "Legend_" + convert.str();
136  }
137 
138  utils->textBoundingBox(wtxt, htxt, name);
139  canvas->drawText(box.getLowerLeftX() + m_symbolsize + m_displacementBetweenSymbolsAndText, box.m_ury, name, 0);
140  } // end for each <Symbolizer>
141 }
142 
144 {
145  MapModel* map = dynamic_cast<MapModel*>(m_visitable);
146 
147  if(map)
148  {
149  m_layer = map->getLayer();
150 
151  refreshRule();
152 
153  draw(context);
154  }
155 }
156 
158 {
159  return m_rule;
160 }
161 
163 {
164  m_rule = rule;
165 }
166 
168 {
169  if(!m_layer)
170  return;
171 
172  if(!m_layer->getStyle())
173  return;
174 
175  // Number of rules defined on feature type style
176  std::size_t nRules = m_layer->getStyle()->getRules().size();
177 
178  for(std::size_t i = 0; i < nRules; ++i) // for each <Rule>
179  {
180  // The current rule
181  te::se::Rule* rule = m_layer->getStyle()->getRule(i);
182 
183  if(rule == m_rule)
184  {
185  m_rule = rule;
186  }
187  } // end for each <Rule>
188 }
Class responsible for maintaining the drawing context of a MVC component. It is always used by the "M...
Definition: ContextItem.h:49
virtual te::se::Rule * getRule()
virtual void setPixmap(te::color::RGBAColor **pixmap)
Stores pixmap generated after drawing.
Definition: ContextItem.cpp:85
void setColor(const std::string &hexColor)
It sets the color using a two hexadecimal RGB-encoded color.
Definition: RGBAColor.h:329
virtual void textBoundingBox(double &w, double &h, std::string txt)
A method that calculates the height and width of a text.
Definition: Utils.cpp:271
virtual void drawLegend(te::map::Canvas *canvas, Utils *utils)
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
virtual void visitDependent(ContextItem context)
visit and if necessary redraw
virtual void drawRectW(te::gm::Envelope box)
Draw a rectangle in world coordinates (mm).
Definition: Utils.cpp:58
te::color::RGBAColor m_fontColor
Definition: LegendModel.h:134
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.
void setPointSize(int point)
Sets point size of the font.
Definition: Font.cpp:66
EnumType * m_type
type of the MVC component
virtual void setTextUnderline(bool b)=0
It sets the text underline flag.
static Enums & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual Utils * getUtils()
const std::string & getName() const
Definition: Symbolizer.cpp:72
te::gm::Envelope m_box
bounding rectangle
virtual te::map::AbstractLayerPtr getLayer()
Definition: MapModel.cpp:226
Class that represents a "Model" part of Map MVC component. Its coordinate system is the same of scene...
virtual void setTextColor(const te::color::RGBAColor &color)=0
It sets the text drawing color.
virtual te::map::Canvas * getCanvas()
Return canvas.
Class that represents a "Model" part of Map MVC component. Its coordinate system is the same of scene...
Definition: MapModel.h:61
virtual void setTextStrikeOut(bool b)=0
It sets the text strike out flag.
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:78
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
virtual te::color::RGBAColor ** getImageW(te::gm::Envelope boxmm)
Returns a pointer RGBA colors that representing an image.
Definition: Utils.cpp:111
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual void draw(ContextItem context)
Drawing method of the MVC component.
std::string convert(const path &v)
URI path to string.
Definition: path.cpp:219
virtual void configCanvas(te::gm::Envelope box, bool resize=true, bool applyZoom=true)
Sets the viewport and window of the canvas. The viewport is only changed if the resize parameter is t...
Definition: Utils.cpp:138
void config(const te::se::Symbolizer *symbolizer)
It configs the canvas based on given symbolizer.
virtual void setRule(te::se::Rule *rule)
Utility class with functions to manipulate the canvas and conversion between projections.
Definition: Utils.h:61
virtual bool isResizeCanvas()
Returns whether the canvas should or should not be resized.
Definition: ContextItem.cpp:95
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...