All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ParentItem.h
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 ParentItem.h
22 
23  \brief Abstract class that represents a graphic item.
24  Its coordinate system is the same of scene (millimeters). Knows rotate and resize. Stores a pixmap drawn by model.
25  This is also son of ItemObserver, so it can become observer of a model (Observable).
26  This class will be inherited and will became the view part of the MVC component.
27  Who inherits it is required the implementation of updateObserver(ContextItem context) method.
28  Drawing starting point is llx, lly.
29  Can't add signals and slots in this class because moc(Qt) doesn't support templates.
30 
31  \ingroup layout
32 */
33 
34 #ifndef __TERRALIB_LAYOUT_INTERNAL_PARENT_ITEM_H
35 #define __TERRALIB_LAYOUT_INTERNAL_PARENT_ITEM_H
36 
37 // Qt
38 #include <QPixmap>
39 #include <QVariant>
40 #include <QPainter>
41 #include <QGraphicsSceneHoverEvent>
42 #include <QGraphicsSceneMouseEvent>
43 #include <QStyleOptionGraphicsItem>
44 #include <QWidget>
45 #include <QTransform>
46 
47 // TerraLib
48 #include "../../core/pattern/mvc/ItemObserver.h"
49 #include "../../../geometry/Envelope.h"
50 #include "../../core/enum/AbstractType.h"
51 #include "../../../color/RGBAColor.h"
52 #include "../../core/Config.h"
53 #include "../../core/pattern/singleton/Context.h"
54 #include "../../core/pattern/mvc/ItemController.h"
55 #include "../../core/AbstractScene.h"
56 #include "../../core/pattern/mvc/ItemModelObservable.h"
57 #include "../core/Scene.h"
58 #include "../../../qt/widgets/Utils.h"
59 #include "../../../geometry/Envelope.h"
60 #include "../../../common/STLUtils.h"
61 #include "../../core/ContextItem.h"
62 #include "../../../geometry/Coord2D.h"
63 
64 // STL
65 
66 #include <string>
67 
68 namespace te
69 {
70  namespace layout
71  {
72  /*!
73  \brief Abstract class that represents a graphic item.
74  Its coordinate system is the same of scene (millimeters). Knows rotate and resize. Stores a pixmap drawn by model.
75  This is also son of ItemObserver, so it can become observer of a model (Observable).
76  This class will be inherited and will became the view part of the MVC component.
77  Who inherits it is required the implementation of updateObserver(ContextItem context) method.
78  Drawing starting point is llx, lly.
79  Can't add signals and slots in this class because moc(Qt) doesn't support templates.
80 
81  \ingroup layout
82 
83  \sa te::layout::ItemObserver
84  */
85  template <class T>
86  class TELAYOUTEXPORT ParentItem : public T, public ItemObserver
87  {
88  public:
89 
90  /*!
91  \brief Constructor
92 
93  \param controller "Controller" part of MVC component
94  \param o "Model" part of MVC component
95  */
96  ParentItem(ItemController* controller = 0, Observable* o = 0, bool inverted = false);
97 
98  /*!
99  \brief Destructor
100  */
101  virtual ~ParentItem();
102 
103  /*!
104  \brief Reimplemented from ItemObserver
105  */
106  virtual void updateObserver(te::layout::ContextItem context);
107 
108  /*!
109  \brief Reimplemented from QGraphicsItem
110  */
111  virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
112 
113  /*!
114  \brief Reimplemented from ItemObserver
115  */
116  virtual te::gm::Coord2D getPosition();
117 
118  virtual void setPixmap( const QPixmap& pixmap );
119 
120  virtual QPixmap getPixmap();
121 
122  virtual QRectF boundingRect() const;
123 
124  virtual void setRect(QRectF rect);
125 
126  //Override
127  /*World coordinates(mm)*/
128  virtual bool contains(const QPointF &point) const;
129 
130  /*!
131  \brief The Z value decides the stacking order of drawing.
132 
133  \param drawing order
134  */
135  void setZValue ( qreal z );
136 
137  /*!
138  \brief Reimplemented from ItemObserver
139  */
140  virtual te::color::RGBAColor** getRGBAColorImage(int &w, int &h);
141 
142  /*!
143  \brief Reimplemented from ItemObserver
144  */
145  virtual QImage getImage();
146 
147  protected:
148 
149  virtual void drawBackground( QPainter* painter );
150 
151  virtual void drawSelection(QPainter* painter);
152 
153  virtual void drawBorder(QPainter* painter);
154 
155  /*!
156  \brief Reimplemented from QGraphicsItem
157  */
158  virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
159 
160  /*!
161  \brief Reimplemented from QGraphicsItem
162  */
163  virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
164 
165  /*!
166  \brief Reimplemented from QGraphicsItem
167  */
168  virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
169 
170  /*!
171  \brief Reimplemented from QGraphicsItem
172  */
173  virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
174 
175  /*!
176  \brief Reimplemented from QGraphicsItem
177  */
178  virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event );
179 
180  /*!
181  \brief Reimplemented from QGraphicsItem
182  */
183  virtual void hoverMoveEvent ( QGraphicsSceneHoverEvent * event );
184 
185  /*!
186  \brief Reimplemented from QGraphicsItem
187  */
188  virtual QVariant itemChange ( QGraphicsItem::GraphicsItemChange change, const QVariant & value );
189 
190  virtual bool checkTouchesCorner(const double& x, const double& y);
191 
192  virtual QPixmap calculateNewPixmap(const double& x, const double& y);
193 
194  virtual te::gm::Envelope createNewBoxInCoordScene(const double& x, const double& y);
195 
196  /*!
197  \brief Reimplemented from ItemObserver
198  */
199  virtual int getZValueItem();
200 
201  /*!
202  \brief Reimplemented from ItemObserver
203  */
204  virtual void applyRotation();
205 
206  /*!
207  \brief Draw a text. Converts boundingRect item coordinates (local coordinates) in pixel coordinates (screen coordinates) of the current device.
208 
209  \param point initial text coordinate (local coordinates from boundingRect)
210  \param painter low-level painting on widgets and other paint devices
211  \param text
212  */
213  virtual void drawText(QPointF point, QPainter* painter, std::string text);
214 
215  virtual void createResizePixmap();
216 
217  protected:
218 
219  QPixmap m_pixmap;
220  QRectF m_rect;//In local coordinate
221 
222  //Resize
223  te::gm::Envelope m_boxCopy; //!< box with resize
224  QPixmap m_clonePixmap;
225  bool m_mousePressedAlt; //!< mouse and active alt key
226  QPointF m_initialCoord;
227  QPointF m_finalCoord;
228  bool m_toResizeItem; //!< pixmap to perform the resize is not yet built
230  bool m_resizeMode; //!< pixmap to perform the resize is already built
231  };
232 
233  template <class T>
234  inline te::layout::ParentItem<T>::ParentItem( ItemController* controller, Observable* o, bool inverted ) :
235  T(0),
236  ItemObserver(controller, o),
237  m_mousePressedAlt(false),
238  m_toResizeItem(false),
239  m_enumSides(TPNoneSide),
240  m_resizeMode(false)
241  {
242 
243  m_invertedMatrix = inverted;
244 
245  QGraphicsItem* item = this;
246  Context::getInstance().getScene()->insertItem((ItemObserver*)item);
247 
248  this->setFlags(QGraphicsItem::ItemIsMovable
249  | QGraphicsItem::ItemIsSelectable
250  | QGraphicsItem::ItemSendsGeometryChanges
251  | QGraphicsItem::ItemIsFocusable);
252 
253  //If enabled is true, this item will accept hover events
254  QGraphicsItem::setAcceptHoverEvents(true);
255 
256  m_boxCopy = m_model->getBox();
257 
258  QRectF rect(0, 0, m_model->getBox().getWidth(), m_model->getBox().getHeight());
259  setRect(rect);
260  }
261 
262  template <class T>
264  {
265 
266  }
267 
268  template <class T>
270  {
271  if(m_model)
272  {
273  if(m_rect.width() != m_model->getBox().getWidth() ||
274  m_rect.height() != m_model->getBox().getHeight())
275  {
276  setRect(QRectF(0, 0, m_model->getBox().getWidth(), m_model->getBox().getHeight()));
277  }
278  }
279 
280  if(context.isChangePos())
281  {
282  double x = context.getPos().x;
283  double y = context.getPos().y;
284  QGraphicsItem::setPos(x, y);
285  }
286 
287  QGraphicsItem::update();
288  }
289 
290  template <class T>
291  inline void te::layout::ParentItem<T>::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget /*= 0 */ )
292  {
293  Q_UNUSED( option );
294  Q_UNUSED( widget );
295  if ( !painter || !m_toResizeItem )
296  {
297  return;
298  }
299 
300  drawBackground( painter );
301 
302  QRectF boundRect;
303  boundRect = boundingRect();
304 
305  painter->save();
306  painter->translate( -boundRect.bottomLeft().x(), -boundRect.topRight().y() );
307  QRectF rtSource( 0, 0, m_clonePixmap.width(), m_clonePixmap.height() );
308  painter->drawPixmap(boundRect, m_clonePixmap, rtSource);
309  painter->restore();
310 
311  drawBorder(painter);
312 
313  //Draw Selection
314  if (option->state & QStyle::State_Selected)
315  {
316  drawSelection(painter);
317  }
318  }
319 
320  template <class T>
321  inline void te::layout::ParentItem<T>::drawText( QPointF point, QPainter* painter, std::string text )
322  {
323  painter->save();
324 
325  QTransform t = painter->transform();
326  QPointF p = t.map(point);
327 
328  double zoomFactor = Context::getInstance().getZoomFactor();
329 
330  QFont ft = painter->font();
331  ft.setPointSize(ft.pointSize() * zoomFactor);
332  painter->setFont(ft);
333 
334  //Keeps the size of the text.(Aspect)
335  painter->setMatrixEnabled(false);
336  painter->drawText(p, text.c_str());
337  painter->setMatrixEnabled(true);
338 
339  painter->restore();
340  }
341 
342  template <class T>
344  {
345  QPointF posF = QGraphicsItem::scenePos();
346  qreal valuex = posF.x();
347  qreal valuey = posF.y();
348 
349  te::gm::Coord2D coordinate;
350  coordinate.x = valuex;
351  coordinate.y = valuey;
352 
353  return coordinate;
354  }
355 
356  template <class T>
357  inline void te::layout::ParentItem<T>::setPixmap( const QPixmap& pixmap )
358  {
359  if(pixmap.isNull())
360  return;
361 
362  /* The model draws on Cartesian coordinates, millimeter.
363  The canvas of Terralib 5 works with the Y-Axis inverted,
364  so the pixmap generated is upside down.*/
365  m_pixmap = pixmap;
366  QImage img = m_pixmap.toImage();
367  QImage image = img.mirrored(false, true);
368  m_pixmap = QPixmap::fromImage(image);
369 
370  te::gm::Envelope box = m_model->getBox();
371 
372  if(m_mousePressedAlt)
373  box = m_boxCopy;
374 
375  QGraphicsItem::prepareGeometryChange();
376  setRect(QRectF(0, 0, box.getWidth(), box.getHeight()));
377  QGraphicsItem::update();
378  }
379 
380  template <class T>
381  inline void te::layout::ParentItem<T>::drawBackground( QPainter * painter )
382  {
383  if ( !painter )
384  {
385  return;
386  }
387 
388  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
389 
390  if(!model)
391  return;
392 
393  te::color::RGBAColor clrBack = model->getBackgroundColor();
394  QColor backColor;
395  backColor.setRed(clrBack.getRed());
396  backColor.setGreen(clrBack.getGreen());
397  backColor.setBlue(clrBack.getBlue());
398  backColor.setAlpha(clrBack.getAlpha());
399 
400  painter->save();
401  painter->setPen(Qt::NoPen);
402  painter->setBrush(QBrush(backColor));
403  painter->setBackground(QBrush(backColor));
404  painter->setRenderHint( QPainter::Antialiasing, true );
405  painter->drawRect(QRectF( 0, 0, boundingRect().width(), boundingRect().height()));
406  painter->restore();
407  }
408 
409  template <class T>
410  inline void te::layout::ParentItem<T>::drawSelection( QPainter* painter )
411  {
412  if(!painter)
413  {
414  return;
415  }
416 
417  painter->save();
418 
419  qreal penWidth = painter->pen().widthF();
420 
421  const qreal adj = penWidth / 2;
422  const QColor fgcolor(0,255,0);
423  const QColor backgroundColor(0,0,0);
424 
425  QRectF rtAdjusted = boundingRect().adjusted(adj, adj, -adj, -adj);
426 
427  QPen penBackground(backgroundColor, 0, Qt::SolidLine);
428  painter->setPen(penBackground);
429  painter->setBrush(Qt::NoBrush);
430  painter->drawRect(rtAdjusted);
431 
432  QPen penForeground(fgcolor, 0, Qt::DashLine);
433  painter->setPen(penForeground);
434  painter->setBrush(Qt::NoBrush);
435  painter->drawRect(rtAdjusted);
436 
437  painter->setPen(Qt::NoPen);
438  QBrush brushEllipse(fgcolor);
439  painter->setBrush(fgcolor);
440 
441  double w = 2.0;
442  double h = 2.0;
443  double half = 1.0;
444 
445  painter->drawRect(rtAdjusted.center().x() - half, rtAdjusted.center().y() - half, w, h); // center
446  painter->drawRect(rtAdjusted.bottomLeft().x() - half, rtAdjusted.bottomLeft().y() - half, w, h); // left-top
447  painter->drawRect(rtAdjusted.bottomRight().x() - half, rtAdjusted.bottomRight().y() - half, w, h); // right-top
448  painter->drawRect(rtAdjusted.topLeft().x() - half, rtAdjusted.topLeft().y() - half, w, h); // left-bottom
449  painter->drawRect(rtAdjusted.topRight().x() - half, rtAdjusted.topRight().y() - half, w, h); // right-bottom
450 
451  painter->restore();
452  }
453 
454  template <class T>
455  inline void te::layout::ParentItem<T>::drawBorder( QPainter * painter )
456  {
457  if ( !painter )
458  {
459  return;
460  }
461 
462  if(!m_model)
463  return;
464 
465  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
466  if(!model)
467  return;
468 
469  if(!model->isBorder())
470  return;
471 
472  te::color::RGBAColor clrBack = model->getBorderColor();
473  QColor borderColor;
474  borderColor.setRed(clrBack.getRed());
475  borderColor.setGreen(clrBack.getGreen());
476  borderColor.setBlue(clrBack.getBlue());
477  borderColor.setAlpha(clrBack.getAlpha());
478 
479  painter->save();
480  QPen penBackground(borderColor, 0, Qt::SolidLine);
481  painter->setPen(penBackground);
482  painter->setBrush(Qt::NoBrush);
483  painter->setRenderHint( QPainter::Antialiasing, true );
484  painter->drawRect(QRectF( 0, 0, boundingRect().width(), boundingRect().height()));
485  painter->restore();
486  }
487 
488  template <class T>
490  {
491  return m_rect;
492  }
493 
494  template <class T>
495  inline void te::layout::ParentItem<T>::setRect( QRectF rect )
496  {
497  if (rect.isEmpty() && !rect.isNull())
498  return;
499 
500  m_rect = rect;
501  T::update(rect);
502  }
503 
504  template <class T>
505  inline void te::layout::ParentItem<T>::mousePressEvent( QGraphicsSceneMouseEvent * event )
506  {
507  QGraphicsItem::mousePressEvent(event);
508 
509  if(event->modifiers() == Qt::AltModifier && m_toResizeItem && m_model->isResizable())
510  {
511  m_clonePixmap = getPixmap();
512  createResizePixmap();
513  m_mousePressedAlt = true;
514  m_initialCoord = event->scenePos();
515  }
516  }
517 
518  template <class T>
519  inline void te::layout::ParentItem<T>::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
520  {
521  QGraphicsItem::mouseReleaseEvent(event);
522 
523  m_finalCoord = event->scenePos();
524 
525  te::gm::Envelope boxScene = createNewBoxInCoordScene(event->scenePos().x(), event->scenePos().y());
526  if(boxScene.isValid() && boxScene.getWidth() > 0 && boxScene.getHeight() > 0)
527  m_controller->setBox(boxScene);
528 
529  m_mousePressedAlt = false;
530 
531  if(m_model->isResizable() && m_toResizeItem && boxScene.isValid())
532  {
533  m_clonePixmap = QPixmap();
534  m_toResizeItem = false;
535  m_resizeMode = false;
536  redraw();
537  T::setOpacity(1.);
538  }
539 
540  refresh();
541  }
542 
543  template <class T>
544  inline void te::layout::ParentItem<T>::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
545  {
546  if(event->modifiers() == Qt::AltModifier && event->buttons() == Qt::LeftButton && m_toResizeItem && m_model->isResizable())
547  {
548  m_mousePressedAlt = true;
549  T::setOpacity(0.5);
550 
551  m_finalCoord = event->scenePos();
552 
553  QPixmap pix = calculateNewPixmap(event->scenePos().x(), event->scenePos().y());
554  setPixmap(pix);
555  T::update();
556  }
557  else
558  {
559  if(!m_toResizeItem)
560  T::setOpacity(1.);
561  m_mousePressedAlt = false;
562  QGraphicsItem::mouseMoveEvent(event);
563  }
564  }
565 
566  template <class T>
567  inline void te::layout::ParentItem<T>::hoverEnterEvent( QGraphicsSceneHoverEvent * event )
568  {
569  QGraphicsItem::hoverEnterEvent(event);
570  }
571 
572  template <class T>
573  inline void te::layout::ParentItem<T>::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
574  {
575  T::setCursor(Qt::ArrowCursor);
576  QGraphicsItem::hoverLeaveEvent(event);
577  }
578 
579  template <class T>
580  inline void te::layout::ParentItem<T>::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
581  {
582  if(m_model->isResizable())
583  {
584  m_toResizeItem = checkTouchesCorner(event->pos().x(), event->pos().y());
585  }
586  QGraphicsItem::hoverMoveEvent(event);
587  }
588 
589  template <class T>
590  inline bool te::layout::ParentItem<T>::checkTouchesCorner( const double& x, const double& y )
591  {
592  bool result = true;
593  QRectF bRect = boundingRect();
594  double margin = 2.; //precision
595 
596  QPointF ll = bRect.bottomLeft();
597  QPointF lr = bRect.bottomRight();
598  QPointF tl = bRect.topLeft();
599  QPointF tr = bRect.topRight();
600 
601  if((x >= (ll.x() - margin) && x <= (ll.x() + margin))
602  && (y >= (ll.y() - margin) && y <= (ll.y() + margin)))
603  {
604  T::setCursor(Qt::SizeFDiagCursor);
605  m_enumSides = TPLowerLeft;
606  }
607  else if((x >= (lr.x() - margin) && x <= (lr.x() + margin))
608  && (y >= (lr.y() - margin) && y <= (lr.y() + margin)))
609  {
610  T::setCursor(Qt::SizeBDiagCursor);
611  m_enumSides = TPLowerRight;
612  }
613  else if((x >= (tl.x() - margin) && x <= (tl.x() + margin))
614  && (y >= (tl.y() - margin) && y <= (tl.y() + margin)))
615  {
616  T::setCursor(Qt::SizeBDiagCursor);
617  m_enumSides = TPTopLeft;
618  }
619  else if((x >= (tr.x() - margin) && x <= (tr.x() + margin))
620  && (y >= (tr.y() - margin) && y <= (tr.y() + margin)))
621  {
622  T::setCursor(Qt::SizeFDiagCursor);
623  m_enumSides = TPTopRight;
624  }
625  else
626  {
627  T::setCursor(Qt::ArrowCursor);
628  m_enumSides = TPNoneSide;
629  result = false;
630  }
631 
632  return result;
633  }
634 
635  template <class T>
636  inline QPixmap te::layout::ParentItem<T>::calculateNewPixmap( const double& x, const double& y )
637  {
638  te::gm::Envelope boxScene = createNewBoxInCoordScene(x, y);
639  QPixmap pix = m_clonePixmap.scaled(boxScene.getWidth(), boxScene.getHeight());
640 
641  if(pix.isNull())
642  pix = m_clonePixmap;
643 
644  return pix;
645  }
646 
647  template <class T>
649  {
650  QPointF pbxy1 = T::mapToScene(boundingRect().bottomLeft());
651  QPointF pbxy2 = T::mapToScene(boundingRect().topRight());
652 
653  double dx = 0;
654  double dy = 0;
655 
656  if(m_mousePressedAlt && m_toResizeItem)
657  {
658  dx = m_finalCoord.x() - m_initialCoord.x();
659  dy = m_finalCoord.y() - m_initialCoord.y();
660 
661  switch(m_enumSides)
662  {
663  case TPLowerRight :
664  {
665  if(m_finalCoord.x() > pbxy1.x() && m_finalCoord.y() > pbxy2.y())
666  {
667  m_boxCopy = te::gm::Envelope(m_model->getBox().m_llx,
668  m_model->getBox().m_lly, m_model->getBox().m_urx + dx, m_model->getBox().m_ury + dy);
669 
670  //In Parent Coordinates
671  T::setPos(QPointF(m_model->getBox().m_llx, m_model->getBox().m_lly));
672  }
673  break;
674  }
675  case TPLowerLeft:
676  {
677  if(m_finalCoord.x() < pbxy2.x() && m_finalCoord.y() > pbxy2.y())
678  {
679  m_boxCopy = te::gm::Envelope(m_model->getBox().m_llx + dx,
680  m_model->getBox().m_lly - dy, m_model->getBox().m_urx, m_model->getBox().m_ury);
681 
682  //In Parent Coordinates
683  T::setPos(QPointF(m_finalCoord.x(), m_model->getBox().m_lly));
684  }
685  break;
686  }
687  case TPTopRight:
688  {
689  if(m_finalCoord.x() > pbxy1.x() && m_finalCoord.y() < pbxy1.y())
690  {
691  m_boxCopy = te::gm::Envelope(m_model->getBox().m_llx,
692  m_model->getBox().m_lly, m_model->getBox().m_urx + dx, m_model->getBox().m_ury - dy);
693 
694  //In Parent Coordinates
695  T::setPos(QPointF(m_model->getBox().m_llx, m_finalCoord.y()));
696  }
697  break;
698  }
699  case TPTopLeft :
700  {
701  if(m_finalCoord.x() < pbxy2.x() && m_finalCoord.y() < pbxy1.y())
702  {
703  m_boxCopy = te::gm::Envelope(m_model->getBox().m_llx + dx,
704  m_model->getBox().m_lly + dy, m_model->getBox().m_urx, m_model->getBox().m_ury);
705 
706  //In Parent Coordinates
707  T::setPos(QPointF(m_finalCoord.x(), m_finalCoord.y()));
708  }
709  break;
710  }
711  default :
712  {
713  m_boxCopy = m_model->getBox();
714  break;
715  }
716  }
717  }
718 
719  return m_boxCopy;
720  }
721 
722  template <class T>
723  inline bool te::layout::ParentItem<T>::contains( const QPointF &point ) const
724  {
725  te::gm::Coord2D coord(point.x(), point.y());
726  return m_controller->contains(coord);
727  }
728 
729  template <class T>
731  {
732  return m_pixmap;
733  }
734 
735  template <class T>
737  {
738  QGraphicsItem::setZValue(z);
739  setZValueItem(z);
740  }
741 
742  template <class T>
744  {
745  return QGraphicsItem::zValue();
746  }
747 
748  template <class T>
750  {
751  if(!m_model)
752  return;
753 
754  ItemModelObservable* model = dynamic_cast<ItemModelObservable*>(m_model);
755  if(!model)
756  return;
757 
758  double angle = model->getAngle();
759 
760  if(angle == model->getOldAngle())
761  return;
762 
763  double w = boundingRect().width();
764  double h = boundingRect().height();
765 
766  QTransform transf = T::transform();
767 
768  if(m_invertedMatrix)
769  {
770  angle = -angle;
771  }
772 
773  transf.translate(w/2, h/2);
774  T::setTransform(transf);
775  T::setRotation(angle);
776  transf.translate(-(w/2), -(h/2));
777  T::setTransform(transf);
778  }
779 
780  template <class T>
782  {
783  w = 0;
784  h = 0;
785  te::color::RGBAColor** teImg = 0;
786 
787  QRectF rect = boundingRect();
788  QStyleOptionGraphicsItem opt;
789  QPixmap pix(rect.width(), rect.height());
790  QPainter p(&pix);
791  this->paint(&p, &opt, 0);
792  QImage img = pix.toImage();
793 
794  if(img.isNull())
795  {
796  return teImg;
797  }
798 
799  w = pix.width();
800  h = pix.height();
801 
802  teImg = te::qt::widgets::GetImage(&img);
803  return teImg;
804  }
805 
806  template <class T>
808  {
809  QRectF rect = boundingRect();
810  QStyleOptionGraphicsItem opt;
811  QPixmap pix(rect.width(), rect.height());
812  QPainter p(&pix);
813  this->paint(&p, &opt, 0);
814 
815  return pix.toImage();
816  }
817 
818  template <class T>
819  inline QVariant te::layout::ParentItem<T>::itemChange( QGraphicsItem::GraphicsItemChange change, const QVariant & value )
820  {
821  if(change == QGraphicsItem::ItemPositionHasChanged)
822  {
823  refresh();
824  }
825 
826  if(change == QGraphicsItem::ItemChildAddedChange)
827  {
828  QGraphicsItem* item = value.value<QGraphicsItem*>();
829  if(item)
830  {
831  ItemObserver* iOb = dynamic_cast<ItemObserver*>(item);
832  if(iOb)
833  {
834  ItemModelObservable* itemModel = dynamic_cast<ItemModelObservable*>(m_model);
835  if(itemModel)
836  {
837  itemModel->addChildren(iOb);
838  }
839  }
840  }
841  }
842 
843  if(change == QGraphicsItem::ItemChildRemovedChange)
844  {
845  QGraphicsItem* item = value.value<QGraphicsItem*>();
846  if(item)
847  {
848  ItemObserver* iOb = dynamic_cast<ItemObserver*>(item);
849  if(iOb)
850  {
851  ItemModelObservable* itemModel = dynamic_cast<ItemModelObservable*>(m_model);
852  if(itemModel)
853  {
854  itemModel->removeChildren(iOb->getModel()->getHashCode());
855  }
856  }
857  }
858  }
859 
860  return QGraphicsItem::itemChange(change, value);
861  }
862 
863  template <class T>
865  {
866  QStyleOptionGraphicsItem opt;
867  m_clonePixmap = QPixmap(m_rect.width(), m_rect.height());
868  QPainter p(&m_clonePixmap);
869  this->paint(&p, &opt, 0);
870 
871  if(!m_clonePixmap.isNull())
872  {
873  m_resizeMode = true;
874  }
875  }
876  }
877 }
878 
879 #endif
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
te::gm::Coord2D getPos()
virtual void updateObserver(te::layout::ContextItem context)
Reimplemented from ItemObserver.
Definition: ParentItem.h:269
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Reimplemented from QGraphicsItem.
Definition: ParentItem.h:519
double y
y-coordinate.
Definition: Coord2D.h:114
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event)
Reimplemented from QGraphicsItem.
Definition: ParentItem.h:580
virtual void setPixmap(const QPixmap &pixmap)
Definition: ParentItem.h:357
te::layout::Observable * m_model
"Model" part of MVC component.
Definition: ItemObserver.h:198
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
#define TELAYOUTEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:99
virtual te::gm::Coord2D getPosition()
Reimplemented from ItemObserver.
Definition: ParentItem.h:343
double x
x-coordinate.
Definition: Coord2D.h:113
bool m_resizeMode
pixmap to perform the resize is already built
Definition: ParentItem.h:230
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Reimplemented from QGraphicsItem.
Definition: ParentItem.h:544
QIcon getImage(int type)
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Reimplemented from QGraphicsItem.
Definition: ParentItem.h:567
virtual te::color::RGBAColor ** getRGBAColorImage(int &w, int &h)
Reimplemented from ItemObserver.
Definition: ParentItem.h:781
virtual void applyRotation()
Reimplemented from ItemObserver.
Definition: ParentItem.h:749
virtual te::gm::Envelope getBox()=0
Returns the bounding rectangle of the component in scene coordinates(millimeters). Starting point is llx, lly. Reimplement this function in a Observable subclass to provide the model's getBox implementation.
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
virtual void drawText(QPointF point, QPainter *painter, std::string text)
Draw a text. Converts boundingRect item coordinates (local coordinates) in pixel coordinates (screen ...
Definition: ParentItem.h:321
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
virtual void drawBorder(QPainter *painter)
Definition: ParentItem.h:455
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
virtual bool contains(const QPointF &point) const
Definition: ParentItem.h:723
bool m_invertedMatrix
true if inverted, false otherwise the matrix scene
Definition: ItemObserver.h:202
virtual QPixmap calculateNewPixmap(const double &x, const double &y)
Definition: ParentItem.h:636
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
virtual QRectF boundingRect() const
Definition: ParentItem.h:489
Abstract class to represent a controller. "Controller" part of MVC component. All classes representin...
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
Reimplemented from QGraphicsItem.
Definition: ParentItem.h:505
bool m_mousePressedAlt
mouse and active alt key
Definition: ParentItem.h:225
te::gm::Envelope m_boxCopy
box with resize
Definition: ParentItem.h:223
virtual void drawSelection(QPainter *painter)
Definition: ParentItem.h:410
virtual ~ParentItem()
Destructor.
Definition: ParentItem.h:263
virtual double getAngle()
Returns the value of rotation.
virtual int getZValueItem()
Reimplemented from ItemObserver.
Definition: ParentItem.h:743
bool m_toResizeItem
pixmap to perform the resize is not yet built
Definition: ParentItem.h:228
virtual te::color::RGBAColor getBorderColor()
Returns the border color of the MVC component.
static Context & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
virtual void setRect(QRectF rect)
Definition: ParentItem.h:495
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
Definition: RGBAColor.h:310
virtual int getHashCode()=0
Returns the hashcode of a MVC component. Reimplement this function in a Observable subclass to provid...
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Reimplemented from QGraphicsItem.
Definition: ParentItem.h:291
virtual te::color::RGBAColor getBackgroundColor()
Returns the background color of the MVC component.
virtual bool isBorder()
Returns whether the border should be drawn or not.
virtual bool addChildren(ItemObserver *item)
Reimplemented from Observable.
LayoutAlign m_enumSides
Definition: ParentItem.h:229
TEQTWIDGETSEXPORT QImage * GetImage(te::color::RGBAColor **img, int width, int height)
It creates a QImage from an RGBA color array.
Definition: Utils.cpp:69
void setZValue(qreal z)
The Z value decides the stacking order of drawing.
Definition: ParentItem.h:736
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual Observable * getModel()
Returns the "Model" part of the MVC.
ParentItem(ItemController *controller=0, Observable *o=0, bool inverted=false)
Constructor.
Definition: ParentItem.h:234
virtual void createResizePixmap()
Definition: ParentItem.h:864
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Reimplemented from QGraphicsItem.
Definition: ParentItem.h:573
double getZoomFactor()
Returns current zoom factor. Ex.: 0.5 (50%)
Definition: Context.cpp:103
virtual QImage getImage()
Reimplemented from ItemObserver.
Definition: ParentItem.h:807
virtual bool checkTouchesCorner(const double &x, const double &y)
Definition: ParentItem.h:590
virtual double getOldAngle()
Returns the value of old rotation.
Abstract class that represents a graphic item. Its coordinate system is the same of scene (millimeter...
Definition: ParentItem.h:86
LayoutAlign
Enum TdkAbstractComponentType. This is the enumeration of the components types.
Definition: AbstractType.h:92
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 QPixmap getPixmap()
Definition: ParentItem.h:730
virtual te::gm::Envelope createNewBoxInCoordScene(const double &x, const double &y)
Definition: ParentItem.h:648
virtual bool removeChildren(int hashCode)
Reimplemented from Observable.
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
Reimplemented from QGraphicsItem.
Definition: ParentItem.h:819
virtual void drawBackground(QPainter *painter)
Definition: ParentItem.h:381