1 /*
2   quickdecorationsdrawer.h
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2010-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Filipe Azevedo <filipe.azevedo@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #ifndef GAMMARAY_QUICKINSPECTOR_QUICKDECORATIONSDRAWER_H
30 #define GAMMARAY_QUICKINSPECTOR_QUICKDECORATIONSDRAWER_H
31 
32 #include <QPen>
33 
34 #include "quickitemgeometry.h"
35 
36 namespace GammaRay {
37 
38 struct QuickDecorationsSettings
39 {
QuickDecorationsSettingsQuickDecorationsSettings40     QuickDecorationsSettings()
41         : boundingRectColor(QColor(232, 87, 82, 170))
42         , boundingRectBrush(QBrush(QColor(232, 87, 82, 95)))
43         , geometryRectColor(QColor(Qt::gray))
44         , geometryRectBrush(QBrush(QColor(Qt::gray), Qt::BDiagPattern))
45         , childrenRectColor(QColor(0, 99, 193, 170))
46         , childrenRectBrush(QBrush(QColor(0, 99, 193, 95)))
47         , transformOriginColor(QColor(156, 15, 86, 170))
48         , coordinatesColor(QColor(136, 136, 136))
49         , marginsColor(QColor(139, 179, 0))
50         , paddingColor(QColor(Qt::darkBlue))
51         , gridOffset(QPointF(0, 0))
52         , gridCellSize(QSizeF(0, 0))
53         , gridColor(QColor(Qt::red))
54         , componentsTraces(false)
55         , gridEnabled(false)
56     {
57     }
58 
59     bool operator==(const QuickDecorationsSettings &other) const {
60         return boundingRectColor == other.boundingRectColor &&
61                 boundingRectBrush == other.boundingRectBrush &&
62                 geometryRectColor == other.geometryRectColor &&
63                 geometryRectBrush == other.geometryRectBrush &&
64                 childrenRectColor == other.childrenRectColor &&
65                 childrenRectBrush == other.childrenRectBrush &&
66                 transformOriginColor == other.transformOriginColor &&
67                 coordinatesColor == other.coordinatesColor &&
68                 marginsColor == other.marginsColor &&
69                 paddingColor == other.paddingColor &&
70                 gridOffset == other.gridOffset &&
71                 gridCellSize == other.gridCellSize &&
72                 gridColor == other.gridColor &&
73                 componentsTraces == other.componentsTraces &&
74                 gridEnabled == other.gridEnabled
75                 ;
76     }
77 
78     bool operator!=(const QuickDecorationsSettings &other) const {
79         return !operator==(other);
80     }
81 
82     QColor boundingRectColor;
83     QBrush boundingRectBrush;
84     QColor geometryRectColor;
85     QBrush geometryRectBrush;
86     QColor childrenRectColor;
87     QBrush childrenRectBrush;
88     QColor transformOriginColor;
89     QColor coordinatesColor;
90     QColor marginsColor;
91     QColor paddingColor;
92     QPointF gridOffset;
93     QSizeF gridCellSize;
94     QColor gridColor;
95     bool componentsTraces;
96     bool gridEnabled;
97 };
98 
99 QDataStream &operator<<(QDataStream &stream, const GammaRay::QuickDecorationsSettings &settings);
100 QDataStream &operator>>(QDataStream &stream, GammaRay::QuickDecorationsSettings &settings);
101 
102 struct QuickDecorationsBaseRenderInfo {
103     QuickDecorationsBaseRenderInfo(const QuickDecorationsSettings &settings = QuickDecorationsSettings(),
104                                    const QRectF &viewRect = QRectF(),
105                                    qreal zoom = 1.0)
settingsQuickDecorationsBaseRenderInfo106         : settings(settings)
107         , viewRect(viewRect)
108         , zoom(zoom)
109     { }
110 
111     const QuickDecorationsSettings settings;
112     const QRectF viewRect;
113     const qreal zoom;
114 };
115 
116 struct QuickDecorationsRenderInfo : QuickDecorationsBaseRenderInfo {
117     QuickDecorationsRenderInfo(const QuickDecorationsSettings &settings = QuickDecorationsSettings(),
118                                const QuickItemGeometry &itemGeometry = QuickItemGeometry(),
119                                const QRectF &viewRect = QRectF(),
120                                qreal zoom = 1.0)
QuickDecorationsBaseRenderInfoQuickDecorationsRenderInfo121         : QuickDecorationsBaseRenderInfo(settings, viewRect, zoom)
122         , itemGeometry(itemGeometry)
123     { }
124 
125     const QuickItemGeometry itemGeometry;
126 };
127 
128 struct QuickDecorationsTracesInfo : QuickDecorationsBaseRenderInfo {
129     QuickDecorationsTracesInfo(const QuickDecorationsSettings &settings = QuickDecorationsSettings(),
130                                const QVector<QuickItemGeometry> &itemsGeometry = QVector<QuickItemGeometry>(),
131                                const QRectF &viewRect = QRectF(),
132                                qreal zoom = 1.0)
QuickDecorationsBaseRenderInfoQuickDecorationsTracesInfo133         : QuickDecorationsBaseRenderInfo(settings, viewRect, zoom)
134         , itemsGeometry(itemsGeometry)
135     { }
136 
137     const QVector<QuickItemGeometry> itemsGeometry;
138 };
139 
140 class QuickDecorationsDrawer
141 {
142 public:
143     enum Type {
144         Decorations,
145         Traces
146     };
147 
148     QuickDecorationsDrawer(QuickDecorationsDrawer::Type type, QPainter &painter,
149                            const QuickDecorationsBaseRenderInfo &renderInfo);
150 
151     void render();
152 
153 private:
154     struct DrawTextInfo {
155         DrawTextInfo(const QPen &pen = QPen(), const QRectF &rect = QRectF(),
156                      const QString &label = QString(),
157                      int align = Qt::AlignCenter | Qt::TextDontClip)
penDrawTextInfo158             : pen(pen)
159             , rect(rect)
160             , label(label)
161             , align(align)
162         { }
163 
164         QPen pen;
165         QRectF rect;
166         QString label;
167         int align;
168     };
169 
170     typedef QVector<DrawTextInfo> DrawTextInfoList;
171 
172     QuickItemGeometry itemGeometry() const;
173     QVector<QuickItemGeometry> itemsGeometry() const;
174 
175     void drawDecorations();
176     void drawTraces();
177     void drawGrid();
178     void drawArrow(const QPointF &first, const QPointF &second);
179     void drawAnchor(const QuickItemGeometry &itemGeometry, Qt::Orientation orientation,
180                     qreal ownAnchorLine, qreal offset);
181     void drawVerticalAnchor(const QuickItemGeometry &itemGeometry, qreal ownAnchorLine,
182                             qreal offset);
183     void drawHorizontalAnchor(const QuickItemGeometry &itemGeometry, qreal ownAnchorLine,
184                               qreal offset);
185     DrawTextInfo drawAnchorLabel(const QuickItemGeometry &itemGeometry, Qt::Orientation orientation,
186                                  qreal ownAnchorLine, qreal offset, const QString &label,
187                                  Qt::Alignment align);
188     DrawTextInfo drawHorizontalAnchorLabel(const QuickItemGeometry &itemGeometry, qreal ownAnchorLine,
189                                            qreal offset, const QString &label, Qt::Alignment align);
190     DrawTextInfo drawVerticalAnchorLabel(const QuickItemGeometry &itemGeometry, qreal ownAnchorLine,
191                                          qreal offset, const QString &label, Qt::Alignment align);
192 
193     const QuickDecorationsDrawer::Type m_type;
194     const QuickDecorationsBaseRenderInfo *const m_renderInfo;
195     QPainter *const m_painter;
196 };
197 }
198 
199 Q_DECLARE_METATYPE(GammaRay::QuickDecorationsSettings)
200 
201 #endif
202