LeechCraft  0.6.70-18450-gabe19ee3b0
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>
12 #include <QWidget>
14 #include <util/xpc/util.h>
15 #include <util/sll/prelude.h>
16 #include <util/sll/visitor.h>
21 
22 namespace LC::Util
23 {
24  ShortcutManager::ShortcutManager (const ICoreProxy_ptr& proxy, QObject *parent)
25  : QObject { parent }
26  , CoreProxy_ { proxy }
27  , ContextObj_ { parent }
28  {
29  }
30 
31  void ShortcutManager::RegisterAction (const QByteArray& id, QAction *act)
32  {
33  Actions_ [id] << act;
34  connect (act,
35  &QObject::destroyed,
36  this,
37  [this, act]
38  {
39  for (auto& list : Actions_)
40  list.removeAll (act);
41  });
42 
43  if (HasActionInfo (id))
44  {
45  const auto& info = ActionInfo_ [id];
46  if (act->text ().isEmpty ())
47  act->setText (info.Text_);
48  if (act->icon ().isNull ())
49  Util::Visit (info.Icon_,
50  [] (Util::Void) {},
51  [this, act] (const QByteArray& name)
52  {
53  act->setIcon (CoreProxy_->GetIconThemeManager ()->GetIcon (name));
54  act->setProperty ("ActionIcon", name);
55  },
56  [act] (const QIcon& icon) { act->setIcon (icon); });
57  }
58  else
59  {
60  const auto& icon = act->icon ().isNull () ?
61  CoreProxy_->GetIconThemeManager ()->GetIcon (act->property ("ActionIcon").toString ()) :
62  act->icon ();
63  auto shortcuts = act->shortcuts ();
65  {
66  act->text (),
67  shortcuts.value (0),
68  icon,
69  shortcuts.size () > 1 ? shortcuts.mid (1) : QList<QKeySequence> {},
70  });
71  }
72 
73  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
74  SetShortcut (id,
75  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
76  }
77 
78  void ShortcutManager::RegisterActions (const std::initializer_list<IDPair_t>& pairs)
79  {
80  for (const auto& [id, act] : pairs)
81  RegisterAction (id, act);
82  }
83 
84  void ShortcutManager::RegisterShortcut (const QByteArray& id, const ActionInfo& info, QShortcut *shortcut)
85  {
86  Shortcuts_ [id] << shortcut;
87  connect (shortcut,
88  &QObject::destroyed,
89  this,
90  [this, shortcut]
91  {
92  for (auto& list : Shortcuts_)
93  list.removeAll (shortcut);
94 
95  qDeleteAll (Shortcut2Subs_.take (shortcut));
96  });
97 
98  RegisterActionInfo (id, info);
99 
100  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
101  SetShortcut (id,
102  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
103  }
104 
105  void ShortcutManager::RegisterActionInfo (const QByteArray& id, const ActionInfo& info)
106  {
107  if (!HasActionInfo (id))
108  ActionInfo_ [id] = info;
109  }
110 
111  void ShortcutManager::RegisterGlobalShortcut (const QByteArray& id,
112  QObject *target, const QByteArray& method, const ActionInfo& info)
113  {
115  using namespace EF::GlobalAction;
116  e.Additional_ [Receiver] = QVariant::fromValue (target);
117  e.Additional_ [ActionID] = id;
118  e.Additional_ [Method] = method;
119  e.Additional_ [Shortcut] = QVariant::fromValue (info.Seq_);
121  [] (const QKeySequence& seq) { return QVariant::fromValue (seq); });
122  Globals_ [id] = e;
123 
124  ActionInfo_ [id] = info;
125  }
126 
128  {
129  for (const auto& entity : std::as_const (Globals_))
130  CoreProxy_->GetEntityManager ()->HandleEntity (entity);
131  }
132 
133  void ShortcutManager::SetShortcut (const QByteArray& id, const QKeySequences_t& seqs)
134  {
135  for (auto act : std::as_const (Actions_ [id]))
136  act->setShortcuts (seqs);
137 
138  for (auto sc : std::as_const (Shortcuts_ [id]))
139  {
140  sc->setKey (seqs.value (0));
141  qDeleteAll (Shortcut2Subs_.take (sc));
142 
143  const int seqsSize = seqs.size ();
144  for (int i = 1; i < seqsSize; ++i)
145  {
146  auto subsc = new QShortcut { sc->parent () };
147  subsc->setContext (sc->context ());
148  subsc->setKey (seqs.value (i));
149  connect (subsc,
150  &QShortcut::activated,
151  sc,
152  &QShortcut::activated);
153  Shortcut2Subs_ [sc] << subsc;
154  }
155  }
156 
157  if (Globals_.contains (id))
158  {
159  auto& e = Globals_ [id];
160  e.Additional_ [QStringLiteral ("Shortcut")] = QVariant::fromValue (seqs.value (0));
161  e.Additional_ [QStringLiteral ("AltShortcuts")] = Util::Map (seqs.mid (1),
162  [] (const QKeySequence& seq) { return QVariant::fromValue (seq); });
163  CoreProxy_->GetEntityManager ()->HandleEntity (e);
164  }
165  }
166 
168  {
169  return ActionInfo_;
170  }
171 
172  ShortcutManager& ShortcutManager::operator<< (const QPair<QByteArray, QAction*>& pair)
173  {
174  RegisterAction (pair.first, pair.second);
175  return *this;
176  }
177 
178  bool ShortcutManager::HasActionInfo (const QByteArray& id) const
179  {
180  return ActionInfo_.contains (id) &&
181  !ActionInfo_ [id].Text_.isEmpty ();
182  }
183 }
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:180
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:177
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:165
Q_DECL_IMPORT const QString Shortcut
Aids in providing configurable shortcuts.
A message used for inter-plugin communication.
Definition: structures.h:96
QKeySequence Seq_
The primary key sequence for this action.
Definition: anutil.h:15
A proper void type, akin to unit (or ()) type in functional languages.
Definition: void.h:20