File: listutils.cpp

package info (click to toggle)
kcachegrind 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 9,964 kB
  • sloc: cpp: 32,149; xml: 403; perl: 325; python: 235; sh: 8; makefile: 6
file content (231 lines) | stat: -rw-r--r-- 5,365 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
    This file is part of KCachegrind.

    SPDX-FileCopyrightText: 2003-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

    SPDX-License-Identifier: GPL-2.0-only
*/

/*
 * Some helper functions for QListViewItem derivates
 */

#include "listutils.h"

#include <QPainter>
#include <QPixmap>

#include "globalguiconfig.h"

#define COSTPIX_WIDTH 25

QPixmap colorPixmap(int w, int h, QColor c)
{
    static QPixmap* pixs[37];
    static QColor cols[37];
    static bool inited = false;

    if (!inited) {
        for (int i=0;i<37;i++) pixs[i]=nullptr;
        inited = true;
    }
    int hash = (w+h+c.red()+c.green()+c.blue()) % 37;
    if (pixs[hash]) {
        if ((pixs[hash]->width() == w) &&
            (pixs[hash]->height() == h) &&
            (cols[hash] == c))
            return *pixs[hash];

        delete pixs[hash];
    }


    QPixmap* pix = new QPixmap(w, h);
    pix->fill(c);
    QPainter p(pix);
    p.setPen(c.lighter());
    p.drawLine(0, 0, w-1, 0);
    p.drawLine(0, 0, 0, h-1);
    p.setPen(c.darker());
    p.drawLine(w-1, 0, w-1, h-1);
    p.drawLine(0, h-1, w-1, h-1);

    pixs[hash] = pix;
    cols[hash] = c;
    return *pix;
}

/**
 * Create a percentage pixmap with a filling rate of p percent (0-100).
 * When withFrame==false, the pixmap is truncated to only the filled portion.
 */
QPixmap percentagePixmap(int w, int h, int percent, QColor c, bool framed)
{
    int iw, ix1, ih, iy1, iy2;

    // at max, draw 100%
    if (percent > 100) percent = 100;

    // inner rectangle to fill with bar
    if (framed) {
        iw = w-2, ix1 = 1;
        ih = h-2, iy1 = 1, iy2 = h-2;
    }
    else {
        iw = w; ix1 = 0;
        ih = h; iy1 = 0; iy2 = h-1;
    }

    int filled = iw*percent/100+1;
    if (!framed) w=filled;
    if (w<3) return QPixmap();

    QPixmap pix(w, h);
    pix.fill(Qt::white);
    QPainter p(&pix);
    p.setPen(Qt::black);
    if (framed)
        p.drawRect(0, 0, w-1, h-1);

    // inside
    p.setPen(Qt::NoPen);
    p.setBrush(c);
    p.drawRect(ix1, iy1, filled-1,ih-1);

    // last right pix column
    int lastY = ih-(filled*ih - iw*ih*percent/100);
    int lastX1 = ix1+filled-2 + ((lastY>1) ? 1: 0);
    int lastX2 = ix1+filled-2;

    // frame
    p.setPen(c.lighter());
    p.drawLine(ix1, iy1, lastX1, iy1);
    p.drawLine(ix1, iy1, ix1, iy2);
    p.setPen(c.darker());
    p.drawLine(lastX1, iy1, lastX1, iy1+lastY);
    p.drawLine(lastX2, iy1+lastY, lastX2, iy2);
    p.drawLine(ix1+1, iy2, lastX2, iy2);

    return pix;
}

inline QColor partitionColor(int d, int max)
{
    return QColor::fromHsv((720*d/max) % 360, 255-(128*d/max), 192);
}


QPixmap partitionPixmap(int w, int h,
                        double* hist, EventTypeSet* set, int maxIndex, bool framed)
{
    int lastPos = 0, nextPos;
    double val=0.0, sum=0.0;
    int d, dmin=maxIndex, dmax=0;
    for (d = 0;d<maxIndex;d++)
        if (hist[d]>0.0) {
            sum += hist[d];
            if (dmin>d) dmin = d;
            if (dmax<d) dmax = d;
        }

    // inner rectangle to fill with bar
    int iw, ix1, ih, iy1, iy2;
    if (framed) {
        iw = w-2, ix1 = 1;
        ih = h-2, iy1 = 1, iy2 = h-2;
    }
    else {
        iw = w; ix1 = 0;
        ih = h; iy1 = 0; iy2 = h-1;
    }

    int filled = (int)(iw*sum+1);
    if (!framed) w=filled;
    if (w<3) return QPixmap();

    QPixmap pix(w, h);
    pix.fill(Qt::white);
    QPainter p(&pix);
    p.setPen(Qt::black);
    if (framed)
        p.drawRect(0, 0, w-1, h-1);

    //qDebug("Sum %f, dw %d", sum,dw);

    QColor c, cLast;
    bool leftDrawn = false;
    int x1, x2=0;
    int diff;
    d=dmin;
    while (d<dmax+1) {
        val += hist[d];
        nextPos = (int)(filled * val/sum);

        //qDebug(" hist[%d] %f, val %f, nextPos %d", d, hist[d], val, nextPos);

        diff = nextPos-lastPos;
        if (diff==0) { d++; continue; }

        if (set)
            c = GlobalGUIConfig::eventTypeColor(set->realType(d));
        else
            c = partitionColor(d,maxIndex);

        x1 = ix1+lastPos;
        x2 = ix1+nextPos;
        if (x2>=iw) x2=iw-1;

        // inside
        p.setPen(Qt::NoPen);
        p.setBrush(c);
        p.drawRect(x1, iy1, x2-x1, ih-1);

        // lighter top border
        p.setPen(c.lighter());
        p.drawLine(x1, iy1, x2-1, iy1);

        // when width for last and current distance >2, draw full 3D effect...
        if (!leftDrawn) {
            p.drawLine(x1, iy1+1, x1, iy2);
            leftDrawn = true;
        }

        // darker bottom border
        p.setPen(c.darker());
        p.drawLine(x1, iy2, x2-1, iy2);

        lastPos = nextPos;
        cLast = c;
        d++;
    }

    // right border (in last color)
    if (x2>0)
        p.drawLine(x2, iy1, x2, iy2);

    return pix;
}


QPixmap costPixmap(EventType* ct, ProfileCostArray* cost,
                   double total, bool framed)
{
    if (!ct) return QPixmap();

    if (ct->isReal()) {
        QColor color = GlobalGUIConfig::eventTypeColor(ct);
        double p = 100.0 * cost->subCost(ct) / total;
        return percentagePixmap(COSTPIX_WIDTH, 10, (int)(p+.5), color, framed);
    }

    int maxIndex;
    double h[MaxRealIndexValue];
    maxIndex = ct->histCost(cost, total, h);

    if (maxIndex ==0) return QPixmap();
    return partitionPixmap(COSTPIX_WIDTH, 10, h, ct->set(), maxIndex, framed);
}