From 9bfe18807f9e03c22a01b73bfeca2cebf9d59f53 Mon Sep 17 00:00:00 2001 From: Kellermorph Date: Mon, 14 Sep 2026 16:19:37 +0200 Subject: [PATCH] Fix undo not restoring PLC slave fields and stale connections on position change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two additional bugs found after the initial xref cleanup: 1. Undo after unlink did not restore PLC variables (PLC_TYPE, PLC_ADDRESS, etc.) because PlcLinkWidget::on_m_unlink_pb_clicked() never stored the current group index in the LinkElementCommand. makeLink() only populates PLC fields when group_idx >= 0, so the slave was re-linked but appeared empty. Fixed by reading the group index from the master via groupIndexForElement() and calling setGroupIndex() before unlinking. 2. m_update_slave_Xref_connection was only cleared inside the if(m_slave_Xref_item) block in the updateXref() cleanup path. For AlignHCenter (Text field) position, no m_slave_Xref_item is ever created — the connections are stored in m_update_slave_Xref_connection but never cleaned up on unlink. On re-link, the old stale entries prevented new connections from being established. Fixed by clearing the list unconditionally in the cleanup path. --- sources/qetgraphicsitem/dynamicelementtextitem.cpp | 3 ++- sources/qetgraphicsitem/elementtextitemgroup.cpp | 3 ++- sources/ui/plclinkwidget.cpp | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.cpp b/sources/qetgraphicsitem/dynamicelementtextitem.cpp index 9a2b67d25..db6340114 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.cpp +++ b/sources/qetgraphicsitem/dynamicelementtextitem.cpp @@ -1470,11 +1470,12 @@ void DynamicElementTextItem::updateXref() m_Xref_item = nullptr; } + m_update_slave_Xref_connection.clear(); + if(m_slave_Xref_item) { delete m_slave_Xref_item; m_slave_Xref_item = nullptr; - m_update_slave_Xref_connection.clear(); //If position changed to Champ de texte, store xref in element info if(m_parent_element->linkType() == Element::Slave && diff --git a/sources/qetgraphicsitem/elementtextitemgroup.cpp b/sources/qetgraphicsitem/elementtextitemgroup.cpp index 9adade296..a9cdeb6c0 100644 --- a/sources/qetgraphicsitem/elementtextitemgroup.cpp +++ b/sources/qetgraphicsitem/elementtextitemgroup.cpp @@ -873,11 +873,12 @@ void ElementTextItemGroup::updateXref() delete m_Xref_item; m_Xref_item = nullptr; } + m_update_slave_Xref_connection.clear(); + if(m_slave_Xref_item) { delete m_slave_Xref_item; m_slave_Xref_item = nullptr; - m_update_slave_Xref_connection.clear(); //If position changed to Champ de texte, store xref in element info if(m_parent_element->linkType() == Element::Slave && diff --git a/sources/ui/plclinkwidget.cpp b/sources/ui/plclinkwidget.cpp index 144ee0bff..545c68b1b 100644 --- a/sources/ui/plclinkwidget.cpp +++ b/sources/ui/plclinkwidget.cpp @@ -355,6 +355,20 @@ void PlcLinkWidget::on_m_unlink_pb_clicked() return; LinkElementCommand *undo = new LinkElementCommand(m_element); + + // Store the current group index so makeLink() can + // restore PLC variables and terminal labels on undo. + for (Element *elmt : m_element->linkedElements()) + { + if (elmt->linkType() == Element::Master) + { + int idx = elmt->groupIndexForElement(m_element); + if (idx >= 0) + undo->setGroupIndex(idx); + break; + } + } + undo->unlinkAll(); m_element->diagram()->undoStack().push(undo);