Dynamic element text item new feature

Add feature keep visual rotation.
When parent is rotated, the text rotation is changed to keep the same
visual rotation.
This commit is contained in:
Claveau Joshua
2020-11-20 20:52:19 +01:00
parent f7e12e5e87
commit 8fec6dabef
6 changed files with 90 additions and 6 deletions

View File

@@ -44,6 +44,7 @@ DynamicElementTextItem::DynamicElementTextItem(Element *parent_element) :
setParentItem(parent_element);
QSettings settings;
setRotation(settings.value("dynamic_text_rotation", 0).toInt());
setKeepVisualRotation(true);
setTextWidth(settings.value("dynamic_text_widht", -1).toInt());
connect(this, &DynamicElementTextItem::textEdited, [this](const QString &old_str, const QString &new_str)
{
@@ -95,6 +96,7 @@ QDomElement DynamicElementTextItem::toXml(QDomDocument &dom_doc) const
root_element.setAttribute("frame", m_frame? "true" : "false");
root_element.setAttribute("text_width", QString::number(m_text_width));
root_element.setAttribute("font", font().toString());
root_element.setAttribute("keep_visual_rotation", m_keep_visual_rotation ? "true" : "false");
QMetaEnum me = textFromMetaEnum();
root_element.setAttribute("text_from", me.valueToKey(m_text_from));
@@ -159,6 +161,7 @@ void DynamicElementTextItem::fromXml(const QDomElement &dom_elmt)
}
QGraphicsTextItem::setRotation(dom_elmt.attribute("rotation", QString::number(0)).toDouble());
setKeepVisualRotation(dom_elmt.attribute("keep_visual_rotation", "true") == "true"? true : false);
if (dom_elmt.hasAttribute("font"))
{
@@ -1248,6 +1251,32 @@ void DynamicElementTextItem::zoomToLinkedElement()
}
}
/**
* @brief DynamicElementTextItem::parentElementRotationChanged
* Called when the parent element is rotated
*/
void DynamicElementTextItem::parentElementRotationChanged()
{
if (m_parent_element && m_keep_visual_rotation)
{
//We temporally disconnect for not change m_visual_rotation value.
//We don't use block signal, because rotationChanged signal is used in other place.
disconnect(this, &DynamicElementTextItem::rotationChanged, this, &DynamicElementTextItem::thisRotationChanged);
this->setRotation(QET::correctAngle(m_visual_rotation_ref - m_parent_element->rotation(), true));
connect(this, &DynamicElementTextItem::rotationChanged, this, &DynamicElementTextItem::thisRotationChanged);
}
}
/**
* @brief DynamicElementTextItem::thisRotationChanged
* This function is called when user change the rotation of the text
* and "keep visual rotation" is to true
* to keep in memory the visual rotation wanted by the user.
*/
void DynamicElementTextItem::thisRotationChanged() {
m_visual_rotation_ref = this->rotation() + m_parent_element->rotation();
}
/**
@brief DynamicElementTextItem::updateXref
Create or delete the Xref according to the current properties of the project
@@ -1422,3 +1451,22 @@ void DynamicElementTextItem::setXref_item(Qt::AlignmentFlag m_exHrefPos)
return;
}
void DynamicElementTextItem::setKeepVisualRotation(bool set)
{
m_keep_visual_rotation = set;
emit keepVisualRotationChanged(set);
if (set) {
m_visual_rotation_ref = this->rotation() + m_parent_element->rotation();
connect(m_parent_element, &Element::rotationChanged, this, &DynamicElementTextItem::parentElementRotationChanged);
connect(this, &DynamicElementTextItem::rotationChanged, this, &DynamicElementTextItem::thisRotationChanged);
}
else {
disconnect(m_parent_element, &Element::rotationChanged, this, &DynamicElementTextItem::parentElementRotationChanged);
disconnect(this, &DynamicElementTextItem::rotationChanged, this, &DynamicElementTextItem::thisRotationChanged);
}
}
bool DynamicElementTextItem::keepVisualRotation() const {
return m_keep_visual_rotation;
}

View File

@@ -49,6 +49,7 @@ class DynamicElementTextItem : public DiagramTextItem
Q_PROPERTY(QString compositeText READ compositeText WRITE setCompositeText NOTIFY compositeTextChanged)
Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged)
Q_PROPERTY(bool keepVisualRotation READ keepVisualRotation WRITE setKeepVisualRotation NOTIFY keepVisualRotationChanged)
public:
@@ -69,6 +70,7 @@ class DynamicElementTextItem : public DiagramTextItem
void frameChanged(bool frame);
void plainTextChanged();
void textWidthChanged(qreal width);
void keepVisualRotationChanged(bool keep);
public:
DynamicElementTextItem(Element *parent_element);
@@ -104,6 +106,9 @@ class DynamicElementTextItem : public DiagramTextItem
void setTextWidth(qreal width);
void setXref_item(Qt::AlignmentFlag m_exHrefPos);
void setKeepVisualRotation(bool set);
bool keepVisualRotation() const;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
@@ -133,6 +138,8 @@ class DynamicElementTextItem : public DiagramTextItem
void conductorPropertiesChanged();
QString reportReplacedCompositeText() const;
void zoomToLinkedElement();
void parentElementRotationChanged();
void thisRotationChanged();
private:
QPointer <Element>
@@ -160,6 +167,8 @@ class DynamicElementTextItem : public DiagramTextItem
QGraphicsTextItem *m_slave_Xref_item = nullptr;
qreal m_text_width = -1;
QPointF m_initial_position;
bool m_keep_visual_rotation = true;
qreal m_visual_rotation_ref = 0;
};
#endif // DYNAMICELEMENTTEXTITEM_H