summaryrefslogtreecommitdiffstats
path: root/tdecachegrind/tdecachegrind/partgraph.cpp
blob: a20f53dc24317bd6335417f40dfa9f4e4e9b72e6 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* This file is part of KCachegrind.
   Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

   KCachegrind is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
 * TracePart as Nested Area
 */

#include <klocale.h>

#include "partgraph.h"
#include "configuration.h"
#include "listutils.h"


// PartAreaWidget

PartAreaWidget::PartAreaWidget(TQWidget* parent, const char* name)
    : TreeMapWidget(new BasePartItem(), parent, name)
{
  _data = 0;
  _function = 0;

  _costType = 0;
  _groupType = TraceCost::NoCostType;
  _visualisation = NoVisualisation;
  _zoomFunction = false;
  _callLevels = 1;
}

void PartAreaWidget::setData(TraceData* data)
{
  if (data == _data) return;

  _data = data;
  _function = 0;
  _hiddenParts.clear();

  ((BasePartItem*)base())->setData(data);
}

void PartAreaWidget::changeHidden(const TracePartList& list)
{
  _hiddenParts = list;
  base()->refresh();
}


void PartAreaWidget::setCostType(TraceCostType* ct)
{
  _costType = ct;

  // this resizes items
  base()->redraw();
}

void PartAreaWidget::setVisualisation(VisualisationMode m)
{
  _visualisation = m;
  refreshParts();
}

void PartAreaWidget::setZoomFunction(bool zoomFunction)
{
  _zoomFunction = zoomFunction;
  refreshParts();
}

void PartAreaWidget::setCallLevels(int callLevels)
{
  _callLevels = callLevels;
  refreshParts();
}

void PartAreaWidget::refreshParts()
{
  // rebuild only subparts to keep part selection state
  TreeMapItem* i;
  TreeMapItemList* l = base()->children();
  if (l)
    for (i=l->first();i;i=l->next())
      i->refresh();

  // but resize part areas
  base()->redraw();
}


void PartAreaWidget::setFunction(TraceFunction* f)
{
  _function = f;

  if (_visualisation == PartAreaWidget::Inclusive)
    refreshParts();
}

void PartAreaWidget::setGroupType(TraceCost::CostType gt)
{
  _groupType = gt;

  // rebuild hierarchy below parts.
  // thus, selected parts stay selected
  TreeMapItem* i;
  TreeMapItemList* l = base()->children();
  if (l)
    for (i=l->first();i;i=l->next())
      i->refresh();

  base()->redraw();
}

bool PartAreaWidget::isHidden(TracePart* part) const
{
  return (_hiddenParts.containsRef(part)>0);
}

TQColor PartAreaWidget::groupColor(TraceFunction* f) const
{
  if (!f)
    return colorGroup().button();

  return Configuration::functionColor(_groupType, f);
}

TQString PartAreaWidget::tipString(TreeMapItem* i) const
{
  TQString tip, itemTip;
  int count = 0;

  //qDebug("PartAreaWidget::tipString for '%s'", i->name().ascii());

  // first, SubPartItem's
  while (i && count<Configuration::maxSymbolCount() && i->rtti() == 3) {
    itemTip = i->text(0);
    if ((int)itemTip.length()>Configuration::maxSymbolLength())
      itemTip = itemTip.left(Configuration::maxSymbolLength()) + "...";

    if (!i->text(1).isEmpty())
      itemTip += " (" + i->text(1) + ")";

    if (!tip.isEmpty())
      itemTip += "\n";

    tip = itemTip + tip;
    i = i->parent();
    count++;
  }

  // skip to part
  while (i && i->rtti()==3) i = i->parent();

  if (i && i->rtti()==2) {
    itemTip = i18n("Profile Part %1").arg(i->text(0));
    if (!i->text(1).isEmpty())
        itemTip += " (" + i->text(1) + ")";

    if (!tip.isEmpty())
      itemTip += "\n";

    tip = itemTip + tip;
  }

//  qDebug("PartAreaWidget:: tip %s, itemTip %s",
//         tip.ascii(), itemTip.ascii());

  return tip;
}





// BasePartItem

BasePartItem::BasePartItem()
  : TreeMapItem()
{
  _data = 0;
  setSorting(-1);
}

void BasePartItem::setData(TraceData* data)
{
  if (data == _data) return;

  _data = data;
  refresh();
}

