r24177 by jghali - #16308: Readability and feature fixes for getAllObjects()

scribus-commit scribus-commit at lists.scribus.net
Wed Nov 4 18:41:26 UTC 2020


Author: jghali
Date: Wed Nov  4 18:41:26 2020
New Revision: 24177

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=24177
Log:
#16308: Readability and feature fixes for getAllObjects()

Modified:
    trunk/Scribus/scribus/plugins/scriptplugin/cmdgetprop.cpp
    trunk/Scribus/scribus/plugins/scriptplugin_py2x/cmdgetprop.cpp

Modified: trunk/Scribus/scribus/plugins/scriptplugin/cmdgetprop.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=24177&path=/trunk/Scribus/scribus/plugins/scriptplugin/cmdgetprop.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin/cmdgetprop.cpp	(original)
+++ trunk/Scribus/scribus/plugins/scriptplugin/cmdgetprop.cpp	Wed Nov  4 18:41:26 2020
@@ -4,6 +4,9 @@
 a copyright and/or license notice that predates the release of Scribus 1.3.2
 for which a new license (GPL+exception) is in place.
 */
+
+#include <algorithm>
+
 #include "cmdgetprop.h"
 #include "cmdutil.h"
 #include "scribuscore.h"
@@ -310,9 +313,9 @@
 
 PyObject *scribus_getallobjects(PyObject* /* self */, PyObject* args, PyObject *keywds)
 {
-	int type = -1;
+	int itemType = -1;
+	int layerId = -1;
 	uint counter = 0;
-	uint counter2 = 0;
 
 	if (!checkHaveDocument())
 		return nullptr;
@@ -322,7 +325,7 @@
 	char *kwlist[] = { const_cast<char*>("type"), const_cast<char*>("page"), const_cast<char*>("layer"), nullptr};
 	char* szLayerName = const_cast<char*>("");
 
-	if (!PyArg_ParseTupleAndKeywords(args, keywds, "|iies", kwlist, &type, &pageNr, "utf-8", &szLayerName))
+	if (!PyArg_ParseTupleAndKeywords(args, keywds, "|iies", kwlist, &itemType, &pageNr, "utf-8", &szLayerName))
 		return nullptr;
 
 	int numPages = currentDoc->Pages->count();
@@ -336,41 +339,38 @@
 	QString layerName = QString::fromUtf8(szLayerName);
 	if (!layerName.isEmpty())
 	{
-		layer = currentDoc->Layers.layerByName(layerName);
+		const ScLayer *layer = currentDoc->Layers.layerByName(layerName);
 		if (!layer)
 		{
 			PyErr_SetString(PyExc_ValueError, QObject::tr("layer name is invalid.", "python error").toLocal8Bit().constData());
 			return nullptr;
 		}
-	}
-
-	// have doc already
+		layerId = layer->ID;
+	}
+
+	auto isReturnedItem = [pageNr, layerId, itemType](PageItem* item)
+	{
+		if  (pageNr != item->OwnPage)
+			return false;
+		if ((itemType != -1) && (item->itemType() != itemType))
+			return false;
+		if ((layerId != -1) && (item->m_layerID) != layerId)
+			return false;
+		return true;
+	};
+
+	int returnedItemCount = std::count_if(currentDoc->DocItems.begin(), currentDoc->DocItems.end(), isReturnedItem);
+
+	PyObject* pyItemList = PyList_New(returnedItemCount);
 	for (int i = 0; i < currentDoc->Items->count(); ++i)
 	{
 		PageItem* item = currentDoc->Items->at(i);
-		if  (pageNr != item->OwnPage)
+		if (!isReturnedItem(item))
 			continue;
-		if ((type != -1) && (item->itemType() != type))
-			continue;
-		if (layer && (layer->ID != item->m_layerID))
-			continue;
+		PyList_SetItem(pyItemList, counter, PyUnicode_FromString(item->itemName().toUtf8()));
 		counter++;
 	}
-
-	PyObject* pyList = PyList_New(counter);
-	for (int i = 0; i < currentDoc->Items->count(); ++i)
-	{
-		PageItem* item = currentDoc->Items->at(i);
-		if  (pageNr != item->OwnPage)
-			continue;
-		if ((type != -1) && (item->itemType() != type))
-			continue;
-		if (layer && (layer->ID != item->m_layerID))
-			continue;
-		PyList_SetItem(pyList, counter2, PyUnicode_FromString(item->itemName().toUtf8()));
-		counter2++;
-	}
-	return pyList;
+	return pyItemList;
 }
 
 PyObject *scribus_getobjectattributes(PyObject* /* self */, PyObject* args)

