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

0001 /*
0002     SPDX-FileCopyrightText: 2021 Milian Wolff <milian.wolff@kdab.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "suppressionsmodel.h"
0008 
0009 #include <analyze/suppressions.h>
0010 
0011 #include <summarydata.h>
0012 #include <util.h>
0013 
0014 SuppressionsModel::SuppressionsModel(QObject* parent)
0015     : QAbstractTableModel(parent)
0016 {
0017 }
0018 
0019 SuppressionsModel::~SuppressionsModel() = default;
0020 
0021 void SuppressionsModel::setSuppressions(const SummaryData& summaryData)
0022 {
0023     beginResetModel();
0024     m_suppressions = summaryData.suppressions;
0025     m_totalAllocations = summaryData.cost.allocations;
0026     m_totalLeaked = summaryData.cost.leaked;
0027     endResetModel();
0028 }
0029 
0030 int SuppressionsModel::columnCount(const QModelIndex& parent) const
0031 {
0032     if (parent.isValid() || m_suppressions.empty()) {
0033         return 0;
0034     }
0035     return static_cast<int>(Columns::COLUMN_COUNT);
0036 }
0037 
0038 int SuppressionsModel::rowCount(const QModelIndex& parent) const
0039 {
0040     return parent.isValid() ? 0 : static_cast<int>(m_suppressions.size());
0041 }
0042 
0043 QVariant SuppressionsModel::headerData(int section, Qt::Orientation orientation, int role) const
0044 {
0045     if (section < 0 || section >= columnCount() || orientation != Qt::Horizontal || role != Qt::DisplayRole) {
0046         return {};
0047     }
0048 
0049     switch (static_cast<Columns>(section)) {
0050     case Columns::Matches:
0051         return tr("Matches");
0052     case Columns::Leaked:
0053         return tr("Leaked");
0054     case Columns::Pattern:
0055         return tr("Pattern");
0056     case Columns::COLUMN_COUNT:
0057         break;
0058     }
0059     return {};
0060 }
0061 
0062 QVariant SuppressionsModel::data(const QModelIndex& index, int role) const
0063 {
0064     if (!index.isValid() || index.parent().isValid() || index.column() >= columnCount() || index.row() >= rowCount()) {
0065         return {};
0066     }
0067 
0068     const auto& suppression = m_suppressions[index.row()];
0069 
0070     if (role == Qt::ToolTipRole) {
0071         return tr("<qt>Suppression rule: <code>%1</code><br/>"
0072                   "Matched Allocations: %2<br/>&nbsp;&nbsp;%3% out of %4 total<br/>"
0073                   "Suppressed Leaked Memory: %5<br/>&nbsp;&nbsp;%6% out of %7 total</qt>")
0074             .arg(QString::fromStdString(suppression.pattern), QString::number(suppression.matches),
0075                  Util::formatCostRelative(suppression.matches, m_totalAllocations), QString::number(m_totalAllocations),
0076                  Util::formatBytes(suppression.leaked), Util::formatCostRelative(suppression.leaked, m_totalLeaked),
0077                  Util::formatBytes(m_totalLeaked));
0078     }
0079 
0080     switch (static_cast<Columns>(index.column())) {
0081     case Columns::Matches:
0082         if (role == Qt::DisplayRole || role == SortRole) {
0083             return static_cast<quint64>(suppression.matches);
0084         } else if (role == Qt::InitialSortOrderRole) {
0085             return Qt::DescendingOrder;
0086         } else if (role == TotalCostRole) {
0087             return m_totalAllocations;
0088         }
0089         break;
0090     case Columns::Leaked:
0091         if (role == Qt::DisplayRole) {
0092             return Util::formatBytes(suppression.leaked);
0093         } else if (role == SortRole) {
0094             return static_cast<qint64>(suppression.leaked);
0095         } else if (role == Qt::InitialSortOrderRole) {
0096             return Qt::DescendingOrder;
0097         } else if (role == TotalCostRole) {
0098             return m_totalLeaked;
0099         }
0100         break;
0101     case Columns::Pattern:
0102         if (role == Qt::DisplayRole || role == SortRole) {
0103             return QString::fromStdString(suppression.pattern);
0104         } else if (role == Qt::InitialSortOrderRole) {
0105             return Qt::AscendingOrder;
0106         }
0107         break;
0108     case Columns::COLUMN_COUNT:
0109         break;
0110     }
0111 
0112     return {};
0113 }
0114 
0115 #include "moc_suppressionsmodel.cpp"