[kde-doc-english] [krusader/plasma] /: Make shortcuts in KrViewer configurable

Simon Persson simon.persson at mykolab.com
Wed Jul 8 08:10:03 UTC 2015


Git commit 92ee349a4817eb0bd069798f07dc1c9f09aa0564 by Simon Persson.
Committed on 08/07/2015 at 08:07.
Pushed by persson into branch 'plasma'.

Make shortcuts in KrViewer configurable

Adds the actions to the main window's KActionCollection. Adds an action for opening a KShortcutDialog. Removes the "next tab", "previous tab" and "close tab" actions from the menu (to perform these actions.. if you are using the mouse you will click directly on the tab instead of in the menu, if you are using keyboard then most likely you use the shortcuts. Having them in the menu seems pointless then. If you want to see what shortcut to use for an action, you can open the config dialog and see it there. Or read the documentation.)

The rest are just small cleanups (renames, moving things, refactoring a loop), no change in behaviour.

REVIEW: 124247

M  +1    -43   doc/viewer-editor.docbook
M  +67   -31   krusader/KViewer/krviewer.cpp
M  +6    -3    krusader/KViewer/krviewer.h

http://commits.kde.org/krusader/92ee349a4817eb0bd069798f07dc1c9f09aa0564

diff --git a/doc/viewer-editor.docbook b/doc/viewer-editor.docbook
index f5af7c0..85f774c 100644
--- a/doc/viewer-editor.docbook
+++ b/doc/viewer-editor.docbook
@@ -1331,36 +1331,6 @@ linkend="kredit_config">settings</link> may be adjusted.
         <term>
           <menuchoice>
             <shortcut>
-              <keycombo action="simul">&Alt;
-              <keycap>Right</keycap></keycombo>
-            </shortcut>
-            <guimenu>KrViewer</guimenu>
-            <guimenuitem>Next Tab</guimenuitem>
-          </menuchoice>
-        </term>
-        <listitem>
-          <para>Next browsing Tab.</para>
-        </listitem>
-      </varlistentry>
-      <varlistentry>
-        <term>
-          <menuchoice>
-            <shortcut>
-              <keycombo action="simul">&Alt;
-              <keycap>Left</keycap></keycombo>
-            </shortcut>
-            <guimenu>KrViewer</guimenu>
-            <guimenuitem>Previous Tab</guimenuitem>
-          </menuchoice>
-        </term>
-        <listitem>
-          <para>Previous browsing Tab.</para>
-        </listitem>
-      </varlistentry>
-      <varlistentry>
-        <term>
-          <menuchoice>
-            <shortcut>
               <keycombo action="simul">
               &Ctrl;&Shift;
               <keycap>D</keycap></keycombo>
@@ -1377,25 +1347,13 @@ linkend="kredit_config">settings</link> may be adjusted.
       <varlistentry>
         <term>
           <menuchoice>
-            <shortcut>&Esc;</shortcut>
-            <guimenu>KrViewer</guimenu>
-            <guimenuitem>Close current Tab.</guimenuitem>
-          </menuchoice>
-        </term>
-        <listitem>
-          <para>Close current browsing Tab.</para>
-        </listitem>
-      </varlistentry>
-      <varlistentry>
-        <term>
-          <menuchoice>
             <shortcut>
 	    <keycombo action="simul">
             &Ctrl;
             <keycap>Q</keycap></keycombo>
 	    </shortcut>
             <guimenu>KrViewer</guimenu>
-            <guimenuitem>Close</guimenuitem>
+            <guimenuitem>Quit</guimenuitem>
           </menuchoice>
         </term>
         <listitem>
diff --git a/krusader/KViewer/krviewer.cpp b/krusader/KViewer/krviewer.cpp
index 3a4763a..25d6479 100644
--- a/krusader/KViewer/krviewer.cpp
+++ b/krusader/KViewer/krviewer.cpp
@@ -28,17 +28,18 @@
 #include <QtWidgets/QMenuBar>
 #include <QtWidgets/QStatusBar>
 
-#include <KParts/Part>
-//#include <KParts/ComponentFactory> // missing?
-#include <KWidgetsAddons/KMessageBox>
 #include <KCoreAddons/KProcess>
+#include <KCoreAddons/KShell>
 #include <KConfigCore/KSharedConfig>
+#include <KConfigWidgets/KStandardAction>
 #include <KI18n/KLocalizedString>
 #include <KIconThemes/KIconLoader>
 #include <KIOCore/KFileItem>
