Commit abb8f88e authored by Michael Abrahams's avatar Michael Abrahams
Browse files

Update tooltips to include keyboard shortcut.

Summary:
Tooltips will automatically change with changes to shorcuts.

Ref T199
BUG: 348626

Reviewers: dkazakov, rempt

Maniphest Tasks: T199

Differential Revision: https://phabricator.kde.org/D245
parent 93b9fdd3
......@@ -788,19 +788,24 @@ void KoToolManager::registerTools(KActionCollection *ac, KoCanvasController *con
return;
}
// Actions available during the use of individual tools
CanvasData *cd = d->canvasses.value(controller).first();
foreach(KoToolBase *tool, cd->allTools) {
QHash<QString, KAction*> actions = tool->actions();
QHash<QString, KAction*>::const_iterator it(actions.constBegin());
for (; it != actions.constEnd(); ++it) {
if (!ac->action(it.key()))
ac->addAction(it.key(), it.value());
QHash<QString, KAction*>::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()));
}
}
......
......@@ -44,7 +44,7 @@ class QCursor;
/// 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()
......
......@@ -25,24 +25,33 @@
#include <KoToolFactoryBase.h>
#include <QToolButton>
#include <kicon.h>
#include <klocale.h>
#include <QtGlobal> // 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 @@ QString ToolHelper::toolTip() const
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 @@ int ToolHelper::priority() const
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),
......
......@@ -127,19 +127,26 @@ public:
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
......
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