From kde-commits Tue Jul 31 23:22:45 2018 From: Friedrich W. H. Kossebau Date: Tue, 31 Jul 2018 23:22:45 +0000 To: kde-commits Subject: [kdev-clang-tidy] src: Update enabld state of main menu action on availability change Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=153307937712310 Git commit 43e0fca8898de91a163a24067302ee0a6e989971 by Friedrich W. H. Koss= ebau. 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/43e0fca8898de91a163a24067302ee0a6e9= 89971 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, "kdevclang= tidy.json", registerPlugi = namespace ClangTidy { + +static +bool isSupportedMimeType(const QMimeType& mimeType) +{ + const QString mime =3D mimeType.name(); + return (mime =3D=3D QLatin1String("text/x-c++src") || mime =3D=3D QLat= in1String("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 =3D 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 =3D new QAction(QIcon::fromTheme(QStringLiteral("dia= log-ok")), + i18n("Analyze Current File with Clang-= Tidy"), this); + connect(m_checkFileAction, &QAction::triggered, this, &Plugin::runClan= gTidyFile); + actionCollection()->addAction(QStringLiteral("clangtidy_file"), m_chec= kFileAction); = /* 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::pr= ojectOpened, + this, &Plugin::updateActions); + + updateActions(); } = Plugin::~Plugin() =3D 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 =3D 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 allFile= s) core()->runController()->registerJob(job2); = m_runningJob =3D job2; + + updateActions(); } = bool Plugin::isRunning() const @@ -256,13 +296,8 @@ void Plugin::result(KJob* job) core()->uiController()->findToolView(i18nd("kdevproblemreporter", = "Problems"), nullptr, KDevelop::IUiController::Find= Flags::Raise); } -} = -static -bool isSupportedMimeType(const QMimeType& mimeType) -{ - const QString mime =3D mimeType.name(); - return (mime =3D=3D QLatin1String("text/x-c++src") || mime =3D=3D QLat= in1String("text/x-csrc")); + updateActions(); } = ContextMenuExtension Plugin::contextMenuExtension(Context* context, QWidge= t* 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 m_runningJob; = + QAction* m_checkFileAction; ConfigGroup m_config; QScopedPointer m_model; QStringList m_allChecks;