[qet] [3060] Cross ref item: when xref is show has table, user can add prefix to the text of power and delay contact.

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


Revision: 3060
Author:   blacksun
Date:     2014-05-07 22:56:51 +0200 (Wed, 07 May 2014)
Log Message:
-----------
Cross ref item: when xref is show has table, user can add prefix to the text of power and delay contact.
Minor improvemnt: the table update is size according to his content

Modified Paths:
--------------
    trunk/sources/properties/xrefproperties.cpp
    trunk/sources/properties/xrefproperties.h
    trunk/sources/qetgraphicsitem/crossrefitem.cpp
    trunk/sources/qetgraphicsitem/crossrefitem.h
    trunk/sources/ui/xrefpropertieswidget.cpp
    trunk/sources/ui/xrefpropertieswidget.ui

Modified: trunk/sources/properties/xrefproperties.cpp
===================================================================
--- trunk/sources/properties/xrefproperties.cpp	2014-05-06 23:38:06 UTC (rev 3059)
+++ trunk/sources/properties/xrefproperties.cpp	2014-05-07 20:56:51 UTC (rev 3060)
@@ -35,6 +35,8 @@
 	settings.setValue(prefix + "showpowerctc", m_show_power_ctc);
 	QString display = m_display == Cross? "cross" : "contacts";
 	settings.setValue(prefix + "displayhas", display);
+	settings.setValue(prefix + "powerprefix",  m_prefix.value("power"));
+	settings.setValue(prefix + "delayprefix", m_prefix.value("delay"));
 }
 
 /**
@@ -47,6 +49,8 @@
 	m_show_power_ctc = settings.value(prefix + "showpowerctc", false).toBool();
 	QString display = settings.value(prefix + "displayhas", "cross").toString();
 	display == "cross"? m_display = Cross : m_display = Contacts;
+	m_prefix.insert("power", settings.value(prefix + "powerprefix").toString());
+	m_prefix.insert("delay", settings.value(prefix + "delayprefix").toString());
 }
 
 /**
@@ -58,6 +62,8 @@
 	xml_element.setAttribute("showpowerctc", m_show_power_ctc? "true" : "fasle");
 	QString display = m_display == Cross? "cross" : "contacts";
 	xml_element.setAttribute("displayhas", display);
+	xml_element.setAttribute("powerprefix", m_prefix.value("power"));
+	xml_element.setAttribute("delayprefix", m_prefix.value("delay"));
 }
 
 /**
@@ -69,11 +75,14 @@
 	m_show_power_ctc = xml_element.attribute("showpowerctc")  == "true";
 	QString display = xml_element.attribute("displayhas", "cross");
 	display == "cross"? m_display = Cross : m_display = Contacts;
+	m_prefix.insert("power", xml_element.attribute("powerprefix"));
+	m_prefix.insert("delay", xml_element.attribute("delayprefix"));
 }
 
 bool XRefProperties::operator ==(const XRefProperties &xrp) const{
 	return (m_show_power_ctc == xrp.m_show_power_ctc &&
-			m_display == xrp.m_display);
+			m_display == xrp.m_display &&
+			m_prefix == xrp.m_prefix);
 }
 
 bool XRefProperties::operator !=(const XRefProperties &xrp) const {

Modified: trunk/sources/properties/xrefproperties.h
===================================================================
--- trunk/sources/properties/xrefproperties.h	2014-05-06 23:38:06 UTC (rev 3059)
+++ trunk/sources/properties/xrefproperties.h	2014-05-07 20:56:51 UTC (rev 3060)
@@ -48,9 +48,13 @@
 	void setDisplayHas (const DisplayHas dh) {m_display = dh;}
 	DisplayHas displayHas () const			 {return m_display;}
 
+	void setPrefix (const QString &key, const QString &value) {m_prefix.insert(key, value);}
+	QString prefix (const QString &key) const {return m_prefix.value(key);}
+
 	private:
 	bool m_show_power_ctc;
 	DisplayHas m_display;
+	QHash <QString, QString> m_prefix;
 };
 
 #endif // XREFPROPERTIES_H

Modified: trunk/sources/qetgraphicsitem/crossrefitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/crossrefitem.cpp	2014-05-06 23:38:06 UTC (rev 3059)
+++ trunk/sources/qetgraphicsitem/crossrefitem.cpp	2014-05-07 20:56:51 UTC (rev 3060)
@@ -22,8 +22,6 @@
 
 //define the height of the header.
 #define header 5
-//define the widht of the cross
-#define crossWidth 50
 
 /**
  * @brief CrossRefItem::CrossRefItem
@@ -71,7 +69,25 @@
 	return m_shape_path;
 }
 
-void CrossRefItem::setProperties(XRefProperties xrp) {
+/**
+ * @brief CrossRefItem::elementPositionText
+ * @param elmt
+ * @return the string corresponding to the position of @elmt in the diagram.
+ * if @add_prefix is true, prefix (for power and delay contact) is added to the poistion text.
+ */
+QString CrossRefItem::elementPositionText(const Element *elmt, const bool &add_prefix) const{
+	QString txt;
+	txt += QString::number(elmt->diagram()->folioIndex() + 1);
+	txt += "-";
+	txt += elmt->diagram()->convertPosition(elmt -> scenePos()).toString();
+	if (add_prefix) {
+		if (elmt->kindInformations()["type"].toString() == "power") txt.prepend(m_properties.prefix("power"));
+		else if (elmt->kindInformations()["type"].toString().contains("delay")) txt.prepend(m_properties.prefix("delay"));
+	}
+	return txt;
+}
+
+void CrossRefItem::setProperties(const XRefProperties &xrp) {
 	if (m_properties != xrp) {
 		m_properties = xrp;
 		updateLabel();
@@ -128,7 +144,8 @@
 		point.setY(border.height() - m_element->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height());
 	}
 
-	point.setX(point.x() - crossWidth/2);
+	qreal offset = m_bounding_rect.topLeft().x() < 0 ? m_bounding_rect.topLeft().x() : 0;
+	point.setX(point.x() - m_bounding_rect.width()/2 - offset);
 	setPos(point);
 }
 
