Improve user expirience. Added grainline hover.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2017-02-28 17:33:53 +02:00
parent 5fbdb4f7f0
commit 04009746d5
3 changed files with 63 additions and 39 deletions

View File

@ -68,8 +68,10 @@ VGrainlineItem::VGrainlineItem(QGraphicsItem* pParent)
m_ptFinish(), m_ptFinish(),
m_ptCenter(), m_ptCenter(),
m_dAngle(0), m_dAngle(0),
m_eArrowType(ArrowType::atBoth) m_eArrowType(ArrowType::atBoth),
m_penWidth(LINE_PEN_WIDTH)
{ {
setAcceptHoverEvents(true);
m_inactiveZ = 5; m_inactiveZ = 5;
Reset(); Reset();
UpdateRectangle(); UpdateRectangle();
@ -85,49 +87,16 @@ VGrainlineItem::~VGrainlineItem()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QPainterPath VGrainlineItem::shape() const QPainterPath VGrainlineItem::shape() const
{ {
QPainterPath path;
if (m_eMode == mNormal) if (m_eMode == mNormal)
{ {
QPointF pt1; return MainShape();
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();
}
} }
else else
{ {
QPainterPath path;
path.addPolygon(m_polyBound); path.addPolygon(m_polyBound);
return path;
} }
return path;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -143,7 +112,7 @@ void VGrainlineItem::paint(QPainter* pP, const QStyleOptionGraphicsItem* pOption
Q_UNUSED(pWidget) Q_UNUSED(pWidget)
pP->save(); pP->save();
QColor clr = Qt::black; 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); pP->setRenderHints(QPainter::Antialiasing);
// line // 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 * @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)); pt2.y() - dArrLen*sin(M_PI + m_dRotation - ARROW_ANGLE));
return poly; 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;
}

View File

@ -54,6 +54,8 @@ protected:
virtual void mousePressEvent(QGraphicsSceneMouseEvent* pME) Q_DECL_OVERRIDE; virtual void mousePressEvent(QGraphicsSceneMouseEvent* pME) Q_DECL_OVERRIDE;
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* pME) Q_DECL_OVERRIDE; virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* pME) Q_DECL_OVERRIDE;
virtual void mouseReleaseEvent(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; virtual void Update() Q_DECL_OVERRIDE;
void UpdateRectangle(); void UpdateRectangle();
@ -76,12 +78,15 @@ private:
QPointF m_ptCenter; QPointF m_ptCenter;
qreal m_dAngle; qreal m_dAngle;
ArrowType m_eArrowType; ArrowType m_eArrowType;
int m_penWidth;
qreal GetScale() const; qreal GetScale() const;
QLineF MainLine() const; QLineF MainLine() const;
QPolygonF FirstArrow(qreal dArrLen) const; QPolygonF FirstArrow(qreal dArrLen) const;
QPolygonF SecondArrow(qreal dArrLen) const; QPolygonF SecondArrow(qreal dArrLen) const;
QPainterPath MainShape() const;
}; };
#endif // VGRAINLINEITEM_H #endif // VGRAINLINEITEM_H

View File

@ -117,5 +117,5 @@ VPieceItem::MoveType VPieceItem::GetMoveType() const
void VPieceItem::SetMoveType(const MoveType &moveType) void VPieceItem::SetMoveType(const MoveType &moveType)
{ {
m_moveType = moveType; m_moveType = moveType;
setAcceptHoverEvents(m_moveType != NotMovable);
} }