BibleTime
btactioncollection.h
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #pragma once
14 
15 #include <QObject>
16 
17 #include <QAction>
18 #include <QKeySequence>
19 #include <QMap>
20 #include <QString>
21 #include "../../util/btassert.h"
22 
23 
24 class BtActionCollection: public QObject {
25 
26  Q_OBJECT
27 
28 public: // methods:
29 
30  BtActionCollection(QObject * const parent = nullptr)
31  : QObject{parent}
32  {}
33 
34  QMap<QString, QAction *> const & actions() const noexcept
35  { return m_actions; }
36 
37  void addAction(QString const & name, QAction * const action);
38 
39  void removeAction(QString const & name);
40 
41  QAction & action(QString const & name) const;
42 
43  template <typename T>
44  T & actionAs(QString const & name) const {
45  QAction & a = action(name);
46  BT_ASSERT(dynamic_cast<T *>(&a));
47  return static_cast<T &>(a);
48  }
49 
50  /*!
51  * \brief Read shortcuts from config.
52  *
53  * Read the shortcuts for the given group
54  * from the configuration and add them to
55  * this action collection.
56  *
57  * \param[in] group Shortcut group to read actions from.
58  */
59  void readShortcuts(QString const & group);
60 
61  /*!
62  * \brief Write shortcuts to config.
63  *
64  * Write the shortcuts of this action collection
65  * to the given group in the configuration.
66  *
67  * \param[in] group Shortcut group to write actions to.
68  */
69  void writeShortcuts(QString const & group) const;
70 
71  static QKeySequence getDefaultShortcut(QAction & action);
72 
73 private: // fields:
74 
75  QMap<QString, QAction *> m_actions;
76 
77 };
#define BT_ASSERT(...)
Definition: btassert.h:17
BtActionCollection(QObject *const parent=nullptr)
void readShortcuts(QString const &group)
Read shortcuts from config.
void writeShortcuts(QString const &group) const
Write shortcuts to config.
QMap< QString, QAction * > const & actions() const noexcept
QMap< QString, QAction * > m_actions
void addAction(QString const &name, QAction *const action)
void removeAction(QString const &name)
T & actionAs(QString const &name) const
QAction & action(QString const &name) const
static QKeySequence getDefaultShortcut(QAction &action)
#define T(f)