• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

kcachegrind

  • sources
  • kde-4.12
  • kdesdk
  • kcachegrind
  • libviews
listutils.cpp
Go to the documentation of this file.
1 /* This file is part of KCachegrind.
2  Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
3 
4  KCachegrind is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public
6  License as published by the Free Software Foundation, version 2.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 /*
20  * Some helper functions for QListViewItem derivates
21  */
22 
23 #include "listutils.h"
24 
25 #include <QPainter>
26 #include <QPixmap>
27 
28 #include "globalguiconfig.h"
29 
30 #define COSTPIX_WIDTH 25
31 
32 QPixmap colorPixmap(int w, int h, QColor c)
33 {
34  static QPixmap* pixs[37];
35  static QColor cols[37];
36  static bool inited = false;
37 
38  if (!inited) {
39  for (int i=0;i<37;i++) pixs[i]=0;
40  inited = true;
41  }
42  int hash = (w+h+c.red()+c.green()+c.blue()) % 37;
43  if (pixs[hash]) {
44  if ((pixs[hash]->width() == w) &&
45  (pixs[hash]->height() == h) &&
46  (cols[hash] == c))
47  return *pixs[hash];
48 
49  delete pixs[hash];
50  }
51 
52 
53  QPixmap* pix = new QPixmap(w, h);
54  pix->fill(c);
55  QPainter p(pix);
56  p.setPen(c.light());
57  p.drawLine(0, 0, w-1, 0);
58  p.drawLine(0, 0, 0, h-1);
59  p.setPen(c.dark());
60  p.drawLine(w-1, 0, w-1, h-1);
61  p.drawLine(0, h-1, w-1, h-1);
62 
63  pixs[hash] = pix;
64  cols[hash] = c;
65  return *pix;
66 }
67 
72 QPixmap percentagePixmap(int w, int h, int percent, QColor c, bool framed)
73 {
74  int iw, ix1, ih, iy1, iy2;
75 
76  // inner rectangle to fill with bar
77  if (framed) {
78  iw = w-2, ix1 = 1;
79  ih = h-2, iy1 = 1, iy2 = h-2;
80  }
81  else {
82  iw = w; ix1 = 0;
83  ih = h; iy1 = 0; iy2 = h-1;
84  }
85 
86  int filled = iw*percent/100+1;
87  if (!framed) w=filled;
88  if (w<3) return QPixmap();
89 
90  QPixmap pix(w, h);
91  pix.fill(Qt::white);
92  QPainter p(&pix);
93  p.setPen(Qt::black);
94  if (framed)
95  p.drawRect(0, 0, w-1, h-1);
96 
97  // inside
98  p.setPen(Qt::NoPen);
99  p.setBrush(c);
100  p.drawRect(ix1, iy1, filled-1,ih-1);
101 
102  // last right pix column
103  int lastY = ih-(filled*ih - iw*ih*percent/100);
104  int lastX1 = ix1+filled-2 + ((lastY>1) ? 1: 0);
105  int lastX2 = ix1+filled-2;
106 
107  // frame
108  p.setPen(c.light());
109  p.drawLine(ix1, iy1, lastX1, iy1);
110  p.drawLine(ix1, iy1, ix1, iy2);
111  p.setPen(c.dark());
112  p.drawLine(lastX1, iy1, lastX1, iy1+lastY);
113  p.drawLine(lastX2, iy1+lastY, lastX2, iy2);
114  p.drawLine(ix1+1, iy2, lastX2, iy2);
115 
116  return pix;
117 }
118 
119 inline QColor partitionColor(int d, int max)
120 {
121  return QColor::fromHsv((720*d/max) % 360, 255-(128*d/max), 192);
122 }
123 
124 
125 QPixmap partitionPixmap(int w, int h,
126  double* hist, EventTypeSet* set, int maxIndex, bool framed)
127 {
128  int lastPos = 0, nextPos;
129  double val=0.0, sum=0.0;
130  int d, dmin=maxIndex, dmax=0;
131  for (d = 0;d<maxIndex;d++)
132  if (hist[d]>0.0) {
133  sum += hist[d];
134  if (dmin>d) dmin = d;
135  if (dmax<d) dmax = d;
136  }
137 
138  // inner rectangle to fill with bar
139  int iw, ix1, ih, iy1, iy2;
140  if (framed) {
141  iw = w-2, ix1 = 1;
142  ih = h-2, iy1 = 1, iy2 = h-2;
143  }
144  else {
145  iw = w; ix1 = 0;
146  ih = h; iy1 = 0; iy2 = h-1;
147  }
148 
149  int filled = (int)(iw*sum+1);
150  if (!framed) w=filled;
151  if (w<3) return QPixmap();
152 
153  QPixmap pix(w, h);
154  pix.fill(Qt::white);
155  QPainter p(&pix);
156  p.setPen(Qt::black);
157  if (framed)
158  p.drawRect(0, 0, w-1, h-1);
159 
160  //qDebug("Sum %f, dw %d", sum,dw);
161 
162  QColor c, cLast;
163  bool leftDrawn = false;
164  int x1, x2=0;
165  int diff;
166  d=dmin;
167  while (d<dmax+1) {
168  val += hist[d];
169  nextPos = (int)(filled * val/sum);
170 
171  //qDebug(" hist[%d] %f, val %f, nextPos %d", d, hist[d], val, nextPos);
172 
173  diff = nextPos-lastPos;
174  if (diff==0) { d++; continue; }
175 
176  if (set)
177  c = GlobalGUIConfig::eventTypeColor(set->realType(d));
178  else
179  c = partitionColor(d,maxIndex);
180 
181  x1 = ix1+lastPos;
182  x2 = ix1+nextPos;
183  if (x2>=iw) x2=iw-1;
184 
185  // inside
186  p.setPen(Qt::NoPen);
187  p.setBrush(c);
188  p.drawRect(x1, iy1, x2-x1, ih-1);
189 
190  // lighter top border
191  p.setPen(c.light());
192  p.drawLine(x1, iy1, x2-1, iy1);
193 
194  // when width for last and current distance >2, draw full 3D effect...
195  if (!leftDrawn) {
196  p.drawLine(x1, iy1+1, x1, iy2);
197  leftDrawn = true;
198  }
199 
200  // darker bottom border
201  p.setPen(c.dark());
202  p.drawLine(x1, iy2, x2-1, iy2);
203 
204  lastPos = nextPos;
205  cLast = c;
206  d++;
207  }
208 
209  // right border (in last color)
210  if (x2>0)
211  p.drawLine(x2, iy1, x2, iy2);
212 
213  return pix;
214 }
215 
216 
217 QPixmap costPixmap(EventType* ct, ProfileCostArray* cost,
218  double total, bool framed)
219 {
220  if (!ct) return QPixmap();
221 
222  if (ct->isReal()) {
223  QColor color = GlobalGUIConfig::eventTypeColor(ct);
224  double p = 100.0 * cost->subCost(ct) / total;
225  return percentagePixmap(COSTPIX_WIDTH, 10, (int)(p+.5), color, framed);
226  }
227 
228  int maxIndex;
229  double h[MaxRealIndexValue];
230  maxIndex = ct->histCost(cost, total, h);
231 
232  if (maxIndex ==0) return QPixmap();
233  return partitionPixmap(COSTPIX_WIDTH, 10, h, ct->set(), maxIndex, framed);
234 }
235 
236 
237 
238 
partitionColor
QColor partitionColor(int d, int max)
Definition: listutils.cpp:119
ProfileCostArray::subCost
SubCost subCost(EventType *)
Returns a sub cost.
Definition: costitem.cpp:591
GlobalGUIConfig::eventTypeColor
static QColor eventTypeColor(EventType *)
Definition: globalguiconfig.cpp:193
MaxRealIndexValue
#define MaxRealIndexValue
Definition: costitem.h:136
percentagePixmap
QPixmap percentagePixmap(int w, int h, int percent, QColor c, bool framed)
Create a percentage pixmap with a filling rate of p percent (0-100).
Definition: listutils.cpp:72
EventType
A cost type, e.g.
Definition: eventtype.h:43
ProfileCostArray
An array of basic cost metrics for a trace item.
Definition: costitem.h:144
partitionPixmap
QPixmap partitionPixmap(int w, int h, double *hist, EventTypeSet *set, int maxIndex, bool framed)
Definition: listutils.cpp:125
listutils.h
globalguiconfig.h
COSTPIX_WIDTH
#define COSTPIX_WIDTH
Definition: listutils.cpp:30
EventType::isReal
bool isReal()
Definition: eventtype.h:70
EventTypeSet::realType
EventType * realType(int)
Definition: eventtype.cpp:462
EventTypeSet
A class for managing a set of event types.
Definition: eventtype.h:117
costPixmap
QPixmap costPixmap(EventType *ct, ProfileCostArray *cost, double total, bool framed)
Definition: listutils.cpp:217
colorPixmap
QPixmap colorPixmap(int w, int h, QColor c)
Definition: listutils.cpp:32
EventType::set
EventTypeSet * set()
Definition: eventtype.h:68
EventType::histCost
int histCost(ProfileCostArray *c, double total, double *hist)
Definition: eventtype.cpp:214
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:03:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kcachegrind

Skip menu "kcachegrind"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal