# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kstdaccel.cpp

    00001 /* This file is part of the KDE libraries
    00002     Copyright (C) 1997 Stefan Taferner (taferner@alpin.or.at)
    00003     Copyright (C) 2000 Nicolas Hadacek (haadcek@kde.org)
    00004     Copyright (C) 2001,2002 Ellis Whitehead (ellis@kde.org)
    00005
    00006     This library is free software; you can redistribute it and/or
    00007     modify it under the terms of the GNU Library General Public
    00008     License version 2 as published by the Free Software Foundation.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public License
    00016     along with this library; see the file COPYING.LIB.  If not, write to
    00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include "kstdaccel.h"
    00022
    00023 #include <kaccelaction.h>
    00024 #include <kaccelbase.h>
    00025 #include <kconfig.h>
    00026 #include <kdebug.h>
    00027 #include <kglobal.h>
    00028 #include <klocale.h>
    00029 #include <kshortcut.h>
    00030 #include <kshortcutlist.h>
    00031
    00032 namespace KStdAccel
    00033 {
    00034
    00035 struct KStdAccelInfo
    00036 {
    00037         StdAccel id;
    00038         const char* psName;
    00039         const char* psDesc;
    00040         int cutDefault, cutDefault4, cutDefault3B, cutDefault4B;
    00041         KShortcut cut;
    00042         bool bInitialized;
    00043 };
    00044
    00045 static KStdAccelInfo g_infoStdAccel[] =
    00046 {
    00047         {AccelNone,            "Group:File", I18N_NOOP("File"), 0, 0, 0, 0, KShortcut(), false },
    00048         { Open,                I18N_NOOP("Open"), 0,     Qt::CTRL+Qt::Key_O, 0, 0, 0, KShortcut(), false },
    00049         { New,                 I18N_NOOP("New"), 0,      Qt::CTRL+Qt::Key_N, 0, 0, 0, KShortcut(), false },
    00050         { Close,               I18N_NOOP("Close"), 0,    Qt::CTRL+Qt::Key_W, Qt::CTRL+Qt::Key_Escape, 0, Qt::CTRL+Qt::Key_W, KShortcut(), false },
    00051         { Save,                I18N_NOOP("Save"), 0,     Qt::CTRL+Qt::Key_S, 0, 0, 0, KShortcut(), false },
    00052         { Print,               I18N_NOOP("Print"), 0,    Qt::CTRL+Qt::Key_P, 0, 0, 0, KShortcut(), false },
    00053         { Quit,                I18N_NOOP("Quit"), 0,     Qt::CTRL+Qt::Key_Q, 0, 0, 0, KShortcut(), false },
    00054         {AccelNone,            "Group:Edit", I18N_NOOP("Edit"), 0, 0, 0, 0, KShortcut(), false },
    00055         { Undo,                I18N_NOOP("Undo"), 0,     Qt::CTRL+Qt::Key_Z, 0, 0, 0, KShortcut(), false },
    00056         { Redo,                I18N_NOOP("Redo"), 0,     Qt::CTRL+Qt::SHIFT+Qt::Key_Z, 0, 0, 0, KShortcut(), false },
    00057         { Cut,                 I18N_NOOP("Cut"), 0,      Qt::CTRL+Qt::Key_X, 0, Qt::SHIFT+Qt::Key_Delete, 0, KShortcut(), false },
    00058         { Copy,                I18N_NOOP("Copy"), 0,     Qt::CTRL+Qt::Key_C, 0, Qt::CTRL+Qt::Key_Insert, 0, KShortcut(), false },
    00059         { Paste,               I18N_NOOP("Paste"), 0,    Qt::CTRL+Qt::Key_V, 0, Qt::SHIFT+Qt::Key_Insert, 0, KShortcut(), false },
    00060         { SelectAll,           "SelectAll", I18N_NOOP("Select All"), Qt::CTRL+Qt::Key_A, 0, 0, 0, KShortcut(), false },
    00061         { Deselect,            I18N_NOOP("Deselect"), 0, Qt::CTRL+Qt::SHIFT+Qt::Key_A, 0, 0, 0, KShortcut(), false },
    00062         { DeleteWordBack,      "DeleteWordBack", I18N_NOOP("Delete Word Backwards"), Qt::CTRL+Qt::Key_Backspace, 0, 0, 0, KShortcut(), false },
    00063         { DeleteWordForward,   "DeleteWordForward", I18N_NOOP("Delete Word Forward"), Qt::CTRL+Qt::Key_Delete, 0,  0, 0, KShortcut(), false },
    00064         { Find,                I18N_NOOP("Find"), 0,     Qt::CTRL+Qt::Key_F, 0, 0, 0, KShortcut(), false },
    00065         { FindNext,            "FindNext", I18N_NOOP("Find Next"), Qt::Key_F3, 0, 0, 0, KShortcut(), false },
    00066         { FindPrev,            "FindPrev", I18N_NOOP("Find Prev"), Qt::SHIFT+Qt::Key_F3, 0, 0, 0, KShortcut(), false },
    00067         { Replace,             I18N_NOOP("Replace"), 0,  Qt::CTRL+Qt::Key_R, 0, 0, 0, KShortcut(), false },
    00068         {AccelNone,            "Group:Navigation", I18N_NOOP("Navigation"), 0, 0, 0, 0, KShortcut(), false },
    00069         { Home,                I18N_NOOP("Home"), 0,     Qt::CTRL+Qt::Key_Home, 0, 0, 0, KShortcut(), false },
    00070         { End,                 I18N_NOOP("End"), 0,      Qt::CTRL+Qt::Key_End, 0, 0, 0, KShortcut(), false },
    00071         { Prior,               I18N_NOOP("Prior"), 0,    Qt::Key_Prior, 0, 0, 0, KShortcut(), false },
    00072         { Next,                I18N_NOOP("Next"), 0,     Qt::Key_Next, 0, 0, 0, KShortcut(), false },
    00073         { GotoLine,            "GotoLine", I18N_NOOP("Go to Line"), Qt::CTRL+Qt::Key_G, 0, 0, 0, KShortcut(), false },
    00074         { AddBookmark,         "AddBookmark", I18N_NOOP("Add Bookmark"), Qt::CTRL+Qt::Key_B, 0, 0, 0, KShortcut(), false },
    00075         { ZoomIn,              "ZoomIn", I18N_NOOP("Zoom In"), Qt::CTRL+Qt::Key_Plus, 0, 0, 0, KShortcut(), false },
    00076         { ZoomOut,             "ZoomOut", I18N_NOOP("Zoom Out"), Qt::CTRL+Qt::Key_Minus, 0, 0, 0, KShortcut(), false },
    00077         { Up,                  I18N_NOOP("Up"), 0,       Qt::ALT+Qt::Key_Up, 0, 0, 0, KShortcut(), false },
    00078         { Back,                I18N_NOOP("Back"), 0,     Qt::ALT+Qt::Key_Left, 0, 0, 0, KShortcut(), false },
    00079         { Forward,             I18N_NOOP("Forward"), 0,  Qt::ALT+Qt::Key_Right, 0, 0, 0, KShortcut(), false },
    00080         { Reload,              I18N_NOOP("Reload"), 0,   Qt::Key_F5, 0, 0, 0, KShortcut(), false },
    00081         { PopupMenuContext,    "PopupMenuContext", I18N_NOOP("Popup Menu Context"), Qt::Key_Menu, 0, 0, 0, KShortcut(), false },
    00082         { ShowMenubar,         "ShowMenubar", I18N_NOOP("Show Menu Bar"), Qt::CTRL+Qt::Key_M, 0, 0, 0, KShortcut(), false },
    00083         {AccelNone,            "Group:Help", I18N_NOOP("Help"), 0, 0, 0, 0, KShortcut(), false },
    00084         { Help,                I18N_NOOP("Help"), 0,     Qt::Key_F1, 0, 0, 0, KShortcut(), false },
    00085         { WhatsThis,           "WhatsThis", I18N_NOOP("What's This"), Qt::SHIFT+Qt::Key_F1, 0, 0, 0, KShortcut(), false },
    00086         {AccelNone,            "Group:TextCompletion", I18N_NOOP("Text Completion"), 0, 0, 0, 0, KShortcut(), false },
    00087         { TextCompletion,      "TextCompletion", I18N_NOOP("Text Completion"), Qt::CTRL+Qt::Key_E, 0, 0, 0, KShortcut(), false },
    00088         { PrevCompletion,      "PrevCompletion", I18N_NOOP("Previous Completion Match"), Qt::CTRL+Qt::Key_Up, 0, 0, 0, KShortcut(), false },
    00089         { NextCompletion,      "NextCompletion", I18N_NOOP("Next Completion Match"), Qt::CTRL+Qt::Key_Down, 0, 0, 0, KShortcut(), false },
    00090         { SubstringCompletion, "SubstringCompletion", I18N_NOOP("Substring Completion"), Qt::CTRL+Qt::Key_T, 0, 0, 0, KShortcut(), false },
    00091         { RotateUp,            "RotateUp", I18N_NOOP("Previous Item in List"), Qt::Key_Up, 0, 0, 0, KShortcut(), false },
    00092         { RotateDown,          "RotateDown", I18N_NOOP("Next Item in List"), Qt::Key_Down, 0, 0, 0, KShortcut(), false },
    00093         { AccelNone,           0, 0, 0, 0, 0, 0, KShortcut(), false }
    00094 };
    00095
    00096 static KStdAccelInfo* infoPtr( StdAccel id )
    00097 {
    00098         if( id != AccelNone ) {
    00099                 for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) {
    00100                         if( g_infoStdAccel[i].id == id )
    00101                                 return &g_infoStdAccel[i];
    00102                 }
    00103         }
    00104         return 0;
    00105 }
    00106
    00107 static void initialize( StdAccel id )
    00108 {
    00109         KConfigGroupSaver saver( KGlobal::config(), "Shortcuts" );
    00110         KStdAccelInfo* pInfo = infoPtr( id );
    00111
    00112         if( !pInfo ) {
    00113                 kdWarning(125) << "KStdAccel: id not found!" << endl; // -- ellis
    00114                 return;
    00115         }
    00116
    00117         if( saver.config()->hasKey( pInfo->psName ) ) {
    00118                 QString s = saver.config()->readEntry( pInfo->psName );
    00119                 if( s != "none" )
    00120                         pInfo->cut.init( s );
    00121                 else
    00122                         pInfo->cut.clear();
    00123         } else
    00124                 pInfo->cut = shortcutDefault( id );
    00125         pInfo->bInitialized = true;
    00126 }
    00127
    00128 QString name( StdAccel id )
    00129 {
    00130         KStdAccelInfo* pInfo = infoPtr( id );
    00131         if( !pInfo )
    00132                 return QString::null;
    00133         return pInfo->psName;
    00134 }
    00135
    00136 QString label( StdAccel id )
    00137 {
    00138         KStdAccelInfo* pInfo = infoPtr( id );
    00139         if( !pInfo )
    00140                 return QString::null;
    00141         return i18n((pInfo->psDesc) ? pInfo->psDesc : pInfo->psName);
    00142 }
    00143
    00144 // TODO: Add psWhatsThis entry to KStdAccelInfo
    00145 QString whatsThis( StdAccel /*id*/ )
    00146 {
    00147 //      KStdAccelInfo* pInfo = infoPtr( id );
    00148 //      if( pInfo && pInfo->psWhatsThis )
    00149 //              return i18n(pInfo->psWhatsThis);
    00150 //      else
    00151                 return QString::null;
    00152 }
    00153
    00154 const KShortcut& shortcut( StdAccel id )
    00155 {
    00156         KStdAccelInfo* pInfo = infoPtr( id );
    00157         if( !pInfo )
    00158                 return KShortcut::null();
    00159
    00160         if( !pInfo->bInitialized )
    00161                 initialize( id );
    00162
    00163         return pInfo->cut;
    00164 }
    00165
    00166 StdAccel findStdAccel( const KKeySequence& seq )
    00167 {
    00168         if( !seq.isNull() ) {
    00169                 for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) {
    00170                         StdAccel id = g_infoStdAccel[i].id;
    00171                         if( id != AccelNone ) {
    00172                                 if( !g_infoStdAccel[i].bInitialized )
    00173                                         initialize( id );
    00174                                 if( g_infoStdAccel[i].cut.contains( seq ) )
    00175                                         return id;
    00176                         }
    00177                 }
    00178         }
    00179         return AccelNone;
    00180 }
    00181
    00182 KShortcut shortcutDefault( StdAccel id )
    00183 {
    00184         return (KAccelAction::useFourModifierKeys())
    00185                 ? shortcutDefault4(id) : shortcutDefault3(id);
    00186 }
    00187
    00188 KShortcut shortcutDefault3( StdAccel id )
    00189 {
    00190         KShortcut cut;
    00191
    00192         KStdAccelInfo* pInfo = infoPtr( id );
    00193         if( pInfo ) {
    00194                 if( pInfo->cutDefault )
    00195                         cut.init( pInfo->cutDefault );
    00196                 // FIXME: if there is no cutDefault, then this we be made the primary
    00197                 //  instead of alternate shortcut.
    00198                 if( pInfo->cutDefault3B )
    00199                         cut.append( QKeySequence(pInfo->cutDefault3B) );
    00200         }
    00201
    00202         return cut;
    00203 }
    00204
    00205 KShortcut shortcutDefault4( StdAccel id )
    00206 {
    00207         KShortcut cut;
    00208
    00209         KStdAccelInfo* pInfo = infoPtr( id );
    00210         if( pInfo ) {
    00211                 KStdAccelInfo& info = *pInfo;
    00212                 KKeySequence key2;
    00213
    00214                 cut.init( (info.cutDefault4) ?
    00215                         QKeySequence(info.cutDefault) : QKeySequence(info.cutDefault4) );
    00216
    00217                 if( info.cutDefault4B )
    00218                         key2.init( QKeySequence(info.cutDefault4B) );
    00219                 else if( info.cutDefault3B )
    00220                         key2.init( QKeySequence(info.cutDefault3B) );
    00221
    00222                 if( key2.count() )
    00223                         cut.append( key2 );
    00224         }
    00225
    00226         return cut;
    00227 }
    00228
    00229 void createAccelActions( KAccelActions& actions )
    00230 {
    00231         actions.clear();
    00232
    00233         for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) {
    00234                 StdAccel id = g_infoStdAccel[i].id;
    00235                 KStdAccelInfo* pInfo = &g_infoStdAccel[i];
    00236
    00237                 if( id != AccelNone ) {
    00238                         actions.insert( pInfo->psName,
    00239                                 i18n((pInfo->psDesc) ? pInfo->psDesc : pInfo->psName),
    00240                                 QString::null, // pInfo->psWhatsThis,
    00241                                 shortcutDefault3(id),
    00242                                 shortcutDefault4(id) );
    00243                 } else
    00244                         actions.insert( pInfo->psName, i18n(pInfo->psDesc) );
    00245         }
    00246 }
    00247
    00248 const KShortcut& open()                  { return shortcut( Open ); }
    00249 const KShortcut& openNew()               { return shortcut( New ); }
    00250 const KShortcut& close()                 { return shortcut( Close ); }
    00251 const KShortcut& save()                  { return shortcut( Save ); }
    00252 const KShortcut& print()                 { return shortcut( Print ); }
    00253 const KShortcut& quit()                  { return shortcut( Quit ); }
    00254 const KShortcut& cut()                   { return shortcut( Cut ); }
    00255 const KShortcut& copy()                  { return shortcut( Copy ); }
    00256 const KShortcut& paste()                 { return shortcut( Paste ); }
    00257 const KShortcut& deleteWordBack()        { return shortcut( DeleteWordBack ); }
    00258 const KShortcut& deleteWordForward()     { return shortcut( DeleteWordForward ); }
    00259 const KShortcut& undo()                  { return shortcut( Undo ); }
    00260 const KShortcut& redo()                  { return shortcut( Redo ); }
    00261 const KShortcut& find()                  { return shortcut( Find ); }
    00262 const KShortcut& findNext()              { return shortcut( FindNext ); }
    00263 const KShortcut& findPrev()              { return shortcut( FindPrev ); }
    00264 const KShortcut& replace()               { return shortcut( Replace ); }
    00265 const KShortcut& home()                  { return shortcut( Home ); }
    00266 const KShortcut& end()                   { return shortcut( End ); }
    00267 const KShortcut& prior()                 { return shortcut( Prior ); }
    00268 const KShortcut& next()                  { return shortcut( Next ); }
    00269 const KShortcut& gotoLine()              { return shortcut( GotoLine ); }
    00270 const KShortcut& addBookmark()           { return shortcut( AddBookmark ); }
    00271 const KShortcut& zoomIn()                { return shortcut( ZoomIn ); }
    00272 const KShortcut& zoomOut()               { return shortcut( ZoomOut ); }
    00273 const KShortcut& help()                  { return shortcut( Help ); }
    00274 const KShortcut& completion()            { return shortcut( TextCompletion ); }
    00275 const KShortcut& prevCompletion()        { return shortcut( PrevCompletion ); }
    00276 const KShortcut& nextCompletion()        { return shortcut( NextCompletion ); }
    00277 const KShortcut& rotateUp()              { return shortcut( RotateUp ); }
    00278 const KShortcut& rotateDown()            { return shortcut( RotateDown ); }
    00279 const KShortcut& substringCompletion()   { return shortcut( SubstringCompletion ); }
    00280 const KShortcut& popupMenuContext()      { return shortcut( PopupMenuContext ); }
    00281 const KShortcut& whatsThis()             { return shortcut( WhatsThis ); }
    00282 const KShortcut& reload()                { return shortcut( Reload ); }
    00283 const KShortcut& selectAll()             { return shortcut( SelectAll ); }
    00284 const KShortcut& up()                    { return shortcut( Up ); }
    00285 const KShortcut& back()                  { return shortcut( Back ); }
    00286 const KShortcut& forward()               { return shortcut( Forward ); }
    00287 const KShortcut& showMenubar()           { return shortcut( ShowMenubar ); }
    00288
    00289 //---------------------------------------------------------------------
    00290 // ShortcutList
    00291 //---------------------------------------------------------------------
    00292
    00293 ShortcutList::ShortcutList()
    00294         { }
    00295
    00296 ShortcutList::~ShortcutList()
    00297         { }
    00298
    00299 uint ShortcutList::count() const
    00300 {
    00301         static uint g_nAccels = 0;
    00302         if( g_nAccels == 0 ) {
    00303                 for( ; g_infoStdAccel[g_nAccels].psName != 0; g_nAccels++ )
    00304                         ;
    00305         }
    00306         return g_nAccels;
    00307 }
    00308
    00309 QString ShortcutList::name( uint i ) const
    00310         { return g_infoStdAccel[i].psName; }
    00311
    00312 QString ShortcutList::label( uint i ) const
    00313         { return i18n((g_infoStdAccel[i].psDesc) ? g_infoStdAccel[i].psDesc : g_infoStdAccel[i].psName); }
    00314
    00315 QString ShortcutList::whatsThis( uint ) const
    00316         { return QString::null; }
    00317
    00318 const KShortcut& ShortcutList::shortcut( uint i ) const
    00319 {
    00320         if( !g_infoStdAccel[i].bInitialized )
    00321                 initialize( g_infoStdAccel[i].id );
    00322         return g_infoStdAccel[i].cut;
    00323 }
    00324
    00325 const KShortcut& ShortcutList::shortcutDefault( uint i ) const
    00326 {
    00327         static KShortcut cut;
    00328         cut = KStdAccel::shortcutDefault( g_infoStdAccel[i].id );
    00329         return cut;
    00330 }
    00331
    00332 bool ShortcutList::isConfigurable( uint i ) const
    00333         { return (g_infoStdAccel[i].id != AccelNone); }
    00334
    00335 bool ShortcutList::setShortcut( uint i, const KShortcut& cut )
    00336         { g_infoStdAccel[i].cut = cut; return true; }
    00337
    00338 QVariant ShortcutList::getOther( Other, uint ) const
    00339         { return QVariant(); }
    00340
    00341 bool ShortcutList::setOther( Other, uint, QVariant )
    00342         { return false; }
    00343
    00344 bool ShortcutList::save() const
    00345 {
    00346         return writeSettings( QString::null, 0, false, true );
    00347 }
    00348
    00349 #ifndef KDE_NO_COMPAT
    00350 QString action(StdAccel id)
    00351         { return name(id); }
    00352 QString description(StdAccel id)
    00353         { return label(id); }
    00354 int key(StdAccel id)
    00355         { return shortcut(id).keyCodeQt(); }
    00356 int defaultKey(StdAccel id)
    00357         { return shortcutDefault(id).keyCodeQt(); }
    00358
    00359 bool isEqual(const QKeyEvent* ev, int keyQt)
    00360 {
    00361         KKey key1( ev ), key2( keyQt );
    00362         return key1 == key2;
    00363 }
    00364 #endif // !KDE_NO_COMPAT
    00365
    00366 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:57 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001