diff --git a/sources/diagram.cpp b/sources/diagram.cpp index 6a8c86568..357f1f49e 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -1014,13 +1014,18 @@ void Diagram::folioSequentialsFromXml(const QDomElement &root, QHashinitLink(project()); + for (DynamicElementTextItem *deti : elmt->dynamicTextItems()) + deti->refreshLabelConnection(); + } - foreach (Conductor *conductor, conductors()) + for (Conductor *conductor : conductors()) conductor->refreshText(); } diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.cpp b/sources/qetgraphicsitem/dynamicelementtextitem.cpp index 7cb555c0e..505b15448 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.cpp +++ b/sources/qetgraphicsitem/dynamicelementtextitem.cpp @@ -266,6 +266,21 @@ Element *DynamicElementTextItem::elementUseForInfo() const } } +/** + * @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 @@ DynamicElementTextItem::TextFrom DynamicElementTextItem::textFrom() const { void DynamicElementTextItem::setTextFrom(DynamicElementTextItem::TextFrom text_from) { setNoEditable(text_from == UserText? false : true); + clearFormulaConnection(); if(text_from == UserText) { @@ -290,14 +306,27 @@ void DynamicElementTextItem::setTextFrom(DynamicElementTextItem::TextFrom text_f } 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); } 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 @@ void DynamicElementTextItem::setInfoName(const QString &info_name) { 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 @@ void DynamicElementTextItem::setInfoName(const QString &info_name) conductorPropertiesChanged(); } } + else if (m_info_name == "label" && elementUseForInfo()) + { + setupFormulaConnection(); + updateLabel(); + } else if(elementUseForInfo()) { setPlainText(elementUseForInfo()->elementInformations().value(info_name).toString()); } @@ -398,6 +435,9 @@ void DynamicElementTextItem::setCompositeText(const QString &text) { 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 @@ void DynamicElementTextItem::setCompositeText(const QString &text) updateReportText(); } + else if (m_composite_text.contains("%{label}") && elementUseForInfo()) + { + setupFormulaConnection(); + updateLabel(); + } else { DiagramContext dc; @@ -615,11 +660,33 @@ void DynamicElementTextItem::elementInfoChanged() 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 @@ void DynamicElementTextItem::removeConnectionForReportFormula(const QString &for } +/** + * @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::updateReportText() } } +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 diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.h b/sources/qetgraphicsitem/dynamicelementtextitem.h index 4407f2e75..89f4010fe 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.h +++ b/sources/qetgraphicsitem/dynamicelementtextitem.h @@ -76,6 +76,7 @@ class DynamicElementTextItem : public DiagramTextItem Element *parentElement() const; Element *elementUseForInfo() const; + void refreshLabelConnection(); DynamicElementTextItem::TextFrom textFrom() const; void setTextFrom (DynamicElementTextItem::TextFrom text_from); @@ -104,8 +105,11 @@ class DynamicElementTextItem : public DiagramTextItem 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 @@ class DynamicElementTextItem : public DiagramTextItem DynamicElementTextItem::TextFrom m_text_from = UserText; QUuid m_uuid; QMetaObject::Connection m_report_formula_con; + QList m_formula_connection; QColor m_user_color; }; diff --git a/sources/qetgraphicsitem/element.h b/sources/qetgraphicsitem/element.h index 57132138c..715b76cda 100644 --- a/sources/qetgraphicsitem/element.h +++ b/sources/qetgraphicsitem/element.h @@ -149,6 +149,7 @@ class Element : public QetGraphicsItem void setPrefix(QString); QString getPrefix() const; void freezeLabel(bool freeze); + bool isFreezeLabel() const {return m_freeze_label;} void freezeNewAddedElement(); protected: