[qet] [3512] QetShapeItem : improve mouse interaction

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 3512
Author:   blacksun
Date:     2014-11-24 18:36:02 +0100 (Mon, 24 Nov 2014)
Log Message:
-----------
QetShapeItem : improve mouse interaction

Modified Paths:
--------------
    trunk/sources/qetgraphicsitem/qetshapeitem.cpp
    trunk/sources/qetgraphicsitem/qetshapeitem.h

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2014-11-24 03:00:36 UTC (rev 3511)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2014-11-24 17:36:02 UTC (rev 3512)
@@ -35,16 +35,17 @@
 	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 @@
  * @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 @@
  */
 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 @@
 			break;
 	}
 
-	return path;
+	QPainterPathStroker pps;
+	pps.setWidth(10);
+	pps.setJoinStyle(Qt::RoundJoin);
+
+	return (pps.createStroke(path));
 }
 
 /**
@@ -170,24 +168,33 @@
 {
 	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);
@@ -196,6 +203,30 @@
 }
 
 /**
+ * @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
  * @param e element where is stored this item

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.h	2014-11-24 03:00:36 UTC (rev 3511)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.h	2014-11-24 17:36:02 UTC (rev 3512)
@@ -68,6 +68,8 @@
 
 	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 @@
 	Qt::PenStyle m_shapeStyle;
 	QPointF		 m_P1, m_P2;
 	QPolygonF	 m_polygon;
+	bool         m_hovered;
 };
 #endif // QETSHAPEITEM_H


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/