Commit c30ca18b authored by Halla Rempt's avatar Halla Rempt

Fix activating tools with the tool action

(cherry picked from commit 7f4f7504a446af75e072ea283b149b9dcd693e27)
parent febda59a
......@@ -10,6 +10,7 @@
#include <kactioncollection.h>
#include <kis_action_registry.h>
#include <KoToolManager.h>
#include <QKeySequence>
#include <QAction>
......@@ -49,7 +50,11 @@ QList<QAction *> KoToolFactoryBase::createActions(KActionCollection *actionColle
KisActionRegistry *actionRegistry = KisActionRegistry::instance();
QList<QAction*> 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<QAction *> KoToolFactoryBase::createActionsImpl()
return QList<QAction *>();
}
void KoToolFactoryBase::activateTool()
{
KoToolManager::instance()->switchToolRequested(sender()->objectName());
}
......@@ -12,6 +12,7 @@
#include <QString>
#include <QList>
#include <QObject>
class KoCanvasBase;
class KoToolBase;
......@@ -42,8 +43,11 @@ class QAction;
</pre>
*/
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<QAction *> createActionsImpl();
private Q_SLOTS:
void activateTool();
private:
class Private;
......
......@@ -91,21 +91,28 @@ public:
QMap<QKeySequence, QStringList> 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)
......
......@@ -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<ShortcutToolAction*>(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);
}
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment