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
00054
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
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
00172
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
00207
00208
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 )
00226 {
00227 if (allowed) {
00228
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;
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
00300
00301
00302 #include "kaction.moc"