+#include <KParts/Part>
+#include <KWidgetsAddons/KMessageBox>
+#include <KXmlGui/KActionCollection>
+#include <KXmlGui/KShortcutsDialog>
 #include <KXmlGui/KToolBar>
-#include <KConfigWidgets/KStandardAction>
-#include <KCoreAddons/KShell>
 
 #include "../krglobal.h"
 #include "../defaults.h"
@@ -81,21 +82,24 @@ KrViewer::KrViewer(QWidget *parent) :
     copyAction = KStandardAction::copy(this, SLOT(copy()), 0);
 
     viewerMenu = new QMenu(this);
-    viewerMenu->addAction(i18n("&Generic Viewer"), this, SLOT(viewGeneric()))->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_G);
-    viewerMenu->addAction(i18n("&Text Viewer"), this, SLOT(viewText()))->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_T);
-    viewerMenu->addAction(i18n("&Hex Viewer"), this, SLOT(viewHex()))->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_H);
-    viewerMenu->addAction(i18n("&Lister"), this, SLOT(viewLister()))->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_L);
-    viewerMenu->addSeparator();
-    viewerMenu->addAction(i18n("Text &Editor"), this, SLOT(editText()))->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_E);
+    QAction *tempAction;
+    KActionCollection *ac = actionCollection();
+
+#define addCustomMenuAction(name, text, slot, shortcut)\
+    tempAction = ac->addAction(name, this, slot);\
+    tempAction->setText(text);\
+    ac->setDefaultShortcut(tempAction, shortcut);\
+    viewerMenu->addAction(tempAction);
+
+    addCustomMenuAction("genericViewer", i18n("&Generic Viewer"), SLOT(viewGeneric()), Qt::CTRL + Qt::SHIFT + Qt::Key_G);
+    addCustomMenuAction("textViewer", i18n("&Text Viewer"), SLOT(viewText()), Qt::CTRL + Qt::SHIFT + Qt::Key_T);
+    addCustomMenuAction("hexViewer", i18n("&Hex Viewer"), SLOT(viewHex()), Qt::CTRL + Qt::SHIFT + Qt::Key_H);
+    addCustomMenuAction("lister", i18n("&Lister"), SLOT(viewLister()), Qt::CTRL + Qt::SHIFT + Qt::Key_L);
     viewerMenu->addSeparator();
-    viewerMenu->addAction(i18n("&Next Tab"), this, SLOT(nextTab()))->setShortcut(Qt::ALT + Qt::Key_Right);
-    viewerMenu->addAction(i18n("&Previous Tab"), this, SLOT(prevTab()))->setShortcut(Qt::ALT + Qt::Key_Left);
 
-    detachAction = viewerMenu->addAction(i18n("&Detach Tab"), this, SLOT(detachTab()));
-    detachAction->setShortcut(Qt::META + Qt::Key_D);
-    //no point in detaching only one tab..
-    detachAction->setEnabled(false);
+    addCustomMenuAction("textEditor", i18n("Text &Editor"), SLOT(editText()), Qt::CTRL + Qt::SHIFT + Qt::Key_E);
     viewerMenu->addSeparator();
