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

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 #include "partlistitem.h"
0010 
0011 #include <math.h>
0012 
0013 #include <QPixmap>
0014 
0015 #include "listutils.h"
0016 #include "coverage.h"
0017 #include "globalconfig.h"
0018 
0019 
0020 // PartListItem
0021 
0022 PartListItem::PartListItem(QTreeWidget* parent, TraceCostItem* costItem,
0023                            EventType* et, ProfileContext::Type gt,
0024                            TracePart* part)
0025     :QTreeWidgetItem(parent)
0026 {
0027     _partCostItem = costItem->findDepFromPart(part);
0028     _part = part;
0029     _groupType = gt;
0030     _eventType = et;
0031 
0032     setTextAlignment(0, Qt::AlignRight);
0033     setTextAlignment(1, Qt::AlignRight);
0034     setTextAlignment(2, Qt::AlignRight);
0035 
0036     setText(0, _part->prettyName());
0037 
0038     if (_part->trigger().isEmpty())
0039         setText(4, QObject::tr("(none)"));
0040     else
0041         setText(4, _part->trigger());
0042 
0043     update();
0044 }
0045 
0046 void PartListItem::setEventType(EventType* et)
0047 {
0048     if (_eventType == et) return;
0049 
0050     _eventType = et;
0051     update();
0052 }
0053 
0054 void PartListItem::setGroupType(ProfileContext::Type gt)
0055 {
0056     if (_groupType == gt) return;
0057 
0058     _groupType = gt;
0059     update();
0060 }
0061 
0062 void PartListItem::update()
0063 {
0064     TracePartFunction* pf;
0065     pf = !_partCostItem ? nullptr :
0066                           (_partCostItem->type()==ProfileContext::PartFunction) ?
0067                               ((TracePartFunction*)_partCostItem) : nullptr;
0068 
0069     double total = _part->subCost(_eventType);
0070 
0071     ProfileCostArray* selfTotalCost = _part;
0072     if (pf && GlobalConfig::showExpanded()) {
0073         switch(_groupType) {
0074         case ProfileContext::Object: selfTotalCost = pf->partObject(); break;
0075         case ProfileContext::Class:  selfTotalCost = pf->partClass(); break;
0076         case ProfileContext::File:   selfTotalCost = pf->partFile(); break;
0077         default: break;
0078         }
0079     }
0080     double selfTotal = selfTotalCost->subCost(_eventType);
0081 
0082     _pure = _partCostItem ? _partCostItem->subCost(_eventType) : SubCost(0);
0083     _sum = pf ? pf->inclusive()->subCost(_eventType) : SubCost(0);
0084 
0085     if (selfTotal == 0 || !_partCostItem) {
0086         setText(2, QStringLiteral("-"));
0087         setIcon(2, QPixmap());
0088     }
0089     else {
0090         double pure  = 100.0 * _pure / selfTotal;
0091         if (GlobalConfig::showPercentage()) {
0092             setText(2, QStringLiteral("%1")
0093                     .arg(pure, 0, 'f', GlobalConfig::percentPrecision()));
0094         }
0095         else
0096             setText(2, _partCostItem->prettySubCost(_eventType));
0097 
0098         setIcon(2, costPixmap(_eventType, _partCostItem, selfTotal, false));
0099     }
0100 
0101     if (total == 0 || !pf) {
0102         setText(1, QStringLiteral("-"));
0103         setIcon(1, QPixmap());
0104     }
0105     else {
0106         double sum  = 100.0 * _sum / total;
0107         if (GlobalConfig::showPercentage()) {
0108             setText(1, QStringLiteral("%1")
0109                     .arg(sum, 0, 'f', GlobalConfig::percentPrecision()));
0110         }
0111         else
0112             setText(1, _sum.pretty());
0113 
0114         setIcon(1, costPixmap(_eventType, pf->inclusive(), total, false));
0115     }
0116 
0117     if (!pf) {
0118         setText(3, QStringLiteral("-"));
0119         _callCount = 0;
0120         return;
0121     }
0122 
0123     SubCost callCount;
0124     QString str;
0125 
0126     callCount = 0;
0127     foreach(TracePartCall* pc, pf->partCallers())
0128         callCount += pc->callCount();
0129 
0130     if ((callCount == 0) && (pf->calledContexts()>0))
0131         str = QObject::tr("(active)");
0132     else
0133         str = callCount.pretty();
0134 
0135     _callCount = callCount;
0136     setText(3, str);
0137 }
0138 
0139 bool PartListItem::operator<(const QTreeWidgetItem& other) const
0140 {
0141     const PartListItem* pi1 = this;
0142     const PartListItem* pi2 = (PartListItem*) &other;
0143     int col = treeWidget()->sortColumn();
0144 
0145     if (col==0)
0146         return (*(pi1->_part) < *(pi2->_part));
0147 
0148     if (col==1)
0149         return (pi1->_sum < pi2->_sum);
0150 
0151     if (col==2)
0152         return (pi1->_pure < pi2->_pure);
0153 
0154     if (col==3)
0155         return (pi1->_callCount < pi2->_callCount);
0156 
0157     return QTreeWidgetItem::operator <(other);
0158 }