diff --git a/sources/diagram.cpp b/sources/diagram.cpp index cf018794c..69ffaa42b 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -1016,15 +1016,6 @@ void Diagram::addItem(QGraphicsItem *item) Element *elmt = static_cast(item); foreach(ElementTextItem *eti, elmt->texts()) connect (eti, &ElementTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged); - Element::kind linkType = elmt->linkType(); - if ((linkType == Element::Simple) || - (linkType == Element::Master) || - (linkType == Element::Slave) || - (linkType == Element::Terminale)) { - CustomElement *celmt = static_cast(item); - celmt->parseLabels(); - } - elmt->updateLabel(); } break; diff --git a/sources/diagramevent/diagrameventaddelement.cpp b/sources/diagramevent/diagrameventaddelement.cpp index 9261343e4..215eae3cb 100644 --- a/sources/diagramevent/diagrameventaddelement.cpp +++ b/sources/diagramevent/diagrameventaddelement.cpp @@ -189,7 +189,7 @@ bool DiagramEventAddElement::buildElement() //Everything is good return true; } - +#include "elementtextitem.h" /** * @brief DiagramEventAddElement::addElement * Add an element at the current pos en current rotation, @@ -237,8 +237,9 @@ void DiagramEventAddElement::addElement() conductor->setFreezeLabel(true); } }; + m_diagram -> undoStack().push(undo_object); - element->SetUpSequential(); - element->freezeNewAddedElement(); + element->setUpFormula(); element->updateLabel(); + element->freezeNewAddedElement(); } diff --git a/sources/qetgraphicsitem/commentitem.cpp b/sources/qetgraphicsitem/commentitem.cpp index be78f96c4..e0fb864ba 100644 --- a/sources/qetgraphicsitem/commentitem.cpp +++ b/sources/qetgraphicsitem/commentitem.cpp @@ -113,7 +113,6 @@ void CommentItem::updateLabel() QString location = m_element -> elementInformations()["location"].toString(); - QPainterPath m_shape_path_ = QPainterPath(); prepareGeometryChange(); m_bounding_rect = QRectF(); diff --git a/sources/qetgraphicsitem/customelement.cpp b/sources/qetgraphicsitem/customelement.cpp index e79f19ec2..323fd7116 100644 --- a/sources/qetgraphicsitem/customelement.cpp +++ b/sources/qetgraphicsitem/customelement.cpp @@ -694,18 +694,6 @@ bool CustomElement::parseText(QDomElement &e, QPainter &qp) { eti -> setFollowParentRotations(e.attribute("rotate") == "true"); list_texts_ << eti; - if (e.attribute("tagg")=="label") { - DiagramContext &dc = this->rElementInformations(); - dc.addValue("label", e.attribute("text")); - this->setElementInformations(dc); - this->setTaggedText("label", e.attribute("text")); - } - else if (e.attribute("tagg")=="function") { - DiagramContext &dc = this->rElementInformations(); - dc.addValue("function", e.attribute("text")); - this->setElementInformations(dc); - } - // Se positionne aux coordonnees indiquees dans la description du texte qp.setTransform(QTransform(), false); qp.translate(pos_x, pos_y); @@ -762,18 +750,19 @@ ElementTextItem *CustomElement::parseInput(QDomElement &e) { ElementTextItem *eti = new ElementTextItem(e.attribute("text"), this); eti -> setFont(QETApp::diagramTextsFont(size)); eti -> setTagg(e.attribute("tagg", "other")); + m_element_informations.addValue(e.attribute("tagg", "other"), e.attribute("text")); - // position the text field + // position the text field eti -> setOriginalPos(QPointF(pos_x, pos_y)); eti -> setPos(pos_x, pos_y); - // rotation of the text field + // rotation of the text field qreal original_rotation_angle = 0.0; QET::attributeIsAReal(e, "rotation", &original_rotation_angle); eti -> setOriginalRotationAngle(original_rotation_angle); eti -> setRotationAngle(original_rotation_angle); - // behavior when the parent element is rotated + // behavior when the parent element is rotated eti -> setFollowParentRotations(e.attribute("rotate") == "true"); list_texts_ << eti; diff --git a/sources/qetgraphicsitem/element.cpp b/sources/qetgraphicsitem/element.cpp index a0f538108..738b986c2 100644 --- a/sources/qetgraphicsitem/element.cpp +++ b/sources/qetgraphicsitem/element.cpp @@ -730,19 +730,45 @@ void Element::hoverLeaveEvent(QGraphicsSceneHoverEvent *e) { } /** - * @brief Element::SetUpSequential - * Setup the sequential value of this element + * @brief Element::setUpFormula + * Set up the formula used to create the label of this element + * @param : if true set tagged text to code letter (ex K for coil) with condition : + * formula is empty, text tagged "label" is emptty or "_"; */ -void Element::SetUpSequential() +void Element::setUpFormula(bool code_letter) { + if (linkType() == Element::Slave || linkType() & Element::AllReport) + return; + if (diagram()) { - QString element_currentAutoNum = diagram()->project()->elementCurrentAutoNum(); - NumerotationContext nc = diagram()->project()->elementAutoNum(element_currentAutoNum); - NumerotationContextCommands ncc (nc); + QString formula = diagram()->project()->elementAutoNumCurrentFormula(); - autonum::setSequential(elementInformations()["label"].toString(), m_autoNum_seq, nc, diagram(), element_currentAutoNum); - diagram()->project()->addElementAutoNum(element_currentAutoNum, ncc.next()); + if (formula.isEmpty()) + { + if (code_letter && !m_prefix.isEmpty()) + { + if (ElementTextItem *eti = taggedText("label")) + { + QString text = eti->toPlainText(); + if (text.isEmpty() || text == "_") + { + m_element_informations.addValue("formula", "%prefix"); + } + } + } + } + else + { + m_element_informations.addValue("formula", formula); + + QString element_currentAutoNum = diagram()->project()->elementCurrentAutoNum(); + NumerotationContext nc = diagram()->project()->elementAutoNum(element_currentAutoNum); + NumerotationContextCommands ncc (nc); + + autonum::setSequential(formula, m_autoNum_seq, nc, diagram(), element_currentAutoNum); + diagram()->project()->addElementAutoNum(element_currentAutoNum, ncc.next()); + } } } @@ -824,3 +850,26 @@ void Element::freezeNewAddedElement() { } else return; } + +/** + * @brief Element::setUpConnectionForFormula + * setup connection according to the variable of formula + * @param old_formula + * @param new_formula + */ +void Element::setUpConnectionForFormula(QString old_formula, QString new_formula) +{ + if (diagram() && (old_formula.contains("%f") || old_formula.contains("%id"))) + disconnect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &Element::updateLabel); + if (old_formula.contains("%l")) + disconnect(this, &Element::yChanged, this, &Element::updateLabel); + if (old_formula.contains("%c")) + disconnect(this, &Element::xChanged, this, &Element::updateLabel); + + if (diagram() && (new_formula.contains("%f") || new_formula.contains("%id"))) + connect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &Element::updateLabel); + if (new_formula.contains("%l")) + connect(this, &Element::yChanged, this, &Element::updateLabel); + if (new_formula.contains("%c")) + connect(this, &Element::xChanged, this, &Element::updateLabel); +} diff --git a/sources/qetgraphicsitem/element.h b/sources/qetgraphicsitem/element.h index 4d69754c5..a0d368511 100644 --- a/sources/qetgraphicsitem/element.h +++ b/sources/qetgraphicsitem/element.h @@ -32,7 +32,8 @@ class NumerotationContext; /** This is the base class for electrical elements. */ -class Element : public QetGraphicsItem { +class Element : public QetGraphicsItem +{ Q_OBJECT // constructors, destructor @@ -139,14 +140,17 @@ class Element : public QetGraphicsItem { autonum::sequenceStruct sequenceStruct () const {return m_autoNum_seq;} autonum::sequenceStruct& rSequenceStruct() {return m_autoNum_seq;} - void SetUpSequential (); + void setUpFormula(bool code_letter = true); void setPrefix(QString); QString getPrefix() const; void freezeLabel(); void unfreezeLabel(); void freezeNewAddedElement(); - //ATTRIBUTES + protected: + void setUpConnectionForFormula(QString old_formula, QString new_formula); + + //ATTRIBUTES protected: DiagramContext m_element_informations, kind_informations_; autonum::sequenceStruct m_autoNum_seq; diff --git a/sources/qetgraphicsitem/masterelement.cpp b/sources/qetgraphicsitem/masterelement.cpp index 624c5e432..e15855aa9 100644 --- a/sources/qetgraphicsitem/masterelement.cpp +++ b/sources/qetgraphicsitem/masterelement.cpp @@ -31,13 +31,11 @@ */ MasterElement::MasterElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) : CustomElement(location, qgi, state), - cri_ (nullptr) + m_Xref_item (nullptr) { link_type_ = Master; connect(this, SIGNAL(elementInfoChange(DiagramContext, DiagramContext)), this, SLOT(updateLabel(DiagramContext, DiagramContext))); - connect(this, SIGNAL(xChanged()), this, SLOT(changeElementInfo())); - connect(this, SIGNAL(yChanged()), this, SLOT(changeElementInfo())); - connect(this, SIGNAL(updateLabel()), this, SLOT(changeElementInfo())); + connect(this, &Element::updateLabel, [this]() {this->updateLabel(this->elementInformations(), this->elementInformations());}); } /** @@ -62,12 +60,12 @@ void MasterElement::linkToElement(Element *elmt) connected_elements << elmt; elmt->linkToElement(this); - if (!cri_) cri_ = new CrossRefItem(this); //create cross ref item if not yet + if (!m_Xref_item) m_Xref_item = new CrossRefItem(this); //create cross ref item if not yet - connect(elmt, SIGNAL(xChanged()), cri_, SLOT(updateLabel())); - connect(elmt, SIGNAL(yChanged()), cri_, SLOT(updateLabel())); - connect(elmt, SIGNAL(updateLabel()), cri_, SLOT(updateLabel())); - cri_ -> updateLabel(); + connect(elmt, SIGNAL(xChanged()), m_Xref_item, SLOT(updateLabel())); + connect(elmt, SIGNAL(yChanged()), m_Xref_item, SLOT(updateLabel())); + connect(elmt, SIGNAL(updateLabel()), m_Xref_item, SLOT(updateLabel())); + m_Xref_item -> updateLabel(); emit linkedElementChanged(); } } @@ -102,11 +100,11 @@ void MasterElement::unlinkElement(Element *elmt) elmt -> setHighlighted (false); //update the graphics cross ref - disconnect(elmt, SIGNAL(xChanged()), cri_, SLOT(updateLabel())); - disconnect(elmt, SIGNAL(yChanged()), cri_, SLOT(updateLabel())); - disconnect(elmt, SIGNAL(updateLabel()), cri_, SLOT(updateLabel())); + disconnect(elmt, SIGNAL(xChanged()), m_Xref_item, SLOT(updateLabel())); + disconnect(elmt, SIGNAL(yChanged()), m_Xref_item, SLOT(updateLabel())); + disconnect(elmt, SIGNAL(updateLabel()), m_Xref_item, SLOT(updateLabel())); - cri_ -> updateLabel(); + m_Xref_item -> updateLabel(); aboutDeleteXref(); emit linkedElementChanged(); } @@ -124,27 +122,6 @@ void MasterElement::initLink(QETProject *project) { updateLabel(DiagramContext(), elementInformations()); } -/** - * @brief MasterElement::folioIdChange - * Used to update the label of this item when the folio id change - */ -void MasterElement::folioIdChange() { - DiagramContext dc =elementInformations(); - setTaggedText("label", autonum::AssignVariables::formulaToLabel(dc["label"].toString(), m_autoNum_seq, diagram(), this), true); -} - -/** - * @brief MasterElement::changeElementInfo() - * Update label if it contains %c, %l, %f or %F variables - */ -void MasterElement::changeElementInfo(){ - QString temp_label = this->elementInformations()["label"].toString(); - if (temp_label.contains("\%")) { - if (this->diagram()!=NULL) - this->updateLabel(this->elementInformations(),this->elementInformations()); - } -} - /** * @brief MasterElement::updateLabel * update label of this element @@ -152,49 +129,33 @@ void MasterElement::changeElementInfo(){ */ void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info) { - const QString old_label = old_info["label"].toString(); - const QString new_label = new_info["label"].toString(); + QString old_formula = old_info["formula"].toString(); + QString new_formula = new_info["formula"].toString(); - QString newstr = autonum::AssignVariables::formulaToLabel(new_label, m_autoNum_seq, diagram(), this); + setUpConnectionForFormula(old_formula, new_formula); - ElementTextItem *eti = taggedText("label"); + QString label = autonum::AssignVariables::formulaToLabel(new_formula, m_autoNum_seq, diagram(), this); - //Label of element - if (eti && (eti->toPlainText() != newstr)) + if (label.isEmpty()) { - if (new_label.isEmpty()) - { - setTaggedText("label", "_", false); - } - else - { - setTaggedText("label", newstr, true); - } - - //If autonum formula have %id %f or %F (because %F can itself contain %id or %f), - //we connect the change of folio position, to keep up to date the label. - if (diagram() && diagram()->project()) - { - if (old_label.contains(QRegularExpression("%id|%f|%F")) && !new_label.contains(QRegularExpression("%id|%f|%F"))) - { - disconnect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &MasterElement::folioIdChange); - } - else if (new_label.contains(QRegularExpression("%id|%f|%F"))) - { - connect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &MasterElement::folioIdChange); - } - } + setTaggedText("label", new_info["label"].toString()); + } + else + { + bool visible = m_element_informations.contains("label") ? m_element_informations.keyMustShow("label") : true; + m_element_informations.addValue("label", label, visible); + setTaggedText("label", label); } - if (eti) + if (ElementTextItem *eti = taggedText("label")) { - new_label.isEmpty() ? eti->setVisible(true) : eti -> setVisible(new_info.keyMustShow("label")); + new_info["label"].toString().isEmpty() ? eti->setVisible(true) : eti -> setVisible(new_info.keyMustShow("label")); } //Delete or update the xref - if (cri_) { - cri_ -> updateLabel(); + if (m_Xref_item) { + m_Xref_item -> updateLabel(); aboutDeleteXref(); } else { @@ -205,7 +166,7 @@ void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info bool must_show_location = elementInformations().keyMustShow("location"); if (! (comment.isEmpty() || !must_show_comment) || !(location.isEmpty() || !must_show_location)) { - cri_ = new CrossRefItem(this); + m_Xref_item = new CrossRefItem(this); } } } @@ -221,12 +182,12 @@ void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info * @return */ bool MasterElement::aboutDeleteXref() { - if(!cri_) return true; + if(!m_Xref_item) return true; if(!linkedElements().isEmpty()) return false; - if (cri_ -> boundingRect().isNull()) { - delete cri_; - cri_ = nullptr; + if (m_Xref_item -> boundingRect().isNull()) { + delete m_Xref_item; + m_Xref_item = nullptr; return true; } diff --git a/sources/qetgraphicsitem/masterelement.h b/sources/qetgraphicsitem/masterelement.h index 330699fcf..7a7f0a1bf 100644 --- a/sources/qetgraphicsitem/masterelement.h +++ b/sources/qetgraphicsitem/masterelement.h @@ -40,21 +40,15 @@ class MasterElement : public CustomElement virtual void unlinkAllElements (); virtual void unlinkElement (Element *elmt); virtual void initLink (QETProject *project); - - signals: - - private: - void folioIdChange(); public slots: void updateLabel(DiagramContext old_info, DiagramContext new_info); - void changeElementInfo(); private: bool aboutDeleteXref (); private: - CrossRefItem *cri_; + CrossRefItem *m_Xref_item; }; #endif // MASTERELEMENT_H diff --git a/sources/qetgraphicsitem/simpleelement.cpp b/sources/qetgraphicsitem/simpleelement.cpp index ba0c4c595..b12120d4f 100644 --- a/sources/qetgraphicsitem/simpleelement.cpp +++ b/sources/qetgraphicsitem/simpleelement.cpp @@ -34,9 +34,7 @@ SimpleElement::SimpleElement(const ElementsLocation &location, QGraphicsItem *qg { link_type_ = Simple; connect(this, SIGNAL(elementInfoChange(DiagramContext, DiagramContext)), this, SLOT(updateLabel(DiagramContext, DiagramContext))); - connect(this, SIGNAL(xChanged()),this, SLOT(changeElementInfo())); - connect(this, SIGNAL(yChanged()),this, SLOT(changeElementInfo())); - connect(this, SIGNAL(updateLabel()),this,SLOT(changeElementInfo())); + connect(this, &Element::updateLabel, [this]() {this->updateLabel(this->elementInformations(), this->elementInformations());}); } /** @@ -57,60 +55,36 @@ void SimpleElement::initLink(QETProject *project) { updateLabel(DiagramContext(), elementInformations()); } -/** - * @brief SimpleElement::folioIdChange - * Use to update the label of this item when the foio id change - */ -void SimpleElement::folioIdChange() -{ - DiagramContext dc =elementInformations(); - setTaggedText("label", autonum::AssignVariables::formulaToLabel(dc["label"].toString(), m_autoNum_seq, diagram(), this)); -} - -/** - * @brief SimpleElement::changeElementInfo() - * Update label if it contains %c, %l, %f or %F variables - */ -void SimpleElement::changeElementInfo(){ - QString temp_label = this->elementInformations()["label"].toString(); - if (temp_label.contains("\%")) { - if (this->diagram()!=NULL) - this->updateLabel(this->elementInformations(),this->elementInformations()); - } -} - /** * @brief SimpleElement::updateLabel * update label of this element */ void SimpleElement::updateLabel(DiagramContext old_info, DiagramContext new_info) { - QString label = autonum::AssignVariables::formulaToLabel(new_info["label"].toString(), m_autoNum_seq, diagram(), this); + QString old_formula = old_info["formula"].toString(); + QString new_formula = new_info["formula"].toString(); - //Label of element - if (old_info["label"].toString() != label) { - if (new_info["label"].toString().isEmpty()) - setTaggedText("label", "_", false); - else { - setTaggedText("label", label, true); - } + setUpConnectionForFormula(old_formula, new_formula); - //If autonum formula have %id we connect the change of folio position, to keep up to date the label. - if (diagram() && diagram()->project()) { - if (old_info["label"].toString().contains("%id") && !new_info["label"].toString().contains("%id")) { - disconnect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &SimpleElement::folioIdChange); - } - else if (new_info["label"].toString().contains("%id")) { - connect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &SimpleElement::folioIdChange); - } + QString label = autonum::AssignVariables::formulaToLabel(new_formula, m_autoNum_seq, diagram(), this); + + if (label.isEmpty()) + { + setTaggedText("label", new_info["label"].toString()); } + else + { + bool visible = m_element_informations.contains("label") ? m_element_informations.keyMustShow("label") : true; + m_element_informations.addValue("label", label, visible); + setTaggedText("label", label); } - if (ElementTextItem *eti = taggedText("label")) { + if (ElementTextItem *eti = taggedText("label")) + { new_info["label"].toString().isEmpty() ? eti->setVisible(true) : eti -> setVisible(new_info.keyMustShow("label")); } - //Comment and Location of element + //Comment and Location of element QString comment = new_info["comment"].toString(); bool must_show = new_info.keyMustShow("comment"); QString location = new_info["location"].toString(); diff --git a/sources/qetgraphicsitem/simpleelement.h b/sources/qetgraphicsitem/simpleelement.h index 5282942ad..c1ac916c2 100644 --- a/sources/qetgraphicsitem/simpleelement.h +++ b/sources/qetgraphicsitem/simpleelement.h @@ -37,14 +37,8 @@ class SimpleElement : public CustomElement { virtual void initLink(QETProject *project); - signals: - - private: - void folioIdChange(); - public slots: void updateLabel(DiagramContext old_info, DiagramContext new_info); - void changeElementInfo(); private: CommentItem *m_comment_item;