element editor: add mirror and flip for “text”

Maybe not (yet) perfect, but it looks pretty good to me!

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.
This commit is contained in:
plc-user
2025-02-17 19:39:53 +01:00
parent 0f647a5c38
commit dc836248f0
3 changed files with 37 additions and 2 deletions

View File

@@ -686,7 +686,7 @@ void MirrorElementsCommand::redo()
foreach (auto *item, m_items) {
if (item->type() == PartText::Type) {
PartText* staticText = qgraphicsitem_cast<PartText*>(item);
//staticText->mirror();
staticText->mirror();
} else if (item->type() == PartDynamicTextField::Type) {
PartDynamicTextField* dyntext = qgraphicsitem_cast<PartDynamicTextField*>(item);
dyntext->mirror();
@@ -734,7 +734,7 @@ void FlipElementsCommand::redo()
foreach (auto *item, m_items) {
if (item->type() == PartText::Type) {
PartText* staticText = qgraphicsitem_cast<PartText*>(item);
//staticText->flip();
staticText->flip();
} else if (item->type() == PartDynamicTextField::Type) {
PartDynamicTextField* dyntext = qgraphicsitem_cast<PartDynamicTextField*>(item);
dyntext->flip();

View File

@@ -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

View File

@@ -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;