TreeMapItemList* BasePartItem::children()
{
  if (!_data) return _children;

  if (!initialized()) {
//    qDebug("Create Parts (%s)", name().ascii());

    PartAreaWidget* w = (PartAreaWidget*) widget();
    TracePart* part;
    TracePartList l = _data->parts();
    for (part=l.first();part;part=l.next())
      if (!w->isHidden(part))
        addItem(new PartItem(part));
  }

  return _children;
}

TQString BasePartItem::text(int textNo) const
{
  if (textNo == 0) {
    if (!_data)
      return i18n("(no trace)");

    if (_data->parts().count() == 0)
      return i18n("(no part)");
  }
  return TQString();
}


TQColor BasePartItem::backColor() const
{
  return widget()->colorGroup().base();
}

double BasePartItem::value() const
{
  if (!_data) return 0;

  PartAreaWidget* w = (PartAreaWidget*) widget();
  return (double)_data->subCost(w->costType());
}





// PartItem

PartItem::PartItem(TracePart* p)
{
  _p = p;
  _factor=1;
}

TQString PartItem::text(int textNo) const
{
  if (textNo == 0)
    return _p->prettyName();

  if (textNo != 1)
    return TQString();

  TraceCostType* ct;
  PartAreaWidget* w = (PartAreaWidget*)widget();
  SubCost v;

  ct = w->costType();
  v = _p->subCost(ct);

  if (Configuration::showPercentage()) {
    TraceCost* t = _p->data()->totals();
    double p  = 100.0 * v / t->subCost(ct);
    return TQString("%1 %")
      .arg(p, 0, 'f', Configuration::percentPrecision());
  }
  return v.pretty();
}


TQPixmap PartItem::pixmap(int i) const
{
    if (i != 1) return TQPixmap();

    // Cost pixmap

    TraceCostType* ct = ((PartAreaWidget*)widget())->costType();
    return costPixmap( ct, _p, (double) (_p->data()->totals()->subCost(ct)), false );
}


double PartItem::value() const
{
  PartAreaWidget* w = (PartAreaWidget*)widget();
  TraceCostType* ct = w->costType();
  if ((w->visualisation() == PartAreaWidget::Inclusive) &&
      w->zoomFunction()) {

    // use value of zoomed function
    TraceFunction* f = w->function();
    if (f) {
      TracePartFunction* pf = (TracePartFunction*) f->findDepFromPart(_p);
      if (pf)
        return (double) pf->inclusive()->subCost(ct);
      // when function is not available in part, hide part
      return 0.0;
    }
  }
  return (double) _p->subCost(ct);
}

double PartItem::sum() const
{
  PartAreaWidget* w = (PartAreaWidget*)widget();
  if (w->visualisation() == PartAreaWidget::Inclusive) {
    double s = value();
    //qDebug("PartItem::sum [part %s]: %d", _p->name().ascii(), s);
    return s;
  }
  return 0.0;
}

TreeMapItemList* PartItem::children()
{
  if (initialized()) return _children;

  TraceCost* c;
//    qDebug("Create Part subitems (%s)", name().ascii());

  PartAreaWidget* w = (PartAreaWidget*)widget();
  if (w->visualisation() == PartAreaWidget::Inclusive) {
    TraceFunction* f = w->function();
    if (f) {
      c = f->findDepFromPart(_p);
      if (c) addItem(new SubPartItem(c));
    }

    return _children;
  }


  switch( ((PartAreaWidget*)widget())->groupType() ) {

  case TraceCost::Object:
  {
    TraceObjectMap::Iterator it;
    for ( it = _p->data()->objectMap().begin();
          it != _p->data()->objectMap().end(); ++it ) {
      c = (*it).findDepFromPart(_p);
      if (c)
        addItem(new SubPartItem(c));
    }
  }
  break;

  case TraceCost::Class:
  {
    TraceClassMap::Iterator it;
    for ( it = _p->data()->classMap().begin();
          it != _p->data()->classMap().end(); ++it ) {
      c = (*it).findDepFromPart(_p);
      if (c)
        addItem(new SubPartItem(c));
    }
  }
  break;

  case TraceCost::File:
  {
    TraceFileMap::Iterator it;
    for ( it = _p->data()->fileMap().begin();
          it != _p->data()->fileMap().end(); ++it ) {
      c = (*it).findDepFromPart(_p);
      if (c)
        addItem(new SubPartItem(c));
    }
  }
  break;

  case TraceCost::Function:
  {
    TraceFunctionMap::Iterator it;
    for ( it = _p->data()->functionMap().begin();
          it != _p->data()->functionMap().end(); ++it ) {
      c = (*it).findDepFromPart(_p);
      if (c)
        addItem(new SubPartItem(c));
    }
  }
  break;

  default:
    break;
  }

  return _children;
}


