View Issue Details

IDProjectCategoryView StatusLast Update
0012951ScribusUndo/Redopublic2026-10-29 17:09
Reporterale Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Product Version1.5.0svn 
Target Version1.7.5.svn 
Summary0012951: wrong position when undoing a moved ungrouped svg item
Description- download http://llk.media.mit.edu/scratch/assets/Scratchlogo2.svg
- import it in your document
- ungroup it to as much as possible
- move a letter
- undo

the letter is not placed at the previous place

i hope that this simple sample can help fix other svg import / und bugs and crashes...
Tags#please_test
PatchNo

Relationships

child of 0012500 acknowledged Metabug: Undo/Redo (1.5.x.svn) 
child of 0003812 acknowledged Metabug: Grouped objects 

Activities

ale

2025-11-08 19:47

manager   ~0053218

https://web.archive.org/web/20130801151343if_/http://llk.media.mit.edu:80/scratch/assets/Scratchlogo2.svg

It still happens.

buttar

2026-10-16 02:08

reporter   ~0054528

Could reproduce. Fixed.

## Cause

- Group restoration recalculated geometry, corrupting nested group hierarchy, paths and positions.
- SVG import auto-fit created conflicting resize undo states.

## Fix (both variants)

- Capture and restore group hierarchy, stacking order and geometry deterministically.
- Preserve path geometry and clip-resize baselines (`OldB2`/`OldH2`).
- Validate parent-child relationships and prevent duplicate object insertion.
- Prevent import auto-fit from generating independent resize undo actions.

## Variants

- GUR1 v1.05-MODULAR: Introduces `scribusdoc_grouping.cpp` for grouping and undo restoration logic, with CMake registration.
- GUR1 v1.05-INPLACE: Keeps the implementation in existing Scribus source files; no new module or CMake changes.
gur1-group-ungroup-undo-redo-integrity-v1.05-INPLACE.patch (16,544 bytes)   
Index: scribus/scribusdoc.cpp
===================================================================
--- scribus/scribusdoc.cpp	(revision 27888)
+++ scribus/scribusdoc.cpp	(working copy)
@@ -46,6 +46,7 @@
 #include <QRandomGenerator>
 #include <QRegularExpression>
 #include <QScopedValueRollback>
+#include <QSet>
 #include <QStringList>
 #include <QtAlgorithms>
 #include <QTime>
@@ -2126,41 +2127,264 @@
 	setCurrentPage(oldPage);
 }
 
