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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
/* This file is part of KCachegrind.
Copyright (C) 2002, 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.
*/
/**
* A Widget for visualizing hierarchical metrics as areas.
* The API is similar to TQListView.
*
* This file defines the following classes:
* DrawParams, RectDrawing, TreeMapItem, TreeMapWidget
*
* DrawParams/RectDrawing allows reusing of TreeMap drawing
* functions in other widgets.
*/
#ifndef TREEMAP_H
#define TREEMAP_H
#include <tqstring.h>
#include <tqwidget.h>
#include <tqpixmap.h>
#include <tqptrlist.h>
#include <tqvaluevector.h>
#include <tqcolor.h>
#include <tqapplication.h>
#include <tqstringlist.h>
class TQPopupMenu;
class TreeMapTip;
class TreeMapWidget;
class TreeMapItem;
class TreeMapItemList;
class TQString;
class KConfigGroup;
/**
* Drawing parameters for an object.
* A Helper Interface for RectDrawing.
*/
class DrawParams
{
public:
/**
* Positions for drawing into a rectangle.
*
* The specified position assumes no rotation.
* If there is more than one text for one position, it is put
* nearer to the center of the item.
*
* Drawing at top positions cuts free space from top,
* drawing at bottom positions cuts from bottom.
* Default usually gives positions clockwise according to field number.
*/
enum Position { TopLeft, TopCenter, TopRight,
BottomLeft, BottomCenter, BottomRight,
Default, Unknown};
// no constructor as this is an abstract class
virtual ~DrawParams() {}
virtual TQString text(int) const = 0;
virtual TQPixmap pixmap(int) const = 0;
virtual Position position(int) const = 0;
// 0: no limit, negative: leave at least -maxLines() free
virtual int maxLines(int) const { return 0; }
virtual int fieldCount() const { return 0; }
virtual TQColor backColor() const { return TQt::white; }
virtual const TQFont& font() const = 0;
virtual bool selected() const { return false; }
virtual bool current() const { return false; }
virtual bool shaded() const { return true; }
virtual bool rotated() const { return false; }
};
/*
* DrawParam with attributes stored
*/
class StoredDrawParams: public DrawParams
{
public:
StoredDrawParams();
StoredDrawParams(TQColor c,
bool selected = false, bool current = false);
// getters
TQString text(int) const;
TQPixmap pixmap(int) const;
Position position(int) const;
int maxLines(int) const;
int fieldCount() const { return _field.size(); }
TQColor backColor() const { return _backColor; }
bool selected() const { return _selected; }
bool current() const { return _current; }
bool shaded() const { return _shaded; }
bool rotated() const { return _rotated; }
const TQFont& font() const;
// attribute setters
void setField(int f, const TQString& t, TQPixmap pm = TQPixmap(),
Position p = Default, int maxLines = 0);
void setText(int f, const TQString&);
void setPixmap(int f, const TQPixmap&);
void setPosition(int f, Position);
void setMaxLines(int f, int);
void setBackColor(const TQColor& c) { _backColor = c; }
void setSelected(bool b) { _selected = b; }
void setCurrent(bool b) { _current = b; }
void setShaded(bool b) { _shaded = b; }
void setRotated(bool b) { _rotated = b; }
protected:
TQColor _backColor;
bool _selected, _current, _shaded, _rotated;
private:
// resize field array if needed to allow to access field <f>
void ensureField(int f);
struct Field {
TQString text;
TQPixmap pix;
Position pos;
int maxLines;
};
TQValueVector<Field> _field;
};
/* State for drawing on a rectangle.
*
* Following drawing functions are provided:
* - background drawing with shading and 3D frame
* - successive pixmap/text drawing at various positions with wrap-around
* optimized for minimal space usage (e.g. if a text is drawn at top right
* after text on top left, the same line is used if space allows)
*
*/
class RectDrawing
{
public:
RectDrawing(TQRect);
~RectDrawing();
// The default DrawParams object used.
DrawParams* drawParams();
// we take control over the given object (i.e. delete at destruction)
void setDrawParams(DrawParams*);
// draw on a given TQPainter, use this class as info provider per default
void drawBack(TQPainter*, DrawParams* dp = 0);
/* Draw field at position() from pixmap()/text() with maxLines().
* Returns true if something was drawn
*/
bool drawField(TQPainter*, int f, DrawParams* dp = 0);
// resets rectangle for free space
void setRect(TQRect);
// Returns the rectangle area still free of text/pixmaps after
// a number of drawText() calls.
TQRect remainingRect(DrawParams* dp = 0);
private:
int _usedTopLeft, _usedTopCenter, _usedTopRight;
int _usedBottomLeft, _usedBottomCenter, _usedBottomRight;
TQRect _rect;
// temporary
int _fontHeight;
TQFontMetrics* _fm;
DrawParams* _dp;
};
class TreeMapItemList: public TQPtrList<TreeMapItem>
{
public:
TreeMapItem* commonParent();
protected:
int compareItems ( Item item1, Item item2 );
};
typedef TQPtrListIterator<TreeMapItem> TreeMapItemListIterator;
/**
* Base class of items in TreeMap.
*
* This class supports an arbitrary number of text() strings
* positioned counterclock-wise starting at TopLeft. Each item
* has its own static value(), sum() and sorting(). The
* splitMode() and borderWidth() is taken from a TreeMapWidget.
*
* If you want more flexibility, reimplement TreeMapItem and
* override the corresponding methods. For dynamic creation of child
* items on demand, reimplement tqchildren().
*/
class TreeMapItem: public StoredDrawParams
{
public:
/**
* Split direction for nested areas:
* AlwaysBest: Choose split direction for every subitem according to
* longest side of rectangle left for drawing
* Best: Choose split direction for all subitems of an area
* depending on longest side
* HAlternate:Qt::Horizontal at top; alternate direction on depth step
* VAlternate:Qt::Vertical at top; alternate direction on depth step
* Qt::Horizontal: Always horizontal split direction
* Qt::Vertical: Always vertical split direction
*/
enum SplitMode { Bisection, Columns, Rows,
AlwaysBest, Best,
HAlternate, VAlternate,
Horizontal, Vertical };
TreeMapItem(TreeMapItem* parent = 0, double value = 1.0 );
TreeMapItem(TreeMapItem* parent, double value,
TQString text1, TQString text2 = TQString(),
TQString text3 = TQString(), TQString text4 = TQString());
virtual ~TreeMapItem();
bool isChildOf(TreeMapItem*);
TreeMapItem* commonParent(TreeMapItem* item);
// force a redraw of this item
void redraw();
// delete all tqchildren
void clear();
// force new child generation & refresh
void refresh();
// call in a reimplemented items() method to check if already called
// after a clear(), this will return false
bool initialized();
/**
* Adds an item to a parent.
* When no sorting is used, the item is appended (drawn at bottom).
* This is only needed if the parent was not already specified in the
* construction of the item.
*/
void addItem(TreeMapItem*);
/**
* Returns a list of text strings of specified text number,
* from root up to this item.
*/
TQStringList path(int) const;
/**
* Depth of this item. This is the distance to root.
*/
int depth() const;
/**
* Parent Item
*/
TreeMapItem* parent() const { return _parent; }
/**
* Temporary rectangle used for drawing this item the last time.
* This is internally used to map from a point to an item.
*/
void setItemRect(const TQRect& r) { _rect = r; }
void clearItemRect();
const TQRect& itemRect() const { return _rect; }
int width() const { return _rect.width(); }
int height() const { return _rect.height(); }
/**
* Temporary rectangle list of free space of this item.
* Used internally to enable tooltip.
*/
void clearFreeRects();
TQPtrList<TQRect>* freeRects() const { return _freeRects; }
void addFreeRect(const TQRect& r);
/**
* Temporary child item index of the child that was current() recently.
*/
int index() const { return _index; }
void setIndex(int i) { _index = i; }
/**
* TreeMap widget this item is put in.
*/
TreeMapWidget* widget() const { return _widget; }
void setParent(TreeMapItem* p);
void setWidget(TreeMapWidget* w) { _widget = w; }
void setSum(double s) { _sum = s; }
void setValue(double s) { _value = s; }
virtual double sum() const;
virtual double value() const;
// replace "Default" position with setting from TreeMapWidget
virtual Position position(int) const;
virtual const TQFont& font() const;
virtual bool isMarked(int) const;
virtual int borderWidth() const;
/**
* Returns the text number after that sorting is done or
* -1 for no sorting, -2 for value() sorting (default).
* If ascending != 0, a bool value is written at that location
* to indicate if sorting should be ascending.
*/
virtual int sorting(bool* ascending) const;
/**
* Set the sorting for child drawing.
*
* Default is no sorting: <textNo> = -1
* For value() sorting, use <textNo> = -2
*
* For fast sorting, set this to -1 before child insertions and call
* again after inserting all tqchildren.
*/
void setSorting(int textNo, bool ascending = true);
/**
* Resort according to the already set sorting.
*
* This has to be done if the sorting base changes (e.g. text or values
* change). If this is only true for the tqchildren of this item, you can
* set the recursive parameter to false.
*/
void resort(bool recursive = true);
virtual SplitMode splitMode() const;
virtual int rtti() const;
// not const as this can create tqchildren on demand
virtual TreeMapItemList* tqchildren();
protected:
TreeMapItemList* _tqchildren;
double _sum, _value;
private:
TreeMapWidget* _widget;
TreeMapItem* _parent;
int _sortTextNo;
bool _sortAscending;
// temporary tqlayout
TQRect _rect;
TQPtrList<TQRect>* _freeRects;
int _depth;
// temporary self value (when using level skipping)
double _unused_self;
// index of last active subitem
int _index;
};
/**
* Class for visualisation of a metric of hierarchically
* nested items as 2D areas.
*/
class TreeMapWidget: public TQWidget
{
Q_OBJECT
TQ_OBJECT
public:
/**
* Same as in TQListBox/TQListView
*/
enum SelectionMode { Single, Multi, Extended, NoSelection };
TreeMapWidget(TreeMapItem* base, TQWidget* parent=0, const char* name=0);
~TreeMapWidget();
/**
* Returns the TreeMapItem filling out the widget space
*/
TreeMapItem* base() const { return _base; }
/**
* Returns a reference to the current widget font.
*/
const TQFont& currentFont() const;
/**
* Returns the area item at position x/y, independent from any
* maxSelectDepth setting.
*/
TreeMapItem* item(int x, int y) const;
/**
* Returns the nearest item with a visible area; this
* can be the given item itself.
*/
TreeMapItem* visibleItem(TreeMapItem*) const;
/**
* Returns the item possible for selection. this returns the
* given item itself or a parent thereof,
* depending on setting of maxSelectDepth().
*/
TreeMapItem* possibleSelection(TreeMapItem*) const;
/**
* Selects or unselects an item.
* In multiselection mode, the constrain that a selected item
* has no selected tqchildren or parents stays true.
*/
void setSelected(TreeMapItem*, bool selected = true);
/**
* Switches on the marking <markNo>. Marking 0 switches off marking.
* This is mutually exclusive to selection, and is automatically
* switched off when selection is changed (also by the user).
* Marking is visually the same as selection, and is based on
* TreeMapItem::isMarked(<markNo>).
* This enables to programmatically show multiple selected items
* at once even in single selection mode.
*/
void setMarked(int markNo = 1, bool redraw = true);
/**
* Clear selection of all selected items which are tqchildren of
* parent. When parent == 0, clears whole selection
* Returns true if selection changed.
*/
bool clearSelection(TreeMapItem* parent = 0);
/**
* Selects or unselects items in a range.
* This is needed internally for Shift-Click in Extented mode.
* Range means for a hierarchical widget:
* - select/unselect i1 and i2 according selected
* - search common parent of i1 and i2, and select/unselect the
* range of direct tqchildren between but excluding the child
* leading to i1 and the child leading to i2.
*/
void setRangeSelection(TreeMapItem* i1,
TreeMapItem* i2, bool selected);
/**
* Sets the current item.
* The current item is mainly used for keyboard navigation.
*/
void setCurrent(TreeMapItem*, bool kbd=false);
/**
* Set the maximal depth a selected item can have.
* If you try to select a item with higher depth, the ancestor holding
* this condition is used.
*
* See also possibleSelection().
*/
void setMaxSelectDepth(int d) { _maxSelectDepth = d; }
void setSelectionMode(SelectionMode m) { _selectionMode = m; }
/**
* for setting/getting global split direction
*/
void setSplitMode(TreeMapItem::SplitMode m);
TreeMapItem::SplitMode splitMode() const;
// returns true if string was recognized
bool setSplitMode(TQString);
TQString splitModeString() const;
/*
* Shading of rectangles enabled ?
*/
void setShadingEnabled(bool s);
bool isShadingEnabled() const { return _shading; }
/**
* Items usually have a size proportional to their value().
* With <width>, you can give the minimum width
* of the resulting rectangle to still be drawn.
* For space not used because of to small items, you can specify
* with <reuseSpace> if the background should shine through or
* the space will be used to enlarge the next item to be drawn
* at this level.
*/
void setVisibleWidth(int width, bool reuseSpace = false);
/**
* If a tqchildren value() is almost the parents sum(),
* it can happen that the border to be drawn for visibilty of
* nesting relations takes to much space, and the
* parent/child size relation can not be mapped to a correct
* area size relation.
*
* Either
* (1) Ignore the incorrect drawing, or
* (2) Skip drawing of the parent level alltogether.
*/
void setSkipIncorrectBorder(bool enable = true);
bool skipIncorrectBorder() const { return _skipIncorrectBorder; }
/**
* Maximal nesting depth
*/
void setMaxDrawingDepth(int d);
int maxDrawingDepth() const { return _maxDrawingDepth; }
/**
* Minimal area for rectangles to draw
*/
void setMinimalArea(int area);
int minimalArea() const { return _minimalArea; }
/* defaults for text attributes */
TQString defaultFieldType(int) const;
TQString defaultFieldStop(int) const;
bool defaultFieldVisible(int) const;
bool defaultFieldForced(int) const;
DrawParams::Position defaultFieldPosition(int) const;
/**
* Set the type name of a field.
* This is important for the visualization menu generated
* with visualizationMenu()
*/
void setFieldType(int, TQString);
TQString fieldType(int) const;
/**
* Stop drawing at item with name
*/
void setFieldStop(int, TQString);
TQString fieldStop(int) const;
/**
* Should the text with number textNo be visible?
* This is only done if remaining space is enough to allow for
* proportional size constrains.
*/
void setFieldVisible(int, bool);
bool fieldVisible(int) const;
/**
* Should the drawing of the name into the rectangle be forced?
* This enables drawing of the name before drawing subitems, and
* thus destroys proportional constrains.
*/
void setFieldForced(int, bool);
bool fieldForced(int) const;
/**
* Set the field position in the area. See TreeMapItem::Position
*/
void setFieldPosition(int, DrawParams::Position);
DrawParams::Position fieldPosition(int) const;
void setFieldPosition(int, TQString);
TQString fieldPositionString(int) const;
/**
* Do we allow the texts to be rotated by 90 degrees for better fitting?
*/
void setAllowRotation(bool);
bool allowRotation() const { return _allowRotation; }
void setBorderWidth(int w);
int borderWidth() const { return _borderWidth; }
/**
* Save/restore options.
*/
void saveOptions(KConfigGroup*, TQString prefix = TQString());
void restoreOptions(KConfigGroup*, TQString prefix = TQString());
/**
* These functions populate given popup menus.
* The added items are already connected to handlers.
*
* The int is the menu id where to start for the items (100 IDs reserved).
*/
void addSplitDirectionItems(TQPopupMenu*, int);
void addSelectionItems(TQPopupMenu*, int, TreeMapItem*);
void addFieldStopItems(TQPopupMenu*, int, TreeMapItem*);
void addAreaStopItems(TQPopupMenu*, int, TreeMapItem*);
void addDepthStopItems(TQPopupMenu*, int, TreeMapItem*);
void addVisualizationItems(TQPopupMenu*, int);
TreeMapWidget* widget() { return this; }
TreeMapItem* current() const { return _current; }
TreeMapItemList selection() const { return _selection; }
bool isSelected(TreeMapItem* i) const;
int maxSelectDepth() const { return _maxSelectDepth; }
SelectionMode selectionMode() const { return _selectionMode; }
/**
* Return tooltip string to show for a item (can be rich text)
* Default implementation gives lines with "text0 (text1)" going to root.
*/
virtual TQString tipString(TreeMapItem* i) const;
/**
* Redraws an item with all tqchildren.
* This takes changed values(), sums(), colors() and text() into account.
*/
void redraw(TreeMapItem*);
void redraw() { redraw(_base); }
/**
* Resort all TreeMapItems. See TreeMapItem::resort().
*/
void resort() { _base->resort(true); }
// internal
void drawTreeMap();
// used internally when items are destroyed
void deletingItem(TreeMapItem*);
protected slots:
void splitActivated(int);
void selectionActivated(int);
void fieldStopActivated(int);
void areaStopActivated(int);
void depthStopActivated(int);
void visualizationActivated(int);
signals:
void selectionChanged();
void selectionChanged(TreeMapItem*);
/**
* This signal is emitted if the current item changes.
* If the change is done because of keyboard navigation,
* the <kbd> is set to true
*/
void currentChanged(TreeMapItem*, bool keyboard);
void clicked(TreeMapItem*);
void returnPressed(TreeMapItem*);
void doubleClicked(TreeMapItem*);
void rightButtonPressed(TreeMapItem*, const TQPoint &);
void contextMenuRequested(TreeMapItem*, const TQPoint &);
protected:
void mousePressEvent( TQMouseEvent * );
void contextMenuEvent( TQContextMenuEvent * );
void mouseReleaseEvent( TQMouseEvent * );
void mouseMoveEvent( TQMouseEvent * );
void mouseDoubleClickEvent( TQMouseEvent * );
void keyPressEvent( TQKeyEvent* );
void paintEvent( TQPaintEvent * );
void resizeEvent( TQResizeEvent * );
void showEvent( TQShowEvent * );
void fontChange( const TQFont& );
private:
TreeMapItemList diff(TreeMapItemList&, TreeMapItemList&);
// returns true if selection changed
TreeMapItem* setTmpSelected(TreeMapItem*, bool selected = true);
TreeMapItem* setTmpRangeSelection(TreeMapItem* i1,
TreeMapItem* i2, bool selected);
bool isTmpSelected(TreeMapItem* i);
void drawItem(TQPainter* p, TreeMapItem*);
void drawItems(TQPainter* p, TreeMapItem*);
bool horizontal(TreeMapItem* i, const TQRect& r);
void drawFill(TreeMapItem*,TQPainter* p, TQRect& r);
void drawFill(TreeMapItem*,TQPainter* p, TQRect& r,
TreeMapItemListIterator it, int len, bool goBack);
bool drawItemArray(TQPainter* p, TreeMapItem*, TQRect& r, double,
TreeMapItemListIterator it, int len, bool);
bool resizeAttr(int);
TreeMapItem* _base;
TreeMapItem *_current, *_pressed, *_lastOver, *_oldCurrent;
TreeMapTip* _tip;
int _maxSelectDepth, _maxDrawingDepth;
// attributes for field, per textNo
struct FieldAttr {
TQString type, stop;
bool visible, forced;
DrawParams::Position pos;
};
TQValueVector<FieldAttr> _attr;
SelectionMode _selectionMode;
TreeMapItem::SplitMode _splitMode;
int _visibleWidth, _stopArea, _minimalArea, _borderWidth;
bool _reuseSpace, _skipIncorrectBorder, _drawSeparators, _shading;
bool _allowRotation;
TreeMapItem * _needsRefresh;
TreeMapItemList _selection;
int _markNo;
// for the context menus: start IDs
int _splitID, _selectionID, _visID;
int _fieldStopID, _areaStopID, _depthStopID;
TreeMapItem* _menuItem;
// temporary selection while dragging, used for drawing
// most of the time, _selection == _tmpSelection
TreeMapItemList _tmpSelection;
bool _inShiftDrag, _inControlDrag;
// temporary widget font metrics while drawing
TQFont _font;
int _fontHeight;
// back buffer pixmap
TQPixmap _pixmap;
};
#endif
|