Changeset View
Changeset View
Standalone View
Standalone View
ui/toggleactionmenu.h
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by David Hurka <david.hurka@mailbox.org> * | ||||
3 | * * | ||||
4 | * Inspired by and replacing toolaction.h by: * | ||||
5 | * Copyright (C) 2004-2006 by Albert Astals Cid <aacid@kde.org> * | ||||
6 | * * | ||||
7 | * This program is free software; you can redistribute it and/or modify * | ||||
8 | * it under the terms of the GNU General Public License as published by * | ||||
9 | * the Free Software Foundation; either version 2 of the License, or * | ||||
10 | * (at your option) any later version. * | ||||
11 | ***************************************************************************/ | ||||
12 | | ||||
13 | #ifndef TOGGLEACTIONMENU_H | ||||
14 | #define TOGGLEACTIONMENU_H | ||||
15 | | ||||
16 | #include <KActionMenu> | ||||
17 | #include <QToolButton> | ||||
18 | #include <QSet> | ||||
19 | | ||||
20 | /** | ||||
21 | * @brief A KActionMenu, with allows to set the default action of its toolbar buttons. | ||||
22 | * | ||||
23 | * Usually, a KActionMenu creates toolbar buttons which reflect its own action properties | ||||
24 | * (icon, text, tooltip, checked state,...), as it is a QAction itself. | ||||
25 | * ToggleActionMenu will use its own action properties only when plugged as submenu in another menu. | ||||
26 | * But the default action of the toolbar buttons can easily be changed with the slot setDefaultAction(). | ||||
27 | * | ||||
28 | * Naming: The user can *Toggle* the checked state of an *Action* by directly clicking the toolbar button, | ||||
29 | * but can also open a *Menu*. | ||||
30 | * | ||||
31 | * @par Intention | ||||
32 | * Setting the default action of the toolbar button can be useful for: | ||||
33 | * * Providing the most propably needed entry of a menu directly on the menu button. | ||||
34 | * * Showing the last used menu entry on the menu button, including its checked state. | ||||
35 | * The advantage is that the user often does not need to open the menu, | ||||
36 | * and that the toolbar button shows additional information | ||||
37 | * like checked state or the user's last selection. | ||||
38 | * | ||||
39 | * This shall replace the former ToolAction in Okular, | ||||
40 | * while beeing flexible enough for other (planned) action menus. | ||||
41 | */ | ||||
42 | class ToggleActionMenu : public KActionMenu | ||||
43 | { | ||||
44 | Q_OBJECT | ||||
45 | | ||||
46 | public: | ||||
47 | /** | ||||
48 | * Defines how the menu behaves. | ||||
49 | */ | ||||
50 | enum MenuLogic { | ||||
51 | DefaultLogic = 0x0, | ||||
52 | /** | ||||
53 | * Automatically makes the triggered action the default action, even if in a submenu. | ||||
54 | * When a toolbar button is constructed, | ||||
55 | * the default action is set to the default action set with setDefaultAction() before, | ||||
56 | * otherwise to the first checked action in the menu, | ||||
57 | * otherwise to the action suggested with suggestDefaultAction(). | ||||
58 | */ | ||||
59 | ImplicitDefaultAction = 0x1 | ||||
60 | }; | ||||
61 | | ||||
62 | explicit ToggleActionMenu( QObject *parent ); | ||||
63 | ToggleActionMenu( const QString &text, QObject * parent ); | ||||
64 | /** | ||||
65 | * These are the usual constructors for KActionMenu. | ||||
66 | * | ||||
67 | * @note | ||||
68 | * @p text and @p icon are used only if this menu is a submenu in another menu. | ||||
69 | * To set the appearance of the toolbar buttons, use setDefaultAction. | ||||
70 | */ | ||||
71 | ToggleActionMenu( const QIcon &icon, | ||||
72 | const QString &text, | ||||
73 | QObject *parent, | ||||
74 | QToolButton::ToolButtonPopupMode popupMode = QToolButton::MenuButtonPopup, | ||||
75 | MenuLogic logic = DefaultLogic | ||||
76 | ); | ||||
77 | | ||||
78 | QWidget *createWidget( QWidget *parent ) override; | ||||
79 | | ||||
80 | /** | ||||
81 | * Returns the current default action of the toolbar buttons. | ||||
82 | * | ||||
83 | * In ImplicitDefaultAction mode, | ||||
84 | * when the default action was not yet set with setDefaultAction(), | ||||
85 | * it will determine it from the first checked action in the menu, | ||||
86 | * otherwise from the action set with suggestDefaultAction(). | ||||
87 | */ | ||||
88 | QAction *defaultAction(); | ||||
89 | | ||||
90 | /** | ||||
91 | * Suggests a default action to be used as fallback. | ||||
92 | * | ||||
93 | * It will be used if the default action is not determined another way. | ||||
94 | */ | ||||
95 | void suggestDefaultAction( QAction * action ); | ||||
96 | | ||||
97 | public slots: | ||||
98 | /** | ||||
99 | * Sets the default action of the toolbar buttons. | ||||
100 | * | ||||
101 | * This action will be triggered by clicking directly on the toolbar buttons. | ||||
102 | * It will also set the text, icon, checked state, etc. of the toolbar buttons. | ||||
103 | * | ||||
104 | * @note | ||||
105 | * The default action will not set the enabled state or popup mode of the menu buttons. | ||||
106 | * These properties are still set by the corresponding properties of this ToggleActionMenu. | ||||
107 | * | ||||
108 | * @note | ||||
109 | * The action will not be added to the menu, | ||||
110 | * it usually makes sense to addAction() it before to setDefaultAction() it. | ||||
111 | */ | ||||
112 | void setDefaultAction( QAction *action ); | ||||
113 | | ||||
114 | private: | ||||
115 | QAction *m_defaultAction; | ||||
116 | QAction *m_suggestedDefaultAction; | ||||
117 | QList< QPointer< QToolButton > > m_buttons; | ||||
118 | MenuLogic m_menuLogic; | ||||
119 | | ||||
120 | /** | ||||
121 | * Returns the first checked action in menu(), | ||||
122 | * or nullptr if no action is checked. | ||||
123 | */ | ||||
124 | QAction *checkedAction() const; | ||||
125 | | ||||
126 | private slots: | ||||
127 | /** | ||||
128 | * Updates the toolbar buttons, using both the default action and properties of this menu itself. | ||||
129 | * | ||||
130 | * This ensures that the toolbar buttons reflect e. g. a disabled state of this menu. | ||||
131 | */ | ||||
132 | void updateButtons(); | ||||
133 | }; | ||||
134 | | ||||
135 | #endif // TOGGLEACTIONMENU_H |