[qet] qet/qet: [5086] Dynamic element text item : when the source of text is label or composite text with variable %{label} , and the label of the element is created from a formula,

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


Revision: 5086
Author:   blacksun
Date:     2017-10-26 19:12:49 +0200 (Thu, 26 Oct 2017)
Log Message:
-----------
Dynamic element text item : when the source of text is label or composite text with variable %{label}, and the label of the element is created from a formula,
the displayed text of the dynamic text item is up to date according to the formula 

Modified Paths:
--------------
    trunk/sources/diagram.cpp
    trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp
    trunk/sources/qetgraphicsitem/dynamicelementtextitem.h
    trunk/sources/qetgraphicsitem/element.h

Modified: trunk/sources/diagram.cpp
===================================================================
--- trunk/sources/diagram.cpp	2017-10-23 12:53:00 UTC (rev 5085)
+++ trunk/sources/diagram.cpp	2017-10-26 17:12:49 UTC (rev 5086)
@@ -1014,13 +1014,18 @@
  * refresh all content of diagram.
  * - refresh conductor text.
  * - linking the elements waiting to be linked
+ * - Refresh the connection of the dynamic element text item (use for text with source of text label)
  */
-void Diagram::refreshContents() {
-
-	foreach (Element *elmt, elements())
+void Diagram::refreshContents()
+{
+	for (Element *elmt : elements())
+	{
 		elmt->initLink(project());
+		for (DynamicElementTextItem *deti : elmt->dynamicTextItems())
+			deti->refreshLabelConnection();
+	}
 
-	foreach (Conductor *conductor, conductors())
+	for (Conductor *conductor : conductors())
 		conductor->refreshText();
 }
 

Modified: trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp	2017-10-23 12:53:00 UTC (rev 5085)
+++ trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp	2017-10-26 17:12:49 UTC (rev 5086)
@@ -267,6 +267,21 @@
 }
 
 /**
+ * @brief DynamicElementTextItem::refreshLabelConnection
+ * Refresh the connection of this text when the source of text is label,
+ * or composite text, with a variable %{label}
+ */
+void DynamicElementTextItem::refreshLabelConnection()
+{
+	if ((m_text_from == ElementInfo && m_info_name == "label") ||
+		(m_text_from == CompositeText && m_composite_text.contains("%{label}")))
+	{
+		setupFormulaConnection();
+		updateLabel();
+	}
+}
+
+/**
  * @brief DynamicElementTextItem::textFrom
  * @return what the final text is created from.
  */
