summaryrefslogtreecommitdiffstats
path: root/kdirstat/ktreemapview.h
blob: 63d8b8b8436d4d7b6acbcedfa064a1ac6d49c1c9 (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
/*
 *   File name:	ktreemapview.h
 *   Summary:	High level classes for KDirStat
 *   License:	LGPL - See file COPYING.LIB for details.
 *   Author:	Stefan Hundhammer <sh@suse.de>
 *
 *   Updated:	2003-02-02
 */


#ifndef KTreemapView_h
#define KTreemapView_h

#include <tqcanvas.h>

#ifdef HAVE_CONFIG_H
#   include <config.h>
#endif


#define MinAmbientLight			0
#define MaxAmbientLight			200
#define DefaultAmbientLight		40

#define	MinHeightScalePercent		10
#define	MaxHeightScalePercent		200
#define DefaultHeightScalePercent	100
#define DefaultHeightScaleFactor	( DefaultHeightScalePercent / 100.0 )

#define DefaultMinTileSize		3
#define CushionHeight			1.0


class TQMouseEvent;
class TDEConfig;

namespace KDirStat
{
    class KTreemapTile;
    class KTreemapSelectionRect;
    class KDirTree;
    class KFileInfo;

    class KTreemapView:	public TQCanvasView
    {
	Q_OBJECT
  

    public:
	/**
	 * Constructor.
	 **/
	KTreemapView( KDirTree * 	tree,
		      TQWidget * 	parent 		= 0,
		      const TQSize &	initialSize 	= TQSize() );

	/**
	 * Destructor.
	 **/
	virtual ~KTreemapView();

	/**
	 * Returns the (topmost) treemap tile at the specified position
	 * or 0 if there is none.
	 **/
	KTreemapTile * tileAt( TQPoint pos );

	/**
	 * Returns the minimum recommended size for this widget.
	 * Reimplemented from TQWidget.
	 **/
	virtual TQSize minimumSizeHint() const { return TQSize( 0, 0 ); }

	/**
	 * Returns this treemap view's currently selected treemap tile or 0 if
	 * there is none.
	 **/
	KTreemapTile * selectedTile() const { return _selectedTile; }


	/**
	 * Returns this treemap view's root treemap tile or 0 if there is none.
	 **/
	KTreemapTile * rootTile() const { return _rootTile; }

	/**
	 * Returns this treemap view's @ref KDirTree.
	 **/
	KDirTree * tree() const { return _tree; }

	/**
	 * Search the treemap for a tile that corresponds to the specified
	 * KFileInfo node. Returns 0 if there is none.
	 *
	 * Notice: This is an expensive operation since all treemap tiles need
	 * to be searched.
	 **/
	KTreemapTile * findTile( KFileInfo * node );

	/**
	 * Returns a suitable color for 'file' based on a set of internal rules
	 * (according to filename extension, MIME type or permissions).
	 **/
	TQColor tileColor( KFileInfo * file );


    public slots:

        /**
	 * Make a treemap tile this treemap's selected tile.
	 * 'tile' may be 0. In this case, only the previous selection is
	 * deselected.
	 **/
        void selectTile( KTreemapTile * tile );

	/**
	 * Search the treemap for a tile with the specified KFileInfo node and
	 * select that tile if it is found. If nothing is found or if 'node' is
	 * 0, the previously selected tile is deselected.
	 **/
        void selectTile( KFileInfo * node );

	/**
	 * Zoom in one level towards the currently selected treemap tile:
	 * The entire treemap will be rebuilt with the near-topmost ancestor of
	 * the selected tile as the new root.
	 **/
	void zoomIn();

	/**
	 * Zoom out one level: The parent (if there is any) KFileInfo node of
	 * the current treemap root becomes the new root. This usually works
	 * only after zoomIn().
	 **/
	void zoomOut();

	/**
	 * Select the parent of the currently selected tile (if possible).
	 *
	 * This is very much the same as clicking with the middle mouse button,
	 * but not quite: The middle mouse button cycles back to the tile
	 * clicked at if there is no more parent. This method does not (because
	 * there is no known mouse position).
	 **/
	void selectParent();

	/**
	 * Completely rebuild the entire treemap from the internal tree's root
	 * on.
	 **/
	void rebuildTreemap();

	/**
	 * Clear the treemap contents.
	 **/
	void clear();

	/**
	 * Delete all items of a TQCanvas.
	 *
	 * Strangely enough, TQCanvas itself does not provide such a function.
	 **/
	static void deleteAllItems( TQCanvas * canvas );

	/**
	 * Notification that a dir tree node has been deleted.
	 **/
	void deleteNotify( KFileInfo * node );

	/**
	 * Read some parameters from the global @ref TDEConfig object.
	 **/
	void readConfig();

    public:

	/**
	 * Rebuild the treemap with 'newRoot' as the new root and the specified
	 * size. If 'newSize' is (0, 0), visibleSize() is used.
	 **/
	void rebuildTreemap( KFileInfo * 	newRoot,
			     const TQSize &	newSize = TQSize() );

	/**
	 * Returns the visible size of the viewport presuming no scrollbars are
	 * needed - which makes a lot more sense than fiddling with scrollbars
	 * since treemaps can be scaled to make scrollbars unnecessary.
	 **/
	TQSize visibleSize();

	/**
	 * Returns the visible width of the viewport presuming no scrollbars
	 * are needed.
	 *
	 * This uses visibleSize() which is a somewhat expensive operation, so
	 * if you need both visibleWidth() and visibleHeight(), better call
	 * visibleSize() once and access its width() and height() methods.
	 **/
	int visibleWidth() { return visibleSize().width(); }

	/**
	 * Returns the visible height of the viewport presuming no scrollbars
	 * are needed.
	 *
	 * This uses visibleSize() which is a somewhat expensive operation, so
	 * if you need both visibleWidth() and visibleHeight(), better call
	 * visibleSize() once and access its width() and height() methods.
	 **/
	int visibleHeight() { return visibleSize().height(); }

	/**
	 * Returns true if it is possible to zoom in with the currently
	 * selected tile, false if not.
	 **/
	bool canZoomIn() const;

	/**
	 * Returns true if it is possible to zoom out with the currently
	 * selected tile, false if not.
	 **/
	bool canZoomOut() const;

	/**
	 * Returns true if it is possible to select the parent of the currently
	 * selected tile, false if not.
	 **/
	bool canSelectParent() const;

	/**
	 * Returns 'true' if the treemap is automatically resized to fit into
	 * the available space, 'false' if not.
	 **/
	bool autoResize() const { return _autoResize; }

	/**
	 * Returns 'true' if treemap tiles are to be squarified upon creation,
	 * 'false' if not.
	 **/
	bool squarify() const { return _squarify; }

	/**
	 * Returns 'true' if cushion shading is to be used, 'false' if not.
	 **/
	bool doCushionShading() const { return _doCushionShading; }

	/**
	 * Returns 'true' if cushion shaded treemap tiles are to be separated
	 * by a grid, 'false' if not.
	 **/
	bool forceCushionGrid() const { return _forceCushionGrid; }

	/**
	 * Returns 'true' if tile boundary lines should be drawn for cushion
	 * treemaps, 'false'  if not.
	 **/
	bool ensureContrast() const { return _ensureContrast; }

	/**
	 * Returns the minimum tile size in pixels. No treemap tiles less than
	 * this in width or height are desired.
	 **/
	int minTileSize() const { return _minTileSize; }

	/**
	 * Returns the cushion grid color.
	 **/
	const TQColor & cushionGridColor() const { return _cushionGridColor; }

	/**
	 * Returns the outline color to use if cushion shading is not used.
	 **/
	const TQColor & outlineColor() const { return _outlineColor; }

	/**
	 * Returns the fill color for non-directory treemap tiles when cushion
	 * shading is not used.
	 **/
	const TQColor & fileFillColor() const { return _fileFillColor; }

	/**
	 * Returns the fill color for directory (or "dotentry") treemap tiles
	 * when cushion shading is not used.
	 **/
	const TQColor & dirFillColor() const { return _dirFillColor; }

	/**
	 * Returns the intensity of ambient light for cushion shading 
	 * [0..255]
	 **/
	int ambientLight() const { return _ambientLight; }

	/**
	 * Returns the X coordinate of a directed light source for cushion
	 * shading.
	 **/

	double lightX() const { return _lightX; }

	/**
	 * Returns the Y coordinate of a directed light source for cushion
	 * shading.
	 **/
	double lightY() const { return _lightY; }

	/**
	 * Returns the Z coordinate of a directed light source for cushion
	 * shading.
	 **/
	double lightZ() const { return _lightZ; }

	/**
	 * Returns cushion ridge height degradation factor (0 .. 1.0) for each
	 * level of subdivision.
	 **/
	double heightScaleFactor() const { return _heightScaleFactor; }


    signals:

	/**
	 * Emitted when the currently selected item changes.
	 * Caution: 'item' may be 0 when the selection is cleared.
	 **/
	void selectionChanged( KFileInfo * item );

	/**
	 * Emitted when the treemap changes, e.g. is rebuilt, zoomed in, or
	 * zoomed out.
	 **/
	void treemapChanged();

	/**
	 * Emitted when a context menu for this tile should be opened.
	 * (usually on right click). 'pos' contains the click's mouse
	 * coordinates.
	 **/
	void contextMenu( KTreemapTile * tile, const TQPoint & pos );

	/**
	 * Emitted at user activity. Some interactive actions are assigned an
	 * amount of "activity points" that can be used to judge whether or not
	 * the user is actually using this program or if it's just idly sitting
	 * around on the desktop. This is intended for use together with a @ref
	 * KActivityTracker.
	 **/
	void userActivity( int points );


    protected:

	/**
	 * Catch mouse click - emits a selectionChanged() signal.
	 **/
	virtual void contentsMousePressEvent( TQMouseEvent * event );

	/**
	 * Catch mouse double click:
	 * 	Left   button double-click zooms in,
	 *	right  button double-click zooms out,
	 * 	middle button double-click rebuilds treemap.
	 **/
	virtual void contentsMouseDoubleClickEvent( TQMouseEvent * event );

	/**
	 * Resize the treemap view. Suppress the treemap contents if the size
	 * falls below a minimum size, redisplay it if it grows above that
	 * minimum size.
	 *
	 * Reimplemented from TQFrame.
	 **/
	virtual void resizeEvent( TQResizeEvent * event );

	/**
	 * Convenience method to read a color from 'config'.
	 **/
	TQColor readColorEntry( TDEConfig * 	config,
			       const char * 	entryName,
			       TQColor 		defaultColor );

	// Data members

	KDirTree *		_tree;
	KTreemapTile *		_rootTile;
	KTreemapTile * 		_selectedTile;
	KTreemapSelectionRect *	_selectionRect;
	TQString			_savedRootUrl;

	bool			_autoResize;
	bool			_squarify;
	bool			_doCushionShading;
	bool			_forceCushionGrid;
	bool			_ensureContrast;
	int			_minTileSize;

	TQColor			_highlightColor;
	TQColor			_cushionGridColor;
	TQColor			_outlineColor;
	TQColor			_fileFillColor;
	TQColor			_dirFillColor;

	int			_ambientLight;

	double			_lightX;
	double			_lightY;
	double			_lightZ;

	double 			_heightScaleFactor;

    }; // class KTreemapView



    /**
     * Transparent rectangle to make a treemap tile clearly visible as
     * "selected". Leaf tiles could do that on their own, but higher-level
     * tiles (corresponding to directories) are obscured for the most part, so
     * only a small portion (if any) of their highlighted outline could be
     * visible. This selection rectangle simply draws a two-pixel red outline
     * on top (i.e., great z-height) of everything else. The rectangle is
     * transparent, so the treemap tile contents remain visible.
     **/
    class KTreemapSelectionRect: public TQCanvasRectangle
    {
    public:

	/**
	 * Constructor.
	 **/
	KTreemapSelectionRect( TQCanvas * canvas, const TQColor & color );

	/**
	 * Highlight the specified treemap tile: Resize this selection
	 * rectangle to match this tile and move it to this tile's
	 * position. Show the selection rectangle if it is currently
	 * invisible.
	 **/
	void highlight( KTreemapTile * tile );

    }; // class KTreemapSelectionRect

}	// namespace KDirStat


#endif // ifndef KTreemapView_h


// EOF