1 /***************************************************************************
2  *   Copyright (C) 2005-2009 by Rajko Albrecht  ral@alwins-world.de        *
3  *   http://kdesvn.alwins-world.de/                                        *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20 /* This file was part of KCachegrind.
21    Copyright (C) 2002, 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
22    Adapted for the needs of kdesvn  by Rajko Albrecht <ral@alwins-world.de>
23 */
24 /**
25  * A Widget for visualizing hierarchical metrics as areas.
26  * The API is similar to QListView.
27  *
28  * This file defines the following classes:
29  *  DrawParams, RectDrawing, TreeMapItem, TreeMapWidget
30  *
31  * DrawParams/RectDrawing allows reusing of TreeMap drawing
32  * functions in other widgets.
33  */
34 
35 #ifndef DRAWPARAMS_H
36 #define DRAWPARAMS_H
37 
38 #include <qstring.h>
39 #include <qwidget.h>
40 #include <qpixmap.h>
41 #include <qcolor.h>
42 #include <qapplication.h>
43 #include <qstringlist.h>
44 
45 class QString;
46 
47 class KConfigGroup;
48 
49 /**
50  * Drawing parameters for an object.
51  * A Helper Interface for RectDrawing.
52  */
53 class DrawParams
54 {
55 public:
56     /**
57      * Positions for drawing into a rectangle.
58      *
59      * The specified position assumes no rotation.
60      * If there is more than one text for one position, it is put
61      * nearer to the center of the item.
62      *
63      * Drawing at top positions cuts free space from top,
64      * drawing at bottom positions cuts from bottom.
65      * Default usually gives positions clockwise according to field number.
66      */
67     enum Position { TopLeft, TopCenter, TopRight,
68                     BottomLeft, BottomCenter, BottomRight,
69                     Default, Unknown
70                   };
71 
72     // no constructor as this is an abstract class
~DrawParams()73     virtual ~DrawParams() {}
74 
75     virtual QString  text(int) const = 0;
76     virtual QPixmap  pixmap(int) const = 0;
77     virtual Position position(int) const = 0;
78     // 0: no limit, negative: leave at least -maxLines() free
maxLines(int)79     virtual int      maxLines(int) const
80     {
81         return 0;
82     }
fieldCount()83     virtual int      fieldCount() const
84     {
85         return 0;
86     }
87 
backColor()88     virtual QColor   backColor() const
89     {
90         return Qt::white;
91     }
92     virtual QFont font() const = 0;
93 
selected()94     virtual bool selected() const
95     {
96         return false;
97     }
current()98     virtual bool current() const
99     {
100         return false;
101     }
shaded()102     virtual bool shaded() const
103     {
104         return true;
105     }
rotated()106     virtual bool rotated() const
107     {
108         return false;
109     }
drawFrame()110     virtual bool drawFrame() const
111     {
112         return true;
113     }
114 };
115 
116 /*
117  * DrawParam with attributes stored
118  */
119 class StoredDrawParams: public DrawParams
120 {
121 public:
122     explicit StoredDrawParams();
123     explicit StoredDrawParams(const QColor &c,
124                               bool selected = false,
125                               bool current = false);
126 
127     // getters
128     QString  text(int) const override;
129     QPixmap  pixmap(int) const override;
130     Position position(int) const override;
131     int      maxLines(int) const override;
fieldCount()132     int      fieldCount() const override
133     {
134         return _field.size();
135     }
136 
backColor()137     QColor   backColor() const override
138     {
139         return _backColor;
140     }
selected()141     bool selected() const override
142     {
143         return _selected;
144     }
current()145     bool current() const override
146     {
147         return _current;
148     }
shaded()149     bool shaded() const override
150     {
151         return _shaded;
152     }
rotated()153     bool rotated() const override
154     {
155         return _rotated;
156     }
drawFrame()157     bool drawFrame() const override
158     {
159         return _drawFrame;
160     }
161 
162     QFont font() const override;
163 
164     // attribute setters
165     void setField(int f, const QString &t, const QPixmap &pm = QPixmap(),
166                   Position p = Default, int maxLines = 0);
167     void setText(int f, const QString &);
168     void setPixmap(int f, QPixmap);
169     void setPosition(int f, Position);
170     void setMaxLines(int f, int);
setBackColor(QColor c)171     void setBackColor(QColor c)
172     {
173         _backColor = c;
174     }
setSelected(bool b)175     void setSelected(bool b)
176     {
177         _selected = b;
178     }
setCurrent(bool b)179     void setCurrent(bool b)
180     {
181         _current = b;
182     }
setShaded(bool b)183     void setShaded(bool b)
184     {
185         _shaded = b;
186     }
setRotated(bool b)187     void setRotated(bool b)
188     {
189         _rotated = b;
190     }
drawFrame(bool b)191     void drawFrame(bool b)
192     {
193         _drawFrame = b;
194     }
195 
196 protected:
197     QColor _backColor;
198     bool _selected : 1;
199     bool _current : 1;
200     bool _shaded : 1;
201     bool _rotated : 1;
202     bool _drawFrame : 1;
203 
204 private:
205     // resize field array if needed to allow to access field <f>
206     void ensureField(int f);
207 
208     struct Field {
209         QString text;
210         QPixmap pix;
211         Position pos = Unknown;
212         int maxLines = 0;
213     };
214 
215     QVector<Field> _field;
216 };
217 
218 /* State for drawing on a rectangle.
219  *
220  * Following drawing functions are provided:
221  * - background drawing with shading and 3D frame
222  * - successive pixmap/text drawing at various positions with wrap-around
223  *   optimized for minimal space usage (e.g. if a text is drawn at top right
224  *   after text on top left, the same line is used if space allows)
225  *
226  */
227 class RectDrawing
228 {
229 public:
230     explicit RectDrawing(const QRect &r);
231     ~RectDrawing();
232 
233     // The default DrawParams object used.
234     DrawParams *drawParams();
235     // we take control over the given object (i.e. delete at destruction)
236     void setDrawParams(DrawParams *);
237 
238     // draw on a given QPainter, use this class as info provider per default
239     void drawBack(QPainter *, DrawParams *dp = nullptr);
240     /* Draw field at position() from pixmap()/text() with maxLines().
241      * Returns true if something was drawn
242      */
243     bool drawField(QPainter *, int f, DrawParams *dp = nullptr);
244 
245     // resets rectangle for free space
246     void setRect(QRect);
247 
248     // Returns the rectangle area still free of text/pixmaps after
249     // a number of drawText() calls.
250     QRect remainingRect(DrawParams *dp = nullptr);
251 
252 private:
253     int _usedTopLeft, _usedTopCenter, _usedTopRight;
254     int _usedBottomLeft, _usedBottomCenter, _usedBottomRight;
255     QRect _rect;
256 
257     // temporary
258     int _fontHeight;
259     QFontMetrics *_fm;
260     DrawParams *_dp;
261 };
262 
263 #endif
264