summaryrefslogtreecommitdiffstats
path: root/tdecachegrind/tdecachegrind/callgraphview.h
blob: 4db619dbf4677d8aa26e18aa5847c54b144578d2 (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
/* 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.
*/

/*
 * Callgraph View
 */

#ifndef CALLGRAPHVIEW_H
#define CALLGRAPHVIEW_H

#include <tqcanvas.h>
#include <tqwidget.h>
#include <tqmap.h>
#include <tqtimer.h>

#include "treemap.h" // for DrawParams
#include "tracedata.h"
#include "traceitemview.h"

class TQProcess;

class KTempFile;
class CanvasNode;
class CanvasEdge;
class GraphEdge;
class CallGraphView;

// sorts according start/end position of a call arc
// this depends on attached CanvasEdge's !
class GraphEdgeList: public TQPtrList<GraphEdge>
{
 public:
    GraphEdgeList();
    void setSortCallerPos(bool b) { _sortCallerPos = b; }

 protected:
    int compareItems ( Item item1, Item item2 );

 private:
    bool _sortCallerPos;
};


typedef TQMap<GraphEdge*, int> GraphEdgeSet;

// temporary parts of call graph to be shown
class GraphNode
{
public:
    GraphNode();

  TraceFunction* function() { return _f; }
  void setFunction(TraceFunction* f) { _f = f; }

  CanvasNode* canvasNode() { return _cn; }
  void setCanvasNode(CanvasNode* cn) { _cn = cn; }

  bool isVisible() { return _visible; }
  void setVisible(bool v) { _visible = v; }

  // keyboard navigation
  TraceCall* visibleCaller();
  TraceCall* visibleCalling();
  void setCalling(GraphEdge*);
  void setCaller(GraphEdge*);
  TraceFunction* nextVisible();
  TraceFunction* priorVisible();
  TraceCall* nextVisibleCaller(GraphEdge*);
  TraceCall* nextVisibleCalling(GraphEdge*);
  TraceCall* priorVisibleCaller(GraphEdge*);
  TraceCall* priorVisibleCalling(GraphEdge*);

  double self, incl;
  GraphEdgeList callers, callings;
  // for fast unique insertion of GraphEdges in above lists
  GraphEdgeSet callerSet, callingSet;

 private:
  TraceFunction* _f;
  CanvasNode* _cn;
  bool _visible;

  // for keyboard navigation
  int _lastCallerIndex, _lastCallingIndex;
  bool _lastFromCaller;
};

class GraphEdge
{
public:
    GraphEdge();

  CanvasEdge* canvasEdge() { return _ce; }
  void setCanvasEdge(CanvasEdge* ce) { _ce = ce; }

  TraceCall* call() { return _c; }
  void setCall(TraceCall* c) { _c = c; }

  bool isVisible() { return _visible; }
  void setVisible(bool v) { _visible = v; }

  GraphNode* fromNode() { return _fromNode; }
  GraphNode* toNode() { return _toNode; }
  TraceFunction* from() { return _from; }
  TraceFunction* to() { return _to; }

  // has special cases for collapsed edges
  TQString prettyName();

  void setCaller(TraceFunction* f) { _from = f; }
  void setCalling(TraceFunction* f) { _to = f; }
  void setCallerNode(GraphNode* n) { _fromNode = n; }
  void setCallingNode(GraphNode* n) { _toNode = n; }

  // keyboard navigation
  TraceFunction* visibleCaller();
  TraceFunction* visibleCalling();
  TraceCall* nextVisible();
  TraceCall* priorVisible();

  double cost, count;

 private:
  // we have a _c *and* _from/_to because for collapsed edges,
  // only _to or _from will be unequal NULL
  TraceCall* _c;
  TraceFunction * _from, * _to;
  GraphNode *_fromNode, *_toNode;
  CanvasEdge* _ce;
  bool _visible;
  // for keyboard navigation: have we last reached this edge via a caller?
  bool _lastFromCaller;

};


typedef TQMap<TraceFunction*, GraphNode> GraphNodeMap;
typedef TQMap<TQPair<TraceFunction*, TraceFunction*>, GraphEdge> GraphEdgeMap;


/* Abstract Interface for graph options */
class GraphOptions
{
 public:
    enum Layout { TopDown, LeftRight, Circular};

    virtual double funcLimit() = 0;
    virtual double callLimit() = 0;
    virtual int maxCallerDepth() = 0;
    virtual int maxCallingDepth() = 0;
    virtual bool showSkipped() = 0;
    virtual bool expandCycles() = 0;
    virtual bool clusterGroups() = 0;
    virtual int detailLevel() = 0;
    virtual Layout layout() = 0;

    static TQString layoutString(Layout);
    static Layout layout(TQString);
};

/* Graph Options Storage */
class StorableGraphOptions: public GraphOptions
{
 public:
    StorableGraphOptions();

    // implementation of getters
    virtual double funcLimit() { return _funcLimit; }
    virtual double callLimit() { return _callLimit; }
    virtual int maxCallerDepth() { return _maxCallerDepth; }
    virtual int maxCallingDepth() { return _maxCallingDepth; }
    virtual bool showSkipped() { return _showSkipped; }
    virtual bool expandCycles() { return _expandCycles; }
    virtual bool clusterGroups() { return _clusterGroups; }
    virtual int detailLevel() { return _detailLevel; }
    virtual Layout layout() { return _layout; }

    // setters
    void setMaxCallerDepth(int d) { _maxCallerDepth = d; }
    void setMaxCallingDepth(int d) { _maxCallingDepth = d; }
    void setFuncLimit(double l) { _funcLimit = l; }
    void setCallLimit(double l) { _callLimit = l; }
    void setShowSkipped(bool b) { _showSkipped = b; }
    void setExpandCycles(bool b) { _expandCycles = b; }
    void setClusterGroups(bool b) { _clusterGroups = b; }
    void setDetailLevel(int l) { _detailLevel = l; }
    void setLayout(Layout l) { _layout = l; }

 protected:
    double _funcLimit, _callLimit;
    int _maxCallerDepth, _maxCallingDepth;
    bool _showSkipped, _expandCycles, _clusterGroups;
    int _detailLevel;
    Layout _layout;
};

/**
 * GraphExporter
 *
 * Generates a graph file for "dot"
 * Create an instance and
 */
class GraphExporter: public StorableGraphOptions
{
public:
  GraphExporter();
  GraphExporter(TraceData*, TraceFunction*, TraceCostType*,
		TraceItem::CostType, TQString filename = TQString());
  virtual ~GraphExporter();

  void reset(TraceData*, TraceItem*, TraceCostType*,
	     TraceItem::CostType, TQString filename = TQString());

  TQString filename() { return _dotName; }
  int edgeCount() { return _edgeMap.count(); }
  int nodeCount() { return _nodeMap.count(); }

  // Set the object from which to get graph options for creation.
  // Default is this object itself (supply 0 for default)
  void setGraphOptions(GraphOptions* go = 0);

  // Create a subgraph with given limits/maxDepths
  void createGraph();

  // calls createGraph before dumping of not already created
  void writeDot();

  // to map back to structures when parsing a layouted graph

  /* <toFunc> is a helper for node() and edge().
   * Don't use the returned pointer directly, but only with
   * node() or edge(), because it could be a dangling pointer.
   */
  TraceFunction* toFunc(TQString);
  GraphNode* node(TraceFunction*);
  GraphEdge* edge(TraceFunction*, TraceFunction*);

  /* After CanvasEdges are attached to GraphEdges, we can
   * sort the incoming and outgoing edges of all nodes
   * regarding start/end points for keyboard navigation
   */
  void sortEdges();

private:
  void buildGraph(TraceFunction*, int, bool, double);

  TQString _dotName;
  TraceItem* _item;
  TraceCostType* _costType;
  TraceItem::CostType _groupType;
  KTempFile* _tmpFile;
  double _realFuncLimit, _realCallLimit;
  int _maxDepth;
  bool _graphCreated;

  GraphOptions* _go;

  // optional graph attributes
  bool _useBox;

  // graph parts written to file
  GraphNodeMap _nodeMap;
  GraphEdgeMap _edgeMap;
};

/**
 * A panner layed over a TQCanvas
 */
class PannerView: public TQCanvasView
{
  Q_OBJECT
  TQ_OBJECT

public:
  PannerView(TQWidget * parent = 0, const char * name = 0);

  void setZoomRect(TQRect r);

signals:
  void zoomRectMoved(int dx, int dy);
  void zoomRectMoveFinished();

protected:
  void contentsMousePressEvent(TQMouseEvent*);
  void contentsMouseMoveEvent(TQMouseEvent*);
  void contentsMouseReleaseEvent(TQMouseEvent*);
  void drawContents(TQPainter * p, int clipx, int clipy, int clipw, int cliph);

  TQRect _zoomRect;
  bool _movingZoomRect;
  TQPoint _lastPos;
};


/*
 * Canvas Items:
 * - CanvasNode       (Rectangular Area)
 * - CanvasEdge       (Spline curve)
 * - CanvasEdgeLabel  (Label for edges)
 * - CanvasEdgeArrow  (Arrows at the end of the edge spline)
 * - CanvasFrame      (Grey background blending to show active node)
 */

enum {
    CANVAS_NODE = 1122,
    CANVAS_EDGE, CANVAS_EDGELABEL, CANVAS_EDGEARROW,
    CANVAS_FRAME
};

class CanvasNode: public TQCanvasRectangle, public StoredDrawParams
{
public:
  CanvasNode(CallGraphView*,GraphNode*, int, int, int, int, TQCanvas*);

  void updateGroup();
  void setSelected(bool);
  void drawShape(TQPainter&);

  GraphNode* node() { return _node; }
  int rtti() const { return CANVAS_NODE; }

private:
  GraphNode* _node;
  CallGraphView* _view;
};

class CanvasEdgeLabel: public TQCanvasRectangle, public StoredDrawParams
{
public:
  CanvasEdgeLabel(CallGraphView*, CanvasEdge*, int, int, int, int, TQCanvas*);

  void drawShape(TQPainter&);

  CanvasEdge* canvasEdge() { return _ce; }
  int rtti() const { return CANVAS_EDGELABEL; }

private:
  CanvasEdge* _ce;
  CallGraphView* _view;
};

class CanvasEdgeArrow: public TQCanvasPolygon
{
public:
  CanvasEdgeArrow(CanvasEdge*, TQCanvas*);

  void drawShape(TQPainter&);

  CanvasEdge* canvasEdge() { return _ce; }
  int rtti() const { return CANVAS_EDGEARROW; }

private:
  CanvasEdge* _ce;
};


class CanvasEdge: public TQCanvasSpline
{
public:
  CanvasEdge(GraphEdge*, TQCanvas*);

  void setSelected(bool);
  void drawShape(TQPainter&);
  TQPointArray areaPoints() const;

  CanvasEdgeLabel* label() { return _label; }
  void setLabel(CanvasEdgeLabel* l) { _label = l; }
  CanvasEdgeArrow* arrow() { return _arrow; }
  void setArrow(CanvasEdgeArrow* a) { _arrow = a; }

  GraphEdge* edge() { return _edge; }
  int rtti() const { return CANVAS_EDGE; }

private:
  GraphEdge* _edge;
  CanvasEdgeLabel* _label;
  CanvasEdgeArrow* _arrow;
};


class CanvasFrame: public TQCanvasRectangle
{
public:
  CanvasFrame( CanvasNode*, TQCanvas *canvas );
  int rtti () const { return CANVAS_FRAME; }
  bool hit( const TQPoint&) const { return false; }
protected:
    void drawShape( TQPainter & );
private:
    static TQPixmap* _p;
};


class CallGraphTip;

/**
 * A CanvasView showing a part of the call graph
 * and another zoomed out CanvasView in a border acting as
 * a panner to select to visible part (only if needed)
 */
class CallGraphView: public TQCanvasView,  public TraceItemView,
		     public StorableGraphOptions
{
 Q_OBJECT
  TQ_OBJECT

public:
 enum ZoomPosition { TopLeft, TopRight, BottomLeft, BottomRight, Auto };

  CallGraphView(TraceItemView* parentView,
                TQWidget* parent=0, const char* name=0);
  ~CallGraphView();

  void readViewConfig(KConfig*, TQString prefix, TQString postfix, bool);
  void saveViewConfig(KConfig*, TQString prefix, TQString postfix, bool);

  TQWidget* widget() { return this; }
  TQString whatsThis() const;

  ZoomPosition zoomPos() const { return _zoomPosition; }
  static ZoomPosition zoomPos(TQString);
  static TQString zoomPosString(ZoomPosition);

public slots:
  void contentsMovingSlot(int,int);
  void zoomRectMoved(int,int);
  void zoomRectMoveFinished();

  void showRenderWarning();
  void stopRendering();
  void readDotOutput();
  void dotExited();

protected:
  void resizeEvent(TQResizeEvent*);
  void contentsMousePressEvent(TQMouseEvent*);
  void contentsMouseMoveEvent(TQMouseEvent*);
  void contentsMouseReleaseEvent(TQMouseEvent*);
  void contentsMouseDoubleClickEvent(TQMouseEvent*);
  void contentsContextMenuEvent(TQContextMenuEvent*);
  void keyPressEvent(TQKeyEvent*);
  void focusInEvent(TQFocusEvent*);
  void focusOutEvent(TQFocusEvent*);

private:
  void updateSizes(TQSize s = TQSize(0,0));
  TraceItem* canShow(TraceItem*);
  void doUpdate(int);
  void refresh();
  void makeFrame(CanvasNode*, bool active);
  void clear();
  void showText(TQString);

  TQCanvas *_canvas;
  int _xMargin, _yMargin;
  PannerView *_completeView;
  double _cvZoom;

  CallGraphTip* _tip;

  bool _isMoving;
  TQPoint _lastPos;

  GraphExporter _exporter;

  GraphNode* _selectedNode;
  GraphEdge* _selectedEdge;

  // widget options
  ZoomPosition _zoomPosition, _lastAutoPosition;

  // background rendering
  TQProcess* _renderProcess;
  TQTimer _renderTimer;
  GraphNode* _prevSelectedNode;
  TQPoint _prevSelectedPos;
  TQString _unparsedOutput;
};




#endif