From 04009746d502eedbe292656b7a1fcde15063f282 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 28 Feb 2017 17:33:53 +0200 Subject: [PATCH] Improve user expirience. Added grainline hover. --HG-- branch : feature --- src/libs/vwidgets/vgrainlineitem.cpp | 95 +++++++++++++++++----------- src/libs/vwidgets/vgrainlineitem.h | 5 ++ src/libs/vwidgets/vpieceitem.cpp | 2 +- 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/src/libs/vwidgets/vgrainlineitem.cpp b/src/libs/vwidgets/vgrainlineitem.cpp index 30e6f6acc..c0299ddb1 100644 --- a/src/libs/vwidgets/vgrainlineitem.cpp +++ b/src/libs/vwidgets/vgrainlineitem.cpp @@ -68,8 +68,10 @@ VGrainlineItem::VGrainlineItem(QGraphicsItem* pParent) m_ptFinish(), m_ptCenter(), m_dAngle(0), - m_eArrowType(ArrowType::atBoth) + m_eArrowType(ArrowType::atBoth), + m_penWidth(LINE_PEN_WIDTH) { + setAcceptHoverEvents(true); m_inactiveZ = 5; Reset(); UpdateRectangle(); @@ -85,49 +87,16 @@ VGrainlineItem::~VGrainlineItem() //--------------------------------------------------------------------------------------------------------------------- QPainterPath VGrainlineItem::shape() const { - QPainterPath path; - if (m_eMode == mNormal) { - QPointF pt1; - QPointF pt2(pt1.x() + m_dLength * cos(m_dRotation), pt1.y() - m_dLength * sin(m_dRotation)); - - const QLineF mainLine = MainLine(); - QPainterPath linePath; - linePath.moveTo(mainLine.p1()); - linePath.lineTo(mainLine.p2()); - linePath.closeSubpath(); - - QPainterPathStroker stroker; - stroker.setWidth(LINE_PEN_WIDTH); - path.addPath((stroker.createStroke(linePath) + linePath).simplified()); - path.closeSubpath(); - - const qreal dArrLen = ARROW_LENGTH*GetScale(); - if (m_eArrowType != ArrowType::atRear) - { - // first arrow - QPainterPath polyPath; - polyPath.addPolygon(FirstArrow(dArrLen)); - path.addPath((stroker.createStroke(polyPath) + polyPath).simplified()); - path.closeSubpath(); - } - - if (m_eArrowType != ArrowType::atFront) - { - // second arrow - QPainterPath polyPath; - polyPath.addPolygon(SecondArrow(dArrLen)); - path.addPath((stroker.createStroke(polyPath) + polyPath).simplified()); - path.closeSubpath(); - } + return MainShape(); } else { + QPainterPath path; path.addPolygon(m_polyBound); + return path; } - - return path; } //--------------------------------------------------------------------------------------------------------------------- @@ -143,7 +112,7 @@ void VGrainlineItem::paint(QPainter* pP, const QStyleOptionGraphicsItem* pOption Q_UNUSED(pWidget) pP->save(); QColor clr = Qt::black; - pP->setPen(QPen(clr, LINE_PEN_WIDTH, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + pP->setPen(QPen(clr, m_penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); pP->setRenderHints(QPainter::Antialiasing); // line @@ -489,6 +458,20 @@ void VGrainlineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME) } } +//--------------------------------------------------------------------------------------------------------------------- +void VGrainlineItem::hoverEnterEvent(QGraphicsSceneHoverEvent *pME) +{ + m_penWidth = LINE_PEN_WIDTH + 1; + VPieceItem::hoverEnterEvent(pME); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VGrainlineItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *pME) +{ + m_penWidth = LINE_PEN_WIDTH; + VPieceItem::hoverLeaveEvent(pME); +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief VGrainlineItem::UpdateBox updates the item @@ -652,3 +635,39 @@ QPolygonF VGrainlineItem::SecondArrow(qreal dArrLen) const pt2.y() - dArrLen*sin(M_PI + m_dRotation - ARROW_ANGLE)); return poly; } + +//--------------------------------------------------------------------------------------------------------------------- +QPainterPath VGrainlineItem::MainShape() const +{ + QPainterPath path; + const QLineF mainLine = MainLine(); + QPainterPath linePath; + linePath.moveTo(mainLine.p1()); + linePath.lineTo(mainLine.p2()); + linePath.closeSubpath(); + + QPainterPathStroker stroker; + stroker.setWidth(m_penWidth); + path.addPath((stroker.createStroke(linePath) + linePath).simplified()); + path.closeSubpath(); + + const qreal dArrLen = ARROW_LENGTH*GetScale(); + if (m_eArrowType != ArrowType::atRear) + { + // first arrow + QPainterPath polyPath; + polyPath.addPolygon(FirstArrow(dArrLen)); + path.addPath((stroker.createStroke(polyPath) + polyPath).simplified()); + path.closeSubpath(); + } + + if (m_eArrowType != ArrowType::atFront) + { + // second arrow + QPainterPath polyPath; + polyPath.addPolygon(SecondArrow(dArrLen)); + path.addPath((stroker.createStroke(polyPath) + polyPath).simplified()); + path.closeSubpath(); + } + return path; +} diff --git a/src/libs/vwidgets/vgrainlineitem.h b/src/libs/vwidgets/vgrainlineitem.h index cac4e1327..0c59896c9 100644 --- a/src/libs/vwidgets/vgrainlineitem.h +++ b/src/libs/vwidgets/vgrainlineitem.h @@ -54,6 +54,8 @@ protected: virtual void mousePressEvent(QGraphicsSceneMouseEvent* pME) Q_DECL_OVERRIDE; virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* pME) Q_DECL_OVERRIDE; virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* pME) Q_DECL_OVERRIDE; + virtual void hoverEnterEvent(QGraphicsSceneHoverEvent* pME) Q_DECL_OVERRIDE; + virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent* pME) Q_DECL_OVERRIDE; virtual void Update() Q_DECL_OVERRIDE; void UpdateRectangle(); @@ -76,12 +78,15 @@ private: QPointF m_ptCenter; qreal m_dAngle; ArrowType m_eArrowType; + int m_penWidth; qreal GetScale() const; QLineF MainLine() const; QPolygonF FirstArrow(qreal dArrLen) const; QPolygonF SecondArrow(qreal dArrLen) const; + + QPainterPath MainShape() const; }; #endif // VGRAINLINEITEM_H diff --git a/src/libs/vwidgets/vpieceitem.cpp b/src/libs/vwidgets/vpieceitem.cpp index 40ea08864..f11b87d77 100644 --- a/src/libs/vwidgets/vpieceitem.cpp +++ b/src/libs/vwidgets/vpieceitem.cpp @@ -117,5 +117,5 @@ VPieceItem::MoveType VPieceItem::GetMoveType() const void VPieceItem::SetMoveType(const MoveType &moveType) { m_moveType = moveType; + setAcceptHoverEvents(m_moveType != NotMovable); } -