@@ -178,6 +195,52 @@
 }
 
 /**
+ * @brief CrossRefItem::buildHeaderContact
+ * Draw the QPicture of m_hdr_no_ctc and m_hdr_nc_ctc
+ */
+void CrossRefItem::buildHeaderContact() {
+	if (!m_hdr_no_ctc.isNull() && !m_hdr_nc_ctc.isNull()) return;
+
+	//init the painter
+	QPainter qp;
+	QPen pen_;
+	pen_.setWidthF(0.2);
+
+	//draw the NO contact
+	if (m_hdr_no_ctc.isNull()) {
+		qp.begin(&m_hdr_no_ctc);
+		qp.setPen(pen_);
+		qp.drawLine(0, 3, 5, 3);
+		QPointF p1[3] = {
+			QPointF(5, 0),
+			QPointF(10, 3),
+			QPointF(15, 3),
+		};
+		qp.drawPolyline(p1,3);
+		qp.end();
+	}
+
+	//draw the NC contact
+	if (m_hdr_nc_ctc.isNull()) {
+		qp.begin(&m_hdr_nc_ctc);
+		qp.setPen(pen_);
+		QPointF p2[3] = {
+			QPointF(0, 3),
+			QPointF(5, 3),
+			QPointF(5, 0)
+		};
+		qp.drawPolyline(p2,3);
+		QPointF p3[3] = {
+			QPointF(4, 0),
+			QPointF(10, 3),
+			QPointF(15, 3),
+		};
+		qp.drawPolyline(p3,3);
+		qp.end();
+	}
+}
+
+/**
  * @brief CrossRefItem::setUpCrossBoundingRect
  * Get the numbers of slaves elements linked to this parent element,
  * for calculate the size of the cross bounding rect.
@@ -185,30 +248,55 @@
  */
 void CrossRefItem::setUpCrossBoundingRect() {
 	//this is the default size of cross ref item
-	QRectF default_bounding(0, 0, crossWidth, 40);
+	//add 2 to header for better visual
+	QRectF default_bounding(0, 0, 40, header+2);
 
 	//No need to calcul if nothing is linked
 	if (!m_element->isFree()) {
+		/*Set up a Qpainter with the same parametres
+		than the QPainter used for draw the text inside the cross,
+		for calculate the size of each text
+		=====
+		We can also use the QPainter used for draw the text inside the cross
+		and call the method "boundingrect", for know the size of text,
+		but the QrectF returned isn't good (bug)*/
+		QPainter qp;
+		QPicture pict;
+		qp.begin(&pict);
+		QPen pen_;
+		pen_.setWidthF(0.2);
+		qp.setPen(pen_);
+		qp.setFont(QETApp::diagramTextsFont(5));
+
 		QList <Element *> NO_list;
 		QList <Element *> NC_list;
 
 		//find each no and nc of connected element to m_element
+		//and define the size of default_bounding according to the connected elements
+		bool was_ajusted = false;
+		qreal no_height = 0, nc_height = 0;
 		foreach (Element *elmt, m_element->linkedElements()) {
+			QRectF r;
+			qp.drawText(r, Qt::AlignCenter, elementPositionText(elmt, true), &r);
+			if (r.width() > default_bounding.width()/2) {
+				default_bounding.setWidth(r.width()*2);
+				was_ajusted = true;
+			}
+
 			QString state = elmt->kindInformations()["state"].toString();
-			if (state == "NO")		NO_list << elmt;
-			else if (state == "NC") NC_list << elmt;
+			if (state == "NO") {
+				NO_list << elmt;
+				no_height += r.height();
+			}
+			else if (state == "NC") {
+				NC_list << elmt;
+				nc_height += r.height();
+			}
 		}
-
-		int i =0;
-		if (NO_list.count()>4 || NC_list.count()>4) {
-			i = NO_list.count() > NC_list.count()?
-						NO_list.count() : NC_list.count();
-
-			//increase the height of bounding rect,
-			//according to the number of slave item less 4.
-			i-=4;
-			default_bounding.setHeight(default_bounding.height() + (i*8));
-		}
+		if (was_ajusted)	   default_bounding.setWidth  (default_bounding.width()+5); //adjust only for better visual
+		no_height > nc_height? default_bounding.setHeight (default_bounding.height() + no_height) :
+							   default_bounding.setHeight (default_bounding.height() + nc_height);
+		qp.end();
 	}
 	m_shape_path.addRect(default_bounding);
 	m_bounding_rect = default_bounding;
@@ -226,42 +314,15 @@
 	//draw the cross
 	QRectF br = boundingRect();
 	painter.drawLine(br.width()/2, 0, br.width()/2, br.height());	//vertical line
-	painter.drawLine(br.width()/2-(crossWidth/2), header, br.width()/2+(crossWidth/2), header);	//horizontal line
+	painter.drawLine(0, header, br.width(), header);	//horizontal line
 
-	//draw the symbolic NO
-	qreal xoffset = br.width()/2 - 25;
-	painter.drawLine(xoffset+5, 3, xoffset+10, 3);
-	QPointF p1[3] = {
-		QPointF(xoffset+10, 0),
-		QPointF(xoffset+15, 3),
-		QPointF(xoffset+20, 3),
-	};
-	painter.drawPolyline(p1,3);
+	//Add the symbolic contacts
+	buildHeaderContact();
+	QPointF p((m_bounding_rect.width()/4) - (m_hdr_no_ctc.width()/2), 0);
+	painter.drawPicture (p, m_hdr_no_ctc);
+	p.setX((m_bounding_rect.width() * 3/4) - (m_hdr_nc_ctc.width()/2));
+	painter.drawPicture (p, m_hdr_nc_ctc);
 
-	//draw the symbolic NC
-	xoffset = br.width()/2;
-	QPointF p2[3] = {
-		QPointF(xoffset+5, 3),
-		QPointF(xoffset+10, 3),
-		QPointF(xoffset+10, 0)
-	};
-	painter.drawPolyline(p2,3);
-	QPointF p3[3] = {
-		QPointF(xoffset+9, 0),
-		QPointF(xoffset+15, 3),
-		QPointF(xoffset+20, 3),
-	};
-	painter.drawPolyline(p3,3);
-
-	///keep this code for possible next feature
-	///choice to use symbolic or text.
-	//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(painter);
 }
