Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
5#include "qcssutil_p.h"
6#include "private/qcssparser_p.h"
7#include "qpainter.h"
8#include <qmath.h>
9
10#ifndef QT_NO_CSSPARSER
11
13
14using namespace QCss;
15
16static QPen qPenFromStyle(const QBrush& b, qreal width, BorderStyle s)
17{
18 Qt::PenStyle ps = Qt::NoPen;
19
20 switch (s) {
21 case BorderStyle_Dotted:
22 ps = Qt::DotLine;
23 break;
24 case BorderStyle_Dashed:
25 ps = Qt::DashLine;
26 break;
27 case BorderStyle_DotDash:
28 ps = Qt::DashDotLine;
29 break;
30 case BorderStyle_DotDotDash:
31 ps = Qt::DashDotDotLine;
32 break;
33 case BorderStyle_Inset:
34 case BorderStyle_Outset:
35 case BorderStyle_Solid:
36 ps = Qt::SolidLine;
37 break;
38 default:
39 break;
40 }
41
42 return QPen(b, width, ps, Qt::FlatCap);
43}
44
45void qDrawRoundedCorners(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2,
46 const QSizeF& r1, const QSizeF& r2,
47 Edge edge, BorderStyle s, QBrush c)
48{
49 const qreal pw = (edge == TopEdge || edge == BottomEdge) ? y2-y1 : x2-x1;
50 if (s == BorderStyle_Double) {
51 qreal wby3 = pw/3;
52 switch (edge) {
53 case TopEdge:
54 case BottomEdge:
55 qDrawRoundedCorners(p, x1, y1, x2, y1+wby3, r1, r2, edge, BorderStyle_Solid, c);
56 qDrawRoundedCorners(p, x1, y2-wby3, x2, y2, r1, r2, edge, BorderStyle_Solid, c);
57 break;
58 case LeftEdge:
59 qDrawRoundedCorners(p, x1, y1+1, x1+wby3, y2, r1, r2, LeftEdge, BorderStyle_Solid, c);
60 qDrawRoundedCorners(p, x2-wby3, y1+1, x2, y2, r1, r2, LeftEdge, BorderStyle_Solid, c);
61 break;
62 case RightEdge:
63 qDrawRoundedCorners(p, x1, y1+1, x1+wby3, y2, r1, r2, RightEdge, BorderStyle_Solid, c);
64 qDrawRoundedCorners(p, x2-wby3, y1+1, x2, y2, r1, r2, RightEdge, BorderStyle_Solid, c);
65 break;
66 default:
67 break;
68 }
69 return;
70 } else if (s == BorderStyle_Ridge || s == BorderStyle_Groove) {
71 BorderStyle s1, s2;
72 if (s == BorderStyle_Groove) {
73 s1 = BorderStyle_Inset;
74 s2 = BorderStyle_Outset;
75 } else {
76 s1 = BorderStyle_Outset;
77 s2 = BorderStyle_Inset;
78 }
79 int pwby2 = qRound(pw/2);
80 switch (edge) {
81 case TopEdge:
82 qDrawRoundedCorners(p, x1, y1, x2, y1 + pwby2, r1, r2, TopEdge, s1, c);
83 qDrawRoundedCorners(p, x1, y1 + pwby2, x2, y2, r1, r2, TopEdge, s2, c);
84 break;
85 case BottomEdge:
86 qDrawRoundedCorners(p, x1, y1 + pwby2, x2, y2, r1, r2, BottomEdge, s1, c);
87 qDrawRoundedCorners(p, x1, y1, x2, y2-pwby2, r1, r2, BottomEdge, s2, c);
88 break;
89 case LeftEdge:
90 qDrawRoundedCorners(p, x1, y1, x1 + pwby2, y2, r1, r2, LeftEdge, s1, c);
91 qDrawRoundedCorners(p, x1 + pwby2, y1, x2, y2, r1, r2, LeftEdge, s2, c);
92 break;
93 case RightEdge:
94 qDrawRoundedCorners(p, x1 + pwby2, y1, x2, y2, r1, r2, RightEdge, s1, c);
95 qDrawRoundedCorners(p, x1, y1, x2 - pwby2, y2, r1, r2, RightEdge, s2, c);
96 break;
97 default:
98 break;
99 }
100 } else if ((s == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
101 || (s == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
102 c = c.color().lighter();
103
104 p->save();
105 qreal pwby2 = pw/2;
106 p->setBrush(Qt::NoBrush);
107 QPen pen = qPenFromStyle(c, pw, s);
108 pen.setCapStyle(Qt::SquareCap); // this eliminates the offby1 errors that we might hit below
109 p->setPen(pen);
110 switch (edge) {
111 case TopEdge:
112 if (!r1.isEmpty())
113 p->drawArc(QRectF(x1 - r1.width() + pwby2, y1 + pwby2,
114 2*r1.width() - pw, 2*r1.height() - pw), 135*16, -45*16);
115 if (!r2.isEmpty())
116 p->drawArc(QRectF(x2 - r2.width() + pwby2, y1 + pwby2,
117 2*r2.width() - pw, 2*r2.height() - pw), 45*16, 45*16);
118 break;
119 case BottomEdge:
120 if (!r1.isEmpty())
121 p->drawArc(QRectF(x1 - r1.width() + pwby2, y2 - 2*r1.height() + pwby2,
122 2*r1.width() - pw, 2*r1.height() - pw), -90 * 16, -45 * 16);
123 if (!r2.isEmpty())
124 p->drawArc(QRectF(x2 - r2.width() + pwby2, y2 - 2*r2.height() + pwby2,
125 2*r2.width() - pw, 2*r2.height() - pw), -90 * 16, 45 * 16);
126 break;
127 case LeftEdge:
128 if (!r1.isEmpty())
129 p->drawArc(QRectF(x1 + pwby2, y1 - r1.height() + pwby2,
130 2*r1.width() - pw, 2*r1.height() - pw), 135*16, 45*16);
131 if (!r2.isEmpty())
132 p->drawArc(QRectF(x1 + pwby2, y2 - r2.height() + pwby2,
133 2*r2.width() - pw, 2*r2.height() - pw), 180*16, 45*16);
134 break;
135 case RightEdge:
136 if (!r1.isEmpty())
137 p->drawArc(QRectF(x2 - 2*r1.width() + pwby2, y1 - r1.height() + pwby2,
138 2*r1.width() - pw, 2*r1.height() - pw), 45*16, -45*16);
139 if (!r2.isEmpty())
140 p->drawArc(QRectF(x2 - 2*r2.width() + pwby2, y2 - r2.height() + pwby2,
141 2*r2.width() - pw, 2*r2.height() - pw), 315*16, 45*16);
142 break;
143 default:
144 break;
145 }
146 p->restore();
147}
148
149
150void qDrawEdge(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, qreal dw1, qreal dw2,
151 QCss::Edge edge, QCss::BorderStyle style, QBrush c)
152{
153 p->save();
154 const qreal width = (edge == TopEdge || edge == BottomEdge) ? (y2-y1) : (x2-x1);
155
156 if (width <= 2 && style == BorderStyle_Double)
157 style = BorderStyle_Solid;
158
159 switch (style) {
160 case BorderStyle_Inset:
161 case BorderStyle_Outset:
162 if ((style == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
163 || (style == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
164 c = c.color().lighter();
165 Q_FALLTHROUGH();
166 case BorderStyle_Solid: {
167 p->setPen(Qt::NoPen);
168 p->setBrush(c);
169 if (width == 1 || (dw1 == 0 && dw2 == 0)) {
170 p->drawRect(QRectF(x1, y1, x2-x1, y2-y1));
171 } else { // draw trapezoid
172 std::array<QPointF, 4> quad;
173 switch (edge) {
174 case TopEdge:
175 quad = {QPointF(x1, y1), QPointF(x1 + dw1, y2),
176 QPointF(x2 - dw2, y2), QPointF(x2, y1)};
177 break;
178 case BottomEdge:
179 quad = {QPointF(x1 + dw1, y1), QPointF(x1, y2),
180 QPointF(x2, y2), QPointF(x2 - dw2, y1)};
181 break;
182 case LeftEdge:
183 quad = {QPointF(x1, y1), QPointF(x1, y2),
184 QPointF(x2, y2 - dw2), QPointF(x2, y1 + dw1)};
185 break;
186 case RightEdge:
187 quad = {QPointF(x1, y1 + dw1), QPointF(x1, y2 - dw2),
188 QPointF(x2, y2), QPointF(x2, y1)};
189 break;
190 default:
191 break;
192 }
193 p->drawConvexPolygon(quad.data(), static_cast<int>(quad.size()));
194 }
195 break;
196 }
197 case BorderStyle_Dotted:
198 case BorderStyle_Dashed:
199 case BorderStyle_DotDash:
200 case BorderStyle_DotDotDash:
201 p->setPen(qPenFromStyle(c, width, style));
202 if (width == 1)
203 p->drawLine(QLineF(x1, y1, x2 - 1, y2 - 1));
204 else if (edge == TopEdge || edge == BottomEdge)
205 p->drawLine(QLineF(x1 + width/2, (y1 + y2)/2, x2 - width/2, (y1 + y2)/2));
206 else
207 p->drawLine(QLineF((x1+x2)/2, y1 + width/2, (x1+x2)/2, y2 - width/2));
208 break;
209
210 case BorderStyle_Double: {
211 int wby3 = qRound(width/3);
212 int dw1by3 = qRound(dw1/3);
213 int dw2by3 = qRound(dw2/3);
214 switch (edge) {
215 case TopEdge:
216 qDrawEdge(p, x1, y1, x2, y1 + wby3, dw1by3, dw2by3, TopEdge, BorderStyle_Solid, c);
217 qDrawEdge(p, x1 + dw1 - dw1by3, y2 - wby3, x2 - dw2 + dw1by3, y2,
218 dw1by3, dw2by3, TopEdge, BorderStyle_Solid, c);
219 break;
220 case LeftEdge:
221 qDrawEdge(p, x1, y1, x1 + wby3, y2, dw1by3, dw2by3, LeftEdge, BorderStyle_Solid, c);
222 qDrawEdge(p, x2 - wby3, y1 + dw1 - dw1by3, x2, y2 - dw2 + dw2by3, dw1by3, dw2by3,
223 LeftEdge, BorderStyle_Solid, c);
224 break;
225 case BottomEdge:
226 qDrawEdge(p, x1 + dw1 - dw1by3, y1, x2 - dw2 + dw2by3, y1 + wby3, dw1by3, dw2by3,
227 BottomEdge, BorderStyle_Solid, c);
228 qDrawEdge(p, x1, y2 - wby3, x2, y2, dw1by3, dw2by3, BottomEdge, BorderStyle_Solid, c);
229 break;
230 case RightEdge:
231 qDrawEdge(p, x2 - wby3, y1, x2, y2, dw1by3, dw2by3, RightEdge, BorderStyle_Solid, c);
232 qDrawEdge(p, x1, y1 + dw1 - dw1by3, x1 + wby3, y2 - dw2 + dw2by3, dw1by3, dw2by3,
233 RightEdge, BorderStyle_Solid, c);
234 break;
235 default:
236 break;
237 }
238 break;
239 }
240 case BorderStyle_Ridge:
241 case BorderStyle_Groove: {
242 BorderStyle s1, s2;
243 if (style == BorderStyle_Groove) {
244 s1 = BorderStyle_Inset;
245 s2 = BorderStyle_Outset;
246 } else {
247 s1 = BorderStyle_Outset;
248 s2 = BorderStyle_Inset;
249 }
250 int dw1by2 = qFloor(dw1/2), dw2by2 = qFloor(dw2/2);
251 int wby2 = qRound(width/2);
252 switch (edge) {
253 case TopEdge:
254 qDrawEdge(p, x1, y1, x2, y1 + wby2, dw1by2, dw2by2, TopEdge, s1, c);
255 qDrawEdge(p, x1 + dw1by2, y1 + wby2, x2 - dw2by2, y2, dw1by2, dw2by2, TopEdge, s2, c);
256 break;
257 case BottomEdge:
258 qDrawEdge(p, x1, y1 + wby2, x2, y2, dw1by2, dw2by2, BottomEdge, s1, c);
259 qDrawEdge(p, x1 + dw1by2, y1, x2 - dw2by2, y1 + wby2, dw1by2, dw2by2, BottomEdge, s2, c);
260 break;
261 case LeftEdge:
262 qDrawEdge(p, x1, y1, x1 + wby2, y2, dw1by2, dw2by2, LeftEdge, s1, c);
263 qDrawEdge(p, x1 + wby2, y1 + dw1by2, x2, y2 - dw2by2, dw1by2, dw2by2, LeftEdge, s2, c);
264 break;
265 case RightEdge:
266 qDrawEdge(p, x1 + wby2, y1, x2, y2, dw1by2, dw2by2, RightEdge, s1, c);
267 qDrawEdge(p, x1, y1 + dw1by2, x1 + wby2, y2 - dw2by2, dw1by2, dw2by2, RightEdge, s2, c);
268 break;
269 default:
270 break;
271 }
272 break;
273 }
274 default:
275 break;
276 }
277 p->restore();
278}
279
280void qNormalizeRadii(const QRect &br, const QSize *radii,
281 QSize *tlr, QSize *trr, QSize *blr, QSize *brr)
282{
283 *tlr = radii[0].expandedTo(QSize(0, 0));
284 *trr = radii[1].expandedTo(QSize(0, 0));
285 *blr = radii[2].expandedTo(QSize(0, 0));
286 *brr = radii[3].expandedTo(QSize(0, 0));
287 if (tlr->width() + trr->width() > br.width())
288 *tlr = *trr = QSize(0, 0);
289 if (blr->width() + brr->width() > br.width())
290 *blr = *brr = QSize(0, 0);
291 if (tlr->height() + blr->height() > br.height())
292 *tlr = *blr = QSize(0, 0);
293 if (trr->height() + brr->height() > br.height())
294 *trr = *brr = QSize(0, 0);
295}
296
297// Determines if Edge e1 draws over Edge e2. Depending on this trapezoids or rectangles are drawn
298static bool paintsOver(const QCss::BorderStyle *styles, const QBrush *colors, QCss::Edge e1, QCss::Edge e2)
299{
300 QCss::BorderStyle s1 = styles[e1];
301 QCss::BorderStyle s2 = styles[e2];
302
303 if (s2 == BorderStyle_None || colors[e2] == Qt::transparent)
304 return true;
305
306 if ((s1 == BorderStyle_Solid && s2 == BorderStyle_Solid) && (colors[e1] == colors[e2])
307 && colors[e1].isOpaque()) {
308 return true;
309 }
310
311 return false;
312}
313
314void qDrawBorder(QPainter *p, const QRect &rect, const QCss::BorderStyle *styles,
315 const int *borders, const QBrush *colors, const QSize *radii)
316{
317 const QRectF br(rect);
318 QSize tlr, trr, blr, brr;
319 qNormalizeRadii(rect, radii, &tlr, &trr, &blr, &brr);
320
321 // Drawn in increasing order of precedence
322 if (styles[BottomEdge] != BorderStyle_None && borders[BottomEdge] > 0) {
323 qreal dw1 = (blr.width() || paintsOver(styles, colors, BottomEdge, LeftEdge)) ? 0 : borders[LeftEdge];
324 qreal dw2 = (brr.width() || paintsOver(styles, colors, BottomEdge, RightEdge)) ? 0 : borders[RightEdge];
325 qreal x1 = br.x() + blr.width();
326 qreal y1 = br.y() + br.height() - borders[BottomEdge];
327 qreal x2 = br.x() + br.width() - brr.width();
328 qreal y2 = br.y() + br.height() ;
329
330 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, BottomEdge, styles[BottomEdge], colors[BottomEdge]);
331 if (blr.width() || brr.width())
332 qDrawRoundedCorners(p, x1, y1, x2, y2, blr, brr, BottomEdge, styles[BottomEdge], colors[BottomEdge]);
333 }
334 if (styles[RightEdge] != BorderStyle_None && borders[RightEdge] > 0) {
335 qreal dw1 = (trr.height() || paintsOver(styles, colors, RightEdge, TopEdge)) ? 0 : borders[TopEdge];
336 qreal dw2 = (brr.height() || paintsOver(styles, colors, RightEdge, BottomEdge)) ? 0 : borders[BottomEdge];
337 qreal x1 = br.x() + br.width() - borders[RightEdge];
338 qreal y1 = br.y() + trr.height();
339 qreal x2 = br.x() + br.width();
340 qreal y2 = br.y() + br.height() - brr.height();
341
342 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, RightEdge, styles[RightEdge], colors[RightEdge]);
343 if (trr.height() || brr.height())
344 qDrawRoundedCorners(p, x1, y1, x2, y2, trr, brr, RightEdge, styles[RightEdge], colors[RightEdge]);
345 }
346 if (styles[LeftEdge] != BorderStyle_None && borders[LeftEdge] > 0) {
347 qreal dw1 = (tlr.height() || paintsOver(styles, colors, LeftEdge, TopEdge)) ? 0 : borders[TopEdge];
348 qreal dw2 = (blr.height() || paintsOver(styles, colors, LeftEdge, BottomEdge)) ? 0 : borders[BottomEdge];
349 qreal x1 = br.x();
350 qreal y1 = br.y() + tlr.height();
351 qreal x2 = br.x() + borders[LeftEdge];
352 qreal y2 = br.y() + br.height() - blr.height();
353
354 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, LeftEdge, styles[LeftEdge], colors[LeftEdge]);
355 if (tlr.height() || blr.height())
356 qDrawRoundedCorners(p, x1, y1, x2, y2, tlr, blr, LeftEdge, styles[LeftEdge], colors[LeftEdge]);
357 }
358 if (styles[TopEdge] != BorderStyle_None && borders[TopEdge] > 0) {
359 qreal dw1 = (tlr.width() || paintsOver(styles, colors, TopEdge, LeftEdge)) ? 0 : borders[LeftEdge];
360 qreal dw2 = (trr.width() || paintsOver(styles, colors, TopEdge, RightEdge)) ? 0 : borders[RightEdge];
361 qreal x1 = br.x() + tlr.width();
362 qreal y1 = br.y();
363 qreal x2 = br.left() + br.width() - trr.width();
364 qreal y2 = br.y() + borders[TopEdge];
365
366 qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, TopEdge, styles[TopEdge], colors[TopEdge]);
367 if (tlr.width() || trr.width())
368 qDrawRoundedCorners(p, x1, y1, x2, y2, tlr, trr, TopEdge, styles[TopEdge], colors[TopEdge]);
369 }
370}
371
372#endif //QT_NO_CSSPARSER
373
374QT_END_NAMESPACE
friend class QPainter
Combined button and popup list for selecting options.
void qNormalizeRadii(const QRect &br, const QSize *radii, QSize *tlr, QSize *trr, QSize *blr, QSize *brr)
Definition qcssutil.cpp:280
void qDrawBorder(QPainter *p, const QRect &rect, const QCss::BorderStyle *styles, const int *borders, const QBrush *colors, const QSize *radii)
Definition qcssutil.cpp:314
static QPen qPenFromStyle(const QBrush &b, qreal width, BorderStyle s)
Definition qcssutil.cpp:16
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:45
static bool paintsOver(const QCss::BorderStyle *styles, const QBrush *colors, QCss::Edge e1, QCss::Edge e2)
Definition qcssutil.cpp:298
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:150