LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
shortcutmanager.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "shortcutmanager.h"
10 #include <QAction>
11 #include <QShortcut>
13 #include <util/xpc/util.h>
14 #include <util/sll/prelude.h>
19 
20 namespace LC::Util
21 {
22  ShortcutManager::ShortcutManager (const ICoreProxy_ptr& proxy, QObject *parent)
23  : QObject { parent }
24  , CoreProxy_ { proxy }
25  , ContextObj_ { parent }
26  {
27  }
28 
29  void ShortcutManager::RegisterAction (const QByteArray& id, QAction *act)
30  {
31  Actions_ [id] << act;
32  connect (act,
33  &QObject::destroyed,
34  this,
35  [this, act]
36  {
37  for (auto& list : Actions_)
38  list.removeAll (act);
39  });
40 
41  if (HasActionInfo (id))
42  {
43  const auto& info = ActionInfo_ [id];
44  if (act->text ().isEmpty ())
45  act->setText (info.Text_);
46  if (act->icon ().isNull ())
47  Util::Visit (info.Icon_,
48  [] (Util::Void) {},
49  [this, act] (const QByteArray& name)
50  {
51  act->setIcon (CoreProxy_->GetIconThemeManager ()->GetIcon (name));
52  act->setProperty ("ActionIcon", name);
53  },
54  [act] (const QIcon& icon) { act->setIcon (icon); });
55  }
56  else
57  {
58  const auto& icon = act->icon ().isNull () ?
59  CoreProxy_->GetIconThemeManager ()->GetIcon (act->property ("ActionIcon").toString ()) :
60  act->icon ();
61  auto shortcuts = act->shortcuts ();
63  {
64  act->text (),
65  shortcuts.value (0),
66  icon,
67  shortcuts.size () > 1 ? shortcuts.mid (1) : QList<QKeySequence> {},
68  });
69  }
70 
71  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
72  SetShortcut (id,
73  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
74  }
75 
76  void ShortcutManager::RegisterActions (const std::initializer_list<IDPair_t>& pairs)
77  {
78  for (const auto& [id, act] : pairs)
79  RegisterAction (id, act);
80  }
81 
82  void ShortcutManager::RegisterShortcut (const QByteArray& id, const ActionInfo& info, QShortcut *shortcut)
83  {
84  Shortcuts_ [id] << shortcut;
85  connect (shortcut,
86  &QObject::destroyed,
87  this,
88  [this, shortcut]
89  {
90  for (auto& list : Shortcuts_)
91  list.removeAll (shortcut);
92 
93  qDeleteAll (Shortcut2Subs_.take (shortcut));
94  });
95 
96  RegisterActionInfo (id, info);
97 
98  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
99  SetShortcut (id,
100  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
101  }
102 
103  void ShortcutManager::RegisterActionInfo (const QByteArray& id, const ActionInfo& info)
104  {
105  if (!HasActionInfo (id))
106  ActionInfo_ [id] = info;
107  }
108 
109  void ShortcutManager::RegisterGlobalShortcut (const QByteArray& id,
110  QObject *target, const QByteArray& method, const ActionInfo& info)
111  {
113  using namespace EF::GlobalAction;
114  e.Additional_ [Receiver] = QVariant::fromValue (target);
115  e.Additional_ [ActionID] = id;
116  e.Additional_ [Method] = method;
117  e.Additional_ [Shortcut] = QVariant::fromValue (info.Seq_);
118  e.Additional_ [AltShortcuts] = Util::Map (info.AdditionalSeqs_, &QVariant::fromValue<QKeySequence>);
119  Globals_ [id] = e;
120 
121  ActionInfo_ [id] = info;
122  }
123 
125  {
126  for (const auto& entity : qAsConst (Globals_))
127  CoreProxy_->GetEntityManager ()->HandleEntity (entity);
128  }
129 
130  void ShortcutManager::SetShortcut (const QByteArray& id, const QKeySequences_t& seqs)
131  {
132  for (auto act : qAsConst (Actions_ [id]))
133  act->setShortcuts (seqs);
134 
135  for (auto sc : qAsConst (Shortcuts_ [id]))
136  {
137  sc->setKey (seqs.value (0));
138  qDeleteAll (Shortcut2Subs_.take (sc));
139 
140  const int seqsSize = seqs.size ();
141  for (int i = 1; i < seqsSize; ++i)
142  {
143  auto subsc = new QShortcut { sc->parentWidget () };
144  subsc->setContext (sc->context ());
145  subsc->setKey (seqs.value (i));
146  connect (subsc,
147  &QShortcut::activated,
148  sc,
149  &QShortcut::activated);
150  Shortcut2Subs_ [sc] << subsc;
151  }
152  }
153 
154  if (Globals_.contains (id))
155  {
156  auto& e = Globals_ [id];
157  e.Additional_ [QStringLiteral ("Shortcut")] = QVariant::fromValue (seqs.value (0));
158  e.Additional_ [QStringLiteral ("AltShortcuts")] = Util::Map (seqs.mid (1),
159  &QVariant::fromValue<QKeySequence>);
160  CoreProxy_->GetEntityManager ()->HandleEntity (e);
161  }
162  }
163 
165  {
166  return ActionInfo_;
167  }
168 
169  ShortcutManager& ShortcutManager::operator<< (const QPair<QByteArray, QAction*>& pair)
170  {
171  RegisterAction (pair.first, pair.second);
172  return *this;
173  }
174 
175  bool ShortcutManager::HasActionInfo (const QByteArray& id) const
176  {
177  return ActionInfo_.contains (id) &&
178  !ActionInfo_ [id].Text_.isEmpty ();
179  }
180 }
ShortcutManager(const ICoreProxy_ptr &proxy, QObject *parent)
Creates the shortcut manager.
void RegisterGlobalShortcut(const QByteArray &id, QObject *target, const QByteArray &method, const ActionInfo &info)
Registers the given global shortcut with the given id.
void SetShortcut(const QByteArray &id, const QKeySequences_t &sequences)
Sets the key sequence for the given action.
void RegisterActionInfo(const QByteArray &id, const ActionInfo &info)
Registers the given action info with the given id.
Describes an action exposed in shortcut manager.
void RegisterActions(const std::initializer_list< IDPair_t > &actions)
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:215
auto Map(Container &&c, F &&f) noexcept(noexcept(std::is_nothrow_invocable_v< F, decltype(*c.begin())>))
Definition: prelude.h:104
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:82
QKeySequences_t AdditionalSeqs_
The additional key sequences for this action.
Q_DECL_IMPORT const QString Receiver
QMap< QByteArray, ActionInfo > GetActionInfo() const
Returns the map with information about actions.
Q_DECL_IMPORT const QString ActionID
void RegisterShortcut(const QByteArray &id, const ActionInfo &info, QShortcut *shortcut)
Registers the given QShortcut with the given id.
void AnnounceGlobalShorcuts()
Announces the global shortcuts.
void RegisterAction(const QByteArray &id, QAction *action)
Registers the given QAction by the given id.
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:181
Q_DECL_IMPORT const QString Method
Q_DECL_IMPORT const QString GlobalActionRegister
Registration of a global system-wide action.
Q_DECL_IMPORT const QString AltShortcuts
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:164
Q_DECL_IMPORT const QString Shortcut
Aids in providing configurable shortcuts.
A message used for inter-plugin communication.
Definition: structures.h:95
QKeySequence Seq_
The primary key sequence for this action.
Definition: anutil.h:17
A proper void type, akin to unit (or ()) type in functional languages.
Definition: void.h:20