@@ -294,15 +355,11 @@
 			else if (type == "delayOn") option += DelayOn;
 			else if (type == "delayOff") option += DelayOff;
 
-			QString contact_str;
-			contact_str += QString::number(elmt->diagram()->folioIndex() + 1);
-			contact_str += "-";
-			contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString();
-			drawContact(painter, option, contact_str);
+			drawContact(painter, option, elementPositionText(elmt));
 		}
 	}
 
-	QRectF br(0,0,crossWidth, m_drawed_contacts*10+4);
+	QRectF br(0, 0, 50, m_drawed_contacts*10+4);
 	m_bounding_rect = br;
 	m_shape_path.addRect(br);
 	painter.restore();
@@ -416,30 +473,27 @@
 	//fill the NO
 	foreach (Element *elmt, NO_list) {
 		++i;
-		contact_str += QString::number(elmt->diagram()->folioIndex() + 1);
-		contact_str += "-";
-		contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString();
+		contact_str += elementPositionText(elmt, true);
 		if(NO_list.size() > i) contact_str += "\n";
 	}
-	QRectF rect_(middle_cross - (crossWidth/2),
+	QRectF rect_(0,
 				 header,
 				 middle_cross,
-				 (m_bounding_rect.height()-header));
+				 m_bounding_rect.height()-header);
 	painter.drawText(rect_, Qt::AlignTop | Qt::AlignLeft, contact_str);
 
 	//fill the NC
 	contact_str.clear();
 	i = 0;
 	foreach (Element *elmt, NC_list) {
-		contact_str += QString::number(elmt->diagram()->folioIndex() + 1);
-		contact_str += "-";
-		contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString();
+		++i;
+		contact_str += elementPositionText(elmt, true);
 		if (NC_list.size() > i) contact_str += "\n";
 	}
 	rect_.setRect(middle_cross,
 				  header,
-				  crossWidth/2,
-				  (m_bounding_rect.height()-header));
+				  middle_cross,
+				  m_bounding_rect.height()-header);
 	painter.drawText(rect_, Qt::AlignTop | Qt::AlignRight, contact_str);
 }
 
