| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qcssutil_p.h"
|
|---|
| 43 | #include "private/qcssparser_p.h"
|
|---|
| 44 | #include "qpainter.h"
|
|---|
| 45 | #include <qmath.h>
|
|---|
| 46 |
|
|---|
| 47 | #ifndef QT_NO_CSSPARSER
|
|---|
| 48 |
|
|---|
| 49 | QT_BEGIN_NAMESPACE
|
|---|
| 50 |
|
|---|
| 51 | using namespace QCss;
|
|---|
| 52 |
|
|---|
| 53 | static QPen qPenFromStyle(const QBrush& b, qreal width, BorderStyle s)
|
|---|
| 54 | {
|
|---|
| 55 | Qt::PenStyle ps = Qt::NoPen;
|
|---|
| 56 |
|
|---|
| 57 | switch (s) {
|
|---|
| 58 | case BorderStyle_Dotted:
|
|---|
| 59 | ps = Qt::DotLine;
|
|---|
| 60 | break;
|
|---|
| 61 | case BorderStyle_Dashed:
|
|---|
| 62 | ps = width == 1 ? Qt::DotLine : Qt::DashLine;
|
|---|
| 63 | break;
|
|---|
| 64 | case BorderStyle_DotDash:
|
|---|
| 65 | ps = Qt::DashDotLine;
|
|---|
| 66 | break;
|
|---|
| 67 | case BorderStyle_DotDotDash:
|
|---|
| 68 | ps = Qt::DashDotDotLine;
|
|---|
| 69 | break;
|
|---|
| 70 | case BorderStyle_Inset:
|
|---|
| 71 | case BorderStyle_Outset:
|
|---|
| 72 | case BorderStyle_Solid:
|
|---|
| 73 | ps = Qt::SolidLine;
|
|---|
| 74 | break;
|
|---|
| 75 | default:
|
|---|
| 76 | break;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | return QPen(b, width, ps, Qt::FlatCap);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void qDrawRoundedCorners(QPainter *p, qreal x1, qreal y1, qreal x2, qreal y2,
|
|---|
| 83 | const QSizeF& r1, const QSizeF& r2,
|
|---|
| 84 | Edge edge, BorderStyle s, QBrush c)
|
|---|
| 85 | {
|
|---|
| 86 | const qreal pw = (edge == TopEdge || edge == BottomEdge) ? y2-y1 : x2-x1;
|
|---|
| 87 | if (s == BorderStyle_Double) {
|
|---|
| 88 | qreal wby3 = pw/3;
|
|---|
| 89 | switch (edge) {
|
|---|
| 90 | case TopEdge:
|
|---|
| 91 | case BottomEdge:
|
|---|
| 92 | qDrawRoundedCorners(p, x1, y1, x2, y1+wby3, r1, r2, edge, BorderStyle_Solid, c);
|
|---|
| 93 | qDrawRoundedCorners(p, x1, y2-wby3, x2, y2, r1, r2, edge, BorderStyle_Solid, c);
|
|---|
| 94 | break;
|
|---|
| 95 | case LeftEdge:
|
|---|
| 96 | qDrawRoundedCorners(p, x1, y1+1, x1+wby3, y2, r1, r2, LeftEdge, BorderStyle_Solid, c);
|
|---|
| 97 | qDrawRoundedCorners(p, x2-wby3, y1+1, x2, y2, r1, r2, LeftEdge, BorderStyle_Solid, c);
|
|---|
| 98 | break;
|
|---|
| 99 | case RightEdge:
|
|---|
| 100 | qDrawRoundedCorners(p, x1, y1+1, x1+wby3, y2, r1, r2, RightEdge, BorderStyle_Solid, c);
|
|---|
| 101 | qDrawRoundedCorners(p, x2-wby3, y1+1, x2, y2, r1, r2, RightEdge, BorderStyle_Solid, c);
|
|---|
| 102 | break;
|
|---|
| 103 | default:
|
|---|
| 104 | break;
|
|---|
| 105 | }
|
|---|
| 106 | return;
|
|---|
| 107 | } else if (s == BorderStyle_Ridge || s == BorderStyle_Groove) {
|
|---|
| 108 | BorderStyle s1, s2;
|
|---|
| 109 | if (s == BorderStyle_Groove) {
|
|---|
| 110 | s1 = BorderStyle_Inset;
|
|---|
| 111 | s2 = BorderStyle_Outset;
|
|---|
| 112 | } else {
|
|---|
| 113 | s1 = BorderStyle_Outset;
|
|---|
| 114 | s2 = BorderStyle_Inset;
|
|---|
| 115 | }
|
|---|
| 116 | int pwby2 = qRound(pw/2);
|
|---|
| 117 | switch (edge) {
|
|---|
| 118 | case TopEdge:
|
|---|
| 119 | qDrawRoundedCorners(p, x1, y1, x2, y1 + pwby2, r1, r2, TopEdge, s1, c);
|
|---|
| 120 | qDrawRoundedCorners(p, x1, y1 + pwby2, x2, y2, r1, r2, TopEdge, s2, c);
|
|---|
| 121 | break;
|
|---|
| 122 | case BottomEdge:
|
|---|
| 123 | qDrawRoundedCorners(p, x1, y1 + pwby2, x2, y2, r1, r2, BottomEdge, s1, c);
|
|---|
| 124 | qDrawRoundedCorners(p, x1, y1, x2, y2-pwby2, r1, r2, BottomEdge, s2, c);
|
|---|
| 125 | break;
|
|---|
| 126 | case LeftEdge:
|
|---|
| 127 | qDrawRoundedCorners(p, x1, y1, x1 + pwby2, y2, r1, r2, LeftEdge, s1, c);
|
|---|
| 128 | qDrawRoundedCorners(p, x1 + pwby2, y1, x2, y2, r1, r2, LeftEdge, s2, c);
|
|---|
| 129 | break;
|
|---|
| 130 | case RightEdge:
|
|---|
| 131 | qDrawRoundedCorners(p, x1 + pwby2, y1, x2, y2, r1, r2, RightEdge, s1, c);
|
|---|
| 132 | qDrawRoundedCorners(p, x1, y1, x2 - pwby2, y2, r1, r2, RightEdge, s2, c);
|
|---|
| 133 | break;
|
|---|
| 134 | default:
|
|---|
| 135 | break;
|
|---|
| 136 | }
|
|---|
| 137 | } else if ((s == BorderStyle_Outset && (edge == TopEdge || edge == LeftEdge))
|
|---|
| 138 | || (s == BorderStyle_Inset && (edge == BottomEdge || edge == RightEdge)))
|
|---|
| 139 | c = c.color().lighter();
|
|---|
| 140 |
|
|---|
| 141 | p->save();
|
|---|
| 142 | qreal pwby2 = pw/2;
|
|---|
| 143 | p->setBrush(Qt::NoBrush);
|
|---|
| 144 | QPen pen = qPenFromStyle(c, pw, s);
|
|---|
| 145 | pen.setCapStyle(Qt::SquareCap); // this eliminates the offby1 errors that we might hit below
|
|---|
| 146 | p->setPen(pen);
|
|---|
| 147 | switch (edge) {
|
|---|
| 148 | case TopEdge:
|
|---|
| 149 | if (!r1.isEmpty())
|
|---|
| 150 | p->drawArc(QRectF(x1 - r1.width() + pwby2, y1 + pwby2,
|
|---|
| 151 | 2*r1.width() - pw, 2*r1.height() - pw), 135*16, -45*16);
|
|---|
| 152 | if (!r2.isEmpty())
|
|---|
| 153 | p->drawArc(QRectF(x2 - r2.width() + pwby2, y1 + pwby2,
|
|---|
| 154 | 2*r2.width() - pw, 2*r2.height() - pw), 45*16, 45*16);
|
|---|
| 155 | break;
|
|---|
| 156 | case BottomEdge:
|
|---|
| 157 | if (!r1.isEmpty())
|
|---|
| 158 | p->drawArc(QRectF(x1 - r1.width() + pwby2, y2 - 2*r1.height() + pwby2,
|
|---|
| 159 | 2*r1.width() - pw, 2*r1.height() - pw), -90 * 16, -45 * 16);
|
|---|
| 160 | if (!r2.isEmpty())
|
|---|
| 161 | p->drawArc(QRectF(x2 - r2.width() + pwby2, y2 - 2*r2.height() + pwby2,
|
|---|
|
|---|