Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Fix crashes when a python plugin requests an invalid action path
Dmitry Kazakovc91dee3fee48
diff --git a/libs/libkis/Window.cpp b/libs/libkis/Window.cppindex a4c40f4174..d716a45dab 100644--- a/libs/libkis/Window.cpp+++ b/libs/libkis/Window.cpp@@ -133,6 +133,7 @@ QAction *Window::createAction(const QString &id, const QString &text, const QStr}action->setObjectName(id);action->setProperty("menulocation", menuLocation);+ action->setProperty("defaultmenulocation", "tools/scripts");return action;}diff --git a/libs/ui/KisMainWindow.cpp b/libs/ui/KisMainWindow.cppindex bab8eb42c8..e30590d9d8 100644--- a/libs/ui/KisMainWindow.cpp+++ b/libs/ui/KisMainWindow.cpp@@ -3385,49 +3385,7 @@ void KisMainWindow::applyActionIconOverridesFromLocalXML()void KisMainWindow::synchronizeDynamicActions(){// Add all actions with a menu property to the main window- Q_FOREACH(QAction *action, this->actionCollection()->actions()) {- QString menuLocation = action->property("menulocation").toString();- if (!menuLocation.isEmpty()) {- QAction *found = 0;- QList<QAction *> candidates = this->menuBar()->actions();- Q_FOREACH(const QString &name, menuLocation.split("/")) {- Q_FOREACH(QAction *candidate, candidates) {- if (candidate->objectName().toLower() == name.toLower()) {- found = candidate;- candidates = candidate->menu()->actions();- break;- }- }- if (candidates.isEmpty()) {- break;- }- }-- if (found && found->menu()) {- QList<QAction *> existingActions = found->menu()->actions();-- if (std::find_if(existingActions.begin(),- existingActions.end(),- kismpl::mem_equal_to(&QAction::objectName, action->objectName()))- == existingActions.end()) {-- if (std::is_sorted(existingActions.begin(),- existingActions.end(),- kismpl::mem_less(&QAction::objectName))) {-- auto it = std::upper_bound(existingActions.begin(),- existingActions.end(),- action->objectName(),- kismpl::mem_less(&QAction::objectName));- found->menu()->insertAction(it != existingActions.end() ? *it : nullptr, action);-- } else {- found->menu()->addAction(action);- }- }- }- }- }+ KisActionManager::synchronizeDynamicActions(this->actionCollection()->actions(), this->menuBar()->actions());}#include <moc_KisMainWindow.cpp>diff --git a/libs/ui/kis_action_manager.cpp b/libs/ui/kis_action_manager.cppindex 462f1caff2..df03f8973b 100644--- a/libs/ui/kis_action_manager.cpp+++ b/libs/ui/kis_action_manager.cpp@@ -496,3 +496,91 @@ void KisActionManager::dumpActionFlags()}}}++void KisActionManager::synchronizeOneDynamicAction(QAction* action, const QList<QAction*> &menuBarActions, const QString &menuLocation, const QString &fallbackMenuLocation)+{+ if (menuLocation.isEmpty()) return;++ QAction *found = 0;+ QList<QAction *> nextCandidates = menuBarActions;++ const QStringList menuLocationComponents = menuLocation.split('/', Qt::SkipEmptyParts);++ for (auto it = menuLocationComponents.begin(); it != menuLocationComponents.end(); ++it) {+ const QString &name = *it;++ QList<QAction *> candidates;+ std::swap(nextCandidates, candidates);++ bool foundComponent = false;++ Q_FOREACH (QAction *candidate, candidates) {+ if (candidate->objectName().compare(name, Qt::CaseInsensitive) == 0) {+ if (candidate->menu()) {+ found = candidate;+ nextCandidates = candidate->menu()->actions();+ foundComponent = true;+ } else {+ warnUI << "Failed to add action" << action->objectName() << "to another action"+ << candidate->objectName()+ << "; This action does not have a menu! Requested menu location:" << menuLocation;+ }+ break;+ }+ }++ if (!foundComponent) {+ warnUI << "Couldn't find the menu location for action" << action->objectName()+ << "; requested menu location:" << menuLocation;+ if (!fallbackMenuLocation.isEmpty()) {+ warnUI << "Using fallback menu location" << fallbackMenuLocation << "for action" << action->objectName();+ synchronizeOneDynamicAction(action, menuBarActions, fallbackMenuLocation, "");+ }+ return;+ }+ }++ if (found && found->menu()) {+ QList<QAction *> existingActions = found->menu()->actions();++ auto existingActionIt = std::find_if(existingActions.begin(),+ existingActions.end(),+ kismpl::mem_equal_to(&QAction::objectName, action->objectName()));++ if (existingActionIt == existingActions.end()) {+ if (std::is_sorted(existingActions.begin(),+ existingActions.end(),+ kismpl::mem_less(&QAction::objectName))) {+ auto it = std::upper_bound(existingActions.begin(),+ existingActions.end(),+ action->objectName(),+ kismpl::mem_less(&QAction::objectName));+ found->menu()->insertAction(it != existingActions.end() ? *it : nullptr, action);++ } else {+ found->menu()->addAction(action);+ }+ } else {+ // replacing the existing action with the newly provided one+ if (*existingActionIt != action) {+ auto nextIt = std::next(existingActionIt);+ QAction *insertBefore = nextIt != existingActions.end() ? *nextIt : nullptr;+ found->menu()->removeAction(*existingActionIt);+ found->menu()->insertAction(insertBefore, action);+ }+ }+ }+}++void KisActionManager::synchronizeDynamicActions(QList<QAction*> newActions, const QList<QAction*> &menuBarActions)+{+ Q_FOREACH (QAction *action, newActions) {+ QString menuLocation = action->property("menulocation").toString();+ QString defaultMenuLocation = action->property("defaultmenulocation").toString();++ // actions with empty menu location are considered as not for the menus+ if (!menuLocation.isEmpty()) {+ synchronizeOneDynamicAction(action, menuBarActions, menuLocation, defaultMenuLocation);+ }+ }+}diff --git a/libs/ui/kis_action_manager.h b/libs/ui/kis_action_manager.hindex b3db0330d0..0fb184388b 100644--- a/libs/ui/kis_action_manager.h+++ b/libs/ui/kis_action_manager.h@@ -85,6 +85,34 @@ public:static void safePopulateMenu(QMenu *menu, const QString &actionId, KisActionManager *actionManager);+ /**+ * Adds one action \p action into the menu hierarchy \p menuBarActions+ *+ * The function first tries to add the action into location \p menuLocation.+ * If the location is invalid, then it tried to add into+ * location \p fallbackMenuLocation.+ */+ static void synchronizeOneDynamicAction(QAction* action, const QList<QAction*> &menuBarActions, const QString &menuLocation, const QString &fallbackMenuLocation);++ /**+ * Synchronize actions from the list \p newActions with the actions in \p menuBarActions+ *+ * For every action in \p newActions the function will inspect+ * property "menulocation" and will try to add this action into+ * the proper position in menus represente.+ *+ * If "menulocation" property is missing or empty, the the action+ * is not added to the menu hierarchy at all.+ *+ * If the path mentioned in "menulocation" is not found, then+ * the function also checks property "defaultmenulocation" and+ * tries to add the action there.+ *+ * WARNING: due to current design limitation, it is impossible+ * to add an action to the toplevel menu!+ */+ static void synchronizeDynamicActions(QList<QAction*> newActions, const QList<QAction*> &menuBarActions);+private Q_SLOTS:void slotActionAddedToCollection(QAction *action);diff --git a/libs/ui/tests/kis_action_manager_test.cpp b/libs/ui/tests/kis_action_manager_test.cppindex c2b1eb6386..975f09c350 100644--- a/libs/ui/tests/kis_action_manager_test.cpp+++ b/libs/ui/tests/kis_action_manager_test.cpp@@ -9,6 +9,8 @@#include "kis_action_manager_test.h"#include <kis_debug.h>+#include <QMenu>+#include "KisPortingUtils.h"#include <KisPart.h>#include <KisMainWindow.h>#include <KisDocument.h>@@ -119,5 +121,139 @@ void KisActionManagerTest::testTakeAction()QVERIFY(view->viewManager()->actionManager()->actionByName("dummy") == 0);}+namespace+{++void printActionHierarchyImpl(const QList<QAction *> actions, int indentation)+{+ Q_FOREACH (QAction *action, actions) {+ qDebug().nospace().noquote() << QString(" ").repeated(indentation) << action->objectName();+ if (action->menu()) {+ printActionHierarchyImpl(action->menu()->actions(), indentation + 2);+ }+ }+}++void printActionHierarchy(const QList<QAction *> actions)+{+ printActionHierarchyImpl(actions, 0);+};++std::pair<QString, int> findActionLocationImpl(QAction *action, const QList<QAction*> &menuBarActions, const QString &prefix)+{+ auto calcPrefix = [&] (const QAction *action) {+ return !prefix.isEmpty() ? prefix + '/' + action->objectName() : action->objectName();+ };++ Q_FOREACH(QAction *node, menuBarActions) {+ if (node == action) {+ return {calcPrefix(node), menuBarActions.indexOf(node)};+ } else if (node->menu()) {+ auto result = findActionLocationImpl(action, node->menu()->actions(), calcPrefix(node));+ if (result.second >= 0) return result;+ }+ }++ return {{}, -1};+}++// returns a pair of "full path" and an "index in the submenu it belongs to"+std::pair<QString, int> findActionLocation(QAction *action, const QList<QAction*> &menuBarActions)+{+ return findActionLocationImpl(action, menuBarActions, "");+}++} // namespace++void KisActionManagerTest::testDynamicActionUpdate_data()+{+ QTest::addColumn<QString>("newActionPath");+ QTest::addColumn<QString>("newActionName");+ QTest::addColumn<QString>("expectedNewPath");+ QTest::addColumn<int>("expectedFinalIndex");++ QTest::addRow("addScriptBefore") << "tools/scripts" << "scriptA" << "tools/scripts/scriptA" << 0;+ QTest::addRow("addScriptAfter") << "tools/scripts" << "scriptD" << "tools/scripts/scriptD" << 2;+ QTest::addRow("addScriptAfterWeirdCase") << "ToOlS/sCrIpTs" << "scriptD" << "tools/scripts/scriptD" << 2;++ QTest::addRow("addScriptAfterTrailingSlash") << "tools/scripts/" << "scriptD" << "tools/scripts/scriptD" << 2;+ QTest::addRow("addScriptAfterDoubleSlash") << "tools//scripts" << "scriptD" << "tools/scripts/scriptD" << 2;++ QTest::addRow("addScriptOverwriteFirst") << "tools/scripts" << "scriptB" << "tools/scripts/scriptB" << 0;+ QTest::addRow("addScriptOverwriteLast") << "tools/scripts" << "scriptC" << "tools/scripts/scriptC" << 1;++ QTest::addRow("addNonScriptToolBefore") << "tools" << "a-tool" << "tools/a-tool" << 0;+ QTest::addRow("addNonScriptToolAfter") << "tools" << "z-tool" << "tools/z-tool" << 2;+ QTest::addRow("addToEmptyMenu") << "file" << "new" << "file/new" << 0;+ QTest::addRow("addToActionWithoutMenu") << "exit" << "something" << "tools/fallback/something" << 0;++ QTest::addRow("addToInexistentToplevelMenu") << "help" << "something" << "tools/fallback/something" << 0;+ QTest::addRow("addToInexistentSubmenu") << "tools/non-existent" << "something" << "tools/fallback/something" << 0;+}++void KisActionManagerTest::testDynamicActionUpdate()+{+ using namespace Qt::StringLiterals;++ QFETCH(QString, newActionPath);+ QFETCH(QString, newActionName);+ QFETCH(QString, expectedNewPath);+ QFETCH(int, expectedFinalIndex);++ auto createAction = [] (const QString &name) {+ QAction *action = new QAction(name);+ action->setObjectName(name);+ return action;+ };++ QList<QAction*> menuBarActions;++ QAction *fileAction = createAction("file"_L1);+ QAction *exitAction = createAction("exit"_L1);+ QAction *toolsAction = createAction("tools"_L1);++ menuBarActions.append(fileAction);+ menuBarActions.append(exitAction);+ menuBarActions.append(toolsAction);++ QAction *scriptsAction = createAction("scripts"_L1);+ QAction *existingScriptBAction = createAction("scriptB"_L1);+ QAction *existingScriptCAction = createAction("scriptC"_L1);++ QAction *fallbackAction = createAction("fallback"_L1);++ // just an empty "File" menu+ fileAction->setMenu(new QMenu());++ toolsAction->setMenu(new QMenu());+ toolsAction->menu()->addAction(fallbackAction);+ toolsAction->menu()->addAction(scriptsAction);++ scriptsAction->setMenu(new QMenu());+ scriptsAction->menu()->addAction(existingScriptBAction);+ scriptsAction->menu()->addAction(existingScriptCAction);++ // just an empty fallback menu+ fallbackAction->setMenu(new QMenu());++ QAction *newAction = createAction(newActionName);+ newAction->setProperty("menulocation", newActionPath);+ newAction->setProperty("defaultmenulocation", "tools/fallback");++ KisActionManager::synchronizeDynamicActions({newAction}, menuBarActions);++#if 0+ qDebug() << "Result menu bar actions:";+ printActionHierarchy(menuBarActions);+#else+ Q_UNUSED(printActionHierarchy)+#endif++ auto [path, finalIndex] = findActionLocation(newAction, menuBarActions);++ QCOMPARE(path, expectedNewPath);+ QCOMPARE(finalIndex, expectedFinalIndex);+}+KISTEST_MAIN(KisActionManagerTest)diff --git a/libs/ui/tests/kis_action_manager_test.h b/libs/ui/tests/kis_action_manager_test.hindex 5e5fe09e05..e0cc274490 100644--- a/libs/ui/tests/kis_action_manager_test.h+++ b/libs/ui/tests/kis_action_manager_test.h@@ -17,6 +17,8 @@ private Q_SLOTS:void testUpdateGUI();void testCondition();void testTakeAction();+ void testDynamicActionUpdate_data();+ void testDynamicActionUpdate();};#endif // KIS_ACTION_MANAGER_TEST_Hdiff --git a/plugins/python/mutator/mutator.py b/plugins/python/mutator/mutator.pyindex a30d92da04..79e9fa69ab 100644--- a/plugins/python/mutator/mutator.py+++ b/plugins/python/mutator/mutator.py@@ -79,10 +79,9 @@ class Mutator(Extension):'''Adds an "action" to the Krita menus, which connects to the mutate function.'''- action = window.createAction("mutate", i18n("Mutate"), "tools/scripting")+ action = window.createAction("mutate", i18n("Mutate"), "tools/scripts")action.triggered.connect(self.mutate)--+def mutate(self):'''Mutates current brush/color/etc. settings by some user-configurable amount.