KDEUI
kstandardshortcut.cpp
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 #define ALTSHIFT(x) Qt::ALT+Qt::SHIFT+Qt::Key_##x 00075 00080 // STUFF WILL BREAK IF YOU DON'T READ THIS!!! 00081 // Read the comments of the big enum in kstandardshortcut.h before you change anything! 00082 static KStandardShortcutInfo g_infoStandardShortcut[] = 00083 { 00084 //Group File, 00085 {AccelNone, 0 , 0 , 0 , 0 , 0 , KShortcut(), false }, 00086 { Open , "Open" , I18N_NOOP2("@action", "Open") , CTRL(O), 0 , KShortcut(), false } , 00087 { New , "New" , I18N_NOOP2("@action", "New") , CTRL(N), 0 , KShortcut(), false } , 00088 { Close , "Close", I18N_NOOP2("@action", "Close"), CTRL(W), CTRL(Escape), KShortcut(), false } , 00089 { Save , "Save" , I18N_NOOP2("@action", "Save") , CTRL(S), 0 , KShortcut(), false } , 00090 { Print , "Print", I18N_NOOP2("@action", "Print"), CTRL(P), 0 , KShortcut(), false } , 00091 { Quit , "Quit" , I18N_NOOP2("@action", "Quit") , CTRL(Q), 0 , KShortcut(), false } , 00092 00093 //Group Edit 00094 { Undo , "Undo" , I18N_NOOP2("@action", "Undo") , CTRL(Z) , 0 , KShortcut(), false }, 00095 { Redo , "Redo" , I18N_NOOP2("@action", "Redo") , CTRLSHIFT(Z) , 0 , KShortcut(), false }, 00096 { Cut , "Cut" , I18N_NOOP2("@action", "Cut") , CTRL(X) , SHIFT(Delete), KShortcut(), false }, 00097 { Copy , "Copy" , I18N_NOOP2("@action", "Copy") , CTRL(C) , CTRL(Insert) , KShortcut(), false }, 00098 { Paste , "Paste" , I18N_NOOP2("@action", "Paste") , CTRL(V) , SHIFT(Insert), KShortcut(), false }, 00099 { PasteSelection , "Paste Selection" , I18N_NOOP2("@action", "Paste Selection") , CTRLSHIFT(Insert), 0 , KShortcut(), false }, 00100 00101 { SelectAll , "SelectAll" , I18N_NOOP2("@action", "Select All") , CTRL(A) , 0 , KShortcut(), false }, 00102 { Deselect , "Deselect" , I18N_NOOP2("@action", "Deselect") , CTRLSHIFT(A) , 0 , KShortcut(), false }, 00103 { DeleteWordBack , "DeleteWordBack" , I18N_NOOP2("@action", "Delete Word Backwards"), CTRL(Backspace) , 0 , KShortcut(), false }, 00104 { DeleteWordForward, "DeleteWordForward", I18N_NOOP2("@action", "Delete Word Forward") , CTRL(Delete) , 0 , KShortcut(), false }, 00105 00106 { Find , "Find" , I18N_NOOP2("@action", "Find") , CTRL(F) , 0 , KShortcut(), false }, 00107 { FindNext , "FindNext" , I18N_NOOP2("@action", "Find Next") , Qt::Key_F3 , 0 , KShortcut(), false }, 00108 { FindPrev , "FindPrev" , I18N_NOOP2("@action", "Find Prev") , SHIFT(F3) , 0 , KShortcut(), false }, 00109 { Replace , "Replace" , I18N_NOOP2("@action", "Replace") , CTRL(R) , 0 , KShortcut(), false }, 00110 00111 //Group Navigation 00112 { Home , "Home" , I18N_NOOP2("@action Go to main page" , "Home") , ALT(Home) , Qt::Key_HomePage , KShortcut(), false }, 00113 { Begin , "Begin" , I18N_NOOP2("@action Beginning of document", "Begin") , CTRL(Home) , 0 , KShortcut(), false }, 00114 { End , "End" , I18N_NOOP2("@action End of document" , "End") , CTRL(End) , 0 , KShortcut(), false }, 00115 { Prior , "Prior" , I18N_NOOP2("@action" , "Prior") , Qt::Key_PageUp , 0 , KShortcut(), false }, 00116 { Next , "Next" , I18N_NOOP2("@action Opposite to Prior" , "Next") , Qt::Key_PageDown, 0 , KShortcut(), false }, 00117 00118 { Up , "Up" , I18N_NOOP2("@action" , "Up") , ALT(Up) , 0 , KShortcut(), false }, 00119 { Back , "Back" , I18N_NOOP2("@action" , "Back") , ALT(Left) , Qt::Key_Back , KShortcut(), false }, 00120 { Forward , "Forward" , I18N_NOOP2("@action" , "Forward") , ALT(Right) , Qt::Key_Forward , KShortcut(), false }, 00121 { Reload , "Reload" , I18N_NOOP2("@action" , "Reload") , Qt::Key_F5 , Qt::Key_Refresh , KShortcut(), false }, 00122 00123 { BeginningOfLine, "BeginningOfLine" , I18N_NOOP2("@action" , "Beginning of Line") , Qt::Key_Home , 0 , KShortcut(), false }, 00124 { EndOfLine , "EndOfLine" , I18N_NOOP2("@action" , "End of Line") , Qt::Key_End , 0 , KShortcut(), false }, 00125 { GotoLine , "GotoLine" , I18N_NOOP2("@action" , "Go to Line") , CTRL(G) , 0 , KShortcut(), false }, 00126 { BackwardWord , "BackwardWord" , I18N_NOOP2("@action" , "Backward Word") , CTRL(Left) , 0 , KShortcut(), false }, 00127 { ForwardWord , "ForwardWord" , I18N_NOOP2("@action" , "Forward Word") , CTRL(Right) , 0 , KShortcut(), false }, 00128 00129 { AddBookmark , "AddBookmark" , I18N_NOOP2("@action" , "Add Bookmark") , CTRL(B) , 0 , KShortcut(), false }, 00130 { ZoomIn , "ZoomIn" , I18N_NOOP2("@action" , "Zoom In") , CTRL(Plus) , CTRL(Equal) , KShortcut(), false }, 00131 { ZoomOut , "ZoomOut" , I18N_NOOP2("@action" , "Zoom Out") , CTRL(Minus) , 0 , KShortcut(), false }, 00132 { FullScreen , "FullScreen" , I18N_NOOP2("@action" , "Full Screen Mode") , CTRLSHIFT(F) , 0 , KShortcut(), false }, 00133 00134 { ShowMenubar , "ShowMenubar" , I18N_NOOP2("@action" , "Show Menu Bar") , CTRL(M) , 0 , KShortcut(), false }, 00135 { TabNext , "Activate Next Tab" , I18N_NOOP2("@action" , "Activate Next Tab") , CTRL(Period) , CTRL(BracketRight), KShortcut(), false }, 00136 { TabPrev , "Activate Previous Tab", I18N_NOOP2("@action" , "Activate Previous Tab"), CTRL(Comma) , CTRL(BracketLeft) , KShortcut(), false }, 00137 00138 //Group Help 00139 { Help , "Help" , I18N_NOOP2("@action" , "Help") , Qt::Key_F1 , 0 , KShortcut(), false }, 00140 { WhatsThis , "WhatsThis" , I18N_NOOP2("@action" , "What's This") , SHIFT(F1) , 0 , KShortcut(), false }, 00141 00142 //Group TextCompletion 00143 { TextCompletion , "TextCompletion" , I18N_NOOP2("@action", "Text Completion") , CTRL(E) , 0, KShortcut(), false }, 00144 { PrevCompletion , "PrevCompletion" , I18N_NOOP2("@action", "Previous Completion Match"), CTRL(Up) , 0, KShortcut(), false }, 00145 { NextCompletion , "NextCompletion" , I18N_NOOP2("@action", "Next Completion Match") , CTRL(Down) , 0, KShortcut(), false }, 00146 { SubstringCompletion , "SubstringCompletion" , I18N_NOOP2("@action", "Substring Completion") , CTRL(T) , 0, KShortcut(), false }, 00147 00148 { RotateUp , "RotateUp" , I18N_NOOP2("@action", "Previous Item in List") , Qt::Key_Up , 0, KShortcut(), false }, 00149 { RotateDown , "RotateDown" , I18N_NOOP2("@action", "Next Item in List") , Qt::Key_Down, 0, KShortcut(), false }, 00150 00151 { OpenRecent , "OpenRecent" , I18N_NOOP2("@action", "Open Recent") , 0 , 0, KShortcut(), false }, 00152 { SaveAs , "SaveAs" , I18N_NOOP2("@action", "Save As") , 0 , 0, KShortcut(), false }, 00153 { Revert , "Revert" , I18N_NOOP2("@action", "Revert") , 0 , 0, KShortcut(), false }, 00154 { PrintPreview , "PrintPreview" , I18N_NOOP2("@action", "Print Preview") , 0 , 0, KShortcut(), false }, 00155 { Mail , "Mail" , I18N_NOOP2("@action", "Mail") , 0 , 0, KShortcut(), false }, 00156 { Clear , "Clear" , I18N_NOOP2("@action", "Clear") , 0 , 0, KShortcut(), false }, 00157 { ActualSize , "ActualSize" , I18N_NOOP2("@action", "Actual Size") , 0 , 0, KShortcut(), false }, 00158 { FitToPage , "FitToPage" , I18N_NOOP2("@action", "Fit To Page") , 0 , 0, KShortcut(), false }, 00159 { FitToWidth , "FitToWidth" , I18N_NOOP2("@action", "Fit To Width") , 0 , 0, KShortcut(), false }, 00160 { FitToHeight , "FitToHeight" , I18N_NOOP2("@action", "Fit To Height") , 0 , 0, KShortcut(), false }, 00161 { Zoom , "Zoom" , I18N_NOOP2("@action", "Zoom") , 0 , 0, KShortcut(), false }, 00162 { Goto , "Goto" , I18N_NOOP2("@action", "Goto") , 0 , 0, KShortcut(), false }, 00163 { GotoPage , "GotoPage" , I18N_NOOP2("@action", "Goto Page") , 0 , 0, KShortcut(), false }, 00164 { DocumentBack , "DocumentBack" , I18N_NOOP2("@action", "Document Back") , ALTSHIFT(Left), 0, KShortcut(), false }, 00165 { DocumentForward , "DocumentForward" , I18N_NOOP2("@action", "Document Forward") , ALTSHIFT(Right), 0, KShortcut(), false }, 00166 { EditBookmarks , "EditBookmarks" , I18N_NOOP2("@action", "Edit Bookmarks") , 0 , 0, KShortcut(), false }, 00167 { Spelling , "Spelling" , I18N_NOOP2("@action", "Spelling") , 0 , 0, KShortcut(), false }, 00168 { ShowToolbar , "ShowToolbar" , I18N_NOOP2("@action", "Show Toolbar") , 0 , 0, KShortcut(), false }, 00169 { ShowStatusbar , "ShowStatusbar" , I18N_NOOP2("@action", "Show Statusbar") , 0 , 0, KShortcut(), false }, 00170 { SaveOptions , "SaveOptions" , I18N_NOOP2("@action", "Save Options") , 0 , 0, KShortcut(), false }, 00171 { KeyBindings , "KeyBindings" , I18N_NOOP2("@action", "Key Bindings") , 0 , 0, KShortcut(), false }, 00172 { Preferences , "Preferences" , I18N_NOOP2("@action", "Preferences") , 0 , 0, KShortcut(), false }, 00173 { ConfigureToolbars , "ConfigureToolbars" , I18N_NOOP2("@action", "Configure Toolbars") , 0 , 0, KShortcut(), false }, 00174 { ConfigureNotifications , "ConfigureNotifications" , I18N_NOOP2("@action", "Configure Notifications") , 0 , 0, KShortcut(), false }, 00175 { TipofDay , "TipofDay" , I18N_NOOP2("@action", "Tip Of Day") , 0 , 0, KShortcut(), false }, 00176 { ReportBug , "ReportBug" , I18N_NOOP2("@action", "Report Bug") , 0 , 0, KShortcut(), false }, 00177 { SwitchApplicationLanguage, "SwitchApplicationLanguage", I18N_NOOP2("@action", "Switch Application Language"), 0 , 0, KShortcut(), false }, 00178 { AboutApp , "AboutApp" , I18N_NOOP2("@action", "About Application") , 0 , 0, KShortcut(), false }, 00179 { AboutKDE , "AboutKDE" , I18N_NOOP2("@action", "About KDE") , 0 , 0, KShortcut(), false }, 00180 00181 //dummy entry to catch simple off-by-one errors. Insert new entries before this line. 00182 { AccelNone , 0 , 0 , 0 , 0, 0, KShortcut(), false } 00183 }; 00184 00185 00189 static KStandardShortcutInfo *guardedStandardShortcutInfo(StandardShortcut id) 00190 { 00191 if (id >= static_cast<int>(sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo)) || 00192 id < 0) { 00193 kWarning(125) << "KStandardShortcut: id not found!"; 00194 return &g_infoStandardShortcut[AccelNone]; 00195 } else 00196 return &g_infoStandardShortcut[id]; 00197 } 00198 00204 static void initialize(StandardShortcut id) 00205 { 00206 KStandardShortcutInfo *info = guardedStandardShortcutInfo(id); 00207 00208 // All three are needed. 00209 if (info->id!=AccelNone) { 00210 Q_ASSERT(info->description); 00211 Q_ASSERT(info->translation_context); 00212 Q_ASSERT(info->name); 00213 } 00214 00215 KConfigGroup cg(KGlobal::config(), "Shortcuts"); 00216 00217 #ifdef Q_WS_X11 00218 // Code within this block breaks if we aren't running in GUI mode. 00219 if(QX11Info::display() && cg.hasKey(info->name)) 00220 #else 00221 if(cg.hasKey(info->name)) 00222 #endif 00223 { 00224 QString s = cg.readEntry(info->name); 00225 if (s != "none") 00226 info->cut = KShortcut(s); 00227 else 00228 info->cut = KShortcut(); 00229 } else { 00230 info->cut = hardcodedDefaultShortcut(id); 00231 } 00232 00233 info->isInitialized = true; 00234 } 00235 00236 void saveShortcut(StandardShortcut id, const KShortcut &newShortcut) 00237 { 00238 KStandardShortcutInfo *info = guardedStandardShortcutInfo(id); 00239 // If the action has no standard shortcut associated there is nothing to 00240 // save 00241 if(info->id == AccelNone) 00242 return; 00243 00244 KConfigGroup cg(KGlobal::config(), "Shortcuts"); 00245 00246 info->cut = newShortcut; 00247 bool sameAsDefault = (newShortcut == hardcodedDefaultShortcut(id)); 00248 00249 if (sameAsDefault) { 00250 // If the shortcut is the equal to the hardcoded one we remove it from 00251 // kdeglobal if necessary and return. 00252 if(cg.hasKey(info->name)) 00253 cg.deleteEntry(info->name, KConfig::Global|KConfig::Persistent); 00254 00255 return; 00256 } 00257 00258 // Write the changed shortcut to kdeglobals 00259 cg.writeEntry(info->name, info->cut.toString(), KConfig::Global|KConfig::Persistent); 00260 } 00261 00262 QString name(StandardShortcut id) 00263 { 00264 return guardedStandardShortcutInfo(id)->name; 00265 } 00266 00267 QString label(StandardShortcut id) 00268 { 00269 KStandardShortcutInfo *info = guardedStandardShortcutInfo( id ); 00270 return i18nc( 00271 info->translation_context, 00272 info->description); 00273 } 00274 00275 // TODO: Add psWhatsThis entry to KStandardShortcutInfo 00276 QString whatsThis( StandardShortcut /*id*/ ) 00277 { 00278 // KStandardShortcutInfo* info = guardedStandardShortcutInfo( id ); 00279 // if( info && info->whatsThis ) 00280 // return i18n(info->whatsThis); 00281 // else 00282 return QString(); 00283 } 00284 00285 const KShortcut &shortcut(StandardShortcut id) 00286 { 00287 KStandardShortcutInfo *info = guardedStandardShortcutInfo(id); 00288 00289 if(!info->isInitialized) 00290 initialize(id); 00291 00292 return info->cut; 00293 } 00294 00295 StandardShortcut find(const QKeySequence &seq) 00296 { 00297 if( !seq.isEmpty() ) { 00298 for(uint i = 0; i < sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo); i++) { 00299 StandardShortcut id = g_infoStandardShortcut[i].id; 00300 if( id != AccelNone ) { 00301 if(!g_infoStandardShortcut[i].isInitialized) 00302 initialize(id); 00303 if(g_infoStandardShortcut[i].cut.contains(seq)) 00304 return id; 00305 } 00306 } 00307 } 00308 return AccelNone; 00309 } 00310 00311 StandardShortcut find(const char *keyName) 00312 { 00313 for(uint i = 0; i < sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo); i++) 00314 if (qstrcmp(g_infoStandardShortcut[i].name, keyName)) 00315 return g_infoStandardShortcut[i].id; 00316 00317 return AccelNone; 00318 } 00319 00320 KShortcut hardcodedDefaultShortcut(StandardShortcut id) 00321 { 00322 KShortcut cut; 00323 KStandardShortcutInfo *info = guardedStandardShortcutInfo(id); 00324 00325 return KShortcut(info->cutDefault, info->cutDefault2); 00326 } 00327 00328 const KShortcut& open() { return shortcut( Open ); } 00329 const KShortcut& openNew() { return shortcut( New ); } 00330 const KShortcut& close() { return shortcut( Close ); } 00331 const KShortcut& save() { return shortcut( Save ); } 00332 const KShortcut& print() { return shortcut( Print ); } 00333 const KShortcut& quit() { return shortcut( Quit ); } 00334 const KShortcut& cut() { return shortcut( Cut ); } 00335 const KShortcut& copy() { return shortcut( Copy ); } 00336 const KShortcut& paste() { return shortcut( Paste ); } 00337 const KShortcut& pasteSelection() { return shortcut( PasteSelection ); } 00338 const KShortcut& deleteWordBack() { return shortcut( DeleteWordBack ); } 00339 const KShortcut& deleteWordForward() { return shortcut( DeleteWordForward ); } 00340 const KShortcut& undo() { return shortcut( Undo ); } 00341 const KShortcut& redo() { return shortcut( Redo ); } 00342 const KShortcut& find() { return shortcut( Find ); } 00343 const KShortcut& findNext() { return shortcut( FindNext ); } 00344 const KShortcut& findPrev() { return shortcut( FindPrev ); } 00345 const KShortcut& replace() { return shortcut( Replace ); } 00346 const KShortcut& home() { return shortcut( Home ); } 00347 const KShortcut& begin() { return shortcut( Begin ); } 00348 const KShortcut& end() { return shortcut( End ); } 00349 const KShortcut& beginningOfLine() { return shortcut( BeginningOfLine ); } 00350 const KShortcut& endOfLine() { return shortcut( EndOfLine ); } 00351 const KShortcut& prior() { return shortcut( Prior ); } 00352 const KShortcut& next() { return shortcut( Next ); } 00353 const KShortcut& backwardWord() { return shortcut( BackwardWord ); } 00354 const KShortcut& forwardWord() { return shortcut( ForwardWord ); } 00355 const KShortcut& gotoLine() { return shortcut( GotoLine ); } 00356 const KShortcut& addBookmark() { return shortcut( AddBookmark ); } 00357 const KShortcut& tabNext() { return shortcut( TabNext ); } 00358 const KShortcut& tabPrev() { return shortcut( TabPrev ); } 00359 const KShortcut& fullScreen() { return shortcut( FullScreen ); } 00360 const KShortcut& zoomIn() { return shortcut( ZoomIn ); } 00361 const KShortcut& zoomOut() { return shortcut( ZoomOut ); } 00362 const KShortcut& help() { return shortcut( Help ); } 00363 const KShortcut& completion() { return shortcut( TextCompletion ); } 00364 const KShortcut& prevCompletion() { return shortcut( PrevCompletion ); } 00365 const KShortcut& nextCompletion() { return shortcut( NextCompletion ); } 00366 const KShortcut& rotateUp() { return shortcut( RotateUp ); } 00367 const KShortcut& rotateDown() { return shortcut( RotateDown ); } 00368 const KShortcut& substringCompletion() { return shortcut( SubstringCompletion ); } 00369 const KShortcut& whatsThis() { return shortcut( WhatsThis ); } 00370 const KShortcut& reload() { return shortcut( Reload ); } 00371 const KShortcut& selectAll() { return shortcut( SelectAll ); } 00372 const KShortcut& up() { return shortcut( Up ); } 00373 const KShortcut& back() { return shortcut( Back ); } 00374 const KShortcut& forward() { return shortcut( Forward ); } 00375 const KShortcut& showMenubar() { return shortcut( ShowMenubar ); } 00376 00377 }
KDE 4.6 API Reference