[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdev-clang-tidy] src: Update enabld state of main menu action on availability change
From:       Friedrich W. H. Kossebau <null () kde ! org>
Date:       2018-07-31 23:22:45
Message-ID: E1fkdyX-0002rY-8Z () code ! kde ! org
[Download RAW message or body]

Git commit 43e0fca8898de91a163a24067302ee0a6e989971 by Friedrich W. H. Kossebau.
Committed on 31/07/2018 at 23:12.
Pushed by kossebau into branch 'master'.

Update enabld state of main menu action on availability change

M  +45   -10   src/plugin.cpp
M  +3    -0    src/plugin.h

https://commits.kde.org/kdev-clang-tidy/43e0fca8898de91a163a24067302ee0a6e989971

diff --git a/src/plugin.cpp b/src/plugin.cpp
index f4798ed..b06dbb4 100644
--- a/src/plugin.cpp
+++ b/src/plugin.cpp
@@ -60,16 +60,24 @@ K_PLUGIN_FACTORY_WITH_JSON(ClangTidyFactory, "kdevclangtidy.json", registerPlugi
 
 namespace ClangTidy
 {
+
+static
+bool isSupportedMimeType(const QMimeType& mimeType)
+{
+    const QString mime = mimeType.name();
+    return (mime == QLatin1String("text/x-c++src") || mime == QLatin1String("text/x-csrc"));
+}
+
 Plugin::Plugin(QObject* parent, const QVariantList& /*unused*/)
     : IPlugin("kdevclangtidy", parent)
     , m_model(new KDevelop::ProblemModel(parent))
 {
     setXMLFile("kdevclangtidy.rc");
 
-    QAction* act_checkfile;
-    act_checkfile = actionCollection()->addAction("clangtidy_file", this, SLOT(runClangTidyFile()));
-    act_checkfile->setText(i18n("Analyze Current File with Clang-Tidy"));
-    act_checkfile->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok")));
+    m_checkFileAction = new QAction(QIcon::fromTheme(QStringLiteral("dialog-ok")),
+                                    i18n("Analyze Current File with Clang-Tidy"), this);
+    connect(m_checkFileAction, &QAction::triggered, this, &Plugin::runClangTidyFile);
+    actionCollection()->addAction(QStringLiteral("clangtidy_file"), m_checkFileAction);
 
     /*     TODO: Uncomment this only when discover a safe way to run clang-tidy on
     the whole project.
@@ -109,6 +117,16 @@ Plugin::Plugin(QObject* parent, const QVariantList& /*unused*/)
     }
     m_activeChecks.removeDuplicates();
     m_config.writeEntry(ConfigGroup::EnabledChecks, m_activeChecks.join(','));
+
+    connect(core()->documentController(), &KDevelop::IDocumentController::documentClosed,
+            this, &Plugin::updateActions);
+    connect(core()->documentController(), &KDevelop::IDocumentController::documentActivated,
+            this, &Plugin::updateActions);
+
+    connect(core()->projectController(), &KDevelop::IProjectController::projectOpened,
+            this, &Plugin::updateActions);
+
+    updateActions();
 }
 
 Plugin::~Plugin() = default;
@@ -119,6 +137,26 @@ void Plugin::unload()
     pms->removeModel(QStringLiteral("ClangTidy"));
 }
 
+void Plugin::updateActions()
+{
+    m_checkFileAction->setEnabled(false);
+
+    if (isRunning()) {
+        return;
+    }
+
+    KDevelop::IDocument* activeDocument = core()->documentController()->activeDocument();
+    if (!activeDocument) {
+        return;
+    }
+
+    if (!isSupportedMimeType(activeDocument->mimeType())) {
+        return;
+    }
+
+    m_checkFileAction->setEnabled(true);
+}
+
 void Plugin::collectAllAvailableChecks(const QString& clangTidyPath)
 {
     m_allChecks.clear();
@@ -224,6 +262,8 @@ void Plugin::runClangTidy(const QUrl& url, bool allFiles)
     core()->runController()->registerJob(job2);
 
     m_runningJob = job2;
+
+    updateActions();
 }
 
 bool Plugin::isRunning() const
@@ -256,13 +296,8 @@ void Plugin::result(KJob* job)
         core()->uiController()->findToolView(i18nd("kdevproblemreporter", "Problems"), nullptr,
                                              KDevelop::IUiController::FindFlags::Raise);
     }
-}
 
-static
-bool isSupportedMimeType(const QMimeType& mimeType)
-{
-    const QString mime = mimeType.name();
-    return (mime == QLatin1String("text/x-c++src") || mime == QLatin1String("text/x-csrc"));
+    updateActions();
 }
 
 ContextMenuExtension Plugin::contextMenuExtension(Context* context, QWidget* parent)
diff --git a/src/plugin.h b/src/plugin.h
index ea9a53d..9dc30dc 100644
--- a/src/plugin.h
+++ b/src/plugin.h
@@ -32,6 +32,7 @@
 #include "debug.h"
 
 class KJob;
+class QAction;
 
 namespace KDevelop
 {
@@ -89,6 +90,7 @@ private Q_SLOTS:
     void runClangTidyFile();
     void runClangTidyAll();
     void result(KJob* job);
+    void updateActions();
 
 private:
     bool isRunning() const;
@@ -96,6 +98,7 @@ private:
 private:
     QPointer<KJob> m_runningJob;
 
+    QAction* m_checkFileAction;
     ConfigGroup m_config;
     QScopedPointer<KDevelop::ProblemModel> m_model;
     QStringList m_allChecks;
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic