qcssutil.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9