+// Group/ungroup is an ownership change *and* a coordinate-space change.
+// Save both sides of the operation: replaying addToGroup/removeFromGroup
+// repeatedly loses geometry (especially with scaled, rotated nested groups).
+namespace
+{
+struct GroupItemGeometry
+{
+	QPointer<PageItem> item;
+	double x { 0 }, y { 0 }, width { 0 }, height { 0 }, rotation { 0 };
+	double groupX { 0 }, groupY { 0 }, groupWidth { 0 }, groupHeight { 0 };
+	double lineWidth { 0 }, imageScaleX { 0 }, imageScaleY { 0 };
+	bool flipH { false }, flipV { false };
+	int ownPage { -1 };
+	FPointArray path;
+	FPointArray contour;
+	FPointArray imageClip;
+	QList<uint> segments;
+	QPolygon clip;
+	ScPatternTransform pattern;
+	ScMaskTransform mask;
+	double arcWidth { 0 }, arcHeight { 0 };
+	std::shared_ptr<const StoryText> text;
+
+	explicit GroupItemGeometry(PageItem* p) : item(p)
+	{
+		x = p->xPos();
+		y = p->yPos();
+		width = p->width();
+		height = p->height();
+		rotation = p->rotation();
+		groupX = p->gXpos;
+		groupY = p->gYpos;
+		groupWidth = p->gWidth;
+		groupHeight = p->gHeight;
+		lineWidth = p->lineWidth();
+		imageScaleX = p->imageXScale();
+		imageScaleY = p->imageYScale();
+		ownPage = p->OwnPage;
+		flipH = p->imageFlippedH();
+		flipV = p->imageFlippedV();
+		path = p->PoLine;
+		contour = p->ContourLine;
+		imageClip = p->imageClip;
+		segments = p->Segments;
+		clip = p->Clip;
+		pattern = p->patternTransform();
+		mask = p->maskTransform();
+		if (const auto* arc = p->asArc())
+		{
+			arcWidth = arc->arcWidth;
+			arcHeight = arc->arcHeight;
+		}
+		if ((p->isTextFrame() || p->isPathText()) && !p->itemText.isEmpty())
+			text = std::make_shared<StoryText>(p->itemText);
+	}
+
+	void restore() const
+	{
+		PageItem* p = item;
+		p->setXYPos(x, y, true);
+		p->setWidthHeight(width, height, true);
+		p->setRotation(rotation, true);
+		p->gXpos = groupX;
+		p->gYpos = groupY;
+		p->gWidth = groupWidth;
+		p->gHeight = groupHeight;
+		p->OwnPage = ownPage;
+		p->setLineWidth(lineWidth);
+		p->setImageXScale(imageScaleX);
+		p->setImageYScale(imageScaleY);
+		p->setImageFlippedH(flipH);
+		p->setImageFlippedV(flipV);
+		p->PoLine = path;
+		p->ContourLine = contour;
+		p->imageClip = imageClip;
+		p->Segments = segments;
+		p->Clip = clip;
+		p->setPatternTransform(pattern);
+		p->setMaskTransform(mask);
+		if (auto* arc = p->asArc())
+		{
+			arc->arcWidth = arcWidth;
+			arc->arcHeight = arcHeight;
+		}
+		if (text)
+			p->itemText = *text;
+		// The path already matches the restored frame dimensions. updateClip()
+		// must not apply the preceding ungroup's size conversion again.
+		p->OldB2 = width;
+		p->OldH2 = height;
+		p->syncUndoBaseline();
+	}
+};
+
+struct GroupItemPlace
+{
+	QPointer<PageItem> item;
+	QPointer<PageItem> parent;
+	int index { -1 }; // -1: detached (original group before creation/after ungroup)
+};
+
+struct GroupSnapshot
+{
+	QList<GroupItemPlace> places; // direct children, followed by the original group
+	QList<GroupItemGeometry> geometry; // also includes descendants of nested groups
+};
+
+class GroupUndoState final : public SimpleState
+{
+public:
+	using SimpleState::SimpleState;
+	GroupSnapshot grouped;
+	GroupSnapshot ungrouped;
+};
+
+void addGroupGeometry(GroupSnapshot& state, PageItem* item, QSet<PageItem*>& seen)
+{
+	if (!item || seen.contains(item))
+		return;
+	seen.insert(item);
+	state.geometry.append(GroupItemGeometry(item));
+	if (item->isGroup())
+	{
+		for (PageItem* child : item->groupItemList)
+			addGroupGeometry(state, child, seen);
+	}
+}
+
+GroupSnapshot captureGroupSnapshot(ScribusDoc* doc, PageItem* group, const QList<PageItem*>& children)
+{
+	GroupSnapshot state;
+	QSet<PageItem*> seen;
+	for (PageItem* item : children)
+	{
+		QList<PageItem*>* owner = doc->parentGroup(item, doc->Items);
+		state.places.append({item, (owner && owner != doc->Items) ? item->Parent : nullptr,
+		                     owner ? owner->indexOf(item) : -1});
+		addGroupGeometry(state, item, seen);
+	}
+	if (group)
+	{
+		QList<PageItem*>* owner = doc->parentGroup(group, doc->Items);
+		state.places.append({group, (owner && owner != doc->Items) ? group->Parent : nullptr,
+		                     owner ? owner->indexOf(group) : -1});
+		addGroupGeometry(state, group, seen);
+	}
+	return state;
+}
+
+// Validate the hierarchy and plan the insertion order before making changes.
+// Geometry conversion belongs only to the original group/ungroup operation.
+bool restoreGroupSnapshot(ScribusDoc* doc, const GroupSnapshot& state)
+{
+	QSet<PageItem*> recorded;
+	QList<GroupItemPlace> pending;
+	for (const auto& place : state.places)
+	{
+		PageItem* item = place.item;
+		if (!item || recorded.contains(item) || place.index < -1)
+			return false;
+		recorded.insert(item);
+		if (place.index >= 0)
+			pending.append(place);
+	}
+	for (const auto& place : pending)
+	{
+		PageItem* parent = place.parent;
+		if (!parent)
+			continue;
+		if (!parent->isGroup() || parent == place.item)
+			return false;
+		if (doc->parentGroup(parent, doc->Items))
+			continue;
+		const bool parentWillAttach = std::any_of(pending.cbegin(), pending.cend(),
+			[parent](const GroupItemPlace& candidate) { return candidate.item == parent; });
+		if (!parentWillAttach)
+			return false;
+	}
+	for (const auto& geometry : state.geometry)
+	{
+		if (!geometry.item)
+			return false;
+	}
+
+	// Produce a parent-first, index-ordered plan before touching the document.
+	// Siblings are ordered only within their own list; unrelated lists need no
+	// artificial total ordering.
+	QList<GroupItemPlace> ordered;
+	QSet<PageItem*> planned;
+	while (!pending.isEmpty())
+	{
+		int next = -1;
+		for (int i = 0; i < pending.size(); ++i)
+		{
+			const auto& place = pending.at(i);
+			PageItem* parent = place.parent;
+			if (parent && recorded.contains(parent) && !planned.contains(parent))
+				continue;
+			if (next < 0 || (place.parent == pending.at(next).parent && place.index < pending.at(next).index))
+				next = i;
+		}
+		if (next < 0)
+			return false; // Parent cycle: leave the document unchanged.
+		const auto place = pending.takeAt(next);
+		ordered.append(place);
+		planned.insert(place.item);
+	}
+
+	// Detach direct children before their group, which may then be unreachable.
+	for (const auto& place : state.places)
+	{
+		PageItem* item = place.item;
+		if (QList<PageItem*>* owner = doc->parentGroup(item, doc->Items))
+			owner->removeAll(item);
+		item->Parent = nullptr;
+	}
+	for (const auto& place : ordered)
+	{
+		PageItem* item = place.item;
+		PageItem* parent = place.parent;
+		QList<PageItem*>* owner = parent ? &parent->groupItemList : doc->Items;
+		owner->insert(qBound(0, place.index, static_cast<int>(owner->size())), item);
+		item->Parent = parent;
+	}
+	for (const auto& geometry : state.geometry)
+		geometry.restore();
+	for (const auto& geometry : state.geometry)
+		geometry.item->setRedrawBounding();
+	return true;
+}
+} // namespace
+
 void ScribusDoc::restoreGrouping(SimpleState* ss, bool isUndo)
 {
-	auto* is = dynamic_cast<ScItemState<QList<QPointer<PageItem> > >*>(ss);
-	if (!is)
-	{
-		qFatal("ScribusDoc::restoreGrouping: dynamic cast failed");
-		return;
-	}
-
-	QList<QPointer<PageItem> > select = is->getItem();
+	auto* state = dynamic_cast<GroupUndoState*>(ss);
+	if (!state)
+		return;
+
+	const GroupSnapshot& target = isUndo ? state->ungrouped : state->grouped;
 	m_Selection->delaySignalsOn();
-	for (int i = 0; i < select.count(); ++i)
-		m_Selection->removeItem(select.at(i));
-	Selection tempSelect(this, false);
-	double x, y, w, h;
-	if (isUndo)
-	{
-		tempSelect.addItem(select.first()->Parent);
-		tempSelect.getGroupRect(&x, &y, &w, &h);
-		itemSelection_UnGroupObjects(&tempSelect);
-	}
-	else
-	{
-		for (int i = 0; i < select.size() - 1; ++i)
-			tempSelect.addItem(select.at(i));
-		tempSelect.getGroupRect(&x, &y, &w, &h);
-		PageItem_Group* oldGroupItem = select.last()->asGroupFrame();
-		PageItem* newGroupItem = itemSelection_GroupObjects(false, false, &tempSelect, oldGroupItem);
-		select.removeLast();
-		select.append(newGroupItem);
-		is->setItem(select);
-	}
-	QRectF rect(x, y , w, h);
-	regionsChanged()->update(rect.adjusted(-10, -10, 20, 20));
+	for (const auto& place : target.places)
+		m_Selection->removeItem(place.item);
+	{
+		UndoBlocker undoBlocker;
+		const bool wasLoading = isLoading();
+		setLoading(true);
+		const bool restored = restoreGroupSnapshot(this, target);
+		setLoading(wasLoading);
+		if (!restored)
+			qWarning("ScribusDoc::restoreGrouping: could not restore group hierarchy");
+		else
+		{
+			invalidateAll();
+			changed();
+		}
+	}
 	m_Selection->delaySignalsOff();
+	regionsChanged()->update(QRectF());
 }
 
 void ScribusDoc::restoreMarks(UndoState* state, bool isUndo)
@@ -15982,16 +16206,16 @@
 	itemList.append(groupItem);
 }
 
-PageItem * ScribusDoc::itemSelection_GroupObjects(bool changeLock, bool lock, Selection* customSelection, PageItem_Group* groupItem)
+PageItem* ScribusDoc::itemSelection_GroupObjects(bool changeLock, bool lock, Selection* customSelection, PageItem_Group* groupItem)
 {
 	Selection* itemSelection = (customSelection != nullptr) ? customSelection : m_Selection;
 	if (itemSelection->count() < 1)
 		return nullptr;
-	int objectsLayer = itemSelection->objectsLayer();
+	const int objectsLayer = itemSelection->objectsLayer();
 	if (objectsLayer == -1)
 		return nullptr;
-	PageItem *currItem;
-	
+	PageItem* currItem;
+
 	// Remove from selection any item which may already be grouped
 	QList<PageItem*> selectedItems = itemSelection->items();
 	for (int i = 0; i < selectedItems.count(); ++i)
@@ -16004,30 +16228,28 @@
 		}
 	}
 
-	int selectedItemCount = selectedItems.count();
+	const int selectedItemCount = selectedItems.count();
 	if (selectedItemCount <= 1)
 		return nullptr;
 
 	// Sort selection so as to preserve item levels
 	std::stable_sort(selectedItems.begin(), selectedItems.end(), compareItemLevel);
