File indexing completed on 2025-02-02 04:14:57

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "TestKoMarkerCollection.h"
0008 
0009 #include <simpletest.h>
0010 #include <QFileInfo>
0011 #include <QPainter>
0012 #include <QPainterPath>
0013 #include <KoMarker.h>
0014 #include <KoMarkerCollection.h>
0015 #include <KoPathShape.h>
0016 #include <KoColorBackground.h>
0017 
0018 #include "kis_debug.h"
0019 #include <qimage_test_util.h>
0020 #include <testflake.h>
0021 
0022 #include <cmath>
0023 
0024 
0025 void initMarkerCollection(KoMarkerCollection *collection)
0026 {
0027     QCOMPARE(collection->markers().size(), 1);
0028 
0029     const QString fileName = TestUtil::fetchDataFileLazy("test_markers.svg");
0030     QVERIFY(QFileInfo(fileName).exists());
0031 
0032     collection->loadMarkersFromFile(fileName);
0033     QCOMPARE(collection->markers().size(), 10);
0034 }
0035 
0036 void TestKoMarkerCollection::testLoadMarkersFromFile()
0037 {
0038     KoMarkerCollection collection;
0039     initMarkerCollection(&collection);
0040 }
0041 
0042 void TestKoMarkerCollection::testDeduplication()
0043 {
0044     QPainterPath path1;
0045     path1.addRect(QRect(5,5,15,15));
0046 
0047     KoPathShape *shape1(KoPathShape::createShapeFromPainterPath(path1));
0048     shape1->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(Qt::blue)));
0049 
0050     KoMarker *marker(new KoMarker());
0051     marker->setAutoOrientation(true);
0052     marker->setShapes({shape1});
0053 
0054     KoMarkerCollection collection;
0055     QCOMPARE(collection.markers().size(), 1);
0056 
0057     KoMarker *clonedMarker = new KoMarker(*marker);
0058 
0059     collection.addMarker(marker);
0060     QCOMPARE(collection.markers().size(), 2);
0061 
0062     collection.addMarker(marker);
0063     QCOMPARE(collection.markers().size(), 2);
0064 
0065     collection.addMarker(clonedMarker);
0066     QCOMPARE(collection.markers().size(), 2);
0067 }
0068 
0069 void testOneMarkerPosition(KoMarker *marker, KoFlake::MarkerPosition position, const QString &testName)
0070 {
0071     QImage image(30,30, QImage::Format_ARGB32);
0072     image.fill(0);
0073     QPainter painter(&image);
0074 
0075     QPen pen(Qt::black, 2);
0076     marker->drawPreview(&painter, image.rect(), pen, position);
0077 
0078     QVERIFY(TestUtil::checkQImage(image, "marker_collection", "preview", testName));
0079 }
0080 
0081 void TestKoMarkerCollection::testMarkerBounds()
0082 {
0083     KoMarkerCollection collection;
0084     initMarkerCollection(&collection);
0085 
0086     QList<KoMarker*> allMarkers = collection.markers();
0087     KoMarker *marker = allMarkers[3];
0088 
0089     QCOMPARE(marker->boundingRect(1, 0).toAlignedRect(), QRect(-7,-3,9,6));
0090     QCOMPARE(marker->boundingRect(1, M_PI).toAlignedRect(), QRect(-2,-3,9,6));
0091 
0092     QCOMPARE(marker->outline(1, 0).boundingRect().toAlignedRect(), QRect(-6,-2,7,4));
0093     QCOMPARE(marker->outline(1, M_PI).boundingRect().toAlignedRect(), QRect(-1,-2,7,4));
0094 
0095     testOneMarkerPosition(marker, KoFlake::StartMarker, "start_marker");
0096     testOneMarkerPosition(marker, KoFlake::MidMarker, "mid_marker");
0097     testOneMarkerPosition(marker, KoFlake::EndMarker, "end_marker");
0098 }
0099 
0100 KISTEST_MAIN(TestKoMarkerCollection)