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) 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 ItemGroup.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "ItemGroup.h"
30 #include "../../core/pattern/singleton/Context.h"
31 #include "../../core/pattern/mvc/ItemModelObservable.h"
32 #include "../../core/pattern/mvc/ItemController.h"
33 #include "../../../color/RGBAColor.h"
34 #include "../../../qt/widgets/Utils.h"
35 #include "../../../geometry/Envelope.h"
36 #include "../../../common/STLUtils.h"
37 #include "../../core/AbstractScene.h"
38 #include "../../core/pattern/mvc/ItemObserver.h"
39 
40 // Qt
41 #include <QGraphicsSceneMouseEvent>
42 #include <QStyleOptionGraphicsItem>
43 
45  QGraphicsItemGroup(0),
46  ItemObserver(controller, o)
47 {
48  this->setFlags(QGraphicsItem::ItemIsMovable
49  | QGraphicsItem::ItemIsSelectable
50  | QGraphicsItem::ItemSendsGeometryChanges);
51 
52  QGraphicsItem* item = this;
53  Context::getInstance().getScene()->insertItem((ItemObserver*)item);
54 
55  m_nameClass = "ItemGroup";
56 
57  //If enabled is true, this item will accept hover events
58  setAcceptHoverEvents(true);
59 }
60 
62 {
63 
64 }
65 
67 {
68  if(!m_model)
69  return;
70 
71  te::color::RGBAColor** rgba = context.getPixmap();
72 
73  if(!rgba)
74  return;
75 
76  Utils* utils = context.getUtils();
77 
78  if(!utils)
79  return;
80 
81  te::gm::Envelope box = utils->viewportBox(m_model->getBox());
82 
83  if(!box.isValid())
84  return;
85 
86  if(!box.isValid())
87  return;
88 
89  QPixmap pixmp;
90  QImage* img = 0;
91 
92  if(rgba)
93  {
94  img = te::qt::widgets::GetImage(rgba, box.getWidth(), box.getHeight());
95  pixmp.fill(Qt::transparent);
96  pixmp = QPixmap::fromImage(*img);
97  }
98 
99  te::common::Free(rgba, box.getHeight());
100  if(img)
101  delete img;
102 
103  setPixmap(pixmp);
104  update();
105 }
106 
107 void te::layout::ItemGroup::setPosition( const double& x, const double& y )
108 {
109  setPos(x,y);
110 }
111 
113 {
114  QPointF posF = scenePos();
115  qreal valuex = posF.x();
116  qreal valuey = posF.y();
117 
118  te::gm::Coord2D coordinate;
119  coordinate.x = valuex;
120  coordinate.y = valuey;
121 
122  return coordinate;
123 }
124 
125 void te::layout::ItemGroup::addToGroup( QGraphicsItem * item )
126 {
127  QGraphicsItemGroup::addToGroup(item);
128 
129  QRectF chRect = childrenBoundingRect();
130 
131  ItemModelObservable* model = (ItemModelObservable*)m_controller->getModel();
132  model->setBox(te::gm::Envelope(chRect.x(), chRect.y(), chRect.x() + childrenBoundingRect().width(),
133  chRect.y() + childrenBoundingRect().height()));
134 }
135 
136 void te::layout::ItemGroup::mouseMoveEvent( QGraphicsSceneMouseEvent* event )
137 {
138  QGraphicsItem::mouseMoveEvent(event);
139 }
140 
141 void te::layout::ItemGroup::mousePressEvent( QGraphicsSceneMouseEvent* event )
142 {
143  QGraphicsItem::mousePressEvent(event);
144 
145  QList<QGraphicsItem*> graphicsItems = childItems();
146  foreach( QGraphicsItem *item, graphicsItems)
147  {
148  if (item)
149  {
150  if(item->contains(event->scenePos()))
151  {
152 
153  }
154  }
155  }
156 }
157 
158 void te::layout::ItemGroup::mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
159 {
160  QGraphicsItem::mouseReleaseEvent(event);
161 }
162 
163 void te::layout::ItemGroup::setPixmap( const QPixmap& pixmap )
164 {
165  m_pixmap = pixmap;
166 }
167 
168 void te::layout::ItemGroup::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /*= 0 */ )
169 {
170  Q_UNUSED( option );
171  Q_UNUSED( widget );
172  if ( !painter )
173  {
174  return;
175  }
176 
177  drawBackground( painter );
178  ItemModelObservable* model = (ItemModelObservable*)m_controller->getModel();
179 
180  QRectF boundRect;
181  boundRect = boundingRect();
182 
183  painter->save();
184  painter->translate( model->getBox().getWidth() / 2.0, model->getBox().getHeight() / 2.0 );
185  painter->translate( -boundRect.width() / 2.0, -boundRect.height() / 2.0 );
186  QRectF rtSource( 0, 0, m_pixmap.width(), m_pixmap.height() );
187  painter->drawPixmap(boundRect, m_pixmap, rtSource);
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  te::gm::Coord2D coord(point.x(), point.y());
235  return m_controller->contains(coord);
236 }
237 
239 {
240  QGraphicsItem::setZValue(z);
241  setZValueItem(z);
242 }
243 
245 {
246  return QGraphicsItem::zValue();
247 }
248 
250 {
251  if(!m_model)
252  return;
253 
254  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
255  if(!model)
256  return;
257 
258  double angle = model->getAngle();
259 
260  if(angle == model->getOldAngle())
261  return;
262 
263  QPointF center = boundingRect().center();
264 
265  double centerX = center.x();
266  double centerY = center.y();
267 
268  setTransform(QTransform().translate(centerX, centerY).rotate(angle).translate(-centerX, -centerY));
269 }
270 
272 {
273  QImage img = m_pixmap.toImage();
275  return teImg;
276 }
277 
278 
Abstract class to represent an observable. "Model" part of MVC component.
Definition: Observable.h:56
Class responsible for maintaining the drawing context of a MVC component. It is always used by the "M...
Definition: ContextItem.h:49
virtual void applyRotation()
Rotates the graphic component. Reimplement this function in a ItemObserver subclass to provide the it...
Definition: ItemGroup.cpp:249
virtual te::color::RGBAColor ** getRGBAColorImage(int &w, int &h)
Returns a image of the graphic component. Reimplement this function in a ItemObserver subclass to pro...
Definition: ItemGroup.cpp:271
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: ItemGroup.cpp:136
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Definition: ItemGroup.cpp:158
Abstract class to represent an observable. "Model" part of MVC component. All classes representing th...
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
ItemGroup(ItemController *controller, Observable *o)
Definition: ItemGroup.cpp:44
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
Definition: ItemObserver.h:52
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
Abstract class to represent a controller. "Controller" part of MVC component. All classes representin...
virtual int getZValueItem()
Return the Z value. Reimplement this function in a ItemObserver subclass to provide the item's getZVa...
Definition: ItemGroup.cpp:244
virtual void updateObserver(ContextItem context)
Reimplemented from Observer.
Definition: ItemGroup.cpp:66
virtual te::color::RGBAColor ** getPixmap()
Returns pixmap generated after drawing.
Definition: ContextItem.cpp:90
virtual double getAngle()
Returns the value of rotation.
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
static Context & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual Utils * getUtils()
Class that represents the grouping of objects of type QGraphicsItem, MVC components. Its coordinate system is the same of scene (millimeters). He is also the son of ItemObserver and ObjectItem, so it can become observer of a model (Observable).
void drawSelection(QPainter *painter)
Definition: ItemGroup.cpp:210
std::string m_nameClass
Class name.
Definition: ItemObserver.h:201
virtual te::gm::Envelope viewportBox(te::gm::Envelope box)
Converts the box world (mm) to screen coordinates (pixel).
Definition: Utils.cpp:167
virtual void addToGroup(QGraphicsItem *item)
Definition: ItemGroup.cpp:125
virtual void setPosition(const double &x, const double &y)
Definition: ItemGroup.cpp:107
void setPixmap(const QPixmap &pixmap)
Definition: ItemGroup.cpp:163
TEQTWIDGETSEXPORT QImage * GetImage(te::color::RGBAColor **img, int width, int height)
It creates a QImage from an RGBA color array.
Definition: Utils.cpp:69
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: ItemGroup.cpp:141
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual te::gm::Envelope getBox()
Reimplemented from Observable.
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:168
void setZValue(qreal z)
Definition: ItemGroup.cpp:238
Utility class with functions to manipulate the canvas and conversion between projections.
Definition: Utils.h:61
virtual double getOldAngle()
Returns the value of old rotation.
virtual void setBox(te::gm::Envelope box)
Change the bounding rectangle.
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()
Method that returns the position llx, lly Reimplement this function in a ItemObserver subclass to pro...
Definition: ItemGroup.cpp:112