All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ArrowItem.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 ArrowItem.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ArrowItem.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/ArrowModel.h"
38 
39 #include <cmath>
40 
42  ObjectItem(controller, o)
43 {
44  m_nameClass = std::string(this->metaObject()->className());
45 }
46 
48 {
49 
50 }
51 
52 void te::layout::ArrowItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /*= 0 */ )
53 {
54  Q_UNUSED( option );
55  Q_UNUSED( widget );
56  if ( !painter )
57  {
58  return;
59  }
60 
61  drawBackground(painter);
62 
63  drawArrow(painter);
64 
65  drawBorder(painter);
66 
67  //Draw Selection
68  if (option->state & QStyle::State_Selected)
69  {
70  drawSelection(painter);
71  }
72 }
73 
74 void te::layout::ArrowItem::drawArrow( QPainter * painter )
75 {
76  ArrowModel* model = dynamic_cast<ArrowModel*>(m_model);
77  if(!model)
78  {
79  return;
80  }
81 
82  painter->save();
83 
84  QRectF boundRect = boundingRect();
85 
86  QColor cblack(0,0,0);
87  QPen pn(cblack, 0, Qt::SolidLine);
88  painter->setPen(pn);
89 
90  double centerY = boundRect.center().y();
91 
92  QLineF lne(boundRect.bottomLeft().x(), centerY, boundRect.width(), centerY);
93 
94  painter->drawLine(lne);
95 
96  painter->restore();
97 
98  /* Draw Arrow Head */
99  drawHeadArrow(painter);
100 }
101 
102 void te::layout::ArrowItem::drawHeadArrow( QPainter * painter )
103 {
104  painter->save();
105 
106  QBrush bsh(Qt::black);
107  painter->setBrush(bsh);
108  painter->setPen(Qt::NoPen);
109 
110  QRectF boundRect = boundingRect();
111 
112  double h = boundRect.height();
113 
114  double centerY = boundRect.center().y();
115  QLineF lne(boundRect.bottomLeft().x(), centerY, boundRect.width(), centerY);
116 
117  double Pi = 3.14;
118  double sizeHead = (h / 5.);
119 
120  double angle = std::acos(lne.dx() / lne.length());
121  if (lne.dy() >= 0)
122  angle = (Pi * 2) - angle;
123 
124  QPointF arrowP1 = lne.p1() + QPointF(sin(angle + Pi / 3) * sizeHead,
125  cos(angle + Pi / 3) * sizeHead);
126  QPointF arrowP2 = lne.p1() + QPointF(sin(angle + Pi - Pi / 3) * sizeHead,
127  cos(angle + Pi - Pi / 3) * sizeHead);
128 
129  QPolygonF trianglePolygon;
130  trianglePolygon << lne.p1() << arrowP1 << arrowP2;
131 
132  painter->drawPolygon(trianglePolygon);
133 
134  painter->restore();
135 }
136 
137 
Abstract class to represent an observable. "Model" part of MVC component.
Definition: Observable.h:56
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented from QGraphicsItem.
Definition: ArrowItem.cpp:52
ArrowItem(ItemController *controller, Observable *o)
Constructor.
Definition: ArrowItem.cpp:41
virtual ~ArrowItem()
Destructor.
Definition: ArrowItem.cpp:47
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
std::string m_nameClass
Class name.
Definition: ItemObserver.h:201
Class that represents a "Model" part of Arrow MVC component. Its coordinate system is the same of sce...
Definition: ArrowModel.h:54
virtual void drawHeadArrow(QPainter *painter)
Drawing method of a Head Arrow.
Definition: ArrowItem.cpp:102
virtual void drawArrow(QPainter *painter)
Drawing method of a Arrow.
Definition: ArrowItem.cpp:74
Class that represents a graphic Arrow. Its coordinate system is the same of scene (millimeters)...