+	GroupSnapshot beforeGrouping;
+	if (UndoManager::undoEnabled())
+		beforeGrouping = captureGroupSnapshot(this, nullptr, selectedItems);
 
 	UndoTransaction activeTransaction;
 	if (UndoManager::undoEnabled())
 		activeTransaction = m_undoManager->beginTransaction(Um::Selection, Um::IGroup, Um::Group, "", Um::IGroup);
-	QString tooltip = Um::ItemsInvolved + "\n";
-	if (selectedItemCount > Um::ItemsInvolvedLimit)
-		tooltip = Um::ItemsInvolved2 + "\n";
 	if (changeLock)
 	{
 		for (int i = 0; i < selectedItemCount; ++i)
 		{
 			currItem = selectedItems.at(i);
 			currItem->setLocked(lock);
-			if (selectedItemCount <= Um::ItemsInvolvedLimit)
-				tooltip += "\t" + currItem->getUName() + "\n";
-		}
-	}
-	
+		}
+	}
+
 	double x, y, w, h;
 	Selection tempSelection(this, false);
 	tempSelection.addItems(selectedItems);
@@ -16090,10 +16312,11 @@
 
 	if (UndoManager::undoEnabled())
 	{
-		auto *is = new ScItemState<QList<QPointer<PageItem> > >(UndoManager::Group);
+		auto* is = new GroupUndoState(UndoManager::Group);
 		is->set("GROUP");
-		tempSelection.addItem(groupItem);
-		is->setItem(tempSelection.selectionList());
+		is->ungrouped = beforeGrouping;
+		is->ungrouped.places.append({groupItem, nullptr, -1});
+		is->grouped = captureGroupSnapshot(this, groupItem, selectedItems);
 		m_undoManager->action(this, is);
 	}
 	if (activeTransaction)
@@ -16106,7 +16329,6 @@
 	if (needTextInteractionCheck)
 		invalidateRegion(regionToUpdate);
 	regionsChanged()->update(needTextInteractionCheck ? QRectF() : regionToUpdate);
-	//emit docChanged();
 	changed();
 
 	if (m_ScMW && ScCore->usingGUI())
@@ -16123,18 +16345,17 @@
 	Selection* itemSelection = (customSelection != nullptr) ? customSelection : m_Selection;
 	if (itemSelection->isEmpty())
 		return;
-	
+
 	bool wasLoad = isLoading();
 	bool needTextInteractionCheck = false;
 	int docSelectionCount = itemSelection->count();
-	PageItem *currItem;
+	PageItem* currItem;
 
 	UndoTransaction activeTransaction;
 	if (UndoManager::undoEnabled())
 		activeTransaction = m_undoManager->beginTransaction(Um::Selection, Um::IGroup, Um::Ungroup, "", Um::IGroup);
 
 	QList<PageItem*> toDelete;
-//	QRectF textInteractionRect;
 	for (int i = 0; i < docSelectionCount; ++i)
 	{
 		currItem = itemSelection->itemAt(i);
@@ -16151,8 +16372,10 @@
 	for (int i = 0; i < toDelete.count(); i++)
 	{
 		currItem = toDelete.at(i);
-		QList<PageItem*> *list = Items;
-		list = parentGroup(currItem, Items);
+		GroupSnapshot beforeUngrouping;
+		if (UndoManager::undoEnabled())
+			beforeUngrouping = captureGroupSnapshot(this, currItem, currItem->groupItemList);
+		QList<PageItem*>* list = parentGroup(currItem, Items);
 		int d = list->indexOf(currItem);
 		if (d >= 0)
 			list->removeAt(d);
@@ -16178,12 +16401,10 @@
 		}
 		if (UndoManager::undoEnabled())
 		{
-			auto *is = new ScItemState<QList<QPointer<PageItem> > >(UndoManager::Ungroup);
+			auto* is = new GroupUndoState(UndoManager::Ungroup);
 			is->set("UNGROUP");
-			Selection tempSelection(this, false);
-			tempSelection.addItems(oldGroupItems);
-			tempSelection.addItem(currItem);
-			is->setItem(tempSelection.selectionList());
+			is->grouped = beforeUngrouping;
+			is->ungrouped = captureGroupSnapshot(this, currItem, oldGroupItems);
 			m_undoManager->action(this, is);
 		}
 	}
@@ -16207,7 +16428,6 @@
 	double x, y, w, h;
 	itemSelection->connectItemToGUI();
 	itemSelection->getGroupRect(&x, &y, &w, &h);
-	//emit docChanged();
 	changed();
 	if (itemSelection->count() > 0)
 		m_ScMW->HaveNewSel();
Index: scribus/scpage.cpp
===================================================================
--- scribus/scpage.cpp	(revision 27888)
+++ scribus/scpage.cpp	(working copy)
@@ -383,8 +383,23 @@
 	{
 		if ((stateCode == 0) || (stateCode == 1))
 			m_Doc->view()->deselectItems(true);
-		m_Doc->Items->append(ite);
-		ite->OwnPage = m_Doc->OnPage(ite);
+		// A previous restoration may already have reattached the object.
+		// A grouped item must never also exist in the document root list.
+		if (!m_Doc->parentGroup(ite, m_Doc->Items))
+		{
+			if (!ite->Parent)
+			{
+				m_Doc->Items->append(ite);
+				ite->OwnPage = m_Doc->OnPage(ite);
+			}
+			else if (m_Doc->parentGroup(ite->Parent, m_Doc->Items))
+			{
+				ite->Parent->groupItemList.append(ite);
+				ite->OwnPage = m_Doc->OnPage(ite);
+			}
+			else
+				qWarning("ScPage::restorePageItemCreation: item has a detached parent group");
+		}
 	}
 	if ((stateCode == 0) || (stateCode == 2))
 		update();
Index: scribus/scribusview.cpp
===================================================================
--- scribus/scribusview.cpp	(revision 27888)
+++ scribus/scribusview.cpp	(working copy)
@@ -1004,6 +1004,11 @@
 								undoManager->setUndoEnabled(false);
 								m_doc->m_Selection->getGroupRect(&x2, &y2, &w, &h);
 								m_doc->moveGroup(dropPosDoc.x() - x2, dropPosDoc.y() - y2);
+								// Imported items are already represented by creation undo actions.
+								// Their placement must become the baseline for later edits,
+								// including the children of imported nested groups.
+								for (int i = 0; i < m_doc->m_Selection->count(); ++i)
+									m_doc->m_Selection->itemAt(i)->syncUndoBaseline();
 								m_ScMW->requestUpdate(reqColorsUpdate | reqSymbolsUpdate | reqTextStylesUpdate | reqLineStylesUpdate);
 								undoManager->setUndoEnabled(true);
 							}