Modified: trunk/Scribus/scribus/plugins/scriptplugin_py2x/cmdgetprop.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=24177&path=/trunk/Scribus/scribus/plugins/scriptplugin_py2x/cmdgetprop.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/scriptplugin_py2x/cmdgetprop.cpp	(original)
+++ trunk/Scribus/scribus/plugins/scriptplugin_py2x/cmdgetprop.cpp	Wed Nov  4 18:41:26 2020
@@ -4,6 +4,9 @@
 a copyright and/or license notice that predates the release of Scribus 1.3.2
 for which a new license (GPL+exception) is in place.
 */
+
+#include <algorithm>
+
 #include "cmdgetprop.h"
 #include "cmdutil.h"
 #include "scribuscore.h"
@@ -310,9 +313,9 @@
 
 PyObject *scribus_getallobjects(PyObject* /* self */, PyObject* args, PyObject *keywds)
 {
-	int type = -1;
+	int itemType = -1;
+	int layerId = -1;
 	uint counter = 0;
-	uint counter2 = 0;
 
 	if (!checkHaveDocument())
 		return nullptr;
@@ -322,13 +325,13 @@
 	char *kwlist[] = { const_cast<char*>("type"), const_cast<char*>("page"), const_cast<char*>("layer"), nullptr};
 	char* szLayerName = const_cast<char*>("");
 
-	if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ii", kwlist, &type, &pageNr))
+	if (!PyArg_ParseTupleAndKeywords(args, keywds, "|iies", kwlist, &itemType, &pageNr, "utf-8", &szLayerName))
 		return nullptr;
 
 	int numPages = currentDoc->Pages->count();
 	if (pageNr < 0 || pageNr >= numPages)
 	{
-		PyErr_SetString(PyExc_ValueError, QObject::tr("page number is invalid.","python error").toLocal8Bit().constData());
+		PyErr_SetString(PyExc_ValueError, QObject::tr("page number is invalid.", "python error").toLocal8Bit().constData());
 		return nullptr;
 	}
 
@@ -336,41 +339,38 @@
 	QString layerName = QString::fromUtf8(szLayerName);
 	if (!layerName.isEmpty())
 	{
-		layer = currentDoc->Layers.layerByName(layerName);
+		const ScLayer *layer = currentDoc->Layers.layerByName(layerName);
 		if (!layer)
 		{
 			PyErr_SetString(PyExc_ValueError, QObject::tr("layer name is invalid.", "python error").toLocal8Bit().constData());
 			return nullptr;
 		}
-	}
-
-	// have doc already
+		layerId = layer->ID;
+	}
+
+	auto isReturnedItem = [pageNr, layerId, itemType](PageItem* item)
+	{
+		if  (pageNr != item->OwnPage)
+			return false;
+		if ((itemType != -1) && (item->itemType() != itemType))
+			return false;
+		if ((layerId != -1) && (item->m_layerID) != layerId)
+			return false;
+		return true;
+	};
+
+	int returnedItemCount = std::count_if(currentDoc->DocItems.begin(), currentDoc->DocItems.end(), isReturnedItem);
+
+	PyObject* pyItemList = PyList_New(returnedItemCount);
 	for (int i = 0; i < currentDoc->Items->count(); ++i)
 	{
 		PageItem* item = currentDoc->Items->at(i);
-		if  (pageNr != item->OwnPage)
+		if (!isReturnedItem(item))
 			continue;
-		if ((type != -1) && (item->itemType() != type))
-			continue;
-		if (layer && (layer->ID != item->m_layerID))
-			continue;
+		PyList_SetItem(pyItemList, counter, PyString_FromString(item->itemName().toUtf8()));
 		counter++;
 	}
-
-	PyObject* pyList = PyList_New(counter);
-	for (int i = 0; i < currentDoc->Items->count(); ++i)
-	{
-		PageItem* item = currentDoc->Items->at(i);
-		if  (pageNr != item->OwnPage)
-			continue;
-		if ((type != -1) && (item->itemType() != type))
-			continue;
-		if (layer && (layer->ID != item->m_layerID))
-			continue;
-		PyList_SetItem(pyList, counter2, PyString_FromString(item->itemName().toUtf8()));
-		counter2++;
-	}
-	return pyList;
+	return pyItemList;
 }
 
 PyObject *scribus_getobjectattributes(PyObject* /* self */, PyObject* args)




More information about the scribus-commit mailing list