File indexing completed on 2024-05-05 13:02:05
0001 /* This file is part of KDevelop 0002 * Copyright 2011 Mathieu Lornac <mathieu.lornac@gmail.com> 0003 * Copyright 2011 Damien Coppel <damien.coppel@gmail.com> 0004 * Copyright 2011 Lionel Duc <lionel.data@gmail.com> 0005 * Copyright 2017 Anton Anikin <anton@anikin.xyz> 0006 0007 This program is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU General Public 0009 License as published by the Free Software Foundation; either 0010 version 2 of the License, or (at your option) any later version. 0011 0012 This program is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 General Public License for more details. 0016 0017 You should have received a copy of the GNU General Public License 0018 along with this program; see the file COPYING. If not, write to 0019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0020 Boston, MA 02110-1301, USA. 0021 */ 0022 0023 #include "massif_model.h" 0024 0025 #include "debug.h" 0026 #include "massif_snapshot.h" 0027 0028 #include <KLocalizedString> 0029 0030 #include <QFontDatabase> 0031 0032 namespace Valgrind 0033 { 0034 0035 MassifSnapshotsModel::MassifSnapshotsModel(QObject* parent) 0036 : QAbstractTableModel(parent) 0037 { 0038 } 0039 0040 MassifSnapshotsModel::~MassifSnapshotsModel() 0041 { 0042 qDeleteAll(m_snapshots); 0043 } 0044 0045 void MassifSnapshotsModel::addSnapshot(MassifSnapshot* snapshot) 0046 { 0047 Q_ASSERT(snapshot); 0048 if (snapshot) { 0049 m_snapshots.append(snapshot); 0050 } 0051 } 0052 0053 QModelIndex MassifSnapshotsModel::index(int row, int column, const QModelIndex&) const 0054 { 0055 if (row >= 0 && row < rowCount() && column >= 0 && column < columnCount()) { 0056 return createIndex(row, column, m_snapshots.at(row)); 0057 } 0058 0059 return QModelIndex(); 0060 } 0061 0062 int MassifSnapshotsModel::rowCount(const QModelIndex&) const 0063 { 0064 return m_snapshots.size(); 0065 } 0066 0067 int MassifSnapshotsModel::columnCount(const QModelIndex&) const 0068 { 0069 return 5; 0070 } 0071 0072 QString humanSize(int byteSize) 0073 { 0074 static const QStringList units{ "KiB", "MiB", "GiB", "TiB" }; 0075 0076 if (byteSize < 1024) { 0077 return QString::number(byteSize); 0078 } 0079 0080 float size = byteSize; 0081 QStringListIterator i(units); 0082 QString unit; 0083 0084 while (size >= 1024.0 && i.hasNext()) { 0085 unit = i.next(); 0086 size /= 1024.0; 0087 } 0088 0089 return QStringLiteral("%1 %2").arg(size, 0, 'f', 1).arg(unit); 0090 } 0091 0092 QVariant MassifSnapshotsModel::data(const QModelIndex& index, int role) const 0093 { 0094 auto snapshot = static_cast<MassifSnapshot*>(index.internalPointer()); 0095 if (!snapshot) { 0096 return QVariant(); 0097 } 0098 0099 if (role == Qt::DisplayRole) { 0100 if (index.column() <= MassifSnapshot::Time) { 0101 return snapshot->values[index.column()]; 0102 } 0103 0104 return humanSize(snapshot->values[index.column()].toInt()); 0105 } 0106 0107 if (role == Qt::FontRole) { 0108 QFont f = QFontDatabase::systemFont(QFontDatabase::GeneralFont); 0109 if (!snapshot->heapTree.isEmpty()) { 0110 f.setBold(true); 0111 } 0112 return f; 0113 } 0114 0115 return QVariant(); 0116 } 0117 0118 QVariant MassifSnapshotsModel::headerData(int section, Qt::Orientation orientation, int role) const 0119 { 0120 Q_UNUSED(orientation) 0121 0122 if (role == Qt::DisplayRole) { 0123 switch (section) { 0124 0125 case MassifSnapshot::SnapshotId: 0126 return i18n("Snapshot"); 0127 0128 case MassifSnapshot::Time: 0129 return i18n("Time"); 0130 0131 case MassifSnapshot::Heap: 0132 return i18n("Heap"); 0133 0134 case MassifSnapshot::HeapExtra: 0135 return i18n("Heap (extra)"); 0136 0137 case MassifSnapshot::Stack: 0138 return i18n("Stack"); 0139 0140 } 0141 } 0142 0143 return QVariant(); 0144 } 0145 0146 }