LeechCraft 0.6.70-17609-g3dde4097dd
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 <QWidget>
14#include <util/xpc/util.h>
15#include <util/sll/prelude.h>
20
21namespace LC::Util
22{
23 ShortcutManager::ShortcutManager (const ICoreProxy_ptr& proxy, QObject *parent)
24 : QObject { parent }
25 , CoreProxy_ { proxy }
26 , ContextObj_ { parent }
27 {
28 }
29
30 void ShortcutManager::RegisterAction (const QByteArray& id, QAction *act)
31 {
32 Actions_ [id] << act;
33 connect (act,
34 &QObject::destroyed,
35 this,
36 [this, act]
37 {
38 for (auto& list : Actions_)
39 list.removeAll (act);
40 });
41
42 if (HasActionInfo (id))
43 {
44 const auto& info = ActionInfo_ [id];
45 if (act->text ().isEmpty ())
46 act->setText (info.Text_);
47 if (act->icon ().isNull ())
48 Util::Visit (info.Icon_,
49 [] (Util::Void) {},
50 [this, act] (const QByteArray& name)
51 {
52 act->setIcon (CoreProxy_->GetIconThemeManager ()->GetIcon (name));
53 act->setProperty ("ActionIcon", name);
54 },
55 [act] (const QIcon& icon) { act->setIcon (icon); });
56 }
57 else
58 {
59 const auto& icon = act->icon ().isNull () ?
60 CoreProxy_->GetIconThemeManager ()->GetIcon (act->property ("ActionIcon").toString ()) :
61 act->icon ();
62 auto shortcuts = act->shortcuts ();
64 {
65 act->text (),
66 shortcuts.value (0),
67 icon,
68 shortcuts.size () > 1 ? shortcuts.mid (1) : QList<QKeySequence> {},
69 });
70 }
71
72 if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
73 SetShortcut (id,
74 CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
75 }
76
77 void ShortcutManager::RegisterActions (const std::initializer_list<IDPair_t>& pairs)
78 {
79 for (const auto& [id, act] : pairs)
80 RegisterAction (id, act);
81 }
82
83 void ShortcutManager::RegisterShortcut (const QByteArray& id, const ActionInfo& info, QShortcut *shortcut)
84 {
85 Shortcuts_ [id] << shortcut;
86 connect (shortcut,
87 &QObject::destroyed,
88 this,
89 [this, shortcut]
90 {
91 for (auto& list : Shortcuts_)
92 list.removeAll (shortcut);
93
94 qDeleteAll (Shortcut2Subs_.take (shortcut));
95 });
96
97 RegisterActionInfo (id, info);
98
99 if (CoreProxy_->GetShortcutProxy ()->HasObject (ContextObj_))
100 SetShortcut (id,
101 CoreProxy_->GetShortcutProxy ()->GetShortcuts (ContextObj_, id));
102 }
103
104 void ShortcutManager::RegisterActionInfo (const QByteArray& id, const ActionInfo& info)
105 {
106 if (!HasActionInfo (id))
107 ActionInfo_ [id] = info;
108 }
109
110 void ShortcutManager::RegisterGlobalShortcut (const QByteArray& id,
111 QObject *target, const QByteArray& method, const ActionInfo& info)
112 {
114 using namespace EF::GlobalAction;
115 e.Additional_ [Receiver] = QVariant::fromValue (target);
116 e.Additional_ [ActionID] = id;
117 e.Additional_ [Method] = method;
118 e.Additional_ [Shortcut] = QVariant::fromValue (info.Seq_);
119 e.Additional_ [AltShortcuts] = Util::Map (info.AdditionalSeqs_,
120 [] (const QKeySequence& seq) { return QVariant::fromValue (seq); });
121 Globals_ [id] = e;
122
123 ActionInfo_ [id] = info;
124 }
125
127 {
128 for (const auto& entity : std::as_const (Globals_))
129 CoreProxy_->GetEntityManager ()->HandleEntity (entity);
130 }
131
132 void ShortcutManager::SetShortcut (const QByteArray& id, const QKeySequences_t& seqs)
133 {
134 for (auto act : std::as_const (Actions_ [id]))
135 act->setShortcuts (seqs);
136
137 for (auto sc : std::as_const (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->parent () };
146 subsc->setContext (sc->context ());
147 subsc->setKey (seqs.value (i));
148 connect (subsc,
149 &QShortcut::activated,
150 sc,
151 &QShortcut::activated);
152 Shortcut2Subs_ [sc] << subsc;
153 }
154 }
155
156 if (Globals_.contains (id))
157 {
158 auto& e = Globals_ [id];
159 e.Additional_ [QStringLiteral ("Shortcut")] = QVariant::fromValue (seqs.value (0));
160 e.Additional_ [QStringLiteral ("AltShortcuts")] = Util::Map (seqs.mid (1),
161 [] (const QKeySequence& seq) { return QVariant::fromValue (seq); });
162 CoreProxy_->GetEntityManager ()->HandleEntity (e);
163 }
164 }
165
167 {
168 return ActionInfo_;
169 }
170
171 ShortcutManager& ShortcutManager::operator<< (const QPair<QByteArray, QAction*>& pair)
172 {
173 RegisterAction (pair.first, pair.second);
174 return *this;
175 }
176
177 bool ShortcutManager::HasActionInfo (const QByteArray& id) const
178 {
179 return ActionInfo_.contains (id) &&
180 !ActionInfo_ [id].Text_.isEmpty ();
181 }
182}
void AnnounceGlobalShorcuts()
Announces the global shortcuts.
ShortcutManager(const ICoreProxy_ptr &proxy, QObject *parent)
Creates the shortcut manager.
ShortcutManager & operator<<(const QPair< QByteArray, QAction * > &pair)
Utility function equivalent to RegisterAction().
void RegisterAction(const QByteArray &id, QAction *action)
Registers the given QAction by the given id.
void SetShortcut(const QByteArray &id, const QKeySequences_t &sequences)
Sets the key sequence for the given action.
void RegisterGlobalShortcut(const QByteArray &id, QObject *target, const QByteArray &method, const ActionInfo &info)
Registers the given global shortcut with the given id.
void RegisterActionInfo(const QByteArray &id, const ActionInfo &info)
Registers the given action info with the given id.
QMap< QByteArray, ActionInfo > GetActionInfo() const
Returns the map with information about actions.
void RegisterActions(const std::initializer_list< IDPair_t > &actions)
void RegisterShortcut(const QByteArray &id, const ActionInfo &info, QShortcut *shortcut)
Registers the given QShortcut with the given id.
Definition anutil.h:15
std::shared_ptr< ICoreProxy > ICoreProxy_ptr
Definition icoreproxy.h:181
QList< QKeySequence > QKeySequences_t
Entity fields corresponding to global action registration.
Q_DECL_IMPORT const QString GlobalActionRegister
Registration of a global system-wide action.
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition either.h:207
auto Map(Container &&c, F &&f) noexcept(noexcept(std::is_nothrow_invocable_v< F, decltype(*c.begin())>))
Definition prelude.h:104
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 AdditionalSeqs_
The additional key sequences for this action.
QKeySequence Seq_
The primary key sequence for this action.
A message used for inter-plugin communication.
Definition structures.h:96
QMap< QString, QVariant > Additional_
Additional parameters.
Definition structures.h:164
A proper void type, akin to unit (or ()) type in functional languages.
Definition void.h:21