All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PointItem.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 PointItem.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "PointItem.h"
30 #include "../../core/pattern/mvc/ItemController.h"
31 #include "../../core/AbstractScene.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/PointModel.h"
38 #include "../../core/enum/EnumPointType.h"
39 
40 #include <cmath>
41 
43  ObjectItem(controller, o)
44 {
45  m_nameClass = std::string(this->metaObject()->className());
46 }
47 
49 {
50 
51 }
52 
53 void te::layout::PointItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /*= 0 */ )
54 {
55  Q_UNUSED( option );
56  Q_UNUSED( widget );
57  if ( !painter )
58  {
59  return;
60  }
61 
62  if(m_resizeMode)
63  {
64  ObjectItem::paint(painter, option, widget);
65  return;
66  }
67 
68  drawBackground(painter);
69 
70  PointModel* model = dynamic_cast<PointModel*>(m_model);
71 
72  if(model)
73  {
74  EnumPointType* enumScale = model->getEnumPointType();
75 
76  if(model->getCurrentPointType() == enumScale->getStarType())
77  {
78  drawStar(painter);
79  }
80  if(model->getCurrentPointType() == enumScale->getCircleType())
81  {
82  drawCircle(painter);
83  }
84  if(model->getCurrentPointType() == enumScale->getXType())
85  {
86  drawX(painter);
87  }
88  if(model->getCurrentPointType() == enumScale->getSquareType())
89  {
90  drawSquare(painter);
91  }
92  if(model->getCurrentPointType() == enumScale->getRhombusType())
93  {
94  drawRhombus(painter);
95  }
96  if(model->getCurrentPointType() == enumScale->getCrossType())
97  {
98  drawCross(painter);
99  }
100  }
101 
102  drawBorder(painter);
103 
104  //Draw Selection
105  if (option->state & QStyle::State_Selected)
106  {
107  drawSelection(painter);
108  }
109 }
110 
111 void te::layout::PointItem::drawStar( QPainter * painter )
112 {
113  PointModel* model = dynamic_cast<PointModel*>(m_model);
114  if(!model)
115  {
116  return;
117  }
118 
119  painter->save();
120 
121  double halfW = boundingRect().width() / 4.;
122 
123  double w = boundingRect().width() / 2.;
124 
125  QPainterPath rhombus_path;
126 
127  QPolygonF poly;
128  qreal const c = halfW;
129  qreal const d = w;
130  bool inner = true;
131  QPointF pUnion;
132  for ( qreal i = 0 ; i < 2*3.14; i += 3.14/5.0, inner=!inner ) {
133  qreal const f = inner ? c : d;
134  poly << QPointF( f * std::cos(i), f * std::sin(i) );
135  if(i == 0)
136  {
137  pUnion = QPointF( f * std::cos(i), f * std::sin(i) );
138  }
139  }
140  poly << pUnion;
141  poly.translate(boundingRect().center());
142 
143  rhombus_path.addPolygon(poly);
144 
145  te::color::RGBAColor clrPoint = model->getPointColor();
146 
147  QColor pointColor;
148  pointColor.setRed(clrPoint.getRed());
149  pointColor.setGreen(clrPoint.getGreen());
150  pointColor.setBlue(clrPoint.getBlue());
151  pointColor.setAlpha(clrPoint.getAlpha());
152 
153  QPen pn(pointColor, 0, Qt::SolidLine);
154  painter->setPen(pn);
155 
156  painter->setBrush(pointColor);
157  painter->drawPath(rhombus_path);
158 
159  painter->restore();
160 }
161 
162 void te::layout::PointItem::drawCircle( QPainter * painter )
163 {
164  PointModel* model = dynamic_cast<PointModel*>(m_model);
165  if(!model)
166  {
167  return;
168  }
169 
170  painter->save();
171 
172  double halfW = boundingRect().width() / 4.;
173  double halfH = boundingRect().height() / 4.;
174 
175  double x = boundingRect().center().x() - halfW;
176  double y = boundingRect().center().y() - halfH;
177  double w = boundingRect().width() / 2.;
178  double h = boundingRect().height() / 2.;
179 
180  QRectF pointRect(x, y, w, h);
181 
182  QPainterPath circle_path;
183  circle_path.addEllipse(pointRect);
184 
185  te::color::RGBAColor clrPoint = model->getPointColor();
186 
187  QColor pointColor;
188  pointColor.setRed(clrPoint.getRed());
189  pointColor.setGreen(clrPoint.getGreen());
190  pointColor.setBlue(clrPoint.getBlue());
191  pointColor.setAlpha(clrPoint.getAlpha());
192 
193  QPen pn(pointColor, 0, Qt::SolidLine);
194  painter->setPen(pn);
195 
196  painter->setBrush(pointColor);
197  painter->drawPath(circle_path);
198 
199  painter->restore();
200 }
201 
202 void te::layout::PointItem::drawX( QPainter * painter )
203 {
204  PointModel* model = dynamic_cast<PointModel*>(m_model);
205  if(!model)
206  {
207  return;
208  }
209 
210  painter->save();
211 
212  double halfW = boundingRect().width() / 4.;
213  double halfH = boundingRect().height() / 4.;
214 
215  double x = boundingRect().center().x() - halfW;
216  double y = boundingRect().center().y() + halfH;
217  double w = boundingRect().width() / 2.;
218 
219  QFont ft = painter->font();
220  ft.setPointSizeF(w);
221 
222  QPainterPath rect_path;
223  rect_path.addText(x, y, ft, "X");
224 
225  te::color::RGBAColor clrPoint = model->getPointColor();
226 
227  QColor pointColor;
228  pointColor.setRed(clrPoint.getRed());
229  pointColor.setGreen(clrPoint.getGreen());
230  pointColor.setBlue(clrPoint.getBlue());
231  pointColor.setAlpha(clrPoint.getAlpha());
232 
233  QPen pn(pointColor, 0, Qt::SolidLine);
234  painter->setPen(pn);
235 
236  painter->setBrush(pointColor);
237  painter->drawPath(rect_path);
238 
239  painter->restore();
240 }
241 
242 void te::layout::PointItem::drawSquare( QPainter * painter )
243 {
244  PointModel* model = dynamic_cast<PointModel*>(m_model);
245  if(!model)
246  {
247  return;
248  }
249 
250  painter->save();
251 
252  double halfW = boundingRect().width() / 4.;
253  double halfH = boundingRect().height() / 4.;
254 
255  double x = boundingRect().center().x() - halfW;
256  double y = boundingRect().center().y() - halfH;
257  double w = boundingRect().width() / 2.;
258  double h = boundingRect().height() / 2.;
259 
260  QRectF pointRect(x, y, w, h);
261 
262  QPainterPath rect_path;
263  rect_path.addRect(pointRect);
264 
265  te::color::RGBAColor clrPoint = model->getPointColor();
266 
267  QColor pointColor;
268  pointColor.setRed(clrPoint.getRed());
269  pointColor.setGreen(clrPoint.getGreen());
270  pointColor.setBlue(clrPoint.getBlue());
271  pointColor.setAlpha(clrPoint.getAlpha());
272 
273  QPen pn(pointColor, 0, Qt::SolidLine);
274  painter->setPen(pn);
275 
276  painter->setBrush(pointColor);
277  painter->drawPath(rect_path);
278 
279  painter->restore();
280 }
281 
282 void te::layout::PointItem::drawRhombus( QPainter * painter )
283 {
284  PointModel* model = dynamic_cast<PointModel*>(m_model);
285  if(!model)
286  {
287  return;
288  }
289 
290  painter->save();
291 
292  double centerX = boundingRect().center().x();
293  double centerY = boundingRect().center().y();
294 
295  double halfW = boundingRect().width() / 4.;
296  double halfH = boundingRect().height() / 4.;
297 
298  double x = centerX - halfW;
299  double y = centerY + halfH;
300 
301  QPolygonF poly;
302  poly.push_back(QPoint(centerX, y));
303  poly.push_back(QPoint(centerX + halfW, centerY));
304  poly.push_back(QPoint(centerX, centerY - halfH));
305  poly.push_back(QPoint(x, centerY));
306  poly.push_back(QPoint(centerX, y));
307 
308  QPainterPath rhombus_path;
309  rhombus_path.addPolygon(poly);
310 
311  te::color::RGBAColor clrPoint = model->getPointColor();
312 
313  QColor pointColor;
314  pointColor.setRed(clrPoint.getRed());
315  pointColor.setGreen(clrPoint.getGreen());
316  pointColor.setBlue(clrPoint.getBlue());
317  pointColor.setAlpha(clrPoint.getAlpha());
318 
319  QPen pn(pointColor, 0, Qt::SolidLine);
320  painter->setPen(pn);
321 
322  painter->setBrush(pointColor);
323  painter->drawPath(rhombus_path);
324 
325  painter->restore();
326 }
327 
328 void te::layout::PointItem::drawCross( QPainter * painter )
329 {
330  PointModel* model = dynamic_cast<PointModel*>(m_model);
331  if(!model)
332  {
333  return;
334  }
335 
336  painter->save();
337 
338  double centerX = boundingRect().center().x();
339  double centerY = boundingRect().center().y();
340 
341  double halfW = boundingRect().width() / 4.;
342  double halfH = boundingRect().height() / 4.;
343 
344  double x = centerX - halfW;
345  double y = centerY + halfH;
346  double w = boundingRect().width() / 2.;
347  double h = boundingRect().height() / 2.;
348 
349  te::color::RGBAColor clrPoint = model->getPointColor();
350 
351  QColor pointColor;
352  pointColor.setRed(clrPoint.getRed());
353  pointColor.setGreen(clrPoint.getGreen());
354  pointColor.setBlue(clrPoint.getBlue());
355  pointColor.setAlpha(clrPoint.getAlpha());
356 
357  QPen pn(pointColor, 0, Qt::SolidLine);
358  painter->setPen(pn);
359 
360  painter->setBrush(pointColor);
361  painter->drawLine(x, centerY, x + w, centerY);
362  painter->drawLine(centerX, y, centerX, y - h);
363 
364  painter->restore();
365 }
366 
367 
virtual void drawSquare(QPainter *painter)
Definition: PointItem.cpp:242
Abstract class to represent an observable. "Model" part of MVC component.
Definition: Observable.h:56
virtual EnumType * getCircleType() const
Returns value that represents circle type belonging to enumeration.
virtual void drawStar(QPainter *painter)
Definition: PointItem.cpp:111
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
Class to represent a scale point enumeration. Ex.: X, square, circle, etc.
Definition: EnumPointType.h:48
PointItem(ItemController *controller, Observable *o)
Constructor.
Definition: PointItem.cpp:42
virtual EnumType * getCurrentPointType()
Definition: PointModel.cpp:97
virtual te::color::RGBAColor getPointColor()
Definition: PointModel.cpp:143
virtual ~PointItem()
Destructor.
Definition: PointItem.cpp:48
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
virtual void drawCross(QPainter *painter)
Definition: PointItem.cpp:328
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 EnumPointType * getEnumPointType()
Definition: PointModel.cpp:92
virtual void drawCircle(QPainter *painter)
Definition: PointItem.cpp:162
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented from QGraphicsItem.
Definition: PointItem.cpp:53
virtual void drawX(QPainter *painter)
Definition: PointItem.cpp:202
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
std::string m_nameClass
Class name.
Definition: ItemObserver.h:201
virtual void drawRhombus(QPainter *painter)
Definition: PointItem.cpp:282
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented from QGraphicsItem.
Class that represents a "Model" part of Point MVC component. Its coordinate system is the same of sce...
Definition: PointModel.h:54
virtual EnumType * getRhombusType() const
Returns value that represents rhombus type belonging to enumeration.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual EnumType * getSquareType() const
Returns value that represents square type belonging to enumeration.
virtual EnumType * getXType() const
Returns value that represents X type belonging to enumeration.
virtual EnumType * getStarType() const
Returns value that represents star type belonging to enumeration.
virtual EnumType * getCrossType() const
Returns value that represents cross type belonging to enumeration.
Class that represents a graphic Point. Its coordinate system is the same of scene (millimeters)...