View Issue Details
ID | Project | Category | View Status | Date Submitted | Last Update |
---|---|---|---|---|---|
0017399 | Scribus | General | public | 2025-01-31 16:05 | 2025-02-24 21:28 |
Reporter | tropion | Assigned To | |||
Priority | normal | Severity | minor | Reproducibility | always |
Status | new | Resolution | open | ||
Summary | 0017399: Objects in groups are not accessible via the scriptplugin python API | ||||
Description | I suggest the attached patch | ||||
Steps To Reproduce | Create 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. | ||||
Tags | No tags attached. | ||||
Patch | |||||
|
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 |
|
can you explain what exactly your script does? if i understand it correctly, it's patching all the function that have an own implementation of getItem (and getItem itself) to also look inside of groups. if it's the case, that's fine for me and i would be ready to review your patch. i already have one remark: shouldn't also groups in groups be explored? this can be done by creating a stack with the items to be looped and adding the children to it, when a group is found. |
|
You do understand this correctly, it changes getItem and all variations of it to also look inside of groups. Whenever a group is encountered getAllChildren() is called. That should explore groups in groups. |
|
/tmp/scriptplugin.diff:61: trailing whitespace. PyObject* l = PyList_New (0); /tmp/scriptplugin.diff:80: trailing whitespace. if (it->OwnPage == pageNr) /tmp/scriptplugin.diff:104: trailing whitespace. // We do not call getPageItemByName() here, warning: 3 lines add whitespace errors. |
|
I've made a review on Gitlab: https://gitlab.com/scribus/scribus/-/merge_requests/48 The patch seems Ok to me, just a few minor or aesthetic things that can be fixed. |
Date Modified | Username | Field | Change |
---|---|---|---|
2025-01-31 16:05 | tropion | New Issue | |
2025-01-31 16:05 | tropion | File Added: scriptplugin.diff | |
2025-02-22 10:39 | ale | Project | Infrastructure => Scribus |
2025-02-22 11:19 | ale | Note Added: 0052099 | |
2025-02-24 09:35 | tropion | Note Added: 0052110 | |
2025-02-24 19:38 | ale | Note Added: 0052112 | |
2025-02-24 21:28 | ale | Note Added: 0052114 |