1 /* This file is part of the KDE libraries
2     Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
3               (C) 1999 Simon Hausmann <hausmann@kde.org>
4               (C) 2000 Nicolas Hadacek <haadcek@kde.org>
5               (C) 2000 Kurt Granroth <granroth@kde.org>
6               (C) 2000 Michael Koch <koch@kde.org>
7               (C) 2001 Holger Freyther <freyther@kde.org>
8               (C) 2002 Ellis Whitehead <ellis@kde.org>
9               (C) 2002 Joseph Wenninger <jowenn@kde.org>
10               (C) 2005-2006 Hamish Rodda <rodda@kde.org>
11 
12     This library is free software; you can redistribute it and/or
13     modify it under the terms of the GNU Library General Public
14     License version 2 as published by the Free Software Foundation.
15 
16     This library is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     Library General Public License for more details.
20 
21     You should have received a copy of the GNU Library General Public License
22     along with this library; see the file COPYING.LIB.  If not, write to
23     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24     Boston, MA 02110-1301, USA.
25 */
26 
27 #include "kaction.h"
28 #include "kaction_p.h"
29 #include "klocalizedstring.h"
30 #include "kauthobjectdecorator.h"
31 
32 #include <QApplication>
33 #include <QHBoxLayout>
34 #include <QShortcutEvent>
35 #include <QToolBar>
36 
37 #include <kdebug.h>
38 
39 //---------------------------------------------------------------------
40 // KActionPrivate
41 //---------------------------------------------------------------------
42 
init(KAction * q_ptr)43 void KActionPrivate::init(KAction *q_ptr)
44 {
45     q = q_ptr;
46 
47     QObject::connect(q, SIGNAL(triggered(bool)), q, SLOT(slotTriggered()));
48 
49     q->setProperty("isShortcutConfigurable", true);
50 
51     decorator = new KAuth::ObjectDecorator(q);
52     QObject::connect(decorator, SIGNAL(authorized(KAuth::Action)),
53                      q, SIGNAL(authorized(KAuth::Action)));
54     QObject::connect(KGlobalAccel::self(), SIGNAL(globalShortcutChanged(QAction*,QKeySequence)),
55                      q, SLOT(_k_emitActionGlobalShortcutChanged(QAction*,QKeySequence)));
56 }
57 
slotTriggered()58 void KActionPrivate::slotTriggered()
59 {
60 #ifdef KDE3_SUPPORT
61     emit q->activated();
62 #endif
63     emit q->triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
64 }
65 
_k_emitActionGlobalShortcutChanged(QAction * action,const QKeySequence & seq)66 void KActionPrivate::_k_emitActionGlobalShortcutChanged(QAction *action, const QKeySequence &seq)
67 {
68     if (action == q) {
69         // reemit the legacy KAction::globalShortcutChanged
70         // TODO: completely remove this method when KAction::globalShortcutChanged signal will be removed
71         emit q->globalShortcutChanged(seq);
72     }
73 }
74 
75 //---------------------------------------------------------------------
76 // KAction
77 //---------------------------------------------------------------------
78 
KAction(QObject * parent)79 KAction::KAction(QObject *parent)
80     : QWidgetAction(parent), d(new KActionPrivate)
81 {
82     d->init(this);
83 }
84 
KAction(const QString & text,QObject * parent)85 KAction::KAction(const QString &text, QObject *parent)
86     : QWidgetAction(parent), d(new KActionPrivate)
87 {
88     d->init(this);
89     setText(text);
90 }
91 
KAction(const QIcon & icon,const QString & text,QObject * parent)92 KAction::KAction(const QIcon &icon, const QString &text, QObject *parent)
93     : QWidgetAction(parent), d(new KActionPrivate)
94 {
95     d->init(this);
96     setIcon(icon);
97     setText(text);
98 }
99 
~KAction()100 KAction::~KAction()
101 {
102     delete d;
103 }
104 
isShortcutConfigurable() const105 bool KAction::isShortcutConfigurable() const
106 {
107     return property("isShortcutConfigurable").toBool();
108 }
109 
setShortcutConfigurable(bool b)110 void KAction::setShortcutConfigurable(bool b)
111 {
112     setProperty("isShortcutConfigurable", b);
113 }
114 
shortcut(ShortcutTypes type) const115 KShortcut KAction::shortcut(ShortcutTypes type) const
116 {
117     Q_ASSERT(type);
118 
119     if (type == DefaultShortcut) {
120         QList<QKeySequence> shortcuts = property("defaultShortcuts").value<QList<QKeySequence> >();
121         return KShortcut(shortcuts);
122     }
123 
124     QKeySequence primary = shortcuts().value(0);
125     QKeySequence secondary = shortcuts().value(1);
126     return KShortcut(primary, secondary);
127 }
128 
setShortcut(const KShortcut & shortcut,ShortcutTypes type)129 void KAction::setShortcut(const KShortcut &shortcut, ShortcutTypes type)
130 {
131     Q_ASSERT(type);
132 
133     if (type & DefaultShortcut) {
134         setProperty("defaultShortcuts", QVariant::fromValue(shortcut.toList()));
135     }
136 
137     if (type & ActiveShortcut) {
138         QAction::setShortcuts(shortcut);
139     }
140 }
141 
setShortcut(const QKeySequence & keySeq,ShortcutTypes type)142 void KAction::setShortcut(const QKeySequence &keySeq, ShortcutTypes type)
143 {
144     Q_ASSERT(type);
145 
146     if (type & DefaultShortcut) {
147         setProperty("defaultShortcuts", QVariant::fromValue(QList<QKeySequence>() << keySeq));
148     }
149 
150     if (type & ActiveShortcut) {
151         QAction::setShortcut(keySeq);
152     }
153 }
154 
setShortcuts(const QList<QKeySequence> & shortcuts,ShortcutTypes type)155 void KAction::setShortcuts(const QList<QKeySequence> &shortcuts, ShortcutTypes type)
156 {
157     setShortcut(KShortcut(shortcuts), type);
158 }
159 
globalShortcut(ShortcutTypes type) const160 KShortcut KAction::globalShortcut(ShortcutTypes type) const
161 {
162     Q_ASSERT(type);
163 
164     if (type == DefaultShortcut) {
165         return KGlobalAccel::self()->defaultShortcut(this);
166     }
167 
168     return KGlobalAccel::self()->shortcut(this);
169 }
170 
setGlobalShortcut(const KShortcut & shortcut,ShortcutTypes type,GlobalShortcutLoading load)171 void KAction::setGlobalShortcut(const KShortcut &shortcut, ShortcutTypes type,
172                                 GlobalShortcutLoading load)
173 {
174     Q_ASSERT(type);
175 
176     if ((type & DefaultShortcut) && globalShortcut(DefaultShortcut) != shortcut) {
177         KGlobalAccel::self()->setDefaultShortcut(this, shortcut,
178                 static_cast<KGlobalAccel::GlobalShortcutLoading>(load));
179     }
180 
181     if ((type & ActiveShortcut) && globalShortcut(ActiveShortcut) != shortcut) {
182         KGlobalAccel::self()->setShortcut(this, shortcut,
183                                           static_cast<KGlobalAccel::GlobalShortcutLoading>(load));
184     }
185 }
186 
187 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
globalShortcutAllowed() const188 bool KAction::globalShortcutAllowed() const
189 {
190     return isGlobalShortcutEnabled();
191 }
192 #endif
193 
isGlobalShortcutEnabled() const194 bool KAction::isGlobalShortcutEnabled() const
195 {
196     return KGlobalAccel::self()->hasShortcut(this);
197 }
198 
199 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
setGlobalShortcutAllowed(bool allowed,GlobalShortcutLoading)200 void KAction::setGlobalShortcutAllowed(bool allowed, GlobalShortcutLoading /* load */)
201 {
202     if (allowed) {
203         //### no-op
204     } else {
205         forgetGlobalShortcut();
206     }
207 }
208 #endif
209 
forgetGlobalShortcut()210 void KAction::forgetGlobalShortcut()
211 {
212     if (isGlobalShortcutEnabled()) {
213         KGlobalAccel::self()->removeAllShortcuts(this);
214     }
215 }
216 
setHelpText(const QString & text)217 void KAction::setHelpText(const QString &text)
218 {
219     setStatusTip(text);
220     setToolTip(text);
221     if (whatsThis().isEmpty()) {
222         setWhatsThis(text);
223     }
224 }
225 
authAction() const226 KAuth::Action KAction::authAction() const
227 {
228     return d->decorator->authAction();
229 }
230 
setAuthAction(const QString & actionName)231 void KAction::setAuthAction(const QString &actionName)
232 {
233     d->decorator->setAuthAction(actionName);
234 }
235 
setAuthAction(const KAuth::Action & action)236 void KAction::setAuthAction(const KAuth::Action &action)
237 {
238     d->decorator->setAuthAction(action);
239 }
240 
241 #include "moc_kaction.cpp"
242