[qet] [2906] add cross reference graphic item, only for master element.

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


Revision: 2906
Author:   blacksun
Date:     2014-03-03 18:31:45 +0100 (Mon, 03 Mar 2014)
Log Message:
-----------
add cross reference graphic item, only for master element.
(basic implementation, need to be improved)

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

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

Added: trunk/sources/qetgraphicsitem/crossrefitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/crossrefitem.cpp	                        (rev 0)
+++ trunk/sources/qetgraphicsitem/crossrefitem.cpp	2014-03-03 17:31:45 UTC (rev 2906)
@@ -0,0 +1,175 @@
+#include "crossrefitem.h"
+#include "element.h"
+#include "qetapp.h"
+#include "diagramposition.h"
+
+/**
+ * @brief CrossRefItem::CrossRefItem
+ * Default constructor
+ * @param elmt element to dispaly the cross ref
+ * @param parent parent QetGraphicsItem
+ */
+CrossRefItem::CrossRefItem(Element *elmt, QetGraphicsItem *parent) :
+	QetGraphicsItem(parent),
+	element_ (elmt)
+{
+	setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
+	connect(elmt, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
+	connect(diagram()->project(), SIGNAL(projectDiagramsOrderChanged(QETProject*,int,int)), this, SLOT(updateLabel()));
+	updateLabel();
+	autoPos();
+}
+
+/**
+ * @brief CrossRefItem::~CrossRefItem
+ * Default destructor
+ */
+CrossRefItem::~CrossRefItem() {
+	disconnect(element_, SIGNAL(positionChange(QPointF)), this, SLOT(autoPos()));
+	disconnect(diagram()->project(), SIGNAL(projectDiagramsOrderChanged(QETProject*,int,int)), this, SLOT(updateLabel()));
+}
+
+/**
+ * @brief CrossRefItem::updateLabel
+ * Update the content of the item
+ */
+void CrossRefItem::updateLabel() {
+	//init the painter
+	QPainter qp(&drawing_);
+	QPen pen_;
+	pen_.setWidth(1);
+	qp.setPen(pen_);
+
+	//draw the cross
+	qp.drawLine(30,0, 30,50); //vertical line
+	qp.drawLine(0,10, 60,10); //horizontal line
+
+	//draw the header
+	qp.setFont(QETApp::diagramTextsFont(7));
+	QRectF header_rect (0,0,30,10);
+	qp.drawText(header_rect, Qt::AlignCenter, "NO");
+	header_rect.setRect(30, 0, 30, 10);
+	qp.drawText(header_rect, Qt::AlignCenter, "NC");
+
+	//and fill it
+	fillCrossRef(&qp);
+	update();
+}
+
+/**
+ * @brief CrossRefItem::autoPos
+ * Calculate and set position automaticaly.
+ */
+void CrossRefItem::autoPos() {
+	if (isSelected() && element_->isSelected()) return;
+	QRectF border= element_->diagram()->border();
+	QPointF point;
+
+	//if this item have parent calcule
+	//te position by using mapped point.
+	if(parentItem()) {
+		point = element_->boundingRect().center();
+		QPointF ypoint_ = mapToParent(mapFromScene(0, border.height() - element_->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height()));
+		point.setY(ypoint_.y());
+	}
+	else {
+		point = element_->sceneBoundingRect().center();
+		point.setY(border.height() - element_->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height());
+	}
+
+	point.setX(point.x() - boundingRect().width()/2);
+	setPos(point);
+}
+
+/**
+ * @brief CrossRefItem::boundingRect
+ * @return the bounding rect of this item
+ */
+QRectF CrossRefItem::boundingRect() const {
+	return (QRectF(QPointF(0,0), QSizeF(60,50)));
+}
+
+/**
+ * @brief CrossRefItem::paint
+ * Paint this item
+ * @param painter
+ * @param option
+ * @param widget
+ */
+void CrossRefItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+	Q_UNUSED(option);
+	Q_UNUSED(widget);
+
+	//draw the selection rect
+	if (isSelected()) {
+		painter->save();
+		QPen t(Qt::black);
+		t.setStyle(Qt::DashLine);
+		painter -> setPen(t);
+		painter->drawRect(boundingRect());
+		painter->restore();
+	}
+	drawing_.play(painter);
+}
+
+/**
+ * @brief CrossRefItem::mouseMoveEvent
+ * handle mouse move event
+ * @param e event
+ */
+void CrossRefItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
+	element_->setHighlighted(true);
+	QetGraphicsItem::mouseMoveEvent(e);
+}
+
+/**
+ * @brief CrossRefItem::mouseReleaseEvent
+ * handle mouse release event
+ * @param e event
+ */
+void CrossRefItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
+	element_->setHighlighted(false);
+	QetGraphicsItem::mouseReleaseEvent(e);
+}
+
+/**
+ * @brief CrossRefItem::fillCrossRef
+ * Fill the content of the cross ref
+ * @param painter painter to use.
+ */
+void CrossRefItem::fillCrossRef(QPainter *painter) {
+	if (element_->isFree()) return;
+
+	QList <Element *> NO_list;
+	QList <Element *> NC_list;
+
+	//find each no and nc of connected element to element_
+	foreach (Element *elmt, element_->linkedElements()) {
+		QString state = elmt->kindInformations()["state"].toString();
+		if (state == "NO")		NO_list << elmt;
+		else if (state == "NC") NC_list << elmt;
+	}
+
+	painter->setFont(QETApp::diagramTextsFont(6));
+	//fill the NO
+	QString contact_str;
+	foreach (Element *elmt, NO_list) {
+		contact_str += QString::number(elmt->diagram()->folioIndex() + 1);
+		contact_str += "-";
+		contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString();
+		contact_str += "\n";
+	}
+	QRectF rect_(0, 13, 30, 40);
+	painter->drawText(rect_, Qt::AlignHCenter, contact_str);
+
+	//fill the NC
+	contact_str.clear();
+	foreach (Element *elmt, NC_list) {
+		contact_str += QString::number(elmt->diagram()->folioIndex() + 1);
+		contact_str += "-";
+		contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString();
+		contact_str += "\n";
+	}
+	rect_.setRect(30, 13, 30, 40);
+	painter->drawText(rect_, Qt::AlignHCenter, contact_str);
+}

