0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
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 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
|
|
45 |
#include <qfile.h>
|
|
46 |
#include <qpainterpath.h>
|
|
47 |
#include <qpen.h>
|
|
48 |
|
|
49 |
#define _USE_MATH_DEFINES
|
|
50 |
#include <math.h>
|
|
51 |
|
|
52 |
//TESTED_CLASS=
|
|
53 |
//TESTED_FILES=
|
|
54 |
|
|
55 |
class tst_QPainterPath : public QObject
|
|
56 |
{
|
|
57 |
Q_OBJECT
|
|
58 |
|
|
59 |
public:
|
|
60 |
|
|
61 |
private slots:
|
|
62 |
void getSetCheck();
|
|
63 |
void contains_QPointF_data();
|
|
64 |
void contains_QPointF();
|
|
65 |
|
|
66 |
void contains_QRectF_data();
|
|
67 |
void contains_QRectF();
|
|
68 |
|
|
69 |
void intersects_QRectF_data();
|
|
70 |
void intersects_QRectF();
|
|
71 |
|
|
72 |
void testContainsAndIntersects_data();
|
|
73 |
void testContainsAndIntersects();
|
|
74 |
|
|
75 |
void testSimplified_data();
|
|
76 |
void testSimplified();
|
|
77 |
|
|
78 |
void testStroker_data();
|
|
79 |
void testStroker();
|
|
80 |
|
|
81 |
void currentPosition();
|
|
82 |
|
|
83 |
void testOperatorEquals();
|
|
84 |
void testOperatorEquals_fuzzy();
|
|
85 |
void testOperatorDatastream();
|
|
86 |
|
|
87 |
void testArcMoveTo_data();
|
|
88 |
void testArcMoveTo();
|
|
89 |
void setElementPositionAt();
|
|
90 |
|
|
91 |
void testOnPath_data();
|
|
92 |
void testOnPath();
|
|
93 |
|
|
94 |
void pointAtPercent_data();
|
|
95 |
void pointAtPercent();
|
|
96 |
|
|
97 |
void angleAtPercent();
|
|
98 |
|
|
99 |
void arcWinding_data();
|
|
100 |
void arcWinding();
|
|
101 |
|
|
102 |
void testToFillPolygons();
|
|
103 |
|
|
104 |
void closing();
|
|
105 |
|
|
106 |
void operators_data();
|
|
107 |
void operators();
|
|
108 |
|
|
109 |
void connectPathDuplicatePoint();
|
|
110 |
|
|
111 |
void translate();
|
|
112 |
};
|
|
113 |
|
|
114 |
// Testing get/set functions
|
|
115 |
void tst_QPainterPath::getSetCheck()
|
|
116 |
{
|
|
117 |
QPainterPathStroker obj1;
|
|
118 |
// qreal QPainterPathStroker::width()
|
|
119 |
// void QPainterPathStroker::setWidth(qreal)
|
|
120 |
obj1.setWidth(0.0);
|
|
121 |
QCOMPARE(qreal(1.0), obj1.width()); // Pathstroker sets with to 1 if <= 0
|
|
122 |
obj1.setWidth(0.5);
|
|
123 |
QCOMPARE(qreal(0.5), obj1.width());
|
|
124 |
obj1.setWidth(1.1);
|
|
125 |
QCOMPARE(qreal(1.1), obj1.width());
|
|
126 |
|
|
127 |
// qreal QPainterPathStroker::miterLimit()
|
|
128 |
// void QPainterPathStroker::setMiterLimit(qreal)
|
|
129 |
obj1.setMiterLimit(0.0);
|
|
130 |
QCOMPARE(qreal(0.0), obj1.miterLimit());
|
|
131 |
obj1.setMiterLimit(1.1);
|
|
132 |
QCOMPARE(qreal(1.1), obj1.miterLimit());
|
|
133 |
|
|
134 |
// qreal QPainterPathStroker::curveThreshold()
|
|
135 |
// void QPainterPathStroker::setCurveThreshold(qreal)
|
|
136 |
obj1.setCurveThreshold(0.0);
|
|
137 |
QCOMPARE(qreal(0.0), obj1.curveThreshold());
|
|
138 |
obj1.setCurveThreshold(1.1);
|
|
139 |
QCOMPARE(qreal(1.1), obj1.curveThreshold());
|
|
140 |
}
|
|
141 |
|
|
142 |
Q_DECLARE_METATYPE(QPainterPath)
|
|
143 |
Q_DECLARE_METATYPE(QPointF)
|
|
144 |
Q_DECLARE_METATYPE(QRectF)
|
|
145 |
|
|
146 |
void tst_QPainterPath::currentPosition()
|
|
147 |
{
|
|
148 |
QPainterPath p;
|
|
149 |
|
|
150 |
QCOMPARE(p.currentPosition(), QPointF());
|
|
151 |
|
|
152 |
p.moveTo(100, 100);
|
|
153 |
QCOMPARE(p.currentPosition(), QPointF(100, 100));
|
|
154 |
|
|
155 |
p.lineTo(200, 200);
|
|
156 |
QCOMPARE(p.currentPosition(), QPointF(200, 200));
|
|
157 |
|
|
158 |
p.cubicTo(300, 200, 200, 300, 500, 500);
|
|
159 |
QCOMPARE(p.currentPosition(), QPointF(500, 500));
|
|
160 |
}
|
|
161 |
|
|
162 |
void tst_QPainterPath::contains_QPointF_data()
|
|
163 |
{
|
|
164 |
QTest::addColumn<QPainterPath>("path");
|
|
165 |
QTest::addColumn<QPointF>("pt");
|
|
166 |
QTest::addColumn<bool>("contained");
|
|
167 |
|
|
168 |
QPainterPath path;
|
|
169 |
path.addRect(0, 0, 100, 100);
|
|
170 |
|
|
171 |
// #####
|
|
172 |
// # #
|
|
173 |
// # #
|
|
174 |
// # #
|
|
175 |
// #####
|
|
176 |
|
|
177 |
QTest::newRow("[0,0] in [0,0,100,100]") << path << QPointF(0, 0) << true;
|
|
178 |
|
|
179 |
QTest::newRow("[99,0] in [0,0,100,100]") << path << QPointF(99, 0) << true;
|
|
180 |
QTest::newRow("[0,99] in [0,0,100,100]") << path << QPointF(0, 99) << true;
|
|
181 |
QTest::newRow("[99,99] in [0,0,100,100]") << path << QPointF(99, 99) << true;
|
|
182 |
|
|
183 |
QTest::newRow("[99.99,0] in [0,0,100,100]") << path << QPointF(99.99, 0) << true;
|
|
184 |
QTest::newRow("[0,99.99] in [0,0,100,100]") << path << QPointF(0, 99.99) << true;
|
|
185 |
QTest::newRow("[99.99,99.99] in [0,0,100,100]") << path << QPointF(99.99, 99.99) << true;
|
|
186 |
|
|
187 |
QTest::newRow("[0.01,0.01] in [0,0,100,100]") << path << QPointF(0.01, 0.01) << true;
|
|
188 |
QTest::newRow("[0,0.01] in [0,0,100,100]") << path << QPointF(0, 0.01) << true;
|
|
189 |
QTest::newRow("[0.01,0] in [0,0,100,100]") << path << QPointF(0.01, 0) << true;
|
|
190 |
|
|
191 |
QTest::newRow("[-0.01,-0.01] in [0,0,100,100]") << path << QPointF(-0.01, -0.01) << false;
|
|
192 |
QTest::newRow("[-0,-0.01] in [0,0,100,100]") << path << QPointF(0, -0.01) << false;
|
|
193 |
QTest::newRow("[-0.01,0] in [0,0,100,100]") << path << QPointF(-0.01, 0) << false;
|
|
194 |
|
|
195 |
|
|
196 |
QTest::newRow("[-10,0] in [0,0,100,100]") << path << QPointF(-10, 0) << false;
|
|
197 |
QTest::newRow("[100,0] in [0,0,100,100]") << path << QPointF(100, 0) << false;
|
|
198 |
|
|
199 |
QTest::newRow("[0,-10] in [0,0,100,100]") << path << QPointF(0, -10) << false;
|
|
200 |
QTest::newRow("[0,100] in [0,0,100,100]") << path << QPointF(0, 100) << false;
|
|
201 |
|
|
202 |
QTest::newRow("[100.1,0] in [0,0,100,100]") << path << QPointF(100.1, 0) << false;
|
|
203 |
QTest::newRow("[0,100.1] in [0,0,100,100]") << path << QPointF(0, 100.1) << false;
|
|
204 |
|
|
205 |
path.addRect(50, 50, 100, 100);
|
|
206 |
|
|
207 |
// #####
|
|
208 |
// # #
|
|
209 |
// # #####
|
|
210 |
// # # # #
|
|
211 |
// ##### #
|
|
212 |
// # #
|
|
213 |
// #####
|
|
214 |
|
|
215 |
QTest::newRow("[49,49] in 2 rects") << path << QPointF(49,49) << true;
|
|
216 |
QTest::newRow("[50,50] in 2 rects") << path << QPointF(50,50) << false;
|
|
217 |
QTest::newRow("[100,100] in 2 rects") << path << QPointF(100,100) << true;
|
|
218 |
|
|
219 |
path.setFillRule(Qt::WindingFill);
|
|
220 |
QTest::newRow("[50,50] in 2 rects (winding)") << path << QPointF(50,50) << true;
|
|
221 |
|
|
222 |
path.addEllipse(0, 0, 150, 150);
|
|
223 |
|
|
224 |
// #####
|
|
225 |
// ## ##
|
|
226 |
// # #####
|
|
227 |
// # # # #
|
|
228 |
// ##### #
|
|
229 |
// ## ##
|
|
230 |
// #####
|
|
231 |
|
|
232 |
QTest::newRow("[50,50] in complex (winding)") << path << QPointF(50, 50) << true;
|
|
233 |
|
|
234 |
path.setFillRule(Qt::OddEvenFill);
|
|
235 |
QTest::newRow("[50,50] in complex (windinf)") << path << QPointF(50, 50) << true;
|
|
236 |
QTest::newRow("[49,49] in complex") << path << QPointF(49,49) << false;
|
|
237 |
QTest::newRow("[100,100] in complex") << path << QPointF(49,49) << false;
|
|
238 |
|
|
239 |
|
|
240 |
// unclosed triangle
|
|
241 |
path = QPainterPath();
|
|
242 |
path.moveTo(100, 100);
|
|
243 |
path.lineTo(130, 70);
|
|
244 |
path.lineTo(150, 110);
|
|
245 |
|
|
246 |
QTest::newRow("[100,100] in triangle") << path << QPointF(100, 100) << true;
|
|
247 |
QTest::newRow("[140,100] in triangle") << path << QPointF(140, 100) << true;
|
|
248 |
QTest::newRow("[130,80] in triangle") << path << QPointF(130, 80) << true;
|
|
249 |
|
|
250 |
QTest::newRow("[110,80] in triangle") << path << QPointF(110, 80) << false;
|
|
251 |
QTest::newRow("[150,100] in triangle") << path << QPointF(150, 100) << false;
|
|
252 |
QTest::newRow("[120,110] in triangle") << path << QPointF(120, 110) << false;
|
|
253 |
|
|
254 |
QRectF base_rect(0, 0, 20, 20);
|
|
255 |
|
|
256 |
path = QPainterPath();
|
|
257 |
path.addEllipse(base_rect);
|
|
258 |
|
|
259 |
// not strictly precise, but good enougth to verify fair precision.
|
|
260 |
QPainterPath inside;
|
|
261 |
inside.addEllipse(base_rect.adjusted(5, 5, -5, -5));
|
|
262 |
QPolygonF inside_poly = inside.toFillPolygon();
|
|
263 |
for (int i=0; i<inside_poly.size(); ++i)
|
|
264 |
QTest::newRow("inside_ellipse") << path << inside_poly.at(i) << true;
|
|
265 |
|
|
266 |
QPainterPath outside;
|
|
267 |
outside.addEllipse(base_rect.adjusted(-5, -5, 5, 5));
|
|
268 |
QPolygonF outside_poly = outside.toFillPolygon();
|
|
269 |
for (int i=0; i<outside_poly.size(); ++i)
|
|
270 |
QTest::newRow("outside_ellipse") << path << outside_poly.at(i) << false;
|
|
271 |
|
|
272 |
path = QPainterPath();
|
|
273 |
base_rect = QRectF(50, 50, 200, 200);
|
|
274 |
path.addEllipse(base_rect);
|
|
275 |
path.setFillRule(Qt::WindingFill);
|
|
276 |
|
|
277 |
QTest::newRow("topleft outside ellipse") << path << base_rect.topLeft() << false;
|
|
278 |
QTest::newRow("topright outside ellipse") << path << base_rect.topRight() << false;
|
|
279 |
QTest::newRow("bottomright outside ellipse") << path << base_rect.bottomRight() << false;
|
|
280 |
QTest::newRow("bottomleft outside ellipse") << path << base_rect.bottomLeft() << false;
|
|
281 |
|
|
282 |
// Test horizontal curve segment
|
|
283 |
path = QPainterPath();
|
|
284 |
path.moveTo(100, 100);
|
|
285 |
path.cubicTo(120, 100, 180, 100, 200, 100);
|
|
286 |
path.lineTo(150, 200);
|
|
287 |
path.closeSubpath();
|
|
288 |
|
|
289 |
QTest::newRow("horizontal cubic, out left") << path << QPointF(0, 100) << false;
|
|
290 |
QTest::newRow("horizontal cubic, out right") << path << QPointF(300, 100) <<false;
|
|
291 |
QTest::newRow("horizontal cubic, in mid") << path << QPointF(150, 100) << true;
|
|
292 |
}
|
|
293 |
|
|
294 |
void tst_QPainterPath::contains_QPointF()
|
|
295 |
{
|
|
296 |
QFETCH(QPainterPath, path);
|
|
297 |
QFETCH(QPointF, pt);
|
|
298 |
QFETCH(bool, contained);
|
|
299 |
|
|
300 |
QCOMPARE(path.contains(pt), contained);
|
|
301 |
}
|
|
302 |
|
|
303 |
void tst_QPainterPath::contains_QRectF_data()
|
|
304 |
{
|
|
305 |
QTest::addColumn<QPainterPath>("path");
|
|
306 |
QTest::addColumn<QRectF>("rect");
|
|
307 |
QTest::addColumn<bool>("contained");
|
|
308 |
|
|
309 |
QPainterPath path;
|
|
310 |
path.addRect(0, 0, 100, 100);
|
|
311 |
|
|
312 |
QTest::newRow("same rect") << path << QRectF(0.1, 0.1, 99, 99) << true; // ###
|
|
313 |
QTest::newRow("outside") << path << QRectF(-1, -1, 100, 100) << false;
|
|
314 |
QTest::newRow("covers") << path << QRectF(-1, -1, 102, 102) << false;
|
|
315 |
QTest::newRow("left") << path << QRectF(-10, 50, 5, 5) << false;
|
|
316 |
QTest::newRow("top") << path << QRectF(50, -10, 5, 5) << false;
|
|
317 |
QTest::newRow("right") << path << QRectF(110, 50, 5, 5) << false;
|
|
318 |
QTest::newRow("bottom") << path << QRectF(50, 110, 5, 5) << false;
|
|
319 |
|
|
320 |
path.addRect(50, 50, 100, 100);
|
|
321 |
|
|
322 |
QTest::newRow("r1 top") << path << QRectF(0.1, 0.1, 99, 49) << true;
|
|
323 |
QTest::newRow("r1 left") << path << QRectF(0.1, 0.1, 49, 99) << true;
|
|
324 |
QTest::newRow("r2 right") << path << QRectF(100.01, 50.1, 49, 99) << true;
|
|
325 |
QTest::newRow("r2 bottom") << path << QRectF(50.1, 100.1, 99, 49) << true;
|
|
326 |
QTest::newRow("inside 2 rects") << path << QRectF(51, 51, 48, 48) << false;
|
|
327 |
QTest::newRow("topRight 2 rects") << path << QRectF(100, 0, 49, 49) << false;
|
|
328 |
QTest::newRow("bottomLeft 2 rects") << path << QRectF(0, 100, 49, 49) << false;
|
|
329 |
|
|
330 |
path.setFillRule(Qt::WindingFill);
|
|
331 |
QTest::newRow("inside 2 rects (winding)") << path << QRectF(51, 51, 48, 48) << true;
|
|
332 |
|
|
333 |
path.addEllipse(0, 0, 150, 150);
|
|
334 |
QTest::newRow("topRight 2 rects") << path << QRectF(100, 25, 24, 24) << true;
|
|
335 |
QTest::newRow("bottomLeft 2 rects") << path << QRectF(25, 100, 24, 24) << true;
|
|
336 |
|
|
337 |
path.setFillRule(Qt::OddEvenFill);
|
|
338 |
QTest::newRow("inside 2 rects") << path << QRectF(50, 50, 49, 49) << false;
|
|
339 |
}
|
|
340 |
|
|
341 |
void tst_QPainterPath::contains_QRectF()
|
|
342 |
{
|
|
343 |
QFETCH(QPainterPath, path);
|
|
344 |
QFETCH(QRectF, rect);
|
|
345 |
QFETCH(bool, contained);
|
|
346 |
|
|
347 |
QCOMPARE(path.contains(rect), contained);
|
|
348 |
}
|
|
349 |
|
|
350 |
static inline QPainterPath rectPath(qreal x, qreal y, qreal w, qreal h)
|
|
351 |
{
|
|
352 |
QPainterPath path;
|
|
353 |
path.addRect(x, y, w, h);
|
|
354 |
path.closeSubpath();
|
|
355 |
return path;
|
|
356 |
}
|
|
357 |
|
|
358 |
static inline QPainterPath ellipsePath(qreal x, qreal y, qreal w, qreal h)
|
|
359 |
{
|
|
360 |
QPainterPath path;
|
|
361 |
path.addEllipse(x, y, w, h);
|
|
362 |
path.closeSubpath();
|
|
363 |
return path;
|
|
364 |
}
|
|
365 |
|
|
366 |
static inline QPainterPath linePath(qreal x1, qreal y1, qreal x2, qreal y2)
|
|
367 |
{
|
|
368 |
QPainterPath path;
|
|
369 |
path.moveTo(x1, y1);
|
|
370 |
path.lineTo(x2, y2);
|
|
371 |
return path;
|
|
372 |
}
|
|
373 |
|
|
374 |
void tst_QPainterPath::intersects_QRectF_data()
|
|
375 |
{
|
|
376 |
QTest::addColumn<QPainterPath>("path");
|
|
377 |
QTest::addColumn<QRectF>("rect");
|
|
378 |
QTest::addColumn<bool>("intersects");
|
|
379 |
|
|
380 |
QPainterPath path;
|
|
381 |
path.addRect(0, 0, 100, 100);
|
|
382 |
|
|
383 |
QTest::newRow("same rect") << path << QRectF(0.1, 0.1, 99, 99) << true; // ###
|
|
384 |
QTest::newRow("outside") << path << QRectF(-1, -1, 100, 100) << true;
|
|
385 |
QTest::newRow("covers") << path << QRectF(-1, -1, 102, 102) << true;
|
|
386 |
QTest::newRow("left") << path << QRectF(-10, 50, 5, 5) << false;
|
|
387 |
QTest::newRow("top") << path << QRectF(50, -10, 5, 5) << false;
|
|
388 |
QTest::newRow("right") << path << QRectF(110, 50, 5, 5) << false;
|
|
389 |
QTest::newRow("bottom") << path << QRectF(50, 110, 5, 5) << false;
|
|
390 |
|
|
391 |
path.addRect(50, 50, 100, 100);
|
|
392 |
|
|
393 |
QTest::newRow("r1 top") << path << QRectF(0.1, 0.1, 99, 49) << true;
|
|
394 |
QTest::newRow("r1 left") << path << QRectF(0.1, 0.1, 49, 99) << true;
|
|
395 |
QTest::newRow("r2 right") << path << QRectF(100.01, 50.1, 49, 99) << true;
|
|
396 |
QTest::newRow("r2 bottom") << path << QRectF(50.1, 100.1, 99, 49) << true;
|
|
397 |
QTest::newRow("inside 2 rects") << path << QRectF(51, 51, 48, 48) << false;
|
|
398 |
|
|
399 |
path.setFillRule(Qt::WindingFill);
|
|
400 |
QTest::newRow("inside 2 rects (winding)") << path << QRectF(51, 51, 48, 48) << true;
|
|
401 |
|
|
402 |
path.addEllipse(0, 0, 150, 150);
|
|
403 |
QTest::newRow("topRight 2 rects") << path << QRectF(100, 25, 24, 24) << true;
|
|
404 |
QTest::newRow("bottomLeft 2 rects") << path << QRectF(25, 100, 24, 24) << true;
|
|
405 |
|
|
406 |
QTest::newRow("horizontal line") << linePath(0, 0, 10, 0) << QRectF(1, -1, 2, 2) << true;
|
|
407 |
QTest::newRow("vertical line") << linePath(0, 0, 0, 10) << QRectF(-1, 1, 2, 2) << true;
|
|
408 |
}
|
|
409 |
|
|
410 |
void tst_QPainterPath::intersects_QRectF()
|
|
411 |
{
|
|
412 |
QFETCH(QPainterPath, path);
|
|
413 |
QFETCH(QRectF, rect);
|
|
414 |
QFETCH(bool, intersects);
|
|
415 |
|
|
416 |
QCOMPARE(path.intersects(rect), intersects);
|
|
417 |
}
|
|
418 |
|
|
419 |
|
|
420 |
void tst_QPainterPath::testContainsAndIntersects_data()
|
|
421 |
{
|
|
422 |
QTest::addColumn<QPainterPath>("path");
|
|
423 |
QTest::addColumn<QPainterPath>("candidate");
|
|
424 |
QTest::addColumn<bool>("contained");
|
|
425 |
QTest::addColumn<bool>("intersects");
|
|
426 |
|
|
427 |
QTest::newRow("rect vs small ellipse (upper left)") << rectPath(0, 0, 100, 100) << ellipsePath(0, 0, 50, 50) << false << true;
|
|
428 |
QTest::newRow("rect vs small ellipse (upper right)") << rectPath(0, 0, 100, 100) << ellipsePath(50, 0, 50, 50) << false << true;
|
|
429 |
QTest::newRow("rect vs small ellipse (lower right)") << rectPath(0, 0, 100, 100) << ellipsePath(50, 50, 50, 50) << false << true;
|
|
430 |
QTest::newRow("rect vs small ellipse (lower left)") << rectPath(0, 0, 100, 100) << ellipsePath(0, 50, 50, 50) << false << true;
|
|
431 |
QTest::newRow("rect vs small ellipse (centered)") << rectPath(0, 0, 100, 100) << ellipsePath(25, 25, 50, 50) << true << true;
|
|
432 |
QTest::newRow("rect vs equal ellipse") << rectPath(0, 0, 100, 100) << ellipsePath(0, 0, 100, 100) << false << true;
|
|
433 |
QTest::newRow("rect vs big ellipse") << rectPath(0, 0, 100, 100) << ellipsePath(-10, -10, 120, 120) << false << true;
|
|
434 |
|
|
435 |
QPainterPath twoEllipses = ellipsePath(0, 0, 100, 100).united(ellipsePath(200, 0, 100, 100));
|
|
436 |
|
|
437 |
QTest::newRow("rect vs two small ellipses") << rectPath(0, 0, 100, 100) << ellipsePath(25, 25, 50, 50).united(ellipsePath(225, 25, 50, 50)) << false << true;
|
|
438 |
QTest::newRow("rect vs two equal ellipses") << rectPath(0, 0, 100, 100) << twoEllipses << false << true;
|
|
439 |
|
|
440 |
QTest::newRow("rect vs self") << rectPath(0, 0, 100, 100) << rectPath(0, 0, 100, 100) << false << true;
|
|
441 |
QTest::newRow("ellipse vs self") << ellipsePath(0, 0, 100, 100) << ellipsePath(0, 0, 100, 100) << false << true;
|
|
442 |
|
|
443 |
QPainterPath twoRects = rectPath(0, 0, 100, 100).united(rectPath(200, 0, 100, 100));
|
|
444 |
QTest::newRow("two rects vs small ellipse (upper left)") << twoRects << ellipsePath(0, 0, 50, 50) << false << true;
|
|
445 |
QTest::newRow("two rects vs small ellipse (upper right)") << twoRects << ellipsePath(50, 0, 50, 50) << false << true;
|
|
446 |
QTest::newRow("two rects vs small ellipse (lower right)") << twoRects << ellipsePath(50, 50, 50, 50) << false << true;
|
|
447 |
QTest::newRow("two rects vs small ellipse (lower left)") << twoRects << ellipsePath(0, 50, 50, 50) << false << true;
|
|
448 |
QTest::newRow("two rects vs small ellipse (centered)") << twoRects << ellipsePath(25, 25, 50, 50) << true << true;
|
|
449 |
QTest::newRow("two rects vs equal ellipse") << twoRects << ellipsePath(0, 0, 100, 100) << false << true;
|
|
450 |
QTest::newRow("two rects vs big ellipse") << twoRects << ellipsePath(-10, -10, 120, 120) << false << true;
|
|
451 |
|
|
452 |
QTest::newRow("two rects vs two small ellipses") << twoRects << ellipsePath(25, 25, 50, 50).united(ellipsePath(225, 25, 50, 50)) << true << true;
|
|
453 |
QTest::newRow("two rects vs two equal ellipses") << twoRects << ellipsePath(0, 0, 100, 100).united(ellipsePath(200, 0, 100, 100)) << false << true;
|
|
454 |
|
|
455 |
QTest::newRow("two rects vs self") << twoRects << twoRects << false << true;
|
|
456 |
QTest::newRow("two ellipses vs self") << twoEllipses << twoEllipses << false << true;
|
|
457 |
|
|
458 |
QPainterPath windingRect = rectPath(0, 0, 100, 100);
|
|
459 |
windingRect.addRect(25, 25, 100, 50);
|
|
460 |
windingRect.setFillRule(Qt::WindingFill);
|
|
461 |
|
|
462 |
QTest::newRow("rect with winding rule vs tall rect") << windingRect << rectPath(40, 20, 20, 60) << true << true;
|
|
463 |
QTest::newRow("rect with winding rule vs self") << windingRect << windingRect << false << true;
|
|
464 |
|
|
465 |
QPainterPath thickFrame = rectPath(0, 0, 100, 100).subtracted(rectPath(25, 25, 50, 50));
|
|
466 |
QPainterPath thinFrame = rectPath(10, 10, 80, 80).subtracted(rectPath(15, 15, 70, 70));
|
|
467 |
|
|
468 |
QTest::newRow("thin frame in thick frame") << thickFrame << thinFrame << true << true;
|
|
469 |
QTest::newRow("rect in thick frame") << thickFrame << rectPath(40, 40, 20, 20) << false << false;
|
|
470 |
QTest::newRow("rect in thin frame") << thinFrame << rectPath(40, 40, 20, 20) << false << false;
|
|
471 |
|
|
472 |
QPainterPath ellipses;
|
|
473 |
ellipses.addEllipse(0, 0, 10, 10);
|
|
474 |
ellipses.addEllipse(4, 4, 2, 2);
|
|
475 |
ellipses.setFillRule(Qt::WindingFill);
|
|
476 |
|
|
477 |
// the definition of QPainterPath::intersects() and contains() is fill-area based,
|
|
478 |
QTest::newRow("line in rect") << rectPath(0, 0, 100, 100) << linePath(10, 10, 90, 90) << true << true;
|
|
479 |
QTest::newRow("horizontal line in rect") << rectPath(0, 0, 100, 100) << linePath(10, 50, 90, 50) << true << true;
|
|
480 |
QTest::newRow("vertical line in rect") << rectPath(0, 0, 100, 100) << linePath(50, 10, 50, 90) << true << true;
|
|
481 |
|
|
482 |
QTest::newRow("line through rect") << rectPath(0, 0, 100, 100) << linePath(-10, -10, 110, 110) << false << true;
|
|
483 |
QTest::newRow("line through rect 2") << rectPath(0, 0, 100, 100) << linePath(-10, 0, 110, 100) << false << true;
|
|
484 |
QTest::newRow("line through rect 3") << rectPath(0, 0, 100, 100) << linePath(5, 10, 110, 100) << false << true;
|
|
485 |
QTest::newRow("line through rect 4") << rectPath(0, 0, 100, 100) << linePath(-10, 0, 90, 90) << false << true;
|
|
486 |
|
|
487 |
QTest::newRow("horizontal line through rect") << rectPath(0, 0, 100, 100) << linePath(-10, 50, 110, 50) << false << true;
|
|
488 |
QTest::newRow("vertical line through rect") << rectPath(0, 0, 100, 100) << linePath(50, -10, 50, 110) << false << true;
|
|
489 |
|
|
490 |
QTest::newRow("line vs line") << linePath(0, 0, 10, 10) << linePath(10, 0, 0, 10) << false << true;
|
|
491 |
|
|
492 |
QTest::newRow("line in rect with hole") << rectPath(0, 0, 10, 10).subtracted(rectPath(2, 2, 6, 6)) << linePath(4, 4, 6, 6) << false << false;
|
|
493 |
QTest::newRow("line in ellipse") << ellipses << linePath(3, 5, 7, 5) << false << true;
|
|
494 |
QTest::newRow("line in ellipse 2") << ellipses << linePath(4.5, 5, 5.5, 5) << true << true;
|
|
495 |
|
|
496 |
QTest::newRow("winding ellipse") << ellipses << ellipsePath(4, 4, 2, 2) << false << true;
|
|
497 |
QTest::newRow("winding ellipse 2") << ellipses << ellipsePath(4.5, 4.5, 1, 1) << true << true;
|
|
498 |
ellipses.setFillRule(Qt::OddEvenFill);
|
|
499 |
QTest::newRow("odd even ellipse") << ellipses << ellipsePath(4, 4, 2, 2) << false << true;
|
|
500 |
QTest::newRow("odd even ellipse 2") << ellipses << ellipsePath(4.5, 4.5, 1, 1) << false << false;
|
|
501 |
}
|
|
502 |
|
|
503 |
void tst_QPainterPath::testContainsAndIntersects()
|
|
504 |
{
|
|
505 |
QFETCH(QPainterPath, path);
|
|
506 |
QFETCH(QPainterPath, candidate);
|
|
507 |
QFETCH(bool, contained);
|
|
508 |
QFETCH(bool, intersects);
|
|
509 |
|
|
510 |
QCOMPARE(path.intersects(candidate), intersects);
|
|
511 |
QCOMPARE(path.contains(candidate), contained);
|
|
512 |
}
|
|
513 |
|
|
514 |
void tst_QPainterPath::testSimplified_data()
|
|
515 |
{
|
|
516 |
QTest::addColumn<QPainterPath>("path");
|
|
517 |
QTest::addColumn<int>("elements");
|
|
518 |
|
|
519 |
QTest::newRow("rect") << rectPath(0, 0, 10, 10) << 5;
|
|
520 |
QTest::newRow("ellipse") << ellipsePath(0, 0, 10, 10) << 13;
|
|
521 |
|
|
522 |
QPainterPath twoRects = rectPath(0, 0, 10, 10);
|
|
523 |
twoRects.addPath(rectPath(5, 0, 10, 10));
|
|
524 |
QTest::newRow("two rects (odd)") << twoRects << 10;
|
|
525 |
|
|
526 |
twoRects.setFillRule(Qt::WindingFill);
|
|
527 |
QTest::newRow("two rects (winding)") << twoRects << 5;
|
|
528 |
|
|
529 |
QPainterPath threeSteps = rectPath(0, 0, 10, 10);
|
|
530 |
threeSteps.addPath(rectPath(0, 10, 20, 10));
|
|
531 |
threeSteps.addPath(rectPath(0, 20, 30, 10));
|
|
532 |
|
|
533 |
QTest::newRow("three rects (steps)") << threeSteps << 9;
|
|
534 |
}
|
|
535 |
|
|
536 |
void tst_QPainterPath::testSimplified()
|
|
537 |
{
|
|
538 |
QFETCH(QPainterPath, path);
|
|
539 |
QFETCH(int, elements);
|
|
540 |
|
|
541 |
QPainterPath simplified = path.simplified();
|
|
542 |
|
|
543 |
QCOMPARE(simplified.elementCount(), elements);
|
|
544 |
|
|
545 |
QVERIFY(simplified.subtracted(path).isEmpty());
|
|
546 |
QVERIFY(path.subtracted(simplified).isEmpty());
|
|
547 |
}
|
|
548 |
|
|
549 |
void tst_QPainterPath::testStroker_data()
|
|
550 |
{
|
|
551 |
QTest::addColumn<QPainterPath>("path");
|
|
552 |
QTest::addColumn<QPen>("pen");
|
|
553 |
QTest::addColumn<QPainterPath>("stroke");
|
|
554 |
|
|
555 |
QTest::newRow("line 1") << linePath(2, 2, 10, 2) << QPen(Qt::black, 2, Qt::SolidLine, Qt::FlatCap) << rectPath(2, 1, 8, 2);
|
|
556 |
QTest::newRow("line 2") << linePath(2, 2, 10, 2) << QPen(Qt::black, 2, Qt::SolidLine, Qt::SquareCap) << rectPath(1, 1, 10, 2);
|
|
557 |
|
|
558 |
QTest::newRow("rect") << rectPath(1, 1, 8, 8) << QPen(Qt::black, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin) << rectPath(0, 0, 10, 10).subtracted(rectPath(2, 2, 6, 6));
|
|
559 |
|
|
560 |
QTest::newRow("dotted line") << linePath(0, 0, 10, 0) << QPen(Qt::black, 2, Qt::DotLine) << rectPath(-1, -1, 4, 2).united(rectPath(5, -1, 4, 2));
|
|
561 |
}
|
|
562 |
|
|
563 |
void tst_QPainterPath::testStroker()
|
|
564 |
{
|
|
565 |
QFETCH(QPainterPath, path);
|
|
566 |
QFETCH(QPen, pen);
|
|
567 |
QFETCH(QPainterPath, stroke);
|
|
568 |
|
|
569 |
QPainterPathStroker stroker;
|
|
570 |
stroker.setWidth(pen.widthF());
|
|
571 |
stroker.setCapStyle(pen.capStyle());
|
|
572 |
stroker.setJoinStyle(pen.joinStyle());
|
|
573 |
stroker.setMiterLimit(pen.miterLimit());
|
|
574 |
stroker.setDashPattern(pen.style());
|
|
575 |
stroker.setDashOffset(pen.dashOffset());
|
|
576 |
|
|
577 |
QPainterPath result = stroker.createStroke(path);
|
|
578 |
|
|
579 |
// check if stroke == result
|
|
580 |
QVERIFY(result.subtracted(stroke).isEmpty());
|
|
581 |
QVERIFY(stroke.subtracted(result).isEmpty());
|
|
582 |
}
|
|
583 |
|
|
584 |
void tst_QPainterPath::testOperatorEquals()
|
|
585 |
{
|
|
586 |
QPainterPath empty1;
|
|
587 |
QPainterPath empty2;
|
|
588 |
QVERIFY(empty1 == empty2);
|
|
589 |
|
|
590 |
QPainterPath rect1;
|
|
591 |
rect1.addRect(100, 100, 100, 100);
|
|
592 |
QVERIFY(rect1 == rect1);
|
|
593 |
QVERIFY(rect1 != empty1);
|
|
594 |
|
|
595 |
QPainterPath rect2;
|
|
596 |
rect2.addRect(100, 100, 100, 100);
|
|
597 |
QVERIFY(rect1 == rect2);
|
|
598 |
|
|
599 |
rect2.setFillRule(Qt::WindingFill);
|
|
600 |
QVERIFY(rect1 != rect2);
|
|
601 |
|
|
602 |
QPainterPath ellipse1;
|
|
603 |
ellipse1.addEllipse(50, 50, 100, 100);
|
|
604 |
QVERIFY(rect1 != ellipse1);
|
|
605 |
|
|
606 |
QPainterPath ellipse2;
|
|
607 |
ellipse2.addEllipse(50, 50, 100, 100);
|
|
608 |
QVERIFY(ellipse1 == ellipse2);
|
|
609 |
}
|
|
610 |
|
|
611 |
void tst_QPainterPath::testOperatorEquals_fuzzy()
|
|
612 |
{
|
|
613 |
// if operator== returns true for two paths it should
|
|
614 |
// also return true when the same transform is applied to both paths
|
|
615 |
{
|
|
616 |
QRectF a(100, 100, 100, 50);
|
|
617 |
QRectF b = a.translated(1e-14, 1e-14);
|
|
618 |
|
|
619 |
QPainterPath pa;
|
|
620 |
pa.addRect(a);
|
|
621 |
QPainterPath pb;
|
|
622 |
pb.addRect(b);
|
|
623 |
|
|
624 |
QVERIFY(pa == pb);
|
|
625 |
|
|
626 |
QTransform transform;
|
|
627 |
transform.translate(-100, -100);
|
|
628 |
|
|
629 |
QVERIFY(transform.map(pa) == transform.map(pb));
|
|
630 |
}
|
|
631 |
|
|
632 |
// higher tolerance for error when path's bounding rect is big
|
|
633 |
{
|
|
634 |
QRectF a(1, 1, 1e6, 0.5e6);
|
|
635 |
QRectF b = a.translated(1e-7, 1e-7);
|
|
636 |
|
|
637 |
QPainterPath pa;
|
|
638 |
pa.addRect(a);
|
|
639 |
QPainterPath pb;
|
|
640 |
pb.addRect(b);
|
|
641 |
|
|
642 |
QVERIFY(pa == pb);
|
|
643 |
|
|
644 |
QTransform transform;
|
|
645 |
transform.translate(-1, -1);
|
|
646 |
|
|
647 |
QVERIFY(transform.map(pa) == transform.map(pb));
|
|
648 |
}
|
|
649 |
|
|
650 |
// operator== should return true for a path that has
|
|
651 |
// been transformed and then inverse transformed
|
|
652 |
{
|
|
653 |
QPainterPath a;
|
|
654 |
a.addRect(0, 0, 100, 100);
|
|
655 |
|
|
656 |
QTransform transform;
|
|
657 |
transform.translate(100, 20);
|
|
658 |
transform.scale(1.5, 1.5);
|
|
659 |
|
|
660 |
QPainterPath b = transform.inverted().map(transform.map(a));
|
|
661 |
|
|
662 |
QVERIFY(a == b);
|
|
663 |
}
|
|
664 |
|
|
665 |
{
|
|
666 |
QPainterPath a;
|
|
667 |
a.lineTo(10, 0);
|
|
668 |
a.lineTo(10, 10);
|
|
669 |
a.lineTo(0, 10);
|
|
670 |
|
|
671 |
QPainterPath b;
|
|
672 |
b.lineTo(10, 0);
|
|
673 |
b.moveTo(10, 10);
|
|
674 |
b.lineTo(0, 10);
|
|
675 |
|
|
676 |
QVERIFY(a != b);
|
|
677 |
}
|
|
678 |
}
|
|
679 |
|
|
680 |
void tst_QPainterPath::testOperatorDatastream()
|
|
681 |
{
|
|
682 |
QPainterPath path;
|
|
683 |
path.addEllipse(0, 0, 100, 100);
|
|
684 |
path.addRect(0, 0, 100, 100);
|
|
685 |
path.setFillRule(Qt::WindingFill);
|
|
686 |
|
|
687 |
// Write out
|
|
688 |
{
|
|
689 |
QFile data("data");
|
|
690 |
bool ok = data.open(QFile::WriteOnly);
|
|
691 |
QVERIFY(ok);
|
|
692 |
QDataStream stream(&data);
|
|
693 |
stream << path;
|
|
694 |
}
|
|
695 |
|
|
696 |
QPainterPath other;
|
|
697 |
// Read in
|
|
698 |
{
|
|
699 |
QFile data("data");
|
|
700 |
bool ok = data.open(QFile::ReadOnly);
|
|
701 |
QVERIFY(ok);
|
|
702 |
QDataStream stream(&data);
|
|
703 |
stream >> other;
|
|
704 |
}
|
|
705 |
|
|
706 |
QVERIFY(other == path);
|
|
707 |
}
|
|
708 |
|
|
709 |
void tst_QPainterPath::closing()
|
|
710 |
{
|
|
711 |
// lineto's
|
|
712 |
{
|
|
713 |
QPainterPath triangle(QPoint(100, 100));
|
|
714 |
|
|
715 |
triangle.lineTo(200, 100);
|
|
716 |
triangle.lineTo(200, 200);
|
|
717 |
QCOMPARE(triangle.elementCount(), 3);
|
|
718 |
|
|
719 |
triangle.closeSubpath();
|
|
720 |
QCOMPARE(triangle.elementCount(), 4);
|
|
721 |
QCOMPARE(triangle.elementAt(3).type, QPainterPath::LineToElement);
|
|
722 |
|
|
723 |
triangle.moveTo(300, 300);
|
|
724 |
QCOMPARE(triangle.elementCount(), 5);
|
|
725 |
QCOMPARE(triangle.elementAt(4).type, QPainterPath::MoveToElement);
|
|
726 |
|
|
727 |
triangle.lineTo(400, 300);
|
|
728 |
triangle.lineTo(400, 400);
|
|
729 |
QCOMPARE(triangle.elementCount(), 7);
|
|
730 |
|
|
731 |
triangle.closeSubpath();
|
|
732 |
QCOMPARE(triangle.elementCount(), 8);
|
|
733 |
|
|
734 |
// this will should trigger implicit moveto...
|
|
735 |
triangle.lineTo(600, 300);
|
|
736 |
QCOMPARE(triangle.elementCount(), 10);
|
|
737 |
QCOMPARE(triangle.elementAt(8).type, QPainterPath::MoveToElement);
|
|
738 |
QCOMPARE(triangle.elementAt(9).type, QPainterPath::LineToElement);
|
|
739 |
|
|
740 |
triangle.lineTo(600, 700);
|
|
741 |
QCOMPARE(triangle.elementCount(), 11);
|
|
742 |
}
|
|
743 |
|
|
744 |
// curveto's
|
|
745 |
{
|
|
746 |
QPainterPath curves(QPoint(100, 100));
|
|
747 |
|
|
748 |
curves.cubicTo(200, 100, 100, 200, 200, 200);
|
|
749 |
QCOMPARE(curves.elementCount(), 4);
|
|
750 |
|
|
751 |
curves.closeSubpath();
|
|
752 |
QCOMPARE(curves.elementCount(), 5);
|
|
753 |
QCOMPARE(curves.elementAt(4).type, QPainterPath::LineToElement);
|
|
754 |
|
|
755 |
curves.moveTo(300, 300);
|
|
756 |
QCOMPARE(curves.elementCount(), 6);
|
|
757 |
QCOMPARE(curves.elementAt(5).type, QPainterPath::MoveToElement);
|
|
758 |
|
|
759 |
curves.cubicTo(400, 300, 300, 400, 400, 400);
|
|
760 |
QCOMPARE(curves.elementCount(), 9);
|
|
761 |
|
|
762 |
curves.closeSubpath();
|
|
763 |
QCOMPARE(curves.elementCount(), 10);
|
|
764 |
|
|
765 |
// should trigger implicit moveto..
|
|
766 |
curves.cubicTo(100, 800, 800, 100, 800, 800);
|
|
767 |
QCOMPARE(curves.elementCount(), 14);
|
|
768 |
QCOMPARE(curves.elementAt(10).type, QPainterPath::MoveToElement);
|
|
769 |
QCOMPARE(curves.elementAt(11).type, QPainterPath::CurveToElement);
|
|
770 |
}
|
|
771 |
|
|
772 |
{
|
|
773 |
QPainterPath rects;
|
|
774 |
rects.addRect(100, 100, 100, 100);
|
|
775 |
|
|
776 |
QCOMPARE(rects.elementCount(), 5);
|
|
777 |
QCOMPARE(rects.elementAt(0).type, QPainterPath::MoveToElement);
|
|
778 |
QCOMPARE(rects.elementAt(4).type, QPainterPath::LineToElement);
|
|
779 |
|
|
780 |
rects.addRect(300, 100, 100,100);
|
|
781 |
QCOMPARE(rects.elementCount(), 10);
|
|
782 |
QCOMPARE(rects.elementAt(5).type, QPainterPath::MoveToElement);
|
|
783 |
QCOMPARE(rects.elementAt(9).type, QPainterPath::LineToElement);
|
|
784 |
|
|
785 |
rects.lineTo(0, 0);
|
|
786 |
QCOMPARE(rects.elementCount(), 12);
|
|
787 |
QCOMPARE(rects.elementAt(10).type, QPainterPath::MoveToElement);
|
|
788 |
QCOMPARE(rects.elementAt(11).type, QPainterPath::LineToElement);
|
|
789 |
}
|
|
790 |
|
|
791 |
{
|
|
792 |
QPainterPath ellipses;
|
|
793 |
ellipses.addEllipse(100, 100, 100, 100);
|
|
794 |
|
|
795 |
QCOMPARE(ellipses.elementCount(), 13);
|
|
796 |
QCOMPARE(ellipses.elementAt(0).type, QPainterPath::MoveToElement);
|
|
797 |
QCOMPARE(ellipses.elementAt(10).type, QPainterPath::CurveToElement);
|
|
798 |
|
|
799 |
ellipses.addEllipse(300, 100, 100,100);
|
|
800 |
QCOMPARE(ellipses.elementCount(), 26);
|
|
801 |
QCOMPARE(ellipses.elementAt(13).type, QPainterPath::MoveToElement);
|
|
802 |
QCOMPARE(ellipses.elementAt(23).type, QPainterPath::CurveToElement);
|
|
803 |
|
|
804 |
ellipses.lineTo(0, 0);
|
|
805 |
QCOMPARE(ellipses.elementCount(), 28);
|
|
806 |
QCOMPARE(ellipses.elementAt(26).type, QPainterPath::MoveToElement);
|
|
807 |
QCOMPARE(ellipses.elementAt(27).type, QPainterPath::LineToElement);
|
|
808 |
}
|
|
809 |
|
|
810 |
{
|
|
811 |
QPainterPath path;
|
|
812 |
path.moveTo(10, 10);
|
|
813 |
path.lineTo(40, 10);
|
|
814 |
path.lineTo(25, 20);
|
|
815 |
path.lineTo(10 + 1e-13, 10 + 1e-13);
|
|
816 |
QCOMPARE(path.elementCount(), 4);
|
|
817 |
path.closeSubpath();
|
|
818 |
QCOMPARE(path.elementCount(), 4);
|
|
819 |
}
|
|
820 |
}
|
|
821 |
|
|
822 |
void tst_QPainterPath::testArcMoveTo_data()
|
|
823 |
{
|
|
824 |
QTest::addColumn<QRectF>("rect");
|
|
825 |
QTest::addColumn<qreal>("angle");
|
|
826 |
|
|
827 |
QList<QRectF> rects;
|
|
828 |
rects << QRectF(100, 100, 100, 100)
|
|
829 |
<< QRectF(100, 100, -100, 100)
|
|
830 |
<< QRectF(100, 100, 100, -100)
|
|
831 |
<< QRectF(100, 100, -100, -100);
|
|
832 |
|
|
833 |
for (int domain=0; domain<rects.size(); ++domain) {
|
|
834 |
for (int i=-360; i<=360; ++i) {
|
|
835 |
QTest::newRow("test") << rects.at(domain) << (qreal) i;
|
|
836 |
}
|
|
837 |
|
|
838 |
// test low angles
|
|
839 |
QTest::newRow("test") << rects.at(domain) << (qreal) 1e-10;
|
|
840 |
QTest::newRow("test") << rects.at(domain) << (qreal)-1e-10;
|
|
841 |
}
|
|
842 |
}
|
|
843 |
|
|
844 |
void tst_QPainterPath::operators_data()
|
|
845 |
{
|
|
846 |
QTest::addColumn<QPainterPath>("test");
|
|
847 |
QTest::addColumn<QPainterPath>("expected");
|
|
848 |
|
|
849 |
QPainterPath a;
|
|
850 |
QPainterPath b;
|
|
851 |
a.addRect(0, 0, 100, 100);
|
|
852 |
b.addRect(50, 50, 100, 100);
|
|
853 |
|
|
854 |
QTest::newRow("a & b") << (a & b) << a.intersected(b);
|
|
855 |
QTest::newRow("a | b") << (a | b) << a.united(b);
|
|
856 |
QTest::newRow("a + b") << (a + b) << a.united(b);
|
|
857 |
QTest::newRow("a - b") << (a - b) << a.subtracted(b);
|
|
858 |
|
|
859 |
QPainterPath c = a;
|
|
860 |
QTest::newRow("a &= b") << (a &= b) << a.intersected(b);
|
|
861 |
c = a;
|
|
862 |
QTest::newRow("a |= b") << (a |= b) << a.united(b);
|
|
863 |
c = a;
|
|
864 |
QTest::newRow("a += b") << (a += b) << a.united(b);
|
|
865 |
c = a;
|
|
866 |
QTest::newRow("a -= b") << (a -= b) << a.subtracted(b);
|
|
867 |
}
|
|
868 |
|
|
869 |
void tst_QPainterPath::operators()
|
|
870 |
{
|
|
871 |
QFETCH(QPainterPath, test);
|
|
872 |
QFETCH(QPainterPath, expected);
|
|
873 |
|
|
874 |
QCOMPARE(test, expected);
|
|
875 |
}
|
|
876 |
|
|
877 |
#ifndef M_PI
|
|
878 |
#define M_PI 3.14159265358979323846
|
|
879 |
#endif
|
|
880 |
#define ANGLE(t) ((t) * 2 * M_PI / 360.0)
|
|
881 |
|
|
882 |
|
|
883 |
static inline bool pathFuzzyCompare(double p1, double p2)
|
|
884 |
{
|
|
885 |
return qAbs(p1 - p2) < 0.001;
|
|
886 |
}
|
|
887 |
|
|
888 |
|
|
889 |
static inline bool pathFuzzyCompare(float p1, float p2)
|
|
890 |
{
|
|
891 |
return qAbs(p1 - p2) < 0.001;
|
|
892 |
}
|
|
893 |
|
|
894 |
|
|
895 |
void tst_QPainterPath::testArcMoveTo()
|
|
896 |
{
|
|
897 |
QFETCH(QRectF, rect);
|
|
898 |
QFETCH(qreal, angle);
|
|
899 |
|
|
900 |
QPainterPath path;
|
|
901 |
path.arcMoveTo(rect, angle);
|
|
902 |
path.arcTo(rect, angle, 30);
|
|
903 |
path.arcTo(rect, angle + 30, 30);
|
|
904 |
|
|
905 |
QPointF pos = path.elementAt(0);
|
|
906 |
|
|
907 |
QVERIFY((path.elementCount()-1) % 3 == 0);
|
|
908 |
|
|
909 |
qreal x_radius = rect.width() / 2.0;
|
|
910 |
qreal y_radius = rect.height() / 2.0;
|
|
911 |
|
|
912 |
QPointF shouldBe = rect.center()
|
|
913 |
+ QPointF(x_radius * cos(ANGLE(angle)), -y_radius * sin(ANGLE(angle)));
|
|
914 |
|
|
915 |
qreal iw = 1 / rect.width();
|
|
916 |
qreal ih = 1 / rect.height();
|
|
917 |
|
|
918 |
QVERIFY(pathFuzzyCompare(pos.x() * iw, shouldBe.x() * iw));
|
|
919 |
QVERIFY(pathFuzzyCompare(pos.y() * ih, shouldBe.y() * ih));
|
|
920 |
}
|
|
921 |
|
|
922 |
void tst_QPainterPath::testOnPath_data()
|
|
923 |
{
|
|
924 |
QTest::addColumn<QPainterPath>("path");
|
|
925 |
QTest::addColumn<qreal>("start");
|
|
926 |
QTest::addColumn<qreal>("middle");
|
|
927 |
QTest::addColumn<qreal>("end");
|
|
928 |
|
|
929 |
QPainterPath path = QPainterPath(QPointF(153, 199));
|
|
930 |
path.cubicTo(QPointF(147, 61), QPointF(414, 18),
|
|
931 |
QPointF(355, 201));
|
|
932 |
|
|
933 |
QTest::newRow("First case") << path
|
|
934 |
<< qreal(93.0)
|
|
935 |
<< qreal(4.0)
|
|
936 |
<< qreal(252.13);
|
|
937 |
|
|
938 |
path = QPainterPath(QPointF(328, 197));
|
|
939 |
path.cubicTo(QPointF(150, 50), QPointF(401, 50),
|
|
940 |
QPointF(225, 197));
|
|
941 |
QTest::newRow("Second case") << path
|
|
942 |
<< qreal(140.0)
|
|
943 |
<< qreal(0.0)
|
|
944 |
<< qreal(220.0);
|
|
945 |
|
|
946 |
path = QPainterPath(QPointF(328, 197));
|
|
947 |
path.cubicTo(QPointF(101 , 153), QPointF(596, 151),
|
|
948 |
QPointF(353, 197));
|
|
949 |
QTest::newRow("Third case") << path
|
|
950 |
<< qreal(169.0)
|
|
951 |
<< qreal(0.22)
|
|
952 |
<< qreal(191.0);
|
|
953 |
|
|
954 |
path = QPainterPath(QPointF(153, 199));
|
|
955 |
path.cubicTo(QPointF(59, 53), QPointF(597, 218),
|
|
956 |
QPointF(355, 201));
|
|
957 |
QTest::newRow("Fourth case") << path
|
|
958 |
<< qreal(122.0)
|
|
959 |
<< qreal(348.0)
|
|
960 |
<< qreal(175.0);
|
|
961 |
|
|
962 |
}
|
|
963 |
|
|
964 |
#define SIGN(x) ((x < 0)?-1:1)
|
|
965 |
void tst_QPainterPath::testOnPath()
|
|
966 |
{
|
|
967 |
QFETCH(QPainterPath, path);
|
|
968 |
QFETCH(qreal, start);
|
|
969 |
QFETCH(qreal, middle);
|
|
970 |
QFETCH(qreal, end);
|
|
971 |
|
|
972 |
int signStart = SIGN(start);
|
|
973 |
int signMid = SIGN(middle);
|
|
974 |
int signEnd = SIGN(end);
|
|
975 |
|
|
976 |
static const qreal diff = 3;
|
|
977 |
|
|
978 |
qreal angle = path.angleAtPercent(0);
|
|
979 |
QCOMPARE(SIGN(angle), signStart);
|
|
980 |
QVERIFY(qAbs(angle-start) < diff);
|
|
981 |
|
|
982 |
angle = path.angleAtPercent(0.5);
|
|
983 |
QCOMPARE(SIGN(angle), signMid);
|
|
984 |
QVERIFY(qAbs(angle-middle) < diff);
|
|
985 |
|
|
986 |
angle = path.angleAtPercent(1);
|
|
987 |
QCOMPARE(SIGN(angle), signEnd);
|
|
988 |
QVERIFY(qAbs(angle-end) < diff);
|
|
989 |
}
|
|
990 |
|
|
991 |
void tst_QPainterPath::pointAtPercent_data()
|
|
992 |
{
|
|
993 |
QTest::addColumn<QPainterPath>("path");
|
|
994 |
QTest::addColumn<qreal>("percent");
|
|
995 |
QTest::addColumn<QPointF>("point");
|
|
996 |
|
|
997 |
QPainterPath path;
|
|
998 |
path.lineTo(100, 0);
|
|
999 |
|
|
1000 |
QTest::newRow("Case 1") << path << qreal(0.2) << QPointF(20, 0);
|
|
1001 |
QTest::newRow("Case 2") << path << qreal(0.5) << QPointF(50, 0);
|
|
1002 |
QTest::newRow("Case 3") << path << qreal(0.0) << QPointF(0, 0);
|
|
1003 |
QTest::newRow("Case 4") << path << qreal(1.0) << QPointF(100, 0);
|
|
1004 |
|
|
1005 |
path = QPainterPath();
|
|
1006 |
path.lineTo(0, 100);
|
|
1007 |
|
|
1008 |
QTest::newRow("Case 5") << path << qreal(0.2) << QPointF(0, 20);
|
|
1009 |
QTest::newRow("Case 6") << path << qreal(0.5) << QPointF(0, 50);
|
|
1010 |
QTest::newRow("Case 7") << path << qreal(0.0) << QPointF(0, 0);
|
|
1011 |
QTest::newRow("Case 8") << path << qreal(1.0) << QPointF(0, 100);
|
|
1012 |
|
|
1013 |
path.lineTo(300, 100);
|
|
1014 |
|
|
1015 |
QTest::newRow("Case 9") << path << qreal(0.25) << QPointF(0, 100);
|
|
1016 |
QTest::newRow("Case 10") << path << qreal(0.5) << QPointF(100, 100);
|
|
1017 |
QTest::newRow("Case 11") << path << qreal(0.75) << QPointF(200, 100);
|
|
1018 |
|
|
1019 |
path = QPainterPath();
|
|
1020 |
path.addEllipse(0, 0, 100, 100);
|
|
1021 |
|
|
1022 |
QTest::newRow("Case 12") << path << qreal(0.0) << QPointF(100, 50);
|
|
1023 |
QTest::newRow("Case 13") << path << qreal(0.25) << QPointF(50, 100);
|
|
1024 |
QTest::newRow("Case 14") << path << qreal(0.5) << QPointF(0, 50);
|
|
1025 |
QTest::newRow("Case 15") << path << qreal(0.75) << QPointF(50, 0);
|
|
1026 |
QTest::newRow("Case 16") << path << qreal(1.0) << QPointF(100, 50);
|
|
1027 |
|
|
1028 |
path = QPainterPath();
|
|
1029 |
QRectF rect(241, 273, 185, 228);
|
|
1030 |
path.addEllipse(rect);
|
|
1031 |
QTest::newRow("Case 17") << path << qreal(1.0) << QPointF(rect.right(), qreal(0.5) * (rect.top() + rect.bottom()));
|
|
1032 |
}
|
|
1033 |
|
|
1034 |
void tst_QPainterPath::pointAtPercent()
|
|
1035 |
{
|
|
1036 |
QFETCH(QPainterPath, path);
|
|
1037 |
QFETCH(qreal, percent);
|
|
1038 |
QFETCH(QPointF, point);
|
|
1039 |
|
|
1040 |
QPointF result = path.pointAtPercent(percent);
|
|
1041 |
QVERIFY(pathFuzzyCompare(point.x() , result.x()));
|
|
1042 |
QVERIFY(pathFuzzyCompare(point.y() , result.y()));
|
|
1043 |
}
|
|
1044 |
|
|
1045 |
void tst_QPainterPath::setElementPositionAt()
|
|
1046 |
{
|
|
1047 |
QPainterPath path(QPointF(42., 42.));
|
|
1048 |
QCOMPARE(path.elementCount(), 1);
|
|
1049 |
QVERIFY(path.elementAt(0).type == QPainterPath::MoveToElement);
|
|
1050 |
QCOMPARE(path.elementAt(0).x, qreal(42.));
|
|
1051 |
QCOMPARE(path.elementAt(0).y, qreal(42.));
|
|
1052 |
|
|
1053 |
QPainterPath copy = path;
|
|
1054 |
copy.setElementPositionAt(0, qreal(0), qreal(0));
|
|
1055 |
QCOMPARE(copy.elementCount(), 1);
|
|
1056 |
QVERIFY(copy.elementAt(0).type == QPainterPath::MoveToElement);
|
|
1057 |
QCOMPARE(copy.elementAt(0).x, qreal(0));
|
|
1058 |
QCOMPARE(copy.elementAt(0).y, qreal(0));
|
|
1059 |
|
|
1060 |
QCOMPARE(path.elementCount(), 1);
|
|
1061 |
QVERIFY(path.elementAt(0).type == QPainterPath::MoveToElement);
|
|
1062 |
QCOMPARE(path.elementAt(0).x, qreal(42.));
|
|
1063 |
QCOMPARE(path.elementAt(0).y, qreal(42.));
|
|
1064 |
}
|
|
1065 |
|
|
1066 |
void tst_QPainterPath::angleAtPercent()
|
|
1067 |
{
|
|
1068 |
for (int angle = 0; angle < 360; ++angle) {
|
|
1069 |
QLineF line = QLineF::fromPolar(100, angle);
|
|
1070 |
QPainterPath path;
|
|
1071 |
path.moveTo(line.p1());
|
|
1072 |
path.lineTo(line.p2());
|
|
1073 |
|
|
1074 |
QCOMPARE(path.angleAtPercent(0.5), line.angle());
|
|
1075 |
}
|
|
1076 |
}
|
|
1077 |
|
|
1078 |
void tst_QPainterPath::arcWinding_data()
|
|
1079 |
{
|
|
1080 |
QTest::addColumn<QPainterPath>("path");
|
|
1081 |
QTest::addColumn<QPointF>("point");
|
|
1082 |
QTest::addColumn<bool>("inside");
|
|
1083 |
|
|
1084 |
QPainterPath a;
|
|
1085 |
a.addEllipse(0, 0, 100, 100);
|
|
1086 |
a.addRect(50, 50, 100, 100);
|
|
1087 |
|
|
1088 |
QTest::newRow("Case A (oddeven)") << a << QPointF(55, 55) << false;
|
|
1089 |
a.setFillRule(Qt::WindingFill);
|
|
1090 |
QTest::newRow("Case A (winding)") << a << QPointF(55, 55) << true;
|
|
1091 |
|
|
1092 |
QPainterPath b;
|
|
1093 |
b.arcMoveTo(0, 0, 100, 100, 10);
|
|
1094 |
b.arcTo(0, 0, 100, 100, 10, 360);
|
|
1095 |
b.addRect(50, 50, 100, 100);
|
|
1096 |
|
|
1097 |
QTest::newRow("Case B (oddeven)") << b << QPointF(55, 55) << false;
|
|
1098 |
b.setFillRule(Qt::WindingFill);
|
|
1099 |
QTest::newRow("Case B (winding)") << b << QPointF(55, 55) << false;
|
|
1100 |
|
|
1101 |
QPainterPath c;
|
|
1102 |
c.arcMoveTo(0, 0, 100, 100, 0);
|
|
1103 |
c.arcTo(0, 0, 100, 100, 0, 360);
|
|
1104 |
c.addRect(50, 50, 100, 100);
|
|
1105 |
|
|
1106 |
QTest::newRow("Case C (oddeven)") << c << QPointF(55, 55) << false;
|
|
1107 |
c.setFillRule(Qt::WindingFill);
|
|
1108 |
QTest::newRow("Case C (winding)") << c << QPointF(55, 55) << false;
|
|
1109 |
|
|
1110 |
QPainterPath d;
|
|
1111 |
d.arcMoveTo(0, 0, 100, 100, 10);
|
|
1112 |
d.arcTo(0, 0, 100, 100, 10, -360);
|
|
1113 |
d.addRect(50, 50, 100, 100);
|
|
1114 |
|
|
1115 |
QTest::newRow("Case D (oddeven)") << d << QPointF(55, 55) << false;
|
|
1116 |
d.setFillRule(Qt::WindingFill);
|
|
1117 |
QTest::newRow("Case D (winding)") << d << QPointF(55, 55) << true;
|
|
1118 |
|
|
1119 |
QPainterPath e;
|
|
1120 |
e.arcMoveTo(0, 0, 100, 100, 0);
|
|
1121 |
e.arcTo(0, 0, 100, 100, 0, -360);
|
|
1122 |
e.addRect(50, 50, 100, 100);
|
|
1123 |
|
|
1124 |
QTest::newRow("Case E (oddeven)") << e << QPointF(55, 55) << false;
|
|
1125 |
e.setFillRule(Qt::WindingFill);
|
|
1126 |
QTest::newRow("Case E (winding)") << e << QPointF(55, 55) << true;
|
|
1127 |
}
|
|
1128 |
|
|
1129 |
void tst_QPainterPath::arcWinding()
|
|
1130 |
{
|
|
1131 |
QFETCH(QPainterPath, path);
|
|
1132 |
QFETCH(QPointF, point);
|
|
1133 |
QFETCH(bool, inside);
|
|
1134 |
|
|
1135 |
QCOMPARE(path.contains(point), inside);
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
void tst_QPainterPath::testToFillPolygons()
|
|
1139 |
{
|
|
1140 |
QPainterPath path;
|
|
1141 |
path.lineTo(QPointF(0, 50));
|
|
1142 |
path.lineTo(QPointF(50, 50));
|
|
1143 |
|
|
1144 |
path.moveTo(QPointF(70, 50));
|
|
1145 |
path.lineTo(QPointF(70, 100));
|
|
1146 |
path.lineTo(QPointF(40, 100));
|
|
1147 |
|
|
1148 |
const QList<QPolygonF> polygons = path.toFillPolygons();
|
|
1149 |
QCOMPARE(polygons.size(), 2);
|
|
1150 |
QCOMPARE(polygons.first().count(QPointF(70, 50)), 0);
|
|
1151 |
}
|
|
1152 |
|
|
1153 |
void tst_QPainterPath::connectPathDuplicatePoint()
|
|
1154 |
{
|
|
1155 |
QPainterPath a;
|
|
1156 |
a.moveTo(10, 10);
|
|
1157 |
a.lineTo(20, 20);
|
|
1158 |
|
|
1159 |
QPainterPath b;
|
|
1160 |
b.moveTo(20, 20);
|
|
1161 |
b.lineTo(30, 10);
|
|
1162 |
|
|
1163 |
a.connectPath(b);
|
|
1164 |
|
|
1165 |
QPainterPath c;
|
|
1166 |
c.moveTo(10, 10);
|
|
1167 |
c.lineTo(20, 20);
|
|
1168 |
c.lineTo(30, 10);
|
|
1169 |
|
|
1170 |
QCOMPARE(c, a);
|
|
1171 |
}
|
|
1172 |
|
|
1173 |
void tst_QPainterPath::translate()
|
|
1174 |
{
|
|
1175 |
QPainterPath path;
|
|
1176 |
|
|
1177 |
// Path with no elements.
|
|
1178 |
QCOMPARE(path.currentPosition(), QPointF());
|
|
1179 |
path.translate(50.5, 50.5);
|
|
1180 |
QCOMPARE(path.currentPosition(), QPointF());
|
|
1181 |
QCOMPARE(path.translated(50.5, 50.5).currentPosition(), QPointF());
|
|
1182 |
|
|
1183 |
// path.isEmpty(), but we have one MoveTo element that should be translated.
|
|
1184 |
path.moveTo(50, 50);
|
|
1185 |
QCOMPARE(path.currentPosition(), QPointF(50, 50));
|
|
1186 |
path.translate(99.9, 99.9);
|
|
1187 |
QCOMPARE(path.currentPosition(), QPointF(149.9, 149.9));
|
|
1188 |
path.translate(-99.9, -99.9);
|
|
1189 |
QCOMPARE(path.currentPosition(), QPointF(50, 50));
|
|
1190 |
QCOMPARE(path.translated(-50, -50).currentPosition(), QPointF(0, 0));
|
|
1191 |
|
|
1192 |
// Complex path.
|
|
1193 |
QRegion shape(100, 100, 300, 200, QRegion::Ellipse);
|
|
1194 |
shape -= QRect(225, 175, 50, 50);
|
|
1195 |
QPainterPath complexPath;
|
|
1196 |
complexPath.addRegion(shape);
|
|
1197 |
QVector<QPointF> untranslatedElements;
|
|
1198 |
for (int i = 0; i < complexPath.elementCount(); ++i)
|
|
1199 |
untranslatedElements.append(QPointF(complexPath.elementAt(i)));
|
|
1200 |
|
|
1201 |
const QPainterPath untranslatedComplexPath(complexPath);
|
|
1202 |
const QPointF offset(100, 100);
|
|
1203 |
complexPath.translate(offset);
|
|
1204 |
|
|
1205 |
for (int i = 0; i < complexPath.elementCount(); ++i)
|
|
1206 |
QCOMPARE(QPointF(complexPath.elementAt(i)) - offset, untranslatedElements.at(i));
|
|
1207 |
|
|
1208 |
QCOMPARE(complexPath.translated(-offset), untranslatedComplexPath);
|
|
1209 |
}
|
|
1210 |
|
|
1211 |
QTEST_APPLESS_MAIN(tst_QPainterPath)
|
|
1212 |
|
|
1213 |
#include "tst_qpainterpath.moc"
|