[qet] qet/qet: [4784] Cross ref item : Double click in a contact of the Xref, go to the real contact.

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


Revision: 4784
Author:   blacksun
Date:     2016-11-16 19:37:31 +0100 (Wed, 16 Nov 2016)
Log Message:
-----------
Cross ref item : Double click in a contact of the Xref, go to the real contact. (only available for Xref draw as contacts) 

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

Modified: trunk/sources/qetgraphicsitem/crossrefitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/crossrefitem.cpp	2016-11-16 16:01:53 UTC (rev 4783)
+++ trunk/sources/qetgraphicsitem/crossrefitem.cpp	2016-11-16 18:37:31 UTC (rev 4784)
@@ -46,6 +46,7 @@
 	Q_ASSERT_X(elmt->diagram(), "CrossRefItem constructor", "Parent element is not in a diagram");
 
 	m_properties = elmt->diagram()->defaultXRefProperties(elmt->kindInformations()["type"].toString());
+	setAcceptHoverEvents(true);
 
 	connect(elmt -> diagram() -> project(), SIGNAL(projectDiagramsOrderChanged(QETProject*,int,int)), this, SLOT(updateLabel()));
 	connect(elmt -> diagram() -> project(), SIGNAL(diagramRemoved(QETProject*,Diagram*)),             this, SLOT(updateLabel()));
@@ -239,11 +240,92 @@
  * @brief CrossRefItem::mouseDoubleClickEvent
  * @param event
  */
-void CrossRefItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
+void CrossRefItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
+{
 	event->accept();
-	m_element->editProperty();
+	if (m_hovered_contact && m_hovered_contact->scene())
+	{
+			//Show and select the linked slave element
+		if (scene() != m_hovered_contact->scene())
+		{
+			m_hovered_contact->diagram()->showMe();
+		}
+		m_hovered_contact->setSelected(true);
+
+			//Zoom to the linked slave element
+		foreach(QGraphicsView *view, m_hovered_contact->diagram()->views())
+		{
+			QRectF fit = m_hovered_contact->sceneBoundingRect();
+			fit.adjust(-200, -200, 200, 200);
+			view->fitInView(fit, Qt::KeepAspectRatioByExpanding);
+		}
+	}
+	else
+	{
+		m_element->editProperty();
+	}
 }
 
+void CrossRefItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
+{
+	m_hovered_contact = nullptr;
+	QGraphicsObject::hoverEnterEvent(event);
+}
+
+void CrossRefItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
+{
+	QPointF pos = event->pos();
+
+	if (m_hovered_contact)
+	{
+		QRectF rect = m_hovered_contacts_map.value(m_hovered_contact);
+			//Mouse hover the same rect than previous hover event
+		if (rect.contains(pos))
+		{
+			QGraphicsObject::hoverMoveEvent(event);
+			return;
+		}
+			//Mouse don't hover previous rect
+		else
+		{
+			m_hovered_contact = nullptr;
+
+			foreach (Element *elmt, m_hovered_contacts_map.keys())
+			{
+					//Mouse hover a contact
+				if (m_hovered_contacts_map.value(elmt).contains(pos))
+				{
+					m_hovered_contact = elmt;
+				}
+			}
+			updateLabel();
+			QGraphicsObject::hoverMoveEvent(event);
+			return;
+		}
+	}
+	else
+	{
+		foreach (Element *elmt, m_hovered_contacts_map.keys())
+		{
+				//Mouse hover a contact
+			if (m_hovered_contacts_map.value(elmt).contains(pos))
+			{
+				m_hovered_contact = elmt;
+				updateLabel();
+				QGraphicsObject::hoverMoveEvent(event);
+				return;
+			}
+		}
+	}
+}
+
+void CrossRefItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
+{
+	m_hovered_contact = nullptr;
+	updateLabel();
+	QGraphicsObject::hoverLeaveEvent(event);
+}
+
 /**
  * @brief CrossRefItem::buildHeaderContact
  * Draw the QPicture of m_hdr_no_ctc and m_hdr_nc_ctc
@@ -374,6 +456,7 @@
 		return;
 
 	m_drawed_contacts = 0;
+	m_hovered_contacts_map.clear();
 	QRectF bounding_rect;
 
 	//Draw each linked contact
@@ -395,7 +478,7 @@
 			else if (type == "delayOn")  option += DelayOn;
 			else if (type == "delayOff") option += DelayOff;
 
-			QRectF br = drawContact(painter, option, elementPositionText(elmt));
+			QRectF br = drawContact(painter, option, elmt);
 			bounding_rect = bounding_rect.united(br);
 		}
 	}
@@ -411,14 +494,19 @@
  * Draw one contact, the type of contact to draw is define in @flags.
  * @param painter, painter to use
  * @param flags, define how to draw the contact (see enul CONTACTS)
- * @param str, the text to display for this contact (the position of the contact).
+ * @param elmt, the element to display text (the position of the contact)
  * @return The bounding rect of the draw (contact + text)
  */
