| # kdecore Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | 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 #define __KSTDACCEL_CPP_ 1
    00021
    00022 #include "kstdaccel.h"
    00023
    00024 #include "kaccelaction.h"
    00025 #include "kaccelbase.h"
    00026 #include <kconfig.h>
    00027 #include <kdebug.h>
    00028 #include <kglobal.h>
    00029 #include <klocale.h>
    00030 #include <kshortcut.h>
    00031 #include <kshortcutlist.h>
    00032
    00033 namespace KStdAccel
    00034 {
    00035
    00036 struct KStdAccelInfo
    00037 {
    00038     StdAccel id;
    00039     const char* psName;
    00040     const char* psDesc;
    00041     int cutDefault, cutDefault4, cutDefault3B, cutDefault4B;
    00042     KShortcut cut;
    00043     bool bInitialized;
    00044 };
    00045
    00046 static KStdAccelInfo g_infoStdAccel[] =
    00047 {
    00048     {AccelNone,            "Group:File", I18N_NOOP("File"), 0, 0, 0, 0, KShortcut(), false },
    00049     { Open,                I18N_NOOP("Open"), 0,     Qt::CTRL+Qt::Key_O, 0, 0, 0, KShortcut(), false },
    00050     { New,                 I18N_NOOP("New"), 0,      Qt::CTRL+Qt::Key_N, 0, 0, 0, KShortcut(), false },
    00051     { Close,               I18N_NOOP("Close"), 0,    Qt::CTRL+Qt::Key_W, Qt::CTRL+Qt::Key_Escape, 0, Qt::CTRL+Qt::Key_W, KShortcut(), false },
    00052     { Save,                I18N_NOOP("Save"), 0,     Qt::CTRL+Qt::Key_S, 0, 0, 0, KShortcut(), false },
    00053     { Print,               I18N_NOOP("Print"), 0,    Qt::CTRL+Qt::Key_P, 0, 0, 0, KShortcut(), false },
    00054     { Quit,                I18N_NOOP("Quit"), 0,     Qt::CTRL+Qt::Key_Q, 0, 0, 0, KShortcut(), false },
    00055     {AccelNone,            "Group:Edit", I18N_NOOP("Edit"), 0, 0, 0, 0, KShortcut(), false },
    00056     { Undo,                I18N_NOOP("Undo"), 0,     Qt::CTRL+Qt::Key_Z, 0, 0, 0, KShortcut(), false },
    00057     { Redo,                I18N_NOOP("Redo"), 0,     Qt::CTRL+Qt::SHIFT+Qt::Key_Z, 0, 0, 0, KShortcut(), false },
    00058     { Cut,                 I18N_NOOP("Cut"), 0,      Qt::CTRL+Qt::Key_X, 0, Qt::SHIFT+Qt::Key_Delete, 0, KShortcut(), false },
    00059     { Copy,                I18N_NOOP("Copy"), 0,     Qt::CTRL+Qt::Key_C, 0, Qt::CTRL+Qt::Key_Insert, 0, KShortcut(), false },
    00060     { Paste,               I18N_NOOP("Paste"), 0,    Qt::CTRL+Qt::Key_V, 0, Qt::SHIFT+Qt::Key_Insert, 0, KShortcut(), false },
    00061     { PasteSelection,      I18N_NOOP("Paste Selection"), 0, 0, 0, Qt::CTRL+Qt::SHIFT+Qt::Key_Insert, 0, KShortcut(), false },
    00062     { SelectAll,           "SelectAll", I18N_NOOP("Select All"), Qt::CTRL+Qt::Key_A, 0, 0, 0, KShortcut(), false },
    00063     { Deselect,            I18N_NOOP("Deselect"), 0, Qt::CTRL+Qt::SHIFT+Qt::Key_A, 0, 0, 0, KShortcut(), false },
    00064     { DeleteWordBack,      "DeleteWordBack", I18N_NOOP("Delete Word Backwards"), Qt::CTRL+Qt::Key_Backspace, 0, 0, 0, KShortcut(), false },
    00065     { DeleteWordForward,   "DeleteWordForward", I18N_NOOP("Delete Word Forward"), Qt::CTRL+Qt::Key_Delete, 0,  0, 0, KShortcut(), false },
    00066     { Find,                I18N_NOOP("Find"), 0,     Qt::CTRL+Qt::Key_F, 0, 0, 0, KShortcut(), false },
    00067     { FindNext,            "FindNext", I18N_NOOP("Find Next"), Qt::Key_F3, 0, 0, 0, KShortcut(), false },
    00068     { FindPrev,            "FindPrev", I18N_NOOP("Find Prev"), Qt::SHIFT+Qt::Key_F3, 0, 0, 0, KShortcut(), false },
    00069     { Replace,             I18N_NOOP("Replace"), 0,  Qt::CTRL+Qt::Key_R, 0, 0, 0, KShortcut(), false },
    00070     {AccelNone,            "Group:Navigation", I18N_NOOP("Navigation"), 0, 0, 0, 0, KShortcut(), false },
    00071     { Home,                I18N_NOOP2("Opposite to End","Home"), 0,     Qt::CTRL+Qt::Key_Home, 0, Qt::Key_HomePage, 0, KShortcut(), false },
    00072     { End,                 I18N_NOOP("End"), 0,      Qt::CTRL+Qt::Key_End, 0, 0, 0, KShortcut(), false },
    00073     { BeginningOfLine,     "BeginningOfLine", I18N_NOOP("Beginning of Line"), Qt::Key_Home, 0, 0, 0, KShortcut(), false},
    00074     { EndOfLine,           "EndOfLine", I18N_NOOP("End of Line"), Qt::Key_End, 0, 0, 0, KShortcut(), false},
    00075     { Prior,               I18N_NOOP("Prior"), 0,    Qt::Key_Prior, 0, 0, 0, KShortcut(), false },
    00076     { Next,                I18N_NOOP2("Opposite to Prior","Next"), 0,     Qt::Key_Next, 0, 0, 0, KShortcut(), false },
    00077     { GotoLine,            "GotoLine", I18N_NOOP("Go to Line"), Qt::CTRL+Qt::Key_G, 0, 0, 0, KShortcut(), false },
    00078     { AddBookmark,         "AddBookmark", I18N_NOOP("Add Bookmark"), Qt::CTRL+Qt::Key_B, 0, 0, 0, KShortcut(), false },
    00079     { ZoomIn,              "ZoomIn", I18N_NOOP("Zoom In"), Qt::CTRL+Qt::Key_Plus, 0, 0, 0, KShortcut(), false },
    00080     { ZoomOut,             "ZoomOut", I18N_NOOP("Zoom Out"), Qt::CTRL+Qt::Key_Minus, 0, 0, 0, KShortcut(), false },
    00081     { Up,                  I18N_NOOP("Up"), 0,       Qt::ALT+Qt::Key_Up, 0, 0, 0, KShortcut(), false },
    00082     { Back,                I18N_NOOP("Back"), 0,     Qt::ALT+Qt::Key_Left, 0, Qt::Key_Back, 0, KShortcut(), false },
    00083     { Forward,             I18N_NOOP("Forward"), 0,  Qt::ALT+Qt::Key_Right, 0, Qt::Key_Forward, 0, KShortcut(), false },
    00084     { Reload,              I18N_NOOP("Reload"), 0,   Qt::Key_F5, 0, Qt::Key_Refresh, 0, KShortcut(), false },
    00085     { PopupMenuContext,    "PopupMenuContext", I18N_NOOP("Popup Menu Context"), Qt::Key_Menu, 0, 0, 0, KShortcut(), false },
    00086     { ShowMenubar,         "ShowMenubar", I18N_NOOP("Show Menu Bar"), Qt::CTRL+Qt::Key_M, 0, 0, 0, KShortcut(), false },
    00087     { BackwardWord,        "BackwardWord", I18N_NOOP("Backward Word"), Qt::CTRL+Qt::Key_Left, 0, 0, 0, KShortcut(), false },
    00088     { ForwardWord,         "ForwardWord", I18N_NOOP("Forward Word"), Qt::CTRL+Qt::Key_Right, 0, 0, 0, KShortcut(), false },
    00089     { TabNext,             I18N_NOOP("Activate Next Tab"), 0,  Qt::CTRL+Qt::Key_Period, 0, Qt::CTRL+Qt::Key_BracketRight, 0, KShortcut(), false },
    00090     { TabPrev,             I18N_NOOP("Activate Previous Tab"), 0,   Qt::CTRL+Qt::Key_Comma, 0, Qt::CTRL+Qt::Key_BracketLeft, 0, KShortcut(), false },
    00091     { FullScreen,          "FullScreen", I18N_NOOP("Full Screen Mode"), Qt::CTRL+Qt::SHIFT+Qt::Key_F, 0, 0, 0, KShortcut(), false },
    00092     {AccelNone,            "Group:Help", I18N_NOOP("Help"), 0, 0, 0, 0, KShortcut(), false },
    00093     { Help,                I18N_NOOP("Help"), 0,     Qt::Key_F1, 0, 0, 0, KShortcut(), false },
    00094     { WhatsThis,           "WhatsThis", I18N_NOOP("What's This"), Qt::SHIFT+Qt::Key_F1, 0, 0, 0, KShortcut(), false },
    00095     {AccelNone,            "Group:TextCompletion", I18N_NOOP("Text Completion"), 0, 0, 0, 0, KShortcut(), false },
    00096     { TextCompletion,      "TextCompletion", I18N_NOOP("Text Completion"), Qt::CTRL+Qt::Key_E, 0, 0, 0, KShortcut(), false },
    00097     { PrevCompletion,      "PrevCompletion", I18N_NOOP("Previous Completion Match"), Qt::CTRL+Qt::Key_Up, 0, 0, 0, KShortcut(), false },
    00098     { NextCompletion,      "NextCompletion", I18N_NOOP("Next Completion Match"), Qt::CTRL+Qt::Key_Down, 0, 0, 0, KShortcut(), false },
    00099     { SubstringCompletion, "SubstringCompletion", I18N_NOOP("Substring Completion"), Qt::CTRL+Qt::Key_T, 0, 0, 0, KShortcut(), false },
    00100     { RotateUp,            "RotateUp", I18N_NOOP("Previous Item in List"), Qt::Key_Up, 0, 0, 0, KShortcut(), false },
    00101     { RotateDown,          "RotateDown", I18N_NOOP("Next Item in List"), Qt::Key_Down, 0, 0, 0, KShortcut(), false },
    00102     { AccelNone,           0, 0, 0, 0, 0, 0, KShortcut(), false }
    00103 };
    00104
    00105 static KStdAccelInfo* infoPtr( StdAccel id )
    00106 {
    00107     if( id != AccelNone ) {
    00108         // Linear search. Changing the data structure doesn't seem possible
    00109         // (since we need groups for the config stuff), but maybe a little
    00110         // additional hashtable wouldn't hurt.
    00111         for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) {
    00112             if( g_infoStdAccel[i].id == id )
    00113                 return &g_infoStdAccel[i];
    00114         }
    00115     }
    00116     return 0;
    00117 }
    00118
    00119 static void initialize( StdAccel id )
    00120 {
    00121     KConfigGroupSaver saver( KGlobal::config(), "Shortcuts" );
    00122     KStdAccelInfo* pInfo = infoPtr( id );
    00123
    00124     if( !pInfo ) {
    00125         kdWarning(125) << "KStdAccel: id not found!" << endl; // -- ellis
    00126         return;
    00127     }
    00128
    00129     if( saver.config()->hasKey( pInfo->psName ) ) {
    00130         QString s = saver.config()->readEntry( pInfo->psName );
    00131         if( s != "none" )
    00132             pInfo->cut.init( s );
    00133         else
    00134             pInfo->cut.clear();
    00135     } else
    00136         pInfo->cut = shortcutDefault( id );
    00137     pInfo->bInitialized = true;
    00138 }
    00139
    00140 QString name( StdAccel id )
    00141 {
    00142     KStdAccelInfo* pInfo = infoPtr( id );
    00143     if( !pInfo )
    00144         return QString::null;
    00145     return pInfo->psName;
    00146 }
    00147
    00148 QString label( StdAccel id )
    00149 {
    00150     KStdAccelInfo* pInfo = infoPtr( id );
    00151     if( !pInfo )
    00152         return QString::null;
    00153     return i18n((pInfo->psDesc) ? pInfo->psDesc : pInfo->psName);
    00154 }
    00155
    00156 // TODO: Add psWhatsThis entry to KStdAccelInfo
    00157 QString whatsThis( StdAccel /*id*/ )
    00158 {
    00159 //  KStdAccelInfo* pInfo = infoPtr( id );
    00160 //  if( pInfo && pInfo->psWhatsThis )
    00161 //      return i18n(pInfo->psWhatsThis);
    00162 //  else
    00163         return QString::null;
    00164 }
    00165
    00166 const KShortcut& shortcut( StdAccel id )
    00167 {
    00168     KStdAccelInfo* pInfo = infoPtr( id );
    00169     if( !pInfo )
    00170         return KShortcut::null();
    00171
    00172     if( !pInfo->bInitialized )
    00173         initialize( id );
    00174
    00175     return pInfo->cut;
    00176 }
    00177
    00178 StdAccel findStdAccel( const KKeySequence& seq )
    00179 {
    00180     if( !seq.isNull() ) {
    00181         for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) {
    00182             StdAccel id = g_infoStdAccel[i].id;
    00183             if( id != AccelNone ) {
    00184                 if( !g_infoStdAccel[i].bInitialized )
    00185                     initialize( id );
    00186                 if( g_infoStdAccel[i].cut.contains( seq ) )
    00187                     return id;
    00188             }
    00189         }
    00190     }
    00191     return AccelNone;
    00192 }
    00193
    00194 KShortcut shortcutDefault( StdAccel id )
    00195 {
    00196     return (KAccelAction::useFourModifierKeys())
    00197         ? shortcutDefault4(id) : shortcutDefault3(id);
    00198 }
    00199
    00200 KShortcut shortcutDefault3( StdAccel id )
    00201 {
    00202     KShortcut cut;
    00203
    00204     KStdAccelInfo* pInfo = infoPtr( id );
    00205     if( pInfo ) {
    00206         if( pInfo->cutDefault )
    00207             cut.init( pInfo->cutDefault );
    00208         // FIXME: if there is no cutDefault, then this we be made the primary
    00209         //  instead of alternate shortcut.
    00210         if( pInfo->cutDefault3B )
    00211             cut.append( KKey(pInfo->cutDefault3B) );
    00212     }
    00213
    00214     return cut;
    00215 }
    00216
    00217 KShortcut shortcutDefault4( StdAccel id )
    00218 {
    00219     KShortcut cut;
    00220
    00221     KStdAccelInfo* pInfo = infoPtr( id );
    00222     if( pInfo ) {
    00223         KStdAccelInfo& info = *pInfo;
    00224         KKeySequence key2;
    00225
    00226         cut.init( (info.cutDefault4) ?
    00227             QKeySequence(info.cutDefault) : QKeySequence(info.cutDefault4) );
    00228
    00229         if( info.cutDefault4B )
    00230             key2.init( QKeySequence(info.cutDefault4B) );
    00231         else if( info.cutDefault3B )
    00232             key2.init( QKeySequence(info.cutDefault3B) );
    00233
    00234         if( key2.count() )
    00235             cut.append( key2 );
    00236     }
    00237
    00238     return cut;
    00239 }
    00240
    00241 #if 0 // unused
    00242 void createAccelActions( KAccelActions& actions )
    00243 {
    00244     actions.clear();
    00245
    00246     for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) {
    00247         StdAccel id = g_infoStdAccel[i].id;
    00248         KStdAccelInfo* pInfo = &g_infoStdAccel[i];
    00249
    00250         if( id != AccelNone ) {
    00251             actions.insert( pInfo->psName,
    00252                 i18n((pInfo->psDesc) ? pInfo->psDesc : pInfo->psName),
    00253                 QString::null, // pInfo->psWhatsThis,
    00254                 shortcutDefault3(id),
    00255                 shortcutDefault4(id) );
    00256         } else
    00257             actions.insert( pInfo->psName, i18n(pInfo->psDesc) );
    00258     }
    00259 }
    00260 #endif
    00261
    00262 const KShortcut& open()                  { return shortcut( Open ); }
    00263 const KShortcut& openNew()               { return shortcut( New ); }
    00264 const KShortcut& close()                 { return shortcut( Close ); }
    00265 const KShortcut& save()                  { return shortcut( Save ); }
    00266 const KShortcut& print()                 { return shortcut( Print ); }
    00267 const KShortcut& quit()                  { return shortcut( Quit ); }
    00268 const KShortcut& cut()                   { return shortcut( Cut ); }
    00269 const KShortcut& copy()                  { return shortcut( Copy ); }
    00270 const KShortcut& paste()                 { return shortcut( Paste ); }
    00271 const KShortcut& pasteSelection()        { return shortcut( PasteSelection ); }
    00272 const KShortcut& deleteWordBack()        { return shortcut( DeleteWordBack ); }
    00273 const KShortcut& deleteWordForward()     { return shortcut( DeleteWordForward ); }
    00274 const KShortcut& undo()                  { return shortcut( Undo ); }
    00275 const KShortcut& redo()                  { return shortcut( Redo ); }
    00276 const KShortcut& find()                  { return shortcut( Find ); }
    00277 const KShortcut& findNext()              { return shortcut( FindNext ); }
    00278 const KShortcut& findPrev()              { return shortcut( FindPrev ); }
    00279 const KShortcut& replace()               { return shortcut( Replace ); }
    00280 const KShortcut& home()                  { return shortcut( Home ); }
    00281 const KShortcut& end()                   { return shortcut( End ); }
    00282 const KShortcut& beginningOfLine()       { return shortcut( BeginningOfLine ); }
    00283 const KShortcut& endOfLine()             { return shortcut( EndOfLine ); }
    00284 const KShortcut& prior()                 { return shortcut( Prior ); }
    00285 const KShortcut& next()                  { return shortcut( Next ); }
    00286 const KShortcut& backwardWord()          { return shortcut( BackwardWord ); }
    00287 const KShortcut& forwardWord()           { return shortcut( ForwardWord ); }
    00288 const KShortcut& gotoLine()              { return shortcut( GotoLine ); }
    00289 const KShortcut& addBookmark()           { return shortcut( AddBookmark ); }
    00290 const KShortcut& tabNext()               { return shortcut( TabNext ); }
    00291 const KShortcut& tabPrev()               { return shortcut( TabPrev ); }
    00292 const KShortcut& fullScreen()            { return shortcut( FullScreen ); }
    00293 const KShortcut& zoomIn()                { return shortcut( ZoomIn ); }
    00294 const KShortcut& zoomOut()               { return shortcut( ZoomOut ); }
    00295 const KShortcut& help()                  { return shortcut( Help ); }
    00296 const KShortcut& completion()            { return shortcut( TextCompletion ); }
    00297 const KShortcut& prevCompletion()        { return shortcut( PrevCompletion ); }
    00298 const KShortcut& nextCompletion()        { return shortcut( NextCompletion ); }
    00299 const KShortcut& rotateUp()              { return shortcut( RotateUp ); }
    00300 const KShortcut& rotateDown()            { return shortcut( RotateDown ); }
    00301 const KShortcut& substringCompletion()   { return shortcut( SubstringCompletion ); }
    00302 const KShortcut& popupMenuContext()      { return shortcut( PopupMenuContext ); }
    00303 const KShortcut& whatsThis()             { return shortcut( WhatsThis ); }
    00304 const KShortcut& reload()                { return shortcut( Reload ); }
    00305 const KShortcut& selectAll()             { return shortcut( SelectAll ); }
    00306 const KShortcut& up()                    { return shortcut( Up ); }
    00307 const KShortcut& back()                  { return shortcut( Back ); }
    00308 const KShortcut& forward()               { return shortcut( Forward ); }
    00309 const KShortcut& showMenubar()           { return shortcut( ShowMenubar ); }
    00310
    00311 //---------------------------------------------------------------------
    00312 // ShortcutList
    00313 //---------------------------------------------------------------------
    00314
    00315 ShortcutList::ShortcutList()
    00316     { }
    00317
    00318 ShortcutList::~ShortcutList()
    00319     { }
    00320
    00321 uint ShortcutList::count() const
    00322 {
    00323     static uint g_nAccels = 0;
    00324     if( g_nAccels == 0 ) {
    00325         for( ; g_infoStdAccel[g_nAccels].psName != 0; g_nAccels++ )
    00326             ;
    00327     }
    00328     return g_nAccels;
    00329 }
    00330
    00331 QString ShortcutList::name( uint i ) const
    00332     { return g_infoStdAccel[i].psName; }
    00333
    00334 QString ShortcutList::label( uint i ) const
    00335     { return i18n((g_infoStdAccel[i].psDesc) ? g_infoStdAccel[i].psDesc : g_infoStdAccel[i].psName); }
    00336
    00337 QString ShortcutList::whatsThis( uint ) const
    00338     { return QString::null; }
    00339
    00340 const KShortcut& ShortcutList::shortcut( uint i ) const
    00341 {
    00342     if( !g_infoStdAccel[i].bInitialized )
    00343         initialize( g_infoStdAccel[i].id );
    00344     return g_infoStdAccel[i].cut;
    00345 }
    00346
    00347 const KShortcut& ShortcutList::shortcutDefault( uint i ) const
    00348 {
    00349     static KShortcut cut;
    00350     cut = KStdAccel::shortcutDefault( g_infoStdAccel[i].id );
    00351     return cut;
    00352 }
    00353
    00354 bool ShortcutList::isConfigurable( uint i ) const
    00355     { return (g_infoStdAccel[i].id != AccelNone); }
    00356
    00357 bool ShortcutList::setShortcut( uint i, const KShortcut& cut )
    00358     { g_infoStdAccel[i].cut = cut; return true; }
    00359
    00360 QVariant ShortcutList::getOther( Other, uint ) const
    00361     { return QVariant(); }
    00362
    00363 bool ShortcutList::setOther( Other, uint, QVariant )
    00364     { return false; }
    00365
    00366 bool ShortcutList::save() const
    00367 {
    00368     return writeSettings( QString::null, 0, false, true );
    00369 }
    00370
    00371 KDECORE_EXPORT QString action(StdAccel id)
    00372     { return name(id); }
    00373 KDECORE_EXPORT QString description(StdAccel id)
    00374     { return label(id); }
    00375 KDECORE_EXPORT int key(StdAccel id)
    00376     { return shortcut(id).keyCodeQt(); }
    00377 KDECORE_EXPORT int defaultKey(StdAccel id)
    00378     { return shortcutDefault(id).keyCodeQt(); }
    00379
    00380 KDECORE_EXPORT bool isEqual(const QKeyEvent* ev, int keyQt)
    00381 {
    00382     KKey key1( ev ), key2( keyQt );
    00383     return key1 == key2;
    00384 }
    00385
    00386 }
    00387
    00388 #undef __KSTDACCEL_CPP_

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:11 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003