All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ItemGroup.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 ItemGroup.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ItemGroup.h"
30 #include "Utils.h"
31 #include "Context.h"
32 #include "ItemModelObservable.h"
33 #include "ItemController.h"
34 #include "../../../color/RGBAColor.h"
35 #include "../../../../qt/widgets/Utils.h"
36 #include "../../../../geometry/Envelope.h"
37 #include "../../../../common/STLUtils.h"
38 #include "AbstractScene.h"
39 #include "ItemObserver.h"
40 
41 // Qt
42 #include <QGraphicsSceneMouseEvent>
43 #include <QStyleOptionGraphicsItem>
44 
46  QGraphicsItemGroup(0, 0),
47  ItemObserver(controller, o)
48 {
49  this->setFlags(QGraphicsItem::ItemIsMovable
50  | QGraphicsItem::ItemIsSelectable
51  | QGraphicsItem::ItemSendsGeometryChanges);
52 
53  QGraphicsItem* item = this;
55 
56  //If enabled is true, this item will accept hover events
57  setAcceptHoverEvents(true);
58 }
59 
61 {
62 
63 }
64 
66 {
67  if(!m_model)
68  return;
69 
70  te::color::RGBAColor** rgba = context.getPixmap();
71 
72  if(!rgba)
73  return;
74 
75  Utils* utils = Context::getInstance()->getUtils();
76 
77  if(!utils)
78  return;
79 
80  te::gm::Envelope box = utils->viewportBox(m_model->getBox());
81 
82  if(!box.isValid())
83  return;
84 
85  if(!box.isValid())
86  return;
87 
88  QPixmap pixmp;
89  QImage* img = 0;
90 
91  if(rgba)
92  {
93  img = te::qt::widgets::GetImage(rgba, box.getWidth(), box.getHeight());
94  pixmp.fill(Qt::transparent);
95  pixmp = QPixmap::fromImage(*img);
96  }
97 
98  te::common::Free(rgba, box.getHeight());
99  if(img)
100  delete img;
101 
102  setPixmap(pixmp);
103  update();
104 }
105 
106 void te::layout::ItemGroup::setPosition( const double& x, const double& y )
107 {
108  setPos(x,y);
109 }
110 
112 {
113  QPointF posF = scenePos();
114  qreal valuex = posF.x();
115  qreal valuey = posF.y();
116 
117  te::gm::Coord2D coordinate;
118  coordinate.x = valuex;
119  coordinate.y = valuey;
120 
121  return coordinate;
122 }
123 
124 void te::layout::ItemGroup::addToGroup( QGraphicsItem * item )
125 {
126  QGraphicsItemGroup::addToGroup(item);
127 
128  QRectF chRect = childrenBoundingRect();
129 
130  ItemModelObservable* model = (ItemModelObservable*)m_controller->getModel();
131  model->setBox(te::gm::Envelope(chRect.x(), chRect.y(), chRect.x() + childrenBoundingRect().width(),
132  chRect.y() + childrenBoundingRect().height()));
133 }
134 
135 void te::layout::ItemGroup::mouseMoveEvent( QGraphicsSceneMouseEvent* event )
136 {
137  QGraphicsItem::mouseMoveEvent(event);
138 }
139 
140 void te::layout::ItemGroup::mousePressEvent( QGraphicsSceneMouseEvent* event )
141 {
142  QGraphicsItem::mousePressEvent(event);
143 
144  int indexItem = -1;
145 
146  QList<QGraphicsItem*> graphicsItems = childItems();
147  foreach( QGraphicsItem *item, graphicsItems)
148  {
149  if (item)
150  {
151  if(item->contains(event->scenePos()))
152  {
153  int a = 0;
154  }
155  }
156  }
157 }
158 
159 void te::layout::ItemGroup::mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
160 {
161  QGraphicsItem::mouseReleaseEvent(event);
162 }
163 
164 void te::layout::ItemGroup::setPixmap( const QPixmap& pixmap )
165 {
166  m_pixmap = pixmap;
167 }
168 
169 void te::layout::ItemGroup::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /*= 0 */ )
170 {
171  Q_UNUSED( option );
172  Q_UNUSED( widget );
173  if ( !painter )
174  {
175  return;
176  }
177 
178  drawBackground( painter );
179  ItemModelObservable* model = (ItemModelObservable*)m_controller->getModel();
180 
181  QRectF boundRect;
182  boundRect = boundingRect();
183 
184  painter->save();
185  painter->translate( model->getBox().getWidth() / 2.0, model->getBox().getHeight() / 2.0 );
186  painter->translate( -boundRect.width() / 2.0, -boundRect.height() / 2.0 );
187  painter->drawPixmap(boundRect, m_pixmap, QRectF( 0, 0, m_pixmap.width(), m_pixmap.height() ));
188  painter->restore();
189 
190  //Draw Selection
191  if (option->state & QStyle::State_Selected)
192  {
193  drawSelection(painter);
194  }
195 
196 }
197 
198 void te::layout::ItemGroup::drawBackground( QPainter * painter )
199 {
200  if (painter)
201  {
202  ItemModelObservable* model = (ItemModelObservable*)m_controller->getModel();
203  //painter->setBrush( brush() );//this causes a problem in atlas generation
204  painter->setPen( Qt::NoPen );
205  painter->setRenderHint( QPainter::Antialiasing, true );
206  painter->drawRect( QRectF( 0, 0, model->getBox().getWidth(), model->getBox().getHeight() ) );
207  }
208 }
209 
210 void te::layout::ItemGroup::drawSelection( QPainter* painter )
211 {
212  if(!painter)
213  {
214  return;
215  }
216 
217  qreal penWidth = painter->pen().widthF();
218 
219  const qreal adj = penWidth / 2;
220  const QColor fgcolor(255,255,255);
221  const QColor backgroundColor(0,0,0);
222 
223  painter->setPen(QPen(backgroundColor, 0, Qt::SolidLine));
224  painter->setBrush(Qt::NoBrush);
225  painter->drawRect(boundingRect().adjusted(adj, adj, -adj, -adj));
226 
227  painter->setPen(QPen(fgcolor, 0, Qt::DashLine));
228  painter->setBrush(Qt::NoBrush);
229  painter->drawRect(boundingRect().adjusted(adj, adj, -adj, -adj));
230 }
231 
232 bool te::layout::ItemGroup::contains( const QPointF &point ) const
233 {
234  ItemModelObservable* model = (ItemModelObservable*)m_controller->getModel();
235 
236  return model->contains(te::gm::Coord2D(point.x(), point.y()));
237 }
238 
240 {
241  QGraphicsItem::setZValue(z);
242  setZValueItem(z);
243 }
244 
246 {
247  return QGraphicsItem::zValue();
248 }
249 
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: ItemGroup.cpp:135
double y
y-coordinate.
Definition: Coord2D.h:87
double x
x-coordinate.
Definition: Coord2D.h:86
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Definition: ItemGroup.cpp:159
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
ItemGroup(ItemController *controller, Observable *o)
Definition: ItemGroup.cpp:45
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
virtual int getZValueItem()
Definition: ItemGroup.cpp:245
virtual void updateObserver(ContextItem context)
Definition: ItemGroup.cpp:65
te::color::RGBAColor ** getPixmap()
Definition: ContextItem.cpp:82
void drawBackground(QPainter *painter)
Definition: ItemGroup.cpp:198
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual void insertItem(ItemObserver *item)=0
Utils * getUtils()
Definition: Context.cpp:126
void drawSelection(QPainter *painter)
Definition: ItemGroup.cpp:210
virtual te::gm::Envelope viewportBox(te::gm::Envelope box)
Definition: Utils.cpp:164
virtual void addToGroup(QGraphicsItem *item)
Definition: ItemGroup.cpp:124
virtual void setPosition(const double &x, const double &y)
Definition: ItemGroup.cpp:106
void setPixmap(const QPixmap &pixmap)
Definition: ItemGroup.cpp:164
TEQTWIDGETSEXPORT QImage * GetImage(te::color::RGBAColor **img, int width, int height)
It creates a QImage from an RGBA color array.
Definition: Utils.cpp:63
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: ItemGroup.cpp:140
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
AbstractScene * getScene()
Definition: Context.cpp:61
virtual te::gm::Envelope getBox()
virtual bool contains(const QPointF &point) const
Definition: ItemGroup.cpp:232
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Definition: ItemGroup.cpp:169
static Context * getInstance()
This function is called to create an instance of the class.
Definition: Context.cpp:46
void setZValue(qreal z)
Definition: ItemGroup.cpp:239
virtual bool contains(const te::gm::Coord2D &coord) const
virtual void setBox(te::gm::Envelope box)
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
virtual te::gm::Coord2D getPosition()
Definition: ItemGroup.cpp:111