@@ -1032,7 +1037,13 @@
 				PageItem *newItem = m_doc->m_Selection->itemAt(0);
 				if ((newItem->width() > m_doc->currentPage()->width()) || (newItem->height() > m_doc->currentPage()->height()))
 				{
-					m_doc->rescaleGroup(newItem, qMin(qMin(m_doc->currentPage()->width() / newItem->width(), m_doc->currentPage()->height() / newItem->height()), 1.0));
+					// Auto-fit is part of the imported item's initial geometry, not
+					// an independently undoable resize following its creation.
+					{
+						UndoBlocker undoBlocker;
+						m_doc->rescaleGroup(newItem, qMin(qMin(m_doc->currentPage()->width() / newItem->width(), m_doc->currentPage()->height() / newItem->height()), 1.0));
+						newItem->syncUndoBaseline();
+					}
 					newItem->update();
 				}
 			}
gur1-group-ungroup-undo-redo-integrity-v1.05-MODULAR.patch (27,822 bytes)   
Index: scribus/CMakeLists_Sources.txt
===================================================================
--- scribus/CMakeLists_Sources.txt	(revision 27888)
+++ scribus/CMakeLists_Sources.txt	(working copy)
@@ -211,6 +211,7 @@
 	scribusapp.cpp
 	scribuscore.cpp
 	scribusdoc.cpp
+	scribusdoc_grouping.cpp
 	scribusview.cpp
 	scribuswin.cpp
 	scslainforeader.cpp
Index: scribus/scpage.cpp
===================================================================
--- scribus/scpage.cpp	(revision 27888)
+++ scribus/scpage.cpp	(working copy)
@@ -383,8 +383,23 @@
 	{
 		if ((stateCode == 0) || (stateCode == 1))
 			m_Doc->view()->deselectItems(true);
-		m_Doc->Items->append(ite);
-		ite->OwnPage = m_Doc->OnPage(ite);
+		// A previous restoration may already have reattached the object.
+		// A grouped item must never also exist in the document root list.
+		if (!m_Doc->parentGroup(ite, m_Doc->Items))
+		{
+			if (!ite->Parent)
+			{
+				m_Doc->Items->append(ite);
+				ite->OwnPage = m_Doc->OnPage(ite);
+			}
+			else if (m_Doc->parentGroup(ite->Parent, m_Doc->Items))
+			{
+				ite->Parent->groupItemList.append(ite);
+				ite->OwnPage = m_Doc->OnPage(ite);
+			}
+			else
+				qWarning("ScPage::restorePageItemCreation: item has a detached parent group");
+		}
 	}
 	if ((stateCode == 0) || (stateCode == 2))
 		update();
Index: scribus/scribusdoc.cpp
===================================================================
--- scribus/scribusdoc.cpp	(revision 27888)
+++ scribus/scribusdoc.cpp	(working copy)
@@ -2124,43 +2124,6 @@
 	}
 	setMasterPageMode(oldMPMode);
 	setCurrentPage(oldPage);
-}
-
-void ScribusDoc::restoreGrouping(SimpleState* ss, bool isUndo)
-{
-	auto* is = dynamic_cast<ScItemState<QList<QPointer<PageItem> > >*>(ss);
-	if (!is)
-	{
-		qFatal("ScribusDoc::restoreGrouping: dynamic cast failed");
-		return;
-	}
-
-	QList<QPointer<PageItem> > select = is->getItem();
-	m_Selection->delaySignalsOn();
-	for (int i = 0; i < select.count(); ++i)
-		m_Selection->removeItem(select.at(i));
-	Selection tempSelect(this, false);
-	double x, y, w, h;
-	if (isUndo)
-	{
-		tempSelect.addItem(select.first()->Parent);
-		tempSelect.getGroupRect(&x, &y, &w, &h);
-		itemSelection_UnGroupObjects(&tempSelect);
-	}
-	else
-	{
-		for (int i = 0; i < select.size() - 1; ++i)
-			tempSelect.addItem(select.at(i));
-		tempSelect.getGroupRect(&x, &y, &w, &h);
-		PageItem_Group* oldGroupItem = select.last()->asGroupFrame();
-		PageItem* newGroupItem = itemSelection_GroupObjects(false, false, &tempSelect, oldGroupItem);
-		select.removeLast();
-		select.append(newGroupItem);
-		is->setItem(select);
-	}
-	QRectF rect(x, y , w, h);
-	regionsChanged()->update(rect.adjusted(-10, -10, 20, 20));
-	m_Selection->delaySignalsOff();
 }
 
 void ScribusDoc::restoreMarks(UndoState* state, bool isUndo)
@@ -15982,242 +15945,6 @@
 	itemList.append(groupItem);
 }
 
