From c30ca18bcf977fe748321c4fa62dd7758e61755d Mon Sep 17 00:00:00 2001 From: Halla Rempt Date: Wed, 13 Jan 2021 15:59:35 +0100 Subject: [PATCH] Fix activating tools with the tool action (cherry picked from commit 7f4f7504a446af75e072ea283b149b9dcd693e27) --- libs/flake/KoToolFactoryBase.cpp | 12 ++++++- libs/flake/KoToolFactoryBase.h | 10 ++++-- libs/flake/KoToolManager.cpp | 35 ++++++++++++-------- libs/flake/KoToolManager_p.cpp | 56 -------------------------------- libs/flake/KoToolManager_p.h | 22 ------------- 5 files changed, 40 insertions(+), 95 deletions(-) diff --git a/libs/flake/KoToolFactoryBase.cpp b/libs/flake/KoToolFactoryBase.cpp index 0362a5495b..ddbc2bdbbf 100644 --- a/libs/flake/KoToolFactoryBase.cpp +++ b/libs/flake/KoToolFactoryBase.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -49,7 +50,11 @@ QList KoToolFactoryBase::createActions(KActionCollection *actionColle KisActionRegistry *actionRegistry = KisActionRegistry::instance(); QList actions = createActionsImpl(); - actions << actionRegistry->makeQAction(id()); + QAction *action = actionRegistry->makeQAction(id()); + actionCollection->addAction(id(), action); + connect(action, SIGNAL(triggered()), SLOT(activateTool())); + //qDebug() << action << action->shortcut(); + Q_FOREACH(QAction *action, actions) { if (action->objectName().isEmpty()) { @@ -200,3 +205,8 @@ QList KoToolFactoryBase::createActionsImpl() return QList(); } +void KoToolFactoryBase::activateTool() +{ + KoToolManager::instance()->switchToolRequested(sender()->objectName()); +} + diff --git a/libs/flake/KoToolFactoryBase.h b/libs/flake/KoToolFactoryBase.h index 77992b149a..c9525c0f34 100644 --- a/libs/flake/KoToolFactoryBase.h +++ b/libs/flake/KoToolFactoryBase.h @@ -12,6 +12,7 @@ #include #include +#include class KoCanvasBase; class KoToolBase; @@ -42,8 +43,11 @@ class QAction; */ -class KRITAFLAKE_EXPORT KoToolFactoryBase +class KRITAFLAKE_EXPORT KoToolFactoryBase : public QObject { + + Q_OBJECT + public: /** * Create the new factory @@ -191,7 +195,6 @@ public: protected: - /** * Set the default shortcut for activation of this tool. */ @@ -241,6 +244,9 @@ protected: */ virtual QList createActionsImpl(); +private Q_SLOTS: + + void activateTool(); private: class Private; diff --git a/libs/flake/KoToolManager.cpp b/libs/flake/KoToolManager.cpp index 1b78336c44..54855d48ae 100644 --- a/libs/flake/KoToolManager.cpp +++ b/libs/flake/KoToolManager.cpp @@ -91,21 +91,28 @@ public: QMap shortcutMap; - //qDebug() << "................... activating tool" << activeToolId; +// qDebug() << "................... activating tool" << activeToolId; Q_FOREACH(QAction *action, windowActionCollection->actions()) { - //qDebug() << "Action" << action->objectName() << "shortcuts" << action->shortcuts(); if (action->property("tool_action").isValid()) { QStringList tools = action->property("tool_action").toStringList(); - //qDebug() << "\tassociated with" << tools; - if (tools.contains(activeToolId)) { - //qDebug() << "\t\tenabling"; + + if (KoToolRegistry::instance()->keys().contains(action->objectName())) { + //qDebug() << "This action needs to be enabled!"; action->setEnabled(true); toolActions << action->objectName(); } else { - action->setDisabled(true); + if (tools.contains(activeToolId) || action->property("always_enabled").toBool()) { + //qDebug() << "\t\tenabling"; + action->setEnabled(true); + toolActions << action->objectName(); + } + else { + //qDebug() << "\t\tDISabling"; + action->setDisabled(true); + } } } else { @@ -236,14 +243,14 @@ void KoToolManager::registerToolActions(KActionCollection *ac, KoCanvasControlle return; } - // Actions used to switch tools via shortcuts - Q_FOREACH (ToolHelper * th, d->tools) { - if (ac->action(th->id())) { - continue; - } - ShortcutToolAction* action = th->createShortcutToolAction(ac); - ac->addCategorizedAction(th->id(), action, "tool-shortcuts"); - } +// // Actions used to switch tools via shortcuts +// Q_FOREACH (ToolHelper * th, d->tools) { +// if (ac->action(th->id())) { +// continue; +// } +// //ShortcutToolAction* action = th->createShortcutToolAction(ac); +// ac->addCategorizedAction(th->id(), action, "tool-shortcuts"); +// } } void KoToolManager::addController(KoCanvasController *controller) diff --git a/libs/flake/KoToolManager_p.cpp b/libs/flake/KoToolManager_p.cpp index 396eaf1820..e5aeb2daec 100644 --- a/libs/flake/KoToolManager_p.cpp +++ b/libs/flake/KoToolManager_p.cpp @@ -30,12 +30,6 @@ ToolHelper::ToolHelper(KoToolFactoryBase *tool) m_hasCustomShortcut(false), m_toolAction(0) { - // TODO: how to get an existing custom shortcut in the beginning here? - // Once the first ShortcutToolAction is added to the actionCollection, - // it will get any custom shortcut set by the actionCollection and - // by that trigger shortcutToolActionUpdated(). - // But until then shortcut() will report a wrong shortcut and e.g. show - // that in the tooltips of the KoToolBox. } KoToolAction *ToolHelper::toolAction() @@ -84,25 +78,6 @@ void ToolHelper::activate() emit toolActivated(this); } -void ToolHelper::shortcutToolActionUpdated() -{ - ShortcutToolAction *action = static_cast(sender()); - // check if shortcut changed - const QKeySequence actionShortcut = action->shortcut(); - const QKeySequence currentShortcut = shortcut(); - if (actionShortcut != currentShortcut) { - m_hasCustomShortcut = true; - m_customShortcut = actionShortcut; - if (m_toolAction) { - emit m_toolAction->changed(); - } - // no need to forward the new shortcut to the other ShortcutToolAction objects, - // they are synchronized behind the scenes - // Thus they will also trigger this method, but due to them having - // the same shortcut not result in any further action. - } -} - KoToolBase *ToolHelper::createTool(KoCanvasBase *canvas) const { KoToolBase *tool = m_toolFactory->createTool(canvas); @@ -112,17 +87,6 @@ KoToolBase *ToolHelper::createTool(KoCanvasBase *canvas) const return tool; } -ShortcutToolAction* ToolHelper::createShortcutToolAction(QObject *parent) -{ - ShortcutToolAction* action = new ShortcutToolAction(id(), text(), parent); - - KisActionRegistry::instance()->propertizeAction(id(), action); - - connect(action, SIGNAL(changed()), SLOT(shortcutToolActionUpdated())); - - return action; -} - QString ToolHelper::section() const { return m_toolFactory->section(); @@ -229,23 +193,3 @@ void Connector::selectionChanged() { emit selectionChanged(m_shapeManager->selection()->selectedShapes()); } - -// ************ ShortcutToolAction ********** -ShortcutToolAction::ShortcutToolAction(const QString &id, const QString &name, QObject *parent) - : QAction(name, parent) - , m_toolID(id) -{ - connect(this, SIGNAL(triggered(bool)), this, SLOT(actionTriggered())); -} - -ShortcutToolAction::~ShortcutToolAction() -{ -} - -void ShortcutToolAction::actionTriggered() -{ - // todo: why not ToolHelper::activate(); and thus a slightly different behaviour? - // Answering the todo item: switchToolRequested - KoToolManager::instance()->switchToolRequested(m_toolID); -} - diff --git a/libs/flake/KoToolManager_p.h b/libs/flake/KoToolManager_p.h index a387a062d4..0f873aa0de 100644 --- a/libs/flake/KoToolManager_p.h +++ b/libs/flake/KoToolManager_p.h @@ -88,8 +88,6 @@ public: bool layerExplicitlyDisabled; }; -class ShortcutToolAction; - /// \internal class ToolHelper : public QObject { @@ -114,7 +112,6 @@ public: /// wrapper around KoToolFactoryBase::priority(); int priority() const; KoToolBase *createTool(KoCanvasBase *canvas) const; - ShortcutToolAction *createShortcutToolAction(QObject *parent); /// unique id, >= 0 int uniqueId() const { return m_uniqueId; @@ -129,9 +126,6 @@ Q_SIGNALS: /// Emitted when the tool should be activated, e.g. by pressing the tool's assigned button in the toolbox void toolActivated(ToolHelper *tool); -private Q_SLOTS: - void shortcutToolActionUpdated(); - private: KoToolFactoryBase * const m_toolFactory; const int m_uniqueId; @@ -158,20 +152,4 @@ private: KoShapeManager *m_shapeManager; }; -/// \internal -/// Helper class to provide a action for tool shortcuts -class ShortcutToolAction : public QAction -{ - Q_OBJECT -public: - ShortcutToolAction(const QString &id, const QString &name, QObject *parent); - ~ShortcutToolAction() override; - -private Q_SLOTS: - void actionTriggered(); - -private: - QString m_toolID; -}; - #endif -- GitLab