From 786e5774d9be93658282d1578448e295cb1347dc Mon Sep 17 00:00:00 2001 From: Lieven Hey Date: Wed, 6 Oct 2021 15:52:12 +0200 Subject: [PATCH] add filtering to diff view --- src/diffviewwidget.cpp | 24 +++++++++++++++++++++--- src/models/CMakeLists.txt | 1 + src/models/diffviewproxy.cpp | 27 +++++++++++++++++++++++++++ src/models/diffviewproxy.h | 20 ++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/models/diffviewproxy.cpp create mode 100644 src/models/diffviewproxy.h diff --git a/src/diffviewwidget.cpp b/src/diffviewwidget.cpp index 728d31d20..853df7ada 100644 --- a/src/diffviewwidget.cpp +++ b/src/diffviewwidget.cpp @@ -1,6 +1,7 @@ #include "diffviewwidget.h" #include +#include #include #include #include @@ -8,6 +9,7 @@ #include #include +#include "diffviewproxy.h" #include "dockwidgetsetup.h" #include "filterandzoomstack.h" #include "models/treemodel.h" @@ -31,11 +33,13 @@ DiffViewWidget::DiffViewWidget(QWidget* parent) , m_timelineA(new TimeLineWidget(m_parserA, m_filterMenu, m_filterAndZoomStackA, this)) , m_timelineB(new TimeLineWidget(m_parserB, m_filterMenu, m_filterAndZoomStackB, this)) { + auto diffProxy = new DiffViewProxy(this); + diffProxy->setSourceModel(m_model); + ui->setupUi(this); - ui->diffTreeView->setModel(m_model); - ui->bottomUpVerticalLayout->addWidget(m_contents); - ResultsUtil::setupTreeView(ui->diffTreeView, ui->diffSearch, m_model); + ResultsUtil::setupTreeView(ui->diffTreeView, ui->diffSearch, diffProxy, DiffViewModel::InitialSortColumn, + DiffViewModel::SortRole); ResultsUtil::setupCostDelegate(m_model, ui->diffTreeView); auto dockify = [](QWidget* widget, const QString& id, const QString& title, const QString& shortcut) { @@ -51,6 +55,20 @@ DiffViewWidget::DiffViewWidget(QWidget* parent) m_timelineDockB = dockify(m_timelineB, QStringLiteral("timelineb"), tr("Timeline B"), tr("Ctrl+B")); m_timelineDockA->addDockWidgetAsTab(m_timelineDockB); + auto costThreshold = new QDoubleSpinBox(this); + costThreshold->setDecimals(2); + costThreshold->setMinimum(0); + costThreshold->setMaximum(99.90); + costThreshold->setPrefix(tr("Cost Threshold: ")); + costThreshold->setSuffix(QStringLiteral("%")); + costThreshold->setValue(0.01); + costThreshold->setSingleStep(0.01); + connect(costThreshold, static_cast(&QDoubleSpinBox::valueChanged), this, + [diffProxy](double threshold) { diffProxy->setThreshhold(threshold); }); + + ui->bottomUpVerticalLayout->addWidget(costThreshold); + ui->bottomUpVerticalLayout->addWidget(m_contents); + auto repositionFilterBusyIndicator = [this] { auto geometry = m_filterBusyIndicator->geometry(); geometry.setWidth(width() / 2); diff --git a/src/models/CMakeLists.txt b/src/models/CMakeLists.txt index 96f22d5ed..85aecc43b 100644 --- a/src/models/CMakeLists.txt +++ b/src/models/CMakeLists.txt @@ -16,6 +16,7 @@ add_library(models STATIC ../settings.cpp ../util.cpp callercalleeproxy.cpp + diffviewproxy.cpp ) target_link_libraries(models diff --git a/src/models/diffviewproxy.cpp b/src/models/diffviewproxy.cpp new file mode 100644 index 000000000..97babdce1 --- /dev/null +++ b/src/models/diffviewproxy.cpp @@ -0,0 +1,27 @@ +#include "diffviewproxy.h" +#include "treemodel.h" + +DiffViewProxy::DiffViewProxy(QObject* parent) + : QSortFilterProxyModel(parent) +{ + setRecursiveFilteringEnabled(true); +} + +void DiffViewProxy::setThreshhold(double threshhold) +{ + m_threshhold = threshhold; + invalidateFilter(); +} + +bool DiffViewProxy::filterAcceptsRow(int source_row, const QModelIndex& parent) const +{ + Q_UNUSED(parent); + + const auto model = qobject_cast(sourceModel()); + + const auto cost = + model->data(model->index(source_row, DiffViewModel::NUM_BASE_COLUMNS), DiffViewModel::SortRole).toLongLong(); + const auto t = std::abs(cost / 10'000.f); + + return t > m_threshhold; +} diff --git a/src/models/diffviewproxy.h b/src/models/diffviewproxy.h new file mode 100644 index 000000000..31b4cd95d --- /dev/null +++ b/src/models/diffviewproxy.h @@ -0,0 +1,20 @@ +#ifndef DIFFVIEWPROXY_H +#define DIFFVIEWPROXY_H + +#include + +class DiffViewProxy : public QSortFilterProxyModel +{ +public: + explicit DiffViewProxy(QObject* parent = nullptr); + + void setThreshhold(double threshhold); + +protected: + bool filterAcceptsRow(int source_row, const QModelIndex& parent) const override; + +private: + double m_threshhold = 0.01; +}; + +#endif // DIFFVIEWPROXY_H