LeechCraft  0.6.70-10870-g558588d6ec
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 LeechCraft
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::RegisterShortcut (const QString& id, const ActionInfo& info, QShortcut *shortcut)
91  {
92  Shortcuts_ [id] << shortcut;
93  connect (shortcut,
94  SIGNAL (destroyed ()),
95  this,
96  SLOT (handleShortcutDestroyed ()));
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 QString& id, const ActionInfo& info)
106  {
107  if (!HasActionInfo (id))
108  ActionInfo_ [id] = info;
109  }
110 
112  QObject *target, const QByteArray& method, const ActionInfo& info)
113  {
114  Entity e = Util::MakeEntity ({}, {}, 0,
115  "x-leechcraft/global-action-register");
116  e.Additional_ ["Receiver"] = QVariant::fromValue (target);
117  e.Additional_ ["ActionID"] = id;
118  e.Additional_ ["Method"] = method;
119  e.Additional_ ["Shortcut"] = QVariant::fromValue (info.Seqs_.value (0));
120  e.Additional_ ["AltShortcuts"] = Util::Map (info.Seqs_.mid (1), &QVariant::fromValue<QKeySequence>);
121  Globals_ [id] = e;
122 
123  ActionInfo_ [id] = info;
124  }
125 
127  {
128  for (const auto& entity : Globals_)
129  CoreProxy_->GetEntityManager ()->HandleEntity (entity);
130  }
131 
132  void ShortcutManager::SetShortcut (const QString& id, const QKeySequences_t& seqs)
133  {
134  for (auto act : Actions_ [id])
135  act->setShortcuts (seqs);
136 
137  for (auto sc : Shortcuts_ [id])
138  {
139  sc->setKey (seqs.value (0));
140  qDeleteAll (Shortcut2Subs_.take (sc));
141 
142  const int seqsSize = seqs.size ();
143  for (int i = 1; i < seqsSize; ++i)
144  {
145  auto subsc = new QShortcut { sc->parentWidget () };
146  subsc->setContext (sc->context ());
147  subsc->setKey (seqs.value (i));
148  connect (subsc,
149  SIGNAL (activated ()),
150  sc,
151  SIGNAL (activated ()));
152  Shortcut2Subs_ [sc] << subsc;
153  }
154  }
155 
156  if (Globals_.contains (id))
157  {
158  auto& e = Globals_ [id];
159  e.Additional_ ["Shortcut"] = QVariant::fromValue (seqs.value (0));
160  e.Additional_ ["AltShortcuts"] = Util::Map (seqs.mid (1),
161  &QVariant::fromValue<QKeySequence>);
162  CoreProxy_->GetEntityManager ()->HandleEntity (e);
163  }
164  }
165 
167  {
168  return ActionInfo_;
169  }
170 
171  ShortcutManager& ShortcutManager::operator<< (const QPair<QString, QAction*>& pair)
172  {
173  RegisterAction (pair.first, pair.second);
174  return *this;
175  }
176 
177  bool ShortcutManager::HasActionInfo (const QString& id) const
178  {
179  return ActionInfo_.contains (id) &&
180  !ActionInfo_ [id].UserVisibleText_.isEmpty ();
181  }
182 
183  void ShortcutManager::handleActionDestroyed ()
184  {
185  auto act = static_cast<QAction*> (sender ());
186  for (auto& list : Actions_)
187  list.removeAll (act);
188  }
189 
190  void ShortcutManager::handleShortcutDestroyed()
191  {
192  auto sc = static_cast<QShortcut*> (sender ());
193  for (auto& list : Shortcuts_)
194  list.removeAll (sc);
195 
196  qDeleteAll (Shortcut2Subs_.take (sc));
197  }
198 }
199 }
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:105
void RegisterShortcut(const QString &id, const ActionInfo &info, QShortcut *shortcut)
Registers the given QShortcut with the given id.
QMap< QString, ActionInfo > GetActionInfo() const
Returns the map with information about actions.
void SetObject(QObject *pluginObj)
Sets the plugin instance object of this manager.
void AnnounceGlobalShorcuts()
Announces the global shortcuts.
ShortcutManager(ICoreProxy_ptr proxy, QObject *parent=nullptr)
Creates the shortcut manager.
void RegisterGlobalShortcut(const QString &id, QObject *target, const QByteArray &method, const ActionInfo &info)
Registers the given global shortcut with the given id.
void RegisterAction(const QString &id, QAction *action)
Registers the given QAction by the given id.
Describes an action exposed in shortcut manager.
QKeySequences_t Seqs_
List of key sequences for this action.
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
Aids in providing configurable shortcuts.
auto Map(Container &&c, F f)
Definition: prelude.h:165
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:188
void SetShortcut(const QString &id, const QKeySequences_t &sequences)
Sets the key sequence for the given action.
Definition: anutil.h:38
A message used for inter-plugin communication.
Definition: structures.h:119
void RegisterActionInfo(const QString &id, const ActionInfo &info)
Registers the given action info with the given id.