From a5fe544415c09d238055688430473b8887e0d206 Mon Sep 17 00:00:00 2001 From: Kellermorph Date: Sun, 13 Sep 2026 18:50:38 +0200 Subject: [PATCH] Fix text position shift when duplicating diagram pages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit duplicateDiagram() called restoreText() on every newly loaded element. Each setPlainText() inside restoreText() is wrapped in m_block_alignment except the last one, so finishAlignment() ran on elements whose positions came straight from the XML — shifting center and right-aligned texts. Fix: use toXml(true, true) which handles correctTextPos/restoreText internally for Slave and Report elements only, and call restoreText() on the target for those same element types to recalculate their text positions for the actual resolved text. --- sources/elementspanelwidget.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/sources/elementspanelwidget.cpp b/sources/elementspanelwidget.cpp index 4b1167119..018064da5 100644 --- a/sources/elementspanelwidget.cpp +++ b/sources/elementspanelwidget.cpp @@ -658,21 +658,14 @@ void ElementsPanelWidget::duplicateDiagram() BorderProperties bp = source_diagram->border_and_titleblock.exportBorder(); new_diagram->border_and_titleblock.importBorder(bp); - for (QGraphicsItem *item : source_diagram->items()) { - if (Element *elmt = dynamic_cast(item)) { - source_diagram->correctTextPos(elmt); - } - } - - QDomDocument doc = source_diagram->toXml(); + // Serialize the whole diagram with is_copy_command=true. + // This is the same mechanism as Ctrl+C: toXml(true, true) + // internally calls correctTextPos/restoreText for Slave and + // Report elements only — producing correct text positions + // in the XML. No manual correctTextPos/restoreText needed. + QDomDocument doc = source_diagram->toXml(true, true); QDomElement diagram_elmt = doc.documentElement(); - for (QGraphicsItem *item : source_diagram->items()) { - if (Element *elmt = dynamic_cast(item)) { - source_diagram->restoreText(elmt); - } - } - new_diagram->fromXml(diagram_elmt, QPointF(0, 0), false, nullptr); for (QGraphicsItem *item : new_diagram->items()) { @@ -683,7 +676,18 @@ void ElementsPanelWidget::duplicateDiagram() // of the project database, so duplicates fail to insert and // silently vanish from nomenclature/summary tables. elmt->newUuid(); - new_diagram->restoreText(elmt); + + // toXml(true, true) applied correctTextPos to Slave and + // Report elements, which shifted their text positions to + // match the stripped composite text. restoreText() + // recalculates the position for the actual resolved text. + // Only Slave and Report need this — other element types + // were not affected by correctTextPos. + if (elmt->linkType() == Element::Slave || + elmt->linkType() & Element::AllReport) + { + new_diagram->restoreText(elmt); + } } else if (Conductor *cond = dynamic_cast(item)) { // Same reasoning for conductors: conductor.uuid is the PRIMARY