Changeset View
Changeset View
Standalone View
Standalone View
shell/actiontooltipmodifier.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2017 Kevin Funk <kfunk@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | * | ||||
20 | */ | ||||
21 | | ||||
22 | #include "actiontooltipmodifier.h" | ||||
23 | | ||||
24 | #include <KActionCollection> | ||||
25 | #include <KLocalizedString> | ||||
26 | | ||||
27 | #include <QDebug> | ||||
28 | #include <QAction> | ||||
29 | | ||||
30 | namespace { | ||||
31 | const char ORIGINAL_TOOLTIP_PROPERTY[] = "__kdev_original_tooltip"; | ||||
32 | } | ||||
33 | | ||||
34 | class ActionToolTipModifierPrivate | ||||
35 | { | ||||
36 | public: | ||||
37 | ActionToolTipModifierPrivate(ActionToolTipModifier* qq) : q(qq) {} | ||||
38 | | ||||
39 | void updateActionTooltips(KActionCollection* actionCollection); | ||||
40 | void updateActionTooltip(QAction* action); | ||||
41 | | ||||
42 | ActionToolTipModifier* q; | ||||
43 | }; | ||||
44 | | ||||
45 | ActionToolTipModifier::ActionToolTipModifier(KActionCollection* actionCollection, QObject* parent) | ||||
46 | : QObject(parent) | ||||
47 | , d(new ActionToolTipModifierPrivate(this)) | ||||
48 | { | ||||
49 | connect(actionCollection, &KActionCollection::inserted, this, | ||||
50 | [this](QAction* action) { d->updateActionTooltip(action); }, | ||||
51 | Qt::QueuedConnection); | ||||
52 | | ||||
53 | d->updateActionTooltips(actionCollection); | ||||
54 | } | ||||
55 | | ||||
56 | ActionToolTipModifier::~ActionToolTipModifier() | ||||
57 | { | ||||
58 | } | ||||
59 | | ||||
60 | void ActionToolTipModifierPrivate::updateActionTooltips(KActionCollection* actionCollection) | ||||
61 | { | ||||
62 | foreach(QAction *action, actionCollection->actions()) { | ||||
63 | updateActionTooltip(action); | ||||
64 | } | ||||
65 | } | ||||
66 | | ||||
67 | void ActionToolTipModifierPrivate::updateActionTooltip(QAction* action) | ||||
68 | { | ||||
69 | QString tooltip = action->property(ORIGINAL_TOOLTIP_PROPERTY).toString(); | ||||
70 | if (tooltip.isEmpty()) { | ||||
71 | tooltip = action->toolTip(); | ||||
72 | action->setProperty(ORIGINAL_TOOLTIP_PROPERTY, tooltip); | ||||
73 | } | ||||
74 | | ||||
75 | auto shortcut = action->shortcut(); | ||||
76 | if (!shortcut.isEmpty()) { | ||||
77 | tooltip = i18nc("%1: original tooltip, %2 shortcut", "%1 <i>(%2)</i>", tooltip, shortcut.toString()); | ||||
78 | } | ||||
79 | action->setToolTip(tooltip); | ||||
80 | | ||||
81 | // track changes of QAction::shortcut property | ||||
82 | q->connect(action, &QAction::changed, q, [this, action]() { | ||||
83 | // note: if the action is ever removed from the action collection, this will still update the action tool tip | ||||
84 | // not worth handling IMO: likely never a problem though. we don't remove actions from collections usually | ||||
85 | updateActionTooltip(action); | ||||
86 | }); | ||||
87 | } |