• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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 {
00040     StandardShortcut id;
00041     const char* name;
00042     const char* description;
00043     int cutDefault, cutDefault2;
00044     KShortcut cut;
00045     bool isInitialized;
00046 };
00047 
00048 #define CTRL(x) Qt::CTRL+Qt::Key_##x
00049 #define SHIFT(x) Qt::SHIFT+Qt::Key_##x
00050 #define CTRLSHIFT(x) Qt::CTRL+Qt::SHIFT+Qt::Key_##x
00051 #define ALT(x) Qt::ALT+Qt::Key_##x
00052 
00057 // STUFF WILL BREAK IF YOU DON'T READ THIS!!!
00058 // Read the comments of the big enum in kstandardshortcut.h before you change anything!
00059 static KStandardShortcutInfo g_infoStandardShortcut[] =
00060 {
00061 //Group File,
00062     {AccelNone,            0, 0, 0, 0, KShortcut(), false },
00063     { Open,                I18N_NOOP2("@action","Open"),  0,    CTRL(O), 0, KShortcut(), false },
00064     { New,                 I18N_NOOP2("@action","New"),   0,    CTRL(N), 0, KShortcut(), false },
00065     { Close,               I18N_NOOP2("@action","Close"), 0,    CTRL(W), CTRL(Escape), KShortcut(), false },
00066     { Save,                I18N_NOOP2("@action","Save"),  0,    CTRL(S), 0, KShortcut(), false },
00067     { Print,               I18N_NOOP2("@action","Print"), 0,    CTRL(P), 0, KShortcut(), false },
00068     { Quit,                I18N_NOOP2("@action","Quit"),  0,    CTRL(Q), 0, KShortcut(), false },
00069 
00070 //Group Edit
00071     { Undo,                I18N_NOOP2("@action","Undo"),  0,           CTRL(Z), 0, KShortcut(), false },
00072     { Redo,                I18N_NOOP2("@action","Redo"),  0,           CTRLSHIFT(Z), 0, KShortcut(), false },
00073     { Cut,                 I18N_NOOP2("@action","Cut"),   0,           CTRL(X), SHIFT(Delete), KShortcut(), false },
00074     { Copy,                I18N_NOOP2("@action","Copy"),  0,           CTRL(C), CTRL(Insert), KShortcut(), false },
00075     { Paste,               I18N_NOOP2("@action","Paste"), 0,           CTRL(V), SHIFT(Insert), KShortcut(), false },
00076     { PasteSelection,      I18N_NOOP2("@action","Paste Selection"),    0, CTRLSHIFT(Insert), 0, KShortcut(), false },
00077 
00078     { SelectAll,           "SelectAll", I18N_NOOP2("@action","Select All"), CTRL(A), 0, KShortcut(), false },
00079     { Deselect,            I18N_NOOP2("@action","Deselect"), 0, CTRLSHIFT(A), 0, KShortcut(), false },
00080     { DeleteWordBack,      "DeleteWordBack", I18N_NOOP2("@action","Delete Word Backwards"), CTRL(Backspace), 0, KShortcut(), false },
00081     { DeleteWordForward,   "DeleteWordForward", I18N_NOOP2("@action","Delete Word Forward"),CTRL(Delete), 0,  KShortcut(), false },
00082 
00083     { Find,                I18N_NOOP2("@action","Find"),  0,     CTRL(F), 0, KShortcut(), false },
00084     { FindNext,            "FindNext", I18N_NOOP2("@action","Find Next"), Qt::Key_F3, 0, KShortcut(), false },
00085     { FindPrev,            "FindPrev", I18N_NOOP2("@action","Find Prev"), SHIFT(F3), 0, KShortcut(), false },
00086     { Replace,             I18N_NOOP2("@action","Replace"), 0,  CTRL(R), 0, KShortcut(), false },
00087 
00088 //Group Navigation
00089     { Home,                I18N_NOOP2("@action Go to main page","Home"), 0, ALT(Home), Qt::Key_HomePage, KShortcut(), false },
00090     { Begin,               I18N_NOOP2("@action Beginning of document","Begin"), 0, CTRL(Home), 0, KShortcut(), false },
00091     { End,                 I18N_NOOP2("@action End of document","End"), 0,      CTRL(End), 0, KShortcut(), false },
00092     { Prior,               I18N_NOOP2("@action","Prior"), 0,    Qt::Key_PageUp, 0,KShortcut(), false },
00093     { Next,                I18N_NOOP2("@action Opposite to Prior","Next"), 0, Qt::Key_PageDown, 0, KShortcut(), false },
00094 
00095     { Up,                  I18N_NOOP2("@action","Up"), 0,       ALT(Up), 0, KShortcut(), false },
00096     { Back,                I18N_NOOP2("@action","Back"), 0,     ALT(Left), Qt::Key_Back, KShortcut(), false },
00097     { Forward,             I18N_NOOP2("@action","Forward"), 0,  ALT(Right), Qt::Key_Forward, KShortcut(), false },
00098     { Reload,              I18N_NOOP2("@action","Reload"), 0,   Qt::Key_F5, Qt::Key_Refresh, KShortcut(), false },
00099 
00100     { BeginningOfLine,     "BeginningOfLine", I18N_NOOP2("@action","Beginning of Line"), Qt::Key_Home, 0, KShortcut(), false},
00101     { EndOfLine,           "EndOfLine", I18N_NOOP2("@action","End of Line"), Qt::Key_End, 0, KShortcut(), false},
00102     { GotoLine,            "GotoLine", I18N_NOOP2("@action","Go to Line"), CTRL(G), 0, KShortcut(), false },
00103     { BackwardWord,        "BackwardWord", I18N_NOOP2("@action","Backward Word"), CTRL(Left), 0, KShortcut(), false },
00104     { ForwardWord,         "ForwardWord", I18N_NOOP2("@action","Forward Word"), CTRL(Right), 0, KShortcut(), false },
00105 
00106     { AddBookmark,         "AddBookmark", I18N_NOOP2("@action","Add Bookmark"), CTRL(B), 0, KShortcut(), false },
00107     { ZoomIn,              "ZoomIn", I18N_NOOP2("@action","Zoom In"), CTRL(Plus), 0, KShortcut(), false },
00108     { ZoomOut,             "ZoomOut", I18N_NOOP2("@action","Zoom Out"), CTRL(Minus), 0, KShortcut(), false },
00109     { FullScreen,          "FullScreen", I18N_NOOP2("@action","Full Screen Mode"), CTRLSHIFT(F), 0, KShortcut(), false },
00110 
00111     { ShowMenubar,         "ShowMenubar", I18N_NOOP2("@action","Show Menu Bar"),CTRL(M), 0, KShortcut(), false },
00112     { TabNext,             I18N_NOOP2("@action","Activate Next Tab"), 0, CTRL(Period), CTRL(BracketRight), KShortcut(), false },
00113     { TabPrev,             I18N_NOOP2("@action","Activate Previous Tab"), 0, CTRL(Comma), CTRL(BracketLeft), KShortcut(), false },
00114 
00115 //Group Help
00116     { Help,                I18N_NOOP2("@action","Help"), 0,                        Qt::Key_F1, 0, KShortcut(), false },
00117     { WhatsThis,           "WhatsThis",       I18N_NOOP2("@action","What's This"), SHIFT(F1),  0, KShortcut(), false },
00118 
00119 //Group TextCompletion
00120     { TextCompletion,      "TextCompletion",       I18N_NOOP2("@action","Text Completion"),           CTRL(E),    0, KShortcut(), false },
00121     { PrevCompletion,      "PrevCompletion",       I18N_NOOP2("@action","Previous Completion Match"), CTRL(Up),   0, KShortcut(), false },
00122     { NextCompletion,      "NextCompletion",       I18N_NOOP2("@action","Next Completion Match"),     CTRL(Down), 0, KShortcut(), false },
00123     { SubstringCompletion, "SubstringCompletion",  I18N_NOOP2("@action","Substring Completion"),      CTRL(T),    0, KShortcut(), false },
00124 
00125     { RotateUp,            "RotateUp",   I18N_NOOP2("@action","Previous Item in List"), Qt::Key_Up,   0, KShortcut(), false },
00126     { RotateDown,          "RotateDown", I18N_NOOP2("@action","Next Item in List"),     Qt::Key_Down, 0, KShortcut(), false },
00127 
00128 //dummy entry to catch simple off-by-one errors. Insert new entries before this line.
00129     { AccelNone,            0, 0, 0, 0, KShortcut(), false }
00130 };
00131 
00132 
00136 static KStandardShortcutInfo *guardedStandardShortcutInfo(StandardShortcut id)
00137 {
00138     if (id >= static_cast<int>(sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo)) ||
00139              id < 0) {
00140         kWarning(125) << "KStandardShortcut: id not found!";
00141         return &g_infoStandardShortcut[AccelNone];
00142     } else
00143         return &g_infoStandardShortcut[id];
00144 }
00145 
00151 static void initialize(StandardShortcut id)
00152 {
00153     KStandardShortcutInfo *info = guardedStandardShortcutInfo(id);
00154 
00155     KConfigGroup cg(KGlobal::config(), "Shortcuts");
00156 
00157 #ifdef Q_WS_X11
00158     // Code within this block breaks if we aren't running in GUI mode.
00159     if(QX11Info::display() && cg.hasKey(info->name))
00160 #else
00161     if(cg.hasKey(info->name))
00162 #endif
00163     {
00164         QString s = cg.readEntry(info->name);
00165         if (s != "none")
00166             info->cut = KShortcut(s);
00167         else
00168             info->cut = KShortcut();
00169     } else {
00170         info->cut = hardcodedDefaultShortcut(id);
00171     }
00172 
00173     info->isInitialized = true;
00174 }
00175 
00176 void saveShortcut(StandardShortcut id, const KShortcut &newShortcut)
00177 {
00178     KStandardShortcutInfo *info = guardedStandardShortcutInfo(id);
00179     if(info->id == AccelNone)
00180         return;
00181 
00182     KConfigGroup cg(KGlobal::config(), "Shortcuts");
00183 
00184     info->cut = newShortcut;
00185     bool sameAsDefault = (newShortcut == hardcodedDefaultShortcut(id));
00186 
00187     if (sameAsDefault)
00188         if(cg.hasKey(info->name))
00189             cg.deleteEntry(info->name);
00190         else
00191             return;
00192 
00193     cg.writeEntry(info->name, info->cut.toString());
00194 }
00195 
00196 QString name(StandardShortcut id)
00197 {
00198     return guardedStandardShortcutInfo(id)->name;
00199 }
00200 
00201 QString label(StandardShortcut id)
00202 {
00203     KStandardShortcutInfo *info = guardedStandardShortcutInfo( id );
00204     return i18n((info->description) ? info->description : info->name);
00205 }
00206 
00207 // TODO: Add psWhatsThis entry to KStandardShortcutInfo
00208 QString whatsThis( StandardShortcut /*id*/ )
00209 {
00210 //  KStandardShortcutInfo* info = guardedStandardShortcutInfo( id );
00211 //  if( info && info->whatsThis )
00212 //      return i18n(info->whatsThis);
00213 //  else
00214         return QString();
00215 }
00216 
00217 const KShortcut &shortcut(StandardShortcut id)
00218 {
00219     KStandardShortcutInfo *info = guardedStandardShortcutInfo(id);
00220 
00221     if(!info->isInitialized)
00222         initialize(id);
00223 
00224     return info->cut;
00225 }
00226 
00227 StandardShortcut find(const QKeySequence &seq)
00228 {
00229     if( !seq.isEmpty() ) {
00230         for(uint i = 0; i < sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo); i++) {
00231             StandardShortcut id = g_infoStandardShortcut[i].id;
00232             if( id != AccelNone ) {
00233                 if(!g_infoStandardShortcut[i].isInitialized)
00234                     initialize(id);
00235                 if(g_infoStandardShortcut[i].cut.contains(seq))
00236                     return id;
00237             }
00238         }
00239     }
00240     return AccelNone;
00241 }
00242 
00243 StandardShortcut find(const char *keyName)
00244 {
00245     for(uint i = 0; i < sizeof(g_infoStandardShortcut) / sizeof(KStandardShortcutInfo); i++)
00246         if (qstrcmp(g_infoStandardShortcut[i].name, keyName))
00247             return g_infoStandardShortcut[i].id;
00248 
00249     return AccelNone;
00250 }
00251 
00252 KShortcut hardcodedDefaultShortcut(StandardShortcut id)
00253 {
00254     KShortcut cut;
00255     KStandardShortcutInfo *info = guardedStandardShortcutInfo(id);
00256 
00257     return KShortcut(info->cutDefault, info->cutDefault2);
00258 }
00259 
00260 const KShortcut& open()                  { return shortcut( Open ); }
00261 const KShortcut& openNew()               { return shortcut( New ); }
00262 const KShortcut& close()                 { return shortcut( Close ); }
00263 const KShortcut& save()                  { return shortcut( Save ); }
00264 const KShortcut& print()                 { return shortcut( Print ); }
00265 const KShortcut& quit()                  { return shortcut( Quit ); }
00266 const KShortcut& cut()                   { return shortcut( Cut ); }
00267 const KShortcut& copy()                  { return shortcut( Copy ); }
00268 const KShortcut& paste()                 { return shortcut( Paste ); }
00269 const KShortcut& pasteSelection()        { return shortcut( PasteSelection ); }
00270 const KShortcut& deleteWordBack()        { return shortcut( DeleteWordBack ); }
00271 const KShortcut& deleteWordForward()     { return shortcut( DeleteWordForward ); }
00272 const KShortcut& undo()                  { return shortcut( Undo ); }
00273 const KShortcut& redo()                  { return shortcut( Redo ); }
00274 const KShortcut& find()                  { return shortcut( Find ); }
00275 const KShortcut& findNext()              { return shortcut( FindNext ); }
00276 const KShortcut& findPrev()              { return shortcut( FindPrev ); }
00277 const KShortcut& replace()               { return shortcut( Replace ); }
00278 const KShortcut& home()                  { return shortcut( Home ); }
00279 const KShortcut& begin()                 { return shortcut( Begin ); }
00280 const KShortcut& end()                   { return shortcut( End ); }
00281 const KShortcut& beginningOfLine()       { return shortcut( BeginningOfLine ); }
00282 const KShortcut& endOfLine()             { return shortcut( EndOfLine ); }
00283 const KShortcut& prior()                 { return shortcut( Prior ); }
00284 const KShortcut& next()                  { return shortcut( Next ); }
00285 const KShortcut& backwardWord()          { return shortcut( BackwardWord ); }
00286 const KShortcut& forwardWord()           { return shortcut( ForwardWord ); }
00287 const KShortcut& gotoLine()              { return shortcut( GotoLine ); }
00288 const KShortcut& addBookmark()           { return shortcut( AddBookmark ); }
00289 const KShortcut& tabNext()               { return shortcut( TabNext ); }
00290 const KShortcut& tabPrev()               { return shortcut( TabPrev ); }
00291 const KShortcut& fullScreen()            { return shortcut( FullScreen ); }
00292 const KShortcut& zoomIn()                { return shortcut( ZoomIn ); }
00293 const KShortcut& zoomOut()               { return shortcut( ZoomOut ); }
00294 const KShortcut& help()                  { return shortcut( Help ); }
00295 const KShortcut& completion()            { return shortcut( TextCompletion ); }
00296 const KShortcut& prevCompletion()        { return shortcut( PrevCompletion ); }
00297 const KShortcut& nextCompletion()        { return shortcut( NextCompletion ); }
00298 const KShortcut& rotateUp()              { return shortcut( RotateUp ); }
00299 const KShortcut& rotateDown()            { return shortcut( RotateDown ); }
00300 const KShortcut& substringCompletion()   { return shortcut( SubstringCompletion ); }
00301 const KShortcut& whatsThis()             { return shortcut( WhatsThis ); }
00302 const KShortcut& reload()                { return shortcut( Reload ); }
00303 const KShortcut& selectAll()             { return shortcut( SelectAll ); }
00304 const KShortcut& up()                    { return shortcut( Up ); }
00305 const KShortcut& back()                  { return shortcut( Back ); }
00306 const KShortcut& forward()               { return shortcut( Forward ); }
00307 const KShortcut& showMenubar()           { return shortcut( ShowMenubar ); }
00308 
00309 }

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal