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
00031 #include <QtGui/QApplication>
00032
00033 #include <kdebug.h>
00034
00035 #include "kguiitem.h"
00036 #include "kicon.h"
00037
00038
00039
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
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
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
00180
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
00215
00216
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 )
00234 {
00235 if (allowed) {
00236
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;
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
00308
00309
00310 #include "kaction.moc"