All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ScaleItem.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 LegendItem.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ScaleItem.h"
30 #include "../../core/pattern/mvc/ItemController.h"
31 #include "../core/Scene.h"
32 #include "../../core/pattern/mvc/Observable.h"
33 #include "../../../color/RGBAColor.h"
34 #include "../../../qt/widgets/Utils.h"
35 #include "../../../geometry/Envelope.h"
36 #include "../../../common/STLUtils.h"
37 #include "../../item/ScaleModel.h"
38 
39 // STL
40 #include <string>
41 #include <sstream> // std::stringstream
42 
43 // Qt
44 #include <QPixmap>
45 
47  ObjectItem(controller, o)
48 {
49  m_nameClass = std::string(this->metaObject()->className());
50 }
51 
53 {
54 
55 }
56 
57 void te::layout::ScaleItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /*= 0 */ )
58 {
59  Q_UNUSED( option );
60  Q_UNUSED( widget );
61  if ( !painter )
62  {
63  return;
64  }
65 
66  drawBackground(painter);
67 
68  ScaleModel* model = dynamic_cast<ScaleModel*>(m_model);
69 
70  if(model)
71  {
72  EnumScaleType* enumScale = model->getEnumScaleType();
73 
74  if(model->getCurrentScaleType() == enumScale->getDoubleAlternatingScaleBarType())
75  {
76  drawDoubleAlternatingScaleBar(painter);
77  }
78  if(model->getCurrentScaleType() == enumScale->getAlternatingScaleBarType())
79  {
80  drawAlternatingScaleBar(painter);
81  }
82  if(model->getCurrentScaleType() == enumScale->getHollowScaleBarType())
83  {
84  drawHollowScaleBar(painter);
85  }
86  }
87 
88  drawBorder(painter);
89 
90  //Draw Selection
91  if (option->state & QStyle::State_Selected)
92  {
93  drawSelection(painter);
94  }
95 }
96 
98 {
99  QRectF boundRect = boundingRect();
100 
101  painter->save();
102 
103  double unit=1000.0;
104  std::string strUnit="(Km)";
105 
106  ScaleModel* model = dynamic_cast<ScaleModel*>(m_model);
107 
108  if(model->getMapScale() < 1000)
109  {
110  unit = 1.0;
111  strUnit="(m)";
112  }
113  else
114  {
115  unit = 1000.0;
116  }
117 
118  //convert millimeters to centimeters
119  double mmToCm = model->getScaleGapX()/10;
120 
121  double spacing = model->getMapScale()/100;
122 
123  double value = 0.;
124  double width = 0.;
125  double x1 = boundRect.bottomLeft().x();
126 
127  QColor black(0, 0, 0, 255);
128  QColor white(255, 255, 255, 255);
129  QColor firstRect = black;
130  QColor secondRect = white;
131  QColor changeColor;
132  QColor textColor(0, 0, 0, 255);
133 
134  QRectF newBoxFirst;
135  QRectF newBoxSecond;
136 
137  double gapX = model->getScaleGapX();
138  double gapY = model->getScaleGapY();
139 
140  for( ; x1 < boundRect.topRight().x(); x1 += width)
141  {
142  if(x1+gapX >= boundRect.topRight().x())
143  {
144  //Draw the remaining rects, near the end
145  double dx = boundRect.width() - x1;
146  gapX = dx;
147  }
148 
149  painter->setPen(Qt::NoPen);
150 
151  //Down rect
152  painter->setBrush(QBrush(secondRect));
153  newBoxSecond = QRectF(x1, boundRect.center().y() - gapY, gapX, gapY);
154  painter->drawRect(newBoxSecond);
155 
156  //Up rect
157  painter->setBrush(QBrush(firstRect));
158  newBoxFirst = QRectF(x1, boundRect.center().y(), gapX, gapY);
159  painter->drawRect(newBoxFirst);
160 
161  if(width == 0)
162  width = gapX;
163  else
164  value += (spacing * mmToCm)/unit;
165 
166  std::stringstream ss_value;
167  ss_value << value;
168 
169  std::string s_value = ss_value.str();
170 
171  painter->setPen(QPen(textColor));
172  QPointF coordText(x1, newBoxSecond.topLeft().y() - 5);
173 
174  drawText(coordText, painter, ss_value.str());
175 
176  changeColor = firstRect;
177  firstRect = secondRect;
178  secondRect = changeColor;
179  }
180 
181  newBoxSecond = QRectF(boundRect.x(), boundRect.center().y() - gapY, boundRect.width(), gapY*2);
182 
183  //Rect around scale
184  QPen penBackground(black, 0, Qt::SolidLine);
185  painter->setBrush(Qt::NoBrush);
186  painter->setPen(penBackground);
187  painter->drawRect(newBoxSecond);
188 
189  //middle-bottom text
190  double centerX = newBoxSecond.center().x();
191  painter->setPen(QPen(textColor));
192 
193  QPointF coordText(centerX, boundRect.topLeft().y() + 1);
194  drawText(coordText, painter, strUnit);
195 
196  painter->restore();
197 }
198 
200 {
201  QRectF boundRect = boundingRect();
202 
203  painter->save();
204 
205  double unit=1000.0;
206  std::string strUnit="(Km)";
207 
208  ScaleModel* model = dynamic_cast<ScaleModel*>(m_model);
209 
210  if(model->getMapScale() < 1000)
211  {
212  unit = 1.0;
213  strUnit="(m)";
214  }
215  else
216  {
217  unit = 1000.0;
218  }
219 
220  //convert millimeters to centimeters
221  double mmToCm = model->getScaleGapX()/10;
222 
223  double spacing = model->getMapScale()/100;
224 
225  double value = 0.;
226  double width = 0.;
227  double x1 = boundRect.bottomLeft().x();
228 
229  QColor black(0, 0, 0, 255);
230  QColor white(255, 255, 255, 255);
231  QColor firstRect = black;
232  QColor secondRect = white;
233  QColor changeColor;
234  QColor textColor(0, 0, 0, 255);
235 
236  QRectF newBoxFirst;
237  QRectF newBoxSecond;
238 
239  double gapX = model->getScaleGapX();
240  double gapY = model->getScaleGapY();
241 
242  for( ; x1 < boundRect.topRight().x(); x1 += width)
243  {
244  if(x1+gapX >= boundRect.topRight().x())
245  {
246  //Draw the remaining rects, near the end
247  double dx = boundRect.width() - x1;
248  gapX = dx;
249  }
250 
251  painter->setPen(Qt::NoPen);
252 
253  painter->setBrush(QBrush(secondRect));
254  newBoxSecond = QRectF(x1, boundRect.center().y() - gapY/2, gapX, gapY);
255  painter->drawRect(newBoxSecond);
256 
257 
258  if(width == 0)
259  width = gapX;
260  else
261  value += (spacing * mmToCm)/unit;
262 
263  std::stringstream ss_value;
264  ss_value << value;
265 
266  std::string s_value = ss_value.str();
267 
268  QPen pn(textColor);
269  pn.setWidthF(0.5);
270  painter->setPen(pn);
271  QPointF coordText(x1, newBoxSecond.topLeft().y() - 5);
272 
273  drawText(coordText, painter, ss_value.str());
274 
275  changeColor = firstRect;
276  firstRect = secondRect;
277  secondRect = changeColor;
278  }
279 
280  newBoxSecond = QRectF(boundRect.x(), boundRect.center().y() - gapY/2, boundRect.width(), gapY);
281 
282  //Rect around scale
283  QPen penBackground(black, 0, Qt::SolidLine);
284  painter->setBrush(Qt::NoBrush);
285  painter->setPen(penBackground);
286  painter->drawRect(newBoxSecond);
287 
288  //middle-bottom text
289  double centerX = newBoxSecond.center().x();
290  painter->setPen(QPen(textColor));
291 
292  QPointF coordText(centerX, boundRect.topLeft().y() + 1);
293  drawText(coordText, painter, strUnit);
294 
295  painter->restore();
296 }
297 
299 {
300  QRectF boundRect = boundingRect();
301 
302  painter->save();
303 
304  double unit=1000.0;
305  std::string strUnit="(Km)";
306 
307  ScaleModel* model = dynamic_cast<ScaleModel*>(m_model);
308 
309  if(model->getMapScale() < 1000)
310  {
311  unit = 1.0;
312  strUnit="(m)";
313  }
314  else
315  {
316  unit = 1000.0;
317  }
318 
319  //convert millimeters to centimeters
320  double mmToCm = model->getScaleGapX()/10;
321 
322  double spacing = model->getMapScale()/100;
323 
324  double value = 0.;
325  double width = 0.;
326  double x1 = boundRect.bottomLeft().x();
327 
328  QColor black(0, 0, 0, 255);
329  QColor white(255, 255, 255, 255);
330  QColor firstRect = black;
331  QColor secondRect = white;
332  QColor changeColor;
333  QColor textColor(0, 0, 0, 255);
334 
335  QRectF newBoxFirst;
336  QRectF newBoxSecond;
337 
338  double gapX = model->getScaleGapX();
339  double gapY = model->getScaleGapY();
340 
341  //Rect around scale
342  QPen pn(black, 0, Qt::SolidLine);
343 
344  for( ; x1 < boundRect.topRight().x(); x1 += width)
345  {
346  if(x1+gapX >= boundRect.topRight().x())
347  {
348  //Draw the remaining rects, near the end
349  double dx = boundRect.width() - x1;
350  gapX = dx;
351  }
352 
353  painter->setPen(Qt::NoPen);
354 
355  painter->setBrush(QBrush(white));
356  newBoxSecond = QRectF(x1, boundRect.center().y() - gapY/2, gapX, gapY);
357  painter->drawRect(newBoxSecond);
358 
359  painter->setPen(pn);
360 
361  QLineF lne(x1, boundRect.center().y(), gapX, boundRect.center().y());
362  painter->drawLine(lne);
363 
364  if(width == 0)
365  width = gapX;
366  else
367  value += (spacing * mmToCm)/unit;
368 
369  std::stringstream ss_value;
370  ss_value << value;
371 
372  std::string s_value = ss_value.str();
373 
374  painter->setPen(QPen(textColor));
375  QPointF coordText(x1, newBoxSecond.topLeft().y() - 5);
376 
377  drawText(coordText, painter, ss_value.str());
378 
379  changeColor = firstRect;
380  firstRect = secondRect;
381  secondRect = changeColor;
382  }
383 
384 
385  newBoxSecond = QRectF(boundRect.x(), boundRect.center().y() - gapY/2, boundRect.width(), gapY);
386 
387  QPen penBackground(black, 0, Qt::SolidLine);
388  painter->setBrush(Qt::NoBrush);
389  painter->setPen(penBackground);
390  painter->drawRect(newBoxSecond);
391 
392  //middle-bottom text
393  double centerX = newBoxSecond.center().x();
394  painter->setPen(QPen(textColor));
395 
396  QPointF coordText(centerX, boundRect.topLeft().y() + 1);
397  drawText(coordText, painter, strUnit);
398 
399  painter->restore();
400 }
401 
402 
403 
404 
virtual EnumType * getAlternatingScaleBarType() const
Returns value that represents alternating scale bar type belonging to enumeration.
virtual double getScaleGapX()
Definition: ScaleModel.cpp:160
Abstract class to represent an observable. "Model" part of MVC component.
Definition: Observable.h:56
virtual ~ScaleItem()
Destructor.
Definition: ScaleItem.cpp:52
virtual void drawDoubleAlternatingScaleBar(QPainter *painter)
Definition: ScaleItem.cpp:97
ScaleItem(ItemController *controller, Observable *o)
Constructor.
Definition: ScaleItem.cpp:46
virtual void drawAlternatingScaleBar(QPainter *painter)
Definition: ScaleItem.cpp:199
Abstract class to represent a controller. "Controller" part of MVC component. All classes representin...
Abstract class that represents a graphic item. This object is of type QGraphicsObject.
Definition: ObjectItem.h:61
virtual EnumType * getDoubleAlternatingScaleBarType() const
Returns value that represents double alternating scale bar type belonging to enumeration.
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented from QGraphicsItem.
Definition: ScaleItem.cpp:57
Class that represents a graphic scale of a map. Its coordinate system is the same of scene (millimete...
std::string m_nameClass
Class name.
Definition: ItemObserver.h:201
virtual double getMapScale()
Definition: ScaleModel.cpp:175
virtual EnumScaleType * getEnumScaleType()
Definition: ScaleModel.cpp:180
Class to represent a scale type enumeration. Ex.: hollow scale bar, etc.
Definition: EnumScaleType.h:48
virtual EnumType * getHollowScaleBarType() const
Returns value that represents hollow scale bar type belonging to enumeration.
virtual double getScaleGapY()
Definition: ScaleModel.cpp:170
virtual EnumType * getCurrentScaleType()
Definition: ScaleModel.cpp:221
virtual void drawHollowScaleBar(QPainter *painter)
Definition: ScaleItem.cpp:298
Class that represents a "Model" part of Scale MVC component. Its coordinate system is the same of sce...
Definition: ScaleModel.h:63