-PageItem * ScribusDoc::itemSelection_GroupObjects(bool changeLock, bool lock, Selection* customSelection, PageItem_Group* groupItem)
-{
-	Selection* itemSelection = (customSelection != nullptr) ? customSelection : m_Selection;
-	if (itemSelection->count() < 1)
-		return nullptr;
-	int objectsLayer = itemSelection->objectsLayer();
-	if (objectsLayer == -1)
-		return nullptr;
-	PageItem *currItem;
-	
-	// Remove from selection any item which may already be grouped
-	QList<PageItem*> selectedItems = itemSelection->items();
-	for (int i = 0; i < selectedItems.count(); ++i)
-	{
-		currItem = selectedItems.at(i);
-		if (currItem->Parent)
-		{
-			selectedItems.removeAt(i);
-			--i;
-		}
-	}
-
-	int selectedItemCount = selectedItems.count();
-	if (selectedItemCount <= 1)
-		return nullptr;
-
-	// Sort selection so as to preserve item levels
-	std::stable_sort(selectedItems.begin(), selectedItems.end(), compareItemLevel);
-
-	UndoTransaction activeTransaction;
-	if (UndoManager::undoEnabled())
-		activeTransaction = m_undoManager->beginTransaction(Um::Selection, Um::IGroup, Um::Group, "", Um::IGroup);
-	QString tooltip = Um::ItemsInvolved + "\n";
-	if (selectedItemCount > Um::ItemsInvolvedLimit)
-		tooltip = Um::ItemsInvolved2 + "\n";
-	if (changeLock)
-	{
-		for (int i = 0; i < selectedItemCount; ++i)
-		{
-			currItem = selectedItems.at(i);
-			currItem->setLocked(lock);
-			if (selectedItemCount <= Um::ItemsInvolvedLimit)
-				tooltip += "\t" + currItem->getUName() + "\n";
-		}
-	}
-	
-	double x, y, w, h;
-	Selection tempSelection(this, false);
-	tempSelection.addItems(selectedItems);
-	tempSelection.getVisualGroupRect(&x, &y, &w, &h);
-
-	int lowestItem = std::numeric_limits<int>::max();
-	for (int i = 0; i < selectedItemCount; ++i)
-	{
-		currItem = selectedItems.at(i);
-		currItem->gXpos = currItem->xPos() - x;
-		currItem->gYpos = currItem->yPos() - y;
-		currItem->gWidth = w;
-		currItem->gHeight = h;
-		lowestItem = qMin(lowestItem, Items->indexOf(currItem));
-	}
-
-	bool needTextInteractionCheck = false;
-	double minx =  std::numeric_limits<double>::max();
-	double miny =  std::numeric_limits<double>::max();
-	double maxx = -std::numeric_limits<double>::max();
-	double maxy = -std::numeric_limits<double>::max();
-	for (int i = 0; i < selectedItemCount; ++i)
-	{
-		const PageItem* currItem = selectedItems.at(i);
-		double x1, x2, y1, y2;
-		currItem->getVisualBoundingRect(&x1, &y1, &x2, &y2);
-		minx = qMin(minx, x1);
-		miny = qMin(miny, y1);
-		maxx = qMax(maxx, x2);
-		maxy = qMax(maxy, y2);
-		needTextInteractionCheck |= currItem->textFlowAroundObject();
-	}
-
-	double gx = minx;
-	double gy = miny;
-	double gw = maxx - minx;
-	double gh = maxy - miny;
-
-	m_undoManager->setUndoEnabled(false);
-	if (!groupItem)
-	{
-		int z = itemAdd(PageItem::Group, PageItem::Rectangle, gx, gy, gw, gh, 0, CommonStrings::None, CommonStrings::None);
-		groupItem = Items->takeAt(z)->asGroupFrame();
-	}
-	Items->insert(lowestItem, groupItem);
-	groupItem->setItemName( tr("Group%1").arg(GroupCounter));
-	groupItem->AutoName = false;
-	groupItem->groupWidth = gw;
-	groupItem->groupHeight = gh;
-	groupItem->m_layerID = objectsLayer;
-	m_undoManager->setUndoEnabled(true);
-	for (int i = 0; i < selectedItemCount; ++i)
-	{
-		currItem = selectedItems.at(i);
-		int d = Items->indexOf(currItem);
-		groupItem->groupItemList.append(Items->takeAt(d));
-		currItem->Parent = groupItem;
-	}
-	groupItem->asGroupFrame()->adjustXYPosition();
-
-	if (UndoManager::undoEnabled())
-	{
-		auto *is = new ScItemState<QList<QPointer<PageItem> > >(UndoManager::Group);
-		is->set("GROUP");
-		tempSelection.addItem(groupItem);
-		is->setItem(tempSelection.selectionList());
-		m_undoManager->action(this, is);
-	}
-	if (activeTransaction)
-		activeTransaction.commit();
-	itemSelection->clear();
-	itemSelection->addItem(groupItem);
-
-	GroupCounter++;
-	QRectF regionToUpdate(gx - 5, gy - 5, gw + 10, gh + 10);
-	if (needTextInteractionCheck)
-		invalidateRegion(regionToUpdate);
-	regionsChanged()->update(needTextInteractionCheck ? QRectF() : regionToUpdate);
-	//emit docChanged();
-	changed();
-
-	if (m_ScMW && ScCore->usingGUI())
-	{
-		m_ScMW->scrActions["itemAttachTextToPath"]->setEnabled(false);
-		m_ScMW->scrActions["itemGroup"]->setEnabled(false);
-		m_ScMW->scrActions["itemUngroup"]->setEnabled(true);
-	}
-	return groupItem;
-}
-
-void ScribusDoc::itemSelection_UnGroupObjects(Selection* customSelection)
-{
-	Selection* itemSelection = (customSelection != nullptr) ? customSelection : m_Selection;
-	if (itemSelection->isEmpty())
-		return;
-	
-	bool wasLoad = isLoading();
-	bool needTextInteractionCheck = false;
-	int docSelectionCount = itemSelection->count();
-	PageItem *currItem;
-
-	UndoTransaction activeTransaction;
-	if (UndoManager::undoEnabled())
-		activeTransaction = m_undoManager->beginTransaction(Um::Selection, Um::IGroup, Um::Ungroup, "", Um::IGroup);
-
-	QList<PageItem*> toDelete;
-//	QRectF textInteractionRect;
-	for (int i = 0; i < docSelectionCount; ++i)
-	{
-		currItem = itemSelection->itemAt(i);
-		if (!currItem->isGroup())
-			continue;
-		needTextInteractionCheck |= currItem->textFlowAroundObject();
-		toDelete.append(currItem);
-	}
-
-	// Remove group control objects
-	setLoading(true);
-	itemSelection->delaySignalsOn();
-	dontResize = true;
-	for (int i = 0; i < toDelete.count(); i++)
-	{
-		currItem = toDelete.at(i);
-		QList<PageItem*> *list = Items;
-		list = parentGroup(currItem, Items);
-		int d = list->indexOf(currItem);
-		if (d >= 0)
-			list->removeAt(d);
-		itemSelection->removeItem(currItem);
-		QList<PageItem*> oldGroupItems = currItem->groupItemList;
-		int gcount = currItem->groupItemList.count();
-		for (int j = 0; j < gcount; j++)
-		{
-			PageItem* gItem = currItem->groupItemList.last();
-			needTextInteractionCheck |= gItem->textFlowAroundObject();
-			removeFromGroup(gItem);
-			if (currItem->isGroupChild())
-			{
-				addToGroup(currItem->Parent, gItem);
-				list->insert(d, gItem);
-			}
-			else
-			{
-				Items->insert(d, gItem);
-				gItem->OwnPage = OnPage(gItem);
-			}
-			itemSelection->addItem(gItem);
-		}
-		if (UndoManager::undoEnabled())
-		{
-			auto *is = new ScItemState<QList<QPointer<PageItem> > >(UndoManager::Ungroup);
-			is->set("UNGROUP");
-			Selection tempSelection(this, false);
-			tempSelection.addItems(oldGroupItems);
-			tempSelection.addItem(currItem);
-			is->setItem(tempSelection.selectionList());
-			m_undoManager->action(this, is);
-		}
-	}
-	dontResize = false;
-	setLoading(wasLoad);
-	itemSelection->delaySignalsOff();
-
-	// Delete items after delaySignalsOff() call so that palette are updated before item deletion
-	for (int i = 0; i < toDelete.count(); i++)
-	{
-		currItem = toDelete.at(i);
-		if (currItem->isWelded())
-			currItem->unWeld();
-		if (!UndoManager::undoEnabled() && !isUndoRedoOngoing())
-			delete currItem;
-	}
-
-	if (activeTransaction)
-		activeTransaction.commit();
-
-	double x, y, w, h;
-	itemSelection->connectItemToGUI();
-	itemSelection->getGroupRect(&x, &y, &w, &h);
-	//emit docChanged();
-	changed();
-	if (itemSelection->count() > 0)
-		m_ScMW->HaveNewSel();
-
-	QRectF regionToUpdate(x - 5, y - 5, w + 10, h + 10);
-	if (needTextInteractionCheck)
-		invalidateRegion(regionToUpdate);
-	regionsChanged()->update(needTextInteractionCheck ? QRectF() : regionToUpdate);
-}
-
 void ScribusDoc::addToGroup(PageItem* group, PageItem* item)
 {
 	QTransform groupTrans = group->getTransform();
Index: scribus/scribusdoc_grouping.cpp
===================================================================
--- scribus/scribusdoc_grouping.cpp	(nonexistent)
+++ scribus/scribusdoc_grouping.cpp	(working copy)
@@ -0,0 +1,519 @@
+/*
+For general Scribus (>=1.3.2) copyright and licensing information please refer
+to the COPYING file provided with the program.
+*/
+
+#include <algorithm>
+#include <limits>
+#include <memory>
+
+#include <QDebug>
+#include <QPointer>
+#include <QRectF>
+#include <QSet>
+
+#include "commonstrings.h"
+#include "pageitem.h"
+#include "pageitem_arc.h"
+#include "pageitem_group.h"
+#include "scribus.h"
+#include "scribuscore.h"
+#include "scribusdoc.h"
+#include "scraction.h"
+#include "selection.h"
+#include "text/storytext.h"
+#include "undomanager.h"
+#include "util.h"
+
+// Group/ungroup is an ownership change *and* a coordinate-space change.
+// Save both sides of the operation: replaying addToGroup/removeFromGroup
+// repeatedly loses geometry (especially with scaled, rotated nested groups).
+namespace
+{
+struct GroupItemGeometry
+{
+	QPointer<PageItem> item;
+	double x { 0 }, y { 0 }, width { 0 }, height { 0 }, rotation { 0 };
+	double groupX { 0 }, groupY { 0 }, groupWidth { 0 }, groupHeight { 0 };
+	double lineWidth { 0 }, imageScaleX { 0 }, imageScaleY { 0 };
+	bool flipH { false }, flipV { false };
+	int ownPage { -1 };
+	FPointArray path;
+	FPointArray contour;
+	FPointArray imageClip;
+	QList<uint> segments;
+	QPolygon clip;
+	ScPatternTransform pattern;
+	ScMaskTransform mask;
+	double arcWidth { 0 }, arcHeight { 0 };
+	std::shared_ptr<const StoryText> text;
+
+	explicit GroupItemGeometry(PageItem* p) : item(p)
+	{
+		x = p->xPos();
+		y = p->yPos();
+		width = p->width();
+		height = p->height();
+		rotation = p->rotation();
+		groupX = p->gXpos;
+		groupY = p->gYpos;
+		groupWidth = p->gWidth;
+		groupHeight = p->gHeight;
+		lineWidth = p->lineWidth();
+		imageScaleX = p->imageXScale();
+		imageScaleY = p->imageYScale();
+		ownPage = p->OwnPage;
+		flipH = p->imageFlippedH();
+		flipV = p->imageFlippedV();
+		path = p->PoLine;
+		contour = p->ContourLine;
+		imageClip = p->imageClip;
+		segments = p->Segments;
+		clip = p->Clip;
+		pattern = p->patternTransform();
+		mask = p->maskTransform();
+		if (const auto* arc = p->asArc())
+		{
+			arcWidth = arc->arcWidth;
+			arcHeight = arc->arcHeight;
+		}
+		if ((p->isTextFrame() || p->isPathText()) && !p->itemText.isEmpty())
+			text = std::make_shared<StoryText>(p->itemText);
+	}
+
+	void restore() const
+	{
+		PageItem* p = item;
+		p->setXYPos(x, y, true);
+		p->setWidthHeight(width, height, true);
+		p->setRotation(rotation, true);
+		p->gXpos = groupX;
+		p->gYpos = groupY;
+		p->gWidth = groupWidth;
+		p->gHeight = groupHeight;
+		p->OwnPage = ownPage;
+		p->setLineWidth(lineWidth);
+		p->setImageXScale(imageScaleX);
+		p->setImageYScale(imageScaleY);
+		p->setImageFlippedH(flipH);
+		p->setImageFlippedV(flipV);
+		p->PoLine = path;
+		p->ContourLine = contour;
+		p->imageClip = imageClip;
+		p->Segments = segments;
+		p->Clip = clip;
+		p->setPatternTransform(pattern);
+		p->setMaskTransform(mask);
+		if (auto* arc = p->asArc())
+		{
+			arc->arcWidth = arcWidth;
+			arc->arcHeight = arcHeight;
+		}
+		if (text)
+			p->itemText = *text;
+		// The path already matches the restored frame dimensions. updateClip()
+		// must not apply the preceding ungroup's size conversion again.
+		p->OldB2 = width;
+		p->OldH2 = height;
+		p->syncUndoBaseline();
+	}
+};
+
+struct GroupItemPlace
+{
+	QPointer<PageItem> item;
+	QPointer<PageItem> parent;
+	int index { -1 }; // -1: detached (original group before creation/after ungroup)
+};
+
+struct GroupSnapshot
+{
+	QList<GroupItemPlace> places; // direct children, followed by the original group
+	QList<GroupItemGeometry> geometry; // also includes descendants of nested groups
+};
+
+class GroupUndoState final : public SimpleState
+{
+public:
+	using SimpleState::SimpleState;
+	GroupSnapshot grouped;
+	GroupSnapshot ungrouped;
+};
+
+void addGroupGeometry(GroupSnapshot& state, PageItem* item, QSet<PageItem*>& seen)
+{
+	if (!item || seen.contains(item))
+		return;
+	seen.insert(item);
+	state.geometry.append(GroupItemGeometry(item));
+	if (item->isGroup())
+	{
+		for (PageItem* child : item->groupItemList)
+			addGroupGeometry(state, child, seen);
+	}
+}
+
+GroupSnapshot captureGroupSnapshot(ScribusDoc* doc, PageItem* group, const QList<PageItem*>& children)
+{
+	GroupSnapshot state;
+	QSet<PageItem*> seen;
+	for (PageItem* item : children)
+	{
+		QList<PageItem*>* owner = doc->parentGroup(item, doc->Items);
+		state.places.append({item, (owner && owner != doc->Items) ? item->Parent : nullptr,
+		                     owner ? owner->indexOf(item) : -1});
+		addGroupGeometry(state, item, seen);
+	}
+	if (group)
+	{
+		QList<PageItem*>* owner = doc->parentGroup(group, doc->Items);
+		state.places.append({group, (owner && owner != doc->Items) ? group->Parent : nullptr,
+		                     owner ? owner->indexOf(group) : -1});
+		addGroupGeometry(state, group, seen);
+	}
+	return state;
+}
+
+// Validate the hierarchy and plan the insertion order before making changes.
+// Geometry conversion belongs only to the original group/ungroup operation.
+bool restoreGroupSnapshot(ScribusDoc* doc, const GroupSnapshot& state)
+{
+	QSet<PageItem*> recorded;
+	QList<GroupItemPlace> pending;
+	for (const auto& place : state.places)
+	{
+		PageItem* item = place.item;
+		if (!item || recorded.contains(item) || place.index < -1)
+			return false;
+		recorded.insert(item);
+		if (place.index >= 0)
+			pending.append(place);
+	}
+	for (const auto& place : pending)
+	{
+		PageItem* parent = place.parent;
+		if (!parent)
+			continue;
+		if (!parent->isGroup() || parent == place.item)
+			return false;
+		if (doc->parentGroup(parent, doc->Items))
+			continue;
+		const bool parentWillAttach = std::any_of(pending.cbegin(), pending.cend(),
+			[parent](const GroupItemPlace& candidate) { return candidate.item == parent; });
+		if (!parentWillAttach)
+			return false;
+	}
+	for (const auto& geometry : state.geometry)
+	{
+		if (!geometry.item)
+			return false;
+	}
+
+	// Produce a parent-first, index-ordered plan before touching the document.
+	// Siblings are ordered only within their own list; unrelated lists need no
+	// artificial total ordering.
+	QList<GroupItemPlace> ordered;
+	QSet<PageItem*> planned;
+	while (!pending.isEmpty())
+	{
+		int next = -1;
+		for (int i = 0; i < pending.size(); ++i)
+		{
+			const auto& place = pending.at(i);
+			PageItem* parent = place.parent;
+			if (parent && recorded.contains(parent) && !planned.contains(parent))
+				continue;
+			if (next < 0 || (place.parent == pending.at(next).parent && place.index < pending.at(next).index))
+				next = i;
+		}
+		if (next < 0)
+			return false; // Parent cycle: leave the document unchanged.
+		const auto place = pending.takeAt(next);
+		ordered.append(place);
+		planned.insert(place.item);
+	}
+
+	// Detach direct children before their group, which may then be unreachable.
+	for (const auto& place : state.places)
+	{
+		PageItem* item = place.item;
+		if (QList<PageItem*>* owner = doc->parentGroup(item, doc->Items))
+			owner->removeAll(item);
+		item->Parent = nullptr;
+	}
+	for (const auto& place : ordered)
+	{
+		PageItem* item = place.item;
+		PageItem* parent = place.parent;
+		QList<PageItem*>* owner = parent ? &parent->groupItemList : doc->Items;
+		owner->insert(qBound(0, place.index, static_cast<int>(owner->size())), item);
+		item->Parent = parent;
+	}
+	for (const auto& geometry : state.geometry)
+		geometry.restore();
+	for (const auto& geometry : state.geometry)
+		geometry.item->setRedrawBounding();
+	return true;
+}
+} // namespace
+
+void ScribusDoc::restoreGrouping(SimpleState* ss, bool isUndo)
+{
+	auto* state = dynamic_cast<GroupUndoState*>(ss);
+	if (!state)
+		return;
+
+	const GroupSnapshot& target = isUndo ? state->ungrouped : state->grouped;
+	m_Selection->delaySignalsOn();
+	for (const auto& place : target.places)
+		m_Selection->removeItem(place.item);
+	{
+		UndoBlocker undoBlocker;
+		const bool wasLoading = isLoading();
+		setLoading(true);
+		const bool restored = restoreGroupSnapshot(this, target);
+		setLoading(wasLoading);
+		if (!restored)
+			qWarning("ScribusDoc::restoreGrouping: could not restore group hierarchy");
+		else
+		{
+			invalidateAll();
+			changed();
+		}
+	}
+	m_Selection->delaySignalsOff();
+	regionsChanged()->update(QRectF());
+}
+
+PageItem* ScribusDoc::itemSelection_GroupObjects(bool changeLock, bool lock, Selection* customSelection, PageItem_Group* groupItem)
+{
+	Selection* itemSelection = (customSelection != nullptr) ? customSelection : m_Selection;
+	if (itemSelection->count() < 1)
+		return nullptr;
+	const int objectsLayer = itemSelection->objectsLayer();
+	if (objectsLayer == -1)
+		return nullptr;
+	PageItem* currItem;
+
+	// Remove from selection any item which may already be grouped
+	QList<PageItem*> selectedItems = itemSelection->items();
+	for (int i = 0; i < selectedItems.count(); ++i)
+	{
+		currItem = selectedItems.at(i);
+		if (currItem->Parent)
+		{
+			selectedItems.removeAt(i);
+			--i;
+		}
+	}
+
+	const int selectedItemCount = selectedItems.count();
+	if (selectedItemCount <= 1)
+		return nullptr;
+
+	// Sort selection so as to preserve item levels
+	std::stable_sort(selectedItems.begin(), selectedItems.end(), compareItemLevel);
+	GroupSnapshot beforeGrouping;
+	if (UndoManager::undoEnabled())
+		beforeGrouping = captureGroupSnapshot(this, nullptr, selectedItems);
+
+	UndoTransaction activeTransaction;
+	if (UndoManager::undoEnabled())
+		activeTransaction = m_undoManager->beginTransaction(Um::Selection, Um::IGroup, Um::Group, "", Um::IGroup);
+	if (changeLock)
+	{
+		for (int i = 0; i < selectedItemCount; ++i)
+		{
+			currItem = selectedItems.at(i);
+			currItem->setLocked(lock);
+		}
+	}
+
+	double x, y, w, h;
+	Selection tempSelection(this, false);
+	tempSelection.addItems(selectedItems);
+	tempSelection.getVisualGroupRect(&x, &y, &w, &h);
+
+	int lowestItem = std::numeric_limits<int>::max();
+	for (int i = 0; i < selectedItemCount; ++i)
+	{
+		currItem = selectedItems.at(i);
+		currItem->gXpos = currItem->xPos() - x;
+		currItem->gYpos = currItem->yPos() - y;
+		currItem->gWidth = w;
+		currItem->gHeight = h;
+		lowestItem = qMin(lowestItem, Items->indexOf(currItem));
+	}
+
+	bool needTextInteractionCheck = false;
+	double minx =  std::numeric_limits<double>::max();
+	double miny =  std::numeric_limits<double>::max();
+	double maxx = -std::numeric_limits<double>::max();
+	double maxy = -std::numeric_limits<double>::max();
+	for (int i = 0; i < selectedItemCount; ++i)
+	{
+		const PageItem* currItem = selectedItems.at(i);
+		double x1, x2, y1, y2;
+		currItem->getVisualBoundingRect(&x1, &y1, &x2, &y2);
+		minx = qMin(minx, x1);
+		miny = qMin(miny, y1);
+		maxx = qMax(maxx, x2);
+		maxy = qMax(maxy, y2);
+		needTextInteractionCheck |= currItem->textFlowAroundObject();
+	}
+
+	double gx = minx;
+	double gy = miny;
+	double gw = maxx - minx;
+	double gh = maxy - miny;
+
+	m_undoManager->setUndoEnabled(false);
+	if (!groupItem)
+	{
+		int z = itemAdd(PageItem::Group, PageItem::Rectangle, gx, gy, gw, gh, 0, CommonStrings::None, CommonStrings::None);
+		groupItem = Items->takeAt(z)->asGroupFrame();
+	}
+	Items->insert(lowestItem, groupItem);
+	groupItem->setItemName( tr("Group%1").arg(GroupCounter));
+	groupItem->AutoName = false;
+	groupItem->groupWidth = gw;
+	groupItem->groupHeight = gh;
+	groupItem->m_layerID = objectsLayer;
+	m_undoManager->setUndoEnabled(true);
+	for (int i = 0; i < selectedItemCount; ++i)
+	{
+		currItem = selectedItems.at(i);
+		int d = Items->indexOf(currItem);
+		groupItem->groupItemList.append(Items->takeAt(d));
+		currItem->Parent = groupItem;
+	}
+	groupItem->asGroupFrame()->adjustXYPosition();
+
+	if (UndoManager::undoEnabled())
+	{
+		auto* is = new GroupUndoState(UndoManager::Group);
+		is->set("GROUP");
+		is->ungrouped = beforeGrouping;
+		is->ungrouped.places.append({groupItem, nullptr, -1});
+		is->grouped = captureGroupSnapshot(this, groupItem, selectedItems);
+		m_undoManager->action(this, is);
+	}
+	if (activeTransaction)
+		activeTransaction.commit();
+	itemSelection->clear();
+	itemSelection->addItem(groupItem);
+
+	GroupCounter++;
+	QRectF regionToUpdate(gx - 5, gy - 5, gw + 10, gh + 10);
+	if (needTextInteractionCheck)
+		invalidateRegion(regionToUpdate);
+	regionsChanged()->update(needTextInteractionCheck ? QRectF() : regionToUpdate);
+	changed();
+
+	if (m_ScMW && ScCore->usingGUI())
+	{
+		m_ScMW->scrActions["itemAttachTextToPath"]->setEnabled(false);
+		m_ScMW->scrActions["itemGroup"]->setEnabled(false);
+		m_ScMW->scrActions["itemUngroup"]->setEnabled(true);
+	}
+	return groupItem;
+}
+
+void ScribusDoc::itemSelection_UnGroupObjects(Selection* customSelection)
+{
+	Selection* itemSelection = (customSelection != nullptr) ? customSelection : m_Selection;
+	if (itemSelection->isEmpty())
+		return;
+
+	bool wasLoad = isLoading();
+	bool needTextInteractionCheck = false;
+	int docSelectionCount = itemSelection->count();
+	PageItem* currItem;
+
+	UndoTransaction activeTransaction;
+	if (UndoManager::undoEnabled())
+		activeTransaction = m_undoManager->beginTransaction(Um::Selection, Um::IGroup, Um::Ungroup, "", Um::IGroup);
+
+	QList<PageItem*> toDelete;
+	for (int i = 0; i < docSelectionCount; ++i)
+	{
+		currItem = itemSelection->itemAt(i);
+		if (!currItem->isGroup())
+			continue;
+		needTextInteractionCheck |= currItem->textFlowAroundObject();
+		toDelete.append(currItem);
+	}
+
+	// Remove group control objects
+	setLoading(true);
+	itemSelection->delaySignalsOn();
+	dontResize = true;
+	for (int i = 0; i < toDelete.count(); i++)
+	{
+		currItem = toDelete.at(i);
+		GroupSnapshot beforeUngrouping;
+		if (UndoManager::undoEnabled())
+			beforeUngrouping = captureGroupSnapshot(this, currItem, currItem->groupItemList);
+		QList<PageItem*>* list = parentGroup(currItem, Items);
+		int d = list->indexOf(currItem);
+		if (d >= 0)
+			list->removeAt(d);
+		itemSelection->removeItem(currItem);
+		QList<PageItem*> oldGroupItems = currItem->groupItemList;
+		int gcount = currItem->groupItemList.count();
+		for (int j = 0; j < gcount; j++)
+		{
+			PageItem* gItem = currItem->groupItemList.last();
+			needTextInteractionCheck |= gItem->textFlowAroundObject();
+			removeFromGroup(gItem);
+			if (currItem->isGroupChild())
+			{
+				addToGroup(currItem->Parent, gItem);
+				list->insert(d, gItem);
+			}
+			else
+			{
+				Items->insert(d, gItem);
+				gItem->OwnPage = OnPage(gItem);
+			}
+			itemSelection->addItem(gItem);
+		}
+		if (UndoManager::undoEnabled())
+		{
+			auto* is = new GroupUndoState(UndoManager::Ungroup);
+			is->set("UNGROUP");
+			is->grouped = beforeUngrouping;
+			is->ungrouped = captureGroupSnapshot(this, currItem, oldGroupItems);
+			m_undoManager->action(this, is);
+		}
+	}
+	dontResize = false;
+	setLoading(wasLoad);
+	itemSelection->delaySignalsOff();
+
+	// Delete items after delaySignalsOff() call so that palette are updated before item deletion
+	for (int i = 0; i < toDelete.count(); i++)
+	{
+		currItem = toDelete.at(i);
+		if (currItem->isWelded())
+			currItem->unWeld();
+		if (!UndoManager::undoEnabled() && !isUndoRedoOngoing())
+			delete currItem;
+	}
+
+	if (activeTransaction)
+		activeTransaction.commit();
+
+	double x, y, w, h;
+	itemSelection->connectItemToGUI();
+	itemSelection->getGroupRect(&x, &y, &w, &h);
+	changed();
+	if (itemSelection->count() > 0)
+		m_ScMW->HaveNewSel();
+
+	QRectF regionToUpdate(x - 5, y - 5, w + 10, h + 10);
+	if (needTextInteractionCheck)
+		invalidateRegion(regionToUpdate);
+	regionsChanged()->update(needTextInteractionCheck ? QRectF() : regionToUpdate);
+}
+
Index: scribus/scribusview.cpp
===================================================================
--- scribus/scribusview.cpp	(revision 27888)
+++ scribus/scribusview.cpp	(working copy)
@@ -1004,6 +1004,11 @@
 								undoManager->setUndoEnabled(false);
 								m_doc->m_Selection->getGroupRect(&x2, &y2, &w, &h);
 								m_doc->moveGroup(dropPosDoc.x() - x2, dropPosDoc.y() - y2);
+								// Imported items are already represented by creation undo actions.
+								// Their placement must become the baseline for later edits,
+								// including the children of imported nested groups.
+								for (int i = 0; i < m_doc->m_Selection->count(); ++i)
+									m_doc->m_Selection->itemAt(i)->syncUndoBaseline();
 								m_ScMW->requestUpdate(reqColorsUpdate | reqSymbolsUpdate | reqTextStylesUpdate | reqLineStylesUpdate);
 								undoManager->setUndoEnabled(true);
 							}
