mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-25 15:34:13 +01:00
Position X/Y of a symbol text is the point chosen by its alignment
The text properties showed and edited the top-left corner of a text, whatever its alignment, and changing the alignment did not move the text. A right-aligned label next to a symbol therefore needed a different X for every text length to keep the same gap (#1155). Position X/Y is now the anchor point: the point of the text chosen by the alignment (right edge for "right", middle for "centre"...). Typing a position puts that point there, and changing the alignment moves the text so that the new anchor point is on the same X/Y. It is the same point that already stays fixed when the text changes. Applies to the element text properties on a folio and to the text field properties in the symbol editor. The saved x/y is still the top-left corner, so projects and symbols load and save unchanged and look the same; only the numbers shown for texts that are not top-left aligned change. The geometry is in textanchor.h, shared by DiagramTextItem and PartDynamicTextField, and tested in tst_textanchor. Not changed: static texts of the symbol editor (PartText), whose position is tied to the font baseline; texts inside a text group, which have no position or alignment of their own; rotation still pivots on the top-left corner (or the centre), not on the anchor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
#include "../../qetapp.h"
|
||||
#include "../../textanchor.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../../utils/qetutils.h"
|
||||
#include <QApplication>
|
||||
@@ -474,6 +475,41 @@ Qt::Alignment PartDynamicTextField::alignment() const
|
||||
return m_alignment;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartDynamicTextField::anchorPos
|
||||
@return the anchor point of this text, in parent coordinates:
|
||||
the point of the text chosen by the alignment (top-left, right edge,
|
||||
centre...). For a top-left aligned text this is pos().
|
||||
Same as DiagramTextItem::anchorPos().
|
||||
*/
|
||||
QPointF PartDynamicTextField::anchorPos() const
|
||||
{
|
||||
return TextAnchor::pos(this, m_alignment);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartDynamicTextField::setAnchorPos
|
||||
Move this text so that its anchor point (see anchorPos()) is at anchor.
|
||||
@param anchor : in parent coordinates
|
||||
*/
|
||||
void PartDynamicTextField::setAnchorPos(const QPointF &anchor)
|
||||
{
|
||||
setPos(TextAnchor::itemPosFor(this, m_alignment, anchor));
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartDynamicTextField::setAlignmentAtAnchor
|
||||
Change the alignment and move the text so that its new anchor point
|
||||
is where the previous one was.
|
||||
@param alignment
|
||||
*/
|
||||
void PartDynamicTextField::setAlignmentAtAnchor(Qt::Alignment alignment)
|
||||
{
|
||||
const QPointF anchor = anchorPos();
|
||||
setAlignment(alignment);
|
||||
setAnchorPos(anchor);
|
||||
}
|
||||
|
||||
void PartDynamicTextField::setFont(const QFont &font) {
|
||||
if (font == this -> font()) {
|
||||
return;
|
||||
|
||||
@@ -42,6 +42,8 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
|
||||
Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged)
|
||||
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
|
||||
Q_PROPERTY(QPointF anchorPos READ anchorPos WRITE setAnchorPos)
|
||||
Q_PROPERTY(Qt::Alignment alignmentAtAnchor READ alignment WRITE setAlignmentAtAnchor NOTIFY alignmentChanged)
|
||||
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
|
||||
Q_PROPERTY(bool keepVisualRotation READ keepVisualRotation WRITE setKeepVisualRotation NOTIFY keepVisualRotationChanged)
|
||||
Q_PROPERTY(bool rotationPointCenter READ rotationPointCenter WRITE setRotationPointCenter NOTIFY rotationPointCenterChanged)
|
||||
@@ -101,6 +103,9 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
void setPlainText(const QString &text);
|
||||
void setAlignment(Qt::Alignment alignment);
|
||||
Qt::Alignment alignment() const;
|
||||
QPointF anchorPos() const;
|
||||
void setAnchorPos(const QPointF &anchor);
|
||||
void setAlignmentAtAnchor(Qt::Alignment alignment);
|
||||
void setFont(const QFont &font);
|
||||
void setKeepVisualRotation(const bool &keep);
|
||||
bool keepVisualRotation() const;
|
||||
|
||||
@@ -134,8 +134,8 @@ QList<CustomElementPart*> DynamicTextFieldEditor::currentParts() const
|
||||
void DynamicTextFieldEditor::updateForm()
|
||||
{
|
||||
if(m_text_field) {
|
||||
ui -> m_x_sb -> setValue(m_text_field.data() -> x());
|
||||
ui -> m_y_sb -> setValue(m_text_field.data() ->y ());
|
||||
ui -> m_x_sb -> setValue(m_text_field.data() -> anchorPos().x());
|
||||
ui -> m_y_sb -> setValue(m_text_field.data() -> anchorPos().y());
|
||||
ui -> m_rotation_sb -> setValue(QET::correctAngle(m_text_field.data() -> rotation()));
|
||||
ui -> m_frame_cb -> setChecked(m_text_field.data() -> frame());
|
||||
ui -> m_user_text_le -> setText(m_text_field.data() -> text());
|
||||
@@ -211,6 +211,9 @@ void DynamicTextFieldEditor::setUpConnections()
|
||||
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::compositeTextChanged,this, [this](){this -> updateForm();});
|
||||
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::keepVisualRotationChanged, this, [this](){this -> updateForm();});
|
||||
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::rotationPointCenterChanged, this, [this](){this -> updateForm();});
|
||||
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::alignmentChanged, this, [this](){this -> updateForm();});
|
||||
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::xChanged, this, [this](){this -> updateForm();});
|
||||
m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::yChanged, this, [this](){this -> updateForm();});
|
||||
|
||||
// Refresh info combo when element data changes (e.g. type switched to PLC-Slave)
|
||||
m_connection_list << connect(elementEditor()->elementScene(), &ElementScene::elementInfoChanged,
|
||||
@@ -290,7 +293,8 @@ void DynamicTextFieldEditor::on_m_x_sb_editingFinished()
|
||||
{
|
||||
double value = ui -> m_x_sb -> value();
|
||||
for (int i = 0; i < m_parts.length(); i++) {
|
||||
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_parts[i], "x", m_parts[i] -> x(), value);
|
||||
const QPointF anchor = m_parts[i] -> anchorPos();
|
||||
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_parts[i], "anchorPos", anchor, QPointF(value, anchor.y()));
|
||||
undo -> setText(tr("Déplacer un champ texte"));
|
||||
undo -> enableAnimation(true);
|
||||
undoStack().push(undo);
|
||||
@@ -301,7 +305,8 @@ void DynamicTextFieldEditor::on_m_y_sb_editingFinished()
|
||||
{
|
||||
double value = ui -> m_y_sb -> value();
|
||||
for (int i = 0; i < m_parts.length(); i++) {
|
||||
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_parts[i], "y", m_parts[i] -> y(), value);
|
||||
const QPointF anchor = m_parts[i] -> anchorPos();
|
||||
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_parts[i], "anchorPos", anchor, QPointF(anchor.x(), value));
|
||||
undo -> setText(tr("Déplacer un champ texte"));
|
||||
undo -> enableAnimation(true);
|
||||
undoStack().push(undo);
|
||||
@@ -459,7 +464,7 @@ void DynamicTextFieldEditor::on_m_alignment_pb_clicked()
|
||||
if(atd.alignment() != m_parts[i] -> alignment()) {
|
||||
QPropertyUndoCommand *undo =\
|
||||
new QPropertyUndoCommand(
|
||||
m_parts[i], "alignment", QVariant(m_parts[i] -> alignment()), QVariant(atd.alignment()));
|
||||
m_parts[i], "alignmentAtAnchor", QVariant(m_parts[i] -> alignment()), QVariant(atd.alignment()));
|
||||
undo -> setText(tr("Modifier l'alignement d'un champ texte"));
|
||||
undoStack().push(undo);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "../diagramcommands.h"
|
||||
#include "../qetapp.h"
|
||||
#include "../richtext/richtexteditor_p.h"
|
||||
#include "../textanchor.h"
|
||||
|
||||
/**
|
||||
@brief DiagramTextItem::DiagramTextItem
|
||||
@@ -193,6 +194,41 @@ Qt::Alignment DiagramTextItem::alignment() const
|
||||
return m_alignment;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief DiagramTextItem::anchorPos
|
||||
@return the anchor point of this text, in parent coordinates:
|
||||
the point of the text chosen by the alignment (top-left, right edge,
|
||||
centre...). For a top-left aligned text this is pos().
|
||||
The anchor stays in place when the text changes, see finishAlignment().
|
||||
*/
|
||||
QPointF DiagramTextItem::anchorPos() const
|
||||
{
|
||||
return TextAnchor::pos(this, m_alignment);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief DiagramTextItem::setAnchorPos
|
||||
Move this text so that its anchor point (see anchorPos()) is at anchor.
|
||||
@param anchor : in parent coordinates
|
||||
*/
|
||||
void DiagramTextItem::setAnchorPos(const QPointF &anchor)
|
||||
{
|
||||
setPos(TextAnchor::itemPosFor(this, m_alignment, anchor));
|
||||
}
|
||||
|
||||
/**
|
||||
@brief DiagramTextItem::setAlignmentAtAnchor
|
||||
Change the alignment and move the text so that its new anchor point
|
||||
is where the previous one was.
|
||||
@param alignment
|
||||
*/
|
||||
void DiagramTextItem::setAlignmentAtAnchor(const Qt::Alignment &alignment)
|
||||
{
|
||||
const QPointF anchor = anchorPos();
|
||||
setAlignment(alignment);
|
||||
setAnchorPos(anchor);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief DiagramTextItem::frameRect
|
||||
@return the rect used to draw a frame around this text
|
||||
|
||||
@@ -36,6 +36,8 @@ class DiagramTextItem : public QGraphicsTextItem
|
||||
|
||||
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
|
||||
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
|
||||
Q_PROPERTY(QPointF anchorPos READ anchorPos WRITE setAnchorPos)
|
||||
Q_PROPERTY(Qt::Alignment alignmentAtAnchor READ alignment WRITE setAlignmentAtAnchor NOTIFY alignmentChanged)
|
||||
Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText)
|
||||
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
|
||||
|
||||
@@ -77,6 +79,10 @@ class DiagramTextItem : public QGraphicsTextItem
|
||||
void setAlignment(const Qt::Alignment &alignment);
|
||||
Qt::Alignment alignment() const;
|
||||
bool m_block_alignment = false;
|
||||
|
||||
QPointF anchorPos() const;
|
||||
void setAnchorPos(const QPointF &anchor);
|
||||
void setAlignmentAtAnchor(const Qt::Alignment &alignment);
|
||||
|
||||
QRectF frameRect() const;
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
Copyright 2006-2026 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef TEXTANCHOR_H
|
||||
#define TEXTANCHOR_H
|
||||
|
||||
#include <QGraphicsItem>
|
||||
#include <QPointF>
|
||||
#include <QRectF>
|
||||
|
||||
/**
|
||||
The anchor point of a text: the point of the text chosen by its
|
||||
alignment (top-left, right edge, centre...). It is the point that
|
||||
stays in place when the text changes, and the point shown as
|
||||
"Position X/Y" in the text properties.
|
||||
|
||||
The saved position of a text is still its top-left corner (pos()),
|
||||
so existing projects and symbols are read and written unchanged.
|
||||
*/
|
||||
namespace TextAnchor
|
||||
{
|
||||
/**
|
||||
@return the point of rect chosen by alignment, in the
|
||||
coordinates of rect. Left and top are x = 0 and y = 0, as
|
||||
finishAlignment() of the text items assumes.
|
||||
*/
|
||||
inline QPointF localPoint(const QRectF &rect, Qt::Alignment alignment)
|
||||
{
|
||||
qreal x = 0, y = 0;
|
||||
|
||||
if (alignment & Qt::AlignRight)
|
||||
x = rect.right();
|
||||
else if (alignment & Qt::AlignHCenter)
|
||||
x = rect.center().x();
|
||||
|
||||
if (alignment & Qt::AlignBottom)
|
||||
y = rect.bottom();
|
||||
else if (alignment & Qt::AlignVCenter)
|
||||
y = rect.center().y();
|
||||
|
||||
return QPointF(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
@return the anchor point of item for alignment, in parent
|
||||
coordinates. For a top-left alignment this is item->pos().
|
||||
The rotation of the item is taken into account.
|
||||
*/
|
||||
inline QPointF pos(const QGraphicsItem *item, Qt::Alignment alignment)
|
||||
{
|
||||
return item->pos()
|
||||
+ item->mapToParent(localPoint(item->boundingRect(), alignment))
|
||||
- item->mapToParent(QPointF(0, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
@return the position (top-left corner) to give to item so that
|
||||
its anchor point for alignment is at anchor.
|
||||
*/
|
||||
inline QPointF itemPosFor(const QGraphicsItem *item,
|
||||
Qt::Alignment alignment,
|
||||
const QPointF &anchor)
|
||||
{
|
||||
return item->pos() + anchor - pos(item, alignment);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TEXTANCHOR_H
|
||||
@@ -303,7 +303,7 @@ QList<QStandardItem *> DynamicElementTextModel::itemsForText(
|
||||
x_pos->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
QStandardItem *x_pos_a = new QStandardItem;
|
||||
x_pos_a->setData(deti->pos().x(), Qt::EditRole);
|
||||
x_pos_a->setData(deti->anchorPos().x(), Qt::EditRole);
|
||||
x_pos_a->setData(DynamicElementTextModel::pos, Qt::UserRole+1);
|
||||
x_pos_a->setFlags(Qt::ItemIsSelectable
|
||||
| Qt::ItemIsEnabled
|
||||
@@ -318,7 +318,7 @@ QList<QStandardItem *> DynamicElementTextModel::itemsForText(
|
||||
y_pos->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
|
||||
QStandardItem *y_pos_a = new QStandardItem;
|
||||
y_pos_a->setData(deti->pos().y(), Qt::EditRole);
|
||||
y_pos_a->setData(deti->anchorPos().y(), Qt::EditRole);
|
||||
y_pos_a->setData(DynamicElementTextModel::pos, Qt::UserRole+1);
|
||||
y_pos_a->setFlags(Qt::ItemIsSelectable
|
||||
| Qt::ItemIsEnabled
|
||||
@@ -600,9 +600,9 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(
|
||||
{
|
||||
QPointF p(text_qsi->child(x_txt_row,1)->data(Qt::EditRole).toDouble(),
|
||||
text_qsi->child(y_txt_row,1)->data(Qt::EditRole).toDouble());
|
||||
if(p != deti->pos())
|
||||
if(p != deti->anchorPos())
|
||||
{
|
||||
QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "pos", QVariant(deti->pos()), QVariant(p), undo);
|
||||
QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "anchorPos", QVariant(deti->anchorPos()), QVariant(p), undo);
|
||||
quc->setAnimated(true, false);
|
||||
quc->setText(tr("Déplacer un texte d'élément"));
|
||||
}
|
||||
@@ -636,7 +636,7 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(
|
||||
Qt::Alignment alignment = text_qsi->child(align_txt_row, 1)->data(Qt::UserRole+2).value<Qt::Alignment>();
|
||||
if (alignment != deti->alignment())
|
||||
{
|
||||
QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "alignment", QVariant(deti->alignment()), QVariant(alignment), undo);
|
||||
QPropertyUndoCommand *quc = new QPropertyUndoCommand(deti, "alignmentAtAnchor", QVariant(deti->alignment()), QVariant(alignment), undo);
|
||||
quc->setText(tr("Modifier l'alignement d'un texte d'élément"));
|
||||
}
|
||||
}
|
||||
@@ -1508,9 +1508,9 @@ void DynamicElementTextModel::updateDataFromText(DynamicElementTextItem *deti,
|
||||
case pos:
|
||||
{
|
||||
if(qsi->child(x_txt_row,1))
|
||||
qsi->child(x_txt_row,1)->setData(deti->pos().x(), Qt::EditRole);
|
||||
qsi->child(x_txt_row,1)->setData(deti->anchorPos().x(), Qt::EditRole);
|
||||
if(qsi->child(y_txt_row,1))
|
||||
qsi->child(y_txt_row,1)->setData(deti->pos().y(), Qt::EditRole);
|
||||
qsi->child(y_txt_row,1)->setData(deti->anchorPos().y(), Qt::EditRole);
|
||||
break;
|
||||
}
|
||||
case frame:
|
||||
@@ -1531,6 +1531,14 @@ void DynamicElementTextModel::updateDataFromText(DynamicElementTextItem *deti,
|
||||
}
|
||||
default:break;
|
||||
}
|
||||
|
||||
//Rotating or resizing the text moves its anchor point (Position X/Y)
|
||||
if ((type == rotation || type == textWidth)
|
||||
&& qsi->child(x_txt_row,1) && qsi->child(y_txt_row,1))
|
||||
{
|
||||
qsi->child(x_txt_row,1)->setData(deti->anchorPos().x(), Qt::EditRole);
|
||||
qsi->child(y_txt_row,1)->setData(deti->anchorPos().y(), Qt::EditRole);
|
||||
}
|
||||
|
||||
m_block_dataChanged = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user