File indexing completed on 2024-04-28 05:41:32

0001 /*
0002     This file is part of KCachegrind.
0003 
0004     SPDX-FileCopyrightText: 2003-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only
0007 */
0008 
0009 /*
0010  * Items of event type view.
0011  */
0012 
0013 #include "eventtypeitem.h"
0014 
0015 #include <QPixmap>
0016 
0017 #include "globalconfig.h"
0018 #include "listutils.h"
0019 
0020 
0021 // EventTypeItem
0022 
0023 
0024 EventTypeItem::EventTypeItem(TraceCostItem* costItem,
0025                              EventType* ct, ProfileContext::Type gt)
0026 {
0027     _costItem = costItem;
0028     _eventType = ct;
0029     _groupType = gt;
0030 
0031     setTextAlignment(1, Qt::AlignRight);
0032     setTextAlignment(2, Qt::AlignRight);
0033     setTextAlignment(3, Qt::AlignRight);
0034     setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
0035 
0036     if (ct) {
0037         setText(0, ct->longName());
0038         setText(3, ct->name());
0039         setText(5, ct->parsedFormula());
0040         if (!ct->isReal()) {
0041             setText(4, QStringLiteral("="));
0042             // we have a virtual type: allow editing
0043             // FIXME: How to enable this only for columns 0,3,5 ?!
0044             setFlags(flags() | Qt::ItemIsEditable);
0045         }
0046     }
0047     else {
0048         setText(0, QObject::tr("Unknown Type"));
0049     }
0050     update();
0051 }
0052 
0053 void EventTypeItem::setGroupType(ProfileContext::Type gt)
0054 {
0055     if (_groupType == gt) return;
0056 
0057     _groupType = gt;
0058     update();
0059 }
0060 
0061 void EventTypeItem::update()
0062 {
0063     TraceData* d = _costItem ? _costItem->data() : nullptr;
0064     double total = d ? ((double)d->subCost(_eventType)) : 0.0;
0065 
0066     if (total == 0.0) {
0067         setText(1, QStringLiteral("-"));
0068         setIcon(1, QIcon());
0069         setText(2, QStringLiteral("-"));
0070         setIcon(2, QIcon());
0071         return;
0072     }
0073 
0074     TraceFunction* f = (_costItem && _costItem->type()==ProfileContext::Function) ?
0075                            (TraceFunction*)_costItem : nullptr;
0076 
0077     ProfileCostArray* selfTotalCost = f ? f->data() : d;
0078     if (f && GlobalConfig::showExpanded()) {
0079         ProfileCostArray* parent = nullptr;
0080         switch(_groupType) {
0081         case ProfileContext::Object:        parent = f->object(); break;
0082         case ProfileContext::Class:         parent = f->cls(); break;
0083         case ProfileContext::File:          parent = f->file(); break;
0084         case ProfileContext::FunctionCycle: parent = f->cycle(); break;
0085         default: break;
0086         }
0087         if (parent) selfTotalCost = parent;
0088     }
0089     if (_costItem && _costItem->type()==ProfileContext::FunctionCycle) {
0090         f = (TraceFunction*)_costItem;
0091         selfTotalCost = f->data();
0092     }
0093 
0094     double selfTotal = selfTotalCost->subCost(_eventType);
0095 
0096     // for all cost items there is a self cost
0097     _pure = _costItem ? _costItem->subCost(_eventType) : SubCost(0);
0098     double pure  = 100.0 * _pure / selfTotal;
0099     if (GlobalConfig::showPercentage()) {
0100         setText(2, QStringLiteral("%1")
0101                 .arg(pure, 0, 'f', GlobalConfig::percentPrecision()));
0102     }
0103     else if (_costItem)
0104         setText(2, _costItem->prettySubCost(_eventType));
0105 
0106     setIcon(2, QIcon(costPixmap(_eventType, _costItem, selfTotal, false)));
0107 
0108     if (!f) {
0109         setText(1, QStringLiteral("-"));
0110         setIcon(1, QIcon());
0111         return;
0112     }
0113 
0114     _sum = f->inclusive()->subCost(_eventType);
0115     double sum  = 100.0 * _sum / total;
0116     if (GlobalConfig::showPercentage()) {
0117         setText(1, QStringLiteral("%1")
0118                 .arg(sum, 0, 'f', GlobalConfig::percentPrecision()));
0119     }
0120     else
0121         setText(1, _sum.pretty());
0122 
0123     setIcon(1, QIcon(costPixmap(_eventType, f->inclusive(), total, false)));
0124 }
0125 
0126 bool EventTypeItem::operator<(const QTreeWidgetItem &other) const
0127 {
0128     int col = treeWidget()->sortColumn();
0129     EventTypeItem* o = (EventTypeItem*) &other;
0130     if (col==0)
0131         return _sum < o->_sum;
0132     if (col==1)
0133         return _pure < o->_pure;
0134 
0135     return QTreeWidgetItem::operator<(other);
0136 }
0137 
0138 QVariant EventTypeItem::data(int column, int role) const
0139 {
0140     if ((column == 5) && (role == Qt::EditRole))
0141         return QVariant(_eventType->formula());
0142     return QTreeWidgetItem::data(column, role);
0143 }
0144 
0145