File indexing completed on 2024-06-09 04:24:41

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "freehand_stroke_test.h"
0008 
0009 #include <simpletest.h>
0010 #include <KoCompositeOpRegistry.h>
0011 #include <KoColor.h>
0012 #include "stroke_testing_utils.h"
0013 #include "strokes/freehand_stroke.h"
0014 #include "strokes/KisFreehandStrokeInfo.h"
0015 #include "KisAsynchronousStrokeUpdateHelper.h"
0016 #include "kis_resources_snapshot.h"
0017 #include "kis_image.h"
0018 #include "kis_painter.h"
0019 #include <brushengine/kis_paint_information.h>
0020 
0021 #include "testui.h"
0022 
0023 class FreehandStrokeTester : public utils::StrokeTester
0024 {
0025 public:
0026     FreehandStrokeTester(const QString &presetFilename, bool useLod = false)
0027         : StrokeTester(useLod ? "freehand-lod" : "freehand", QSize(500, 500), presetFilename),
0028           m_useLod(useLod),
0029           m_flipLineDirection(false)
0030     {
0031         setBaseFuzziness(3);
0032     }
0033 
0034     void setFlipLineDirection(bool value) {
0035         m_flipLineDirection = value;
0036         setNumIterations(2);
0037     }
0038 
0039     void setPaintColor(const QColor &color) {
0040         m_paintColor.reset(new QColor(color));
0041     }
0042 
0043 protected:
0044     using utils::StrokeTester::initImage;
0045     void initImage(KisImageWSP image, KisNodeSP activeNode) override {
0046         Q_UNUSED(activeNode);
0047 
0048         if (m_useLod) {
0049             image->setLodPreferences(KisLodPreferences(1));
0050         }
0051     }
0052 
0053     void beforeCheckingResult(KisImageWSP image, KisNodeSP activeNode) override {
0054         Q_UNUSED(image);
0055         Q_UNUSED(activeNode);
0056 
0057         if (m_useLod) {
0058             //image->testingSetLevelOfDetailsEnabled(true);
0059         }
0060     }
0061 
0062     void modifyResourceManager(KoCanvasResourceProvider *manager,
0063                                KisImageWSP image) override
0064     {
0065         modifyResourceManager(manager, image, 0);
0066     }
0067 
0068 
0069     void modifyResourceManager(KoCanvasResourceProvider *manager,
0070                                KisImageWSP image,
0071                                int iteration) override {
0072 
0073         if (m_paintColor && iteration > 0) {
0074             QVariant i;
0075             i.setValue(KoColor(*m_paintColor, image->colorSpace()));
0076             manager->setResource(KoCanvasResource::ForegroundColor, i);
0077         }
0078     }
0079 
0080     KisStrokeStrategy* createStroke(KisResourcesSnapshotSP resources,
0081                                     KisImageWSP image) override {
0082         Q_UNUSED(image);
0083 
0084         KisFreehandStrokeInfo *strokeInfo = new KisFreehandStrokeInfo();
0085 
0086         QScopedPointer<FreehandStrokeStrategy> stroke(
0087             new FreehandStrokeStrategy(resources, strokeInfo, kundo2_noi18n("Freehand Stroke")));
0088 
0089         return stroke.take();
0090     }
0091 
0092     void addPaintingJobs(KisImageWSP image,
0093                                  KisResourcesSnapshotSP resources) override
0094     {
0095         addPaintingJobs(image, resources, 0);
0096     }
0097 
0098     void addPaintingJobs(KisImageWSP image, KisResourcesSnapshotSP resources, int iteration) override {
0099         Q_UNUSED(resources);
0100 
0101         KisPaintInformation pi1;
0102         KisPaintInformation pi2;
0103 
0104         if (!iteration) {
0105             pi1 = KisPaintInformation(QPointF(200, 200));
0106             pi2 = KisPaintInformation(QPointF(300, 300));
0107         } else {
0108             pi1 = KisPaintInformation(QPointF(200, 300));
0109             pi2 = KisPaintInformation(QPointF(300, 200));
0110         }
0111 
0112         QScopedPointer<KisStrokeJobData> data(
0113             new FreehandStrokeStrategy::Data(0, pi1, pi2));
0114 
0115         image->addJob(strokeId(), data.take());
0116         image->addJob(strokeId(), new KisAsynchronousStrokeUpdateHelper::UpdateData(true));
0117     }
0118 
0119 private:
0120     bool m_useLod;
0121     bool m_flipLineDirection;
0122     QScopedPointer<QColor> m_paintColor;
0123 };
0124 
0125 void FreehandStrokeTest::testAutoBrushStroke()
0126 {
0127     FreehandStrokeTester tester("autobrush_300px.kpp");
0128     tester.test();
0129 }
0130 
0131 void FreehandStrokeTest::testHatchingStroke()
0132 {
0133     FreehandStrokeTester tester("hatching_30px.kpp");
0134     tester.test();
0135 }
0136 
0137 void FreehandStrokeTest::testColorSmudgeStroke()
0138 {
0139     FreehandStrokeTester tester("colorsmudge_predefined.kpp");
0140     tester.test();
0141 }
0142 
0143 void FreehandStrokeTest::testAutoTextured17()
0144 {
0145     FreehandStrokeTester tester("auto_textured_17.kpp");
0146     tester.test();
0147 }
0148 
0149 void FreehandStrokeTest::testAutoTextured38()
0150 {
0151     FreehandStrokeTester tester("auto_textured_38.kpp");
0152     tester.test();
0153 }
0154 
0155 void FreehandStrokeTest::testMixDullCompositing()
0156 {
0157     FreehandStrokeTester tester("Mix_dull.kpp");
0158     tester.setFlipLineDirection(true);
0159     tester.setPaintColor(Qt::red);
0160     tester.test();
0161 }
0162 
0163 void FreehandStrokeTest::testAutoBrushStrokeLod()
0164 {
0165     FreehandStrokeTester tester("Basic_tip_default.kpp", true);
0166     tester.testSimpleStroke();
0167 }
0168 
0169 void FreehandStrokeTest::testPredefinedBrushStrokeLod()
0170 {
0171     qsrand(QTime::currentTime().msec());
0172 
0173     FreehandStrokeTester tester("testing_predefined_lod_spc13.kpp", true);
0174     //FreehandStrokeTester tester("testing_predefined_lod.kpp", true);
0175     tester.testSimpleStroke();
0176 }
0177 
0178 KISTEST_MAIN(FreehandStrokeTest)