@@ -282,6 +297,7 @@
 void DynamicElementTextItem::setTextFrom(DynamicElementTextItem::TextFrom text_from)
 {
 	setNoEditable(text_from == UserText? false : true);
+	clearFormulaConnection();
 	
 	if(text_from == UserText)
 	{
@@ -290,7 +306,13 @@
 	}
 	else if (text_from == ElementInfo && elementUseForInfo())
 	{
-		setPlainText(elementUseForInfo()->elementInformations().value(m_info_name).toString());
+		if(m_info_name == "label")
+		{
+			setupFormulaConnection();
+			updateLabel();
+		}
+		else
+			setPlainText(elementUseForInfo()->elementInformations().value(m_info_name).toString());
 		
 		if(m_text_from == UserText)
 			connect(elementUseForInfo(), &Element::elementInfoChange, this, &DynamicElementTextItem::elementInfoChanged);
@@ -297,7 +319,14 @@
 	}
 	else if (text_from == CompositeText && elementUseForInfo())
 	{
-		setPlainText(autonum::AssignVariables::replaceVariable(m_composite_text, elementUseForInfo()->elementInformations()));
+		if(m_composite_text.contains("%{label}"))
+		{
+			setupFormulaConnection();
+			updateLabel();
+		}
+		else
+			setPlainText(autonum::AssignVariables::replaceVariable(m_composite_text, elementUseForInfo()->elementInformations()));
+		
 		if(m_text_from == UserText)
 			connect(elementUseForInfo(), &Element::elementInfoChange, this, &DynamicElementTextItem::elementInfoChanged);
 	}
@@ -357,6 +386,9 @@
 {
 	QString old_info_name = m_info_name;
 	m_info_name = info_name;
+	
+	if(old_info_name == "label")
+		clearFormulaConnection();
 
 	if (m_parent_element && (m_parent_element.data()->linkType() & Element::AllReport)) //special treatment for report
 	{
@@ -374,6 +406,11 @@
 				conductorPropertiesChanged();
 		}
 	}
+	else if (m_info_name == "label" && elementUseForInfo())
+	{
+		setupFormulaConnection();
+		updateLabel();
+	}
 	else if(elementUseForInfo()) {
 		setPlainText(elementUseForInfo()->elementInformations().value(info_name).toString());
 	}
@@ -398,6 +435,9 @@
 {
 	QString old_composite_text = m_composite_text;
 	m_composite_text = text;
+	
+	if(old_composite_text.contains("%{label}"))
+		clearFormulaConnection();
 
 	if (m_parent_element && (m_parent_element.data()->linkType() & Element::AllReport)) //special treatment for report
 	{
@@ -414,6 +454,11 @@
 		
 		updateReportText();
 	}
+	else if (m_composite_text.contains("%{label}") && elementUseForInfo())
+	{
+		setupFormulaConnection();
+		updateLabel();
+	}
 	else
 	{
 		DiagramContext dc;
@@ -615,11 +660,33 @@
 		dc = elementUseForInfo()->elementInformations();
 	
 	QString final_text;
+	Element *element = elementUseForInfo();
 
 	if (m_text_from == ElementInfo)
-		final_text = dc.value(m_info_name).toString();
+	{
+			//If the info is the label, then we must to make some connection
+			//if the label is created from a formula
+		if(m_info_name == "label")
+		{
+			setupFormulaConnection();
+			
+			if (dc.value("formula").toString().isEmpty())
+				final_text = dc.value(m_info_name).toString();
+			else
+				final_text = autonum::AssignVariables::formulaToLabel(dc.value("formula").toString(), element->rSequenceStruct(), element->diagram(), element);
+		}
+		else
+			final_text = dc.value(m_info_name).toString();
+	}
 	else if (m_text_from == CompositeText)
+	{
+			//If the composite have the label variable, we must to make some
+			//connection if the label is created from a formula
+		if (m_composite_text.contains("%{label}"))
+			setupFormulaConnection();
+		
 		final_text = autonum::AssignVariables::replaceVariable(m_composite_text, dc);
+	}
 	else if (m_text_from  == UserText)
 		final_text = m_text;
 	
@@ -758,6 +825,52 @@
 	
 }
 
+/**
+ * @brief DynamicElementTextItem::setupFormulaConnection
+ * Setup the required connection for the formula of the label.
+ */
+void DynamicElementTextItem::setupFormulaConnection()
+{
+	if ((m_text_from == ElementInfo && m_info_name == "label") ||
+		(m_text_from == CompositeText && m_composite_text.contains("%{label}")))
+	{
+		clearFormulaConnection();
+		
+		Element *element = elementUseForInfo();
+		if (!element)
+			return;
+		
+		Diagram *diagram = element->diagram();
+		QString formula = element->elementInformations().value("formula").toString();
+
+			//Label is frozen, so we don't update it.
+		if (element->isFreezeLabel())
+			return;
+		
+		if (diagram && formula.contains("%F"))
+		{
+			m_F_str = diagram->border_and_titleblock.folio();
+			formula.replace("%F", m_F_str);
+			m_formula_connection << connect(&diagram->border_and_titleblock, &BorderTitleBlock::titleBlockFolioChanged, this, &DynamicElementTextItem::updateLabel);
+		}
+		
+		if (diagram && (formula.contains("%f") || formula.contains("%id")))
+			m_formula_connection << connect(diagram->project(), &QETProject::projectDiagramsOrderChanged, this, &DynamicElementTextItem::updateLabel);
+		if (formula.contains("%l"))
+			m_formula_connection << connect(element, &Element::yChanged, this, &DynamicElementTextItem::updateLabel);
+		if (formula.contains("%c"))
+			m_formula_connection << connect(element, &Element::xChanged, this, &DynamicElementTextItem::updateLabel);
+			
+	}
+}
+
+void DynamicElementTextItem::clearFormulaConnection()
+{
+	for (QMetaObject::Connection con : m_formula_connection)
+		disconnect(con);
+	m_formula_connection.clear();
+}
+
 void DynamicElementTextItem::updateReportFormulaConnection()
 {
 	removeConnectionForReportFormula(m_report_formula);
@@ -783,6 +896,24 @@
 	}
 }
 
+void DynamicElementTextItem::updateLabel()
+{
+	if ((m_text_from == ElementInfo && m_info_name == "label") ||
+		(m_text_from == CompositeText && m_composite_text.contains("%{label}")))
+	{
+		DiagramContext dc;
+		if(elementUseForInfo())
+			dc = elementUseForInfo()->elementInformations();
+		
+		Element *element = elementUseForInfo();
+		
+		if(m_text_from == ElementInfo)
+			setPlainText(autonum::AssignVariables::formulaToLabel(dc.value("formula").toString(), element->rSequenceStruct(), element->diagram(), element));
+		else if (m_text_from == CompositeText)
+			setPlainText(autonum::AssignVariables::replaceVariable(m_composite_text, dc));
+	}
+}
+
 /**
  * @brief DynamicElementTextItem::conductorWasAdded
  * Function only use when parent element is a folio report

Modified: trunk/sources/qetgraphicsitem/dynamicelementtextitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/dynamicelementtextitem.h	2017-10-23 12:53:00 UTC (rev 5085)
+++ trunk/sources/qetgraphicsitem/dynamicelementtextitem.h	2017-10-26 17:12:49 UTC (rev 5086)
@@ -76,6 +76,7 @@
 		
 		Element *parentElement() const;
 		Element *elementUseForInfo() const;
+		void refreshLabelConnection();
 		
 		DynamicElementTextItem::TextFrom textFrom() const;
 		void setTextFrom (DynamicElementTextItem::TextFrom text_from);
@@ -104,8 +105,11 @@
 		void reportFormulaChanged();
 		void setConnectionForReportFormula(const QString &formula);
 		void removeConnectionForReportFormula(const QString &formula);
+		void setupFormulaConnection();
+		void clearFormulaConnection();
 		void updateReportFormulaConnection();
 		void updateReportText();
+		void updateLabel();
 		void conductorWasAdded(Conductor *conductor);
 		void conductorWasRemoved(Conductor *conductor);
 		void setPotentialConductor();
@@ -126,6 +130,7 @@
 		DynamicElementTextItem::TextFrom m_text_from = UserText;
 		QUuid m_uuid;
 		QMetaObject::Connection m_report_formula_con;
+		QList<QMetaObject::Connection> m_formula_connection;
 		QColor m_user_color;
 };
 

Modified: trunk/sources/qetgraphicsitem/element.h
===================================================================
--- trunk/sources/qetgraphicsitem/element.h	2017-10-23 12:53:00 UTC (rev 5085)
+++ trunk/sources/qetgraphicsitem/element.h	2017-10-26 17:12:49 UTC (rev 5086)
@@ -149,6 +149,7 @@
 		void setPrefix(QString);
 		QString getPrefix() const;
 		void freezeLabel(bool freeze);
+		bool isFreezeLabel() const {return m_freeze_label;}
 		void freezeNewAddedElement();
 
 	protected:


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