mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-08-07 05:44:14 +02:00
Merge branch 'master' into qt6_cmake_joshua
This commit is contained in:
@@ -18,10 +18,12 @@
|
||||
#include "parttext.h"
|
||||
|
||||
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
#include <QApplication>
|
||||
#include "../../qetapp.h"
|
||||
#include "../elementprimitivedecorator.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../ui/texteditor.h"
|
||||
#include "../../utils/qetutils.h"
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@@ -124,12 +126,26 @@ void PartText::fromXml(const QDomElement &xml_element) {
|
||||
}
|
||||
else if (xml_element.hasAttribute("font")) {
|
||||
QFont font_;
|
||||
font_.fromString(xml_element.attribute("font"));
|
||||
QETUtils::fontFromString(font_, xml_element.attribute("font"));
|
||||
setFont(font_);
|
||||
}
|
||||
|
||||
setDefaultTextColor(QColor(xml_element.attribute("color", "#000000")));
|
||||
setPlainText(xml_element.attribute("text"));
|
||||
|
||||
// Optional alignment (absent = historical behaviour: top-left anchor,
|
||||
// left-aligned lines), same attributes as the dynamic text fields.
|
||||
Qt::Alignment alignment_ = Qt::AlignTop | Qt::AlignLeft;
|
||||
QMetaEnum me = QMetaEnum::fromType<Qt::Alignment>();
|
||||
if (xml_element.hasAttribute("Halignment"))
|
||||
alignment_ = Qt::Alignment(
|
||||
me.keyToValue(xml_element.attribute("Halignment").toStdString().data()));
|
||||
if (xml_element.hasAttribute("Valignment"))
|
||||
alignment_ = Qt::Alignment(
|
||||
me.keyToValue(xml_element.attribute("Valignment").toStdString().data()))
|
||||
| (alignment_ & Qt::AlignHorizontal_Mask);
|
||||
setAlignment(alignment_);
|
||||
|
||||
setPos(xml_element.attribute("x").toDouble(),
|
||||
xml_element.attribute("y").toDouble());
|
||||
QGraphicsObject::setRotation(QET::correctAngle(xml_element.attribute("rotation", QString::number(0)).toDouble()));
|
||||
@@ -150,10 +166,22 @@ const QDomElement PartText::toXml(QDomDocument &xml_document) const
|
||||
xml_element.setAttribute("x", QString::number(x));
|
||||
xml_element.setAttribute("y", QString::number(y));
|
||||
xml_element.setAttribute("text", toPlainText());
|
||||
xml_element.setAttribute("font", font().toString());
|
||||
xml_element.setAttribute("font", QETUtils::fontToString(font()));
|
||||
xml_element.setAttribute("rotation", QString::number(rot));
|
||||
xml_element.setAttribute("color", defaultTextColor().name());
|
||||
|
||||
// Only written when different from the historical behaviour, so
|
||||
// existing .elmt files round-trip byte-identical.
|
||||
QMetaEnum me = QMetaEnum::fromType<Qt::Alignment>();
|
||||
if (m_alignment & Qt::AlignRight)
|
||||
xml_element.setAttribute("Halignment", me.valueToKey(Qt::AlignRight));
|
||||
else if (m_alignment & Qt::AlignHCenter)
|
||||
xml_element.setAttribute("Halignment", me.valueToKey(Qt::AlignHCenter));
|
||||
if (m_alignment & Qt::AlignBottom)
|
||||
xml_element.setAttribute("Valignment", me.valueToKey(Qt::AlignBottom));
|
||||
else if (m_alignment & Qt::AlignVCenter)
|
||||
xml_element.setAttribute("Valignment", me.valueToKey(Qt::AlignVCenter));
|
||||
|
||||
return(xml_element);
|
||||
}
|
||||
|
||||
@@ -305,24 +333,126 @@ void PartText::setDefaultTextColor(const QColor &color) {
|
||||
|
||||
void PartText::setPlainText(const QString &text) {
|
||||
if (text != this -> toPlainText()) {
|
||||
prepareAlignment();
|
||||
QGraphicsTextItem::setPlainText(text);
|
||||
applyLineAlignment();
|
||||
finishAlignment();
|
||||
emit plainTextChanged(text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartText::setAlignment
|
||||
Set how this text is anchored to its position: when the content later
|
||||
changes, the given corner/center of the bounding rect keeps its place
|
||||
(the historical behaviour, and the default, is top-left). The
|
||||
horizontal part also aligns the lines of a multi-line text relative
|
||||
to each other. Changing the alignment never moves the text itself.
|
||||
@param alignment
|
||||
*/
|
||||
void PartText::setAlignment(const Qt::Alignment &alignment)
|
||||
{
|
||||
if (alignment == m_alignment)
|
||||
return;
|
||||
m_alignment = alignment;
|
||||
applyLineAlignment();
|
||||
emit alignmentChanged(m_alignment);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartText::applyLineAlignment
|
||||
Align the lines of a multi-line text relative to each other according
|
||||
to the horizontal part of the alignment property. QGraphicsTextItem
|
||||
only honors the document text option when a text width is set, hence
|
||||
the idealWidth() dance; -1 restores the free (historical) layout.
|
||||
*/
|
||||
void PartText::applyLineAlignment()
|
||||
{
|
||||
QTextOption option = document()->defaultTextOption();
|
||||
option.setAlignment(m_alignment & Qt::AlignHorizontal_Mask);
|
||||
document()->setDefaultTextOption(option);
|
||||
|
||||
setTextWidth(-1);
|
||||
if (m_alignment & (Qt::AlignHCenter | Qt::AlignRight))
|
||||
setTextWidth(document()->idealWidth());
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartText::prepareAlignment
|
||||
Call before a change of the bounding rect (see finishAlignment).
|
||||
*/
|
||||
void PartText::prepareAlignment()
|
||||
{
|
||||
m_alignment_rect = boundingRect();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartText::finishAlignment
|
||||
Call after a change of the bounding rect: moves the text so that the
|
||||
point selected by the alignment property stays where it was (same
|
||||
logic as DiagramTextItem::finishAlignment).
|
||||
*/
|
||||
void PartText::finishAlignment()
|
||||
{
|
||||
QTransform transform;
|
||||
transform.rotate(rotation());
|
||||
qreal x, xa, y, ya;
|
||||
x = xa = 0;
|
||||
y = ya = 0;
|
||||
|
||||
if (m_alignment & Qt::AlignRight)
|
||||
{
|
||||
x = m_alignment_rect.right();
|
||||
xa = boundingRect().right();
|
||||
}
|
||||
else if (m_alignment & Qt::AlignHCenter)
|
||||
{
|
||||
x = m_alignment_rect.center().x();
|
||||
xa = boundingRect().center().x();
|
||||
}
|
||||
|
||||
if (m_alignment & Qt::AlignBottom)
|
||||
{
|
||||
y = m_alignment_rect.bottom();
|
||||
ya = boundingRect().bottom();
|
||||
}
|
||||
else if (m_alignment & Qt::AlignVCenter)
|
||||
{
|
||||
y = m_alignment_rect.center().y();
|
||||
ya = boundingRect().center().y();
|
||||
}
|
||||
|
||||
QPointF p = transform.map(QPointF(x, y));
|
||||
QPointF pa = transform.map(QPointF(xa, ya));
|
||||
|
||||
setPos(pos() - (pa - p));
|
||||
}
|
||||
|
||||
void PartText::setFont(const QFont &font) {
|
||||
if (font != this -> font()) {
|
||||
prepareAlignment();
|
||||
QGraphicsTextItem::setFont(font);
|
||||
applyLineAlignment();
|
||||
finishAlignment();
|
||||
// Re-anchor: the item's position transform is -margin(), and margin()
|
||||
// depends on the font ascent. Without re-running this on a font change,
|
||||
// the transform keeps the previous font's ascent — so the text renders
|
||||
// at a different spot after save/reopen (the position recomputes from
|
||||
// the saved font on load). See #158.
|
||||
adjustItemPosition();
|
||||
emit fontChanged(font);
|
||||
}
|
||||
}
|
||||
|
||||
void PartText::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
||||
if((event -> buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) {
|
||||
QPointF pos = event -> scenePos() + (m_origin_pos - event -> buttonDownScenePos(Qt::LeftButton));
|
||||
event -> modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene() -> snapToGrid(pos));
|
||||
}
|
||||
else {
|
||||
if ((event->buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) {
|
||||
// Suppress spurious moves from the properties dock resizing the viewport.
|
||||
const QPointF d = event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton);
|
||||
if (d.manhattanLength() < QApplication::startDragDistance())
|
||||
return;
|
||||
QPointF pos = event->scenePos() + (m_origin_pos - event->buttonDownScenePos(Qt::LeftButton));
|
||||
event->modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene()->snapToGrid(pos));
|
||||
} else {
|
||||
QGraphicsObject::mouseMoveEvent(event);
|
||||
}
|
||||
}
|
||||
@@ -387,6 +517,11 @@ void PartText::startEdition()
|
||||
{
|
||||
// !previous_text.isNull() means the text is being edited
|
||||
previous_text = toPlainText();
|
||||
|
||||
// Anchor the aligned point across the whole inline edition; free the
|
||||
// text width so typing is not wrapped at the previous block width.
|
||||
prepareAlignment();
|
||||
setTextWidth(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -395,7 +530,8 @@ void PartText::startEdition()
|
||||
*/
|
||||
void PartText::endEdition()
|
||||
{
|
||||
if (!previous_text.isNull()) {
|
||||
const bool was_editing = !previous_text.isNull();
|
||||
if (was_editing) {
|
||||
// the text was being edited
|
||||
QString new_text = toPlainText();
|
||||
if (previous_text != new_text) {
|
||||
@@ -412,4 +548,9 @@ void PartText::endEdition()
|
||||
setTextCursor(qtc);
|
||||
|
||||
setEditable(false);
|
||||
|
||||
if (was_editing) {
|
||||
applyLineAlignment();
|
||||
finishAlignment();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user