BibleTime
btactioncollection.cpp
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-2025 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#include "btactioncollection.h"
14
15#include <QHash>
16#include "../../backend/config/btconfig.h"
17#include "../../util/btassert.h"
18
19namespace {
20
21char const defaultShortcutPropertyName[] = "BtDefaultShortcut";
22
23} // anonymous namespace
24
25QAction & BtActionCollection::action(QString const & name) const {
26 auto const it = m_actions.find(name);
27 BT_ASSERT(it != m_actions.end());
28 return **it;
29}
30
31void BtActionCollection::addAction(QString const & name,
32 QAction * const action)
33{
35 BT_ASSERT(m_actions.find(name) == m_actions.end());
36 auto const it = m_actions.insert(name, action);
37 try {
38 action->setProperty(defaultShortcutPropertyName, action->shortcut());
39 } catch (...) {
40 m_actions.erase(it);
41 throw;
42 }
43}
44
46{ return action.property(defaultShortcutPropertyName).value<QKeySequence>(); }
47
48void BtActionCollection::readShortcuts(QString const & group) {
49 BtConfig::ShortcutsMap shortcuts = btConfig().getShortcuts(group);
50 for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it)
51 if (auto const actionIt = m_actions.find(it.key());
52 actionIt != m_actions.end())
53 (*actionIt)->setShortcuts(it.value());
54}
55
56void BtActionCollection::writeShortcuts(QString const & group) const {
57 BtConfig::ShortcutsMap shortcuts;
58 for (auto it = m_actions.begin(); it != m_actions.end(); ++it)
59 shortcuts.insert(it.key(), it.value()->shortcuts());
60 btConfig().setShortcuts(group, shortcuts);
61}
#define BT_ASSERT(...)
Definition btassert.h:17
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition btconfig.h:305
void readShortcuts(QString const &group)
Read shortcuts from config.
void writeShortcuts(QString const &group) const
Write shortcuts to config.
QMap< QString, QAction * > m_actions
void addAction(QString const &name, QAction *const action)
QAction & action(QString const &name) const
static QKeySequence getDefaultShortcut(QAction &action)
void setShortcuts(QString const &shortcutGroup, ShortcutsMap const &shortcuts)
Sets the shortcuts for the given group.
Definition btconfig.cpp:258
ShortcutsMap getShortcuts(QString const &shortcutGroup)
Gets the shortcuts for the given group.
Definition btconfig.cpp:233