@@ -1032,7 +1037,13 @@
 				PageItem *newItem = m_doc->m_Selection->itemAt(0);
 				if ((newItem->width() > m_doc->currentPage()->width()) || (newItem->height() > m_doc->currentPage()->height()))
 				{
-					m_doc->rescaleGroup(newItem, qMin(qMin(m_doc->currentPage()->width() / newItem->width(), m_doc->currentPage()->height() / newItem->height()), 1.0));
+					// Auto-fit is part of the imported item's initial geometry, not
+					// an independently undoable resize following its creation.
+					{
+						UndoBlocker undoBlocker;
+						m_doc->rescaleGroup(newItem, qMin(qMin(m_doc->currentPage()->width() / newItem->width(), m_doc->currentPage()->height() / newItem->height()), 1.0));
+						newItem->syncUndoBaseline();
+					}
 					newItem->update();
 				}
 			}

Issue History

Date Modified Username Field Change
2015-03-14 09:00 ale New Issue
2015-05-06 20:19 Kunda Relationship added child of 0012500
2015-05-21 12:35 Kunda Relationship added child of 0003812
2025-11-08 19:47 ale Note Added: 0053218
2026-10-16 02:08 buttar Note Added: 0054528
2026-10-16 02:08 buttar File Added: gur1-group-ungroup-undo-redo-integrity-v1.05-INPLACE.patch
2026-10-16 02:08 buttar File Added: gur1-group-ungroup-undo-redo-integrity-v1.05-MODULAR.patch
2026-10-16 02:09 buttar Tag Attached: #please_test
2026-10-17 20:07 cbradney Target Version => 1.8 Milestone
2026-10-29 17:09 cbradney Target Version 1.8 Milestone => 1.7.5.svn