From f083293c317129dfb411c6b5611c505b148733fc Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Wed, 16 May 2018 13:07:10 +0200 Subject: [PATCH] ClangTools: Adapt button state to checked fixits "Apply Fixits" is disabled by default and enabled as soon as some fixits are checked by the user. Change-Id: I7e1345512b206f52d1e8628705c81c6b34dfb9ba Reviewed-by: Ivan Donchevskii --- src/plugins/clangtools/clangtidyclazytool.cpp | 4 ++++ .../clangtools/clangtoolsdiagnosticmodel.cpp | 15 ++++++++++++--- .../clangtools/clangtoolsdiagnosticmodel.h | 12 +++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/plugins/clangtools/clangtidyclazytool.cpp b/src/plugins/clangtools/clangtidyclazytool.cpp index fdf6a9adc6d..2b977c45482 100644 --- a/src/plugins/clangtools/clangtidyclazytool.cpp +++ b/src/plugins/clangtools/clangtidyclazytool.cpp @@ -161,6 +161,10 @@ ClangTidyClazyTool::ClangTidyClazyTool() // Apply fixits button m_applyFixitsButton = new QToolButton; m_applyFixitsButton->setText(tr("Apply Fixits")); + m_applyFixitsButton->setEnabled(false); + connect(m_diagnosticModel, + &ClangToolsDiagnosticModel::fixItsToApplyCountChanged, + [this](int c) { m_applyFixitsButton->setEnabled(c); }); connect(m_applyFixitsButton, &QToolButton::clicked, [this]() { QVector diagnosticsWithFixits; diff --git a/src/plugins/clangtools/clangtoolsdiagnosticmodel.cpp b/src/plugins/clangtools/clangtoolsdiagnosticmodel.cpp index 1ffc3351e54..1f1dba4039a 100644 --- a/src/plugins/clangtools/clangtoolsdiagnosticmodel.cpp +++ b/src/plugins/clangtools/clangtoolsdiagnosticmodel.cpp @@ -61,8 +61,13 @@ ClangToolsDiagnosticModel::ClangToolsDiagnosticModel(QObject *parent) void ClangToolsDiagnosticModel::addDiagnostics(const QList &diagnostics) { - foreach (const Diagnostic &d, diagnostics) - rootItem()->appendChild(new DiagnosticItem(d)); + const auto onFixItChanged = [this](bool checked){ + m_fixItsToApplyCount += checked ? +1 : -1; + emit fixItsToApplyCountChanged(m_fixItsToApplyCount); + }; + + for (const Diagnostic &d : diagnostics) + rootItem()->appendChild(new DiagnosticItem(d, onFixItChanged)); } QList ClangToolsDiagnosticModel::diagnostics() const @@ -199,7 +204,9 @@ static QString fullText(const Diagnostic &diagnostic) } -DiagnosticItem::DiagnosticItem(const Diagnostic &diag) : m_diagnostic(diag) +DiagnosticItem::DiagnosticItem(const Diagnostic &diag, const OnCheckedFixit &onCheckedFixit) + : m_diagnostic(diag) + , m_onCheckedFixit(onCheckedFixit) { // Don't show explaining steps if they add no information. if (diag.explainingSteps.count() == 1) { @@ -277,6 +284,8 @@ bool DiagnosticItem::setData(int column, const QVariant &data, int role) if (column == DiagnosticView::FixItColumn && role == Qt::CheckStateRole) { m_applyFixits = data.value() == Qt::Checked ? true : false; update(); + if (m_onCheckedFixit) + m_onCheckedFixit(m_applyFixits); return true; } diff --git a/src/plugins/clangtools/clangtoolsdiagnosticmodel.h b/src/plugins/clangtools/clangtoolsdiagnosticmodel.h index 0fbc99a5b7c..61aa6055c5e 100644 --- a/src/plugins/clangtools/clangtoolsdiagnosticmodel.h +++ b/src/plugins/clangtools/clangtoolsdiagnosticmodel.h @@ -35,6 +35,8 @@ #include #include +#include + namespace ProjectExplorer { class Project; } namespace ClangTools { @@ -43,7 +45,8 @@ namespace Internal { class DiagnosticItem : public Utils::TreeItem { public: - DiagnosticItem(const Diagnostic &diag); + using OnCheckedFixit = std::function; + DiagnosticItem(const Diagnostic &diag, const OnCheckedFixit &onCheckedFixit); Diagnostic diagnostic() const { return m_diagnostic; } bool applyFixits() const { return m_applyFixits; } @@ -56,6 +59,7 @@ private: private: const Diagnostic m_diagnostic; bool m_applyFixits = false; + OnCheckedFixit m_onCheckedFixit; }; class ClangToolsDiagnosticModel : public Utils::TreeModel<> @@ -71,6 +75,12 @@ public: enum ItemRole { DiagnosticRole = Debugger::DetailedErrorView::FullTextRole + 1 }; + +signals: + void fixItsToApplyCountChanged(int count); + +private: + int m_fixItsToApplyCount = 0; }; class DiagnosticFilterModel : public QSortFilterProxyModel