swift
actionbind.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_CORE_ACTIONBIND_H
7 #define SWIFT_CORE_ACTIONBIND_H
8 
9 #include "core/application.h"
10 #include "core/swiftcoreexport.h"
11 
12 namespace swift::core
13 {
17  class SWIFT_CORE_EXPORT CActionBind : public QObject
18  {
19  Q_OBJECT
20 
21  public:
23  template <typename U>
24  using MembFunc = void (U::*)(bool);
25 
27  template <typename Receiver>
28  CActionBind(const QString &action, swift::misc::CIcons::IconIndex icon, Receiver *receiver,
29  MembFunc<Receiver> slot = nullptr, const std::function<void()> &deleteCallback = {},
30  QObject *parent = nullptr)
31  : QObject(parent), m_deleteCallback(deleteCallback)
32  {
33  // workaround if a binding is taking place in an empty context
34  if (!sApp || sApp->isShuttingDown()) { return; }
35  if (sApp->getApplicationInfo().isUnitTest()) { return; }
36 
37  const QString a = CActionBind::registerAction(action, icon);
38  Q_ASSERT_X(sApp && sApp->getInputManager(), Q_FUNC_INFO, "Missing input manager");
39  m_index = sApp->getInputManager()->bind(a, receiver, slot);
40  QObject::connect(sApp, &CApplication::aboutToShutdown, this, &CActionBind::shutdown);
41  }
42 
44  CActionBind(const QString &action, swift::misc::CIcons::IconIndex icon, QObject *parent = nullptr);
45 
48  CActionBind(const CActionBind &) = delete;
49  CActionBind &operator=(const CActionBind &) = delete;
51 
53  ~CActionBind() override;
54 
56  void unbind();
57 
59  bool isBound() const { return m_index >= 0; }
60 
62  int getIndex() const { return m_index; }
63 
64  private:
65  void shutdown();
66 
68  static QString normalizeAction(const QString &action);
69 
71  static QString registerAction(const QString &action, swift::misc::CIcons::IconIndex icon);
72 
73  int m_index = -1;
74  std::function<void()> m_deleteCallback;
75  };
76 
78  using CActionBinding = QSharedPointer<CActionBind>;
79 
81  using CActionBindings = QList<CActionBinding>;
82 } // namespace swift::core
83 
84 #endif // SWIFT_CORE_ACTIONBIND_H
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
Definition: application.cpp:71
CActionBind binds a member function to an action.
Definition: actionbind.h:18
CActionBind & operator=(const CActionBind &)=delete
Not copyable.
bool isBound() const
Bound with swift::core::CInputManager.
Definition: actionbind.h:59
void(U::*)(bool) MembFunc
Signature of receiving member function.
Definition: actionbind.h:24
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
int getIndex() const
Index.
Definition: actionbind.h:62
CActionBind(const CActionBind &)=delete
Not copyable.
void aboutToShutdown()
About to shutdown.
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
int bind(const QString &action, RecvObj *receiver, void(RecvObj::*slotPointer)(bool))
Register a new hotkey function.
Definition: inputmanager.h:49
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
QSharedPointer< CActionBind > CActionBinding
Single binding.
Definition: actionbind.h:78
QList< CActionBinding > CActionBindings
List of bindings.
Definition: actionbind.h:81
#define SWIFT_CORE_EXPORT
Export a class or function from the library.