Added: trunk/sources/qetgraphicsitem/crossrefitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/crossrefitem.h	                        (rev 0)
+++ trunk/sources/qetgraphicsitem/crossrefitem.h	2014-03-03 17:31:45 UTC (rev 2906)
@@ -0,0 +1,41 @@
+#ifndef CROSSREFITEM_H
+#define CROSSREFITEM_H
+
+#include "qetgraphicsitem/qetgraphicsitem.h"
+class element;
+
+
+class CrossRefItem : public QetGraphicsItem
+{
+	Q_OBJECT
+
+	//Methods
+	public:
+	explicit CrossRefItem(Element *elmt, QetGraphicsItem *parent = 0);
+	~CrossRefItem();
+
+	QRectF boundingRect() const;
+
+	signals:
+
+	public slots:
+	void updateLabel();
+	void autoPos();
+
+	protected:
+	virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
+	virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
+	virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
+
+	private:
+	void fillCrossRef(QPainter *painter);
+
+	//Attributes
+	private:
+	Element *element_; //element to display the cross reference
+	QPicture drawing_;
+	bool b;
+
+};
+
+#endif // CROSSREFITEM_H

Modified: trunk/sources/qetgraphicsitem/masterelement.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/masterelement.cpp	2014-03-02 21:11:32 UTC (rev 2905)
+++ trunk/sources/qetgraphicsitem/masterelement.cpp	2014-03-03 17:31:45 UTC (rev 2906)
@@ -29,6 +29,7 @@
 	CustomElement(location, qgi, s, state)
 {
 	link_type_ = Master;
+	cri_ = 0;
 }
 
 /**
@@ -48,9 +49,12 @@
 void MasterElement::linkToElement(Element *elmt) {	
 	// check if element is slave and if isn't already linked
 	if (elmt->linkType() == Slave && !connected_elements.contains(elmt)) {
-		///TODO create the cross ref and connection
 		connected_elements << elmt;
 		elmt->linkToElement(this);
+		//create cross ref item if not yet
+		if (!cri_) cri_ = new CrossRefItem(this, this);
+		connect(elmt, SIGNAL(positionChange(QPointF)), cri_, SLOT(updateLabel()));
+		cri_->updateLabel();
 	}
 }
 
@@ -77,5 +81,14 @@
 	if (connected_elements.contains(elmt)) {
 		connected_elements.removeOne(elmt);
 		elmt->unlinkElement(this);
+		//update the graphics cross ref
+		disconnect(elmt, SIGNAL(positionChange(QPointF)), cri_, SLOT(updateLabel()));
+		if (isFree()) {
+			delete cri_;
+			cri_ = 0;
+		}
+		else {
+			cri_->updateLabel();
+		}
 	}
 }

Modified: trunk/sources/qetgraphicsitem/masterelement.h
===================================================================
--- trunk/sources/qetgraphicsitem/masterelement.h	2014-03-02 21:11:32 UTC (rev 2905)
+++ trunk/sources/qetgraphicsitem/masterelement.h	2014-03-03 17:31:45 UTC (rev 2906)
@@ -19,6 +19,7 @@
 #define MASTERELEMENT_H
 
 #include "customelement.h"
+#include "crossrefitem.h"
 
 class MasterElement : public CustomElement
 {
@@ -35,6 +36,9 @@
 
 	public slots:
 
+	private:
+	CrossRefItem *cri_;
+
 };
 
 #endif // MASTERELEMENT_H

Modified: trunk/sources/qetgraphicsitem/qetgraphicsitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/qetgraphicsitem.h	2014-03-02 21:11:32 UTC (rev 2905)
+++ trunk/sources/qetgraphicsitem/qetgraphicsitem.h	2014-03-03 17:31:45 UTC (rev 2906)
@@ -34,7 +34,7 @@
 	virtual void setPos(qreal x, qreal y);
 	virtual void rotateBy(const qreal &);
 	virtual void applyRotation(const qreal &);
-	virtual void editProperty ()=0;
+	virtual void editProperty (){}
 
 	signals:
 	void positionChange(QPointF);


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