Fix copy-paste bugs in DiagramEventAddPaste and PasteDiagramCommand

Fix multiple issues introduced by commit 55c2c0df9 (interactive paste):

Paste placement (diagrameventaddpaste):
- Items now load at their original XML coordinates instead of being
  snapped to the cursor position, preventing jumps on initial placement
- Use delta-based movement: record the actual grid-snapped cursor
  position on first mouseMoveEvent as baseline, then compute
  grid-snapped deltas from there
- Bypass Diagram::snapToGrid() in moveTo() to avoid Ctrl modifier
  causing pixel-snapping instead of grid-snapping
- Remove moveTo() from mouseReleaseEvent to prevent a final jump
  on click
- Warp cursor to group bounding rect origin for visual feedback

Paste command (diagramcommands):
- Always clear PLC slave data (type, address, function, comment,
  cross-ref, label, TC, T1-T4) on paste regardless of the
  erase-label-on-copy user preference
- Block alignment (m_block_alignment / blockAlignmentUpdate) before
  setElementInformations() for both slaves and non-slaves, preventing
  finishAlignment() from shifting right/center-aligned text items
- Clear non-UserText items directly after setElementInformations()
  for slaves as a safety net
This commit is contained in:
Kellermorph
2026-09-17 17:54:57 +02:00
parent 8c135a009d
commit 53a0f07ca3
3 changed files with 153 additions and 40 deletions
+70 -16
View File
@@ -23,6 +23,8 @@
#include "../qetdiagrameditor.h"
#include "../qetgraphicsitem/conductor.h"
#include <QSettings>
#include <QApplication>
#include <QClipboard>
#include <QGraphicsSceneMouseEvent>
@@ -35,7 +37,7 @@
@param start_pos : where the pasted items first appear, in scene
coordinates -- normally the cursor
*/
DiagramEventAddPaste::DiagramEventAddPaste(Diagram *diagram, const QPointF &start_pos) :
DiagramEventAddPaste::DiagramEventAddPaste(Diagram *diagram, const QPointF &start_pos) :
DiagramEventInterface(diagram)
{
//DiagramEventInterface::init() is called by Diagram::setEventInterface
@@ -49,20 +51,37 @@ DiagramEventAddPaste::DiagramEventAddPaste(Diagram *diagram, const QPointF &star
QDomDocument document_xml;
if (!document_xml.setContent(clipboard_text)) return;
m_diagram->fromXml(document_xml, Diagram::snapToGrid(start_pos), false, &m_content);
//Load items at their original XML coordinates.
m_diagram->fromXml(document_xml, QPointF(), false, &m_content);
if (!m_content.count()) return;
//Remember where each item sits relative to the group's top left, so a
//move is one assignment per item rather than an accumulated delta.
QRectF group_rect;
const QList<QGraphicsItem *> movable = m_content.items(MovableItems);
if (movable.isEmpty()) return;
//Compute the bounding rect centre — this is where the cursor
//will start. Items stay at their original XML positions;
//moveTo() handles grid-snapped movement via deltas.
QRectF group_rect;
for (auto *item : movable) {
group_rect = group_rect.united(item->mapToScene(item->boundingRect()).boundingRect());
}
const QPointF top_left = group_rect.topLeft();
QSettings settings;
const int xGrid = settings.value(QStringLiteral("diagrameditor/Xgrid"),
Diagram::xGrid).toInt();
const int yGrid = settings.value(QStringLiteral("diagrameditor/Ygrid"),
Diagram::yGrid).toInt();
const QPointF grid_origin(
qRound(group_rect.topLeft().x() / xGrid) * xGrid,
qRound(group_rect.topLeft().y() / yGrid) * yGrid);
//Store each item's position. moveTo() applies a grid-snapped
//delta from the baseline, so items preserve their layout and
//move in whole grid steps.
for (auto *item : movable) {
m_relative_pos.insert(item, item->pos() - top_left);
m_relative_pos.insert(item, item->pos());
}
m_group_origin = grid_origin;
m_diagram->clearSelection();
for (auto *item : movable) {
@@ -70,8 +89,15 @@ DiagramEventAddPaste::DiagramEventAddPaste(Diagram *diagram, const QPointF &star
}
if (!m_diagram->views().isEmpty()) {
if (const auto qde = QETApp::diagramEditorAncestorOf(m_diagram->views().at(0))) {
m_status_bar = qde->statusBar();
if (auto *view = m_diagram->views().at(0)) {
if (const auto qde = QETApp::diagramEditorAncestorOf(view)) {
m_status_bar = qde->statusBar();
}
//Warp the cursor close to the group origin so the
//first mouseMoveEvent captures the correct baseline.
const QPoint view_pos = view->mapFromScene(m_group_origin);
const QPoint global_pos = view->viewport()->mapToGlobal(view_pos);
QCursor::setPos(global_pos);
}
}
showHint();
@@ -131,19 +157,48 @@ void DiagramEventAddPaste::showHint()
/**
@brief DiagramEventAddPaste::moveTo
Put the group's top left corner at @a scene_pos, snapped to the grid.
Compute a grid-snapped delta from the initial cursor position and
apply it to every item's grid-shifted position. This keeps all
items exactly on grid points regardless of modifier keys or
sub-pixel cursor-warp rounding.
*/
void DiagramEventAddPaste::moveTo(const QPointF &scene_pos)
{
const QPointF anchor = Diagram::snapToGrid(scene_pos);
QSettings settings;
const int xGrid = settings.value(QStringLiteral("diagrameditor/Xgrid"),
Diagram::xGrid).toInt();
const int yGrid = settings.value(QStringLiteral("diagrameditor/Ygrid"),
Diagram::yGrid).toInt();
const auto snapGrid = [xGrid, yGrid](const QPointF &p) -> QPointF {
return QPointF(
qRound(p.x() / xGrid) * xGrid,
qRound(p.y() / yGrid) * yGrid);
};
//On the very first call, record the actual grid-snapped
//cursor position as baseline. The cursor warp in the
//constructor goes through integer rounding (mapFromScene →
//QPoint) so the real position may differ slightly from
//m_initial_cursor. Using the actual scene position avoids
//a one-grid-unit jump on the first mouse movement.
if (m_initial_cursor.isNull()) {
m_initial_cursor = snapGrid(scene_pos);
return;
}
const QPointF delta = snapGrid(scene_pos) - m_initial_cursor;
for (auto it = m_relative_pos.constBegin() ; it != m_relative_pos.constEnd() ; ++it) {
if (it.key()) {
it.key()->setPos(anchor + it.value());
it.key()->setPos(it.value() + delta);
}
}
const auto conductors = m_content.conductors(); // AnyConductor by default
for (auto *cond : conductors) {
cond->updatePath();
//Update conductor paths so they follow the moved terminals.
const QList<Conductor *> conductors = m_content.conductors(DiagramContent::AnyConductor);
for (auto *conductor : conductors) {
conductor->updatePath();
}
}
@@ -168,7 +223,6 @@ void DiagramEventAddPaste::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
event->setAccepted(true);
if (event->button() == Qt::LeftButton) {
moveTo(event->scenePos());
commit();
} else if (event->button() == Qt::RightButton) {
cancel();