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