LeechCraft  0.6.70-13729-g7046a9d2a7
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "shortcutmanager.h"
31 #include <QAction>
32 #include <QShortcut>
33 #include <util/xpc/util.h>
34 #include <util/sll/prelude.h>
39 
40 namespace LC
41 {
42 namespace Util
43 {
45  : QObject (parent)
46  , CoreProxy_ (proxy)
47  {
48  }
49 
50  void ShortcutManager::SetObject (QObject *obj)
51  {
52  ContextObj_ = obj;
53  }
54 
55  void ShortcutManager::RegisterAction (const QString& id, QAction *act)
56  {
57  Actions_ [id] << act;
58  connect (act,
59  SIGNAL (destroyed ()),
60  this,
61  SLOT (handleActionDestroyed ()));
62 
63  if (HasActionInfo (id))
64  {
65  const auto& info = ActionInfo_ [id];
66  if (act->text ().isEmpty ())
67  act->setText (info.UserVisibleText_);
68  if (act->icon ().isNull () &&
69  act->property ("ActionIcon").isNull ())
70  act->setIcon (info.Icon_);
71  }
72  else
73  {
74  const auto& icon = act->icon ().isNull () ?
75  CoreProxy_->GetIconThemeManager ()->GetIcon (act->property ("ActionIcon").toString ()) :
76  act->icon ();
78  {
79  act->text (),
80  act->shortcuts (),
81  icon
82  });
83  }
84 
85  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
86  SetShortcut (id,
87  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
88  }
89 
90  void ShortcutManager::RegisterActions (const std::initializer_list<IDPair_t>& pairs)
91  {
92  for (const auto& [id, act] : pairs)
93  RegisterAction (id, act);
94  }
95 
96  void ShortcutManager::RegisterShortcut (const QString& id, const ActionInfo& info, QShortcut *shortcut)
97  {
98  Shortcuts_ [id] << shortcut;
99  connect (shortcut,
100  SIGNAL (destroyed ()),
101  this,
102  SLOT (handleShortcutDestroyed ()));
103 
104  RegisterActionInfo (id, info);
105 
106  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
107  SetShortcut (id,
108  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
109  }
110 
111  void ShortcutManager::RegisterActionInfo (const QString& id, const ActionInfo& info)
112  {
113  if (!HasActionInfo (id))
114  ActionInfo_ [id] = info;
115  }
116 
118  QObject *target, const QByteArray& method, const ActionInfo& info)
119  {
120  Entity e = Util::MakeEntity ({}, {}, 0,
121  "x-leechcraft/global-action-register");
122  e.Additional_ ["Receiver"] = QVariant::fromValue (target);
123  e.Additional_ ["ActionID"] = id;
124  e.Additional_ ["Method"] = method;
125  e.Additional_ ["Shortcut"] = QVariant::fromValue (info.Seqs_.value (0));
126  e.Additional_ ["AltShortcuts"] = Util::Map (info.Seqs_.mid (1), &QVariant::fromValue<QKeySequence>);
127  Globals_ [id] = e;
128 
129  ActionInfo_ [id] = info;
130  }
131 
133  {
134  for (const auto& entity : Globals_)
135  CoreProxy_->GetEntityManager ()->HandleEntity (entity);
136  }
137 
138  void ShortcutManager::SetShortcut (const QString& id, const QKeySequences_t& seqs)
139  {
140  for (auto act : Actions_ [id])
141  act->setShortcuts (seqs);
142 
143  for (auto sc : Shortcuts_ [id])
144  {
145  sc->setKey (seqs.value (0));
146  qDeleteAll (Shortcut2Subs_.take (sc));
147 
148  const int seqsSize = seqs.size ();
149  for (int i = 1; i < seqsSize; ++i)
150  {
151  auto subsc = new QShortcut { sc->parentWidget () };
152  subsc->setContext (sc->context ());
153  subsc->setKey (seqs.value (i));
154  connect (subsc,
155  SIGNAL (activated ()),
156  sc,
157  SIGNAL (activated ()));
158  Shortcut2Subs_ [sc] << subsc;
159  }
160  }
161 
162  if (Globals_.contains (id))
163  {
164  auto& e = Globals_ [id];
165  e.Additional_ ["Shortcut"] = QVariant::fromValue (seqs.value (0));
166  e.Additional_ ["AltShortcuts"] = Util::Map (seqs.mid (1),
167  &QVariant::fromValue<QKeySequence>);
168  CoreProxy_->GetEntityManager ()->HandleEntity (e);
169  }
170  }
171 
173  {
174  return ActionInfo_;
175  }
176 
177  ShortcutManager& ShortcutManager::operator<< (const QPair<QString, QAction*>& pair)
178  {
179  RegisterAction (pair.first, pair.second);
180  return *this;
181  }
182 
183  bool ShortcutManager::HasActionInfo (const QString& id) const
184  {
185  return ActionInfo_.contains (id) &&
186  !ActionInfo_ [id].UserVisibleText_.isEmpty ();
187  }
188 
189  void ShortcutManager::handleActionDestroyed ()
190  {
191  auto act = static_cast<QAction*> (sender ());
192  for (auto& list : Actions_)
193  list.removeAll (act);
194  }
195 
196  void ShortcutManager::handleShortcutDestroyed()
197  {
198  auto sc = static_cast<QShortcut*> (sender ());
199  for (auto& list : Shortcuts_)
200  list.removeAll (sc);
201 
202  qDeleteAll (Shortcut2Subs_.take (sc));
203  }
204 }
205 }
void SetShortcut(const QString &id, const QKeySequences_t &sequences)
Sets the key sequence for the given action.
ShortcutManager(ICoreProxy_ptr proxy, QObject *parent=nullptr)
Creates the shortcut manager.
void RegisterAction(const QString &id, QAction *action)
Registers the given QAction by the given id.
Describes an action exposed in shortcut manager.
void RegisterActions(const std::initializer_list< IDPair_t > &actions)
void RegisterGlobalShortcut(const QString &id, QObject *target, const QByteArray &method, const ActionInfo &info)
Registers the given global shortcut with the given id.
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:105
auto Map(Container &&c, F f)
Definition: prelude.h:164
void SetObject(QObject *pluginObj)
Sets the plugin instance object of this manager.
void RegisterActionInfo(const QString &id, const ActionInfo &info)
Registers the given action info with the given id.
void RegisterShortcut(const QString &id, const ActionInfo &info, QShortcut *shortcut)
Registers the given QShortcut with the given id.
void AnnounceGlobalShorcuts()
Announces the global shortcuts.
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:202
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:188
Aids in providing configurable shortcuts.
A message used for inter-plugin communication.
Definition: structures.h:119
QKeySequences_t Seqs_
List of key sequences for this action.
Definition: constants.h:35
QMap< QString, ActionInfo > GetActionInfo() const
Returns the map with information about actions.
Definition: anutil.h:38