[kde-doc-english] [kate] addons/project: Project plugin: use KMoreTools to implement the Git context menu

Gregor Mi codestruct at posteo.org
Sat May 23 14:49:11 UTC 2015


Git commit 4e18103e3a4f5753c67ec050c1bab5494113eb54 by Gregor Mi.
Committed on 23/05/2015 at 14:49.
Pushed by gregormi into branch 'master'.

Project plugin: use KMoreTools to implement the Git context menu

Visible changes:
- Move git items away from submenu but put them under a menu section
which reduces amount of submenus
- If one of the items is not installed it is placed in the "More"
submenu to give a hint that the app exist and might be helpful
- new: add gitg
- NEW: add "Git Cola (View History)" that opens the history browser for
the given file
- Menu items get icons when available, e.g. git-cola and gitg
- Menu items get caption and its translation from desktop file

Replacing old code results in less code for the project plugin.

REVIEW: 122374

DIGEST:

GUI:

M  +3    -0    addons/project/CMakeLists.txt
M  +10   -38   addons/project/kateprojecttreeviewcontextmenu.cpp

http://commits.kde.org/kate/4e18103e3a4f5753c67ec050c1bab5494113eb54

diff --git a/addons/project/CMakeLists.txt b/addons/project/CMakeLists.txt
index f576fcc..83dd4a7 100644
--- a/addons/project/CMakeLists.txt
+++ b/addons/project/CMakeLists.txt
@@ -1,5 +1,7 @@
 project(kateprojectplugin)
 
+find_package(KF5NewStuff ${KF5_DEP_VERSION} REQUIRED) # For KMoreTools
+
 # libgit2 integration
 find_package(LibGit2 "0.22.0")
 if(LIBGIT2_FOUND)
@@ -37,6 +39,7 @@ target_link_libraries(kateprojectplugin
     KF5::Parts KF5::I18n
     KF5::GuiAddons
     Qt5::Script KF5::ItemViews KF5::ItemModels KF5::IconThemes KF5::ThreadWeaver
+    KF5::NewStuff # For KMoreTools
     ${PROJECT_OPTIONAL_LIBS}
 )
 
diff --git a/addons/project/kateprojecttreeviewcontextmenu.cpp b/addons/project/kateprojecttreeviewcontextmenu.cpp
index 0dda9b7..7b49f29 100644
--- a/addons/project/kateprojecttreeviewcontextmenu.cpp
+++ b/addons/project/kateprojecttreeviewcontextmenu.cpp
@@ -23,6 +23,8 @@
 #include <klocalizedstring.h>
 #include <KMimeTypeTrader>
 #include <KRun>
+#include <KNS3/KMoreTools>
+#include <KNS3/KMoreToolsMenuFactory>
 
 #include <QMenu>
 #include <QStandardPaths>
@@ -60,22 +62,6 @@ static bool isGit(const QString &filename)
     return isGit;
 }
 
-static bool appExists(const QString &appname)
-{
-    return ! QStandardPaths::findExecutable(appname).isEmpty();
-}
-
-static void launchApp(const QString &app, const QString &file)
-{
-    QFileInfo fi(file);
-    QDir dir(fi.absoluteDir());
-
-    QStringList args;
-    args << file;
-
-    QProcess::startDetached(app, QStringList(), dir.absolutePath());
-}
-
 void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint &pos, QWidget *parent)
 {
     /**
@@ -110,27 +96,16 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint
      */
     openWithMenu->setEnabled(!openWithMenu->isEmpty());
 
-    QList<QAction *> appActions;
+    KMoreToolsMenuFactory menuFactory(QLatin1String("kate/addons/project/git-tools"));
+
     if (isGit(filename)) {
-        QMenu *git = menu.addMenu(i18n("Git Tools"));
-        if (appExists(QStringLiteral("gitk"))) {
-            QAction *action = git->addAction(i18n("Launch gitk"));
-            action->setData(QStringLiteral("gitk"));
-            appActions.append(action);
-        }
-        if (appExists(QStringLiteral("qgit"))) {
-            QAction *action = git->addAction(i18n("Launch qgit"));
-            action->setData(QStringLiteral("qgit"));
-            appActions.append(action);
-        }
-        if (appExists(QStringLiteral("git-cola"))) {
-            QAction *action = git->addAction(i18n("Launch git-cola"));
-            action->setData(QStringLiteral("git-cola"));
-            appActions.append(action);
-        }
 
-        if (appActions.size() == 0) {
-            delete git;
+        auto gitMenu = menuFactory.createMenuFromGroupingNames({ QLatin1String("git-clients-and-actions") },
+                                                               QUrl::fromLocalFile(filename));
+
+        menu.addSection(i18n("Git:"));
+        Q_FOREACH(auto action, gitMenu->actions()) {
+            menu.addAction(action);
         }
     }
 
@@ -142,8 +117,6 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint
         // handle apps
         if (copyAction == action) {
             QApplication::clipboard()->setText(filename);
-        } else if (appActions.contains(action)) {
-            launchApp(action->data().toString(), filename);
         } else {
             // handle "open with"
             const QString openWith = action->data().toString();
@@ -155,4 +128,3 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint
         }
     }
 }
-


More information about the kde-doc-english mailing list