LeechCraft  0.6.70-6645-gcd10d7e
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  const QIcon& icon = act->icon ().isNull () ?
64  CoreProxy_->GetIconThemeManager ()->GetIcon (act->property ("ActionIcon").toString ()) :
65  act->icon ();
67  { act->text (), act->shortcuts (), icon });
68 
69  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
70  SetShortcut (id,
71  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
72  }
73 
74  void ShortcutManager::RegisterShortcut (const QString& id, const ActionInfo& info, QShortcut *shortcut)
75  {
76  Shortcuts_ [id] << shortcut;
77  connect (shortcut,
78  SIGNAL (destroyed ()),
79  this,
80  SLOT (handleShortcutDestroyed ()));
81 
82  RegisterActionInfo (id, info);
83 
84  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
85  SetShortcut (id,
86  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
87  }
88 
89  void ShortcutManager::RegisterActionInfo (const QString& id, const ActionInfo& info)
90  {
91  if (!ActionInfo_.contains (id) ||
92  ActionInfo_ [id].UserVisibleText_.isEmpty ())
93  ActionInfo_ [id] = info;
94  }
95 
96  void ShortcutManager::RegisterGlobalShortcut (const QString& id,
97  QObject *target, const QByteArray& method, const ActionInfo& info)
98  {
99  Entity e = Util::MakeEntity ({}, {}, 0,
100  "x-leechcraft/global-action-register");
101  e.Additional_ ["Receiver"] = QVariant::fromValue (target);
102  e.Additional_ ["ActionID"] = id;
103  e.Additional_ ["Method"] = method;
104  e.Additional_ ["Shortcut"] = QVariant::fromValue (info.Seqs_.value (0));
105  e.Additional_ ["AltShortcuts"] = Util::Map (info.Seqs_.mid (1), &QVariant::fromValue<QKeySequence>);
106  Globals_ [id] = e;
107 
108  ActionInfo_ [id] = info;
109  }
110 
112  {
113  for (const auto& entity : Globals_)
114  CoreProxy_->GetEntityManager ()->HandleEntity (entity);
115  }
116 
117  void ShortcutManager::SetShortcut (const QString& id, const QKeySequences_t& seqs)
118  {
119  for (auto act : Actions_ [id])
120  act->setShortcuts (seqs);
121 
122  for (auto sc : Shortcuts_ [id])
123  {
124  sc->setKey (seqs.value (0));
125  qDeleteAll (Shortcut2Subs_.take (sc));
126 
127  const int seqsSize = seqs.size ();
128  for (int i = 1; i < seqsSize; ++i)
129  {
130  auto subsc = new QShortcut { sc->parentWidget () };
131  subsc->setContext (sc->context ());
132  subsc->setKey (seqs.value (i));
133  connect (subsc,
134  SIGNAL (activated ()),
135  sc,
136  SIGNAL (activated ()));
137  Shortcut2Subs_ [sc] << subsc;
138  }
139  }
140 
141  if (Globals_.contains (id))
142  {
143  auto& e = Globals_ [id];
144  e.Additional_ ["Shortcut"] = QVariant::fromValue (seqs.value (0));
145  e.Additional_ ["AltShortcuts"] = Util::Map (seqs.mid (1),
146  &QVariant::fromValue<QKeySequence>);
147  CoreProxy_->GetEntityManager ()->HandleEntity (e);
148  }
149  }
150 
152  {
153  return ActionInfo_;
154  }
155 
156  ShortcutManager& ShortcutManager::operator<< (const QPair<QString, QAction*>& pair)
157  {
158  RegisterAction (pair.first, pair.second);
159  return *this;
160  }
161 
162  void ShortcutManager::handleActionDestroyed ()
163  {
164  auto act = static_cast<QAction*> (sender ());
165  for (auto& list : Actions_)
166  list.removeAll (act);
167  }
168 
169  void ShortcutManager::handleShortcutDestroyed()
170  {
171  auto sc = static_cast<QShortcut*> (sender ());
172  for (auto& list : Shortcuts_)
173  list.removeAll (sc);
174 
175  qDeleteAll (Shortcut2Subs_.take (sc));
176  }
177 }
178 }
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:105
QMap< QString, ActionInfo > GetActionInfo() const
Returns the map with information about actions.
void RegisterShortcut(const QString &id, const ActionInfo &info, QShortcut *shortcut)
Registers the given QShortcut with the given id.
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(const Container< T > &c, F f) -> typename std::enable_if<!std::is_same< void, decltype(Invoke(f,*c.begin()))>::value, WrapType_t< Container< typename std::decay< decltype(Invoke(f,*c.begin()))>::type >>>::type
Definition: prelude.h:129
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:223
void SetShortcut(const QString &id, const QKeySequences_t &sequences)
Sets the key sequence for the given action.
Definition: anutil.h:38
Describes parameters of an entity.
Definition: structures.h:154
void RegisterActionInfo(const QString &id, const ActionInfo &info)
Registers the given action info with the given id.