[qet] [3432] simple element : comment is snapped and centered to the bottom of text tagged "label"

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


Revision: 3432
Author:   blacksun
Date:     2014-11-02 15:12:38 +0100 (Sun, 02 Nov 2014)
Log Message:
-----------
simple element : comment is snapped and centered to the bottom of text tagged "label"

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

Added Paths:
-----------
    trunk/sources/qetgraphicsitem/qgraphicsitemutility.cpp
    trunk/sources/qetgraphicsitem/qgraphicsitemutility.h

Modified: trunk/sources/qetgraphicsitem/commentitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/commentitem.cpp	2014-11-02 11:51:59 UTC (rev 3431)
+++ trunk/sources/qetgraphicsitem/commentitem.cpp	2014-11-02 14:12:38 UTC (rev 3432)
@@ -19,6 +19,8 @@
 #include "element.h"
 #include "qetapp.h"
 #include "diagram.h"
+#include "elementtextitem.h"
+#include "qgraphicsitemutility.h"
 #include <QPainter>
 
 /**
@@ -27,13 +29,17 @@
  * element is also the parent item of this item
  */
 CommentItem::CommentItem(Element *elmt) :
-	QGraphicsObject(elmt),
-	m_element (elmt)
+	QGraphicsObject (elmt),
+	m_element       (elmt),
+	m_text_parent   (false)
 {
+	if (! setTextParent() ) {
+		connect(elmt, SIGNAL(yChanged()),                        this, SLOT (autoPos()));
+		connect(elmt, SIGNAL(rotationChanged()),                 this, SLOT (autoPos()));
+	}
+	connect(elmt, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT (updateLabel()));
+
 	updateLabel();
-	connect(elmt, SIGNAL(yChanged()),                        this, SLOT (autoPos()));
-	connect(elmt, SIGNAL(rotationChanged()),                 this, SLOT (autoPos()));
-	connect(elmt, SIGNAL(elementInfoChange(DiagramContext)), this, SLOT (updateLabel()));
 }
 
 /**
@@ -49,21 +55,10 @@
  * Adjust the position of this item.
  */
 void CommentItem::autoPos() {
-	if(!m_element -> diagram()) return;
-
-	QRectF  border = m_element -> diagram() -> border();
-	QPointF point  = m_element -> sceneBoundingRect().center();
-
-	point.setY(border.height() - m_element -> diagram() -> border_and_titleblock.titleBlockHeight() - boundingRect().height());
-	point.rx() -= (m_bounding_rect.width()/2 + m_bounding_rect.left()); //< we add boundingrect.left because this value can be négative
-
-	setPos(0,0);	//Due to a weird behavior or bug, before set the new position and rotation,
-	setRotation(0); //we must to set the position and rotation at 0.
-	setPos(mapFromScene(point));
-	if (rotation() != - m_element -> rotation()) {
-		setRotation(0);
-		setRotation(- m_element -> rotation());
-	}
+	if (m_text_parent)
+		centerToParentBottom(this);
+	else
+		centerToBottomDiagram(this, m_element);
 }
 
 /**
@@ -88,35 +83,54 @@
 }
 
 /**
+ * @brief CommentItem::setTextParent
+ * Set text tagged "label" of element has parent.
+ * @return true if text has been set has parent.
+ * else return false, the element is the parent of this comment item
+ */
+bool CommentItem::setTextParent() {
+	if (ElementTextItem *eti = m_element->taggedText("label")) {
+		setParentItem(eti);
+		m_text_parent = true;
+		return true;
+	}
+
+	qDebug() << "Comment item: can't found text tagged 'label' from actual parent element to set has parent, "
+				 "comment will be displayed at bottom of diagram";
+	return false;
+}
+
+/**
  * @brief CommentItem::updateLabel
  * update the content of this item
  * (draw this item in a QPicture)
  */
 void CommentItem::updateLabel() {
 	QString comment = m_element -> elementInformations()["comment"].toString();
-	bool    show    = m_element -> elementInformations().keyMustShow("comment");
 
-	if (comment == m_comment && show == m_show) return;
+	if (comment == m_comment && !m_text_parent) return;
 
-	m_comment = comment;
-	m_show    = show;
+	if (comment != m_comment) {
 
-	QPen pen(Qt::black);
-		 pen.setWidthF (0.5);
+		m_comment = comment;
 
-	QPainter painter(&m_picture);
-			 painter.setPen  (pen);
-			 painter.setFont (QETApp::diagramTextsFont(6));
+		QPen pen(Qt::black);
+			 pen.setWidthF (0.5);
 
-	QRectF drawing_rect(QPointF(0,0), QSizeF(100, 100));
-	QRectF text_bounding;
+		QPainter painter(&m_picture);
+				 painter.setPen  (pen);
+				 painter.setFont (QETApp::diagramTextsFont(6));
 
-	painter.drawText(drawing_rect, Qt::TextWordWrap | Qt::AlignHCenter, m_comment, &text_bounding);
+		QRectF drawing_rect(QPointF(0,0), QSizeF(100, 100));
+		QRectF text_bounding;
 
-	text_bounding.adjust(-1,0,1,0); //adjust only for better visual
-	painter.drawRoundedRect(text_bounding, 2, 2);
+		painter.drawText(drawing_rect, Qt::TextWordWrap | Qt::AlignHCenter, m_comment, &text_bounding);
 
-	m_bounding_rect = text_bounding;
+		text_bounding.adjust(-1,0,1,0); //adjust only for better visual
+		painter.drawRoundedRect(text_bounding, 2, 2);
 
+		m_bounding_rect = text_bounding;
+	}
+
 	autoPos();
 }

