1 /*
2     This file is part of KCachegrind.
3 
4     SPDX-FileCopyrightText: 2003-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
5 
6     SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 /*
10  * Items of stack dockable.
11  */
12 
13 #include "stackitem.h"
14 
15 #include <QPixmap>
16 
17 #include "globalguiconfig.h"
18 #include "listutils.h"
19 #include "stackselection.h"
20 
21 
22 // StackItem
23 
StackItem(StackSelection * ss,QTreeWidget * parent,TraceFunction * f)24 StackItem::StackItem(StackSelection* ss,
25                      QTreeWidget* parent, TraceFunction* f)
26     :QTreeWidgetItem(parent)
27 {
28     _view = ss;
29     _function = f;
30     _call = nullptr;
31 
32     setTextAlignment(0, Qt::AlignRight);
33     setTextAlignment(1, Qt::AlignRight);
34     setTextAlignment(2, Qt::AlignRight);
35 
36     updateGroup();
37     updateCost();
38 
39     setText(2, QStringLiteral("-- "));
40     setText(3, f->prettyName());
41 }
42 
StackItem(StackSelection * ss,QTreeWidget * parent,TraceCall * call)43 StackItem::StackItem(StackSelection* ss,
44                      QTreeWidget* parent, TraceCall* call)
45     :QTreeWidgetItem(parent)
46 {
47     _view = ss;
48     _call = call;
49     _function = call->called();
50 
51     setTextAlignment(0, Qt::AlignRight);
52     setTextAlignment(1, Qt::AlignRight);
53     setTextAlignment(2, Qt::AlignRight);
54 
55     updateGroup();
56     updateCost();
57 
58     setText(3, _function->prettyName());
59 }
60 
61 
updateGroup()62 void StackItem::updateGroup()
63 {
64     QColor c = GlobalGUIConfig::functionColor(_view->groupType(),
65                                               _function);
66     setIcon(3, colorPixmap(10, 10, c));
67 }
68 
updateCost()69 void StackItem::updateCost()
70 {
71     if (!_call) return;
72 
73     setText(2, _call->prettyCallCount());
74 
75     EventType* ct = _view->eventType();
76     _sum = _call->subCost(ct);
77     double total = _call->called()->data()->subCost(ct);
78     if (total == 0.0) {
79         setText(0, QStringLiteral("-"));
80         setIcon(0, QPixmap());
81     }
82     else {
83         double sum  = 100.0 * _sum / total;
84 
85         if (GlobalConfig::showPercentage())
86             setText(0, QStringLiteral("%1")
87                     .arg(sum, 0, 'f', GlobalConfig::percentPrecision()));
88         else
89             setText(0, _call->prettySubCost(ct));
90 
91         setIcon(0, costPixmap(ct, _call, total, false));
92     }
93 
94     // if _eventType2 is 0, column1 is hidden, no change needed
95     EventType* ct2 = _view->eventType2();
96     if (!ct2) return;
97 
98     _sum = _call->subCost(ct2);
99     total = _call->called()->data()->subCost(ct2);
100     if (total == 0.0) {
101         setText(1, QStringLiteral("-"));
102         setIcon(1, QPixmap());
103     }
104     else {
105         double sum  = 100.0 * _sum / total;
106 
107         if (GlobalConfig::showPercentage())
108             setText(1, QStringLiteral("%1")
109                     .arg(sum, 0, 'f', GlobalConfig::percentPrecision()));
110         else
111             setText(1, _call->prettySubCost(ct2));
112 
113         setIcon(1, costPixmap(ct2, _call, total, false));
114     }
115 }
116