1 /*
2     This file is part of KCachegrind.
3 
4     SPDX-FileCopyrightText: 2002-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
5 
6     SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 #include "costlistitem.h"
10 
11 #include <math.h>
12 
13 #include <QPixmap>
14 
15 #include "listutils.h"
16 #include "coverage.h"
17 #include "globalguiconfig.h"
18 
19 
20 // CostListItem
21 
22 
CostListItem(QTreeWidget * parent,TraceCostItem * costItem,EventType * et,int size)23 CostListItem::CostListItem(QTreeWidget* parent, TraceCostItem* costItem,
24                            EventType* et, int size)
25     :QTreeWidgetItem(parent)
26 {
27     _groupSize = size;
28     _skipped = 0;
29     _costItem = costItem;
30     setEventType(et);
31 
32     setTextAlignment(0, Qt::AlignRight);
33 
34     if (costItem) {
35         updateName();
36         setIcon(1, colorPixmap(10, 10,
37                                GlobalGUIConfig::groupColor(_costItem)));
38     }
39 }
40 
CostListItem(QTreeWidget * parent,int skipped,TraceCostItem * costItem,EventType * et)41 CostListItem::CostListItem(QTreeWidget* parent, int skipped,
42                            TraceCostItem* costItem, EventType* et)
43     :QTreeWidgetItem(parent)
44 {
45     _skipped = skipped;
46     _costItem = costItem;
47     setEventType(et);
48 
49     setTextAlignment(0, Qt::AlignRight);
50 
51     //~ singular (%n item skipped)
52     //~ plural (%n items skipped)
53     setText(1, QObject::tr("(%n item(s) skipped)", "", _skipped));
54 }
55 
setEventType(EventType * et)56 void CostListItem::setEventType(EventType* et)
57 {
58     _eventType = et;
59     update();
60 }
61 
updateName()62 void CostListItem::updateName()
63 {
64     if (!_costItem) return;
65 
66     QString n = _costItem->prettyName();
67     if (_groupSize>=0) n += QStringLiteral(" (%1)").arg(_groupSize);
68 
69     setText(1, n);
70 }
71 
setSize(int s)72 void CostListItem::setSize(int s)
73 {
74     _groupSize = s;
75     updateName();
76 }
77 
update()78 void CostListItem::update()
79 {
80     if (!_costItem) return;
81     TraceData* d = _costItem->data();
82 
83     double total = d->subCost(_eventType);
84     if (total == 0.0) {
85         setText(0, QStringLiteral("---"));
86         setIcon(0, QPixmap());
87         return;
88     }
89 
90     _pure = _costItem->subCost(_eventType);
91     double pure  = 100.0 * _pure / total;
92     QString str;
93     if (GlobalConfig::showPercentage())
94         str = QStringLiteral("%1").arg(pure, 0, 'f', GlobalConfig::percentPrecision());
95     else
96         str = _costItem->prettySubCost(_eventType);
97 
98     if (_skipped) {
99         // special handling for skip entries...
100         setText(0, QStringLiteral("< %1").arg(str));
101         return;
102     }
103 
104     setText(0, str);
105     setIcon(0, costPixmap(_eventType, _costItem, total, false));
106 }
107 
operator <(const QTreeWidgetItem & other) const108 bool CostListItem::operator< ( const QTreeWidgetItem & other ) const
109 {
110     const CostListItem* fi1 = this;
111     const CostListItem* fi2 = (CostListItem*) &other;
112     int col = treeWidget()->sortColumn();
113 
114     // a skip entry is always sorted last
115     if (fi1->_skipped) return true;
116     if (fi2->_skipped) return false;
117 
118     if (col==0)
119         return (fi1->_pure < fi2->_pure);
120 
121     return QTreeWidgetItem::operator <(other);
122 }
123