File indexing completed on 2024-05-19 05:44:26

0001 /*
0002     SPDX-FileCopyrightText: 2016-2017 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "topproxy.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 namespace {
0012 TreeModel::Columns toSource(TopProxy::Type type)
0013 {
0014     switch (type) {
0015     case TopProxy::Peak:
0016         return TreeModel::PeakColumn;
0017     case TopProxy::Leaked:
0018         return TreeModel::LeakedColumn;
0019     case TopProxy::Allocations:
0020         return TreeModel::AllocationsColumn;
0021     case TopProxy::Temporary:
0022         return TreeModel::TemporaryColumn;
0023     }
0024     Q_UNREACHABLE();
0025 }
0026 }
0027 
0028 TopProxy::TopProxy(Type type, QObject* parent)
0029     : QSortFilterProxyModel(parent)
0030     , m_type(type)
0031 {
0032 }
0033 
0034 TopProxy::~TopProxy() = default;
0035 
0036 bool TopProxy::filterAcceptsColumn(int source_column, const QModelIndex& /*source_parent*/) const
0037 {
0038     return source_column == TreeModel::LocationColumn || source_column == toSource(m_type);
0039 }
0040 
0041 bool TopProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
0042 {
0043     if (source_parent.isValid()) {
0044         // only show top rows
0045         return false;
0046     }
0047     const auto index = sourceModel()->index(source_row, toSource(m_type));
0048     const auto cost = index.data(TreeModel::SortRole).toLongLong();
0049     // note: explicitly exclude zero values, which could show up when we diff files
0050     //       and no change was observed (overall) for a given metric
0051     if (!cost || cost < m_costThreshold) {
0052         // don't show rows that didn't leak anything, or didn't trigger any
0053         // temporary allocations
0054         // in general, hide anything that's not really interesting
0055         return false;
0056     }
0057     return true;
0058 }
0059 
0060 void TopProxy::setSourceModel(QAbstractItemModel* sourceModel)
0061 {
0062     QSortFilterProxyModel::setSourceModel(sourceModel);
0063     connect(sourceModel, &QAbstractItemModel::modelReset, this, &TopProxy::updateCostThreshold, Qt::UniqueConnection);
0064     updateCostThreshold();
0065 }
0066 
0067 void TopProxy::updateCostThreshold()
0068 {
0069     // hide anything below 1% of the max cost
0070     m_costThreshold = sourceModel()->index(0, toSource(m_type)).data(TreeModel::MaxCostRole).toLongLong() * 0.01;
0071     invalidate();
0072 }
0073 
0074 #include "moc_topproxy.cpp"