TQColor PartItem::backColor() const
{
  PartAreaWidget* w = (PartAreaWidget*)widget();
  return w->groupColor(0);
}


// SubPartItem

SubPartItem::SubPartItem(TraceCost* c)
{
  _partCostItem = c;
  _factor=1;
}

TQString SubPartItem::text(int textNo) const
{
  if (textNo == 0) {
    if (!_partCostItem)
      return i18n("(unknown)");

    return _partCostItem->dependant()->prettyName();
  }

  if (textNo != 1)
    return TQString();

  TraceCostType* ct;
  PartAreaWidget* w = (PartAreaWidget*)widget();
  SubCost v;

  ct = w->costType();
  if (w->visualisation() == PartAreaWidget::Inclusive)
    v = ((TracePartFunction*)_partCostItem)->inclusive()->subCost(ct);
  else
    v = _partCostItem->subCost(ct);

  if (Configuration::showPercentage()) {
    TraceCost* t = Configuration::showExpanded() ?
	_partCostItem->part() : _partCostItem->part()->data()->totals();
    double p  = 100.0 * v / t->subCost(ct);
    return TQString("%1 %")
      .arg(p, 0, 'f', Configuration::percentPrecision());
  }
  return v.pretty();
}

TQPixmap SubPartItem::pixmap(int i) const
{
    if (i != 1) return TQPixmap();

    // Cost pixmap

    PartAreaWidget* w = (PartAreaWidget*)widget();
    TraceCostType* ct = w->costType();
    TraceCost* t = Configuration::showExpanded() ?
	_partCostItem->part() : _partCostItem->part()->data()->totals();
    TraceCost* c;
    if (w->visualisation() == PartAreaWidget::Inclusive)
	c = ((TracePartFunction*)_partCostItem)->inclusive();
    else
	c = _partCostItem;

    return costPixmap( ct, c, (double) (t->subCost(ct)), false );
}

double SubPartItem::value() const
{
  TraceCostType* ct;
  PartAreaWidget* w = (PartAreaWidget*)widget();

  ct = w->costType();
  if (w->visualisation() == PartAreaWidget::Inclusive)
    return (double)
	((TracePartFunction*)_partCostItem)->inclusive()->subCost(ct);

  return (double) _partCostItem->subCost(ct);
}

double SubPartItem::sum() const
{
  PartAreaWidget* w = (PartAreaWidget*)widget();
  if (w->visualisation() == PartAreaWidget::Inclusive) {
    double s = value();
    //qDebug("SubPartItem::sum [Cost %s]: %d", _cost->name().ascii(), s);
    return s;
  }
  return 0.0;
}

TreeMapItemList* SubPartItem::children()
{
  if (!initialized()) {
//    qDebug("Create Part sub-subitems (%s)", name().ascii());

    PartAreaWidget* w = (PartAreaWidget*)widget();

    if (depth()-2 > w->callLevels())
      return _children;

    if (w->visualisation() == PartAreaWidget::Inclusive) {
      TracePartCall* call;
      TracePartCallList l;

      setSum(value());

      l = ((TracePartFunction*)_partCostItem)->partCallings();
      for (call=l.first();call;call=l.next()) {
        TraceFunction* called = call->call()->called();
        TraceCost* partCalled = called->findDepFromPart(call->part());
        if (partCalled)
          addItem(new SubPartItem(partCalled));
      }
    }
  }

  return _children;
}


TQColor SubPartItem::backColor() const
{
  PartAreaWidget* w = (PartAreaWidget*)widget();
  if (w->visualisation() == PartAreaWidget::Inclusive)
    return w->groupColor((TraceFunction*)(_partCostItem->dependant()));

  return Configuration::groupColor(_partCostItem->dependant());
}


#include "partgraph.moc"