-QRectF CrossRefItem::drawContact(QPainter &painter, int flags, QString str)
+QRectF CrossRefItem::drawContact(QPainter &painter, int flags, Element *elmt)
 {
+	QString str = elementPositionText(elmt);
 	int offset = m_drawed_contacts*10;
 	QRectF bounding_rect;
 
+	QPen pen = painter.pen();
+	m_hovered_contact == elmt ? pen.setColor(Qt::blue) :pen.setColor(Qt::black);
+	painter.setPen(pen);
+
 	//Draw NO or NC contact
 	if (flags &NOC)
 	{
@@ -496,6 +584,17 @@
 		painter.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, str);
 		bounding_rect = bounding_rect.united(text_rect);
 
+			//If an element have several contact drawed, we united all bounding rect of contacts.
+		if (m_hovered_contacts_map.contains(elmt))
+		{
+			QRectF rect = m_hovered_contacts_map.value(elmt);
+			m_hovered_contacts_map.insert(elmt, rect.united(bounding_rect));
+		}
+		else
+		{
+			m_hovered_contacts_map.insert(elmt, bounding_rect);
+		}
+
 		++m_drawed_contacts;
 	}
 
@@ -527,6 +626,17 @@
 		painter.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, str);
 		bounding_rect = bounding_rect.united(text_rect);
 
+			//If an element have several contact drawed, we united all bounding rect of contacts.
+		if (m_hovered_contacts_map.contains(elmt))
+		{
+			QRectF rect = m_hovered_contacts_map.value(elmt);
+			m_hovered_contacts_map.insert(elmt, rect.united(bounding_rect));
+		}
+		else
+		{
+			m_hovered_contacts_map.insert(elmt, bounding_rect);
+		}
+
 			//a switch contact take place of two normal contact
 		m_drawed_contacts += 2;
 	}

Modified: trunk/sources/qetgraphicsitem/crossrefitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/crossrefitem.h	2016-11-16 16:01:53 UTC (rev 4783)
+++ trunk/sources/qetgraphicsitem/crossrefitem.h	2016-11-16 18:37:31 UTC (rev 4784)
@@ -75,6 +75,9 @@
 	protected:
 		virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
 		virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * event );
+		virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
+		virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
+		virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
 
 	private:
 		void buildHeaderContact		();
@@ -81,7 +84,7 @@
 		void setUpCrossBoundingRect (QPainter &painter);
 		void drawAsCross			(QPainter &painter);
 		void drawAsContacts		(QPainter &painter);
-		QRectF drawContact			(QPainter &painter, int flags, QString str = QString());
+		QRectF drawContact			(QPainter &painter, int flags, Element *elmt);
 		void fillCrossRef			(QPainter &painter);
 		void AddExtraInfo			(QPainter &painter, QString);
 		void setTextParent			();
@@ -94,6 +97,10 @@
 		QPainterPath   m_shape_path;
 		XRefProperties m_properties;
 		int			   m_drawed_contacts;
+		QMap <Element *, QRectF> m_hovered_contacts_map;
+		Element *m_hovered_contact = nullptr;
+		QRectF m_hover_text_rect;
+
 };
 
 #endif // CROSSREFITEM_H


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