File indexing completed on 2024-04-21 14:55:49

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
0003               (C) 1999 Simon Hausmann <hausmann@kde.org>
0004               (C) 2000 Nicolas Hadacek <haadcek@kde.org>
0005               (C) 2000 Kurt Granroth <granroth@kde.org>
0006               (C) 2000 Michael Koch <koch@kde.org>
0007               (C) 2001 Holger Freyther <freyther@kde.org>
0008               (C) 2002 Ellis Whitehead <ellis@kde.org>
0009               (C) 2002 Joseph Wenninger <jowenn@kde.org>
0010               (C) 2005-2006 Hamish Rodda <rodda@kde.org>
0011 
0012     This library is free software; you can redistribute it and/or
0013     modify it under the terms of the GNU Library General Public
0014     License version 2 as published by the Free Software Foundation.
0015 
0016     This library is distributed in the hope that it will be useful,
0017     but WITHOUT ANY WARRANTY; without even the implied warranty of
0018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0019     Library General Public License for more details.
0020 
0021     You should have received a copy of the GNU Library General Public License
0022     along with this library; see the file COPYING.LIB.  If not, write to
0023     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0024     Boston, MA 02110-1301, USA.
0025 */
0026 
0027 #include "kaction.h"
0028 #include "kaction_p.h"
0029 #include "klocalizedstring.h"
0030 #include <kauth/objectdecorator.h>
0031 
0032 #include <QApplication>
0033 #include <QHBoxLayout>
0034 #include <QShortcutEvent>
0035 #include <QToolBar>
0036 
0037 #include <kdebug.h>
0038 
0039 //---------------------------------------------------------------------
0040 // KActionPrivate
0041 //---------------------------------------------------------------------
0042 
0043 void KActionPrivate::init(KAction *q_ptr)
0044 {
0045     q = q_ptr;
0046 
0047     QObject::connect(q, SIGNAL(triggered(bool)), q, SLOT(slotTriggered()));
0048 
0049     q->setProperty("isShortcutConfigurable", true);
0050 
0051     decorator = new KAuth::ObjectDecorator(q);
0052     QObject::connect(decorator, SIGNAL(authorized(KAuth::Action)),
0053                      q, SIGNAL(authorized(KAuth::Action)));
0054     QObject::connect(KGlobalAccel::self(), SIGNAL(globalShortcutChanged(QAction*,QKeySequence)),
0055                      q, SLOT(_k_emitActionGlobalShortcutChanged(QAction*,QKeySequence)));
0056 }
0057 
0058 void KActionPrivate::slotTriggered()
0059 {
0060 #ifdef KDE3_SUPPORT
0061     emit q->activated();
0062 #endif
0063     emit q->triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
0064 }
0065 
0066 void KActionPrivate::_k_emitActionGlobalShortcutChanged(QAction *action, const QKeySequence &seq)
0067 {
0068     if (action == q) {
0069         // reemit the legacy KAction::globalShortcutChanged
0070         // TODO: completely remove this method when KAction::globalShortcutChanged signal will be removed
0071         emit q->globalShortcutChanged(seq);
0072     }
0073 }
0074 
0075 //---------------------------------------------------------------------
0076 // KAction
0077 //---------------------------------------------------------------------
0078 
0079 KAction::KAction(QObject *parent)
0080     : QWidgetAction(parent), d(new KActionPrivate)
0081 {
0082     d->init(this);
0083 }
0084 
0085 KAction::KAction(const QString &text, QObject *parent)
0086     : QWidgetAction(parent), d(new KActionPrivate)
0087 {
0088     d->init(this);
0089     setText(text);
0090 }
0091 
0092 KAction::KAction(const QIcon &icon, const QString &text, QObject *parent)
0093     : QWidgetAction(parent), d(new KActionPrivate)
0094 {
0095     d->init(this);
0096     setIcon(icon);
0097     setText(text);
0098 }
0099 
0100 KAction::~KAction()
0101 {
0102     delete d;
0103 }
0104 
0105 bool KAction::isShortcutConfigurable() const
0106 {
0107     return property("isShortcutConfigurable").toBool();
0108 }
0109 
0110 void KAction::setShortcutConfigurable(bool b)
0111 {
0112     setProperty("isShortcutConfigurable", b);
0113 }
0114 
0115 KShortcut KAction::shortcut(ShortcutTypes type) const
0116 {
0117     Q_ASSERT(type);
0118 
0119     if (type == DefaultShortcut) {
0120         QList<QKeySequence> shortcuts = property("defaultShortcuts").value<QList<QKeySequence> >();
0121         return KShortcut(shortcuts);
0122     }
0123 
0124     QKeySequence primary = shortcuts().value(0);
0125     QKeySequence secondary = shortcuts().value(1);
0126     return KShortcut(primary, secondary);
0127 }
0128 
0129 void KAction::setShortcut(const KShortcut &shortcut, ShortcutTypes type)
0130 {
0131     Q_ASSERT(type);
0132 
0133     if (type & DefaultShortcut) {
0134         setProperty("defaultShortcuts", QVariant::fromValue(shortcut.toList()));
0135     }
0136 
0137     if (type & ActiveShortcut) {
0138         QAction::setShortcuts(shortcut);
0139     }
0140 }
0141 
0142 void KAction::setShortcut(const QKeySequence &keySeq, ShortcutTypes type)
0143 {
0144     Q_ASSERT(type);
0145 
0146     if (type & DefaultShortcut) {
0147         setProperty("defaultShortcuts", QVariant::fromValue(QList<QKeySequence>() << keySeq));
0148     }
0149 
0150     if (type & ActiveShortcut) {
0151         QAction::setShortcut(keySeq);
0152     }
0153 }
0154 
0155 void KAction::setShortcuts(const QList<QKeySequence> &shortcuts, ShortcutTypes type)
0156 {
0157     setShortcut(KShortcut(shortcuts), type);
0158 }
0159 
0160 KShortcut KAction::globalShortcut(ShortcutTypes type) const
0161 {
0162     Q_ASSERT(type);
0163 
0164     if (type == DefaultShortcut) {
0165         return KGlobalAccel::self()->defaultShortcut(this);
0166     }
0167 
0168     return KGlobalAccel::self()->shortcut(this);
0169 }
0170 
0171 void KAction::setGlobalShortcut(const KShortcut &shortcut, ShortcutTypes type,
0172                                 GlobalShortcutLoading load)
0173 {
0174     Q_ASSERT(type);
0175 
0176     if ((type & DefaultShortcut) && globalShortcut(DefaultShortcut) != shortcut) {
0177         KGlobalAccel::self()->setDefaultShortcut(this, shortcut,
0178                 static_cast<KGlobalAccel::GlobalShortcutLoading>(load));
0179     }
0180 
0181     if ((type & ActiveShortcut) && globalShortcut(ActiveShortcut) != shortcut) {
0182         KGlobalAccel::self()->setShortcut(this, shortcut,
0183                                           static_cast<KGlobalAccel::GlobalShortcutLoading>(load));
0184     }
0185 }
0186 
0187 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0188 bool KAction::globalShortcutAllowed() const
0189 {
0190     return isGlobalShortcutEnabled();
0191 }
0192 #endif
0193 
0194 bool KAction::isGlobalShortcutEnabled() const
0195 {
0196     return KGlobalAccel::self()->hasShortcut(this);
0197 }
0198 
0199 #ifndef KDELIBS4SUPPORT_NO_DEPRECATED
0200 void KAction::setGlobalShortcutAllowed(bool allowed, GlobalShortcutLoading /* load */)
0201 {
0202     if (allowed) {
0203         //### no-op
0204     } else {
0205         forgetGlobalShortcut();
0206     }
0207 }
0208 #endif
0209 
0210 void KAction::forgetGlobalShortcut()
0211 {
0212     if (isGlobalShortcutEnabled()) {
0213         KGlobalAccel::self()->removeAllShortcuts(this);
0214     }
0215 }
0216 
0217 void KAction::setHelpText(const QString &text)
0218 {
0219     setStatusTip(text);
0220     setToolTip(text);
0221     if (whatsThis().isEmpty()) {
0222         setWhatsThis(text);
0223     }
0224 }
0225 
0226 KAuth::Action KAction::authAction() const
0227 {
0228     return d->decorator->authAction();
0229 }
0230 
0231 void KAction::setAuthAction(const QString &actionName)
0232 {
0233     d->decorator->setAuthAction(actionName);
0234 }
0235 
0236 void KAction::setAuthAction(const KAuth::Action &action)
0237 {
0238     d->decorator->setAuthAction(action);
0239 }
0240 
0241 #include "moc_kaction.cpp"