Modified: trunk/sources/qetgraphicsitem/commentitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/commentitem.h	2014-11-02 11:51:59 UTC (rev 3431)
+++ trunk/sources/qetgraphicsitem/commentitem.h	2014-11-02 14:12:38 UTC (rev 3432)
@@ -45,14 +45,14 @@
 	protected:
 		virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
 		virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * event );
+		virtual bool setTextParent ();
 
 	private:
 		Element *m_element;
 		QPicture m_picture;
 		QRectF   m_bounding_rect;
 		QString  m_comment;
-		bool     m_show;
-
+		bool     m_text_parent;
 };
 
 #endif // COMMENTITEM_H

Modified: trunk/sources/qetgraphicsitem/crossrefitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/crossrefitem.cpp	2014-11-02 11:51:59 UTC (rev 3431)
+++ trunk/sources/qetgraphicsitem/crossrefitem.cpp	2014-11-02 14:12:38 UTC (rev 3432)
@@ -21,6 +21,7 @@
 #include "diagramposition.h"
 #include "elementtextitem.h"
 #include "diagram.h"
+#include "qgraphicsitemutility.h"
 
 //define the height of the header.
 #define header 5
@@ -192,27 +193,10 @@
  */
 void CrossRefItem::autoPos() {
 	//We calcul the position according to the @snapTo of the xrefproperties
-	if (m_properties.snapTo() == XRefProperties::Bottom) {
-		QRectF border = m_element->diagram()->border();
-		QPointF point = m_element->sceneBoundingRect().center();
-
-		point.setY(border.height() - m_element->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height());
-		point.rx() -= (m_bounding_rect.width()/2 + m_bounding_rect.left()); //< we add boundingrect.left because this value can be négative
-
-		setPos(0,0);	//Due to a weird behavior or bug, before set the new position and rotation,
-		setRotation(0); //we must to set the position and rotation at 0.
-		setPos(mapFromScene(point));
-		if (rotation() != - m_element->rotation()) {
-			setRotation(0);
-			setRotation(- m_element->rotation());
-		}
-	}
-	else {
-		QPointF p = parentItem()->boundingRect().center();
-		p.ry() += parentItem()->boundingRect().height()/2;
-		p.rx() -= (m_bounding_rect.width()/2 + m_bounding_rect.left()); //< we add boundingrect.left because this value can be négative
-		setPos(p);
-	}
+	if (m_properties.snapTo() == XRefProperties::Bottom)
+		centerToBottomDiagram(this, m_element);
+	else
+		centerToParentBottom(this);
 }
 
 /**

Added: trunk/sources/qetgraphicsitem/qgraphicsitemutility.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qgraphicsitemutility.cpp	                        (rev 0)
+++ trunk/sources/qetgraphicsitem/qgraphicsitemutility.cpp	2014-11-02 14:12:38 UTC (rev 3432)
@@ -0,0 +1,78 @@
+/*
+	Copyright 2006-2014 The QElectroTech Team
+	This file is part of QElectroTech.
+
+	QElectroTech is free software: you can redistribute it and/or modify
+	it under the terms of the GNU General Public License as published by
+	the Free Software Foundation, either version 2 of the License, or
+	(at your option) any later version.
+
+	QElectroTech is distributed in the hope that it will be useful,
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	GNU General Public License for more details.
+
+	You should have received a copy of the GNU General Public License
+	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#include "qgraphicsitemutility.h"
+#include "element.h"
+#include "diagram.h"
+#include <QGraphicsItem>
+#include <QDebug>
+
+/**
+ * @brief centerToParentBottom
+ * Center the item at the bottom of is parent.
+ * @param item item to center
+ * @return true if centered else false (item have not parent)
+ */
+bool centerToParentBottom(QGraphicsItem *item) {
+	if (! item->parentItem()) {
+		qDebug() << "Qet::centerToParentBottom : item have not parent";
+		return false;
+	}
+
+	QPointF p = item -> parentItem() -> boundingRect().center();
+	p.ry() +=   item -> parentItem() -> boundingRect().height()/2;
+	p.rx() -=  (item -> boundingRect().width()/2 + item->boundingRect().left()); //< we add boundingrect.left because this value can be négative
+
+	item -> setPos(p);
+	return true;
+}
+
+/**
+ * @brief centerToBottomDiagram
+ * Set item pos to the bottom of diagram and centered vertically to element_to_follow
+ * @param item_to_center
+ * @param element_to_follow
+ * @return true if element is centered else false (element_to_follow have not diagram)
+ */
+bool centerToBottomDiagram (QGraphicsItem *item_to_center, Element *element_to_follow) {
+	if (! element_to_follow -> diagram()) {
+		qDebug() << "qgraphicsitemutility centerAtBottomDiagram : Element_to_follow have not diagram";
+		return false;
+	}
+
+	QRectF  border = element_to_follow -> diagram() -> border();
+	QPointF point  = element_to_follow -> sceneBoundingRect().center();
+
+	point.setY(border.height() -
+			   element_to_follow -> diagram() -> border_and_titleblock.titleBlockHeight() -
+			   item_to_center -> boundingRect().height());
+
+	point.rx() -= (item_to_center -> boundingRect().width()/2 +
+				   item_to_center -> boundingRect().left()); //< we add boundingrect.left because this value can be négative
+
+	item_to_center -> setPos(0,0);	  //Due to a weird behavior or bug, before set the new position and rotation,
+	item_to_center -> setRotation(0); //we must to set the position and rotation at 0.
+
+	item_to_center -> setPos(item_to_center -> mapFromScene(point));
+
+	if (item_to_center -> rotation() != - element_to_follow -> rotation()) {
+		item_to_center -> setRotation(0);
+		item_to_center -> setRotation(- element_to_follow -> rotation());
+	}
+
+	return true;
+}

Added: trunk/sources/qetgraphicsitem/qgraphicsitemutility.h
===================================================================
--- trunk/sources/qetgraphicsitem/qgraphicsitemutility.h	                        (rev 0)
+++ trunk/sources/qetgraphicsitem/qgraphicsitemutility.h	2014-11-02 14:12:38 UTC (rev 3432)
@@ -0,0 +1,27 @@
+/*
+	Copyright 2006-2014 The QElectroTech Team
+	This file is part of QElectroTech.
+
+	QElectroTech is free software: you can redistribute it and/or modify
+	it under the terms of the GNU General Public License as published by
+	the Free Software Foundation, either version 2 of the License, or
+	(at your option) any later version.
+
+	QElectroTech is distributed in the hope that it will be useful,
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	GNU General Public License for more details.
+
+	You should have received a copy of the GNU General Public License
+	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef QGRAPHICSITEMUTILITY_H
+#define QGRAPHICSITEMUTILITY_H
+
+class QGraphicsItem;
+class Element;
+
+bool centerToParentBottom  (QGraphicsItem *item);
+bool centerToBottomDiagram (QGraphicsItem *item_to_center, Element *element_to_follow);
+
+#endif // QGRAPHICSITEMUTILITY_H


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