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
793 void tst_QPainterPath::closing()
794 {
795 // lineto's
796 {
797 QPainterPath triangle(QPoint(100, 100));
798
799 triangle.lineTo(200, 100);
800 triangle.lineTo(200, 200);
801 QCOMPARE(triangle.elementCount(), 3);
802
803 //add this line to make sure closeSubpath() also calls detach() and detached properly
804 QPainterPath copied = triangle;
805 triangle.closeSubpath();
806 QCOMPARE(copied.elementCount(), 3);
807
808 QCOMPARE(triangle.elementCount(), 4);
809 QCOMPARE(triangle.elementAt(3).type, QPainterPath::LineToElement);
810
811 triangle.moveTo(300, 300);
812 QCOMPARE(triangle.elementCount(), 5);
813 QCOMPARE(triangle.elementAt(4).type, QPainterPath::MoveToElement);
814
815 triangle.lineTo(400, 300);
816 triangle.lineTo(400, 400);
817 QCOMPARE(triangle.elementCount(), 7);
818
819 triangle.closeSubpath();
820 QCOMPARE(triangle.elementCount(), 8);
821
822 // this will should trigger implicit moveto...
823 triangle.lineTo(600, 300);
824 QCOMPARE(triangle.elementCount(), 10);
825 QCOMPARE(triangle.elementAt(8).type, QPainterPath::MoveToElement);
826 QCOMPARE(triangle.elementAt(9).type, QPainterPath::LineToElement);
827
828 triangle.lineTo(600, 700);
829 QCOMPARE(triangle.elementCount(), 11);
830 }
831
832 // curveto's
833 {
834 QPainterPath curves(QPoint(100, 100));
835
836 curves.cubicTo(200, 100, 100, 200, 200, 200);
837 QCOMPARE(curves.elementCount(), 4);
838
839 curves.closeSubpath();
840 QCOMPARE(curves.elementCount(), 5);
841 QCOMPARE(curves.elementAt(4).type, QPainterPath::LineToElement);
842
843 curves.moveTo(300, 300);
844 QCOMPARE(curves.elementCount(), 6);
845 QCOMPARE(curves.elementAt(5).type, QPainterPath::MoveToElement);
846
847 curves.cubicTo(400, 300, 300, 400, 400, 400);
848 QCOMPARE(curves.elementCount(), 9);
849
850 curves.closeSubpath();
851 QCOMPARE(curves.elementCount(), 10);
852
853 // should trigger implicit moveto..
854 curves.cubicTo(100, 800, 800, 100, 800, 800);
855 QCOMPARE(curves.elementCount(), 14);
856 QCOMPARE(curves.elementAt(10).type, QPainterPath::MoveToElement);
857 QCOMPARE(curves.elementAt(11).type, QPainterPath::CurveToElement);
858 }
859
860 {
861 QPainterPath rects;
862 rects.addRect(100, 100, 100, 100);
863
864 QCOMPARE(rects.elementCount(), 5);
865 QCOMPARE(rects.elementAt(0).type, QPainterPath::MoveToElement);
866 QCOMPARE(rects.elementAt(4).type, QPainterPath::LineToElement);
867
868 rects.addRect(300, 100, 100,100);
869 QCOMPARE(rects.elementCount(), 10);
870 QCOMPARE(rects.elementAt(5).type, QPainterPath::MoveToElement);
871 QCOMPARE(rects.elementAt(9).type, QPainterPath::LineToElement);
872
873 rects.lineTo(0, 0);
874 QCOMPARE(rects.elementCount(), 12);
875 QCOMPARE(rects.elementAt(10).type, QPainterPath::MoveToElement);
876 QCOMPARE(rects.elementAt(11).type, QPainterPath::LineToElement);
877 }
878
879 {
880 QPainterPath ellipses;
881 ellipses.addEllipse(100, 100, 100, 100);
882
883 QCOMPARE(ellipses.elementCount(), 13);
884 QCOMPARE(ellipses.elementAt(0).type, QPainterPath::MoveToElement);
885 QCOMPARE(ellipses.elementAt(10).type, QPainterPath::CurveToElement);
886
887 ellipses.addEllipse(300, 100, 100,100);
888 QCOMPARE(ellipses.elementCount(), 26);
889 QCOMPARE(ellipses.elementAt(13).type, QPainterPath::MoveToElement);
890 QCOMPARE(ellipses.elementAt(23).type, QPainterPath::CurveToElement);
891
892 ellipses.lineTo(0, 0);
893 QCOMPARE(ellipses.elementCount(), 28);
894 QCOMPARE(ellipses.elementAt(26).type, QPainterPath::MoveToElement);
895 QCOMPARE(ellipses.elementAt(27).type, QPainterPath::LineToElement);
896 }
897
898 {
899 QPainterPath path;
900 path.moveTo(10, 10);
901 path.lineTo(40, 10);
902 path.lineTo(25, 20);
903 path.lineTo(10 + 1e-13, 10 + 1e-13);
904 QCOMPARE(path.elementCount(), 4);
905 path.closeSubpath();
906 QCOMPARE(path.elementCount(), 4);
907 }
908 }
909
910 void tst_QPainterPath::testArcMoveTo_data()
911 {
912 QTest::addColumn<QRectF>("rect");
913 QTest::addColumn<qreal>("angle");
914
915 static Q_CONSTEXPR QRectF rects[] = {
916 QRectF(100, 100, 100, 100),
917 QRectF(100, 100, -100, 100),
918 QRectF(100, 100, 100, -100),
919 QRectF(100, 100, -100, -100),
920 };
921
922 for (uint domain = 0; domain < sizeof rects / sizeof *rects; ++domain) {
923 const QByteArray dB = QByteArray::number(domain);
924 for (int i=-360; i<=360; ++i) {
925 QTest::newRow(("test " + dB + ' ' + QByteArray::number(i)).constData())
926 << rects[domain] << (qreal) i;
927 }
928
929 // test low angles
930 QTest::newRow("low angles 1") << rects[domain] << (qreal) 1e-10;
931 QTest::newRow("low angles 2") << rects[domain] << (qreal)-1e-10;
932 }
933 }
934
935 void tst_QPainterPath::operators_data()
936 {
937 QTest::addColumn<QPainterPath>("test");
938 QTest::addColumn<QPainterPath>("expected");
939
940 QPainterPath a;
941 QPainterPath b;
942 a.addRect(0, 0, 100, 100);
943 b.addRect(50, 50, 100, 100);
944
945 QTest::newRow("a & b") << (a & b) << a.intersected(b);
946 QTest::newRow("a | b") << (a | b) << a.united(b);
947 QTest::newRow("a + b") << (a + b) << a.united(b);
948 QTest::newRow("a - b") << (a - b) << a.subtracted(b);
949
950 QPainterPath c = a;
951 QTest::newRow("a &= b") << (a &= b) << a.intersected(b);
952 c = a;
953 QTest::newRow("a |= b") << (a |= b) << a.united(b);
954 c = a;
955 QTest::newRow("a += b") << (a += b) << a.united(b);
956 c = a;
957 QTest::newRow("a -= b") << (a -= b) << a.subtracted(b);
958 }
959
960 void tst_QPainterPath::operators()
961 {
962 QFETCH(QPainterPath, test);
963 QFETCH(QPainterPath, expected);
964
965 QCOMPARE(test, expected);
966 }
967
968 static inline bool pathFuzzyCompare(double p1, double p2)
969 {
970 return qAbs(p1 - p2) < 0.001;
971 }
972
973
974 static inline bool pathFuzzyCompare(float p1, float p2)
975 {
976 return qAbs(p1 - p2) < 0.001;
977 }
978
979
980 void tst_QPainterPath::testArcMoveTo()
981 {
982 QFETCH(QRectF, rect);
983 QFETCH(qreal, angle);
984
985 QPainterPath path;
986 path.arcMoveTo(rect, angle);
987 path.arcTo(rect, angle, 30);
988 path.arcTo(rect, angle + 30, 30);
989
990 QPointF pos = path.elementAt(0);
991
992 QVERIFY((path.elementCount()-1) % 3 == 0);
993
994 qreal x_radius = rect.width() / 2.0;
995 qreal y_radius = rect.height() / 2.0;
996
997 QPointF shouldBe = rect.center()
998 + QPointF(x_radius * qCos(qDegreesToRadians(angle)), -y_radius * qSin(qDegreesToRadians(angle)));
999
1000 qreal iw = 1 / rect.width();
1001 qreal ih = 1 / rect.height();
1002
1003 QVERIFY(pathFuzzyCompare(pos.x() * iw, shouldBe.x() * iw));
1004 QVERIFY(pathFuzzyCompare(pos.y() * ih, shouldBe.y() * ih));
1005 }
1006
1007 void tst_QPainterPath::testOnPath_data()
1008 {
1009 QTest::addColumn<QPainterPath>("path");
1010 QTest::addColumn<qreal>("start");
1011 QTest::addColumn<qreal>("middle");
1012 QTest::addColumn<qreal>("end");
1013
1014 QPainterPath path = QPainterPath(QPointF(153, 199));
1015 path.cubicTo(QPointF(147, 61), QPointF(414, 18),
1016 QPointF(355, 201));
1017
1018 QTest::newRow("First case") << path
1019 << qreal(93.0)
1020 << qreal(4.0)
1021 << qreal(252.13);
1022
1023 path = QPainterPath(QPointF(328, 197));
1024 path.cubicTo(QPointF(150, 50), QPointF(401, 50),
1025 QPointF(225, 197));
1026 QTest::newRow("Second case") << path
1027 << qreal(140.0)
1028 << qreal(0.0)
1029 << qreal(220.0);
1030
1031 path = QPainterPath(QPointF(328, 197));
1032 path.cubicTo(QPointF(101 , 153), QPointF(596, 151),
1033 QPointF(353, 197));
1034 QTest::newRow("Third case") << path
1035 << qreal(169.0)
1036 << qreal(0.22)
1037 << qreal(191.0);
1038
1039 path = QPainterPath(QPointF(153, 199));
1040 path.cubicTo(QPointF(59, 53), QPointF(597, 218),
1041 QPointF(355, 201));
1042 QTest::newRow("Fourth case") << path
1043 << qreal(122.0)
1044 << qreal(348.0)
1045 << qreal(175.0);
1046
1047 }
1048
1049 #define SIGN(x) ((x < 0)?-1:1)
1050 void tst_QPainterPath::testOnPath()
1051 {
1052 QFETCH(QPainterPath, path);
1053 QFETCH(qreal, start);
1054 QFETCH(qreal, middle);
1055 QFETCH(qreal, end);
1056
1057 int signStart = SIGN(start);
1058 int signMid = SIGN(middle);
1059 int signEnd = SIGN(end);
1060
1061 static const qreal diff = 3;
1062
1063 qreal angle = path.angleAtPercent(0);
1064 QCOMPARE(SIGN(angle), signStart);
1065 QVERIFY(qAbs(angle-start) < diff);
1066
1067 angle = path.angleAtPercent(0.5);
1068 QCOMPARE(SIGN(angle), signMid);
1069 QVERIFY(qAbs(angle-middle) < diff);
1070
1071 angle = path.angleAtPercent(1);
1072 QCOMPARE(SIGN(angle), signEnd);
1073 QVERIFY(qAbs(angle-end) < diff);
1074 }
1075
1076 void tst_QPainterPath::pointAtPercent_data()
1077 {
1078 QTest::addColumn<QPainterPath>("path");
1079 QTest::addColumn<qreal>("percent");
1080 QTest::addColumn<QPointF>("point");
1081
1082 QPainterPath path;
1083 path.lineTo(100, 0);
1084
1085 QTest::newRow("Case 1") << path << qreal(0.2) << QPointF(20, 0);
1086 QTest::newRow("Case 2") << path << qreal(0.5) << QPointF(50, 0);
1087 QTest::newRow("Case 3") << path << qreal(0.0) << QPointF(0, 0);
1088 QTest::newRow("Case 4") << path << qreal(1.0) << QPointF(100, 0);
1089
1090 path = QPainterPath();
1091 path.lineTo(0, 100);
1092
1093 QTest::newRow("Case 5") << path << qreal(0.2) << QPointF(0, 20);
1094 QTest::newRow("Case 6") << path << qreal(0.5) << QPointF(0, 50);
1095 QTest::newRow("Case 7") << path << qreal(0.0) << QPointF(0, 0);
1096 QTest::newRow("Case 8") << path << qreal(1.0) << QPointF(0, 100);
1097
1098 path.lineTo(300, 100);
1099
1100 QTest::newRow("Case 9") << path << qreal(0.25) << QPointF(0, 100);
1101 QTest::newRow("Case 10") << path << qreal(0.5) << QPointF(100, 100);
1102 QTest::newRow("Case 11") << path << qreal(0.75) << QPointF(200, 100);
1103
1104 path = QPainterPath();
1105 path.addEllipse(0, 0, 100, 100);
1106
1107 QTest::newRow("Case 12") << path << qreal(0.0) << QPointF(100, 50);
1108 QTest::newRow("Case 13") << path << qreal(0.25) << QPointF(50, 100);
1109 QTest::newRow("Case 14") << path << qreal(0.5) << QPointF(0, 50);
1110 QTest::newRow("Case 15") << path << qreal(0.75) << QPointF(50, 0);
1111 QTest::newRow("Case 16") << path << qreal(1.0) << QPointF(100, 50);
1112
1113 path = QPainterPath();
1114 QRectF rect(241, 273, 185, 228);
1115 path.addEllipse(rect);
1116 QTest::newRow("Case 17") << path << qreal(1.0) << QPointF(rect.right(), qreal(0.5) * (rect.top() + rect.bottom()));
1117
1118 path = QPainterPath();
1119 path.moveTo(100, 100);
1120 QTest::newRow("Case 18") << path << qreal(0.0) << QPointF(100, 100);
1121 QTest::newRow("Case 19") << path << qreal(1.0) << QPointF(100, 100);
1122 }
1123
1124 void tst_QPainterPath::pointAtPercent()
1125 {
1126 QFETCH(QPainterPath, path);
1127 QFETCH(qreal, percent);
1128 QFETCH(QPointF, point);
1129
1130 QPointF result = path.pointAtPercent(percent);
1131 QVERIFY(pathFuzzyCompare(point.x() , result.x()));
1132 QVERIFY(pathFuzzyCompare(point.y() , result.y()));
1133 }
1134
1135 void tst_QPainterPath::setElementPositionAt()
1136 {
1137 QPainterPath path(QPointF(42., 42.));
1138 QCOMPARE(path.elementCount(), 1);
1139 QCOMPARE(path.elementAt(0).type, QPainterPath::MoveToElement);
1140 QCOMPARE(path.elementAt(0).x, qreal(42.));
1141 QCOMPARE(path.elementAt(0).y, qreal(42.));
1142
1143 QPainterPath copy = path;
1144 copy.setElementPositionAt(0, qreal(0), qreal(0));
1145 QCOMPARE(copy.elementCount(), 1);
1146 QCOMPARE(copy.elementAt(0).type, QPainterPath::MoveToElement);
1147 QCOMPARE(copy.elementAt(0).x, qreal(0));
1148 QCOMPARE(copy.elementAt(0).y, qreal(0));
1149
1150 QCOMPARE(path.elementCount(), 1);
1151 QCOMPARE(path.elementAt(0).type, QPainterPath::MoveToElement);
1152 QCOMPARE(path.elementAt(0).x, qreal(42.));
1153 QCOMPARE(path.elementAt(0).y, qreal(42.));
1154 }
1155
1156 void tst_QPainterPath::angleAtPercent()
1157 {
1158 for (int angle = 0; angle < 360; ++angle) {
1159 QLineF line = QLineF::fromPolar(100, angle);
1160 QPainterPath path;
1161 path.moveTo(line.p1());
1162 path.lineTo(line.p2());
1163
1164 QCOMPARE(path.angleAtPercent(0.5), line.angle());
1165 }
1166 }
1167
1168 void tst_QPainterPath::arcWinding_data()
1169 {
1170 QTest::addColumn<QPainterPath>("path");
1171 QTest::addColumn<QPointF>("point");
1172 QTest::addColumn<bool>("inside");
1173
1174 QPainterPath a;
1175 a.addEllipse(0, 0, 100, 100);
1176 a.addRect(50, 50, 100, 100);
1177
1178 QTest::newRow("Case A (oddeven)") << a << QPointF(55, 55) << false;
1179 a.setFillRule(Qt::WindingFill);
1180 QTest::newRow("Case A (winding)") << a << QPointF(55, 55) << true;
1181
1182 QPainterPath b;
1183 b.arcMoveTo(0, 0, 100, 100, 10);
1184 b.arcTo(0, 0, 100, 100, 10, 360);
1185 b.addRect(50, 50, 100, 100);
1186
1187 QTest::newRow("Case B (oddeven)") << b << QPointF(55, 55) << false;
1188 b.setFillRule(Qt::WindingFill);
1189 QTest::newRow("Case B (winding)") << b << QPointF(55, 55) << false;
1190
1191 QPainterPath c;
1192 c.arcMoveTo(0, 0, 100, 100, 0);
1193 c.arcTo(0, 0, 100, 100, 0, 360);
1194 c.addRect(50, 50, 100, 100);
1195
1196 QTest::newRow("Case C (oddeven)") << c << QPointF(55, 55) << false;
1197 c.setFillRule(Qt::WindingFill);
1198 QTest::newRow("Case C (winding)") << c << QPointF(55, 55) << false;
1199
1200 QPainterPath d;
1201 d.arcMoveTo(0, 0, 100, 100, 10);
1202 d.arcTo(0, 0, 100, 100, 10, -360);
1203 d.addRect(50, 50, 100, 100);
1204
1205 QTest::newRow("Case D (oddeven)") << d << QPointF(55, 55) << false;
1206 d.setFillRule(Qt::WindingFill);
1207 QTest::newRow("Case D (winding)") << d << QPointF(55, 55) << true;
1208
1209 QPainterPath e;
1210 e.arcMoveTo(0, 0, 100, 100, 0);
1211 e.arcTo(0, 0, 100, 100, 0, -360);
1212 e.addRect(50, 50, 100, 100);
1213
1214 QTest::newRow("Case E (oddeven)") << e << QPointF(55, 55) << false;
1215 e.setFillRule(Qt::WindingFill);
1216 QTest::newRow("Case E (winding)") << e << QPointF(55, 55) << true;
1217 }
1218
1219 void tst_QPainterPath::arcWinding()
1220 {
1221 QFETCH(QPainterPath, path);
1222 QFETCH(QPointF, point);
1223 QFETCH(bool, inside);
1224
1225 QCOMPARE(path.contains(point), inside);
1226 }
1227
1228 void tst_QPainterPath::testToFillPolygons()
1229 {
1230 QPainterPath path;
1231 path.lineTo(QPointF(0, 50));
1232 path.lineTo(QPointF(50, 50));
1233
1234 path.moveTo(QPointF(70, 50));
1235 path.lineTo(QPointF(70, 100));
1236 path.lineTo(QPointF(40, 100));
1237
1238 const QList<QPolygonF> polygons = path.toFillPolygons();
1239 QCOMPARE(polygons.size(), 2);
1240 QCOMPARE(polygons.first().count(QPointF(70, 50)), 0);
1241 }
1242
1243 #if QT_CONFIG(signaling_nan)
1244 void tst_QPainterPath::testNaNandInfinites()
1245 {
1246 QPainterPath path1;
1247 QPainterPath path2 = path1;
1248
1249 QPointF p1 = QPointF(qSNaN(), 1);
1250 QPointF p2 = QPointF(qQNaN(), 1);
1251 QPointF p3 = QPointF(qQNaN(), 1);
1252 QPointF pInf = QPointF(qInf(), 1);
1253
1254 // all these operations with NaN/Inf should be ignored
1255 // can't test operator>> reliably, as we can't create a path with NaN to << later
1256
1257 path1.moveTo(p1);
1258 path1.moveTo(qSNaN(), qQNaN());
1259 path1.moveTo(pInf);
1260
1261 path1.lineTo(p1);
1262 path1.lineTo(qSNaN(), qQNaN());
1263 path1.lineTo(pInf);
1264
1265 path1.cubicTo(p1, p2, p3);
1266 path1.cubicTo(p1, QPointF(1, 1), QPointF(2, 2));
1267 path1.cubicTo(pInf, QPointF(10, 10), QPointF(5, 1));
1268
1269 path1.quadTo(p1, p2);
1270 path1.quadTo(QPointF(1, 1), p3);
1271 path1.quadTo(QPointF(1, 1), pInf);
1272
1273 path1.arcTo(QRectF(p1, p2), 5, 5);
1274 path1.arcTo(QRectF(pInf, QPointF(1, 1)), 5, 5);
1275
1276 path1.addRect(QRectF(p1, p2));
1277 path1.addRect(QRectF(pInf, QPointF(1, 1)));
1278
1279 path1.addEllipse(QRectF(p1, p2));
1280 path1.addEllipse(QRectF(pInf, QPointF(1, 1)));
1281
1282 QCOMPARE(path1, path2);
1283
1284 path1.lineTo(QPointF(1, 1));
1285 QVERIFY(path1 != path2);
1286 }
1287 #endif // signaling_nan
1288
1289 void tst_QPainterPath::connectPathDuplicatePoint()
1290 {
1291 QPainterPath a;
1292 a.moveTo(10, 10);
1293 a.lineTo(20, 20);
1294
1295 QPainterPath b;
1296 b.moveTo(20, 20);
1297 b.lineTo(30, 10);
1298
1299 a.connectPath(b);
1300
1301 QPainterPath c;
1302 c.moveTo(10, 10);
1303 c.lineTo(20, 20);
1304 c.lineTo(30, 10);
1305
1306 QCOMPARE(c, a);
1307 }
1308
1309 void tst_QPainterPath::connectPathMoveTo()
1310 {
1311 QPainterPath path1;
1312 QPainterPath path2;
1313 QPainterPath path3;
1314 QPainterPath path4;
1315
1316 path1.moveTo(1,1);
1317
1318 path2.moveTo(4,4);
1319 path2.lineTo(5,6);
1320 path2.lineTo(6,7);
1321
1322 path3.connectPath(path2);
1323
1324 path4.lineTo(5,5);
1325
1326 path1.connectPath(path2);
1327
1328 QCOMPARE(path1.elementAt(0).type, QPainterPath::MoveToElement);
1329 QCOMPARE(path2.elementAt(0).type, QPainterPath::MoveToElement);
1330 QCOMPARE(path3.elementAt(0).type, QPainterPath::MoveToElement);
1331 QCOMPARE(path4.elementAt(0).type, QPainterPath::MoveToElement);
1332 }
1333
1334 void tst_QPainterPath::translate()
1335 {
1336 QPainterPath path;
1337
1338 // Path with no elements.
1339 QCOMPARE(path.currentPosition(), QPointF());
1340 path.translate(50.5, 50.5);
1341 QCOMPARE(path.currentPosition(), QPointF());
1342 QCOMPARE(path.translated(50.5, 50.5).currentPosition(), QPointF());
1343
1344 // path.isEmpty(), but we have one MoveTo element that should be translated.
1345 path.moveTo(50, 50);
1346 QCOMPARE(path.currentPosition(), QPointF(50, 50));
1347 path.translate(99.9, 99.9);
1348 QCOMPARE(path.currentPosition(), QPointF(149.9, 149.9));
1349 path.translate(-99.9, -99.9);
1350 QCOMPARE(path.currentPosition(), QPointF(50, 50));
1351 QCOMPARE(path.translated(-50, -50).currentPosition(), QPointF(0, 0));
1352
1353 // Complex path.
1354 QRegion shape(100, 100, 300, 200, QRegion::Ellipse);
1355 shape -= QRect(225, 175, 50, 50);
1356 QPainterPath complexPath;
1357 complexPath.addRegion(shape);
1358 QVector<QPointF> untranslatedElements;
1359 for (int i = 0; i < complexPath.elementCount(); ++i)
1360 untranslatedElements.append(QPointF(complexPath.elementAt(i)));
1361
1362 const QPainterPath untranslatedComplexPath(complexPath);
1363 const QPointF offset(100, 100);
1364 complexPath.translate(offset);
1365
1366 for (int i = 0; i < complexPath.elementCount(); ++i)
1367 QCOMPARE(QPointF(complexPath.elementAt(i)) - offset, untranslatedElements.at(i));
1368
1369 QCOMPARE(complexPath.translated(-offset), untranslatedComplexPath);
1370 }
1371
1372
1373 void tst_QPainterPath::lineWithinBounds()
1374 {
1375 const int iteration_count = 3;
1376 volatile const qreal yVal = 0.5;
1377 QPointF a(0.0, yVal);
1378 QPointF b(1000.0, yVal);
1379 QPointF c(2000.0, yVal);
1380 QPointF d(3000.0, yVal);
1381 QPainterPath path;
1382 path.moveTo(QPointF(0, yVal));
1383 path.cubicTo(QPointF(1000.0, yVal), QPointF(2000.0, yVal), QPointF(3000.0, yVal));
1384 for(int i=0; i<=iteration_count; i++) {
1385 qreal actual = path.pointAtPercent(qreal(i) / iteration_count).y();
1386 QVERIFY(actual == yVal); // don't use QCOMPARE, don't want fuzzy comparison
1387 }
1388 }
1389
1390 void tst_QPainterPath::intersectionEquality()
1391 {
1392 // Test case from QTBUG-17027
1393 QPainterPath p1;
1394 p1.moveTo(256.0000000000000000, 135.8384137532701743);
1395 p1.lineTo(50.9999999999999715, 107.9999999999999857);
1396 p1.lineTo(233.5425474228109123, 205.3560252921671462);
1397 p1.lineTo(191.7771366877784373, 318.0257074407572304);
1398 p1.lineTo(-48.2616272048215151, 229.0459803737862216);
1399 p1.lineTo(0.0000000000000000, 98.8515898136580801);
1400 p1.lineTo(0.0000000000000000, 0.0000000000000000);
1401 p1.lineTo(256.0000000000000000, 0.0000000000000000);
1402 p1.lineTo(256.0000000000000000, 135.8384137532701743);
1403
1404 QPainterPath p2;
1405 p2.moveTo(1516.2703263523442274, 306.9795200262722119);
1406 p2.lineTo(-1296.8426224886295585, -75.0331736542986931);
1407 p2.lineTo(-1678.8553161692004778, 2738.0797751866753060);
1408 p2.lineTo(1134.2576326717733081, 3120.0924688672457705);
1409 p2.lineTo(1516.2703263523442274, 306.9795200262722119);
1410
1411 QPainterPath i1 = p1.intersected(p2);
1412 QPainterPath i2 = p2.intersected(p1);
1413 QVERIFY(i1 == i2 || i1.toReversed() == i2);
1414
1415 p1 = QPainterPath();
1416 p1.moveTo(256.00000000, 135.83841375);
1417 p1.lineTo(50.99999999, 107.99999999);
1418 p1.lineTo(233.54254742, 205.35602529);
1419 p1.lineTo(191.77713668, 318.02570744);
1420 p1.lineTo(-48.26162720, 229.04598037);
1421 p1.lineTo(0.00000000, 98.85158981);
1422 p1.lineTo(0.00000000, 0.00000000);
1423 p1.lineTo(256.00000000, 0.00000000);
1424 p1.lineTo(256.00000000, 135.83841375);
1425
1426 p2 = QPainterPath();
1427 p2.moveTo(1516.27032635, 306.97952002);
1428 p2.lineTo(-1296.84262248, -75.03317365);
1429 p2.lineTo(-1678.85531616, 2738.07977518);
1430 p2.lineTo(1134.25763267, 3120.09246886);
1431 p2.lineTo(1516.27032635, 306.97952002);
1432
1433 i1 = p1.intersected(p2);
1434 i2 = p2.intersected(p1);
1435 QVERIFY(i1 == i2 || i1.toReversed() == i2);
1436 }
1437
1438 void tst_QPainterPath::intersectionPointOnEdge()
1439 {
1440 // From QTBUG-31551
1441 QPainterPath p; p.addRoundedRect(-10, 10, 40, 40, 10, 10);
1442 QRectF r(0, 0, 100, 100);
1443 QPainterPath rp; rp.addRect(r);
1444 QVERIFY(!p.intersected(rp).isEmpty());
1445 QVERIFY(p.intersects(rp));
1446 QVERIFY(p.intersects(r));
1447 }
1448
1449 QTEST_APPLESS_MAIN(tst_QPainterPath)
1450
1451 #include "tst_qpainterpath.moc"