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-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 #include "btactioncollection.h"
14 
15 #include <QHash>
16 #include "../../backend/config/btconfig.h"
17 #include "../../util/btassert.h"
18 
19 namespace {
20 
21 char const defaultShortcutPropertyName[] = "BtDefaultShortcut";
22 
23 } // anonymous namespace
24 
25 QAction & 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 
31 void 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 
45 void BtActionCollection::removeAction(QString const & name) {
46  #ifndef NDEBUG
47  int const r =
48  #endif
49  m_actions.remove(name);
50  BT_ASSERT(r > 0);
51 }
52 
54 { return action.property(defaultShortcutPropertyName).value<QKeySequence>(); }
55 
56 void BtActionCollection::readShortcuts(QString const & group) {
57  BtConfig::ShortcutsMap shortcuts = btConfig().getShortcuts(group);
58  for (auto it = shortcuts.begin(); it != shortcuts.end(); ++it)
59  if (auto const actionIt = m_actions.find(it.key());
60  actionIt != m_actions.end())
61  (*actionIt)->setShortcuts(it.value());
62 }
63 
64 void BtActionCollection::writeShortcuts(QString const & group) const {
65  BtConfig::ShortcutsMap shortcuts;
66  for (auto it = m_actions.begin(); it != m_actions.end(); ++it)
67  shortcuts.insert(it.key(), it.value()->shortcuts());
68  btConfig().setShortcuts(group, shortcuts);
69 }
#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)
void removeAction(QString const &name)
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:267
ShortcutsMap getShortcuts(QString const &shortcutGroup)
Gets the shortcuts for the given group.
Definition: btconfig.cpp:233
QStringList r(content.left(bodyIndex))