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