• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kaction.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
00003               (C) 1999 Simon Hausmann <hausmann@kde.org>
00004               (C) 2000 Nicolas Hadacek <haadcek@kde.org>
00005               (C) 2000 Kurt Granroth <granroth@kde.org>
00006               (C) 2000 Michael Koch <koch@kde.org>
00007               (C) 2001 Holger Freyther <freyther@kde.org>
00008               (C) 2002 Ellis Whitehead <ellis@kde.org>
00009               (C) 2002 Joseph Wenninger <jowenn@kde.org>
00010               (C) 2005-2006 Hamish Rodda <rodda@kde.org>
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Library General Public
00014     License version 2 as published by the Free Software Foundation.
00015 
00016     This library is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019     Library General Public License for more details.
00020 
00021     You should have received a copy of the GNU Library General Public License
00022     along with this library; see the file COPYING.LIB.  If not, write to
00023     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00024     Boston, MA 02110-1301, USA.
00025 */
00026 
00027 #include "kaction.h"
00028 #include "kaction_p.h"
00029 #include "kglobalaccel_p.h"
00030 
00031 #include <QtGui/QApplication>
00032 
00033 #include <kdebug.h>
00034 
00035 #include "kguiitem.h"
00036 #include "kicon.h"
00037 
00038 //---------------------------------------------------------------------
00039 // KActionPrivate
00040 //---------------------------------------------------------------------
00041 
00042 void KActionPrivate::init(KAction *q_ptr)
00043 {
00044   q = q_ptr;
00045   globalShortcutEnabled = false;
00046   neverSetGlobalShortcut = true;
00047 
00048   QObject::connect(q, SIGNAL(triggered(bool)), q, SLOT(slotTriggered()));
00049 
00050   q->setProperty("isShortcutConfigurable", true);
00051 }
00052 
00053 //---------------------------------------------------------------------
00054 // KAction
00055 //---------------------------------------------------------------------
00056 
00057 KAction::KAction(QObject *parent)
00058   : QWidgetAction(parent), d(new KActionPrivate)
00059 {
00060   d->init(this);
00061 }
00062 
00063 KAction::KAction(const QString &text, QObject *parent)
00064   : QWidgetAction(parent), d(new KActionPrivate)
00065 {
00066   d->init(this);
00067   setText(text);
00068 }
00069 
00070 KAction::KAction(const KIcon &icon, const QString &text, QObject *parent)
00071   : QWidgetAction(parent), d(new KActionPrivate)
00072 {
00073   d->init(this);
00074   setIcon(icon);
00075   setText(text);
00076 }
00077 
00078 KAction::~KAction()
00079 {
00080     if (d->globalShortcutEnabled) {
00081         // - remove the action from KGlobalAccel
00082         d->globalShortcutEnabled = false;
00083         KGlobalAccel::self()->d->remove(this, KGlobalAccelPrivate::SetInactive);
00084     }
00085 
00086     KGestureMap::self()->removeGesture(d->shapeGesture, this);
00087     KGestureMap::self()->removeGesture(d->rockerGesture, this);
00088     delete d;
00089 }
00090 
00091 bool KAction::isShortcutConfigurable() const
00092 {
00093     return property("isShortcutConfigurable").toBool();
00094 }
00095 
00096 void KAction::setShortcutConfigurable( bool b )
00097 {
00098     setProperty("isShortcutConfigurable", b);
00099 }
00100 
00101 KShortcut KAction::shortcut(ShortcutTypes type) const
00102 {
00103   Q_ASSERT(type);
00104 
00105   if (type == DefaultShortcut) {
00106       QKeySequence primary = property("defaultPrimaryShortcut").value<QKeySequence>();
00107       QKeySequence secondary = property("defaultAlternateShortcut").value<QKeySequence>();
00108       return KShortcut(primary, secondary);
00109   }
00110 
00111   QKeySequence primary = shortcuts().value(0);
00112   QKeySequence secondary = shortcuts().value(1);
00113   return KShortcut(primary, secondary);
00114 }
00115 
00116 void KAction::setShortcut( const KShortcut & shortcut, ShortcutTypes type )
00117 {
00118   Q_ASSERT(type);
00119 
00120   if (type & DefaultShortcut) {
00121       setProperty("defaultPrimaryShortcut", shortcut.primary());
00122       setProperty("defaultAlternateShortcut", shortcut.alternate());
00123   }
00124 
00125   if (type & ActiveShortcut) {
00126       QAction::setShortcuts(shortcut);
00127   }
00128 }
00129 
00130 void KAction::setShortcut( const QKeySequence & keySeq, ShortcutTypes type )
00131 {
00132   Q_ASSERT(type);
00133 
00134   if (type & DefaultShortcut)
00135       setProperty("defaultPrimaryShortcut", keySeq);
00136 
00137   if (type & ActiveShortcut) {
00138       QAction::setShortcut(keySeq);
00139   }
00140 }
00141 
00142 void KAction::setShortcuts(const QList<QKeySequence>& shortcuts, ShortcutTypes type)
00143 {
00144   setShortcut(KShortcut(shortcuts), type);
00145 }
00146 
00147 void KActionPrivate::slotTriggered()
00148 {
00149 #ifdef KDE3_SUPPORT
00150   emit q->activated();
00151 #endif
00152   emit q->triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
00153 }
00154 
00155 const KShortcut & KAction::globalShortcut(ShortcutTypes type) const
00156 {
00157   Q_ASSERT(type);
00158 
00159   if (type == DefaultShortcut)
00160     return d->defaultGlobalShortcut;
00161 
00162   return d->globalShortcut;
00163 }
00164 
00165 void KAction::setGlobalShortcut( const KShortcut & shortcut, ShortcutTypes type,
00166                                  GlobalShortcutLoading load )
00167 {
00168   Q_ASSERT(type);
00169   bool changed = false;
00170   
00171   // protect against garbage keycode -1 that Qt sometimes produces for exotic keys;
00172   // at the moment (~mid 2008) Multimedia PlayPause is one of those keys.
00173   int shortcutKeys[8];
00174   for (int i = 0; i < 4; i++) {
00175     shortcutKeys[i] = shortcut.primary()[i];
00176     shortcutKeys[i + 4] = shortcut.alternate()[i];
00177   }
00178   for (int i = 0; i < 8; i++) {
00179     if (shortcutKeys[i] == -1) {
00180       kWarning(283) << "Encountered garbage keycode (keycode = -1) in input, not doing anything.";
00181       return;
00182     }
00183   }
00184 
00185   if (!d->globalShortcutEnabled) {
00186     changed = true;
00187     if (objectName().isEmpty()) {
00188       kWarning(283) << "Attempt to set global shortcut for action without objectName()."
00189                        " Read the setGlobalShortcut() documentation.";
00190       return;
00191     }
00192     d->globalShortcutEnabled = true;
00193     KGlobalAccel::self()->d->doRegister(this);
00194   }
00195 
00196   if ((type & DefaultShortcut) && d->defaultGlobalShortcut != shortcut) {
00197     d->defaultGlobalShortcut = shortcut;
00198     changed = true;
00199   }
00200 
00201   if ((type & ActiveShortcut) && d->globalShortcut != shortcut) {
00202     d->globalShortcut = shortcut;
00203     changed = true;
00204   }
00205 
00206   //We want to have updateGlobalShortcuts called on a new action in any case so that
00207   //it will be registered properly. In the case of the first setShortcut() call getting an
00208   //empty shortcut parameter this would not happen...
00209   if (changed || d->neverSetGlobalShortcut) {
00210     KGlobalAccel::self()->d->updateGlobalShortcut(this, type | load);
00211     d->neverSetGlobalShortcut = false;
00212   }
00213 }
00214 
00215 bool KAction::globalShortcutAllowed() const
00216 {
00217   return d->globalShortcutEnabled;
00218 }
00219 
00220 bool KAction::isGlobalShortcutEnabled() const
00221 {
00222   return d->globalShortcutEnabled;
00223 }
00224 
00225 void KAction::setGlobalShortcutAllowed( bool allowed, GlobalShortcutLoading /* load */ )
00226 {
00227   if (allowed) {
00228       //### no-op
00229   } else {
00230       forgetGlobalShortcut();
00231   }
00232 }
00233 
00234 void KAction::forgetGlobalShortcut()
00235 {
00236     d->globalShortcut = KShortcut();
00237     d->defaultGlobalShortcut = KShortcut();
00238     if (d->globalShortcutEnabled) {
00239         d->globalShortcutEnabled = false;
00240         d->neverSetGlobalShortcut = true;   //it's a fresh start :)
00241         KGlobalAccel::self()->d->remove(this, KGlobalAccelPrivate::UnRegister);
00242     }
00243 }
00244 
00245 KShapeGesture KAction::shapeGesture( ShortcutTypes type ) const
00246 {
00247   Q_ASSERT(type);
00248   if ( type & DefaultShortcut )
00249     return d->defaultShapeGesture;
00250 
00251   return d->shapeGesture;
00252 }
00253 
00254 KRockerGesture KAction::rockerGesture( ShortcutTypes type ) const
00255 {
00256   Q_ASSERT(type);
00257   if ( type & DefaultShortcut )
00258     return d->defaultRockerGesture;
00259 
00260   return d->rockerGesture;
00261 }
00262 
00263 void KAction::setShapeGesture( const KShapeGesture& gest,  ShortcutTypes type )
00264 {
00265   Q_ASSERT(type);
00266 
00267   if( type & DefaultShortcut )
00268     d->defaultShapeGesture = gest;
00269 
00270   if ( type & ActiveShortcut ) {
00271     if ( KGestureMap::self()->findAction( gest ) ) {
00272       kDebug(283) << "New mouse gesture already in use, won't change gesture.";
00273       return;
00274     }
00275     KGestureMap::self()->removeGesture( d->shapeGesture, this );
00276     KGestureMap::self()->addGesture( gest, this );
00277     d->shapeGesture = gest;
00278   }
00279 }
00280 
00281 void KAction::setRockerGesture( const KRockerGesture& gest,  ShortcutTypes type )
00282 {
00283   Q_ASSERT(type);
00284 
00285   if( type & DefaultShortcut )
00286     d->defaultRockerGesture = gest;
00287 
00288   if ( type & ActiveShortcut ) {
00289     if ( KGestureMap::self()->findAction( gest ) ) {
00290       kDebug(283) << "New mouse gesture already in use, won't change gesture.";
00291       return;
00292     }
00293     KGestureMap::self()->removeGesture( d->rockerGesture, this );
00294     KGestureMap::self()->addGesture( gest, this );
00295     d->rockerGesture = gest;
00296   }
00297 }
00298 
00299 /* vim: et sw=2 ts=2
00300  */
00301 
00302 #include "kaction.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal