KDEUI
kaction.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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,
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
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
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
00203
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
00238
00239
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 )
00257 {
00258 if (allowed) {
00259
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;
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
00331
00332
00333 #include "kaction.moc"