diff --git a/libs/flake/KoToolManager.h b/libs/flake/KoToolManager.h --- a/libs/flake/KoToolManager.h +++ b/libs/flake/KoToolManager.h @@ -44,7 +44,7 @@ /// Struct for the createToolList return type. struct KoToolButton { - QToolButton *button;///< a newly created button. + QToolButton *button; ///< a newly created button. QString section; ///< The section the button wants to be in. int priority; ///< Lower number (higher priority) means coming first in the section. int buttonGroupId; ///< An unique ID for this button as passed by changedTool() diff --git a/libs/flake/KoToolManager.cpp b/libs/flake/KoToolManager.cpp --- a/libs/flake/KoToolManager.cpp +++ b/libs/flake/KoToolManager.cpp @@ -788,19 +788,24 @@ return; } + // Actions available during the use of individual tools CanvasData *cd = d->canvasses.value(controller).first(); foreach(KoToolBase *tool, cd->allTools) { QHash actions = tool->actions(); - QHash::const_iterator it(actions.constBegin()); - for (; it != actions.constEnd(); ++it) { - if (!ac->action(it.key())) - ac->addAction(it.key(), it.value()); + QHash::const_iterator action(actions.constBegin()); + for (; action != actions.constEnd(); ++action) { + if (!ac->action(action.key())) + ac->addAction(action.key(), action.value()); } } + + // Actions used to switch tools; connect slot to keep button tooltips updated foreach(ToolHelper * th, d->tools) { ToolAction* action = new ToolAction(this, th->id(), th->toolTip(), ac); action->setShortcut(th->shortcut()); ac->addAction(th->id(), action); + th->setAction(action); + connect(action, SIGNAL(changed()), th, SLOT(actionUpdated())); } } diff --git a/libs/flake/KoToolManager_p.h b/libs/flake/KoToolManager_p.h --- a/libs/flake/KoToolManager_p.h +++ b/libs/flake/KoToolManager_p.h @@ -127,19 +127,26 @@ int uniqueId() const { return m_uniqueId; } - /// wrapper around KoToolFactoryBase::shortcut() + /// KAction->shortcut() if it exists, otherwise KoToolFactoryBase::shortcut() KShortcut shortcut() const; + /// Writes a tooltip for a button, appending the keyboard shortcut if we have one + QString buttonToolTip() const; + /// Associate an action with this tool + void setAction(KAction *a); Q_SIGNALS: - /// emitted when one of the generated buttons was pressed. + /// Emitted when the generated toolbox button is pressed. void toolActivated(ToolHelper *tool); private Q_SLOTS: void buttonPressed(); + void actionUpdated(); private: KoToolFactoryBase *m_toolFactory; int m_uniqueId; + QToolButton *button; + KAction *action; }; /// \internal diff --git a/libs/flake/KoToolManager_p.cpp b/libs/flake/KoToolManager_p.cpp --- a/libs/flake/KoToolManager_p.cpp +++ b/libs/flake/KoToolManager_p.cpp @@ -25,24 +25,33 @@ #include #include #include +#include #include // for qrand() -// ************ ToolHelper ********** +/* ************ ToolHelper ********** + * This class wrangles the tool factory, toolbox button and switch-tool action + * for a single tool. It assumes the will continue to live once it is created. + * (Hiding the toolbox is OK.) + */ + ToolHelper::ToolHelper(KoToolFactoryBase *tool) + : m_toolFactory(tool), + m_uniqueId((int)qrand()), + button(0), + action(0) { - m_toolFactory = tool; - m_uniqueId = (int) qrand(); } QToolButton* ToolHelper::createButton() { - QToolButton *but = new QToolButton(); - but->setObjectName(m_toolFactory->id()); - but->setIcon(KIcon(m_toolFactory->iconName())); - but->setToolTip(m_toolFactory->toolTip()); - connect(but, SIGNAL(clicked()), this, SLOT(buttonPressed())); - return but; + button = new QToolButton(); + button->setObjectName(m_toolFactory->id()); + button->setIcon(KIcon(m_toolFactory->iconName())); + button->setToolTip(buttonToolTip()); + + connect(button, SIGNAL(clicked()), this, SLOT(buttonPressed())); + return button; } void ToolHelper::buttonPressed() @@ -65,6 +74,20 @@ return m_toolFactory->toolTip(); } +QString ToolHelper::buttonToolTip() const +{ + return shortcut().isEmpty() ? + i18nc("@info:tooltip", "%1", toolTip()) : + i18nc("@info:tooltip %2 is shortcut", "%1 (%2)", toolTip(), + shortcut().toString()); +} + +void ToolHelper::actionUpdated() +{ + if (button) + button->setToolTip(buttonToolTip()); +} + KoToolBase *ToolHelper::createTool(KoCanvasBase *canvas) const { KoToolBase *tool = m_toolFactory->createTool(canvas); @@ -86,9 +109,18 @@ KShortcut ToolHelper::shortcut() const { + if (action) { + return action->shortcut(); + } + return m_toolFactory->shortcut(); } +void ToolHelper::setAction(KAction *a) +{ + action = a; +} + // ************ Connector ********** Connector::Connector(KoShapeManager *parent) : QObject(parent),