View Issue Details

IDProjectCategoryView StatusLast Update
0017399InfrastructureGeneralpublic2025-01-31 16:05
Reportertropion Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Summary0017399: Objects in groups are not accessible via the scriptplugin python API
DescriptionI suggest the attached patch
Steps To ReproduceCreate objects
Group the objects
Open the script console
Type getAllObjects()
Press F9
See that the group is returned, but not the elements of the group.

Type objectsExists('<name of obj in group>')
Press F9

See that the object is not accessible.
getGroupItems ('Group1',recursive=true)
returns all objects in Group1, but there still is no way to do anything with them.
TagsNo tags attached.

Activities

tropion

2025-01-31 16:05

reporter  

scriptplugin.diff (5,645 bytes)   
diff --git a/scribus/plugins/scriptplugin/cmdgetprop.cpp b/scribus/plugins/scriptplugin/cmdgetprop.cpp
index 3106dd471..c60a29070 100644
--- a/scribus/plugins/scriptplugin/cmdgetprop.cpp
+++ b/scribus/plugins/scriptplugin/cmdgetprop.cpp
@@ -383,7 +383,6 @@ PyObject *scribus_getallobjects(PyObject* /* self */, PyObject* args, PyObject *
 {
 	int itemType = -1;
 	int layerId = -1;
-	uint counter = 0;
 
 	if (!checkHaveDocument())
 		return nullptr;
@@ -426,16 +425,24 @@ PyObject *scribus_getallobjects(PyObject* /* self */, PyObject* args, PyObject *
 		return true;
 	};
 
-	int returnedItemCount = std::count_if(currentDoc->Items->begin(), currentDoc->Items->end(), isReturnedItem);
-
-	PyObject* pyItemList = PyList_New(returnedItemCount);
+	PyObject* pyItemList = PyList_New(0);
 	for (int i = 0; i < currentDoc->Items->count(); ++i)
 	{
 		PageItem* item = currentDoc->Items->at(i);
-		if (!isReturnedItem(item))
+		if (!isReturnedItem(item))                    // No items of wrong type or from wrong Layer
 			continue;
-		PyList_SetItem(pyItemList, counter, PyUnicode_FromString(item->itemName().toUtf8()));
-		counter++;
+		PyList_Append(pyItemList, PyUnicode_FromString(item->itemName().toUtf8()));
+		if (item->isGroup())
+		{
+			QList<PageItem*> allItems;
+			allItems = item->asGroupFrame()->getAllChildren();
+			for (PageItem *it : allItems)
+			{
+				if (!isReturnedItem(it))                    // No items of wrong type or from wrong Layer
+					continue;
+				PyList_Append(pyItemList, PyUnicode_FromString(it->itemName().toUtf8()));
+			}
+		}
 	}
 	return pyItemList;
 }
diff --git a/scribus/plugins/scriptplugin/cmdpage.cpp b/scribus/plugins/scriptplugin/cmdpage.cpp
index a99b66c1f..dfbb493ea 100644
--- a/scribus/plugins/scriptplugin/cmdpage.cpp
+++ b/scribus/plugins/scriptplugin/cmdpage.cpp
@@ -244,16 +244,10 @@ PyObject *scribus_getpageitems(PyObject* /* self */)
 
 	if (currentDoc->Items->count() == 0)
 		return Py_BuildValue("[]");
-	uint counter = 0;
+
 	int pageNr = currentDoc->currentPageNumber();
-	for (int lam2 = 0; lam2 < currentDoc->Items->count(); ++lam2)
-	{
-		if (pageNr == currentDoc->Items->at(lam2)->OwnPage)
-			counter++;
-	}
-	PyObject *l = PyList_New(counter);
+	PyObject* l = PyList_New (0); 
 	PyObject *row;
-	counter = 0;
 	for (int i = 0; i<currentDoc->Items->count(); ++i)
 	{
 		if (pageNr == currentDoc->Items->at(i)->OwnPage)
@@ -263,8 +257,26 @@ PyObject *scribus_getpageitems(PyObject* /* self */)
 			                    currentDoc->Items->at(i)->itemType(),
 								currentDoc->Items->at(i)->uniqueNr
 			                   );
-			PyList_SetItem(l, counter, row);
-			counter++;
+			PyList_Append(l, row);
+			if (currentDoc->Items->at(i)->isGroup())
+			{
+				QList<PageItem*> allItems;
+				allItems = currentDoc->Items->at(i)->asGroupFrame()->getAllChildren();
+				for (PageItem* it : allItems)
+				{
+					if (it->OwnPage == pageNr)  
+					{
+						row = Py_BuildValue("(sii)",
+							it->itemName().toUtf8().constData(),
+							it->itemType(),
+							it->uniqueNr
+						);
+						PyList_Append(l, row);
+					}
+					else
+						continue;
+				}
+			}
 		}
 	} // for
 	return l;
