• Skip to content
  • Skip to link menu
KDE 4.2 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 void KActionPrivate::setActiveGlobalShortcutNoEnable(const KShortcut &cut)
00054 {
00055     globalShortcut = cut;
00056     emit q->globalShortcutChanged(cut.primary());
00057 }
00058 
00059 
00060 void KActionPrivate::slotTriggered()
00061 {
00062 #ifdef KDE3_SUPPORT
00063   emit q->activated();
00064 #endif
00065   emit q->triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
00066 }
00067 
00068 
00069 //---------------------------------------------------------------------
00070 // KAction
00071 //---------------------------------------------------------------------
00072 
00073 KAction::KAction(QObject *parent)
00074   : QWidgetAction(parent), d(new KActionPrivate)
00075 {
00076   d->init(this);
00077 }
00078 
00079 KAction::KAction(const QString &text, QObject *parent)
00080   : QWidgetAction(parent), d(new KActionPrivate)
00081 {
00082   d->init(this);
00083   setText(text);
00084 }
00085 
00086 KAction::KAction(const KIcon &icon, const QString &text, QObject *parent)
00087   : QWidgetAction(parent), d(new KActionPrivate)
00088 {
00089   d->init(this);
00090   setIcon(icon);
00091   setText(text);
00092 }
00093 
00094 KAction::~KAction()
00095 {
00096     if (d->globalShortcutEnabled) {
00097         // - remove the action from KGlobalAccel
00098         d->globalShortcutEnabled = false;
00099         KGlobalAccel::self()->d->remove(this, KGlobalAccelPrivate::SetInactive);
00100     }
00101 
00102     KGestureMap::self()->removeGesture(d->shapeGesture, this);
00103     KGestureMap::self()->removeGesture(d->rockerGesture, this);
00104     delete d;
00105 }
00106 
00107 bool KAction::isShortcutConfigurable() const
00108 {
00109     return property("isShortcutConfigurable").toBool();
00110 }
00111 
00112 void KAction::setShortcutConfigurable( bool b )
00113 {
00114     setProperty("isShortcutConfigurable", b);
00115 }
00116 
00117 KShortcut KAction::shortcut(ShortcutTypes type) const
00118 {
00119   Q_ASSERT(type);
00120 
00121   if (type == DefaultShortcut) {
00122       QKeySequence primary = property("defaultPrimaryShortcut").value<QKeySequence>();
00123       QKeySequence secondary = property("defaultAlternateShortcut").value<QKeySequence>();
00124       return KShortcut(primary, secondary);
00125   }
00126 
00127   QKeySequence primary = shortcuts().value(0);
00128   QKeySequence secondary = shortcuts().value(1);
00129   return KShortcut(primary, secondary);
00130 }
00131 
00132 void KAction::setShortcut( const KShortcut & shortcut, ShortcutTypes type )
00133 {
00134   Q_ASSERT(type);
00135 
00136   if (type & DefaultShortcut) {
00137       setProperty("defaultPrimaryShortcut", shortcut.primary());
00138       setProperty("defaultAlternateShortcut", shortcut.alternate());
00139   }
00140 
00141   if (type & ActiveShortcut) {
00142       QAction::setShortcuts(shortcut);
00143   }
00144 }
00145 
00146 void KAction::setShortcut( const QKeySequence & keySeq, ShortcutTypes type )
00147 {
00148   Q_ASSERT(type);
00149 
00150   if (type & DefaultShortcut)
00151       setProperty("defaultPrimaryShortcut", keySeq);
00152 
00153   if (type & ActiveShortcut) {
00154       QAction::setShortcut(keySeq);
00155   }
00156 }
00157 
00158 void KAction::setShortcuts(const QList<QKeySequence>& shortcuts, ShortcutTypes type)
00159 {
00160   setShortcut(KShortcut(shortcuts), type);
00161 }
00162 
00163 const KShortcut & KAction::globalShortcut(ShortcutTypes type) const
00164 {
00165   Q_ASSERT(type);
00166 
00167   if (type == DefaultShortcut)
00168     return d->defaultGlobalShortcut;
00169 
00170   return d->globalShortcut;
00171 }
00172 
00173 void KAction::setGlobalShortcut( const KShortcut & shortcut, ShortcutTypes type,
00174                                  GlobalShortcutLoading load )
00175 {
00176   Q_ASSERT(type);
00177   bool changed = false;
00178   
00179   // protect against garbage keycode -1 that Qt sometimes produces for exotic keys;
00180   // at the moment (~mid 2008) Multimedia PlayPause is one of those keys.
00181   int shortcutKeys[8];
00182   for (int i = 0; i < 4; i++) {
00183     shortcutKeys[i] = shortcut.primary()[i];
00184     shortcutKeys[i + 4] = shortcut.alternate()[i];
00185   }
00186   for (int i = 0; i < 8; i++) {
00187     if (shortcutKeys[i] == -1) {
00188       kWarning(283) << "Encountered garbage keycode (keycode = -1) in input, not doing anything.";
00189       return;
00190     }
00191   }
00192 
00193   if (!d->globalShortcutEnabled) {
00194     changed = true;
00195     if (objectName().isEmpty()) {
00196       kWarning(283) << "Attempt to set global shortcut for action without objectName()."
00197                        " Read the setGlobalShortcut() documentation.";
00198       return;
00199     }
00200     d->globalShortcutEnabled = true;
00201     KGlobalAccel::self()->d->doRegister(this);
00202   }
00203 
00204   if ((type & DefaultShortcut) && d->defaultGlobalShortcut != shortcut) {
00205     d->defaultGlobalShortcut = shortcut;
00206     changed = true;
00207   }
00208 
00209   if ((type & ActiveShortcut) && d->globalShortcut != shortcut) {
00210     d->globalShortcut = shortcut;
00211     changed = true;
00212   }
00213 
00214   //We want to have updateGlobalShortcuts called on a new action in any case so that
00215   //it will be registered properly. In the case of the first setShortcut() call getting an
00216   //empty shortcut parameter this would not happen...
00217   if (changed || d->neverSetGlobalShortcut) {
00218     KGlobalAccel::self()->d->updateGlobalShortcut(this, type | load);
00219     d->neverSetGlobalShortcut = false;
00220   }
00221 }
00222 
00223 bool KAction::globalShortcutAllowed() const
00224 {
00225   return d->globalShortcutEnabled;
00226 }
00227 
00228 bool KAction::isGlobalShortcutEnabled() const
00229 {
00230   return d->globalShortcutEnabled;
00231 }
00232 
00233 void KAction::setGlobalShortcutAllowed( bool allowed, GlobalShortcutLoading /* load */ )
00234 {
00235   if (allowed) {
00236       //### no-op
00237   } else {
00238       forgetGlobalShortcut();
00239   }
00240 }
00241 
00242 void KAction::forgetGlobalShortcut()
00243 {
00244     d->globalShortcut = KShortcut();
00245     d->defaultGlobalShortcut = KShortcut();
00246     if (d->globalShortcutEnabled) {
00247         d->globalShortcutEnabled = false;
00248         d->neverSetGlobalShortcut = true;   //it's a fresh start :)
00249         KGlobalAccel::self()->d->remove(this, KGlobalAccelPrivate::UnRegister);
00250     }
00251 }
00252 
00253 KShapeGesture KAction::shapeGesture( ShortcutTypes type ) const
00254 {
00255   Q_ASSERT(type);
00256   if ( type & DefaultShortcut )
00257     return d->defaultShapeGesture;
00258 
00259   return d->shapeGesture;
00260 }
00261 
00262 KRockerGesture KAction::rockerGesture( ShortcutTypes type ) const
00263 {
00264   Q_ASSERT(type);
00265   if ( type & DefaultShortcut )
00266     return d->defaultRockerGesture;
00267 
00268   return d->rockerGesture;
00269 }
00270 
00271 void KAction::setShapeGesture( const KShapeGesture& gest,  ShortcutTypes type )
00272 {
00273   Q_ASSERT(type);
00274 
00275   if( type & DefaultShortcut )
00276     d->defaultShapeGesture = gest;
00277 
00278   if ( type & ActiveShortcut ) {
00279     if ( KGestureMap::self()->findAction( gest ) ) {
00280       kDebug(283) << "New mouse gesture already in use, won't change gesture.";
00281       return;
00282     }
00283     KGestureMap::self()->removeGesture( d->shapeGesture, this );
00284     KGestureMap::self()->addGesture( gest, this );
00285     d->shapeGesture = gest;
00286   }
00287 }
00288 
00289 void KAction::setRockerGesture( const KRockerGesture& gest,  ShortcutTypes type )
00290 {
00291   Q_ASSERT(type);
00292 
00293   if( type & DefaultShortcut )
00294     d->defaultRockerGesture = gest;
00295 
00296   if ( type & ActiveShortcut ) {
00297     if ( KGestureMap::self()->findAction( gest ) ) {
00298       kDebug(283) << "New mouse gesture already in use, won't change gesture.";
00299       return;
00300     }
00301     KGestureMap::self()->removeGesture( d->rockerGesture, this );
00302     KGestureMap::self()->addGesture( gest, this );
00303     d->rockerGesture = gest;
00304   }
00305 }
00306 
00307 /* vim: et sw=2 ts=2
00308  */
00309 
00310 #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