diff --git a/sources/diagram.cpp b/sources/diagram.cpp index 13813caa7..b53dc64bc 100644 --- a/sources/diagram.cpp +++ b/sources/diagram.cpp @@ -1011,14 +1011,6 @@ void Diagram::addItem(QGraphicsItem *item) switch (item->type()) { - case Element::Type: - { - Element *elmt = static_cast(item); - foreach(ElementTextItem *eti, elmt->texts()) - connect (eti, &ElementTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged); - } - break; - case Conductor::Type: { Conductor *conductor = static_cast(item); @@ -1048,15 +1040,6 @@ void Diagram::removeItem(QGraphicsItem *item) switch (item->type()) { - case Element::Type: - { - Element *elmt = static_cast(item); - elmt->unlinkAllElements(); - foreach(ElementTextItem *text, elmt->texts()) - disconnect(text, &ElementTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged); - } - break; - case Conductor::Type: { Conductor *conductor = static_cast(item); diff --git a/sources/qetgraphicsitem/customelement.cpp b/sources/qetgraphicsitem/customelement.cpp index 323fd7116..7556165aa 100644 --- a/sources/qetgraphicsitem/customelement.cpp +++ b/sources/qetgraphicsitem/customelement.cpp @@ -265,59 +265,6 @@ void CustomElement::paint(QPainter *qp, const QStyleOptionGraphicsItem *options) } } -/** - Retrieves the element label stored in file ./elements/ * /qet_labels.xml - The labels are applied to all elements inside a folder. If an element - has a specific label, it will be applied. See qet_labels.xml for more - instructions. -*/ -void CustomElement::parseLabels() -{ - QString formula = location().project()->elementAutoNumCurrentFormula(); - DiagramContext &dc = rElementInformations(); - - //element is being added - if (taggedText("label") && (location().projectId()!=-1) && (taggedText("label")->toPlainText()=="_")) - { - if (getPrefix().isEmpty()) - { - if (!formula.isEmpty() && (this->linkType() != Element::Slave)) - { - dc.addValue("label", formula); - this->setTaggedText("label",formula); - } - } - else - { - //if there is a formula to assign, assign it - if (!formula.isEmpty() && (this->linkType() != Element::Slave)) - { - dc.addValue("label", formula); - this->setTaggedText("label",formula); - } - else - { //assign only prefix - dc.addValue("label", "%prefix"); - this->setTaggedText("label", getPrefix()); - } - } - } - - //apply formula to specific label - This condition specify elements which have different labels e.g KM - //that are already specified in the element label (inside .elmt file). This method is not called if elements - //are being loaded at first time or being pasted - else if ((this->taggedText("label")!= NULL) && (location().projectId()!=-1) && - (!location().project()->elementAutoNumCurrentFormula().isEmpty()) && - (this->linkType()!=Element::Slave) && !this->diagram()->item_paste) - { - QString prefix = this->taggedText("label")->toPlainText(); - this->setPrefix(prefix); - dc.addValue("label", formula); - this->setTaggedText("label",formula); - this->setElementInformations(dc); - } -} - /** Analyse et prend en compte un element XML decrivant une partie du dessin de l'element perso. Si l'analyse reussit, la partie est ajoutee au dessin. @@ -767,6 +714,8 @@ ElementTextItem *CustomElement::parseInput(QDomElement &e) { list_texts_ << eti; + connect(eti, &ElementTextItem::diagramTextChanged, this, &Element::textItemChanged); + return(eti); } diff --git a/sources/qetgraphicsitem/customelement.h b/sources/qetgraphicsitem/customelement.h index 74dfbf365..56bba1cb2 100644 --- a/sources/qetgraphicsitem/customelement.h +++ b/sources/qetgraphicsitem/customelement.h @@ -72,7 +72,6 @@ class CustomElement : public FixedElement virtual QList *> arcs() const; virtual int terminalsCount() const; virtual void paint(QPainter *, const QStyleOptionGraphicsItem *); - virtual void parseLabels(); QString typeId() const; ElementsLocation location() const; QString name() const; diff --git a/sources/qetgraphicsitem/element.cpp b/sources/qetgraphicsitem/element.cpp index 0732b94a0..6721ec657 100644 --- a/sources/qetgraphicsitem/element.cpp +++ b/sources/qetgraphicsitem/element.cpp @@ -28,6 +28,7 @@ #include "elementpropertieswidget.h" #include "numerotationcontextcommands.h" #include "diagramcontext.h" +#include "changeelementinformationcommand.h" class ElementXmlRetroCompatibility { @@ -436,12 +437,15 @@ bool Element::fromXml(QDomElement &e, QHash &table_id_adr, bool table_id_adr.insert(id_trouve, priv_id_adr.value(id_trouve)); } } - - // importe les valeurs des champs de texte + + //import text filed value QList inputs = QET::findInDomElement(e, "inputs", "input"); - foreach(QGraphicsItem *qgi, childItems()) { - if (ElementTextItem *eti = qgraphicsitem_cast(qgi)) { - foreach(QDomElement input, inputs) { + foreach(QGraphicsItem *qgi, childItems()) + { + if (ElementTextItem *eti = qgraphicsitem_cast(qgi)) + { + foreach(QDomElement input, inputs) + { eti -> fromXml(input); etiToElementLabels(eti); } @@ -778,6 +782,40 @@ ElementTextItem* Element::setTaggedText(const QString &tagg, const QString &news return eti; } +/** + * @brief Element::textItemChanged + * Use to keep up to date the element information when text item changed. + * @param dti + * @param old_str + * @param new_str + */ +void Element::textItemChanged(DiagramTextItem *dti, QString old_str, QString new_str) +{ + Q_UNUSED(new_str) + + if (!diagram()) + return; + + ElementTextItem *eti = qgraphicsitem_cast(dti); + if (!eti) + return; + + QString tagg = eti->tagg(); + if (m_element_informations.contains(tagg)) + { + DiagramContext dc = m_element_informations; + dc.addValue(tagg, eti->toPlainText(), dc.keyMustShow(tagg)); + if (tagg == "label") + dc.addValue("formula", eti->toPlainText(), dc.keyMustShow("formula")); + + diagram()->undoStack().push(new ChangeElementInformationCommand(this, m_element_informations, dc)); + } + else + { + diagram()->undoStack().push(new ChangeDiagramTextCommand(eti, old_str, eti->toPlainText())); + } +} + /** * @brief Element::getPrefix * get Element Prefix diff --git a/sources/qetgraphicsitem/element.h b/sources/qetgraphicsitem/element.h index 746313e9a..088c08e1f 100644 --- a/sources/qetgraphicsitem/element.h +++ b/sources/qetgraphicsitem/element.h @@ -28,6 +28,7 @@ class QETProject; class Terminal; class Conductor; class NumerotationContext; +class DiagramTextItem; /** This is the base class for electrical elements. @@ -46,8 +47,7 @@ class Element : public QetGraphicsItem // attributes public: /** - * Enable the use of qgraphicsitem_cast to safely cast - * a QGraphicsItem into an Element. + * Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into an Element. * @return the QGraphicsItem type */ enum { Type = UserType + 1000 }; @@ -72,30 +72,30 @@ class Element : public QetGraphicsItem // methods public: - /// @return the list of terminals for this element - virtual QList terminals() const = 0; - /// @return the list of conductors attached to this element - virtual QList conductors() const = 0; - /// @return the list of text items attached to this element - virtual QList texts() const = 0; - /// @return the text field tagged with @tagg or NULL if text field isn't found - virtual ElementTextItem* taggedText(const QString &tagg) const = 0; - /// @return the list of lines items in this element - virtual QList lines() const = 0; - /// @return the list of rectangles items in this element - virtual QList rectangles() const = 0; - /// @return the list of bounding rectangles for circles items in this element - virtual QList circles() const = 0; - /// @return the list of polygons in this element - virtual QList *> polygons() const = 0; - /// @return the list of arcs in this element - virtual QList *> arcs() const = 0; - /// @return the current number of terminals of this element - virtual int terminalsCount() const = 0; - /// @return the minimum number of terminals for this element - virtual int minTerminalsCount() const = 0; - /// @return the maximum number of terminals for this element - virtual int maxTerminalsCount() const = 0; + /// @return the list of terminals for this element + virtual QList terminals() const = 0; + /// @return the list of conductors attached to this element + virtual QList conductors() const = 0; + /// @return the list of text items attached to this element + virtual QList texts() const = 0; + /// @return the text field tagged with @tagg or NULL if text field isn't found + virtual ElementTextItem* taggedText(const QString &tagg) const = 0; + /// @return the list of lines items in this element + virtual QList lines() const = 0; + /// @return the list of rectangles items in this element + virtual QList rectangles() const = 0; + /// @return the list of bounding rectangles for circles items in this element + virtual QList circles() const = 0; + /// @return the list of polygons in this element + virtual QList *> polygons() const = 0; + /// @return the list of arcs in this element + virtual QList *> arcs() const = 0; + /// @return the current number of terminals of this element + virtual int terminalsCount() const = 0; + /// @return the minimum number of terminals for this element + virtual int minTerminalsCount() const = 0; + /// @return the maximum number of terminals for this element + virtual int maxTerminalsCount() const = 0; QList > AlignedFreeTerminals () const; @@ -130,6 +130,7 @@ class Element : public QetGraphicsItem //METHODS related to information public: + void textItemChanged(DiagramTextItem *dti, QString old_str, QString new_str); DiagramContext elementInformations ()const {return m_element_informations;} DiagramContext& rElementInformations () {return m_element_informations;} virtual void setElementInformations (DiagramContext dc);