@@ -475,7 +529,6 @@
  * must to be show or not
  */
 void CrossRefItem::checkMustShow() {
-
 	//We always show Xref when is displayed has contact
 	if (m_properties.displayHas() == XRefProperties::Contacts) {
 		this->show();

Modified: trunk/sources/qetgraphicsitem/crossrefitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/crossrefitem.h	2014-05-06 23:38:06 UTC (rev 3059)
+++ trunk/sources/qetgraphicsitem/crossrefitem.h	2014-05-07 20:56:51 UTC (rev 3060)
@@ -50,15 +50,16 @@
 		DelayOff = 16
 	};
 
-	QRectF boundingRect() const;
-	virtual QPainterPath shape() const;
+	QRectF boundingRect			() const;
+	virtual QPainterPath shape	() const;
+	QString elementPositionText (const Element *elmt, const bool &add_prefix = false) const;
 
 	signals:
 
 	public slots:
-	void setProperties (XRefProperties xrp);
-	void updateLabel();
-	void autoPos();
+	void setProperties (const XRefProperties &xrp);
+	void updateLabel   ();
+	void autoPos	   ();
 
 	protected:
 	virtual void paint			   (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
@@ -66,19 +67,20 @@
 	virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *e);
 
 	private:
-	void setUpCrossBoundingRect();
-	void drawHasCross	 (QPainter &painter);
-	void drawHasContacts (QPainter &painter);
-	void drawContact	 (QPainter &painter, int flags, QString str = QString());
-	void fillCrossRef	 (QPainter &painter);
-	void AddExtraInfo	 (QPainter &painter);
-	void checkMustShow	 ();
+	void buildHeaderContact		();
+	void setUpCrossBoundingRect ();
+	void drawHasCross			(QPainter &painter);
+	void drawHasContacts		(QPainter &painter);
+	void drawContact			(QPainter &painter, int flags, QString str = QString());
+	void fillCrossRef			(QPainter &painter);
+	void AddExtraInfo			(QPainter &painter);
+	void checkMustShow			();
 
 	//Attributes
 	private:
 	Element		  *m_element; //element to display the cross reference
 	QRectF		   m_bounding_rect;
-	QPicture	   m_drawing;
+	QPicture	   m_drawing, m_hdr_no_ctc, m_hdr_nc_ctc;
 	QPainterPath   m_shape_path;
 	XRefProperties m_properties;
 	int			   m_drawed_contacts;

Modified: trunk/sources/ui/xrefpropertieswidget.cpp
===================================================================
--- trunk/sources/ui/xrefpropertieswidget.cpp	2014-05-06 23:38:06 UTC (rev 3059)
+++ trunk/sources/ui/xrefpropertieswidget.cpp	2014-05-07 20:56:51 UTC (rev 3060)
@@ -31,7 +31,7 @@
 	m_properties(properties)
 {
 	ui->setupUi(this);
-	connect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_show_power_cb, SLOT(setEnabled(bool)));
+	connect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_cross_properties_gb, SLOT(setEnabled(bool)));
 	updateDisplay();
 }
 
