Debian Codegraph @

tests/auto/gui/painting/qpainterpath/tst_qpainterpath.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29
30 #include <QtTest/QtTest>
31
32 #include <qfile.h>
33 #include <qpainterpath.h>
34 #include <qpen.h>
35 #include <qmath.h>
36
37 class tst_QPainterPath : public QObject
38 {
39 Q_OBJECT
40
41 public:
42 public slots:
43 void cleanupTestCase();
44 private slots:
45 void getSetCheck();
46 void clear();
47 void reserveAndCapacity();
48 void swap();
49
50 void contains_QPointF_data();
51 void contains_QPointF();
52
53 void contains_QRectF_data();
54 void contains_QRectF();
55
56 void intersects_QRectF_data();
57 void intersects_QRectF();
58
59 void testContainsAndIntersects_data();
60 void testContainsAndIntersects();
61
62 void testSimplified_data();
63 void testSimplified();
64
65 void testStroker_data();
66 void testStroker();
67
68 void currentPosition();
69
70 void testOperatorEquals();
71 void testOperatorEquals_fuzzy();
72 void testOperatorDatastream();
73
74 void testArcMoveTo_data();
75 void testArcMoveTo();
76 void setElementPositionAt();
77
78 void testOnPath_data();
79 void testOnPath();
80
81 void pointAtPercent_data();
82 void pointAtPercent();
83
84 void angleAtPercent();
85
86 void arcWinding_data();
87 void arcWinding();
88
89 void testToFillPolygons();
90
91 #if QT_CONFIG(signaling_nan)
92 void testNaNandInfinites();
93 #endif
94
95 void closing();
96
97 void operators_data();
98 void operators();
99
100 void connectPathDuplicatePoint();
101 void connectPathMoveTo();
102
103 void translate();
104
105 void lineWithinBounds();
106
107 void intersectionEquality();
108 void intersectionPointOnEdge();
109 };
110
111 void tst_QPainterPath::cleanupTestCase()
112 {
113 QFile::remove(QLatin1String("data"));
114 }
115
116 // Testing get/set functions
117 void tst_QPainterPath::getSetCheck()
118 {
119 QPainterPathStroker obj1;
120 // qreal QPainterPathStroker::width()
121 // void QPainterPathStroker::setWidth(qreal)
122 obj1.setWidth(0.0);
123 QCOMPARE(qreal(1.0), obj1.width()); // Pathstroker sets with to 1 if <= 0
124 obj1.setWidth(0.5);
125 QCOMPARE(qreal(0.5), obj1.width());
126 obj1.setWidth(1.1);
127 QCOMPARE(qreal(1.1), obj1.width());
128
129 // qreal QPainterPathStroker::miterLimit()
130 // void QPainterPathStroker::setMiterLimit(qreal)
131 obj1.setMiterLimit(0.0);
132 QCOMPARE(qreal(0.0), obj1.miterLimit());
133 obj1.setMiterLimit(1.1);
134 QCOMPARE(qreal(1.1), obj1.miterLimit());
135
136 // qreal QPainterPathStroker::curveThreshold()
137 // void QPainterPathStroker::setCurveThreshold(qreal)
138 obj1.setCurveThreshold(0.0);
139 QCOMPARE(qreal(0.0), obj1.curveThreshold());
140 obj1.setCurveThreshold(1.1);
141 QCOMPARE(qreal(1.1), obj1.curveThreshold());
142 }
143
144 void tst_QPainterPath::swap()
145 {
146 QPainterPath p1;
147 p1.addRect( 0, 0,10,10);
148 QPainterPath p2;
149 p2.addRect(10,10,10,10);
150 p1.swap(p2);
151 QCOMPARE(p1.boundingRect().toRect(), QRect(10,10,10,10));
152 QCOMPARE(p2.boundingRect().toRect(), QRect( 0, 0,10,10));
153 }
154
155 void tst_QPainterPath::clear()
156 {
157 QPainterPath p1;
158 QPainterPath p2;
159 p1.clear();
160 QCOMPARE(p1, p2);
161
162 p1.addRect(0, 0, 10, 10);
163 p1.clear();
164 QCOMPARE(p1, p2);
165
166 p1.lineTo(50, 50);
167 QPainterPath p3;
168 QCOMPARE(p1.elementCount(), 2);
169 p3.lineTo(50, 50);
170 QCOMPARE(p1, p3);
171
172 QCOMPARE(p1.fillRule(), Qt::OddEvenFill);
173 p1.setFillRule(Qt::WindingFill);
174 QVERIFY(p1 != p3);
175 p1.clear();
176 QVERIFY(p1 != p3);
177 p1.setFillRule(Qt::OddEvenFill);
178 QCOMPARE(p1, p2);
179
180 QPainterPath p4;
181 QCOMPARE(p4.fillRule(), Qt::OddEvenFill);
182 p4.setFillRule(Qt::WindingFill);
183 QCOMPARE(p4.fillRule(), Qt::WindingFill);
184 p4.clear();
185 QCOMPARE(p4.fillRule(), Qt::WindingFill);
186 p4 = QPainterPath();
187 QCOMPARE(p4.fillRule(), Qt::OddEvenFill);
188 }
189
190 void tst_QPainterPath::reserveAndCapacity()
191 {
192 QPainterPath p;
193 QVERIFY(p.capacity() == 0);
194
195 p.addRect(0, 0, 10, 10);
196 QVERIFY(p.capacity() > 0);
197
198 p.clear();
199 QVERIFY(p.capacity() > 0);
200
201 p = QPainterPath{};
202 QVERIFY(p.capacity() == 0);
203
204 p.moveTo(100, 100);
205 QVERIFY(p.capacity() > 1);
206
207 p.reserve(1000);
208 QVERIFY(p.capacity() >= 1000);
209
210 p.reserve(0);
211 QVERIFY(p.capacity() >= 1000);
212
213 QPainterPath p2;
214 p2.reserve(10);
215 QVERIFY(p.capacity() >= 10);
216 }
217
218 Q_DECLARE_METATYPE(QPainterPath)
219
220 void tst_QPainterPath::currentPosition()
221 {
222 QPainterPath p;
223
224 QCOMPARE(p.currentPosition(), QPointF());
225
226 p.moveTo(100, 100);
227 QCOMPARE(p.currentPosition(), QPointF(100, 100));
228
229 p.lineTo(200, 200);
230 QCOMPARE(p.currentPosition(), QPointF(200, 200));
231
232 p.cubicTo(300, 200, 200, 300, 500, 500);
233 QCOMPARE(p.currentPosition(), QPointF(500, 500));
234 }
235
236 void tst_QPainterPath::contains_QPointF_data()
237 {
238 QTest::addColumn<QPainterPath>("path");
239 QTest::addColumn<QPointF>("pt");
240 QTest::addColumn<bool>("contained");
241
242 QPainterPath path;
243 path.addRect(0, 0, 100, 100);
244
245 // #####
246 // # #
247 // # #
248 // # #
249 // #####
250
251 QTest::newRow("[0,0] in [0,0,100,100]") << path << QPointF(0, 0) << true;
252
253 QTest::newRow("[99,0] in [0,0,100,100]") << path << QPointF(99, 0) << true;
254 QTest::newRow("[0,99] in [0,0,100,100]") << path << QPointF(0, 99) << true;
255 QTest::newRow("[99,99] in [0,0,100,100]") << path << QPointF(99, 99) << true;
256
257 QTest::newRow("[99.99,0] in [0,0,100,100]") << path << QPointF(99.99, 0) << true;
258 QTest::newRow("[0,99.99] in [0,0,100,100]") << path << QPointF(0, 99.99) << true;
259 QTest::newRow("[99.99,99.99] in [0,0,100,100]") << path << QPointF(99.99, 99.99) << true;
260
261 QTest::newRow("[0.01,0.01] in [0,0,100,100]") << path << QPointF(0.01, 0.01) << true;
262 QTest::newRow("[0,0.01] in [0,0,100,100]") << path << QPointF(0, 0.01) << true;
263 QTest::newRow("[0.01,0] in [0,0,100,100]") << path << QPointF(0.01, 0) << true;
264
265 QTest::newRow("[-0.01,-0.01] in [0,0,100,100]") << path << QPointF(-0.01, -0.01) << false;
266 QTest::newRow("[-0,-0.01] in [0,0,100,100]") << path << QPointF(0, -0.01) << false;
267 QTest::newRow("[-0.01,0] in [0,0,100,100]") << path << QPointF(-0.01, 0) << false;
268
269
270 QTest::newRow("[-10,0] in [0,0,100,100]") << path << QPointF(-10, 0) << false;
271 QTest::newRow("[100,0] in [0,0,100,100]") << path << QPointF(100, 0) << false;
272
273 QTest::newRow("[0,-10] in [0,0,100,100]") << path << QPointF(0, -10) << false;
274 QTest::newRow("[0,100] in [0,0,100,100]") << path << QPointF(0, 100) << false;
275
276 QTest::newRow("[100.1,0] in [0,0,100,100]") << path << QPointF(100.1, 0) << false;
277 QTest::newRow("[0,100.1] in [0,0,100,100]") << path << QPointF(0, 100.1) << false;
278
279 path.addRect(50, 50, 100, 100);
280
281 // #####
282 // # #
283 // # #####
284 // # # # #
285 // ##### #
286 // # #
287 // #####
288
289 QTest::newRow("[49,49] in 2 rects") << path << QPointF(49,49) << true;
290 QTest::newRow("[50,50] in 2 rects") << path << QPointF(50,50) << false;
291 QTest::newRow("[100,100] in 2 rects") << path << QPointF(100,100) << true;
292
293 path.setFillRule(Qt::WindingFill);
294 QTest::newRow("[50,50] in 2 rects (winding)") << path << QPointF(50,50) << true;
295
296 path.addEllipse(0, 0, 150, 150);
297
298 // #####
299 // ## ##
300 // # #####
301 // # # # #
302 // ##### #
303 // ## ##
304 // #####
305
306 QTest::newRow("[50,50] in complex (winding)") << path << QPointF(50, 50) << true;
307
308 path.setFillRule(Qt::OddEvenFill);
309 QTest::newRow("[50,50] in complex (windinf)") << path << QPointF(50, 50) << true;
310 QTest::newRow("[49,49] in complex") << path << QPointF(49,49) << false;
311 QTest::newRow("[100,100] in complex") << path << QPointF(49,49) << false;
312
313
314 // unclosed triangle
315 path = QPainterPath();
316 path.moveTo(100, 100);
317 path.lineTo(130, 70);
318 path.lineTo(150, 110);
319
320 QTest::newRow("[100,100] in triangle") << path << QPointF(100, 100) << true;
321 QTest::newRow("[140,100] in triangle") << path << QPointF(140, 100) << true;
322 QTest::newRow("[130,80] in triangle") << path << QPointF(130, 80) << true;
323
324 QTest::newRow("[110,80] in triangle") << path << QPointF(110, 80) << false;
325 QTest::newRow("[150,100] in triangle") << path << QPointF(150, 100) << false;
326 QTest::newRow("[120,110] in triangle") << path << QPointF(120, 110) << false;
327
328 QRectF base_rect(0, 0, 20, 20);
329
330 path = QPainterPath();
331 path.addEllipse(base_rect);
332
333 // not strictly precise, but good enougth to verify fair precision.
334 QPainterPath inside;
335 inside.addEllipse(base_rect.adjusted(5, 5, -5, -5));
336 QPolygonF inside_poly = inside.toFillPolygon();
337 for (int i=0; i<inside_poly.size(); ++i)
338 QTest::newRow(("inside_ellipse " + QByteArray::number(i)).constData()) << path << inside_poly.at(i) << true;
339
340 QPainterPath outside;
341 outside.addEllipse(base_rect.adjusted(-5, -5, 5, 5));
342 QPolygonF outside_poly = outside.toFillPolygon();
343 for (int i=0; i<outside_poly.size(); ++i)
344 QTest::newRow(("outside_ellipse " + QByteArray::number(i)).constData()) << path << outside_poly.at(i) << false;
345
346 path = QPainterPath();
347 base_rect = QRectF(50, 50, 200, 200);
348 path.addEllipse(base_rect);
349 path.setFillRule(Qt::WindingFill);
350
351 QTest::newRow("topleft outside ellipse") << path << base_rect.topLeft() << false;
352 QTest::newRow("topright outside ellipse") << path << base_rect.topRight() << false;
353 QTest::newRow("bottomright outside ellipse") << path << base_rect.bottomRight() << false;
354 QTest::newRow("bottomleft outside ellipse") << path << base_rect.bottomLeft() << false;
355
356 // Test horizontal curve segment
357 path = QPainterPath();
358 path.moveTo(100, 100);
359 path.cubicTo(120, 100, 180, 100, 200, 100);
360 path.lineTo(150, 200);
361 path.closeSubpath();
362
363 QTest::newRow("horizontal cubic, out left") << path << QPointF(0, 100) << false;
364 QTest::newRow("horizontal cubic, out right") << path << QPointF(300, 100) <<false;
365 QTest::newRow("horizontal cubic, in mid") << path << QPointF(150, 100) << true;
366
367 path = QPainterPath();
368 path.addEllipse(QRectF(-5000.0, -5000.0, 1500000.0, 1500000.0));
369 QTest::newRow("huge ellipse, qreal=float crash") << path << QPointF(1100000.35, 1098000.2) << true;
370
371 }
372
373 void tst_QPainterPath::contains_QPointF()
374 {
375 QFETCH(QPainterPath, path);
376 QFETCH(QPointF, pt);
377 QFETCH(bool, contained);
378
379 QCOMPARE(path.contains(pt), contained);
380 }
381
382 void tst_QPainterPath::contains_QRectF_data()
383 {
384 QTest::addColumn<QPainterPath>("path");
385 QTest::addColumn<QRectF>("rect");
386 QTest::addColumn<bool>("contained");
387
388 QPainterPath path;
389 path.addRect(0, 0, 100, 100);
390
391 QTest::newRow("same rect") << path << QRectF(0.1, 0.1, 99, 99) << true; // ###
392 QTest::newRow("outside") << path << QRectF(-1, -1, 100, 100) << false;
393 QTest::newRow("covers") << path << QRectF(-1, -1, 102, 102) << false;
394 QTest::newRow("left") << path << QRectF(-10, 50, 5, 5) << false;
395 QTest::newRow("top") << path << QRectF(50, -10, 5, 5) << false;
396 QTest::newRow("right") << path << QRectF(110, 50, 5, 5) << false;
397 QTest::newRow("bottom") << path << QRectF(50, 110, 5, 5) << false;
398
399 path.addRect(50, 50, 100, 100);
400
401 QTest::newRow("r1 top") << path << QRectF(0.1, 0.1, 99, 49) << true;
402 QTest::newRow("r1 left") << path << QRectF(0.1, 0.1, 49, 99) << true;
403 QTest::newRow("r2 right") << path << QRectF(100.01, 50.1, 49, 99) << true;
404 QTest::newRow("r2 bottom") << path << QRectF(50.1, 100.1, 99, 49) << true;
405 QTest::newRow("inside 2 rects") << path << QRectF(51, 51, 48, 48) << false;
406 QTest::newRow("topRight 2 rects") << path << QRectF(100, 0, 49, 49) << false;
407 QTest::newRow("bottomLeft 2 rects") << path << QRectF(0, 100, 49, 49) << false;
408
409 path.setFillRule(Qt::WindingFill);
410 QTest::newRow("inside 2 rects (winding)") << path << QRectF(51, 51, 48, 48) << true;
411
412 path.addEllipse(0, 0, 150, 150);
413 QTest::newRow("topRight 2 rects") << path << QRectF(100, 25, 24, 24) << true;
414 QTest::newRow("bottomLeft 2 rects") << path << QRectF(25, 100, 24, 24) << true;
415
416 path.setFillRule(Qt::OddEvenFill);
417 QTest::newRow("inside 2 rects") << path << QRectF(50, 50, 49, 49) << false;
418 }
419
420 void tst_QPainterPath::contains_QRectF()
421 {
422 QFETCH(QPainterPath, path);
423 QFETCH(QRectF, rect);
424 QFETCH(bool, contained);
425
426 QCOMPARE(path.contains(rect), contained);
427 }
428
429 static inline QPainterPath rectPath(qreal x, qreal y, qreal w, qreal h)
430 {
431 QPainterPath path;
432 path.addRect(x, y, w, h);
433 path.closeSubpath();
434 return path;
435 }
436
437 static inline QPainterPath ellipsePath(qreal x, qreal y, qreal w, qreal h)
438 {
439 QPainterPath path;
440 path.addEllipse(x, y, w, h);
441 path.closeSubpath();
442 return path;
443 }
444
445 static inline QPainterPath linePath(qreal x1, qreal y1, qreal x2, qreal y2)
446 {
447 QPainterPath path;
448 path.moveTo(x1, y1);
449 path.lineTo(x2, y2);
450 return path;
451 }
452
453 void tst_QPainterPath::intersects_QRectF_data()
454 {
455 QTest::addColumn<QPainterPath>("path");
456 QTest::addColumn<QRectF>("rect");
457 QTest::addColumn<bool>("intersects");
458
459 QPainterPath path;
460 path.addRect(0, 0, 100, 100);
461
462 QTest::newRow("same rect") << path << QRectF(0.1, 0.1, 99, 99) << true; // ###
463 QTest::newRow("outside") << path << QRectF(-1, -1, 100, 100) << true;
464 QTest::newRow("covers") << path << QRectF(-1, -1, 102, 102) << true;
465 QTest::newRow("left") << path << QRectF(-10, 50, 5, 5) << false;
466 QTest::newRow("top") << path << QRectF(50, -10, 5, 5) << false;
467 QTest::newRow("right") << path << QRectF(110, 50, 5, 5) << false;
468 QTest::newRow("bottom") << path << QRectF(50, 110, 5, 5) << false;
469
470 path.addRect(50, 50, 100, 100);
471
472 QTest::newRow("r1 top") << path << QRectF(0.1, 0.1, 99, 49) << true;
473 QTest::newRow("r1 left") << path << QRectF(0.1, 0.1, 49, 99) << true;
474 QTest::newRow("r2 right") << path << QRectF(100.01, 50.1, 49, 99) << true;
475 QTest::newRow("r2 bottom") << path << QRectF(50.1, 100.1, 99, 49) << true;
476 QTest::newRow("inside 2 rects") << path << QRectF(51, 51, 48, 48) << false;
477
478 path.setFillRule(Qt::WindingFill);
479 QTest::newRow("inside 2 rects (winding)") << path << QRectF(51, 51, 48, 48) << true;
480
481 path.addEllipse(0, 0, 150, 150);
482 QTest::newRow("topRight 2 rects") << path << QRectF(100, 25, 24, 24) << true;
483 QTest::newRow("bottomLeft 2 rects") << path << QRectF(25, 100, 24, 24) << true;
484
485 QTest::newRow("horizontal line") << linePath(0, 0, 10, 0) << QRectF(1, -1, 2, 2) << true;
486 QTest::newRow("vertical line") << linePath(0, 0, 0, 10) << QRectF(-1, 1, 2, 2) << true;
487
488 path = QPainterPath();
489 path.addEllipse(QRectF(-5000.0, -5000.0, 1500000.0, 1500000.0));
490 QTest::newRow("huge ellipse, qreal=float crash") << path << QRectF(1100000.35, 1098000.2, 1500000.0, 1500000.0) << true;
491 }
492
493 void tst_QPainterPath::intersects_QRectF()
494 {
495 QFETCH(QPainterPath, path);
496 QFETCH(QRectF, rect);
497 QFETCH(bool, intersects);
498
499 QCOMPARE(path.intersects(rect), intersects);
500 }
501
502
503 void tst_QPainterPath::testContainsAndIntersects_data()
504 {
505 QTest::addColumn<QPainterPath>("path");
506 QTest::addColumn<QPainterPath>("candidate");
507 QTest::addColumn<bool>("contained");
508 QTest::addColumn<bool>("intersects");
509
510 QTest::newRow("rect vs small ellipse (upper left)") << rectPath(0, 0, 100, 100) << ellipsePath(0, 0, 50, 50) << false << true;
511 QTest::newRow("rect vs small ellipse (upper right)") << rectPath(0, 0, 100, 100) << ellipsePath(50, 0, 50, 50) << false << true;
512 QTest::newRow("rect vs small ellipse (lower right)") << rectPath(0, 0, 100, 100) << ellipsePath(50, 50, 50, 50) << false << true;
513 QTest::newRow("rect vs small ellipse (lower left)") << rectPath(0, 0, 100, 100) << ellipsePath(0, 50, 50, 50) << false << true;
514 QTest::newRow("rect vs small ellipse (centered)") << rectPath(0, 0, 100, 100) << ellipsePath(25, 25, 50, 50) << true << true;
515 QTest::newRow("rect vs equal ellipse") << rectPath(0, 0, 100, 100) << ellipsePath(0, 0, 100, 100) << false << true;
516 QTest::newRow("rect vs big ellipse") << rectPath(0, 0, 100, 100) << ellipsePath(-10, -10, 120, 120) << false << true;
517
518 QPainterPath twoEllipses = ellipsePath(0, 0, 100, 100).united(ellipsePath(200, 0, 100, 100));
519
520 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;
521 QTest::newRow("rect vs two equal ellipses") << rectPath(0, 0, 100, 100) << twoEllipses << false << true;
522
523 QTest::newRow("rect vs self") << rectPath(0, 0, 100, 100) << rectPath(0, 0, 100, 100) << false << true;
524 QTest::newRow("ellipse vs self") << ellipsePath(0, 0, 100, 100) << ellipsePath(0, 0, 100, 100) << false << true;
525
526 QPainterPath twoRects = rectPath(0, 0, 100, 100).united(rectPath(200, 0, 100, 100));
527 QTest::newRow("two rects vs small ellipse (upper left)") << twoRects << ellipsePath(0, 0, 50, 50) << false << true;
528 QTest::newRow("two rects vs small ellipse (upper right)") << twoRects << ellipsePath(50, 0, 50, 50) << false << true;
529 QTest::newRow("two rects vs small ellipse (lower right)") << twoRects << ellipsePath(50, 50, 50, 50) << false << true;
530 QTest::newRow("two rects vs small ellipse (lower left)") << twoRects << ellipsePath(0, 50, 50, 50) << false << true;
531 QTest::newRow("two rects vs small ellipse (centered)") << twoRects << ellipsePath(25, 25, 50, 50) << true << true;
532 QTest::newRow("two rects vs equal ellipse") << twoRects << ellipsePath(0, 0, 100, 100) << false << true;
533 QTest::newRow("two rects vs big ellipse") << twoRects << ellipsePath(-10, -10, 120, 120) << false << true;
534
535 QTest::newRow("two rects vs two small ellipses") << twoRects << ellipsePath(25, 25, 50, 50).united(ellipsePath(225, 25, 50, 50)) << true << true;
536 QTest::newRow("two rects vs two equal ellipses") << twoRects << ellipsePath(0, 0, 100, 100).united(ellipsePath(200, 0, 100, 100)) << false << true;
537
538 QTest::newRow("two rects vs self") << twoRects << twoRects << false << true;
539 QTest::newRow("two ellipses vs self") << twoEllipses << twoEllipses << false << true;
540
541 QPainterPath windingRect = rectPath(0, 0, 100, 100);
542 windingRect.addRect(25, 25, 100, 50);
543 windingRect.setFillRule(Qt::WindingFill);
544
545 QTest::newRow("rect with winding rule vs tall rect") << windingRect << rectPath(40, 20, 20, 60) << true << true;
546 QTest::newRow("rect with winding rule vs self") << windingRect << windingRect << false << true;
547
548 QPainterPath thickFrame = rectPath(0, 0, 100, 100).subtracted(rectPath(25, 25, 50, 50));
549 QPainterPath thinFrame = rectPath(10, 10, 80, 80).subtracted(rectPath(15, 15, 70, 70));
550
551 QTest::newRow("thin frame in thick frame") << thickFrame << thinFrame << true << true;
552 QTest::newRow("rect in thick frame") << thickFrame << rectPath(40, 40, 20, 20) << false << false;
553 QTest::newRow("rect in thin frame") << thinFrame << rectPath(40, 40, 20, 20) << false << false;
554
555 QPainterPath ellipses;
556 ellipses.addEllipse(0, 0, 10, 10);
557 ellipses.addEllipse(4, 4, 2, 2);
558 ellipses.setFillRule(Qt::WindingFill);
559
560 // the definition of QPainterPath::intersects() and contains() is fill-area based,
561 QTest::newRow("line in rect") << rectPath(0, 0, 100, 100) << linePath(10, 10, 90, 90) << true << true;
562 QTest::newRow("horizontal line in rect") << rectPath(0, 0, 100, 100) << linePath(10, 50, 90, 50) << true << true;
563 QTest::newRow("vertical line in rect") << rectPath(0, 0, 100, 100) << linePath(50, 10, 50, 90) << true << true;
564
565 QTest::newRow("line through rect") << rectPath(0, 0, 100, 100) << linePath(-10, -10, 110, 110) << false << true;
566 QTest::newRow("line through rect 2") << rectPath(0, 0, 100, 100) << linePath(-10, 0, 110, 100) << false << true;
567 QTest::newRow("line through rect 3") << rectPath(0, 0, 100, 100) << linePath(5, 10, 110, 100) << false << true;
568 QTest::newRow("line through rect 4") << rectPath(0, 0, 100, 100) << linePath(-10, 0, 90, 90) << false << true;
569
570 QTest::newRow("horizontal line through rect") << rectPath(0, 0, 100, 100) << linePath(-10, 50, 110, 50) << false << true;
571 QTest::newRow("vertical line through rect") << rectPath(0, 0, 100, 100) << linePath(50, -10, 50, 110) << false << true;
572
573 QTest::newRow("line vs line") << linePath(0, 0, 10, 10) << linePath(10, 0, 0, 10) << false << true;
574
575 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;
576 QTest::newRow("line in ellipse") << ellipses << linePath(3, 5, 7, 5) << false << true;
577 QTest::newRow("line in ellipse 2") << ellipses << linePath(4.5, 5, 5.5, 5) << true << true;
578
579 QTest::newRow("winding ellipse") << ellipses << ellipsePath(4, 4, 2, 2) << false << true;
580 QTest::newRow("winding ellipse 2") << ellipses << ellipsePath(4.5, 4.5, 1, 1) << true << true;
581 ellipses.setFillRule(Qt::OddEvenFill);
582 QTest::newRow("odd even ellipse") << ellipses << ellipsePath(4, 4, 2, 2) << false << true;
583 QTest::newRow("odd even ellipse 2") << ellipses << ellipsePath(4.5, 4.5, 1, 1) << false << false;
584 }
585
586 void tst_QPainterPath::testContainsAndIntersects()
587 {
588 QFETCH(QPainterPath, path);
589 QFETCH(QPainterPath, candidate);
590 QFETCH(bool, contained);
591 QFETCH(bool, intersects);
592
593 QCOMPARE(path.intersects(candidate), intersects);
594 QCOMPARE(path.contains(candidate), contained);
595 }
596
597 void tst_QPainterPath::testSimplified_data()
598 {
599 QTest::addColumn<QPainterPath>("path");
600 QTest::addColumn<int>("elements");
601
602 QTest::newRow("rect") << rectPath(0, 0, 10, 10) << 5;
603
604 QPainterPath twoRects = rectPath(0, 0, 10, 10);
605 twoRects.addPath(rectPath(5, 0, 10, 10));
606 QTest::newRow("two rects (odd)") << twoRects << 10;
607
608 twoRects.setFillRule(Qt::WindingFill);
609 QTest::newRow("two rects (winding)") << twoRects << 5;
610
611 QPainterPath threeSteps = rectPath(0, 0, 10, 10);
612 threeSteps.addPath(rectPath(0, 10, 20, 10));
613 threeSteps.addPath(rectPath(0, 20, 30, 10));
614
615 QTest::newRow("three rects (steps)") << threeSteps << 9;
616 }
617
618 void tst_QPainterPath::testSimplified()
619 {
620 QFETCH(QPainterPath, path);
621 QFETCH(int, elements);
622
623 QPainterPath simplified = path.simplified();
624
625 QCOMPARE(simplified.elementCount(), elements);
626
627 QVERIFY(simplified.subtracted(path).isEmpty());
628 QVERIFY(path.subtracted(simplified).isEmpty());
629 }
630
631 void tst_QPainterPath::testStroker_data()
632 {
633 QTest::addColumn<QPainterPath>("path");
634 QTest::addColumn<QPen>("pen");
635 QTest::addColumn<QPainterPath>("stroke");
636
637 QTest::newRow("line 1") << linePath(2, 2, 10, 2) << QPen(Qt::black, 2, Qt::SolidLine, Qt::FlatCap) << rectPath(2, 1, 8, 2);
638 QTest::newRow("line 2") << linePath(2, 2, 10, 2) << QPen(Qt::black, 2, Qt::SolidLine, Qt::SquareCap) << rectPath(1, 1, 10, 2);
639
640 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));
641
642 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));
643 }
644
645 void tst_QPainterPath::testStroker()
646 {
647 QFETCH(QPainterPath, path);
648 QFETCH(QPen, pen);
649 QFETCH(QPainterPath, stroke);
650
651 QPainterPathStroker stroker;
652 stroker.setWidth(pen.widthF());
653 stroker.setCapStyle(pen.capStyle());
654 stroker.setJoinStyle(pen.joinStyle());
655 stroker.setMiterLimit(pen.miterLimit());
656 stroker.setDashPattern(pen.style());
657 stroker.setDashOffset(pen.dashOffset());
658
659 QPainterPath result = stroker.createStroke(path);
660
661 // check if stroke == result
662 QVERIFY(result.subtracted(stroke).isEmpty());
663 QVERIFY(stroke.subtracted(result).isEmpty());
664 }
665
666 void tst_QPainterPath::testOperatorEquals()
667 {
668 QPainterPath empty1;
669 QPainterPath empty2;
670 QCOMPARE(empty1, empty2);
671
672 QPainterPath rect1;
673 rect1.addRect(100, 100, 100, 100);
674 QCOMPARE(rect1, rect1);
675 QVERIFY(rect1 != empty1);
676
677 QPainterPath rect2;
678 rect2.addRect(100, 100, 100, 100);
679 QCOMPARE(rect1, rect2);
680
681 rect2.setFillRule(Qt::WindingFill);
682 QVERIFY(rect1 != rect2);
683
684 QPainterPath ellipse1;
685 ellipse1.addEllipse(50, 50, 100, 100);
686 QVERIFY(rect1 != ellipse1);
687
688 QPainterPath ellipse2;
689 ellipse2.addEllipse(50, 50, 100, 100);
690 QCOMPARE(ellipse1, ellipse2);
691 }
692
693 void tst_QPainterPath::testOperatorEquals_fuzzy()
694 {
695 // if operator== returns true for two paths it should
696 // also return true when the same transform is applied to both paths
697 {
698 QRectF a(100, 100, 100, 50);
699 QRectF b = a.translated(1e-14, 1e-14);
700
701 QPainterPath pa;
702 pa.addRect(a);
703 QPainterPath pb;
704 pb.addRect(b);
705
706 QCOMPARE(pa, pb);
707
708 QTransform transform;
709 transform.translate(-100, -100);
710
711 QCOMPARE(transform.map(pa), transform.map(pb));
712 }
713
714 // higher tolerance for error when path's bounding rect is big
715 {
716 QRectF a(1, 1, 1e6, 0.5e6);
717 QRectF b = a.translated(1e-7, 1e-7);
718
719 QPainterPath pa;
720 pa.addRect(a);
721 QPainterPath pb;
722 pb.addRect(b);
723
724 QCOMPARE(pa, pb);
725
726 QTransform transform;
727 transform.translate(-1, -1);
728
729 QCOMPARE(transform.map(pa), transform.map(pb));
730 }
731
732 // operator== should return true for a path that has
733 // been transformed and then inverse transformed
734 {
735 QPainterPath a;
736 a.addRect(0, 0, 100, 100);
737
738 QTransform transform;
739 transform.translate(100, 20);
740 transform.scale(1.5, 1.5);
741
742 QPainterPath b = transform.inverted().map(transform.map(a));
743
744 QCOMPARE(a, b);
745 }
746
747 {
748 QPainterPath a;
749 a.lineTo(10, 0);
750 a.lineTo(10, 10);
751 a.lineTo(0, 10);
752
753 QPainterPath b;
754 b.lineTo(10, 0);
755 b.moveTo(10, 10);
756 b.lineTo(0, 10);
757
758 QVERIFY(a != b);
759 }
760 }
761
762 void tst_QPainterPath::testOperatorDatastream()
763 {
764 QPainterPath path;
765 path.addEllipse(0, 0, 100, 100);
766 path.addRect(0, 0, 100, 100);
767 path.setFillRule(Qt::WindingFill);
768
769 QTemporaryDir tempDir(QDir::tempPath() + "/tst_qpainterpath.XXXXXX");
770 QVERIFY2(tempDir.isValid(), qPrintable(tempDir.errorString()));
771 // Write out
772 {
773 QFile data(tempDir.path() + "/data");
774 bool ok = data.open(QFile::WriteOnly);
775 QVERIFY(ok);
776 QDataStream stream(&data);
777 stream << path;
778 }
779
780 QPainterPath other;
781 // Read in
782 {
783 QFile data(tempDir.path() + "/data");
784 bool ok = data.open(QFile::ReadOnly);
785 QVERIFY(ok);
786 QDataStream stream(&data);
787 stream >> other;
788 }
789
790 QCOMPARE(other, path);
791
792 // Check reset & detach
793 QPainterPath p3;
794 p3.lineTo(1, 1);
795 QCOMPARE(p3.elementCount(), 2);
796 QPainterPath p4 = p3;
797 QCOMPARE(p4.elementCount(), 2);
798 {
799 QFile data(tempDir.path() + "/data");
800 QVERIFY(data.open(QFile::ReadOnly));
801 QDataStream stream(&data);
802 stream >> p3;
803 }
804 QCOMPARE(p3.elementCount(), path.elementCount());
805 QCOMPARE(p4.elementCount(), 2);
806 }
807
808 void tst_QPainterPath::closing()
809 {
810 // lineto's
811 {
812 QPainterPath triangle(QPoint(100, 100));
813
814 triangle.lineTo(200, 100);
815 triangle.lineTo(200, 200);
816 QCOMPARE(triangle.elementCount(), 3);
817
818 //add this line to make sure closeSubpath() also calls detach() and detached properly
819 QPainterPath copied = triangle;
820 triangle.closeSubpath();
821 QCOMPARE(copied.elementCount(), 3);
822
823 QCOMPARE(triangle.elementCount(), 4);
824 QCOMPARE(triangle.elementAt(3).type, QPainterPath::LineToElement);
825
826 triangle.moveTo(300, 300);
827 QCOMPARE(triangle.elementCount(), 5);
828 QCOMPARE(triangle.elementAt(4).type, QPainterPath::MoveToElement);
829
830 triangle.lineTo(400, 300);
831 triangle.lineTo(400, 400);
832 QCOMPARE(triangle.elementCount(), 7);
833
834 triangle.closeSubpath();
835 QCOMPARE(triangle.elementCount(), 8);
836
837 // this will should trigger implicit moveto...
838 triangle.lineTo(600, 300);
839 QCOMPARE(triangle.elementCount(), 10);
840 QCOMPARE(triangle.elementAt(8).type, QPainterPath::MoveToElement);
841 QCOMPARE(triangle.elementAt(9).type, QPainterPath::LineToElement);
842
843 triangle.lineTo(600, 700);
844 QCOMPARE(triangle.elementCount(), 11);
845 }
846
847 // curveto's
848 {
849 QPainterPath curves(QPoint(100, 100));
850
851 curves.cubicTo(200, 100, 100, 200, 200, 200);
852 QCOMPARE(curves.elementCount(), 4);
853
854 curves.closeSubpath();
855 QCOMPARE(curves.elementCount(), 5);
856 QCOMPARE(curves.elementAt(4).type, QPainterPath::LineToElement);
857
858 curves.moveTo(300, 300);
859 QCOMPARE(curves.elementCount(), 6);
860 QCOMPARE(curves.elementAt(5).type, QPainterPath::MoveToElement);
861
862 curves.cubicTo(400, 300, 300, 400, 400, 400);
863 QCOMPARE(curves.elementCount(), 9);
864
865 curves.closeSubpath();
866 QCOMPARE(curves.elementCount(), 10);
867
868 // should trigger implicit moveto..
869 curves.cubicTo(100, 800, 800, 100, 800, 800);
870 QCOMPARE(curves.elementCount(), 14);
871 QCOMPARE(curves.elementAt(10).type, QPainterPath::MoveToElement);
872 QCOMPARE(curves.elementAt(11).type, QPainterPath::CurveToElement);
873 }
874
875 {
876 QPainterPath rects;
877 rects.addRect(100, 100, 100, 100);
878
879 QCOMPARE(rects.elementCount(), 5);
880 QCOMPARE(rects.elementAt(0).type, QPainterPath::MoveToElement);
881 QCOMPARE(rects.elementAt(4).type, QPainterPath::LineToElement);
882
883 rects.addRect(300, 100, 100,100);
884 QCOMPARE(rects.elementCount(), 10);
885 QCOMPARE(rects.elementAt(5).type, QPainterPath::MoveToElement);
886 QCOMPARE(rects.elementAt(9).type, QPainterPath::LineToElement);
887
888 rects.lineTo(0, 0);
889 QCOMPARE(rects.elementCount(), 12);
890 QCOMPARE(rects.elementAt(10).type, QPainterPath::MoveToElement);
891 QCOMPARE(rects.elementAt(11).type, QPainterPath::LineToElement);
892 }
893
894 {
895 QPainterPath ellipses;
896 ellipses.addEllipse(100, 100, 100, 100);
897
898 QCOMPARE(ellipses.elementCount(), 13);
899 QCOMPARE(ellipses.elementAt(0).type, QPainterPath::MoveToElement);
900 QCOMPARE(ellipses.elementAt(10).type, QPainterPath::CurveToElement);
901
902 ellipses.addEllipse(300, 100, 100,100);
903 QCOMPARE(ellipses.elementCount(), 26);
904 QCOMPARE(ellipses.elementAt(13).type, QPainterPath::MoveToElement);
905 QCOMPARE(ellipses.elementAt(23).type, QPainterPath::CurveToElement);
906
907 ellipses.lineTo(0, 0);
908 QCOMPARE(ellipses.elementCount(), 28);
909 QCOMPARE(ellipses.elementAt(26).type, QPainterPath::MoveToElement);
910 QCOMPARE(ellipses.elementAt(27).type, QPainterPath::LineToElement);
911 }
912
913 {
914 QPainterPath path;
915 path.moveTo(10, 10);
916 path.lineTo(40, 10);
917 path.lineTo(25, 20);
918 path.lineTo(10 + 1e-13, 10 + 1e-13);
919 QCOMPARE(path.elementCount(), 4);
920 path.closeSubpath();
921 QCOMPARE(path.elementCount(), 4);
922 }
923 }
924
925 void tst_QPainterPath::testArcMoveTo_data()
926 {
927 QTest::addColumn<QRectF>("rect");
928 QTest::addColumn<qreal>("angle");
929
930 static Q_CONSTEXPR QRectF rects[] = {
931 QRectF(100, 100, 100, 100),
932 QRectF(100, 100, -100, 100),
933 QRectF(100, 100, 100, -100),
934 QRectF(100, 100, -100, -100),
935 };
936
937 for (uint domain = 0; domain < sizeof rects / sizeof *rects; ++domain) {
938 const QByteArray dB = QByteArray::number(domain);
939 for (int i=-360; i<=360; ++i) {
940 QTest::newRow(("test " + dB + ' ' + QByteArray::number(i)).constData())
941 << rects[domain] << (qreal) i;
942 }
943
944 // test low angles
945 QTest::newRow("low angles 1") << rects[domain] << (qreal) 1e-10;
946 QTest::newRow("low angles 2") << rects[domain] << (qreal)-1e-10;
947 }
948 }
949
950 void tst_QPainterPath::operators_data()
951 {
952 QTest::addColumn<QPainterPath>("test");
953 QTest::addColumn<QPainterPath>("expected");
954
955 QPainterPath a;
956 QPainterPath b;
957 a.addRect(0, 0, 100, 100);
958 b.addRect(50, 50, 100, 100);
959
960 QTest::newRow("a & b") << (a & b) << a.intersected(b);
961 QTest::newRow("a | b") << (a | b) << a.united(b);
962 QTest::newRow("a + b") << (a + b) << a.united(b);
963 QTest::newRow("a - b") << (a - b) << a.subtracted(b);
964
965 QPainterPath c = a;
966 QTest::newRow("a &= b") << (a &= b) << a.intersected(b);
967 c = a;
968 QTest::newRow("a |= b") << (a |= b) << a.united(b);
969 c = a;
970 QTest::newRow("a += b") << (a += b) << a.united(b);
971 c = a;
972 QTest::newRow("a -= b") << (a -= b) << a.subtracted(b);
973 }
974
975 void tst_QPainterPath::operators()
976 {
977 QFETCH(QPainterPath, test);
978 QFETCH(QPainterPath, expected);
979
980 QCOMPARE(test, expected);
981 }
982
983 static inline bool pathFuzzyCompare(double p1, double p2)
984 {
985 return qAbs(p1 - p2) < 0.001;
986 }
987
988
989 static inline bool pathFuzzyCompare(float p1, float p2)
990 {
991 return qAbs(p1 - p2) < 0.001;
992 }
993
994
995 void tst_QPainterPath::testArcMoveTo()
996 {
997 QFETCH(QRectF, rect);
998 QFETCH(qreal, angle);
999
1000 QPainterPath path;
1001 path.arcMoveTo(rect, angle);
1002 path.arcTo(rect, angle, 30);
1003 path.arcTo(rect, angle + 30, 30);
1004
1005 QPointF pos = path.elementAt(0);
1006
1007 QVERIFY((path.elementCount()-1) % 3 == 0);
1008
1009 qreal x_radius = rect.width() / 2.0;
1010 qreal y_radius = rect.height() / 2.0;
1011
1012 QPointF shouldBe = rect.center()
1013 + QPointF(x_radius * qCos(qDegreesToRadians(angle)), -y_radius * qSin(qDegreesToRadians(angle)));
1014
1015 qreal iw = 1 / rect.width();
1016 qreal ih = 1 / rect.height();
1017
1018 QVERIFY(pathFuzzyCompare(pos.x() * iw, shouldBe.x() * iw));
1019 QVERIFY(pathFuzzyCompare(pos.y() * ih, shouldBe.y() * ih));
1020 }
1021
1022 void tst_QPainterPath::testOnPath_data()
1023 {
1024 QTest::addColumn<QPainterPath>("path");
1025 QTest::addColumn<qreal>("start");
1026 QTest::addColumn<qreal>("middle");
1027 QTest::addColumn<qreal>("end");
1028
1029 QPainterPath path = QPainterPath(QPointF(153, 199));
1030 path.cubicTo(QPointF(147, 61), QPointF(414, 18),
1031 QPointF(355, 201));
1032
1033 QTest::newRow("First case") << path
1034 << qreal(93.0)
1035 << qreal(4.0)
1036 << qreal(252.13);
1037
1038 path = QPainterPath(QPointF(328, 197));
1039 path.cubicTo(QPointF(150, 50), QPointF(401, 50),
1040 QPointF(225, 197));
1041 QTest::newRow("Second case") << path
1042 << qreal(140.0)
1043 << qreal(0.0)
1044 << qreal(220.0);
1045
1046 path = QPainterPath(QPointF(328, 197));
1047 path.cubicTo(QPointF(101 , 153), QPointF(596, 151),
1048 QPointF(353, 197));
1049 QTest::newRow("Third case") << path
1050 << qreal(169.0)
1051 << qreal(0.22)
1052 << qreal(191.0);
1053
1054 path = QPainterPath(QPointF(153, 199));
1055 path.cubicTo(QPointF(59, 53), QPointF(597, 218),
1056 QPointF(355, 201));
1057 QTest::newRow("Fourth case") << path
1058 << qreal(122.0)
1059 << qreal(348.0)
1060 << qreal(175.0);
1061
1062 }
1063
1064 #define SIGN(x) ((x < 0)?-1:1)
1065 void tst_QPainterPath::testOnPath()
1066 {
1067 QFETCH(QPainterPath, path);
1068 QFETCH(qreal, start);
1069 QFETCH(qreal, middle);
1070 QFETCH(qreal, end);
1071
1072 int signStart = SIGN(start);
1073 int signMid = SIGN(middle);
1074 int signEnd = SIGN(end);
1075
1076 static const qreal diff = 3;
1077
1078 qreal angle = path.angleAtPercent(0);
1079 QCOMPARE(SIGN(angle), signStart);
1080 QVERIFY(qAbs(angle-start) < diff);
1081
1082 angle = path.angleAtPercent(0.5);
1083 QCOMPARE(SIGN(angle), signMid);
1084 QVERIFY(qAbs(angle-middle) < diff);
1085
1086 angle = path.angleAtPercent(1);
1087 QCOMPARE(SIGN(angle), signEnd);
1088 QVERIFY(qAbs(angle-end) < diff);
1089 }
1090
1091 void tst_QPainterPath::pointAtPercent_data()
1092 {
1093 QTest::addColumn<QPainterPath>("path");
1094 QTest::addColumn<qreal>("percent");
1095 QTest::addColumn<QPointF>("point");
1096
1097 QPainterPath path;
1098 path.lineTo(100, 0);
1099
1100 QTest::newRow("Case 1") << path << qreal(0.2) << QPointF(20, 0);
1101 QTest::newRow("Case 2") << path << qreal(0.5) << QPointF(50, 0);
1102 QTest::newRow("Case 3") << path << qreal(0.0) << QPointF(0, 0);
1103 QTest::newRow("Case 4") << path << qreal(1.0) << QPointF(100, 0);
1104
1105 path = QPainterPath();
1106 path.lineTo(0, 100);
1107
1108 QTest::newRow("Case 5") << path << qreal(0.2) << QPointF(0, 20);
1109 QTest::newRow("Case 6") << path << qreal(0.5) << QPointF(0, 50);
1110 QTest::newRow("Case 7") << path << qreal(0.0) << QPointF(0, 0);
1111 QTest::newRow("Case 8") << path << qreal(1.0) << QPointF(0, 100);
1112
1113 path.lineTo(300, 100);
1114
1115 QTest::newRow("Case 9") << path << qreal(0.25) << QPointF(0, 100);
1116 QTest::newRow("Case 10") << path << qreal(0.5) << QPointF(100, 100);
1117 QTest::newRow("Case 11") << path << qreal(0.75) << QPointF(200, 100);
1118
1119 path = QPainterPath();
1120 path.addEllipse(0, 0, 100, 100);
1121
1122 QTest::newRow("Case 12") << path << qreal(0.0) << QPointF(100, 50);
1123 QTest::newRow("Case 13") << path << qreal(0.25) << QPointF(50, 100);
1124 QTest::newRow("Case 14") << path << qreal(0.5) << QPointF(0, 50);
1125 QTest::newRow("Case 15") << path << qreal(0.75) << QPointF(50, 0);
1126 QTest::newRow("Case 16") << path << qreal(1.0) << QPointF(100, 50);
1127
1128 path = QPainterPath();
1129 QRectF rect(241, 273, 185, 228);
1130 path.addEllipse(rect);
1131 QTest::newRow("Case 17") << path << qreal(1.0) << QPointF(rect.right(), qreal(0.5) * (rect.top() + rect.bottom()));
1132
1133 path = QPainterPath();
1134 path.moveTo(100, 100);
1135 QTest::newRow("Case 18") << path << qreal(0.0) << QPointF(100, 100);
1136 QTest::newRow("Case 19") << path << qreal(1.0) << QPointF(100, 100);
1137 }
1138
1139 void tst_QPainterPath::pointAtPercent()
1140 {
1141 QFETCH(QPainterPath, path);
1142 QFETCH(qreal, percent);
1143 QFETCH(QPointF, point);
1144
1145 QPointF result = path.pointAtPercent(percent);
1146 QVERIFY(pathFuzzyCompare(point.x() , result.x()));
1147 QVERIFY(pathFuzzyCompare(point.y() , result.y()));
1148 }
1149
1150 void tst_QPainterPath::setElementPositionAt()
1151 {
1152 QPainterPath path(QPointF(42., 42.));
1153 QCOMPARE(path.elementCount(), 1);
1154 QCOMPARE(path.elementAt(0).type, QPainterPath::MoveToElement);
1155 QCOMPARE(path.elementAt(0).x, qreal(42.));
1156 QCOMPARE(path.elementAt(0).y, qreal(42.));
1157
1158 QPainterPath copy = path;
1159 copy.setElementPositionAt(0, qreal(0), qreal(0));
1160 QCOMPARE(copy.elementCount(), 1);
1161 QCOMPARE(copy.elementAt(0).type, QPainterPath::MoveToElement);
1162 QCOMPARE(copy.elementAt(0).x, qreal(0));
1163 QCOMPARE(copy.elementAt(0).y, qreal(0));
1164
1165 QCOMPARE(path.elementCount(), 1);
1166 QCOMPARE(path.elementAt(0).type, QPainterPath::MoveToElement);
1167 QCOMPARE(path.elementAt(0).x, qreal(42.));
1168 QCOMPARE(path.elementAt(0).y, qreal(42.));
1169 }
1170
1171 void tst_QPainterPath::angleAtPercent()
1172 {
1173 for (int angle = 0; angle < 360; ++angle) {
1174 QLineF line = QLineF::fromPolar(100, angle);
1175 QPainterPath path;
1176 path.moveTo(line.p1());
1177 path.lineTo(line.p2());
1178
1179 QCOMPARE(path.angleAtPercent(0.5), line.angle());
1180 }
1181 }
1182
1183 void tst_QPainterPath::arcWinding_data()
1184 {
1185 QTest::addColumn<QPainterPath>("path");
1186 QTest::addColumn<QPointF>("point");
1187 QTest::addColumn<bool>("inside");
1188
1189 QPainterPath a;
1190 a.addEllipse(0, 0, 100, 100);
1191 a.addRect(50, 50, 100, 100);
1192
1193 QTest::newRow("Case A (oddeven)") << a << QPointF(55, 55) << false;
1194 a.setFillRule(Qt::WindingFill);
1195 QTest::newRow("Case A (winding)") << a << QPointF(55, 55) << true;
1196
1197 QPainterPath b;
1198 b.arcMoveTo(0, 0, 100, 100, 10);
1199 b.arcTo(0, 0, 100, 100, 10, 360);
1200 b.addRect(50, 50, 100, 100);
1201
1202 QTest::newRow("Case B (oddeven)") << b << QPointF(55, 55) << false;
1203 b.setFillRule(Qt::WindingFill);
1204 QTest::newRow("Case B (winding)") << b << QPointF(55, 55) << false;
1205
1206 QPainterPath c;
1207 c.arcMoveTo(0, 0, 100, 100, 0);
1208 c.arcTo(0, 0, 100, 100, 0, 360);
1209 c.addRect(50, 50, 100, 100);
1210
1211 QTest::newRow("Case C (oddeven)") << c << QPointF(55, 55) << false;
1212 c.setFillRule(Qt::WindingFill);
1213 QTest::newRow("Case C (winding)") << c << QPointF(55, 55) << false;
1214
1215 QPainterPath d;
1216 d.arcMoveTo(0, 0, 100, 100, 10);
1217 d.arcTo(0, 0, 100, 100, 10, -360);
1218 d.addRect(50, 50, 100, 100);
1219
1220 QTest::newRow("Case D (oddeven)") << d << QPointF(55, 55) << false;
1221 d.setFillRule(Qt::WindingFill);
1222 QTest::newRow("Case D (winding)") << d << QPointF(55, 55) << true;
1223
1224 QPainterPath e;
1225 e.arcMoveTo(0, 0, 100, 100, 0);
1226 e.arcTo(0, 0, 100, 100, 0, -360);
1227 e.addRect(50, 50, 100, 100);
1228
1229 QTest::newRow("Case E (oddeven)") << e << QPointF(55, 55) << false;
1230 e.setFillRule(Qt::WindingFill);
1231 QTest::newRow("Case E (winding)") << e << QPointF(55, 55) << true;
1232 }
1233
1234 void tst_QPainterPath::arcWinding()
1235 {
1236 QFETCH(QPainterPath, path);
1237 QFETCH(QPointF, point);
1238 QFETCH(bool, inside);
1239
1240 QCOMPARE(path.contains(point), inside);
1241 }
1242
1243 void tst_QPainterPath::testToFillPolygons()
1244 {
1245 QPainterPath path;
1246 path.lineTo(QPointF(0, 50));
1247 path.lineTo(QPointF(50, 50));
1248
1249 path.moveTo(QPointF(70, 50));
1250 path.lineTo(QPointF(70, 100));
1251 path.lineTo(QPointF(40, 100));
1252
1253 const QList<QPolygonF> polygons = path.toFillPolygons();
1254 QCOMPARE(polygons.size(), 2);
1255 QCOMPARE(polygons.first().count(QPointF(70, 50)), 0);
1256 }
1257
1258 #if QT_CONFIG(signaling_nan)
1259 void tst_QPainterPath::testNaNandInfinites()
1260 {
1261 QPainterPath path1;
1262 QPainterPath path2 = path1;
1263
1264 QPointF p1 = QPointF(qSNaN(), 1);
1265 QPointF p2 = QPointF(qQNaN(), 1);
1266 QPointF p3 = QPointF(qQNaN(), 1);
1267 QPointF pInf = QPointF(qInf(), 1);
1268
1269 // all these operations with NaN/Inf should be ignored
1270 // can't test operator>> reliably, as we can't create a path with NaN to << later
1271
1272 path1.moveTo(p1);
1273 path1.moveTo(qSNaN(), qQNaN());
1274 path1.moveTo(pInf);
1275
1276 path1.lineTo(p1);
1277 path1.lineTo(qSNaN(), qQNaN());
1278 path1.lineTo(pInf);
1279
1280 path1.cubicTo(p1, p2, p3);
1281 path1.cubicTo(p1, QPointF(1, 1), QPointF(2, 2));
1282 path1.cubicTo(pInf, QPointF(10, 10), QPointF(5, 1));
1283
1284 path1.quadTo(p1, p2);
1285 path1.quadTo(QPointF(1, 1), p3);
1286 path1.quadTo(QPointF(1, 1), pInf);
1287
1288 path1.arcTo(QRectF(p1, p2), 5, 5);
1289 path1.arcTo(QRectF(pInf, QPointF(1, 1)), 5, 5);
1290
1291 path1.addRect(QRectF(p1, p2));
1292 path1.addRect(QRectF(pInf, QPointF(1, 1)));
1293
1294 path1.addEllipse(QRectF(p1, p2));
1295 path1.addEllipse(QRectF(pInf, QPointF(1, 1)));
1296
1297 QCOMPARE(path1, path2);
1298
1299 path1.lineTo(QPointF(1, 1));
1300 QVERIFY(path1 != path2);
1301 }
1302 #endif // signaling_nan
1303
1304 void tst_QPainterPath::connectPathDuplicatePoint()
1305 {
1306 QPainterPath a;
1307 a.moveTo(10, 10);
1308 a.lineTo(20, 20);
1309
1310 QPainterPath b;
1311 b.moveTo(20, 20);
1312 b.lineTo(30, 10);
1313
1314 a.connectPath(b);
1315
1316 QPainterPath c;
1317 c.moveTo(10, 10);
1318 c.lineTo(20, 20);
1319 c.lineTo(30, 10);
1320
1321 QCOMPARE(c, a);
1322 }
1323
1324 void tst_QPainterPath::connectPathMoveTo()
1325 {
1326 QPainterPath path1;
1327 QPainterPath path2;
1328 QPainterPath path3;
1329 QPainterPath path4;
1330
1331 path1.moveTo(1,1);
1332
1333 path2.moveTo(4,4);
1334 path2.lineTo(5,6);
1335 path2.lineTo(6,7);
1336
1337 path3.connectPath(path2);
1338
1339 path4.lineTo(5,5);
1340
1341 path1.connectPath(path2);
1342
1343 QCOMPARE(path1.elementAt(0).type, QPainterPath::MoveToElement);
1344 QCOMPARE(path2.elementAt(0).type, QPainterPath::MoveToElement);
1345 QCOMPARE(path3.elementAt(0).type, QPainterPath::MoveToElement);
1346 QCOMPARE(path4.elementAt(0).type, QPainterPath::MoveToElement);
1347 }
1348
1349 void tst_QPainterPath::translate()
1350 {
1351 QPainterPath path;
1352
1353 // Path with no elements.
1354 QCOMPARE(path.currentPosition(), QPointF());
1355 path.translate(50.5, 50.5);
1356 QCOMPARE(path.currentPosition(), QPointF());
1357 QCOMPARE(path.translated(50.5, 50.5).currentPosition(), QPointF());
1358
1359 // path.isEmpty(), but we have one MoveTo element that should be translated.
1360 path.moveTo(50, 50);
1361 QCOMPARE(path.currentPosition(), QPointF(50, 50));
1362 path.translate(99.9, 99.9);
1363 QCOMPARE(path.currentPosition(), QPointF(149.9, 149.9));
1364 path.translate(-99.9, -99.9);
1365 QCOMPARE(path.currentPosition(), QPointF(50, 50));
1366 QCOMPARE(path.translated(-50, -50).currentPosition(), QPointF(0, 0));
1367
1368 // Complex path.
1369 QRegion shape(100, 100, 300, 200, QRegion::Ellipse);
1370 shape -= QRect(225, 175, 50, 50);
1371 QPainterPath complexPath;
1372 complexPath.addRegion(shape);
1373 QVector<QPointF> untranslatedElements;
1374 for (int i = 0; i < complexPath.elementCount(); ++i)
1375 untranslatedElements.append(QPointF(complexPath.elementAt(i)));
1376
1377 const QPainterPath untranslatedComplexPath(complexPath);
1378 const QPointF offset(100, 100);
1379 complexPath.translate(offset);
1380
1381 for (int i = 0; i < complexPath.elementCount(); ++i)
1382 QCOMPARE(QPointF(complexPath.elementAt(i)) - offset, untranslatedElements.at(i));
1383
1384 QCOMPARE(complexPath.translated(-offset), untranslatedComplexPath);
1385 }
1386
1387
1388 void tst_QPainterPath::lineWithinBounds()
1389 {
1390 const int iteration_count = 3;
1391 volatile const qreal yVal = 0.5;
1392 QPointF a(0.0, yVal);
1393 QPointF b(1000.0, yVal);
1394 QPointF c(2000.0, yVal);
1395 QPointF d(3000.0, yVal);
1396 QPainterPath path;
1397 path.moveTo(QPointF(0, yVal));
1398 path.cubicTo(QPointF(1000.0, yVal), QPointF(2000.0, yVal), QPointF(3000.0, yVal));
1399 for(int i=0; i<=iteration_count; i++) {
1400 qreal actual = path.pointAtPercent(qreal(i) / iteration_count).y();
1401 QVERIFY(actual == yVal); // don't use QCOMPARE, don't want fuzzy comparison
1402 }
1403 }
1404
1405 void tst_QPainterPath::intersectionEquality()
1406 {
1407 // Test case from QTBUG-17027
1408 QPainterPath p1;
1409 p1.moveTo(256.0000000000000000, 135.8384137532701743);
1410 p1.lineTo(50.9999999999999715, 107.9999999999999857);
1411 p1.lineTo(233.5425474228109123, 205.3560252921671462);
1412 p1.lineTo(191.7771366877784373, 318.0257074407572304);
1413 p1.lineTo(-48.2616272048215151, 229.0459803737862216);
1414 p1.lineTo(0.0000000000000000, 98.8515898136580801);
1415 p1.lineTo(0.0000000000000000, 0.0000000000000000);
1416 p1.lineTo(256.0000000000000000, 0.0000000000000000);
1417 p1.lineTo(256.0000000000000000, 135.8384137532701743);
1418
1419 QPainterPath p2;
1420 p2.moveTo(1516.2703263523442274, 306.9795200262722119);
1421 p2.lineTo(-1296.8426224886295585, -75.0331736542986931);
1422 p2.lineTo(-1678.8553161692004778, 2738.0797751866753060);
1423 p2.lineTo(1134.2576326717733081, 3120.0924688672457705);
1424 p2.lineTo(1516.2703263523442274, 306.9795200262722119);
1425
1426 QPainterPath i1 = p1.intersected(p2);
1427 QPainterPath i2 = p2.intersected(p1);
1428 QVERIFY(i1 == i2 || i1.toReversed() == i2);
1429
1430 p1 = QPainterPath();
1431 p1.moveTo(256.00000000, 135.83841375);
1432 p1.lineTo(50.99999999, 107.99999999);
1433 p1.lineTo(233.54254742, 205.35602529);
1434 p1.lineTo(191.77713668, 318.02570744);
1435 p1.lineTo(-48.26162720, 229.04598037);
1436 p1.lineTo(0.00000000, 98.85158981);
1437 p1.lineTo(0.00000000, 0.00000000);
1438 p1.lineTo(256.00000000, 0.00000000);
1439 p1.lineTo(256.00000000, 135.83841375);
1440
1441 p2 = QPainterPath();
1442 p2.moveTo(1516.27032635, 306.97952002);
1443 p2.lineTo(-1296.84262248, -75.03317365);
1444 p2.lineTo(-1678.85531616, 2738.07977518);
1445 p2.lineTo(1134.25763267, 3120.09246886);
1446 p2.lineTo(1516.27032635, 306.97952002);
1447
1448 i1 = p1.intersected(p2);
1449 i2 = p2.intersected(p1);
1450 QVERIFY(i1 == i2 || i1.toReversed() == i2);
1451 }
1452
1453 void tst_QPainterPath::intersectionPointOnEdge()
1454 {
1455 // From QTBUG-31551
1456 QPainterPath p; p.addRoundedRect(-10, 10, 40, 40, 10, 10);
1457 QRectF r(0, 0, 100, 100);
1458 QPainterPath rp; rp.addRect(r);
1459 QVERIFY(!p.intersected(rp).isEmpty());
1460 QVERIFY(p.intersects(rp));
1461 QVERIFY(p.intersects(r));
1462 }
1463
1464 QTEST_APPLESS_MAIN(tst_QPainterPath)
1465
1466 #include "tst_qpainterpath.moc"