| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 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 | #ifndef QPATHCLIPPER_P_H
|
|---|
| 43 | #define QPATHCLIPPER_P_H
|
|---|
| 44 |
|
|---|
| 45 | //
|
|---|
| 46 | // W A R N I N G
|
|---|
| 47 | // -------------
|
|---|
| 48 | //
|
|---|
| 49 | // This file is not part of the Qt API. It exists purely as an
|
|---|
| 50 | // implementation detail. This header file may change from version to
|
|---|
| 51 | // version without notice, or even be removed.
|
|---|
| 52 | //
|
|---|
| 53 | // We mean it.
|
|---|
| 54 | //
|
|---|
| 55 |
|
|---|
| 56 | #include <QtGui/qpainterpath.h>
|
|---|
| 57 | #include <QtCore/qlist.h>
|
|---|
| 58 |
|
|---|
| 59 | #include <private/qbezier_p.h>
|
|---|
| 60 | #include <private/qdatabuffer_p.h>
|
|---|
| 61 | #include <stdio.h>
|
|---|
| 62 |
|
|---|
| 63 | QT_BEGIN_HEADER
|
|---|
| 64 |
|
|---|
| 65 | QT_BEGIN_NAMESPACE
|
|---|
| 66 |
|
|---|
| 67 | QT_MODULE(Gui)
|
|---|
| 68 |
|
|---|
| 69 | class QWingedEdge;
|
|---|
| 70 |
|
|---|
| 71 | class Q_AUTOTEST_EXPORT QPathClipper
|
|---|
| 72 | {
|
|---|
| 73 | public:
|
|---|
| 74 | enum Operation {
|
|---|
| 75 | BoolAnd,
|
|---|
| 76 | BoolOr,
|
|---|
| 77 | BoolSub,
|
|---|
| 78 | Simplify
|
|---|
| 79 | };
|
|---|
| 80 | public:
|
|---|
| 81 | QPathClipper(const QPainterPath &subject,
|
|---|
| 82 | const QPainterPath &clip);
|
|---|
| 83 |
|
|---|
| 84 | QPainterPath clip(Operation op = BoolAnd);
|
|---|
| 85 |
|
|---|
| 86 | bool intersect();
|
|---|
| 87 | bool contains();
|
|---|
| 88 |
|
|---|
| 89 | static bool pathToRect(const QPainterPath &path, QRectF *rect = 0);
|
|---|
| 90 | static QPainterPath intersect(const QPainterPath &path, const QRectF &rect);
|
|---|
| 91 |
|
|---|
| 92 | private:
|
|---|
| 93 | Q_DISABLE_COPY(QPathClipper)
|
|---|
| 94 |
|
|---|
| 95 | enum ClipperMode {
|
|---|
| 96 | ClipMode, // do the full clip
|
|---|
| 97 | CheckMode // for contains/intersects (only interested in whether the result path is non-empty)
|
|---|
| 98 | };
|
|---|
| 99 |
|
|---|
| 100 | bool handleCrossingEdges(QWingedEdge &list, qreal y, ClipperMode mode);
|
|---|
| 101 | bool doClip(QWingedEdge &list, ClipperMode mode);
|
|---|
| 102 |
|
|---|
| 103 | QPainterPath subjectPath;
|
|---|
| 104 | QPainterPath clipPath;
|
|---|
| 105 | Operation op;
|
|---|
| 106 |
|
|---|
| 107 | int aMask;
|
|---|
| 108 | int bMask;
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | struct QPathVertex
|
|---|
| 112 | {
|
|---|
| 113 | public:
|
|---|
| 114 | QPathVertex(const QPointF &p = QPointF(), int e = -1);
|
|---|
| 115 | operator QPointF() const;
|
|---|
| 116 |
|
|---|
| 117 | int edge;
|
|---|
| 118 |
|
|---|
| 119 | qreal x;
|
|---|
| 120 | qreal y;
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | class QPathEdge
|
|---|
| 124 | {
|
|---|
| 125 | public:
|
|---|
| 126 | enum Traversal {
|
|---|
| 127 | RightTraversal,
|
|---|
| 128 | LeftTraversal
|
|---|
| 129 | };
|
|---|
| 130 |
|
|---|
| 131 | enum Direction {
|
|---|
| 132 | Forward,
|
|---|
| 133 | Backward
|
|---|
| 134 | };
|
|---|
| 135 |
|
|---|
| 136 | enum Type {
|
|---|
| 137 | Line,
|
|---|
| 138 | Curve
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | QPathEdge(int a = -1, int b = -1);
|
|---|
| 142 |
|
|---|
| 143 | mutable int flag;
|
|---|
| 144 |
|
|---|
| 145 | int windingA;
|
|---|
| 146 | int windingB;
|
|---|
| 147 |
|
|---|
| 148 | int first;
|
|---|
| 149 | int second;
|
|---|
| 150 |
|
|---|
| 151 | double angle;
|
|---|
| 152 | double invAngle;
|
|---|
| 153 |
|
|---|
| 154 | int next(Traversal traversal, Direction direction) const;
|
|---|
| 155 |
|
|---|
| 156 | void setNext(Traversal traversal, Direction direction, int next);
|
|---|
| 157 | void setNext(Direction direction, int next);
|
|---|
| 158 |
|
|---|
| 159 | Direction directionTo(int vertex) const;
|
|---|
| 160 | int vertex(Direction direction) const;
|
|---|
| 161 |
|
|---|
| 162 | private:
|
|---|
| 163 | int m_next[2][2];
|
|---|
| 164 | };
|
|---|
| 165 |
|
|---|
| 166 | class QPathSegments
|
|---|
| 167 | {
|
|---|
| 168 | public:
|
|---|
| 169 | struct Intersection {
|
|---|
| 170 | int vertex;
|
|---|
| 171 | qreal t;
|
|---|
| 172 |
|
|---|
| 173 | int next;
|
|---|
| 174 |
|
|---|
| 175 | bool operator<(const Intersection &o) const {
|
|---|
| 176 | return t < o.t;
|
|---|
| 177 | }
|
|---|
| 178 | };
|
|---|
| 179 |
|
|---|
| 180 | struct Segment {
|
|---|
| 181 | Segment(int pathId, int vertexA, int vertexB)
|
|---|
| 182 | : path(pathId)
|
|---|
| 183 | , va(vertexA)
|
|---|
| 184 | , vb(vertexB)
|
|---|
| 185 | , intersection(-1)
|
|---|
| 186 | {
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | int path;
|
|---|
| 190 |
|
|---|
| 191 | // vertices
|
|---|
| 192 | int va;
|
|---|
| 193 | int vb;
|
|---|
| 194 |
|
|---|
| 195 | // intersection index
|
|---|
| 196 | int intersection;
|
|---|
| 197 |
|
|---|
| 198 | QRectF bounds;
|
|---|
| 199 | };
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 | QPathSegments(int reserve);
|
|---|
| 203 |
|
|---|
| 204 | void setPath(const QPainterPath &path);
|
|---|
| 205 | void addPath(const QPainterPath &path);
|
|---|
| 206 |
|
|---|
| 207 | int intersections() const;
|
|---|
| 208 | int segments() const;
|
|---|
| 209 | int points() const;
|
|---|
| 210 |
|
|---|
| 211 | const Segment &segmentAt(int index) const;
|
|---|
| 212 | const QLineF lineAt(int index) const;
|
|---|
| 213 | const QRectF &elementBounds(int index) const;
|
|---|
| 214 | int pathId(int index) const;
|
|---|
| 215 |
|
|---|
| 216 | const QPointF &pointAt(int vertex) const;
|
|---|
| 217 | int addPoint(const QPointF &point);
|
|---|
| 218 |
|
|---|
| 219 | const Intersection *intersectionAt(int index) const;
|
|---|
| 220 | void addIntersection(int index, const Intersection &intersection);
|
|---|
| 221 |
|
|---|
| 222 | void mergePoints();
|
|---|
| 223 |
|
|---|
| 224 | private:
|
|---|
| 225 | QDataBuffer<QPointF> m_points;
|
|---|
| 226 | QDataBuffer<Segment> m_segments;
|
|---|
| 227 | QDataBuffer<Intersection> m_intersections;
|
|---|
| 228 |
|
|---|
| 229 | int m_pathId;
|
|---|
| 230 | };
|
|---|
| 231 |
|
|---|
| 232 | class Q_AUTOTEST_EXPORT QWingedEdge
|
|---|
| 233 | {
|
|---|
| 234 | public:
|
|---|
| 235 | struct TraversalStatus
|
|---|
| 236 | {
|
|---|
| 237 | int edge;
|
|---|
| 238 | QPathEdge::Traversal traversal;
|
|---|
| 239 | QPathEdge::Direction direction;
|
|---|
| 240 |
|
|---|
| 241 | void flipDirection();
|
|---|
| 242 | void flipTraversal();
|
|---|
| 243 |
|
|---|
| 244 | void flip();
|
|---|
| 245 | };
|
|---|
| 246 |
|
|---|
| 247 | QWingedEdge();
|
|---|
| 248 | QWingedEdge(const QPainterPath &subject, const QPainterPath &clip);
|
|---|
| 249 |
|
|---|
| 250 | void simplify();
|
|---|
|
|---|