QetShapeItem : improve mouse interaction

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3512 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2014-11-24 17:36:02 +00:00
parent 80e78b4e04
commit dec5d3002c
2 changed files with 55 additions and 21 deletions

View File

@@ -35,16 +35,17 @@ QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem
m_shapeType(type), m_shapeType(type),
m_shapeStyle(Qt::DashLine), m_shapeStyle(Qt::DashLine),
m_P1 (Diagram::snapToGrid(p1)), 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; if (type == Polyline) m_polygon << m_P1 << m_P2;
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
setAcceptHoverEvents(true);
} }
QetShapeItem::~QetShapeItem() QetShapeItem::~QetShapeItem()
{ {}
}
/** /**
* @brief QetShapeItem::setStyle * @brief QetShapeItem::setStyle
@@ -108,11 +109,7 @@ void QetShapeItem::setNextPoint(QPointF P) {
* @return the bounding rect of this item * @return the bounding rect of this item
*/ */
QRectF QetShapeItem::boundingRect() const { QRectF QetShapeItem::boundingRect() const {
if (m_shapeType == Polyline) return shape().boundingRect();
return ( shape().boundingRect());
QRectF b(m_P1, m_P2);
return b.normalized();
} }
/** /**
@@ -121,20 +118,17 @@ QRectF QetShapeItem::boundingRect() const {
*/ */
QPainterPath QetShapeItem::shape() const { QPainterPath QetShapeItem::shape() const {
QPainterPath path; QPainterPath path;
QPainterPathStroker pps;
switch (m_shapeType) { switch (m_shapeType) {
case Line: case Line:
path.moveTo(m_P1); path.moveTo(m_P1);
path.lineTo(m_P2); path.lineTo(m_P2);
pps.setWidth(10);
path = pps.createStroke(path);
break; break;
case Rectangle: case Rectangle:
path.addRect(boundingRect()); path.addRect(QRectF(m_P1, m_P2));
break; break;
case Ellipse: case Ellipse:
path.addEllipse(boundingRect()); path.addEllipse(QRectF(m_P1, m_P2));
break; break;
case Polyline: case Polyline:
path.addPolygon(m_polygon); path.addPolygon(m_polygon);
@@ -144,7 +138,11 @@ QPainterPath QetShapeItem::shape() const {
break; 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); Q_UNUSED(option); Q_UNUSED(widget);
QPen pen; QPen pen(m_shapeStyle);
pen.setStyle(m_shapeStyle);
if (isSelected()) pen.setColor(Qt::red);
painter -> setRenderHint(QPainter::Antialiasing, false);
pen.setWidthF(1); 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) { switch (m_shapeType) {
case Line: case Line:
painter->drawLine(QLineF(m_P1, m_P2)); painter->drawLine(QLineF(m_P1, m_P2));
break; break;
case Rectangle: case Rectangle:
painter->drawRect(boundingRect()); painter->drawRect(QRectF(m_P1, m_P2));
break; break;
case Ellipse: case Ellipse:
painter->drawEllipse(boundingRect()); painter->drawEllipse(QRectF(m_P1, m_P2));
break; break;
case Polyline: case Polyline:
painter->drawPolyline(m_polygon); 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 * @brief QetShapeItem::fromXml
* Build this item from the xml description * Build this item from the xml description

View File

@@ -68,6 +68,8 @@ class QetShapeItem : public QetGraphicsItem
protected: protected:
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
virtual void hoverEnterEvent (QGraphicsSceneHoverEvent *event);
virtual void hoverLeaveEvent (QGraphicsSceneHoverEvent *event);
private: private:
void changeGraphicsItem (const ShapeType &newtype); void changeGraphicsItem (const ShapeType &newtype);
@@ -81,5 +83,6 @@ class QetShapeItem : public QetGraphicsItem
Qt::PenStyle m_shapeStyle; Qt::PenStyle m_shapeStyle;
QPointF m_P1, m_P2; QPointF m_P1, m_P2;
QPolygonF m_polygon; QPolygonF m_polygon;
bool m_hovered;
}; };
#endif // QETSHAPEITEM_H #endif // QETSHAPEITEM_H