File indexing completed on 2024-05-12 05:44:25

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht  ral@alwins-world.de        *
0003  *   https://kde.org/applications/development/org.kde.kdesvn               *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 /* This file was part of KCachegrind.
0021    Copyright (C) 2002, 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0022    Adapted for the needs of kdesvn  by Rajko Albrecht <ral@alwins-world.de>
0023 */
0024 /**
0025  * A Widget for visualizing hierarchical metrics as areas.
0026  * The API is similar to QListView.
0027  *
0028  * This file defines the following classes:
0029  *  DrawParams, RectDrawing, TreeMapItem, TreeMapWidget
0030  *
0031  * DrawParams/RectDrawing allows reusing of TreeMap drawing
0032  * functions in other widgets.
0033  */
0034 
0035 #ifndef DRAWPARAMS_H
0036 #define DRAWPARAMS_H
0037 
0038 #include <qapplication.h>
0039 #include <qcolor.h>
0040 #include <qpixmap.h>
0041 #include <qstring.h>
0042 #include <qstringlist.h>
0043 #include <qwidget.h>
0044 
0045 class QString;
0046 
0047 class KConfigGroup;
0048 
0049 /**
0050  * Drawing parameters for an object.
0051  * A Helper Interface for RectDrawing.
0052  */
0053 class DrawParams
0054 {
0055 public:
0056     /**
0057      * Positions for drawing into a rectangle.
0058      *
0059      * The specified position assumes no rotation.
0060      * If there is more than one text for one position, it is put
0061      * nearer to the center of the item.
0062      *
0063      * Drawing at top positions cuts free space from top,
0064      * drawing at bottom positions cuts from bottom.
0065      * Default usually gives positions clockwise according to field number.
0066      */
0067     enum Position { TopLeft, TopCenter, TopRight, BottomLeft, BottomCenter, BottomRight, Default, Unknown };
0068 
0069     // no constructor as this is an abstract class
0070     virtual ~DrawParams()
0071     {
0072     }
0073 
0074     virtual QString text(int) const = 0;
0075     virtual QPixmap pixmap(int) const = 0;
0076     virtual Position position(int) const = 0;
0077     // 0: no limit, negative: leave at least -maxLines() free
0078     virtual int maxLines(int) const
0079     {
0080         return 0;
0081     }
0082     virtual int fieldCount() const
0083     {
0084         return 0;
0085     }
0086 
0087     virtual QColor backColor() const
0088     {
0089         return Qt::white;
0090     }
0091     virtual QFont font() const = 0;
0092 
0093     virtual bool selected() const
0094     {
0095         return false;
0096     }
0097     virtual bool current() const
0098     {
0099         return false;
0100     }
0101     virtual bool shaded() const
0102     {
0103         return true;
0104     }
0105     virtual bool rotated() const
0106     {
0107         return false;
0108     }
0109     virtual bool drawFrame() const
0110     {
0111         return true;
0112     }
0113 };
0114 
0115 /*
0116  * DrawParam with attributes stored
0117  */
0118 class StoredDrawParams : public DrawParams
0119 {
0120 public:
0121     explicit StoredDrawParams();
0122     explicit StoredDrawParams(const QColor &c, bool selected = false, bool current = false);
0123 
0124     // getters
0125     QString text(int) const override;
0126     QPixmap pixmap(int) const override;
0127     Position position(int) const override;
0128     int maxLines(int) const override;
0129     int fieldCount() const override
0130     {
0131         return _field.size();
0132     }
0133 
0134     QColor backColor() const override
0135     {
0136         return _backColor;
0137     }
0138     bool selected() const override
0139     {
0140         return _selected;
0141     }
0142     bool current() const override
0143     {
0144         return _current;
0145     }
0146     bool shaded() const override
0147     {
0148         return _shaded;
0149     }
0150     bool rotated() const override
0151     {
0152         return _rotated;
0153     }
0154     bool drawFrame() const override
0155     {
0156         return _drawFrame;
0157     }
0158 
0159     QFont font() const override;
0160 
0161     // attribute setters
0162     void setField(int f, const QString &t, const QPixmap &pm = QPixmap(), Position p = Default, int maxLines = 0);
0163     void setText(int f, const QString &);
0164     void setPixmap(int f, QPixmap);
0165     void setPosition(int f, Position);
0166     void setMaxLines(int f, int);
0167     void setBackColor(QColor c)
0168     {
0169         _backColor = c;
0170     }
0171     void setSelected(bool b)
0172     {
0173         _selected = b;
0174     }
0175     void setCurrent(bool b)
0176     {
0177         _current = b;
0178     }
0179     void setShaded(bool b)
0180     {
0181         _shaded = b;
0182     }
0183     void setRotated(bool b)
0184     {
0185         _rotated = b;
0186     }
0187     void drawFrame(bool b)
0188     {
0189         _drawFrame = b;
0190     }
0191 
0192 protected:
0193     QColor _backColor;
0194     bool _selected : 1;
0195     bool _current : 1;
0196     bool _shaded : 1;
0197     bool _rotated : 1;
0198     bool _drawFrame : 1;
0199 
0200 private:
0201     // resize field array if needed to allow to access field <f>
0202     void ensureField(int f);
0203 
0204     struct Field {
0205         QString text;
0206         QPixmap pix;
0207         Position pos = Unknown;
0208         int maxLines = 0;
0209     };
0210 
0211     QVector<Field> _field;
0212 };
0213 
0214 /* State for drawing on a rectangle.
0215  *
0216  * Following drawing functions are provided:
0217  * - background drawing with shading and 3D frame
0218  * - successive pixmap/text drawing at various positions with wrap-around
0219  *   optimized for minimal space usage (e.g. if a text is drawn at top right
0220  *   after text on top left, the same line is used if space allows)
0221  *
0222  */
0223 class RectDrawing
0224 {
0225 public:
0226     explicit RectDrawing(const QRect &r);
0227     ~RectDrawing();
0228 
0229     // The default DrawParams object used.
0230     DrawParams *drawParams();
0231     // we take control over the given object (i.e. delete at destruction)
0232     void setDrawParams(DrawParams *);
0233 
0234     // draw on a given QPainter, use this class as info provider per default
0235     void drawBack(QPainter *, DrawParams *dp = nullptr);
0236     /* Draw field at position() from pixmap()/text() with maxLines().
0237      * Returns true if something was drawn
0238      */
0239     bool drawField(QPainter *, int f, DrawParams *dp = nullptr);
0240 
0241     // resets rectangle for free space
0242     void setRect(QRect);
0243 
0244     // Returns the rectangle area still free of text/pixmaps after
0245     // a number of drawText() calls.
0246     QRect remainingRect(DrawParams *dp = nullptr);
0247 
0248 private:
0249     int _usedTopLeft, _usedTopCenter, _usedTopRight;
0250     int _usedBottomLeft, _usedBottomCenter, _usedBottomRight;
0251     QRect _rect;
0252 
0253     // temporary
0254     int _fontHeight;
0255     QFontMetrics *_fm;
0256     DrawParams *_dp;
0257 };
0258 
0259 #endif