File indexing completed on 2024-05-19 05:44:24
0001 /* 0002 SPDX-FileCopyrightText: 2015-2017 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "histogrammodel.h" 0008 0009 #include <KChartGlobal> 0010 0011 #include <KLocalizedString> 0012 0013 #include <QBrush> 0014 #include <QColor> 0015 #include <QPen> 0016 0017 #include <limits> 0018 0019 #include <util.h> 0020 0021 namespace { 0022 QColor colorForColumn(int column, int columnCount) 0023 { 0024 return QColor::fromHsv((double(column) / columnCount) * 255, 255, 255); 0025 } 0026 } 0027 0028 HistogramModel::HistogramModel(QObject* parent) 0029 : QAbstractTableModel(parent) 0030 { 0031 qRegisterMetaType<HistogramData>("HistogramData"); 0032 } 0033 0034 HistogramModel::~HistogramModel() = default; 0035 0036 QVariant HistogramModel::headerData(int section, Qt::Orientation orientation, int role) const 0037 { 0038 if (orientation == Qt::Vertical && role == Qt::DisplayRole && section >= 0 && section < m_data.rows.size()) { 0039 return m_data.rows.at(section).sizeLabel; 0040 } 0041 return {}; 0042 } 0043 0044 QVariant HistogramModel::data(const QModelIndex& index, int role) const 0045 { 0046 if (!hasIndex(index.row(), index.column(), index.parent())) { 0047 return {}; 0048 } 0049 if (role == KChart::DatasetBrushRole) { 0050 return QVariant::fromValue(QBrush(colorForColumn(index.column(), columnCount()))); 0051 } else if (role == KChart::DatasetPenRole) { 0052 return QVariant::fromValue(QPen(Qt::black)); 0053 } 0054 0055 if (role != Qt::DisplayRole && role != Qt::ToolTipRole) { 0056 return {}; 0057 } 0058 0059 const auto& row = m_data.rows.at(index.row()); 0060 const auto& column = row.columns[index.column()]; 0061 if (role == Qt::ToolTipRole) { 0062 if (index.column() == 0) { 0063 return i18n("%1 allocations in total", column.allocations); 0064 } 0065 return i18n("%1 allocations from %2, totalling %3 allocated with an average of %4 per allocation", 0066 column.allocations, Util::toString(column.symbol, *m_data.resultData, Util::Long), 0067 Util::formatBytes(column.totalAllocated), 0068 Util::formatBytes(column.totalAllocated / column.allocations)); 0069 } 0070 return column.allocations; 0071 } 0072 0073 int HistogramModel::columnCount(const QModelIndex& parent) const 0074 { 0075 if (parent.isValid()) { 0076 return 0; 0077 } 0078 return HistogramRow::NUM_COLUMNS; 0079 } 0080 0081 int HistogramModel::rowCount(const QModelIndex& parent) const 0082 { 0083 if (parent.isValid()) { 0084 return 0; 0085 } 0086 return m_data.rows.size(); 0087 } 0088 0089 void HistogramModel::resetData(const HistogramData& data) 0090 { 0091 Q_ASSERT(data.resultData); 0092 beginResetModel(); 0093 m_data = data; 0094 endResetModel(); 0095 } 0096 0097 void HistogramModel::clearData() 0098 { 0099 beginResetModel(); 0100 m_data = {}; 0101 endResetModel(); 0102 } 0103 0104 #include "moc_histogrammodel.cpp"