diff --git a/sources/editor/graphicspart/partdynamictextfield.cpp b/sources/editor/graphicspart/partdynamictextfield.cpp index 1ec9018fe..5522ad5d2 100644 --- a/sources/editor/graphicspart/partdynamictextfield.cpp +++ b/sources/editor/graphicspart/partdynamictextfield.cpp @@ -23,12 +23,26 @@ #include #include #include +#include /** * @brief PartDynamicTextField::PartDynamicTextField + * Return if a dynamic text field can import information from the xml definition of a text field * @param editor * @param parent */ +bool PartDynamicTextField::canImportFromTextField(const QDomElement &dom_element) +{ + if(dom_element.tagName() != "input") + return false; + + QString tagg = dom_element.attribute("tagg", "none"); + if(tagg == "none") + return true; + else + return false; +} + PartDynamicTextField::PartDynamicTextField(QETElementEditor *editor, QGraphicsItem *parent) : QGraphicsTextItem(parent), CustomElementPart(editor), @@ -184,6 +198,35 @@ void PartDynamicTextField::fromXml(const QDomElement &dom_elmt) setColor(QColor(dom_color.text())); } +/** + * @brief PartDynamicTextField::fromTextFieldXml + * Setup this text from the xml definition of a text field (The xml tagg of a text field is "input"); + * @param dom_element + */ +void PartDynamicTextField::fromTextFieldXml(const QDomElement &dom_element) +{ + if(canImportFromTextField(dom_element)) + { + setFont(QETApp::diagramTextsFont(dom_element.attribute("size", QString::number(9)).toInt())); + setTextFrom(DynamicElementTextItem::UserText); + setText(dom_element.attribute("text", "_")); + QGraphicsTextItem::setRotation(dom_element.attribute("rotation", "0").toDouble()); + + //the origin transformation point of PartDynamicTextField is the top left corner, no matter the font size + //The origin transformation point of PartTextField is the middle of left edge, and so by definition, change with the size of the font + //We need to use a QMatrix to find the pos of this text from the saved pos of text item + QMatrix matrix; + //First make the rotation + matrix.rotate(dom_element.attribute("rotation", "0").toDouble()); + QPointF pos = matrix.map(QPointF(0, -boundingRect().height()/2)); + matrix.reset(); + //Second translate to the pos + matrix.translate(dom_element.attribute("x", QString::number(0)).toDouble(), + dom_element.attribute("y", QString::number(0)).toDouble()); + QGraphicsTextItem::setPos(matrix.map(pos)); + } +} + /** * @brief PartDynamicTextField::textFrom * @return what the final text is created from. @@ -358,12 +401,10 @@ void PartDynamicTextField::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) */ QVariant PartDynamicTextField::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) { - if (change == QGraphicsItem::ItemPositionHasChanged || change == QGraphicsItem::ItemSceneHasChanged) { + if (change == QGraphicsItem::ItemPositionHasChanged || change == QGraphicsItem::ItemSceneHasChanged) updateCurrentPartEditor(); - } else if (change == QGraphicsItem::ItemSelectedHasChanged) { - if (value.toBool() == true) { - updateCurrentPartEditor(); - } - } + else if ((change == QGraphicsItem::ItemSelectedHasChanged) && (value.toBool() == true)) + updateCurrentPartEditor(); + return(QGraphicsTextItem::itemChange(change, value)); } diff --git a/sources/editor/graphicspart/partdynamictextfield.h b/sources/editor/graphicspart/partdynamictextfield.h index 367f13998..ceed68314 100644 --- a/sources/editor/graphicspart/partdynamictextfield.h +++ b/sources/editor/graphicspart/partdynamictextfield.h @@ -41,6 +41,9 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) + public: + static bool canImportFromTextField(const QDomElement &dom_element); + public: ///PROPERTY void setProperty(const char *name, const QVariant &value) override {QGraphicsTextItem::setProperty(name, value);} @@ -70,6 +73,7 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart const QDomElement toXml(QDomDocument &dom_doc) const override; void fromXml(const QDomElement &dom_elmt) override; + void fromTextFieldXml(const QDomElement &dom_element); DynamicElementTextItem::TextFrom textFrom() const; void setTextFrom (DynamicElementTextItem::TextFrom text_from); diff --git a/sources/editor/graphicspart/parttextfield.cpp b/sources/editor/graphicspart/parttextfield.cpp index aad893a02..24c966868 100644 --- a/sources/editor/graphicspart/parttextfield.cpp +++ b/sources/editor/graphicspart/parttextfield.cpp @@ -102,14 +102,6 @@ const QDomElement PartTextField::toXml(QDomDocument &xml_document) const { return(xml_element); } -/** - @return le decalage entre l'origine du QGraphicsItem et l'origine du champ de - texte. -*/ -QPointF PartTextField::margin() const { - return(QPointF(0.0, boundingRect().bottom() / 2.0)); -} - void PartTextField::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if((event->buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) diff --git a/sources/editor/graphicspart/parttextfield.h b/sources/editor/graphicspart/parttextfield.h index 509e02b22..d62d40306 100644 --- a/sources/editor/graphicspart/parttextfield.h +++ b/sources/editor/graphicspart/parttextfield.h @@ -108,7 +108,6 @@ class PartTextField : public QGraphicsTextItem, public CustomElementPart QRectF boundingRect() const override; private: - QPointF margin() const; QString previous_text; qreal real_font_size_; QPointF saved_point_;