00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kstandardshortcut.h"
00022
00023 #include "kconfig.h"
00024 #include "kdebug.h"
00025 #include "kglobal.h"
00026 #include "klocale.h"
00027 #include "kshortcut.h"
00028 #include <kconfiggroup.h>
00029
00030 #include <QtGui/QKeySequence>
00031 #ifdef Q_WS_X11
00032 #include <qx11info_x11.h>
00033 #endif
00034
00035 namespace KStandardShortcut
00036 {
00037
00038 struct KStandardShortcutInfo
00039 {
00041 StandardShortcut id;
00042
00048 const char* name;
00049
00051 const char* translation_context;
00052
00054 const char* description;
00055
00057 int cutDefault, cutDefault2;
00058
00060 KShortcut cut;
00061
00063 bool isInitialized;
00064 };
00065
00067 #undef I18N_NOOP2
00068 #define I18N_NOOP2(comment,x) comment, x
00069
00070 #define CTRL(x) Qt::CTRL+Qt::Key_##x
00071 #define SHIFT(x) Qt::SHIFT+Qt::Key_##x
00072 #define CTRLSHIFT(x) Qt::CTRL+Qt::SHIFT+Qt::Key_##x
00073 #define ALT(x) Qt::ALT+Qt::Key_##x
00074
00079
00080
00081 static KStandardShortcutInfo g_infoStandardShortcut[] =
00082 {
00083
00084 {AccelNone, 0 , 0 , 0 , 0 , 0 , KShortcut(), false },
00085 { Open , "Open" , I18N_NOOP2("@action", "Open") , CTRL(O), 0 , KShortcut(), false } ,
00086 { New , "New" , I18N_NOOP2("@action", "New") , CTRL(N), 0 , KShortcut(), false } ,
00087 { Close , "Close", I18N_NOOP2("@action", "Close"), CTRL(W), CTRL(Escape), KShortcut(), false } ,
00088 { Save , "Save" , I18N_NOOP2("@action", "Save") , CTRL(S), 0 , KShortcut(), false } ,
00089 { Print , "Print", I18N_NOOP2("@action", "Print"), CTRL(P), 0 , KShortcut(), false } ,
00090 { Quit , "Quit" , I18N_NOOP2("@action", "Quit") , CTRL(Q), 0 , KShortcut(), false } ,
00091
00092
00093 { Undo , "Undo" , I18N_NOOP2("@action", "Undo") , CTRL(Z) , 0 , KShortcut(), false },
00094 { Redo , "Redo" , I18N_NOOP2("@action", "Redo") , CTRLSHIFT(Z) , 0 , KShortcut(), false },
00095 { Cut , "Cut" , I18N_NOOP2("@action", "Cut") , CTRL(X) , SHIFT(Delete), KShortcut(), false },
00096 { Copy , "Copy" , I18N_NOOP2("@action", "Copy") , CTRL(C) , CTRL(Insert) , KShortcut(), false },
00097 { Paste , "Paste" , I18N_NOOP2("@action", "Paste") , CTRL(V) , SHIFT(Insert), KShortcut(), false },
00098 { PasteSelection , "Paste Selection" , I18N_NOOP2("@action", "Paste Selection") , CTRLSHIFT(Insert), 0 , KShortcut(), false },
00099
00100 { SelectAll , "SelectAll" , I18N_NOOP2("@action", "Select All") , CTRL(A) , 0 , KShortcut(), false },
00101 { Deselect , "Deselect" , I18N_NOOP2("@action", "Deselect") , CTRLSHIFT(A) , 0 , KShortcut(), false },
00102 { DeleteWordBack , "DeleteWordBack" , I18N_NOOP2("@action", "Delete Word Backwards"), CTRL(Backspace) , 0 , KShortcut(), false },
00103 { DeleteWordForward, "DeleteWordForward", I18N_NOOP2("@action", "Delete Word Forward") , CTRL(Delete) , 0 , KShortcut(), false },
00104
00105 { Find , "Find" , I18N_NOOP2("@action", "Find") , CTRL(F) , 0 , KShortcut(), false },
00106 { FindNext , "FindNext" , I18N_NOOP2("@action", "Find Next") , Qt::Key_F3 , 0 , KShortcut(), false },
00107 { FindPrev , "FindPrev" , I18N_NOOP2("@action", "Find Prev") , SHIFT(F3) , 0 , KShortcut(), false },
00108 { Replace , "Replace" , I18N_NOOP2("@action", "Replace") , CTRL(R) , 0 , KShortcut(), false },
00109
00110
00111 { Home , "Home" , I18N_NOOP2("@action Go to main page" , "Home") , ALT(Home) , Qt::Key_HomePage , KShortcut(), false },
00112 { Begin , "Begin" , I18N_NOOP2("@action Beginning of document", "Begin") , CTRL(Home) , 0 , KShortcut(), false },
00113 { End , "End" , I18N_NOOP2("@action End of document" , "End") , CTRL(End) , 0 , KShortcut(), false },
00114 { Prior , "Prior" , I18N_NOOP2("@action" , "Prior") , Qt::Key_PageUp , 0 , KShortcut(), false },
00115 { Next , "Next" , I18N_NOOP2("@action Opposite to Prior" , "Next") , Qt::Key_PageDown, 0 , KShortcut(), false },
00116
00117 { Up , "Up" , I18N_NOOP2("@action" , "Up") , ALT(Up) , 0 , KShortcut(), false },
00118 { Back , "Back" , I18N_NOOP2("@action" , "Back") , ALT(Left) , Qt::Key_Back , KShortcut(), false },
00119 { Forward , "Forward" , I18N_NOOP2("@action" , "Forward") , ALT(Right) , Qt::Key_Forward , KShortcut(), false },
00120 { Reload , "Reload" , I18N_NOOP2("@action" , "Reload") , Qt::Key_F5 , Qt::Key_Refresh , KShortcut(), false },
00121
00122 { BeginningOfLine, "BeginningOfLine" , I18N_NOOP2("@action" , "Beginning of Line") , Qt::Key_Home , 0 , KShortcut(), false },
00123 { EndOfLine , "EndOfLine" , I18N_NOOP2("@action" , "End of Line") , Qt::Key_End , 0 , KShortcut(), false },
00124 { GotoLine , "GotoLine" , I18N_NOOP2("@action" , "Go to Line") , CTRL(G) , 0 , KShortcut(), false },
00125 { BackwardWord , "BackwardWord" , I18N_NOOP2("@action" , "Backward Word") , CTRL(Left) , 0 , KShortcut(), false },
00126 { ForwardWord , "ForwardWord" , I18N_NOOP2("@action" , "Forward Word") , CTRL(Right) , 0 , KShortcut(), false },
00127
00128 { AddBookmark , "AddBookmark" , I18N_NOOP2("@action" , "Add Bookmark") , CTRL(B) , 0 , KShortcut(), false },
00129 { ZoomIn , "ZoomIn" , I18N_NOOP2("@action" , "Zoom In") , CTRL(Plus) , CTRL(Equal) , KShortcut(), false },
00130 { ZoomOut , "ZoomOut" , I18N_NOOP2("@action" , "Zoom Out") , CTRL(Minus) , 0 , KShortcut(), false },
00131 { FullScreen , "FullScreen" , I18N_NOOP2("@action" , "Full Screen Mode") , CTRLSHIFT(F) , 0 , KShortcut(), false },
00132
00133 { ShowMenubar , "ShowMenubar" , I18N_NOOP2("@action" , "Show Menu Bar") , CTRL(M) , 0 , KShortcut(), false },
00134 { TabNext , "Activate Next Tab" , I18N_NOOP2("@action" , "Activate Next Tab") , CTRL(Period) , CTRL(BracketRight), KShortcut(), false },
00135 { TabPrev , "Activate Previous Tab", I18N_NOOP2("@action" , "Activate Previous Tab"), CTRL(Comma) , CTRL(BracketLeft) , KShortcut(), false },
00136
00137
00138 { Help , "Help" , I18N_NOOP2("@action" , "Help") , Qt::Key_F1 , 0 , KShortcut(), false },
00139 { WhatsThis , "WhatsThis" , I18N_NOOP2("@action" , "What's This") , SHIFT(F1) , 0 , KShortcut(), false },
00140
00141
00142 { TextCompletion , "TextCompletion" , I18N_NOOP2("@action", "Text Completion") , CTRL(E) , 0, KShortcut(), false },
00143 { PrevCompletion , "PrevCompletion" , I18N_NOOP2("@action", "Previous Completion Match"), CTRL(Up) , 0, KShortcut(), false },
00144 { NextCompletion , "NextCompletion" , I18N_NOOP2("@action", "Next Completion Match") , CTRL(Down) , 0, KShortcut(), false },
00145 { SubstringCompletion , "SubstringCompletion" , I18N_NOOP2("@action", "Substring Completion") , CTRL(T) , 0, KShortcut(), false },
00146
00147 { RotateUp , "RotateUp" , I18N_NOOP2("@action", "Previous Item in List") , Qt::Key_Up , 0, KShortcut(), false },
00148 { RotateDown , "RotateDown" , I18N_NOOP2("@action", "Next Item in List") , Qt::Key_Down, 0, KShortcut(), false },
00149
00150 { OpenRecent , "OpenRecent" , I18N_NOOP2("@action", "Open Recent") , 0 , 0, KShortcut(), false },
00151 { SaveAs , "SaveAs" , I18N_NOOP2("@action", "Save As") , 0 , 0, KShortcut(), false },
00152 { Revert , "Revert" , I18N_NOOP2("@action", "Revert") , 0 , 0, KShortcut(), false },
00153 { PrintPreview , "PrintPreview" , I18N_NOOP2("@action", "Print Preview") , 0 , 0, KShortcut(), false },
00154 { Mail , "Mail" , I18N_NOOP2("@action", "Mail") , 0 , 0, KShortcut(), false },
00155 { Clear , "Clear" , I18N_NOOP2("@action", "Clear") , 0 , 0, KShortcut(), false },
00156 { ActualSize , "ActualSize" , I18N_NOOP2("@action", "Actual Size") , 0 , 0, KShortcut(), false },
00157 { FitToPage , "FitToPage" , I18N_NOOP2("@action", "Fit To Page") , 0 , 0, KShortcut(), false },
00158 { FitToWidth , "FitToWidth" , I18N_NOOP2("@action", "Fit To Width") , 0 , 0, KShortcut(), false },
00159 { FitToHeight , "FitToHeight" , I18N_NOOP2("@action", "Fit To Height") , 0 , 0, KShortcut(), false },
00160 { Zoom , "Zoom" , I18N_NOOP2("@action", "Zoom") , 0 , 0, KShortcut(), false },
00161 { Goto , "Goto" , I18N_NOOP2("@action", "Goto") , 0 , 0, KShortcut(), false },
00162 { GotoPage , "GotoPage" , I18N_NOOP2("@action", "Goto Page") , 0 , 0, KShortcut(), false },
00163 { DocumentBack , "DocumentBack" , I18N_NOOP2("@action", "Document Back") , 0 , 0, KShortcut(), false },
00164 { DocumentForward , "DocumentForward" , I18N_NOOP2("@action", "Document Forward") , 0 , 0, KShortcut(), false },
00165 { EditBookmarks , "EditBookmarks" , I18N_NOOP2("@action", "Edit Bookmarks") , 0 , 0, KShortcut(), false },
00166 { Spelling , "Spelling" , I18N_NOOP2("@action", "Spelling") , 0 , 0, KShortcut(), false },
00167 { ShowToolbar , "ShowToolbar" , I18N_NOOP2("@action", "Show Toolbar") , 0 , 0, KShortcut(), false },
00168 { ShowStatusbar , "ShowStatusbar" , I18N_NOOP2("@action", "Show Statusbar") , 0 , 0, KShortcut(), false },
00169 { SaveOptions , "SaveOptions" , I18N_NOOP2("@action", "Save Options") , 0 , 0, KShortcut(), false },
00170 { KeyBindings , "KeyBindings" , I18N_NOOP2("@action", "Key Bindings") , 0 , 0, KShortcut(), false },
00171 { Preferences , "Preferences" , I18N_NOOP2("@action", "Preferences") , 0 , 0, KShortcut(), false },
00172 { ConfigureToolbars , "ConfigureToolbars" , I18N_NOOP2("@action", "Configure Toolbars") , 0 , 0, KShortcut(), false },
00173 { ConfigureNotifications , "ConfigureNotifications" , I18N_NOOP2("@action", "Configure Notifications") , 0 , 0, KShortcut(), false },
00174 { TipofDay , "TipofDay" , I18N_NOOP2("@action", "Tip Of Day") , 0 , 0, KShortcut(), false },
00175 { ReportBug , "ReportBug" , I18N_NOOP2("@action", "Report Bug") , 0 , 0, KShortcut(), false },
00176 { SwitchApplicationLanguage, "SwitchApplicationLanguage", I18N_NOOP2("@action", "Switch Application Language"), 0 , 0, KShortcut(), false },
00177 { AboutApp , "AboutApp" , I18N_NOOP2("@action", "About Application") , 0 , 0, KShortcut(), false },
00178 { AboutKDE , "AboutKDE" , I18N_NOOP2("@action", "About KDE") , 0 , 0, KShortcut(), false },
00179
00180
00181 { AccelNone , 0 , 0 , 0 , 0, 0, KShortcut(), false }
00182 };
00183
00184
00188 static KStandardShortcutInfo *guardedStandardShortcutInfo(StandardShortcut id)
00189 {
00190 if (id >= static_cast<int>(sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo)) ||
00191 id < 0) {
00192 kWarning(125) << "KStandardShortcut: id not found!";
00193 return &g_infoStandardShortcut[AccelNone];
00194 } else
00195 return &g_infoStandardShortcut[id];
00196 }
00197
00203 static void initialize(StandardShortcut id)
00204 {
00205 KStandardShortcutInfo *info = guardedStandardShortcutInfo(id);
00206
00207
00208 if (info->id!=AccelNone) {
00209 Q_ASSERT(info->description);
00210 Q_ASSERT(info->translation_context);
00211 Q_ASSERT(info->name);
00212 }
00213
00214 KConfigGroup cg(KGlobal::config(), "Shortcuts");
00215
00216 #ifdef Q_WS_X11
00217
00218 if(QX11Info::display() && cg.hasKey(info->name))
00219 #else
00220 if(cg.hasKey(info->name))
00221 #endif
00222 {
00223 QString s = cg.readEntry(info->name);
00224 if (s != "none")
00225 info->cut = KShortcut(s);
00226 else
00227 info->cut = KShortcut();
00228 } else {
00229 info->cut = hardcodedDefaultShortcut(id);
00230 }
00231
00232 info->isInitialized = true;
00233 }
00234
00235 void saveShortcut(StandardShortcut id, const KShortcut &newShortcut)
00236 {
00237 KStandardShortcutInfo *info = guardedStandardShortcutInfo(id);
00238
00239
00240 if(info->id == AccelNone)
00241 return;
00242
00243 KConfigGroup cg(KGlobal::config(), "Shortcuts");
00244
00245 info->cut = newShortcut;
00246 bool sameAsDefault = (newShortcut == hardcodedDefaultShortcut(id));
00247
00248 if (sameAsDefault) {
00249
00250
00251 if(cg.hasKey(info->name))
00252 cg.deleteEntry(info->name, KConfig::Global|KConfig::Persistent);
00253
00254 return;
00255 }
00256
00257
00258 cg.writeEntry(info->name, info->cut.toString(), KConfig::Global|KConfig::Persistent);
00259 }
00260
00261 QString name(StandardShortcut id)
00262 {
00263 return guardedStandardShortcutInfo(id)->name;
00264 }
00265
00266 QString label(StandardShortcut id)
00267 {
00268 KStandardShortcutInfo *info = guardedStandardShortcutInfo( id );
00269 return i18nc(
00270 info->translation_context,
00271 info->description);
00272 }
00273
00274
00275 QString whatsThis( StandardShortcut )
00276 {
00277
00278
00279
00280
00281 return QString();
00282 }
00283
00284 const KShortcut &shortcut(StandardShortcut id)
00285 {
00286 KStandardShortcutInfo *info = guardedStandardShortcutInfo(id);
00287
00288 if(!info->isInitialized)
00289 initialize(id);
00290
00291 return info->cut;
00292 }
00293
00294 StandardShortcut find(const QKeySequence &seq)
00295 {
00296 if( !seq.isEmpty() ) {
00297 for(uint i = 0; i < sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo); i++) {
00298 StandardShortcut id = g_infoStandardShortcut[i].id;
00299 if( id != AccelNone ) {
00300 if(!g_infoStandardShortcut[i].isInitialized)
00301 initialize(id);
00302 if(g_infoStandardShortcut[i].cut.contains(seq))
00303 return id;
00304 }
00305 }
00306 }
00307 return AccelNone;
00308 }
00309
00310 StandardShortcut find(const char *keyName)
00311 {
00312 for(uint i = 0; i < sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo); i++)
00313 if (qstrcmp(g_infoStandardShortcut[i].name, keyName))
00314 return g_infoStandardShortcut[i].id;
00315
00316 return AccelNone;
00317 }
00318
00319 KShortcut hardcodedDefaultShortcut(StandardShortcut id)
00320 {
00321 KShortcut cut;
00322 KStandardShortcutInfo *info = guardedStandardShortcutInfo(id);
00323
00324 return KShortcut(info->cutDefault, info->cutDefault2);
00325 }
00326
00327 const KShortcut& open() { return shortcut( Open ); }
00328 const KShortcut& openNew() { return shortcut( New ); }
00329 const KShortcut& close() { return shortcut( Close ); }
00330 const KShortcut& save() { return shortcut( Save ); }
00331 const KShortcut& print() { return shortcut( Print ); }
00332 const KShortcut& quit() { return shortcut( Quit ); }
00333 const KShortcut& cut() { return shortcut( Cut ); }
00334 const KShortcut& copy() { return shortcut( Copy ); }
00335 const KShortcut& paste() { return shortcut( Paste ); }
00336 const KShortcut& pasteSelection() { return shortcut( PasteSelection ); }
00337 const KShortcut& deleteWordBack() { return shortcut( DeleteWordBack ); }
00338 const KShortcut& deleteWordForward() { return shortcut( DeleteWordForward ); }
00339 const KShortcut& undo() { return shortcut( Undo ); }
00340 const KShortcut& redo() { return shortcut( Redo ); }
00341 const KShortcut& find() { return shortcut( Find ); }
00342 const KShortcut& findNext() { return shortcut( FindNext ); }
00343 const KShortcut& findPrev() { return shortcut( FindPrev ); }
00344 const KShortcut& replace() { return shortcut( Replace ); }
00345 const KShortcut& home() { return shortcut( Home ); }
00346 const KShortcut& begin() { return shortcut( Begin ); }
00347 const KShortcut& end() { return shortcut( End ); }
00348 const KShortcut& beginningOfLine() { return shortcut( BeginningOfLine ); }
00349 const KShortcut& endOfLine() { return shortcut( EndOfLine ); }
00350 const KShortcut& prior() { return shortcut( Prior ); }
00351 const KShortcut& next() { return shortcut( Next ); }
00352 const KShortcut& backwardWord() { return shortcut( BackwardWord ); }
00353 const KShortcut& forwardWord() { return shortcut( ForwardWord ); }
00354 const KShortcut& gotoLine() { return shortcut( GotoLine ); }
00355 const KShortcut& addBookmark() { return shortcut( AddBookmark ); }
00356 const KShortcut& tabNext() { return shortcut( TabNext ); }
00357 const KShortcut& tabPrev() { return shortcut( TabPrev ); }
00358 const KShortcut& fullScreen() { return shortcut( FullScreen ); }
00359 const KShortcut& zoomIn() { return shortcut( ZoomIn ); }
00360 const KShortcut& zoomOut() { return shortcut( ZoomOut ); }
00361 const KShortcut& help() { return shortcut( Help ); }
00362 const KShortcut& completion() { return shortcut( TextCompletion ); }
00363 const KShortcut& prevCompletion() { return shortcut( PrevCompletion ); }
00364 const KShortcut& nextCompletion() { return shortcut( NextCompletion ); }
00365 const KShortcut& rotateUp() { return shortcut( RotateUp ); }
00366 const KShortcut& rotateDown() { return shortcut( RotateDown ); }
00367 const KShortcut& substringCompletion() { return shortcut( SubstringCompletion ); }
00368 const KShortcut& whatsThis() { return shortcut( WhatsThis ); }
00369 const KShortcut& reload() { return shortcut( Reload ); }
00370 const KShortcut& selectAll() { return shortcut( SelectAll ); }
00371 const KShortcut& up() { return shortcut( Up ); }
00372 const KShortcut& back() { return shortcut( Back ); }
00373 const KShortcut& forward() { return shortcut( Forward ); }
00374 const KShortcut& showMenubar() { return shortcut( ShowMenubar ); }
00375
00376 }