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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qcssutil_p.h"
#include "private/qcssparser_p.h"
#include "qpainter.h"
#include <qmath.h>
#ifndef QT_NO_CSSPARSER
QT_BEGIN_NAMESPACE
using namespace QCss;
static QPen qPenFromStyle(const QBrush& b, qreal width, BorderStyle s)
{
Qt::PenStyle ps = Qt::NoPen;
switch (s) {
case BorderStyle_Dotted:
ps = Qt::DotLine;
break;
case BorderStyle_Dashed:
ps = width == 1 ? Qt::DotLine : Qt::DashLine;
break;
case BorderStyle_DotDash:
ps = Qt::DashDotLine;
break;
case BorderStyle_DotDotDash:
ps = Qt::DashDotDotLine;
break;
case BorderStyle_Inset:
case BorderStyle_Outset:
case BorderStyle_Solid:
ps = Qt::SolidLine;
break;
default:
break;
}
return QPen(b, width, ps, Qt::FlatCap);
}
void qDrawRoundedCorners(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2,
const QSizeF& r1, const QSizeF& r2,
Edge edge, BorderStyle s, QBrush c)
{
const qreal pw = (edge == TopEdge || edge == BottomEdge) ? y2-y1 : x2-x1;
if (s == BorderStyle_Double) {
qreal wby3 = pw/3;
switch (edge) {
case TopEdge:
case BottomEdge:
qDrawRoundedCorners(p, x1, y1, x2, y1+wby3, r1, r2, edge, BorderStyle_Solid, c);
qDrawRoundedCorners(p, x1, y2-wby3, x2, y2, r1, r2, edge, BorderStyle_Solid, c);
break;
case LeftEdge:
qDrawRoundedCorners(p, x1, y1+1, x1+wby3, y2, r1, r2, LeftEdge, BorderStyle_Solid, c);
qDrawRoundedCorners(p, x2-wby3, y1+1, x2, y2, r1, r2, LeftEdge, BorderStyle_Solid, c);
break;
case RightEdge:
qDrawRoundedCorners(p, x1, y1+1, x1+wby3, y2, r1, r2, RightEdge, BorderStyle_Solid, c);
qDrawRoundedCorners(p, x2-wby3, y1+1, x2, y2, r1, r2, RightEdge, BorderStyle_Solid, c);
break;
default:
break;
}
return;
} else if (s == BorderStyle_Ridge || s == BorderStyle_Groove) {
BorderStyle s1, s2;
if (s == BorderStyle_Groove) {
s1 = BorderStyle_Inset;
s2 = BorderStyle_Outset;
} else {
s1 = BorderStyle_Outset;
s2 = BorderStyle_Inset;
}
int pwby2 = qRound(pw/2);
switch (edge) {
case TopEdge:
qDrawRoundedCorners(p, x1, y1, x2, y1 + pwby2, r1, r2, TopEdge, s1, c);
qDrawRoundedCorners(p, x1, y1 + pwby2, x2, y2, r1, r2, TopEdge, s2, c);
break;
case BottomEdge:
qDrawRoundedCorners(p, x1, y1 + pwby2, x2, y2, r1, r2, BottomEdge, s1, c);
qDrawRoundedCorners(p, x1, y1, x2, y2-pwby2, r1, r2, BottomEdge, s2, c);
break;
case LeftEdge:
qDrawRoundedCorners(p, x1, y1, x1 + pwby2, y2, r1, r2, LeftEdge, s1, c);
qDrawRoundedCorners(p, x1 + pwby2, y1, x2, y2, r1, r2, LeftEdge, s2, c);
break;
case RightEdge:
qDrawRoundedCorners(p, x1 + pwby2, y1, x2, y2, r1, r2, RightEdge, s1, c);
qDrawRoundedCorners(p, x1, y1, x2 - pwby2, y2, r1, r2, RightEdge, s2, c);
break;
default:
break;
}
} else if ((s == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
|| (s == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
c = c.color().lighter();
p->save();
qreal pwby2 = pw/2;
p->setBrush(Qt::NoBrush);
QPen pen = qPenFromStyle(c, pw, s);
pen.setCapStyle(Qt::SquareCap); // this eliminates the offby1 errors that we might hit below
p->setPen(pen);
switch (edge) {
case TopEdge:
if (!r1.isEmpty())
p->drawArc(QRectF(x1 - r1.width() + pwby2, y1 + pwby2,
2*r1.width() - pw, 2*r1.height() - pw), 135*16, -45*16);
if (!r2.isEmpty())
p->drawArc(QRectF(x2 - r2.width() + pwby2, y1 + pwby2,
2*r2.width() - pw, 2*r2.height() - pw), 45*16, 45*16);
break;
case BottomEdge:
if (!r1.isEmpty())
p->drawArc(QRectF(x1 - r1.width() + pwby2, y2 - 2*r1.height() + pwby2,
2*r1.width() - pw, 2*r1.height() - pw), -90 * 16, -45 * 16);
if (!r2.isEmpty())
p->drawArc(QRectF(x2 - r2.width() + pwby2, y2 - 2*r2.height() + pwby2,
2*r2.width() - pw, 2*r2.height() - pw), -90 * 16, 45 * 16);
break;
case LeftEdge:
if (!r1.isEmpty())
p->drawArc(QRectF(x1 + pwby2, y1 - r1.height() + pwby2,
2*r1.width() - pw, 2*r1.height() - pw), 135*16, 45*16);
if (!r2.isEmpty())
p->drawArc(QRectF(x1 + pwby2, y2 - r2.height() + pwby2,
2*r2.width() - pw, 2*r2.height() - pw), 180*16, 45*16);
break;
case RightEdge:
if (!r1.isEmpty())
p->drawArc(QRectF(x2 - 2*r1.width() + pwby2, y1 - r1.height() + pwby2,
2*r1.width() - pw, 2*r1.height() - pw), 45*16, -45*16);
if (!r2.isEmpty())
p->drawArc(QRectF(x2 - 2*r2.width() + pwby2, y2 - r2.height() + pwby2,
2*r2.width() - pw, 2*r2.height() - pw), 315*16, 45*16);
break;
default:
break;
}
p->restore();
}
void qDrawEdge(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2, qreal dw1, qreal dw2,
QCss::Edge edge, QCss::BorderStyle style, QBrush c)
{
p->save();
const qreal width = (edge == TopEdge || edge == BottomEdge) ? (y2-y1) : (x2-x1);
if (width <= 2 && style == BorderStyle_Double)
style = BorderStyle_Solid;
switch (style) {
case BorderStyle_Inset:
case BorderStyle_Outset:
if ((style == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
|| (style == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
c = c.color().lighter();
// fall through!
case BorderStyle_Solid: {
p->setPen(Qt::NoPen);
p->setBrush(c);
if (width == 1 || (dw1 == 0 && dw2 == 0)) {
p->drawRect(QRectF(x1, y1, x2-x1, y2-y1));
} else { // draw trapezoid
QPolygonF quad;
switch (edge) {
case TopEdge:
quad << QPointF(x1, y1) << QPointF(x1 + dw1, y2)
<< QPointF(x2 - dw2, y2) << QPointF(x2, y1);
break;
case BottomEdge:
quad << QPointF(x1 + dw1, y1) << QPointF(x1, y2)
<< QPointF(x2, y2) << QPointF(x2 - dw2, y1);
break;
case LeftEdge:
quad << QPointF(x1, y1) << QPointF(x1, y2)
<< QPointF(x2, y2 - dw2) << QPointF(x2, y1 + dw1);
break;
case RightEdge:
quad << QPointF(x1, y1 + dw1) << QPointF(x1, y2 - dw2)
<< QPointF(x2, y2) << QPointF(x2, y1);
break;
default:
break;
}
p->drawConvexPolygon(quad);
}
break;
}
case BorderStyle_Dotted:
case BorderStyle_Dashed:
case BorderStyle_DotDash:
case BorderStyle_DotDotDash:
p->setPen(qPenFromStyle(c, width, style));
if (width == 1)
p->drawLine(QLineF(x1, y1, x2 - 1, y2 - 1));
else if (edge == TopEdge || edge == BottomEdge)
p->drawLine(QLineF(x1 + width/2, (y1 + y2)/2, x2 - width/2, (y1 + y2)/2));
else
p->drawLine(QLineF((x1+x2)/2, y1 + width/2, (x1+x2)/2, y2 - width/2));
break;
case BorderStyle_Double: {
int wby3 = qRound(width/3);
int dw1by3 = qRound(dw1/3);
int dw2by3 = qRound(dw2/3);
switch (edge) {
case TopEdge:
qDrawEdge(p, x1, y1, x2, y1 + wby3, dw1by3, dw2by3, TopEdge, BorderStyle_Solid, c);
qDrawEdge(p, x1 + dw1 - dw1by3, y2 - wby3, x2 - dw2 + dw1by3, y2,
dw1by3, dw2by3, TopEdge, BorderStyle_Solid, c);
break;
case LeftEdge:
qDrawEdge(p, x1, y1, x1 + wby3, y2, dw1by3, dw2by3, LeftEdge, BorderStyle_Solid, c);
qDrawEdge(p, x2 - wby3, y1 + dw1 - dw1by3, x2, y2 - dw2 + dw2by3, dw1by3, dw2by3,
LeftEdge, BorderStyle_Solid, c);
break;
case BottomEdge:
qDrawEdge(p, x1 + dw1 - dw1by3, y1, x2 - dw2 + dw2by3, y1 + wby3, dw1by3, dw2by3,
BottomEdge, BorderStyle_Solid, c);
qDrawEdge(p, x1, y2 - wby3, x2, y2, dw1by3, dw2by3, BottomEdge, BorderStyle_Solid, c);
break;
case RightEdge:
qDrawEdge(p, x2 - wby3, y1, x2, y2, dw1by3, dw2by3, RightEdge, BorderStyle_Solid, c);
qDrawEdge(p, x1, y1 + dw1 - dw1by3, x1 + wby3, y2 - dw2 + dw2by3, dw1by3, dw2by3,
RightEdge, BorderStyle_Solid, c);
break;
default:
break;
}
break;
}
case BorderStyle_Ridge:
case BorderStyle_Groove: {
BorderStyle s1, s2;
if (style == BorderStyle_Groove) {
s1 = BorderStyle_Inset;
s2 = BorderStyle_Outset;
} else {
s1 = BorderStyle_Outset;
s2 = BorderStyle_Inset;
}
int dw1by2 = qFloor(dw1/2), dw2by2 = qFloor(dw2/2);
int wby2 = qRound(width/2);
switch (edge) {
case TopEdge:
qDrawEdge(p, x1, y1, x2, y1 + wby2, dw1by2, dw2by2, TopEdge, s1, c);
qDrawEdge(p, x1 + dw1by2, y1 + wby2, x2 - dw2by2, y2, dw1by2, dw2by2, TopEdge, s2, c);
break;
case BottomEdge:
qDrawEdge(p, x1, y1 + wby2, x2, y2, dw1by2, dw2by2, BottomEdge, s1, c);
qDrawEdge(p, x1 + dw1by2, y1, x2 - dw2by2, y1 + wby2, dw1by2, dw2by2, BottomEdge, s2, c);
break;
case LeftEdge:
qDrawEdge(p, x1, y1, x1 + wby2, y2, dw1by2, dw2by2, LeftEdge, s1, c);
qDrawEdge(p, x1 + wby2, y1 + dw1by2, x2, y2 - dw2by2, dw1by2, dw2by2, LeftEdge, s2, c);
break;
case RightEdge:
qDrawEdge(p, x1 + wby2, y1, x2, y2, dw1by2, dw2by2, RightEdge, s1, c);
qDrawEdge(p, x1, y1 + dw1by2, x1 + wby2, y2 - dw2by2, dw1by2, dw2by2, RightEdge, s2, c);
break;
default:
break;
}
}
default:
break;
}
p->restore();
}
void qNormalizeRadii(const QRect &br, const QSize *radii,
QSize *tlr, QSize *trr, QSize *blr, QSize *brr)
{
*tlr = radii[0].expandedTo(QSize(0, 0));
*trr = radii[1].expandedTo(QSize(0, 0));
*blr = radii[2].expandedTo(QSize(0, 0));
*brr = radii[3].expandedTo(QSize(0, 0));
if (tlr->width() + trr->width() > br.width())
*tlr = *trr = QSize(0, 0);
if (blr->width() + brr->width() > br.width())
*blr = *brr = QSize(0, 0);
if (tlr->height() + blr->height() > br.height())
*tlr = *blr = QSize(0, 0);
if (trr->height() + brr->height() > br.height())
*trr = *brr = QSize(0, 0);
}
// Determines if Edge e1 draws over Edge e2. Depending on this trapezoids or rectanges are drawn
static bool paintsOver(const QCss::BorderStyle *styles, const QBrush *colors, QCss::Edge e1, QCss::Edge e2)
{
QCss::BorderStyle s1 = styles[e1];
QCss::BorderStyle s2 = styles[e2];
if (s2 == BorderStyle_None || colors[e2] == Qt::transparent)
return true;
if ((s1 == BorderStyle_Solid && s2 == BorderStyle_Solid) && (colors[e1] == colors[e2]))
return true;
return false;
}
void qDrawBorder(QPainter *p, const QRect &rect, const QCss::BorderStyle *styles,
const int *borders, const QBrush *colors, const QSize *radii)
{
const QRectF br(rect);
QSize tlr, trr, blr, brr;
qNormalizeRadii(rect, radii, &tlr, &trr, &blr, &brr);
// Drawn in increasing order of precendence
if (styles[BottomEdge] != BorderStyle_None && borders[BottomEdge] > 0) {
qreal dw1 = (blr.width() || paintsOver(styles, colors, BottomEdge, LeftEdge)) ? 0 : borders[LeftEdge];
qreal dw2 = (brr.width() || paintsOver(styles, colors, BottomEdge, RightEdge)) ? 0 : borders[RightEdge];
qreal x1 = br.x() + blr.width();
qreal y1 = br.y() + br.height() - borders[BottomEdge];
qreal x2 = br.x() + br.width() - brr.width();
qreal y2 = br.y() + br.height() ;
qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, BottomEdge, styles[BottomEdge], colors[BottomEdge]);
if (blr.width() || brr.width())
qDrawRoundedCorners(p, x1, y1, x2, y2, blr, brr, BottomEdge, styles[BottomEdge], colors[BottomEdge]);
}
if (styles[RightEdge] != BorderStyle_None && borders[RightEdge] > 0) {
qreal dw1 = (trr.height() || paintsOver(styles, colors, RightEdge, TopEdge)) ? 0 : borders[TopEdge];
qreal dw2 = (brr.height() || paintsOver(styles, colors, RightEdge, BottomEdge)) ? 0 : borders[BottomEdge];
qreal x1 = br.x() + br.width() - borders[RightEdge];
qreal y1 = br.y() + trr.height();
qreal x2 = br.x() + br.width();
qreal y2 = br.y() + br.height() - brr.height();
qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, RightEdge, styles[RightEdge], colors[RightEdge]);
if (trr.height() || brr.height())
qDrawRoundedCorners(p, x1, y1, x2, y2, trr, brr, RightEdge, styles[RightEdge], colors[RightEdge]);
}
if (styles[LeftEdge] != BorderStyle_None && borders[LeftEdge] > 0) {
qreal dw1 = (tlr.height() || paintsOver(styles, colors, LeftEdge, TopEdge)) ? 0 : borders[TopEdge];
qreal dw2 = (blr.height() || paintsOver(styles, colors, LeftEdge, BottomEdge)) ? 0 : borders[BottomEdge];
qreal x1 = br.x();
qreal y1 = br.y() + tlr.height();
qreal x2 = br.x() + borders[LeftEdge];
qreal y2 = br.y() + br.height() - blr.height();
qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, LeftEdge, styles[LeftEdge], colors[LeftEdge]);
if (tlr.height() || blr.height())
qDrawRoundedCorners(p, x1, y1, x2, y2, tlr, blr, LeftEdge, styles[LeftEdge], colors[LeftEdge]);
}
if (styles[TopEdge] != BorderStyle_None && borders[TopEdge] > 0) {
qreal dw1 = (tlr.width() || paintsOver(styles, colors, TopEdge, LeftEdge)) ? 0 : borders[LeftEdge];
qreal dw2 = (trr.width() || paintsOver(styles, colors, TopEdge, RightEdge)) ? 0 : borders[RightEdge];
qreal x1 = br.x() + tlr.width();
qreal y1 = br.y();
qreal x2 = br.left() + br.width() - trr.width();
qreal y2 = br.y() + borders[TopEdge];
qDrawEdge(p, x1, y1, x2, y2, dw1, dw2, TopEdge, styles[TopEdge], colors[TopEdge]);
if (tlr.width() || trr.width())
qDrawRoundedCorners(p, x1, y1, x2, y2, tlr, trr, TopEdge, styles[TopEdge], colors[TopEdge]);
}
}
#endif //QT_NO_CSSPARSER
QT_END_NAMESPACE
|