From dc836248f00398d0984d633d067766bde9260365 Mon Sep 17 00:00:00 2001 From: plc-user <74435298+plc-user@users.noreply.github.com> Date: Mon, 17 Feb 2025 19:39:53 +0100 Subject: [PATCH] =?UTF-8?q?element=20editor:=20add=20mirror=20and=20flip?= =?UTF-8?q?=20for=20=E2=80=9Ctext=E2=80=9D=20Maybe=20not=20(yet)=20perfect?= =?UTF-8?q?,=20but=20it=20looks=20pretty=20good=20to=20me!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why am I doing this to myself? All this crap with fonts and stuff! It's been crap for as long as I can remember. --- sources/editor/editorcommands.cpp | 4 +-- sources/editor/graphicspart/parttext.cpp | 33 ++++++++++++++++++++++++ sources/editor/graphicspart/parttext.h | 2 ++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/sources/editor/editorcommands.cpp b/sources/editor/editorcommands.cpp index 87d7cc706..1a013eefa 100644 --- a/sources/editor/editorcommands.cpp +++ b/sources/editor/editorcommands.cpp @@ -686,7 +686,7 @@ void MirrorElementsCommand::redo() foreach (auto *item, m_items) { if (item->type() == PartText::Type) { PartText* staticText = qgraphicsitem_cast(item); - //staticText->mirror(); + staticText->mirror(); } else if (item->type() == PartDynamicTextField::Type) { PartDynamicTextField* dyntext = qgraphicsitem_cast(item); dyntext->mirror(); @@ -734,7 +734,7 @@ void FlipElementsCommand::redo() foreach (auto *item, m_items) { if (item->type() == PartText::Type) { PartText* staticText = qgraphicsitem_cast(item); - //staticText->flip(); + staticText->flip(); } else if (item->type() == PartDynamicTextField::Type) { PartDynamicTextField* dyntext = qgraphicsitem_cast(item); dyntext->flip(); diff --git a/sources/editor/graphicspart/parttext.cpp b/sources/editor/graphicspart/parttext.cpp index caad2fa18..ec5a0eb5f 100644 --- a/sources/editor/graphicspart/parttext.cpp +++ b/sources/editor/graphicspart/parttext.cpp @@ -72,6 +72,39 @@ void PartText::setRotation(qreal angle) { setPos(QTransform().rotate(angle).map(pos())); } +void PartText::mirror() { + // at first: rotate the text: + QGraphicsObject::setRotation(QET::correctAngle((360-rotation()), true)); + // then see, where we need to re-position depending on text, font ... + QFontMetrics qfm(font()); + qreal textwidth = qfm.horizontalAdvance(toPlainText()); + // ... and angle!!! + qreal rot = qRound(QET::correctAngle(rotation(), true)); + qreal c = qCos(qDegreesToRadians(rot)); + qreal s = qSin(qDegreesToRadians(rot)); + // Now: Move! + qreal x = (-1) * pos().x() - c * (textwidth); + qreal y = pos().y() - s * (textwidth); + setPos(x, y); +} + +void PartText::flip() { + // at first: rotate the text: + QGraphicsObject::setRotation(QET::correctAngle((360-rotation()), true)); + // then see, where we need to re-position depending on text, font ... + QFontMetrics qfm(font()); + qreal textheight = realSize() - qfm.descent(); + // ... and angle!!! + qreal rot = qRound(QET::correctAngle(rotation(), true)); + qreal c = qCos(qDegreesToRadians(rot)); + qreal s = qSin(qDegreesToRadians(rot)); + // Now: Move! + qreal x = pos().x() - s * (textheight); + qreal y = (-1) * pos().y() + c * (textheight); + setPos(x, y); +} + + /** Importe les proprietes d'un texte statique depuis un element XML @param xml_element Element XML a lire diff --git a/sources/editor/graphicspart/parttext.h b/sources/editor/graphicspart/parttext.h index d5650c406..ebfa276df 100644 --- a/sources/editor/graphicspart/parttext.h +++ b/sources/editor/graphicspart/parttext.h @@ -62,6 +62,8 @@ class PartText : public QGraphicsTextItem, public CustomElementPart { void fromXml(const QDomElement &) override; const QDomElement toXml(QDomDocument &) const override; void setRotation(qreal angle); + void mirror(); + void flip(); bool isUseless() const override; QRectF sceneGeometricRect() const override; void startUserTransformation(const QRectF &) override;