mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-01 15:24:13 +02:00
bdc1d5878d
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>
83 lines
2.4 KiB
C++
83 lines
2.4 KiB
C++
/*
|
|
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
|