diff --git a/scribus/plugins/scriptplugin/cmdutil.cpp b/scribus/plugins/scriptplugin/cmdutil.cpp
index d3bd6da8f..b15593907 100644
--- a/scribus/plugins/scriptplugin/cmdutil.cpp
+++ b/scribus/plugins/scriptplugin/cmdutil.cpp
@@ -63,11 +63,23 @@ PageItem *GetItem(const QString& name)
 	ScribusDoc* currentDoc = ScCore->primaryMainWindow()->doc;
 	if (!name.isEmpty())
 	{
+		// We do not call getPageItemByName() here, 
+		// bc we do not want to raise an error in case the name is not found
 		for (int i = 0; i < currentDoc->Items->count(); ++i)
-		{
-			if (currentDoc->Items->at(i)->itemName() == name)
-				return currentDoc->Items->at(i);
-		}
+			{
+				if (currentDoc->Items->at(i)->itemName() == name)
+					 return currentDoc->Items->at(i);
+				if (currentDoc->Items->at(i)->isGroup())
+				{
+					QList<PageItem*> allItems;
+					allItems = currentDoc->Items->at(i)->asGroupFrame()->getAllChildren();
+					for (PageItem* it : allItems)
+					{
+						if (name == it->itemName())
+							return it;
+					}
+				}
+			}
 	}
 	else
 	{
@@ -77,6 +89,7 @@ PageItem *GetItem(const QString& name)
 	return nullptr;
 }
 
+
 void ReplaceColor(const QString& col, const QString& rep)
 {
 	QMap<QString, QString> colorMap;
@@ -123,6 +136,16 @@ PageItem* getPageItemByName(const QString& name)
 	{
 		if (name == currentDoc->Items->at(i)->itemName())
 			return currentDoc->Items->at(i);
+		if (currentDoc->Items->at(i)->isGroup())
+		{
+			QList<PageItem*> allItems;
+			allItems = currentDoc->Items->at(i)->asGroupFrame()->getAllChildren();
+			for (PageItem* it : allItems)
+			{
+				if (name == it->itemName())
+					return it;
+			}
+		}
 	}
 
 	PyErr_SetString(NoValidObjectError, QString("Object not found").toLocal8Bit().constData());
@@ -140,13 +163,7 @@ bool ItemExists(const QString& name)
 	if (name.length() == 0)
 		return false;
 
-	ScribusDoc* currentDoc = ScCore->primaryMainWindow()->doc;
-	for (int i = 0; i < currentDoc->Items->count(); ++i)
-	{
-		if (name == currentDoc->Items->at(i)->itemName())
-			return true;
-	}
-	return false;
+	return GetItem(name) != nullptr;
 }
 
 /*!
@@ -201,11 +218,7 @@ bool setSelectedItemsByName(const QStringList& itemNames)
 	{
 		// Search for the named item
 		PageItem* item = nullptr;
-		for (int j = 0; j < currentDoc->Items->count(); j++)
-		{
-			if (*it == currentDoc->Items->at(j)->itemName())
-				item = currentDoc->Items->at(j);
-		}
+		item = GetItem(*it);
 		if (!item)
 			return false;
 		// And select it
scriptplugin.diff (5,645 bytes)   

Issue History

Date Modified Username Field Change
2025-01-31 16:05 tropion New Issue
2025-01-31 16:05 tropion File Added: scriptplugin.diff