LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  , ContextObj_ (0)
48  {
49  }
50 
51  void ShortcutManager::SetObject (QObject *obj)
52  {
53  ContextObj_ = obj;
54  }
55 
56  void ShortcutManager::RegisterAction (const QString& id, QAction *act)
57  {
58  Actions_ [id] << act;
59  connect (act,
60  SIGNAL (destroyed ()),
61  this,
62  SLOT (handleActionDestroyed ()));
63 
64  const QIcon& icon = act->icon ().isNull () ?
65  CoreProxy_->GetIconThemeManager ()->GetIcon (act->property ("ActionIcon").toString ()) :
66  act->icon ();
68  { act->text (), act->shortcuts (), icon });
69 
70  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
71  SetShortcut (id,
72  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
73  }
74 
75  void ShortcutManager::RegisterShortcut (const QString& id, const ActionInfo& info, QShortcut* shortcut)
76  {
77  Shortcuts_ [id] << shortcut;
78  connect (shortcut,
79  SIGNAL (destroyed ()),
80  this,
81  SLOT (handleShortcutDestroyed ()));
82 
83  RegisterActionInfo (id, info);
84 
85  if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
86  SetShortcut (id,
87  CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
88  }
89 
90  void ShortcutManager::RegisterActionInfo (const QString& id, const ActionInfo& info)
91  {
92  if (!ActionInfo_.contains (id) ||
93  ActionInfo_ [id].UserVisibleText_.isEmpty ())
94  ActionInfo_ [id] = info;
95  }
96 
97  void ShortcutManager::RegisterGlobalShortcut (const QString& id,
98  QObject *target, const QByteArray& method, const ActionInfo& info)
99  {
100  Entity e = Util::MakeEntity ({}, {}, 0,
101  "x-leechcraft/global-action-register");
102  e.Additional_ ["Receiver"] = QVariant::fromValue (target);
103  e.Additional_ ["ActionID"] = id;
104  e.Additional_ ["Method"] = method;
105  e.Additional_ ["Shortcut"] = QVariant::fromValue (info.Seqs_.value (0));
106  e.Additional_ ["AltShortcuts"] = Util::Map (info.Seqs_.mid (1), &QVariant::fromValue<QKeySequence>);
107  Globals_ [id] = e;
108 
109  ActionInfo_ [id] = info;
110  }
111 
113  {
114  for (const auto& entity : Globals_)
115  CoreProxy_->GetEntityManager ()->HandleEntity (entity);
116  }
117 
118  void ShortcutManager::SetShortcut (const QString& id, const QKeySequences_t& seqs)
119  {
120  for (auto act : Actions_ [id])
121  act->setShortcuts (seqs);
122 
123  for (auto sc : Shortcuts_ [id])
124  {
125  sc->setKey (seqs.value (0));
126  qDeleteAll (Shortcut2Subs_.take (sc));
127 
128  const auto seqsSize = seqs.size ();
129  if (seqsSize > 1)
130  for (int i = 1; i < seqsSize; ++i)
131  {
132  auto subsc = new QShortcut { sc->parentWidget () };
133  subsc->setContext (sc->context ());
134  subsc->setKey (seqs.value (i));
135  connect (subsc,
136  SIGNAL (activated ()),
137  sc,
138  SIGNAL (activated ()));
139  Shortcut2Subs_ [sc] << subsc;
140  }
141  }
142 
143  if (Globals_.contains (id))
144  {
145  auto& e = Globals_ [id];
146  e.Additional_ ["Shortcut"] = QVariant::fromValue (seqs.value (0));
147  e.Additional_ ["AltShortcuts"] = Util::Map (seqs.mid (1),
148  &QVariant::fromValue<QKeySequence>);
149  CoreProxy_->GetEntityManager ()->HandleEntity (e);
150  }
151  }
152 
154  {
155  return ActionInfo_;
156  }
157 
158  ShortcutManager& ShortcutManager::operator<< (const QPair<QString, QAction*>& pair)
159  {
160  RegisterAction (pair.first, pair.second);
161  return *this;
162  }
163 
164  void ShortcutManager::handleActionDestroyed ()
165  {
166  auto act = static_cast<QAction*> (sender ());
167  for (const auto& id : Actions_.keys ())
168  Actions_ [id].removeAll (act);
169  }
170 
171  void ShortcutManager::handleShortcutDestroyed()
172  {
173  auto sc = static_cast<QShortcut*> (sender ());
174  for (const auto& id : Shortcuts_.keys ())
175  Shortcuts_ [id].removeAll (sc);
176 
177  qDeleteAll (Shortcut2Subs_.take (sc));
178  }
179 }
180 }
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.
ShortcutManager(ICoreProxy_ptr proxy, QObject *parent=0)
Creates the shortcut manager.
void SetObject(QObject *pluginObj)
Sets the plugin instance object of this manager.
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:225
void AnnounceGlobalShorcuts()
Announces the global shortcuts.
auto Map(const Container< T > &c, F f) -> typename std::enable_if<!std::is_same< void, decltype(Invoke(f, std::declval< T >()))>::value, WrapType_t< Container< typename std::decay< decltype(Invoke(f, std::declval< T >()))>::type >>>::type
Definition: prelude.h:117
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.
Aids in providing configurable shortcuts.
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.