LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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 <util/xpc/util.h>
13#include <util/sll/prelude.h>
18
19namespace LC::Util
20{
21 ShortcutManager::ShortcutManager (const ICoreProxy_ptr& proxy, QObject *parent)
22 : QObject { parent }
23 , CoreProxy_ { proxy }
24 {
25 }
26
27 void ShortcutManager::SetObject (QObject *obj)
28 {
29 ContextObj_ = obj;
30 }
31
32 void ShortcutManager::RegisterAction (const QString& id, QAction *act)
33 {
34 Actions_ [id] << act;
35 connect (act,
36 &QObject::destroyed,
37 this,
38 [this, act]
39 {
40 for (auto& list : Actions_)
41 list.removeAll (act);
42 });
43
44 if (HasActionInfo (id))
45 {
46 const auto& info = ActionInfo_ [id];
47 if (act->text ().isEmpty ())
48 act->setText (info.UserVisibleText_);
49 if (act->icon ().isNull () &&
50 act->property ("ActionIcon").isNull ())
51 act->setIcon (info.Icon_);
52 }
53 else
54 {
55 const auto& icon = act->icon ().isNull () ?
56 CoreProxy_->GetIconThemeManager ()->GetIcon (act->property ("ActionIcon").toString ()) :
57 act->icon ();
59 {
60 act->text (),
61 act->shortcuts (),
62 icon
63 });
64 }
65
66 if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
67 SetShortcut (id,
68 CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
69 }
70
71 void ShortcutManager::RegisterActions (const std::initializer_list<IDPair_t>& pairs)
72 {
73 for (const auto& [id, act] : pairs)
74 RegisterAction (id, act);
75 }
76
77 void ShortcutManager::RegisterShortcut (const QString& id, const ActionInfo& info, QShortcut *shortcut)
78 {
79 Shortcuts_ [id] << shortcut;
80 connect (shortcut,
81 &QObject::destroyed,
82 this,
83 [this, shortcut]
84 {
85 for (auto& list : Shortcuts_)
86 list.removeAll (shortcut);
87
88 qDeleteAll (Shortcut2Subs_.take (shortcut));
89 });
90
91 RegisterActionInfo (id, info);
92
93 if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
94 SetShortcut (id,
95 CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
96 }
97
98 void ShortcutManager::RegisterActionInfo (const QString& id, const ActionInfo& info)
99 {
100 if (!HasActionInfo (id))
101 ActionInfo_ [id] = info;
102 }
103
105 QObject *target, const QByteArray& method, const ActionInfo& info)
106 {
107 Entity e = Util::MakeEntity ({}, {}, {},
108 QStringLiteral ("x-leechcraft/global-action-register"));
109 e.Additional_ [QStringLiteral ("Receiver")] = QVariant::fromValue (target);
110 e.Additional_ [QStringLiteral ("ActionID")] = id;
111 e.Additional_ [QStringLiteral ("Method")] = method;
112 e.Additional_ [QStringLiteral ("Shortcut")] = QVariant::fromValue (info.Seqs_.value (0));
113 e.Additional_ [QStringLiteral ("AltShortcuts")] = Util::Map (info.Seqs_.mid (1), &QVariant::fromValue<QKeySequence>);
114 Globals_ [id] = e;
115
116 ActionInfo_ [id] = info;
117 }
118
120 {
121 for (const auto& entity : Globals_)
122 CoreProxy_->GetEntityManager ()->HandleEntity (entity);
123 }
124
125 void ShortcutManager::SetShortcut (const QString& id, const QKeySequences_t& seqs)
126 {
127 for (auto act : Actions_ [id])
128 act->setShortcuts (seqs);
129
130 for (auto sc : Shortcuts_ [id])
131 {
132 sc->setKey (seqs.value (0));
133 qDeleteAll (Shortcut2Subs_.take (sc));
134
135 const int seqsSize = seqs.size ();
136 for (int i = 1; i < seqsSize; ++i)
137 {
138 auto subsc = new QShortcut { sc->parentWidget () };
139 subsc->setContext (sc->context ());
140 subsc->setKey (seqs.value (i));
141 connect (subsc,
142 &QShortcut::activated,
143 sc,
144 &QShortcut::activated);
145 Shortcut2Subs_ [sc] << subsc;
146 }
147 }
148
149 if (Globals_.contains (id))
150 {
151 auto& e = Globals_ [id];
152 e.Additional_ [QStringLiteral ("Shortcut")] = QVariant::fromValue (seqs.value (0));
153 e.Additional_ [QStringLiteral ("AltShortcuts")] = Util::Map (seqs.mid (1),
154 &QVariant::fromValue<QKeySequence>);
155 CoreProxy_->GetEntityManager ()->HandleEntity (e);
156 }
157 }
158
160 {
161 return ActionInfo_;
162 }
163
164 ShortcutManager& ShortcutManager::operator<< (const QPair<QString, QAction*>& pair)
165 {
166 RegisterAction (pair.first, pair.second);
167 return *this;
168 }
169
170 bool ShortcutManager::HasActionInfo (const QString& id) const
171 {
172 return ActionInfo_.contains (id) &&
173 !ActionInfo_ [id].UserVisibleText_.isEmpty ();
174 }
175}
Aids in providing configurable shortcuts.
void AnnounceGlobalShorcuts()
Announces the global shortcuts.
void RegisterActionInfo(const QString &id, const ActionInfo &info)
Registers the given action info with the given id.
void RegisterAction(const QString &id, QAction *action)
Registers the given QAction by 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 RegisterGlobalShortcut(const QString &id, QObject *target, const QByteArray &method, const ActionInfo &info)
Registers the given global shortcut with the given id.
ShortcutManager(const ICoreProxy_ptr &proxy, QObject *parent=nullptr)
Creates the shortcut manager.
void SetShortcut(const QString &id, const QKeySequences_t &sequences)
Sets the key sequence for the given action.
void RegisterShortcut(const QString &id, const ActionInfo &info, QShortcut *shortcut)
Registers the given QShortcut with the given id.
void RegisterActions(const std::initializer_list< IDPair_t > &actions)
ShortcutManager & operator<<(const QPair< QString, QAction * > &pair)
Utility function equivalent to RegisterAction().
Definition: anutil.h:17
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition: icoreproxy.h:181
auto Map(Container &&c, F f)
Definition: prelude.h:143
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:82
Describes an action exposed in shortcut manager.
QKeySequences_t Seqs_
List of key sequences for this action.
A message used for inter-plugin communication.
Definition: structures.h:96
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:164