From dec5d3002cfc81b7b772c2ad3fc283a1b8b5d91a Mon Sep 17 00:00:00 2001 From: blacksun Date: Mon, 24 Nov 2014 17:36:02 +0000 Subject: [PATCH] QetShapeItem : improve mouse interaction git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3512 bfdf4180-ca20-0410-9c96-a3a8aa849046 --- sources/qetgraphicsitem/qetshapeitem.cpp | 73 +++++++++++++++++------- sources/qetgraphicsitem/qetshapeitem.h | 3 + 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index 0744b9113..134d8bfa5 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -35,16 +35,17 @@ QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem m_shapeType(type), m_shapeStyle(Qt::DashLine), m_P1 (Diagram::snapToGrid(p1)), - m_P2 (Diagram::snapToGrid(p2)) + m_P2 (Diagram::snapToGrid(p2)), + m_hovered(false) { if (type == Polyline) m_polygon << m_P1 << m_P2; setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); + setAcceptHoverEvents(true); } QetShapeItem::~QetShapeItem() -{ -} +{} /** * @brief QetShapeItem::setStyle @@ -108,11 +109,7 @@ void QetShapeItem::setNextPoint(QPointF P) { * @return the bounding rect of this item */ QRectF QetShapeItem::boundingRect() const { - if (m_shapeType == Polyline) - return ( shape().boundingRect()); - - QRectF b(m_P1, m_P2); - return b.normalized(); + return shape().boundingRect(); } /** @@ -121,20 +118,17 @@ QRectF QetShapeItem::boundingRect() const { */ QPainterPath QetShapeItem::shape() const { QPainterPath path; - QPainterPathStroker pps; switch (m_shapeType) { case Line: path.moveTo(m_P1); path.lineTo(m_P2); - pps.setWidth(10); - path = pps.createStroke(path); break; case Rectangle: - path.addRect(boundingRect()); + path.addRect(QRectF(m_P1, m_P2)); break; case Ellipse: - path.addEllipse(boundingRect()); + path.addEllipse(QRectF(m_P1, m_P2)); break; case Polyline: path.addPolygon(m_polygon); @@ -144,7 +138,11 @@ QPainterPath QetShapeItem::shape() const { break; } - return path; + QPainterPathStroker pps; + pps.setWidth(10); + pps.setJoinStyle(Qt::RoundJoin); + + return (pps.createStroke(path)); } /** @@ -170,24 +168,33 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti { Q_UNUSED(option); Q_UNUSED(widget); - QPen pen; - pen.setStyle(m_shapeStyle); - if (isSelected()) pen.setColor(Qt::red); - painter -> setRenderHint(QPainter::Antialiasing, false); + QPen pen(m_shapeStyle); pen.setWidthF(1); - painter->setPen(pen); + if (m_hovered) { + painter->save(); + QColor color(Qt::darkBlue); + color.setAlpha(25); + painter -> setBrush (QBrush (color)); + painter -> setPen (Qt::NoPen); + painter -> drawPath (shape()); + painter -> restore (); + } + else if (isSelected()) { + pen.setColor(Qt::red); + } + painter -> setPen(pen); switch (m_shapeType) { case Line: painter->drawLine(QLineF(m_P1, m_P2)); break; case Rectangle: - painter->drawRect(boundingRect()); + painter->drawRect(QRectF(m_P1, m_P2)); break; case Ellipse: - painter->drawEllipse(boundingRect()); + painter->drawEllipse(QRectF(m_P1, m_P2)); break; case Polyline: painter->drawPolyline(m_polygon); @@ -195,6 +202,30 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti } } +/** + * @brief QetShapeItem::hoverEnterEvent + * Handle hover enter event + * @param event + */ +void QetShapeItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { + Q_UNUSED(event); + + m_hovered = true; + update(); +} + +/** + * @brief QetShapeItem::hoverLeaveEvent + * Handle hover leave event + * @param event + */ +void QetShapeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { + Q_UNUSED(event); + + m_hovered = false; + update(); +} + /** * @brief QetShapeItem::fromXml * Build this item from the xml description diff --git a/sources/qetgraphicsitem/qetshapeitem.h b/sources/qetgraphicsitem/qetshapeitem.h index 68577c8ba..8fbc6d6ef 100644 --- a/sources/qetgraphicsitem/qetshapeitem.h +++ b/sources/qetgraphicsitem/qetshapeitem.h @@ -68,6 +68,8 @@ class QetShapeItem : public QetGraphicsItem protected: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + virtual void hoverEnterEvent (QGraphicsSceneHoverEvent *event); + virtual void hoverLeaveEvent (QGraphicsSceneHoverEvent *event); private: void changeGraphicsItem (const ShapeType &newtype); @@ -81,5 +83,6 @@ class QetShapeItem : public QetGraphicsItem Qt::PenStyle m_shapeStyle; QPointF m_P1, m_P2; QPolygonF m_polygon; + bool m_hovered; }; #endif // QETSHAPEITEM_H