swift
actionbind.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "core/actionbind.h"
5 
6 namespace swift::core
7 {
8  CActionBind::CActionBind(const QString &action, swift::misc::CIcons::IconIndex icon, QObject *parent)
9  : QObject(parent)
10  {
11  CActionBind::registerAction(action, icon);
12  }
13 
14  QString CActionBind::registerAction(const QString &action, swift::misc::CIcons::IconIndex icon)
15  {
16  if (!sApp || sApp->isShuttingDown()) { return {}; }
17  if (sApp->getApplicationInfo().isUnitTest()) { return {}; }
18 
19  const QString a = CActionBind::normalizeAction(action);
20  Q_ASSERT_X(sApp && sApp->getInputManager(), Q_FUNC_INFO, "Missing input manager");
21  sApp->getInputManager()->registerAction(a, icon);
22  return a;
23  }
24 
26  {
27  // void
28  }
29 
31  {
32  if (m_index < 0) { return; }
33  if (sApp->getApplicationInfo().isUnitTest()) { return; }
34 
35  Q_ASSERT_X(sApp && sApp->getInputManager(), Q_FUNC_INFO, "Missing input manager");
36  sApp->getInputManager()->unbind(m_index);
37  m_index = -1;
38  }
39 
40  void CActionBind::shutdown()
41  {
42  unbind();
43  if (m_deleteCallback) { m_deleteCallback(); }
44  }
45 
46  QString CActionBind::normalizeAction(const QString &action)
47  {
48  QString n = action.trimmed();
49  if (!n.startsWith('/')) { n.insert(0, QChar('/')); }
50  if (n.endsWith('/')) { n.remove(n.length() - 1, 1); }
51  return n;
52  }
53 } // namespace swift::core
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
Definition: application.cpp:71
~CActionBind()
Destructor.
Definition: actionbind.cpp:25
void unbind()
Unbind from swift::core::CInputManager.
Definition: actionbind.cpp:30
CActionBind(const QString &action, swift::misc::CIcons::IconIndex icon, Receiver *receiver, MembFunc< Receiver > slot=nullptr, const std::function< void()> &deleteCallback={}, QObject *parent=nullptr)
Constructor.
Definition: actionbind.h:28
CInputManager * getInputManager() const
The input manager, if available.
Definition: application.h:302
bool isShuttingDown() const
Is application shutting down?
const swift::misc::CApplicationInfo & getApplicationInfo() const
swift application running
Definition: application.h:138
void registerAction(const QString &action, swift::misc::CIcons::IconIndex icon=swift::misc::CIcons::StandardIconEmpty16)
Register new action.
void unbind(int index)
Unbind a slot.
bool isUnitTest() const
Unit test.
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition: icons.h:32
Backend services of the swift project, like dealing with the network or the simulators.
Definition: actionbind.cpp:7