+
     QList<QAction *> actList = menuBar()->actions();
     bool hasPrint = false, hasCopy = false;
     foreach(QAction *a, actList) {
@@ -111,8 +115,35 @@ KrViewer::KrViewer(QWidget *parent) :
     if (hasCopy)
         copyAct->setShortcut(copyAction->shortcut());
     viewerMenu->addSeparator();
-    (tabClose = viewerMenu->addAction(i18n("&Close Current Tab"), this, SLOT(tabCloseRequest())))->setShortcut(Qt::Key_Escape);
-    (closeAct = viewerMenu->addAction(i18n("&Quit"), this, SLOT(close())))->setShortcut(Qt::CTRL + Qt::Key_Q);
+
+    configKeysAction = ac->addAction(KStandardAction::KeyBindings, this, SLOT(configureShortcuts()));
+    viewerMenu->addAction(configKeysAction);
+    viewerMenu->addSeparator();
+
+    detachAction = ac->addAction("detachTab", this, SLOT(detachTab()));
+    detachAction->setText(i18n("&Detach Tab"));
+    //no point in detaching only one tab..
+    detachAction->setEnabled(false);
+    ac->setDefaultShortcut(detachAction, Qt::META + Qt::Key_D);
+    viewerMenu->addAction(detachAction);
+
+    quitAction = ac->addAction(KStandardAction::Quit, this, SLOT(close()));
+    viewerMenu->addAction(quitAction);
+
+    tabCloseAction = ac->addAction("closeTab", this, SLOT(tabCloseRequest()));
+    tabCloseAction->setText(i18n("&Close Current Tab"));
+    QList<QKeySequence> shortcuts = KStandardShortcut::close();
+    shortcuts.append(Qt::Key_Escape);
+    ac->setDefaultShortcuts(tabCloseAction, shortcuts);
+
+    tabNextAction = ac->addAction("nextTab", this, SLOT(nextTab()));
+    tabNextAction->setText(i18n("&Next Tab"));
+    ac->setDefaultShortcuts(tabNextAction, KStandardShortcut::tabNext());
+
+    tabPrevAction = ac->addAction("prevTab", this, SLOT(prevTab()));
+    tabPrevAction->setText(i18n("&Previous Tab"));
+    ac->setDefaultShortcuts(tabPrevAction, KStandardShortcut::tabPrev());
+
 
     tabBar.setTabsClosable(true);
 
@@ -171,17 +202,16 @@ void KrViewer::createGUI(KParts::Part* part)
     reservedKeyActions.clear();
 
     QList<QAction *> list = viewerMenu->actions();
+    // also add the actions that are not in the menu...
+    list << tabCloseAction << tabNextAction << tabPrevAction;
     // getting the key sequences of the viewer menu
     for (int w = 0; w != list.count(); w++) {
         QAction *act = list[ w ];
         QList<QKeySequence> sequences = act->shortcuts();
-        if (!sequences.isEmpty()) {
-            for (int i = 0; i != sequences.count(); i++) {
-                QKeySequence keySeq = sequences.at(i);
-                if(keySeq.count() > 0) {
-                    reservedKeys.push_back(keySeq[0]);
-                    reservedKeyActions.push_back(act);
-                }
+        foreach(QKeySequence keySeq, sequences) {
+            for(int i = 0; i < keySeq.count(); ++i) {
+                reservedKeys.push_back(keySeq[i]);
+                reservedKeyActions.push_back(act); //the same action many times in case of multiple shortcuts
             }
         }
     }
@@ -194,8 +224,16 @@ void KrViewer::createGUI(KParts::Part* part)
     menuBar() ->show();
 }
 
+void KrViewer::configureShortcuts()
+{
+    KShortcutsDialog::configure(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this);
+}
+
 bool KrViewer::eventFilter(QObject * /* watched */, QEvent * e)
 {
+    // TODO: after porting to Qt5/KF5 we never catch *ANY* KeyPress or ShortcutOverride events here anymore.
+    // Should look into if there is any way to fix it. Currently if a KPart has same shortcut as KrViewer then
+    // it causes a conflict, messagebox shown to user and no action triggered.
     if (e->type() == QEvent::ShortcutOverride) {
         QKeyEvent* ke = (QKeyEvent*) e;
         if (reservedKeys.contains(ke->key())) {
@@ -205,11 +243,9 @@ bool KrViewer::eventFilter(QObject * /* watched */, QEvent * e)
             if (act != 0) {
                 // don't activate the close functions immediately!
                 // it can cause crash
-                if (act == tabClose)
-                    QTimer::singleShot(0, this, SLOT(tabCloseRequest()));
-                else if (act == closeAct)
-                    QTimer::singleShot(0, this, SLOT(close()));
-                else {
+                if (act == tabCloseAction || act == quitAction) {
+                    QTimer::singleShot(0, act, SLOT(trigger()));
+                } else {
                     act->activate(QAction::Trigger);
                 }
             }
diff --git a/krusader/KViewer/krviewer.h b/krusader/KViewer/krviewer.h
index 1b5516b..6df6c6c 100644
--- a/krusader/KViewer/krviewer.h
+++ b/krusader/KViewer/krviewer.h
@@ -57,6 +57,7 @@ public:
 public slots:
     virtual void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE;
     void createGUI(KParts::Part*);
+    void configureShortcuts();
 
     void viewGeneric();
     void viewText();
@@ -112,12 +113,14 @@ private:
     QPointer<QWidget> returnFocusTo;
 
     QAction *detachAction;
-
     QAction *printAction;
     QAction *copyAction;
+    QAction *quitAction;
 
-    QAction *tabClose;
-    QAction *closeAct;
+    QAction *configKeysAction;
+    QAction *tabCloseAction;
+    QAction *tabNextAction;
+    QAction *tabPrevAction;
 
     static QList<KrViewer *> viewers; // the first viewer is the active one
     QList<int>    reservedKeys;   // the reserved key sequences


More information about the kde-doc-english mailing list