qrenderrule.h
12.3 KB
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
#ifndef QRENDERRULE_H
#define QRENDERRULE_H
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qvector.h"
#include "CssParser.h"
#include "qset.h"
namespace QtCss {
struct QStyleSheetBorderImageData : public QSharedData
{
QStyleSheetBorderImageData()
: horizStretch(QtCss::TileMode_Unknown), vertStretch(QtCss::TileMode_Unknown)
{
for (int i = 0; i < 4; i++)
cuts[i] = -1;
}
int cuts[4];
QPixmap pixmap;
QImage image;
QtCss::TileMode horizStretch, vertStretch;
};
struct QStyleSheetBackgroundData : public QSharedData
{
QStyleSheetBackgroundData(const QBrush& b, const QPixmap& p, QtCss::Repeat r,
Qt::Alignment a, QtCss::Origin o, QtCss::Attachment t, QtCss::Origin c)
: brush(b), pixmap(p), repeat(r), position(a), origin(o), attachment(t), clip(c) { }
bool isTransparent() const {
if (brush.style() != Qt::NoBrush)
return !brush.isOpaque();
return pixmap.isNull() ? false : pixmap.hasAlpha();
}
QBrush brush;
QPixmap pixmap;
QtCss::Repeat repeat;
Qt::Alignment position;
QtCss::Origin origin;
QtCss::Attachment attachment;
QtCss::Origin clip;
};
struct QStyleSheetBorderData : public QSharedData
{
QStyleSheetBorderData() : bi(0)
{
for (int i = 0; i < 4; i++) {
borders[i] = 0;
styles[i] = QtCss::BorderStyle_None;
}
}
QStyleSheetBorderData(int *b, QBrush *c, QtCss::BorderStyle *s, QSize *r) : bi(0)
{
for (int i = 0; i < 4; i++) {
borders[i] = b[i];
styles[i] = s[i];
colors[i] = c[i];
radii[i] = r[i];
}
}
int borders[4];
QBrush colors[4];
QtCss::BorderStyle styles[4];
QSize radii[4]; // topleft, topright, bottomleft, bottomright
const QStyleSheetBorderImageData *borderImage() const
{ return bi; }
bool hasBorderImage() const { return bi!=0; }
QSharedDataPointer<QStyleSheetBorderImageData> bi;
bool isOpaque() const
{
for (int i = 0; i < 4; i++) {
if (styles[i] == QtCss::BorderStyle_Native || styles[i] == QtCss::BorderStyle_None)
continue;
if (styles[i] >= QtCss::BorderStyle_Dotted && styles[i] <= QtCss::BorderStyle_DotDotDash
&& styles[i] != QtCss::BorderStyle_Solid)
return false;
if (!colors[i].isOpaque())
return false;
if (!radii[i].isEmpty())
return false;
}
if (bi != 0 && bi->pixmap.hasAlpha())
return false;
return true;
}
};
struct QStyleSheetOutlineData : public QStyleSheetBorderData
{
QStyleSheetOutlineData()
{
for (int i = 0; i < 4; i++) {
offsets[i] = 0;
}
}
QStyleSheetOutlineData(int *b, QBrush *c, QtCss::BorderStyle *s, QSize *r, int *o)
: QStyleSheetBorderData(b, c, s, r)
{
for (int i = 0; i < 4; i++) {
offsets[i] = o[i];
}
}
int offsets[4];
};
struct QStyleSheetBoxData : public QSharedData
{
QStyleSheetBoxData(int *m, int *p, int s) : spacing(s)
{
for (int i = 0; i < 4; i++) {
margins[i] = m[i];
paddings[i] = p[i];
}
}
int margins[4];
int paddings[4];
int spacing;
};
struct QStyleSheetPaletteData : public QSharedData
{
QStyleSheetPaletteData(const QBrush &fg, const QBrush &sfg, const QBrush &sbg,
const QBrush &abg)
: foreground(fg), selectionForeground(sfg), selectionBackground(sbg),
alternateBackground(abg) { }
QBrush foreground;
QBrush selectionForeground;
QBrush selectionBackground;
QBrush alternateBackground;
};
struct QStyleSheetGeometryData : public QSharedData
{
QStyleSheetGeometryData(int w, int h, int minw, int minh, int maxw, int maxh)
: minWidth(minw), minHeight(minh), width(w), height(h), maxWidth(maxw), maxHeight(maxh) { }
int minWidth, minHeight, width, height, maxWidth, maxHeight;
};
struct QStyleSheetPositionData : public QSharedData
{
QStyleSheetPositionData(int l, int t, int r, int b, QtCss::Origin o, Qt::Alignment p, QtCss::PositionMode m, Qt::Alignment a = 0)
: left(l), top(t), bottom(b), right(r), origin(o), position(p), mode(m), textAlignment(a) { }
int left, top, bottom, right;
QtCss::Origin origin;
Qt::Alignment position;
QtCss::PositionMode mode;
Qt::Alignment textAlignment;
};
struct QStyleSheetImageData : public QSharedData
{
QStyleSheetImageData(const QIcon &i, Qt::Alignment a, const QSize &sz)
: icon(i), alignment(a), size(sz) { }
QIcon icon;
Qt::Alignment alignment;
QSize size;
};
class QRenderRule
{
public:
QRenderRule() : features(0), hasFont(false), pal(0), b(0), bg(0), bd(0), ou(0), geo(0), p(0), img(0), clipset(0) { }
QRenderRule(const QVector<QtCss::Declaration> &, const QWidget *);
~QRenderRule() { }
QRect borderRect(const QRect &r) const;
QRect outlineRect(const QRect &r) const;
QRect paddingRect(const QRect &r) const;
QRect contentsRect(const QRect &r) const;
enum { Margin = 1, Border = 2, Padding = 4, All=Margin|Border|Padding };
QRect boxRect(const QRect &r, int flags = All) const;
QSize boxSize(const QSize &s, int flags = All) const;
QRect originRect(const QRect &rect, QtCss::Origin origin) const;
QPainterPath borderClip(QRect rect);
void drawBorder(QPainter *, const QRect&);
void drawOutline(QPainter *, const QRect&);
void drawBorderImage(QPainter *, const QRect&);
void drawBackground(QPainter *, const QRect&, const QPoint& = QPoint(0, 0));
void drawBackgroundImage(QPainter *, const QRect&, QPoint = QPoint(0, 0));
void drawFrame(QPainter *, const QRect&);
void drawImage(QPainter *p, const QRect &rect);
void drawRule(QPainter *, const QRect&);
void configurePalette(QPalette *, QPalette::ColorGroup, const QWidget *, bool);
void configurePalette(QPalette *p, QPalette::ColorRole fr, QPalette::ColorRole br);
const QStyleSheetPaletteData *palette() const { return pal; }
const QStyleSheetBoxData *box() const { return b; }
const QStyleSheetBackgroundData *background() const { return bg; }
const QStyleSheetBorderData *border() const { return bd; }
const QStyleSheetOutlineData *outline() const { return ou; }
const QStyleSheetGeometryData *geometry() const { return geo; }
const QStyleSheetPositionData *position() const { return p; }
bool hasPalette() const { return pal != 0; }
bool hasBackground() const { return bg != 0 && (!bg->pixmap.isNull() || bg->brush.style() != Qt::NoBrush); }
bool hasGradientBackground() const { return bg && bg->brush.style() >= Qt::LinearGradientPattern
&& bg->brush.style() <= Qt::ConicalGradientPattern; }
bool hasNativeBorder() const {
return bd == 0
|| (!bd->hasBorderImage() && bd->styles[0] == QtCss::BorderStyle_Native);
}
bool hasNativeOutline() const {
return (ou == 0
|| (!ou->hasBorderImage() && ou->styles[0] == QtCss::BorderStyle_Native));
}
bool baseStyleCanDraw() const {
if (!hasBackground() || (background()->brush.style() == Qt::NoBrush && bg->pixmap.isNull()))
return true;
if (bg && !bg->pixmap.isNull())
return false;
if (hasGradientBackground())
return features & QtCss::StyleFeature_BackgroundGradient;
return features & QtCss::StyleFeature_BackgroundColor;
}
bool hasBox() const { return b != 0; }
bool hasBorder() const { return bd != 0; }
bool hasOutline() const { return ou != 0; }
bool hasPosition() const { return p != 0; }
bool hasGeometry() const { return geo != 0; }
bool hasDrawable() const { return !hasNativeBorder() || hasBackground() || hasImage(); }
bool hasImage() const { return img != 0; }
QSize minimumContentsSize() const
{ return geo ? QSize(geo->minWidth, geo->minHeight) : QSize(0, 0); }
QSize minimumSize() const
{ return boxSize(minimumContentsSize()); }
QSize contentsSize() const
{ return geo ? QSize(geo->width, geo->height)
: ((img && img->size.isValid()) ? img->size : QSize()); }
QSize contentsSize(const QSize &sz) const
{
QSize csz = contentsSize();
if (csz.width() == -1) csz.setWidth(sz.width());
if (csz.height() == -1) csz.setHeight(sz.height());
return csz;
}
bool hasContentsSize() const
{ return (geo && (geo->width != -1 || geo->height != -1)) || (img && img->size.isValid()); }
QSize size() const { return boxSize(contentsSize()); }
QSize size(const QSize &sz) const { return boxSize(contentsSize(sz)); }
QSize adjustSize(const QSize &sz)
{
if (!geo)
return sz;
QSize csz = contentsSize();
if (csz.width() == -1) csz.setWidth(sz.width());
if (csz.height() == -1) csz.setHeight(sz.height());
if (geo->maxWidth != -1 && csz.width() > geo->maxWidth) csz.setWidth(geo->maxWidth);
if (geo->maxHeight != -1 && csz.height() > geo->maxHeight) csz.setHeight(geo->maxHeight);
csz=csz.expandedTo(QSize(geo->minWidth, geo->minHeight));
return csz;
}
int features;
QBrush defaultBackground;
QFont font;
bool hasFont;
QHash<QString, QVariant> styleHints;
bool hasStyleHint(const QString& sh) const { return styleHints.contains(sh); }
QVariant styleHint(const QString& sh) const { return styleHints.value(sh); }
void fixupBorder(int);
QSharedDataPointer<QStyleSheetPaletteData> pal;
QSharedDataPointer<QStyleSheetBoxData> b;
QSharedDataPointer<QStyleSheetBackgroundData> bg;
QSharedDataPointer<QStyleSheetBorderData> bd;
QSharedDataPointer<QStyleSheetOutlineData> ou;
QSharedDataPointer<QStyleSheetGeometryData> geo;
QSharedDataPointer<QStyleSheetPositionData> p;
QSharedDataPointer<QStyleSheetImageData> img;
// Shouldn't be here
void setClip(QPainter *p, const QRect &rect);
void unsetClip(QPainter *);
int clipset;
QPainterPath clipPath;
};
typedef QSharedPointer<QRenderRule> QRenderRulePtr;
}
#endif // QRENDERRULE_H