Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcssutil.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qcssutil_p.h"
5#include "private/qcssparser_p.h"
6#include "qpainter.h"
7#include <qmath.h>
8
9#ifndef QT_NO_CSSPARSER
10
12
13using namespace QCss;
14
16{
18
19 switch (s) {
21 ps = Qt::DotLine;
22 break;
24 ps = width == 1 ? Qt::DotLine : Qt::DashLine;
25 break;
27 ps = Qt::DashDotLine;
28 break;
31 break;
35 ps = Qt::SolidLine;
36 break;
37 default:
38 break;
39 }
40
41 return QPen(b, width, ps, Qt::FlatCap);
42}
43
45 const QSizeF& r1, const QSizeF& r2,
46 Edge edge, BorderStyle s, QBrush c)
47{
48 const qreal pw = (edge == TopEdge || edge == BottomEdge) ? y2-y1 : x2-x1;
49 if (s == BorderStyle_Double) {
50 qreal wby3 = pw/3;
51 switch (edge) {
52 case TopEdge:
53 case BottomEdge:
54 qDrawRoundedCorners(p, x1, y1, x2, y1+wby3, r1, r2, edge, BorderStyle_Solid, c);
55 qDrawRoundedCorners(p, x1, y2-wby3, x2, y2, r1, r2, edge, BorderStyle_Solid, c);
56 break;
57 case LeftEdge:
60 break;
61 case RightEdge:
64 break;
65 default:
66 break;
67 }
68 return;
69 } else if (s == BorderStyle_Ridge || s == BorderStyle_Groove) {
71 if (s == BorderStyle_Groove) {
74 } else {
77 }
78 int pwby2 = qRound(pw/2);
79 switch (edge) {
80 case TopEdge:
81 qDrawRoundedCorners(p, x1, y1, x2, y1 + pwby2, r1, r2, TopEdge, s1, c);
82 qDrawRoundedCorners(p, x1, y1 + pwby2, x2, y2, r1, r2, TopEdge, s2, c);
83 break;
84 case BottomEdge:
85 qDrawRoundedCorners(p, x1, y1 + pwby2, x2, y2, r1, r2, BottomEdge, s1, c);
86 qDrawRoundedCorners(p, x1, y1, x2, y2-pwby2, r1, r2, BottomEdge, s2, c);
87 break;
88 case LeftEdge:
89 qDrawRoundedCorners(p, x1, y1, x1 + pwby2, y2, r1, r2, LeftEdge, s1, c);
90 qDrawRoundedCorners(p, x1 + pwby2, y1, x2, y2, r1, r2, LeftEdge, s2, c);
91 break;
92 case RightEdge:
93 qDrawRoundedCorners(p, x1 + pwby2, y1, x2, y2, r1, r2, RightEdge, s1, c);
94 qDrawRoundedCorners(p, x1, y1, x2 - pwby2, y2, r1, r2, RightEdge, s2, c);
95 break;
96 default:
97 break;
98 }
99 } else if ((s == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
100 || (s == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
101 c = c.color().lighter();
102
103 p->save();
104 qreal pwby2 = pw/2;
105 p->setBrush(Qt::NoBrush);
106 QPen pen = qPenFromStyle(c, pw, s);
107 pen.setCapStyle(Qt::SquareCap); // this eliminates the offby1 errors that we might hit below
108 p->setPen(pen);
109 switch (edge) {
110 case TopEdge:
111 if (!r1.isEmpty())
112 p->drawArc(QRectF(x1 - r1.width() + pwby2, y1 + pwby2,
113 2*r1.width() - pw, 2*r1.height() - pw), 135*16, -45*16);
114 if (!r2.isEmpty())
115 p->drawArc(QRectF(x2 - r2.width() + pwby2, y1 + pwby2,
116 2*r2.width() - pw, 2*r2.height() - pw), 45*16, 45*16);
117 break;
118 case BottomEdge:
119 if (!r1.isEmpty())
120 p->drawArc(QRectF(x1 - r1.width() + pwby2, y2 - 2*r1.height() + pwby2,
121 2*r1.width() - pw, 2*r1.height() - pw), -90 * 16, -45 * 16);
122 if (!r2.isEmpty())
123 p->drawArc(QRectF(x2 - r2.width() + pwby2, y2 - 2*r2.height() + pwby2,
124 2*r2.width() - pw, 2*r2.height() - pw), -90 * 16, 45 * 16);
125 break;
126 case LeftEdge:
127 if (!r1.isEmpty())
128 p->drawArc(QRectF(x1 + pwby2, y1 - r1.height() + pwby2,
129 2*r1.width() - pw, 2*r1.height() - pw), 135*16, 45*16);
130 if (!r2.isEmpty())
131 p->drawArc(QRectF(x1 + pwby2, y2 - r2.height() + pwby2,
132 2*r2.width() - pw, 2*r2.height() - pw), 180*16, 45*16);
133 break;
134 case RightEdge:
135 if (!r1.isEmpty())
136 p->drawArc(QRectF(x2 - 2*r1.width() + pwby2, y1 - r1.height() + pwby2,
137 2*r1.width() - pw, 2*r1.height() - pw), 45*16, -45*16);
138 if (!r2.isEmpty())
139 p->drawArc(QRectF(x2 - 2*r2.width() + pwby2, y2 - r2.height() + pwby2,
140 2*r2.width() - pw, 2*r2.height() - pw), 315*16, 45*16);
141 break;
142 default:
143 break;
144 }
145 p->restore();
146}
147
148
151{
152 p->save();
153 const qreal width = (edge == TopEdge || edge == BottomEdge) ? (y2-y1) : (x2-x1);
154
155 if (width <= 2 && style == BorderStyle_Double)
156 style = BorderStyle_Solid;
157
158 switch (style) {
161 if ((style == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
162 || (style == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
163 c = c.color().lighter();
165 case BorderStyle_Solid: {
166 p->setPen(Qt::NoPen);
167 p->setBrush(c);
168 if (width == 1 || (dw1 == 0 && dw2 == 0)) {
169 p->drawRect(QRectF(x1, y1, x2-x1, y2-y1));
170 } else { // draw trapezoid
171 std::array<QPointF, 4> quad;
172 switch (edge) {
173 case TopEdge:
174 quad = {QPointF(x1, y1), QPointF(x1 + dw1, y2),
175 QPointF(x2 - dw2, y2), QPointF(x2, y1)};
176 break;
177 case BottomEdge:
178 quad = {QPointF(x1 + dw1, y1), QPointF(x1, y2),
179 QPointF(x2, y2), QPointF(x2 - dw2, y1)};
180 break;
181 case LeftEdge:
182 quad = {QPointF(x1, y1), QPointF(x1, y2),
183 QPointF(x2, y2 - dw2), QPointF(x2, y1 + dw1)};
184 break;
185 case RightEdge:
186 quad = {QPointF(x1, y1 + dw1), QPointF(x1, y2 - dw2),
187 QPointF(x2, y2), QPointF(x2, y1)};
188 break;
189 default:
190 break;
191 }
192 p->drawConvexPolygon(quad.data(), static_cast<int>(quad.size()));
193 }
194 break;
195 }
200 p->setPen(qPenFromStyle(c, width, style));
201 if (width == 1)
202 p->drawLine(QLineF(x1, y1, x2 - 1, y2 - 1));
203 else if (edge == TopEdge || edge == BottomEdge)
204 p->drawLine(QLineF(x1 + width/2, (y1 + y2)/2, x2 - width/2, (y1 + y2)/2));
205 else
206 p->drawLine(QLineF((x1+x2)/2, y1 + width/2, (x1+x2)/2, y2 - width/2));
207 break;
208
209 case BorderStyle_Double: {
210 int wby3 = qRound(width/3);
211 int dw1by3 = qRound(dw1/3);
212 int dw2by3 = qRound(dw2/3);
213 switch (edge) {
214 case TopEdge:
215 qDrawEdge(p, x1, y1, x2, y1 + wby3, dw1by3, dw2by3, TopEdge, BorderStyle_Solid, c);
216 qDrawEdge(p, x1 + dw1 - dw1by3, y2 - wby3, x2 - dw2 + dw1by3, y2,
217 dw1by3, dw2by3, TopEdge, BorderStyle_Solid, c);
218 break;
219 case LeftEdge:
220 qDrawEdge(p, x1, y1, x1 + wby3, y2, dw1by3, dw2by3, LeftEdge, BorderStyle_Solid, c);
221 qDrawEdge(p, x2 - wby3, y1 + dw1 - dw1by3, x2, y2 - dw2 + dw2by3, dw1by3, dw2by3,
223 break;
224 case BottomEdge:
225 qDrawEdge(p, x1 + dw1 - dw1by3, y1, x2 - dw2 + dw2by3, y1 + wby3, dw1by3, dw2by3,
227 qDrawEdge(p, x1, y2 - wby3, x2, y2, dw1by3, dw2by3, BottomEdge, BorderStyle_Solid, c);
228 break;
229 case RightEdge:
230 qDrawEdge(p, x2 - wby3, y1, x2, y2, dw1by3, dw2by3, RightEdge, BorderStyle_Solid, c);
231 qDrawEdge(p, x1, y1 + dw1 - dw1by3, x1 + wby3, y2 - dw2 + dw2by3, dw1by3, dw2by3,
233 break;
234 default:
235 break;
236 }
237 break;
238 }
240 case BorderStyle_Groove: {
242 if (style == BorderStyle_Groove) {
245 } else {
248 }
249 int dw1by2 = qFloor(dw1/2), dw2by2 = qFloor(dw2/2);
250 int wby2 = qRound(width/2);
251 switch (edge) {
252 case TopEdge:
253 qDrawEdge(p, x1, y1, x2, y1 + wby2, dw1by2, dw2by2, TopEdge, s1, c);
254 qDrawEdge(p, x1 + dw1by2, y1 + wby2, x2 - dw2by2, y2, dw1by2, dw2by2, TopEdge, s2, c);
255 break;
256 case BottomEdge:
257 qDrawEdge(p, x1, y1 + wby2, x2, y2, dw1by2, dw2by2, BottomEdge, s1, c);
258 qDrawEdge(p, x1 + dw1by2, y1, x2 - dw2by2, y1 + wby2, dw1by2, dw2by2, BottomEdge, s2, c);
259 break;
260 case LeftEdge:
261 qDrawEdge(p, x1, y1, x1 + wby2, y2, dw1by2, dw2by2, LeftEdge, s1, c);
262 qDrawEdge(p, x1 + wby2, y1 + dw1by2, x2, y2 - dw2by2, dw1by2, dw2by2, LeftEdge, s2, c);
263 break;
264 case RightEdge:
265 qDrawEdge(p, x1 + wby2, y1, x2, y2, dw1by2, dw2by2, RightEdge, s1, c);
266 qDrawEdge(p, x1, y1 + dw1by2, x1 + wby2, y2 - dw2by2, dw1by2, dw2by2, RightEdge, s2, c);
267 break;
268 default:
269 break;
270 }
271 break;
272 }
273 default:
274 break;
275 }
276 p->restore();
277}
278
279void qNormalizeRadii(const QRect &br, const QSize *radii,
280 QSize *tlr, QSize *trr, QSize *blr, QSize *brr)
281{
282 *tlr = radii[0].expandedTo(QSize(0, 0));
283 *trr = radii[1].expandedTo(QSize(0, 0));
284 *blr = radii[2].expandedTo(QSize(0, 0));
285 *brr = radii[3].expandedTo(QSize(0, 0));
286 if (tlr->width() + trr->width() > br.width())
287 *tlr = *trr = QSize(0, 0);
288 if (blr->width() + brr->width() > br.width())
289 *blr = *brr = QSize(0, 0);
290 if (tlr->height() + blr->height() > br.height())
291 *tlr = *blr = QSize(0, 0);
292 if (trr->height() + brr->height() > br.height())
293 *trr = *brr = QSize(0, 0);
294}
295
296// Determines if Edge e1 draws over Edge e2. Depending on this trapezoids or rectangles are drawn
297static bool paintsOver(const QCss::BorderStyle *styles, const QBrush *colors, QCss::Edge e1, QCss::Edge e2)
298{
299 QCss::BorderStyle s1 = styles[e1];
300 QCss::BorderStyle s2 = styles[e2];
301
302 if (s2 == BorderStyle_None || colors[e2] == Qt::transparent)
303 return true;
304
305 if ((s1 == BorderStyle_Solid && s2 == BorderStyle_Solid) && (colors[e1] == colors[e2])
306 && colors[e1].isOpaque()) {
307 return true;
308 }
309
310 return false;
311}
312
313void qDrawBorder(QPainter *p, const QRect &rect, const QCss::BorderStyle *styles,
314 const int *borders, const QBrush *colors, const QSize *radii)
315{
316 const QRectF br(rect);
317 QSize tlr, trr, blr, brr;
318 qNormalizeRadii(rect, radii, &tlr, &trr, &blr, &brr);
319
320 // Drawn in increasing order of precedence
321 if (styles[BottomEdge] != BorderStyle_None && borders[BottomEdge] > 0) {
322 qreal dw1 = (blr.width() || paintsOver(styles, colors, BottomEdge, LeftEdge)) ? 0 : borders[LeftEdge];
323 qreal dw2 = (brr.width() || paintsOver(styles, colors, BottomEdge, RightEdge)) ? 0 : borders[RightEdge];
324 qreal x1 = br.x() + blr.width();
325 qreal y1 = br.y() + br.height() - borders[BottomEdge];
326 qreal x2 = br.x() + br.width() - brr.width();
327 qreal y2 = br.y() + br.height() ;
328
329 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, BottomEdge, styles[BottomEdge], colors[BottomEdge]);
330 if (blr.width() || brr.width())
332 }
333 if (styles[RightEdge] != BorderStyle_None && borders[RightEdge] > 0) {
334 qreal dw1 = (trr.height() || paintsOver(styles, colors, RightEdge, TopEdge)) ? 0 : borders[TopEdge];
335 qreal dw2 = (brr.height() || paintsOver(styles, colors, RightEdge, BottomEdge)) ? 0 : borders[BottomEdge];
336 qreal x1 = br.x() + br.width() - borders[RightEdge];
337 qreal y1 = br.y() + trr.height();
338 qreal x2 = br.x() + br.width();
339 qreal y2 = br.y() + br.height() - brr.height();
340
341 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, RightEdge, styles[RightEdge], colors[RightEdge]);
342 if (trr.height() || brr.height())
343 qDrawRoundedCorners(p, x1, y1, x2, y2, trr, brr, RightEdge, styles[RightEdge], colors[RightEdge]);
344 }
345 if (styles[LeftEdge] != BorderStyle_None && borders[LeftEdge] > 0) {
346 qreal dw1 = (tlr.height() || paintsOver(styles, colors, LeftEdge, TopEdge)) ? 0 : borders[TopEdge];
347 qreal dw2 = (blr.height() || paintsOver(styles, colors, LeftEdge, BottomEdge)) ? 0 : borders[BottomEdge];
348 qreal x1 = br.x();
349 qreal y1 = br.y() + tlr.height();
350 qreal x2 = br.x() + borders[LeftEdge];
351 qreal y2 = br.y() + br.height() - blr.height();
352
353 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, LeftEdge, styles[LeftEdge], colors[LeftEdge]);
354 if (tlr.height() || blr.height())
355 qDrawRoundedCorners(p, x1, y1, x2, y2, tlr, blr, LeftEdge, styles[LeftEdge], colors[LeftEdge]);
356 }
357 if (styles[TopEdge] != BorderStyle_None && borders[TopEdge] > 0) {
358 qreal dw1 = (tlr.width() || paintsOver(styles, colors, TopEdge, LeftEdge)) ? 0 : borders[LeftEdge];
359 qreal dw2 = (trr.width() || paintsOver(styles, colors, TopEdge, RightEdge)) ? 0 : borders[RightEdge];
360 qreal x1 = br.x() + tlr.width();
361 qreal y1 = br.y();
362 qreal x2 = br.left() + br.width() - trr.width();
363 qreal y2 = br.y() + borders[TopEdge];
364
365 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, TopEdge, styles[TopEdge], colors[TopEdge]);
366 if (tlr.width() || trr.width())
367 qDrawRoundedCorners(p, x1, y1, x2, y2, tlr, trr, TopEdge, styles[TopEdge], colors[TopEdge]);
368 }
369}
370
371#endif //QT_NO_CSSPARSER
372
\inmodule QtGui
Definition qbrush.h:30
\inmodule QtCore\compares equality \compareswith equality QLine \endcompareswith
Definition qline.h:192
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
\inmodule QtGui
Definition qpen.h:28
void setCapStyle(Qt::PenCapStyle pcs)
Sets the pen's cap style to the given style.
Definition qpen.cpp:650
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr qreal y() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:672
constexpr qreal height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:732
constexpr qreal width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:729
constexpr qreal x() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:669
constexpr qreal left() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:497
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr bool isEmpty() const noexcept
Returns true if the rectangle is empty, otherwise returns false.
Definition qrect.h:167
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:239
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:236
\inmodule QtCore
Definition qsize.h:208
\inmodule QtCore
Definition qsize.h:25
rect
[4]
@ BottomEdge
@ RightEdge
@ BorderStyle_Dotted
@ BorderStyle_Solid
@ BorderStyle_Double
@ BorderStyle_DotDash
@ BorderStyle_Ridge
@ BorderStyle_Dashed
@ BorderStyle_Outset
@ BorderStyle_Groove
@ BorderStyle_None
@ BorderStyle_DotDotDash
@ BorderStyle_Inset
Combined button and popup list for selecting options.
@ transparent
Definition qnamespace.h:47
@ DashDotDotLine
@ DotLine
@ SolidLine
@ DashDotLine
@ DashLine
@ NoPen
@ NoBrush
@ SquareCap
@ FlatCap
#define Q_FALLTHROUGH()
void qNormalizeRadii(const QRect &br, const QSize *radii, QSize *tlr, QSize *trr, QSize *blr, QSize *brr)
Definition qcssutil.cpp:279
void qDrawBorder(QPainter *p, const QRect &rect, const QCss::BorderStyle *styles, const int *borders, const QBrush *colors, const QSize *radii)
Definition qcssutil.cpp:313
static QPen qPenFromStyle(const QBrush &b, qreal width, BorderStyle s)
Definition qcssutil.cpp:15
void qDrawRoundedCorners(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, const QSizeF &r1, const QSizeF &r2, Edge edge, BorderStyle s, QBrush c)
Definition qcssutil.cpp:44
static bool paintsOver(const QCss::BorderStyle *styles, const QBrush *colors, QCss::Edge e1, QCss::Edge e2)
Definition qcssutil.cpp:297
void qDrawEdge(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, qreal dw1, qreal dw2, QCss::Edge edge, QCss::BorderStyle style, QBrush c)
Definition qcssutil.cpp:149
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
int qFloor(T v)
Definition qmath.h:42
GLboolean GLboolean GLboolean b
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLuint GLfloat GLfloat GLfloat x1
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
GLint GLsizei width
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
GLfloat GLfloat p
[1]
static QT_BEGIN_NAMESPACE const QRgb colors[][14]
static const struct TessellationModeTab quad[]
#define s2
#define s1
double qreal
Definition qtypes.h:187
QRect r1(100, 200, 11, 16)
[0]
QRect r2(QPoint(100, 200), QSize(11, 16))