Logo    
KDWinUtils
Helper library for MFC to Qt migration
Loading...
Searching...
No Matches
kcommandmanager.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** This file is part of KDWinutils, KDAB's MFC to Qt migration tool.
4**
5** Copyright (C) 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6** All rights reserved.
7**
8** This file is intended solely for use by the migration tools and services
9** provided by Klarälvdalens Datakonsult AB.
10**
11** Any other use or distribution of this software that is not otherwise agreed
12** upon in writing and signed by an authorized representative of Klarälvdalens
13** Datakonsult AB, KDAB (USA) LLC, KDAB (Deutschland) GmbH & Co. K.G.,
14** KDAB (France) SAS, KDAB (UK), Ltd., or any future subsidiary of Klarälvdalens
15** Datakonsult AB is hereby prohibited.
16**
17** Contact info@kdab.com if any conditions stated above are unclear to you.
18**
19****************************************************************************/
20
21#pragma once
22
24
25#include <QHash>
26#include <QMenu>
27#include <QObject>
28#include <QVector>
29
30#include <functional>
31
32class QAction;
33class QWidget;
34class QSignalMapper;
35class QToolBar;
36class QWidgetAction;
37class QTimer;
38
40
62class KDWINUTILS_EXPORT KCommandManager : public QObject
63{
64 Q_OBJECT
65
66public:
67 explicit KCommandManager(QObject *parent = nullptr);
69
70 // Command handling
79 QAction *createCommand(int id, const QString &title = QString(), QWidget *context = nullptr);
88 QAction *findOrCreateCommand(int id, const QString &title = QString(), QWidget *context = nullptr);
96 QWidgetAction *createWidgetCommand(int id, QWidget *defaultWidget, QWidget *context = nullptr);
98 QAction *command(int id);
100 void removeCommand(int id);
104 bool hasCommand(int id) const;
105
107 void setCommandEnabled(int id, bool enabled);
109 void setCommandChecked(int id, bool checked);
110
112 void triggerCommandAsync(int id) const;
114 void triggerCommand(int id) const;
115
117 template <typename T>
118 void connectCommand(int id, T method);
120 template <typename T>
121 void connectCommand(int id, T *object, void (T::*method)());
123 template <typename T>
124 void connectCommand(int id, T *object, void (T::*method)(unsigned int));
126 template <typename T>
127 void connectCommandRange(int first, int last, T *object, void (T::*method)(unsigned int));
128
130 template <typename T>
131 void connectCommandUpdater(int id, T method);
133 template <typename T>
134 void connectCommandUpdater(int id, T *object, void (T::*method)(QAction *));
136 template <typename T>
137 void connectCommandRangeUpdater(int first, int last, T *object, void (T::*method)(QAction *));
138
140 template <typename Func>
141 void visitCommands(Func fn);
142
143 // Menu handling
150 QMenu *createMenu(const QString &title, QWidget *parent = nullptr);
156 QMenu *createMenu(QWidget *parent = nullptr);
158 void addMenu(QMenu *menu);
159
160 // Toolbar handling
162 QToolBar *createToolBar(int id, const QString &title, QWidget *context = nullptr);
164 void addToolBar(int id, QToolBar *toolBar, QWidget *context = nullptr);
166 QToolBar *toolBar(int id) const;
172
173 // Timer handling
178
179signals:
182
183private:
184 void updateAllMenus();
185 void triggerUpdate(QAction *action) const;
186
187 QTimer *m_updateTimer;
188 QHash<int, QAction *> m_actions;
189 QHash<int, QToolBar *> m_toolbars;
190 QSignalMapper *m_actionMapper;
191 QHash<int, std::function<void()>> m_actionSlots;
192 QHash<int, QVector<std::function<void()>>> m_actionUpdates;
193};
194
195template <typename T>
197{
198 m_actionSlots[id] = method;
199}
200template <typename T>
201void KCommandManager::connectCommand(int id, T *object, void (T::*method)())
202{
203 connectCommand(id, [object, method]() {
204 (object->*method)();
205 });
206}
207template <typename T>
208void KCommandManager::connectCommand(int id, T *object, void (T::*method)(unsigned int))
209{
210 connectCommand(id, [id, object, method]() {
211 (object->*method)(id);
212 });
213}
214template <typename T>
215void KCommandManager::connectCommandRange(int first, int last, T *object, void (T::*method)(unsigned int))
216{
217 for (int id = first; id <= last; ++id)
218 connectCommand(id, object, method);
219}
220
221template <typename T>
223{
224 m_actionUpdates[id].append([this, id, method] {
225 method(m_actions.value(id));
226 });
227}
228
229template <typename T>
230void KCommandManager::connectCommandUpdater(int id, T *object, void (T::*method)(QAction *))
231{
232 connectCommandUpdater(id, [this, id, object, method](QAction *action) {
233 (object->*method)(action);
234 });
235}
236
237template <typename T>
238void KCommandManager::connectCommandRangeUpdater(int first, int last, T *object, void (T::*method)(QAction *))
239{
240 for (int id = first; id <= last; ++id)
241 connectCommandUpdater(id, object, method);
242}
243
244template <typename Func>
246{
247 for (const auto action : m_actions)
248 fn(action);
249}
250
The KCommandManager class emulates the way MFC command handling.
Definition kcommandmanager.h:63
void visitCommands(Func fn)
Visits each command in the command manager and applies the given function.
Definition kcommandmanager.h:245
void connectCommandUpdater(int id, T method)
Connects an update handler to the command with the given id.
Definition kcommandmanager.h:222
void addToolBar(int id, QToolBar *toolBar, QWidget *context=nullptr)
Adds an existing toolbar, to manage the update of the toolbar items.
void addMenu(QMenu *menu)
Adds an existing menu, to manage the update of the menu items.
QAction * findOrCreateCommand(int id, const QString &title=QString(), QWidget *context=nullptr)
Find or create a command object.
void triggerCommandAsync(int id) const
Triggers the command with the given id asynchronously.
void commandUpdated()
Emitted when commands have been updated.
KCommandManager(QObject *parent=nullptr)
QAction * command(int id)
Returns the command with the given id, or nullptr if it doesn't exist.
void updateAllToolBars()
Updates the state of the commands on all toolbars.
void connectCommand(int id, T method)
Connects a command handler to the command with the given id.
Definition kcommandmanager.h:196
void connectCommandRangeUpdater(int first, int last, T *object, void(T::*method)(QAction *))
Connects an update handler to a range of commands.
Definition kcommandmanager.h:238
QToolBar * createToolBar(int id, const QString &title, QWidget *context=nullptr)
Creates a toolbar with the given title.
void triggerCommand(int id) const
Triggers the command with the given id.
bool hasCommand(int id) const
Returns true if a command with the given id exists.
QMenu * createMenu(QWidget *parent=nullptr)
Creates a menu with the given title.
void stopUpdateTimer()
Stops the update timer.
void connectCommandRange(int first, int last, T *object, void(T::*method)(unsigned int))
Connects a command handler to a range of commands.
Definition kcommandmanager.h:215
QMenu * createMenu(const QString &title, QWidget *parent=nullptr)
Creates a menu with the given title.
QWidgetAction * createWidgetCommand(int id, QWidget *defaultWidget, QWidget *context=nullptr)
Create a Widget command object.
void setCommandChecked(int id, bool checked)
Checks or unchecks the command with the given id.
void clearAllCommands()
Removes all commands.
QAction * createCommand(int id, const QString &title=QString(), QWidget *context=nullptr)
Create a command object.
void startUpdateTimer()
Starts the update timer.
void setCommandEnabled(int id, bool enabled)
Enables or disables the command with the given id.
QToolBar * toolBar(int id) const
Returns the toolbar with the given id.
void removeCommand(int id)
Returns the command with the given id.
#define KDWINUTILS_EXPORT
Definition kdwinutils_export.h:31
#define KDWINUTILS_BEGIN_NAMESPACE
Definition kdwinutils_global.h:27
#define KDWINUTILS_END_NAMESPACE
Definition kdwinutils_global.h:28