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
11QT_BEGIN_NAMESPACE
12
13using namespace QCss;
14
15static QPen qPenFromStyle(const QBrush& b, qreal width, BorderStyle s)
16{
17 Qt::PenStyle ps = Qt::NoPen;
18
19 switch (s) {
20 case BorderStyle_Dotted:
21 ps = Qt::DotLine;
22 break;
23 case BorderStyle_Dashed:
24 ps = width == 1 ? Qt::DotLine : Qt::DashLine;
25 break;
26 case BorderStyle_DotDash:
27 ps = Qt::DashDotLine;
28 break;
29 case BorderStyle_DotDotDash:
30 ps = Qt::DashDotDotLine;
31 break;
32 case BorderStyle_Inset:
33 case BorderStyle_Outset:
34 case BorderStyle_Solid:
35 ps = Qt::SolidLine;
36 break;
37 default:
38 break;
39 }
40
41 return QPen(b, width, ps, Qt::FlatCap);
42}
43
44void qDrawRoundedCorners(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2,
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, y2: y1+wby3, r1, r2, edge, s: BorderStyle_Solid, c);
55 qDrawRoundedCorners(p, x1, y1: y2-wby3, x2, y2, r1, r2, edge, s: BorderStyle_Solid, c);
56 break;
57 case LeftEdge:
58 qDrawRoundedCorners(p, x1, y1: y1+1, x2: x1+wby3, y2, r1, r2, edge: LeftEdge, s: BorderStyle_Solid, c);
59 qDrawRoundedCorners(p, x1: x2-wby3, y1: y1+1, x2, y2, r1, r2, edge: LeftEdge, s: BorderStyle_Solid, c);
60 break;
61 case RightEdge:
62 qDrawRoundedCorners(p, x1, y1: y1+1, x2: x1+wby3, y2, r1, r2, edge: RightEdge, s: BorderStyle_Solid, c);
63 qDrawRoundedCorners(p, x1: x2-wby3, y1: y1+1, x2, y2, r1, r2, edge: RightEdge, s: BorderStyle_Solid, c);
64 break;
65 default:
66 break;
67 }
68 return;
69 } else if (s == BorderStyle_Ridge || s == BorderStyle_Groove) {
70 BorderStyle s1, s2;
71 if (s == BorderStyle_Groove) {
72 s1 = BorderStyle_Inset;
73 s2 = BorderStyle_Outset;
74 } else {
75 s1 = BorderStyle_Outset;
76 s2 = BorderStyle_Inset;
77 }
78 int pwby2 = qRound(d: pw/2);
79 switch (edge) {
80 case TopEdge:
81 qDrawRoundedCorners(p, x1, y1, x2, y2: y1 + pwby2, r1, r2, edge: TopEdge, s: s1, c);
82 qDrawRoundedCorners(p, x1, y1: y1 + pwby2, x2, y2, r1, r2, edge: TopEdge, s: s2, c);
83 break;
84 case BottomEdge:
85 qDrawRoundedCorners(p, x1, y1: y1 + pwby2, x2, y2, r1, r2, edge: BottomEdge, s: s1, c);
86 qDrawRoundedCorners(p, x1, y1, x2, y2: y2-pwby2, r1, r2, edge: BottomEdge, s: s2, c);
87 break;
88 case LeftEdge:
89 qDrawRoundedCorners(p, x1, y1, x2: x1 + pwby2, y2, r1, r2, edge: LeftEdge, s: s1, c);
90 qDrawRoundedCorners(p, x1: x1 + pwby2, y1, x2, y2, r1, r2, edge: LeftEdge, s: s2, c);
91 break;
92 case RightEdge:
93 qDrawRoundedCorners(p, x1: x1 + pwby2, y1, x2, y2, r1, r2, edge: RightEdge, s: s1, c);
94 qDrawRoundedCorners(p, x1, y1, x2: x2 - pwby2, y2, r1, r2, edge: RightEdge, s: 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(b: c, width: 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(rect: QRectF(x1 - r1.width() + pwby2, y1 + pwby2,
113 2*r1.width() - pw, 2*r1.height() - pw), a: 135*16, alen: -45*16);
114 if (!r2.isEmpty())
115 p->drawArc(rect: QRectF(x2 - r2.width() + pwby2, y1 + pwby2,
116 2*r2.width() - pw, 2*r2.height() - pw), a: 45*16, alen: 45*16);
117 break;
118 case BottomEdge:
119 if (!r1.isEmpty())
120 p->drawArc(rect: QRectF(x1 - r1.width() + pwby2, y2 - 2*r1.height() + pwby2,
121 2*r1.width() - pw, 2*r1.height() - pw), a: -90 * 16, alen: -45 * 16);
122 if (!r2.isEmpty())
123 p->drawArc(rect: QRectF(x2 - r2.width() + pwby2, y2 - 2*r2.height() + pwby2,
124 2*r2.width() - pw, 2*r2.height() - pw), a: -90 * 16, alen: 45 * 16);
125 break;
126 case LeftEdge:
127 if (!r1.isEmpty())
128 p->drawArc(rect: QRectF(x1 + pwby2, y1 - r1.height() + pwby2,
129 2*r1.width() - pw, 2*r1.height() - pw), a: 135*16, alen: 45*16);
130 if (!r2.isEmpty())
131 p->drawArc(rect: QRectF(x1 + pwby2, y2 - r2.height() + pwby2,
132 2*r2.width() - pw, 2*r2.height() - pw), a: 180*16, alen: 45*16);
133 break;
134 case RightEdge:
135 if (!r1.isEmpty())
136 p->drawArc(rect: QRectF(x2 - 2*r1.width() + pwby2, y1 - r1.height() + pwby2,
137 2*r1.width() - pw, 2*r1.height() - pw), a: 45*16, alen: -45*16);
138 if (!r2.isEmpty())
139 p->drawArc(rect: QRectF(x2 - 2*r2.width() + pwby2, y2 - r2.height() + pwby2,
140 2*r2.width() - pw, 2*r2.height() - pw), a: 315*16, alen: 45*16);
141 break;
142 default:
143 break;
144 }
145 p->restore();
146}
147
148
149void qDrawEdge(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, qreal dw1, qreal dw2,
150 QCss::Edge edge, QCss::BorderStyle style, QBrush c)
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) {
159 case BorderStyle_Inset:
160 case BorderStyle_Outset:
161 if ((style == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
162 || (style == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
163 c = c.color().lighter();
164 Q_FALLTHROUGH();
165 case BorderStyle_Solid: {
166 p->setPen(Qt::NoPen);
167 p->setBrush(c);
168 if (width == 1 || (dw1 == 0 && dw2 == 0)) {
169 p->drawRect(rect: QRectF(x1, y1, x2-x1, y2-y1));
170 } else { // draw trapezoid
171 QPolygonF 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(poly: quad);
193 }
194 break;
195 }
196 case BorderStyle_Dotted:
197 case BorderStyle_Dashed:
198 case BorderStyle_DotDash:
199 case BorderStyle_DotDotDash:
200 p->setPen(qPenFromStyle(b: c, width, s: style));
201 if (width == 1)
202 p->drawLine(l: QLineF(x1, y1, x2 - 1, y2 - 1));
203 else if (edge == TopEdge || edge == BottomEdge)
204 p->drawLine(l: QLineF(x1 + width/2, (y1 + y2)/2, x2 - width/2, (y1 + y2)/2));
205 else
206 p->drawLine(l: QLineF((x1+x2)/2, y1 + width/2, (x1+x2)/2, y2 - width/2));
207 break;
208
209 case BorderStyle_Double: {
210 int wby3 = qRound(d: width/3);
211 int dw1by3 = qRound(d: dw1/3);
212 int dw2by3 = qRound(d: dw2/3);
213 switch (edge) {
214 case TopEdge:
215 qDrawEdge(p, x1, y1, x2, y2: y1 + wby3, dw1: dw1by3, dw2: dw2by3, edge: TopEdge, style: BorderStyle_Solid, c);
216 qDrawEdge(p, x1: x1 + dw1 - dw1by3, y1: y2 - wby3, x2: x2 - dw2 + dw1by3, y2,
217 dw1: dw1by3, dw2: dw2by3, edge: TopEdge, style: BorderStyle_Solid, c);
218 break;
219 case LeftEdge:
220 qDrawEdge(p, x1, y1, x2: x1 + wby3, y2, dw1: dw1by3, dw2: dw2by3, edge: LeftEdge, style: BorderStyle_Solid, c);
221 qDrawEdge(p, x1: x2 - wby3, y1: y1 + dw1 - dw1by3, x2, y2: y2 - dw2 + dw2by3, dw1: dw1by3, dw2: dw2by3,
222 edge: LeftEdge, style: BorderStyle_Solid, c);
223 break;
224 case BottomEdge:
225 qDrawEdge(p, x1: x1 + dw1 - dw1by3, y1, x2: x2 - dw2 + dw2by3, y2: y1 + wby3, dw1: dw1by3, dw2: dw2by3,
226 edge: BottomEdge, style: BorderStyle_Solid, c);
227 qDrawEdge(p, x1, y1: y2 - wby3, x2, y2, dw1: dw1by3, dw2: dw2by3, edge: BottomEdge, style: BorderStyle_Solid, c);
228 break;
229 case RightEdge:
230 qDrawEdge(p, x1: x2 - wby3, y1, x2, y2, dw1: dw1by3, dw2: dw2by3, edge: RightEdge, style: BorderStyle_Solid, c);
231 qDrawEdge(p, x1, y1: y1 + dw1 - dw1by3, x2: x1 + wby3, y2: y2 - dw2 + dw2by3, dw1: dw1by3, dw2: dw2by3,
232 edge: RightEdge, style: BorderStyle_Solid, c);
233 break;
234 default:
235 break;
236 }
237 break;
238 }
239 case BorderStyle_Ridge:
240 case BorderStyle_Groove: {
241 BorderStyle s1, s2;
242 if (style == BorderStyle_Groove) {
243 s1 = BorderStyle_Inset;
244 s2 = BorderStyle_Outset;
245 } else {
246 s1 = BorderStyle_Outset;
247 s2 = BorderStyle_Inset;
248 }
249 int dw1by2 = qFloor(v: dw1/2), dw2by2 = qFloor(v: dw2/2);
250 int wby2 = qRound(d: width/2);
251 switch (edge) {
252 case TopEdge:
253 qDrawEdge(p, x1, y1, x2, y2: y1 + wby2, dw1: dw1by2, dw2: dw2by2, edge: TopEdge, style: s1, c);
254 qDrawEdge(p, x1: x1 + dw1by2, y1: y1 + wby2, x2: x2 - dw2by2, y2, dw1: dw1by2, dw2: dw2by2, edge: TopEdge, style: s2, c);
255 break;
256 case BottomEdge:
257 qDrawEdge(p, x1, y1: y1 + wby2, x2, y2, dw1: dw1by2, dw2: dw2by2, edge: BottomEdge, style: s1, c);
258 qDrawEdge(p, x1: x1 + dw1by2, y1, x2: x2 - dw2by2, y2: y1 + wby2, dw1: dw1by2, dw2: dw2by2, edge: BottomEdge, style: s2, c);
259 break;
260 case LeftEdge:
261 qDrawEdge(p, x1, y1, x2: x1 + wby2, y2, dw1: dw1by2, dw2: dw2by2, edge: LeftEdge, style: s1, c);
262 qDrawEdge(p, x1: x1 + wby2, y1: y1 + dw1by2, x2, y2: y2 - dw2by2, dw1: dw1by2, dw2: dw2by2, edge: LeftEdge, style: s2, c);
263 break;
264 case RightEdge:
265 qDrawEdge(p, x1: x1 + wby2, y1, x2, y2, dw1: dw1by2, dw2: dw2by2, edge: RightEdge, style: s1, c);
266 qDrawEdge(p, x1, y1: y1 + dw1by2, x2: x1 + wby2, y2: y2 - dw2by2, dw1: dw1by2, dw2: dw2by2, edge: RightEdge, style: s2, c);
267 break;
268 default:
269 break;
270 }
271 }
272 default:
273 break;
274 }
275 p->restore();
276}
277
278void qNormalizeRadii(const QRect &br, const QSize *radii,
279 QSize *tlr, QSize *trr, QSize *blr, QSize *brr)
280{
281 *tlr = radii[0].expandedTo(otherSize: QSize(0, 0));
282 *trr = radii[1].expandedTo(otherSize: QSize(0, 0));
283 *blr = radii[2].expandedTo(otherSize: QSize(0, 0));
284 *brr = radii[3].expandedTo(otherSize: QSize(0, 0));
285 if (tlr->width() + trr->width() > br.width())
286 *tlr = *trr = QSize(0, 0);
287 if (blr->width() + brr->width() > br.width())
288 *blr = *brr = QSize(0, 0);
289 if (tlr->height() + blr->height() > br.height())
290 *tlr = *blr = QSize(0, 0);
291 if (trr->height() + brr->height() > br.height())
292 *trr = *brr = QSize(0, 0);
293}
294
295// Determines if Edge e1 draws over Edge e2. Depending on this trapezoids or rectangles are drawn
296static bool paintsOver(const QCss::BorderStyle *styles, const QBrush *colors, QCss::Edge e1, QCss::Edge e2)
297{
298 QCss::BorderStyle s1 = styles[e1];
299 QCss::BorderStyle s2 = styles[e2];
300
301 if (s2 == BorderStyle_None || colors[e2] == Qt::transparent)
302 return true;
303
304 if ((s1 == BorderStyle_Solid && s2 == BorderStyle_Solid) && (colors[e1] == colors[e2])
305 && colors[e1].isOpaque()) {
306 return true;
307 }
308
309 return false;
310}
311
312void qDrawBorder(QPainter *p, const QRect &rect, const QCss::BorderStyle *styles,
313 const int *borders, const QBrush *colors, const QSize *radii)
314{
315 const QRectF br(rect);
316 QSize tlr, trr, blr, brr;
317 qNormalizeRadii(br: rect, radii, tlr: &tlr, trr: &trr, blr: &blr, brr: &brr);
318
319 // Drawn in increasing order of precedence
320 if (styles[BottomEdge] != BorderStyle_None && borders[BottomEdge] > 0) {
321 qreal dw1 = (blr.width() || paintsOver(styles, colors, e1: BottomEdge, e2: LeftEdge)) ? 0 : borders[LeftEdge];
322 qreal dw2 = (brr.width() || paintsOver(styles, colors, e1: BottomEdge, e2: RightEdge)) ? 0 : borders[RightEdge];
323 qreal x1 = br.x() + blr.width();
324 qreal y1 = br.y() + br.height() - borders[BottomEdge];
325 qreal x2 = br.x() + br.width() - brr.width();
326 qreal y2 = br.y() + br.height() ;
327
328 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, edge: BottomEdge, style: styles[BottomEdge], c: colors[BottomEdge]);
329 if (blr.width() || brr.width())
330 qDrawRoundedCorners(p, x1, y1, x2, y2, r1: blr, r2: brr, edge: BottomEdge, s: styles[BottomEdge], c: colors[BottomEdge]);
331 }
332 if (styles[RightEdge] != BorderStyle_None && borders[RightEdge] > 0) {
333 qreal dw1 = (trr.height() || paintsOver(styles, colors, e1: RightEdge, e2: TopEdge)) ? 0 : borders[TopEdge];
334 qreal dw2 = (brr.height() || paintsOver(styles, colors, e1: RightEdge, e2: BottomEdge)) ? 0 : borders[BottomEdge];
335 qreal x1 = br.x() + br.width() - borders[RightEdge];
336 qreal y1 = br.y() + trr.height();
337 qreal x2 = br.x() + br.width();
338 qreal y2 = br.y() + br.height() - brr.height();
339
340 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, edge: RightEdge, style: styles[RightEdge], c: colors[RightEdge]);
341 if (trr.height() || brr.height())
342 qDrawRoundedCorners(p, x1, y1, x2, y2, r1: trr, r2: brr, edge: RightEdge, s: styles[RightEdge], c: colors[RightEdge]);
343 }
344 if (styles[LeftEdge] != BorderStyle_None && borders[LeftEdge] > 0) {
345 qreal dw1 = (tlr.height() || paintsOver(styles, colors, e1: LeftEdge, e2: TopEdge)) ? 0 : borders[TopEdge];
346 qreal dw2 = (blr.height() || paintsOver(styles, colors, e1: LeftEdge, e2: BottomEdge)) ? 0 : borders[BottomEdge];
347 qreal x1 = br.x();
348 qreal y1 = br.y() + tlr.height();
349 qreal x2 = br.x() + borders[LeftEdge];
350 qreal y2 = br.y() + br.height() - blr.height();
351
352 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, edge: LeftEdge, style: styles[LeftEdge], c: colors[LeftEdge]);
353 if (tlr.height() || blr.height())
354 qDrawRoundedCorners(p, x1, y1, x2, y2, r1: tlr, r2: blr, edge: LeftEdge, s: styles[LeftEdge], c: colors[LeftEdge]);
355 }
356 if (styles[TopEdge] != BorderStyle_None && borders[TopEdge] > 0) {
357 qreal dw1 = (tlr.width() || paintsOver(styles, colors, e1: TopEdge, e2: LeftEdge)) ? 0 : borders[LeftEdge];
358 qreal dw2 = (trr.width() || paintsOver(styles, colors, e1: TopEdge, e2: RightEdge)) ? 0 : borders[RightEdge];
359 qreal x1 = br.x() + tlr.width();
360 qreal y1 = br.y();
361 qreal x2 = br.left() + br.width() - trr.width();
362 qreal y2 = br.y() + borders[TopEdge];
363
364 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, edge: TopEdge, style: styles[TopEdge], c: colors[TopEdge]);
365 if (tlr.width() || trr.width())
366 qDrawRoundedCorners(p, x1, y1, x2, y2, r1: tlr, r2: trr, edge: TopEdge, s: styles[TopEdge], c: colors[TopEdge]);
367 }
368}
369
370#endif //QT_NO_CSSPARSER
371
372QT_END_NAMESPACE
373

source code of qtbase/src/gui/painting/qcssutil.cpp