File indexing completed on 2024-04-21 04:35:59

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 2011 Lucas Sarie <lucas.sarie@gmail.com>
0006  * Copyright 2017 Anton Anikin <anton@anikin.xyz>
0007 
0008  This program is free software; you can redistribute it and/or
0009  modify it under the terms of the GNU General Public
0010  License as published by the Free Software Foundation; either
0011  version 2 of the License, or (at your option) any later version.
0012 
0013  This program is distributed in the hope that it will be useful,
0014  but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016  General Public License for more details.
0017 
0018  You should have received a copy of the GNU General Public License
0019  along with this program; see the file COPYING.  If not, write to
0020  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021  Boston, MA 02110-1301, USA.
0022 */
0023 
0024 #include "cachegrind_model.h"
0025 
0026 #include "debug.h"
0027 #include "utils.h"
0028 
0029 #include <qtcompat_p.h>
0030 
0031 #include <KLocalizedString>
0032 #include <KMessageBox>
0033 
0034 #include <QFontDatabase>
0035 
0036 namespace Valgrind
0037 {
0038 
0039 CachegrindFunctionsModel::CachegrindFunctionsModel(QObject* parent)
0040     : QAbstractTableModel(parent)
0041     , m_totalItem(nullptr)
0042     , m_percentageValues(false)
0043 {
0044 }
0045 
0046 CachegrindFunctionsModel::~CachegrindFunctionsModel()
0047 {
0048     qDeleteAll(m_items);
0049 }
0050 
0051 void CachegrindFunctionsModel::addItem(CachegrindFunction* newItem, bool isTotal)
0052 {
0053     Q_ASSERT(newItem);
0054     if (isTotal) {
0055         m_totalItem = newItem;
0056         return;
0057     }
0058     bool exists = false;
0059     for (auto item : qAsConst(m_items)) {
0060         if (item->functionName == newItem->functionName) {
0061             exists = true;
0062 
0063             item->fileNames += newItem->fileNames;
0064             item->fileNames.removeDuplicates();
0065             item->fileNames.sort();
0066 
0067             for (int i = 0; i < newItem->eventValues.size(); ++i) {
0068                 item->eventValues[i] += newItem->eventValues[i];
0069             }
0070 
0071             delete newItem;
0072             break;
0073         }
0074     }
0075 
0076     if (!exists) {
0077         m_items.append(newItem);
0078     }
0079 }
0080 
0081 void CachegrindFunctionsModel::setEventsList(const QStringList& eventsList)
0082 {
0083     m_eventList = eventsList;
0084 }
0085 
0086 void CachegrindFunctionsModel::setPercentageValues(bool value)
0087 {
0088     m_percentageValues = value;
0089     emitDataChanged(this);
0090 }
0091 
0092 QModelIndex CachegrindFunctionsModel::index(int row, int column, const QModelIndex&) const
0093 {
0094     if (row >= 0 && row < rowCount() && column >= 0 && column < columnCount()) {
0095         return createIndex(row, column, m_items.at(row));
0096     }
0097 
0098     return QModelIndex();
0099 }
0100 
0101 int CachegrindFunctionsModel::rowCount(const QModelIndex&) const
0102 {
0103     return m_items.size();
0104 }
0105 
0106 int CachegrindFunctionsModel::columnCount(const QModelIndex&) const
0107 {
0108     return m_eventList.size() + 1;
0109 }
0110 
0111 QVariant CachegrindFunctionsModel::data(const QModelIndex& index, int role) const
0112 {
0113     Q_ASSERT(m_totalItem);
0114 
0115     if (!index.isValid()) {
0116         return QVariant();
0117     }
0118 
0119     auto item = static_cast<CachegrindFunction*>(index.internalPointer());
0120     int column = index.column();
0121 
0122     if (role == Qt::TextAlignmentRole && column > 0) {
0123         return rightAlign;
0124     }
0125 
0126     if (column == 0) {
0127         if (role == Qt::DisplayRole || role == SortRole) {
0128             return item->functionName;
0129         }
0130     }
0131 
0132     else {
0133         if (role == Qt::DisplayRole) {
0134             int intValue = item->eventValues.at(column - 1);
0135 
0136             if (!m_percentageValues) {
0137                 return displayValue(intValue);
0138             }
0139 
0140             return displayValue(intValue * 100.0 / m_totalItem->eventValues.at(column - 1));
0141         }
0142 
0143         if (role == SortRole) {
0144             return item->eventValues.at(column - 1);
0145         }
0146     }
0147 
0148     return QVariant();
0149 }
0150 
0151 QVariant CachegrindFunctionsModel::headerData(int section, Qt::Orientation, int role) const
0152 {
0153     if (section == 0) {
0154         if (role == Qt::DisplayRole) {
0155             return i18n("Function");
0156         }
0157     }
0158 
0159     else {
0160         if (role == Qt::DisplayRole) {
0161             return m_eventList[section - 1];
0162         }
0163 
0164         if (role == Qt::ToolTipRole) {
0165             return eventFullName(m_eventList[section - 1]);
0166         }
0167     }
0168 
0169     return QVariant();
0170 }
0171 
0172 }