@@ -41,7 +41,7 @@
  */
 XRefPropertiesWidget::~XRefPropertiesWidget()
 {
-	disconnect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_show_power_cb, SLOT(setEnabled(bool)));
+	disconnect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_cross_properties_gb, SLOT(setEnabled(bool)));
 	delete ui;
 }
 
@@ -63,6 +63,8 @@
 	if		(ui->m_display_has_cross_rb->isChecked())	 m_properties.setDisplayHas(XRefProperties::Cross);
 	else if (ui->m_display_has_contacts_rb->isChecked()) m_properties.setDisplayHas(XRefProperties::Contacts);
 	m_properties.setShowPowerContac(ui->m_show_power_cb->isChecked());
+	m_properties.setPrefix("power", ui->m_power_prefix_le->text());
+	m_properties.setPrefix("delay", ui->m_delay_prefix_le->text());
 
 	return m_properties;
 }
@@ -77,9 +79,9 @@
 	ui->m_display_has_contacts_rb->setDisabled(ro);
 
 	if (m_properties.displayHas() != XRefProperties::Cross)
-		ui->m_show_power_cb->setDisabled(true);
+		ui->m_cross_properties_gb->setDisabled(true);
 	else
-		ui->m_show_power_cb->setDisabled(ro);
+		ui->m_cross_properties_gb->setDisabled(ro);
 }
 
 /**
@@ -96,5 +98,7 @@
 	}
 
 	ui->m_show_power_cb->setChecked(m_properties.showPowerContact());
-	ui->m_show_power_cb->setDisabled(!ui->m_display_has_cross_rb->isChecked());
+	ui->m_power_prefix_le->setText(m_properties.prefix("power"));
+	ui->m_delay_prefix_le->setText(m_properties.prefix("delay"));
+	ui->m_cross_properties_gb->setDisabled(!ui->m_display_has_cross_rb->isChecked());
 }

Modified: trunk/sources/ui/xrefpropertieswidget.ui
===================================================================
--- trunk/sources/ui/xrefpropertieswidget.ui	2014-05-06 23:38:06 UTC (rev 3059)
+++ trunk/sources/ui/xrefpropertieswidget.ui	2014-05-07 20:56:51 UTC (rev 3060)
@@ -42,25 +42,58 @@
     </widget>
    </item>
    <item>
-    <widget class="QCheckBox" name="m_show_power_cb">
-     <property name="text">
-      <string>Afficher les contacts de puissance dans la croix</string>
+    <widget class="QGroupBox" name="m_cross_properties_gb">
+     <property name="title">
+      <string>Option d'affichage en croix</string>
      </property>
+     <layout class="QVBoxLayout" name="verticalLayout_4">
+      <item>
+       <widget class="QCheckBox" name="m_show_power_cb">
+        <property name="text">
+         <string>Afficher les contacts de puissance dans la croix</string>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <layout class="QGridLayout" name="gridLayout">
+        <item row="1" column="1">
+         <widget class="QLineEdit" name="m_delay_prefix_le"/>
+        </item>
+        <item row="0" column="1">
+         <widget class="QLineEdit" name="m_power_prefix_le"/>
+        </item>
+        <item row="0" column="0">
+         <widget class="QLabel" name="label">
+          <property name="text">
+           <string>Préfixe des contacts de puissance:</string>
+          </property>
+         </widget>
+        </item>
+        <item row="1" column="0">
+         <widget class="QLabel" name="label_2">
+          <property name="text">
+           <string>Préfixe des contacts temporisés:</string>
+          </property>
+         </widget>
+        </item>
+       </layout>
+      </item>
+      <item>
+       <spacer name="verticalSpacer">
+        <property name="orientation">
+         <enum>Qt::Vertical</enum>
+        </property>
+        <property name="sizeHint" stdset="0">
+         <size>
+          <width>20</width>
+          <height>40</height>
+         </size>
+        </property>
+       </spacer>
+      </item>
+     </layout>
     </widget>
    </item>
-   <item>
-    <spacer name="verticalSpacer">
-     <property name="orientation">
-      <enum>Qt::Vertical</enum>
-     </property>
-     <property name="sizeHint" stdset="0">
-      <size>
-       <width>20</width>
-       <height>253</height>
-      </size>
-     </property>
-    </spacer>
-   </item>
   </layout>
  </widget>
  <resources/>


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