From 7a857644cde5f42c38380fd71943cdf497eae67c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Simonis?= <simonisfrederic@gmail.com>
Date: Tue, 3 Aug 2021 17:15:35 +0200
Subject: [PATCH] Refactor itemNameExists

---
 scribus/pageitem.h         |  8 ++++++++
 scribus/pageitem_group.cpp | 11 +++++++++++
 scribus/pageitem_group.h   |  1 +
 scribus/scribusdoc.cpp     | 23 ++++-------------------
 4 files changed, 24 insertions(+), 19 deletions(-)

diff --git a/scribus/pageitem.h b/scribus/pageitem.h
index eead94200..f4083bc38 100644
--- a/scribus/pageitem.h
+++ b/scribus/pageitem.h
@@ -813,6 +813,14 @@ public: // Start public functions
 	 */
 	void setItemName(const QString& newName);
 
+	/**
+	 * @brief checks if this or child items have the given name.
+	 * @param checkItemName the name to search for
+	 */
+	virtual bool itemNameExists(const QString& checkItemName) const {
+		return m_itemName == checkItemName;
+	}
+
 	/**
 	* @brief Set the masterpage the object is on
 	* @param mpName name of the master page
diff --git a/scribus/pageitem_group.cpp b/scribus/pageitem_group.cpp
index 9e45a9cbf..cd7ee0a84 100644
--- a/scribus/pageitem_group.cpp
+++ b/scribus/pageitem_group.cpp
@@ -109,6 +109,17 @@ void PageItem_Group::layout()
 	}
 }
 
+bool PageItem_Group::itemNameExists(const QString& checkItemName) const {
+	if (itemName() == checkItemName)
+		return true;
+
+	for (const auto& item: groupItemList) {
+		if (item->itemNameExists(checkItemName))
+			return true;
+	}
+	return false;
+}
+
 void PageItem_Group::setLayer(int newLayerID)
 {
 	for (int em = 0; em < groupItemList.count(); ++em)
diff --git a/scribus/pageitem_group.h b/scribus/pageitem_group.h
index 7aedd2830..29b8799c6 100644
--- a/scribus/pageitem_group.h
+++ b/scribus/pageitem_group.h
@@ -45,6 +45,7 @@ public:
 	bool isTextContainer() const override;
 	ItemType realItemType() const override { return PageItem::Group; }
 
+	bool itemNameExists(const QString& checkItemName) const override;
 	void adjustXYPosition();
 	void setLayer(int layerId) override;
 	void setMasterPage(int page, const QString& mpName) override;
diff --git a/scribus/scribusdoc.cpp b/scribus/scribusdoc.cpp
index e77f8ceef..8ede7ce4e 100644
--- a/scribus/scribusdoc.cpp
+++ b/scribus/scribusdoc.cpp
@@ -6431,28 +6431,13 @@ int ScribusDoc::currentPageNumber()
 }
 
 
-bool ScribusDoc::itemNameExists(const QString& checkItemName)
+bool ScribusDoc::itemNameExists(const QString& checkItemName) const
 {
-	bool found = false;
-	QList<PageItem*> allItems;
-	int docItemCount = Items->count();
-	for (int i = 0; i < docItemCount; ++i)
-	{
-		PageItem *currItem = Items->at(i);
-		if (checkItemName == currItem->itemName())
+	for (const auto& item: *Items) {
+		if (item->itemNameExists(checkItemName))
 			return true;
-		if (currItem->isGroup())
-		{
-			allItems = currItem->getAllChildren();
-			for (int ii = 0; ii < allItems.count(); ii++)
-			{
-				if (checkItemName == allItems.at(ii)->itemName())
-					return true;
-			}
-		}
-		allItems.clear();
 	}
-	return found;
+	return false;
 }
 
 
-- 
2.32.0

