34 #ifndef __TERRALIB_LAYOUT_INTERNAL_PARENT_ITEM_H
35 #define __TERRALIB_LAYOUT_INTERNAL_PARENT_ITEM_H
41 #include <QGraphicsSceneHoverEvent>
42 #include <QGraphicsSceneMouseEvent>
43 #include <QStyleOptionGraphicsItem>
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"
111 virtual void paint ( QPainter * painter,
const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
118 virtual void setPixmap(
const QPixmap& pixmap );
120 virtual QPixmap getPixmap();
122 virtual QRectF boundingRect()
const;
124 virtual void setRect(QRectF rect);
128 virtual bool contains(
const QPointF &point)
const;
135 void setZValue ( qreal z );
149 virtual void drawBackground( QPainter* painter );
151 virtual void drawSelection(QPainter* painter);
153 virtual void drawBorder(QPainter* painter);
158 virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
163 virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
168 virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
173 virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
178 virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event );
183 virtual void hoverMoveEvent ( QGraphicsSceneHoverEvent * event );
188 virtual QVariant itemChange ( QGraphicsItem::GraphicsItemChange change,
const QVariant & value );
190 virtual bool checkTouchesCorner(
const double& x,
const double& y);
192 virtual QPixmap calculateNewPixmap(
const double& x,
const double& y);
194 virtual te::gm::Envelope createNewBoxInCoordScene(
const double& x,
const double& y);
199 virtual int getZValueItem();
204 virtual void applyRotation();
213 virtual void drawText(QPointF point, QPainter* painter, std::string text);
215 virtual void createResizePixmap();
237 m_mousePressedAlt(false),
238 m_toResizeItem(false),
245 QGraphicsItem* item =
this;
248 this->setFlags(QGraphicsItem::ItemIsMovable
249 | QGraphicsItem::ItemIsSelectable
250 | QGraphicsItem::ItemSendsGeometryChanges
251 | QGraphicsItem::ItemIsFocusable);
254 QGraphicsItem::setAcceptHoverEvents(
true);
273 if(m_rect.width() != m_model->getBox().getWidth() ||
274 m_rect.height() != m_model->getBox().getHeight())
276 setRect(QRectF(0, 0, m_model->getBox().getWidth(), m_model->getBox().getHeight()));
284 QGraphicsItem::setPos(x, y);
287 QGraphicsItem::update();
295 if ( !painter || !m_toResizeItem )
300 drawBackground( painter );
303 boundRect = boundingRect();
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);
314 if (option->state & QStyle::State_Selected)
316 drawSelection(painter);
325 QTransform t = painter->transform();
326 QPointF p = t.map(point);
330 QFont ft = painter->font();
331 ft.setPointSize(ft.pointSize() * zoomFactor);
332 painter->setFont(ft);
335 painter->setMatrixEnabled(
false);
336 painter->drawText(p, text.c_str());
337 painter->setMatrixEnabled(
true);
345 QPointF posF = QGraphicsItem::scenePos();
346 qreal valuex = posF.x();
347 qreal valuey = posF.y();
350 coordinate.
x = valuex;
351 coordinate.
y = valuey;
366 QImage img = m_pixmap.toImage();
367 QImage image = img.mirrored(
false,
true);
368 m_pixmap = QPixmap::fromImage(image);
372 if(m_mousePressedAlt)
375 QGraphicsItem::prepareGeometryChange();
377 QGraphicsItem::update();
395 backColor.setRed(clrBack.
getRed());
396 backColor.setGreen(clrBack.
getGreen());
397 backColor.setBlue(clrBack.
getBlue());
398 backColor.setAlpha(clrBack.
getAlpha());
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()));
419 qreal penWidth = painter->pen().widthF();
421 const qreal adj = penWidth / 2;
422 const QColor fgcolor(0,255,0);
423 const QColor backgroundColor(0,0,0);
425 QRectF rtAdjusted = boundingRect().adjusted(adj, adj, -adj, -adj);
428 painter->setPen(penBackground);
429 painter->setBrush(Qt::NoBrush);
430 painter->drawRect(rtAdjusted);
433 painter->setPen(penForeground);
434 painter->setBrush(Qt::NoBrush);
435 painter->drawRect(rtAdjusted);
437 painter->setPen(Qt::NoPen);
438 QBrush brushEllipse(fgcolor);
439 painter->setBrush(fgcolor);
445 painter->drawRect(rtAdjusted.center().x() - half, rtAdjusted.center().y() - half, w, h);
446 painter->drawRect(rtAdjusted.bottomLeft().x() - half, rtAdjusted.bottomLeft().y() - half, w, h);
447 painter->drawRect(rtAdjusted.bottomRight().x() - half, rtAdjusted.bottomRight().y() - half, w, h);
448 painter->drawRect(rtAdjusted.topLeft().x() - half, rtAdjusted.topLeft().y() - half, w, h);
449 painter->drawRect(rtAdjusted.topRight().x() - half, rtAdjusted.topRight().y() - half, w, h);
474 borderColor.setRed(clrBack.
getRed());
475 borderColor.setGreen(clrBack.
getGreen());
476 borderColor.setBlue(clrBack.
getBlue());
477 borderColor.setAlpha(clrBack.
getAlpha());
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()));
497 if (rect.isEmpty() && !rect.isNull())
507 QGraphicsItem::mousePressEvent(event);
509 if(event->modifiers() == Qt::AltModifier && m_toResizeItem && m_model->isResizable())
511 m_clonePixmap = getPixmap();
512 createResizePixmap();
513 m_mousePressedAlt =
true;
514 m_initialCoord =
event->scenePos();
521 QGraphicsItem::mouseReleaseEvent(event);
523 m_finalCoord =
event->scenePos();
525 te::gm::Envelope boxScene = createNewBoxInCoordScene(event->scenePos().x(),
event->scenePos().y());
527 m_controller->setBox(boxScene);
529 m_mousePressedAlt =
false;
531 if(m_model->isResizable() && m_toResizeItem && boxScene.
isValid())
533 m_clonePixmap = QPixmap();
534 m_toResizeItem =
false;
535 m_resizeMode =
false;
546 if(event->modifiers() == Qt::AltModifier &&
event->buttons() == Qt::LeftButton && m_toResizeItem && m_model->isResizable())
548 m_mousePressedAlt =
true;
551 m_finalCoord =
event->scenePos();
553 QPixmap pix = calculateNewPixmap(event->scenePos().x(),
event->scenePos().y());
561 m_mousePressedAlt =
false;
562 QGraphicsItem::mouseMoveEvent(event);
569 QGraphicsItem::hoverEnterEvent(event);
575 T::setCursor(Qt::ArrowCursor);
576 QGraphicsItem::hoverLeaveEvent(event);
582 if(m_model->isResizable())
584 m_toResizeItem = checkTouchesCorner(event->pos().x(),
event->pos().y());
586 QGraphicsItem::hoverMoveEvent(event);
593 QRectF bRect = boundingRect();
596 QPointF ll = bRect.bottomLeft();
597 QPointF lr = bRect.bottomRight();
598 QPointF tl = bRect.topLeft();
599 QPointF tr = bRect.topRight();
601 if((x >= (ll.x() - margin) && x <= (ll.x() + margin))
602 && (y >= (ll.y() - margin) && y <= (ll.y() + margin)))
604 T::setCursor(Qt::SizeFDiagCursor);
607 else if((x >= (lr.x() - margin) && x <= (lr.x() + margin))
608 && (y >= (lr.y() - margin) && y <= (lr.y() + margin)))
610 T::setCursor(Qt::SizeBDiagCursor);
613 else if((x >= (tl.x() - margin) && x <= (tl.x() + margin))
614 && (y >= (tl.y() - margin) && y <= (tl.y() + margin)))
616 T::setCursor(Qt::SizeBDiagCursor);
619 else if((x >= (tr.x() - margin) && x <= (tr.x() + margin))
620 && (y >= (tr.y() - margin) && y <= (tr.y() + margin)))
622 T::setCursor(Qt::SizeFDiagCursor);
627 T::setCursor(Qt::ArrowCursor);
650 QPointF pbxy1 = T::mapToScene(boundingRect().bottomLeft());
651 QPointF pbxy2 = T::mapToScene(boundingRect().topRight());
656 if(m_mousePressedAlt && m_toResizeItem)
658 dx = m_finalCoord.x() - m_initialCoord.x();
659 dy = m_finalCoord.y() - m_initialCoord.y();
665 if(m_finalCoord.x() > pbxy1.x() && m_finalCoord.y() > pbxy2.y())
668 m_model->getBox().m_lly, m_model->getBox().m_urx + dx, m_model->getBox().m_ury + dy);
671 T::setPos(QPointF(m_model->getBox().m_llx, m_model->getBox().m_lly));
677 if(m_finalCoord.x() < pbxy2.x() && m_finalCoord.y() > pbxy2.y())
680 m_model->getBox().m_lly - dy, m_model->getBox().m_urx, m_model->getBox().m_ury);
683 T::setPos(QPointF(m_finalCoord.x(), m_model->getBox().m_lly));
689 if(m_finalCoord.x() > pbxy1.x() && m_finalCoord.y() < pbxy1.y())
692 m_model->getBox().m_lly, m_model->getBox().m_urx + dx, m_model->getBox().m_ury - dy);
695 T::setPos(QPointF(m_model->getBox().m_llx, m_finalCoord.y()));
701 if(m_finalCoord.x() < pbxy2.x() && m_finalCoord.y() < pbxy1.y())
704 m_model->getBox().m_lly + dy, m_model->getBox().m_urx, m_model->getBox().m_ury);
707 T::setPos(QPointF(m_finalCoord.x(), m_finalCoord.y()));
713 m_boxCopy = m_model->getBox();
726 return m_controller->contains(coord);
738 QGraphicsItem::setZValue(z);
745 return QGraphicsItem::zValue();
763 double w = boundingRect().width();
764 double h = boundingRect().height();
766 QTransform transf = T::transform();
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);
787 QRectF rect = boundingRect();
788 QStyleOptionGraphicsItem opt;
789 QPixmap pix(rect.width(), rect.height());
791 this->paint(&p, &opt, 0);
792 QImage img = pix.toImage();
809 QRectF rect = boundingRect();
810 QStyleOptionGraphicsItem opt;
811 QPixmap pix(rect.width(), rect.height());
813 this->paint(&p, &opt, 0);
815 return pix.toImage();
821 if(change == QGraphicsItem::ItemPositionHasChanged)
826 if(change == QGraphicsItem::ItemChildAddedChange)
828 QGraphicsItem* item = value.value<QGraphicsItem*>();
843 if(change == QGraphicsItem::ItemChildRemovedChange)
845 QGraphicsItem* item = value.value<QGraphicsItem*>();
860 return QGraphicsItem::itemChange(change, value);
866 QStyleOptionGraphicsItem opt;
867 m_clonePixmap = QPixmap(m_rect.width(), m_rect.height());
868 QPainter p(&m_clonePixmap);
869 this->paint(&p, &opt, 0);
871 if(!m_clonePixmap.isNull())
Abstract class to represent an observable. "Model" part of MVC component.
Class responsible for maintaining the drawing context of a MVC component. It is always used by the "M...
virtual void updateObserver(te::layout::ContextItem context)
Reimplemented from ItemObserver.
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Reimplemented from QGraphicsItem.
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event)
Reimplemented from QGraphicsItem.
virtual void setPixmap(const QPixmap &pixmap)
te::layout::Observable * m_model
"Model" part of MVC component.
int getRed() const
It returns the red component color value (a value from 0 to 255).
#define TELAYOUTEXPORT
You can use this macro in order to export/import classes and functions from this module.
virtual te::gm::Coord2D getPosition()
Reimplemented from ItemObserver.
bool m_resizeMode
pixmap to perform the resize is already built
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Reimplemented from QGraphicsItem.
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Reimplemented from QGraphicsItem.
virtual te::color::RGBAColor ** getRGBAColorImage(int &w, int &h)
Reimplemented from ItemObserver.
virtual void applyRotation()
Reimplemented from ItemObserver.
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).
virtual void drawText(QPointF point, QPainter *painter, std::string text)
Draw a text. Converts boundingRect item coordinates (local coordinates) in pixel coordinates (screen ...
int getGreen() const
It returns the green component color value (a value from 0 to 255).
virtual void drawBorder(QPainter *painter)
Abstract class to represent an observable. "Model" part of MVC component. All classes representing th...
double getWidth() const
It returns the envelope width.
virtual bool contains(const QPointF &point) const
bool m_invertedMatrix
true if inverted, false otherwise the matrix scene
virtual QPixmap calculateNewPixmap(const double &x, const double &y)
Abstract class to represent an observer. "View" part of MVC component. All classes representing the g...
An utility struct for representing 2D coordinates.
virtual QRectF boundingRect() const
Abstract class to represent a controller. "Controller" part of MVC component. All classes representin...
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
Reimplemented from QGraphicsItem.
bool m_mousePressedAlt
mouse and active alt key
te::gm::Envelope m_boxCopy
box with resize
virtual void drawSelection(QPainter *painter)
virtual ~ParentItem()
Destructor.
virtual double getAngle()
Returns the value of rotation.
virtual int getZValueItem()
Reimplemented from ItemObserver.
bool m_toResizeItem
pixmap to perform the resize is not yet built
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.
virtual void setRect(QRectF rect)
int getAlpha() const
It returns the alpha component color value (a value from 0 to 255).
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.
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.
void setZValue(qreal z)
The Z value decides the stacking order of drawing.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
virtual Observable * getModel()
Returns the "Model" part of the MVC.
ParentItem(ItemController *controller=0, Observable *o=0, bool inverted=false)
Constructor.
virtual void createResizePixmap()
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Reimplemented from QGraphicsItem.
double getZoomFactor()
Returns current zoom factor. Ex.: 0.5 (50%)
virtual QImage getImage()
Reimplemented from ItemObserver.
virtual bool checkTouchesCorner(const double &x, const double &y)
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...
LayoutAlign
Enum TdkAbstractComponentType. This is the enumeration of the components types.
double getHeight() const
It returns the envelope height.
bool isValid() const
It tells if the rectangle is valid or not.
virtual QPixmap getPixmap()
virtual te::gm::Envelope createNewBoxInCoordScene(const double &x, const double &y)
virtual bool removeChildren(int hashCode)
Reimplemented from Observable.
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
Reimplemented from QGraphicsItem.
virtual void drawBackground(QPainter *painter)