All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ScaleModel.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 ScaleModel.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ScaleModel.h"
30 #include "Context.h"
31 #include "MapModel.h"
32 #include "Property.h"
33 #include "Properties.h"
34 #include "SharedProperties.h"
35 #include "../../../geometry/Polygon.h"
36 
37 // STL
38 #include <string>
39 #include <sstream> // std::stringstream
40 
41 
43  m_mapName(""),
44  m_mapScale(0),
45  m_scaleGapX(20),
46  m_scaleGapY(5)
47 {
48  m_box = te::gm::Envelope(0., 0., 70., 30.);
49 }
50 
52 {
53 
54 }
55 
57 {
58  te::color::RGBAColor** pixmap = 0;
59 
61  Utils* utils = Context::getInstance()->getUtils();
62 
63  if(context.isResizeCanvas())
64  utils->configCanvas(m_box);
65 
66  drawScale(canvas, utils, m_box);
67 
68  if(m_border)
69  {
70  canvas->setPolygonContourWidth(2);
71  canvas->setPolygonContourColor(te::color::RGBAColor(0, 0, 0, 255));
72  canvas->setPolygonFillColor(m_backgroundColor);
73 
74  utils->drawRectW(m_box);
75  }
76 
77  if(context.isResizeCanvas())
78  pixmap = utils->getImageW(m_box);
79 
80  ContextItem contextNotify;
81  contextNotify.setPixmap(pixmap);
82  notifyAll(contextNotify);
83 }
84 
86 {
87  double unit=1000.0;
88  std::string strUnit="(Km)";
89 
90  if(m_mapScale < 1000)
91  {
92  unit = 1.0;
93  strUnit="(m)";
94  }
95  else
96  {
97  unit = 1000.0;
98  }
99 
100  //convert millimeters to centimeters
101  double mmToCm = m_scaleGapX/10;
102 
103  double spacing = m_mapScale/100;
104 
105  double value = 0.;
106  double width = 0.;
107  double x1 = box.getLowerLeftX();
108  te::color::RGBAColor black(0, 0, 0, 255);
109  te::color::RGBAColor white(255, 255, 255, 255);
110  te::color::RGBAColor firtRect = black;
111  te::color::RGBAColor secondRect = white;
112  te::color::RGBAColor changeColor;
113  te::gm::Envelope newBoxFirst;
114  te::gm::Envelope newBoxSecond;
115  canvas->setPolygonContourWidth(1);
116  canvas->setPolygonContourColor(te::color::RGBAColor(0, 0, 0, 255));
117  canvas->setTextColor(te::color::RGBAColor(0, 0, 0, 255));
118  for( ; x1 < box.getUpperRightX() ; x1 += width)
119  {
120  //Up rect
121  canvas->setPolygonFillColor(firtRect);
122  newBoxFirst = te::gm::Envelope(x1, box.getUpperRightY(), x1 + m_scaleGapX, box.getUpperRightY() - m_scaleGapY);
123  utils->drawRectW(newBoxFirst);
124 
125  //Down rect
126  canvas->setPolygonFillColor(secondRect);
127  newBoxSecond = te::gm::Envelope(x1, box.getUpperRightY() - (m_scaleGapY*2), x1 + m_scaleGapX, box.getUpperRightY() - m_scaleGapY);
128  utils->drawRectW(newBoxSecond);
129 
130  if(width == 0)
131  width = m_scaleGapX;
132  else
133  value += (spacing * mmToCm)/unit;
134 
135  std::stringstream ss_value;
136  ss_value << value;
137 
138  std::string s_value = ss_value.str();
139  canvas->drawText(x1, newBoxSecond.getLowerLeftY() - 5, s_value, 0);
140 
141  changeColor = firtRect;
142  firtRect = secondRect;
143  secondRect = changeColor;
144  }
145 
146  //middle
147  // Canvas - Necessário saber o tamanho do box do texto em mm: pendente;
148  double centerX = m_box.getCenter().x;
149  double centerY = m_box.getCenter().y;
150 
151  canvas->drawText(centerX, newBoxSecond.getLowerLeftY() - 15, strUnit, 0);
152 
153 }
154 
156 {
158 
159  Property pro_widthRectGap;
160  pro_widthRectGap.setName("scale_width_rect_gap");
161  pro_widthRectGap.setId("");
162  pro_widthRectGap.setValue(m_scaleGapX, DataTypeDouble);
163  m_properties->addProperty(pro_widthRectGap);
164 
165  Property pro_heightRectGap;
166  pro_heightRectGap.setName("scale_height_rect_gap");
167  pro_heightRectGap.setId("");
168  pro_heightRectGap.setValue(m_scaleGapY, DataTypeDouble);
169  m_properties->addProperty(pro_heightRectGap);
170 
171  Property pro_mapName;
172  pro_mapName.setName(m_sharedProps->getMapName());
173  pro_mapName.setId("");
174  pro_mapName.setValue(m_mapName, DataTypeStringList);
175  Variant v;
176  v.setValue(m_mapName, DataTypeString);
177  pro_mapName.addOption(v);
178  m_properties->addProperty(pro_mapName);
179 
180  return m_properties;
181 }
182 
184 {
186 
187  Properties* vectorProps = const_cast<Properties*>(properties);
188 
189  Property pro_mapName = vectorProps->contains(m_sharedProps->getMapName());
190 
191  if(!pro_mapName.isNull())
192  {
193  m_mapName = pro_mapName.getOptionByCurrentChoice().toString();
194  }
195 
196  Property pro_widthRectGap = vectorProps->contains("scale_width_rect_gap");
197 
198  if(!pro_widthRectGap.isNull())
199  {
200  m_scaleGapX = pro_widthRectGap.getValue().toDouble();
201  }
202 
203  Property pro_heightRectGap = vectorProps->contains("scale_height_rect_gap");
204 
205  if(!pro_heightRectGap.isNull())
206  {
207  m_scaleGapY = pro_heightRectGap.getValue().toDouble();
208  }
209 }
210 
212 {
213  MapModel* map = dynamic_cast<MapModel*>(m_visitable);
214 
215  if(map)
216  {
217  m_mapScale = map->getScale();
218 
219  ContextItem contx;
220 
221  draw(contx);
222 
223  ContextItem contextNotify;
224  contextNotify.setWait(true);
225  notifyAll(contextNotify);
226  }
227 }
228 
230 {
231  m_scaleGapX = x;
232 }
233 
235 {
236  return m_scaleGapX;
237 }
238 
240 {
241  m_scaleGapY = y;
242 }
243 
245 {
246  return m_scaleGapY;
247 }
virtual double getScaleGapX()
Definition: ScaleModel.cpp:234
virtual double getScale()
Definition: MapModel.cpp:88
virtual void setScaleGapY(double y)
Definition: ScaleModel.cpp:239
void setPixmap(te::color::RGBAColor **pixmap)
Definition: ContextItem.cpp:77
Variant getValue()
Definition: Property.cpp:74
void addOption(Variant variant)
Definition: Property.cpp:79
virtual void draw(ContextItem context)
Definition: ScaleModel.cpp:56
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
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:410
virtual void updateProperties(te::layout::Properties *properties)
Definition: ScaleModel.cpp:183
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
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
Definition: Envelope.h:400
virtual void configCanvas(te::gm::Envelope box, bool resize=true)
Definition: Utils.cpp:124
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Definition: Envelope.h:420
virtual void setScaleGapX(double x)
Definition: ScaleModel.cpp:229
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
virtual void setTextColor(const te::color::RGBAColor &color)=0
It sets the text drawing color.
Utils * getUtils()
Definition: Context.cpp:126
virtual void setPolygonContourWidth(int w)=0
It sets the polygon contour width.
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
virtual Properties * getProperties() const
virtual te::color::RGBAColor ** getImageW(te::gm::Envelope boxmm)
Definition: Utils.cpp:97
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
Definition: Envelope.h:390
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
virtual double getScaleGapY()
Definition: ScaleModel.cpp:244
virtual Properties * getProperties() const
Definition: ScaleModel.cpp:155
void setName(std::string name)
Definition: Property.cpp:54
void setWait(bool wait)
Definition: ContextItem.cpp:97
static Context * getInstance()
This function is called to create an instance of the class.
Definition: Context.cpp:46
virtual void visitDependent()
Definition: ScaleModel.cpp:211
void setId(std::string id)
Definition: Property.cpp:69
virtual void drawScale(te::map::Canvas *canvas, Utils *utils, te::gm::Envelope box)
Definition: ScaleModel.cpp:85
virtual bool contains(Property property)
Definition: Properties.h:147
te::map::Canvas * getCanvas()
Definition: Context.cpp:116