|
| 1 | +/*************************************************************************** |
| 2 | + testqgsmaptoolregularpolygon.cpp |
| 3 | + --------------------------- |
| 4 | + Date : January 2018 |
| 5 | + Copyright : (C) 2018 by Paul Blottiere |
| 6 | + Email : paul.blottiere@oslandia.com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgstest.h" |
| 17 | + |
| 18 | +#include "qgisapp.h" |
| 19 | +#include "qgsgeometry.h" |
| 20 | +#include "qgsmapcanvas.h" |
| 21 | +#include "qgssettings.h" |
| 22 | +#include "qgsvectorlayer.h" |
| 23 | +#include "qgsmaptooladdfeature.h" |
| 24 | +#include "qgsgeometryutils.h" |
| 25 | + |
| 26 | +#include "testqgsmaptoolutils.h" |
| 27 | +#include "qgsmaptoolregularpolygon2points.h" |
| 28 | +#include "qgsmaptoolregularpolygoncenterpoint.h" |
| 29 | +#include "qgsmaptoolregularpolygoncentercorner.h" |
| 30 | + |
| 31 | + |
| 32 | +class TestQgsMapToolRegularPolygon : public QObject |
| 33 | +{ |
| 34 | + Q_OBJECT |
| 35 | + |
| 36 | + public: |
| 37 | + TestQgsMapToolRegularPolygon(); |
| 38 | + |
| 39 | + private slots: |
| 40 | + void initTestCase(); |
| 41 | + void cleanupTestCase(); |
| 42 | + |
| 43 | + void testRegularPolygonFrom2Points(); |
| 44 | + void testRegularPolygonFromCenterAndPoint(); |
| 45 | + void testRegularPolygonFromCenterAndCroner(); |
| 46 | + |
| 47 | + private: |
| 48 | + QgisApp *mQgisApp = nullptr; |
| 49 | + QgsMapToolCapture *mParentTool = nullptr; |
| 50 | + QgsMapCanvas *mCanvas = nullptr; |
| 51 | + QgsVectorLayer *mLayer = nullptr; |
| 52 | +}; |
| 53 | + |
| 54 | +TestQgsMapToolRegularPolygon::TestQgsMapToolRegularPolygon() = default; |
| 55 | + |
| 56 | + |
| 57 | +//runs before all tests |
| 58 | +void TestQgsMapToolRegularPolygon::initTestCase() |
| 59 | +{ |
| 60 | + QgsApplication::init(); |
| 61 | + QgsApplication::initQgis(); |
| 62 | + |
| 63 | + mQgisApp = new QgisApp(); |
| 64 | + |
| 65 | + mCanvas = new QgsMapCanvas(); |
| 66 | + mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) ); |
| 67 | + |
| 68 | + // make testing layers |
| 69 | + mLayer = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:27700" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) ); |
| 70 | + QVERIFY( mLayer->isValid() ); |
| 71 | + QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayer ); |
| 72 | + |
| 73 | + // set layers in canvas |
| 74 | + mCanvas->setLayers( QList<QgsMapLayer *>() << mLayer ); |
| 75 | + mCanvas->setCurrentLayer( mLayer ); |
| 76 | + |
| 77 | + mParentTool = new QgsMapToolAddFeature( mCanvas, QgsMapToolCapture::CaptureLine ); |
| 78 | +} |
| 79 | + |
| 80 | +void TestQgsMapToolRegularPolygon::cleanupTestCase() |
| 81 | +{ |
| 82 | + QgsApplication::exitQgis(); |
| 83 | +} |
| 84 | + |
| 85 | +void TestQgsMapToolRegularPolygon::testRegularPolygonFrom2Points() |
| 86 | +{ |
| 87 | + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 ); |
| 88 | + mLayer->startEditing(); |
| 89 | + |
| 90 | + QgsMapToolRegularPolygon2Points mapTool( mParentTool, mCanvas ); |
| 91 | + mCanvas->setMapTool( &mapTool ); |
| 92 | + |
| 93 | + TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool ); |
| 94 | + utils.mouseClick( 0, 0, Qt::LeftButton ); |
| 95 | + utils.mouseMove( 2, 1 ); |
| 96 | + utils.mouseClick( 2, 1, Qt::RightButton ); |
| 97 | + QgsFeatureId newFid = utils.newFeatureId(); |
| 98 | + |
| 99 | + QCOMPARE( mLayer->featureCount(), ( long )1 ); |
| 100 | + QgsFeature f = mLayer->getFeature( newFid ); |
| 101 | + |
| 102 | + QString wkt = "LineStringZ (0 0 333, 2 1 333, 4 -0 333, 4 -2 333, 2 -3 333, -0 -2 333, 0 0 333)"; |
| 103 | + QCOMPARE( f.geometry().asWkt( 0 ), wkt ); |
| 104 | + |
| 105 | + mLayer->rollBack(); |
| 106 | + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 ); |
| 107 | +} |
| 108 | + |
| 109 | +void TestQgsMapToolRegularPolygon::testRegularPolygonFromCenterAndPoint() |
| 110 | +{ |
| 111 | + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 ); |
| 112 | + mLayer->startEditing(); |
| 113 | + |
| 114 | + QgsMapToolRegularPolygonCenterPoint mapTool( mParentTool, mCanvas ); |
| 115 | + mCanvas->setMapTool( &mapTool ); |
| 116 | + |
| 117 | + TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool ); |
| 118 | + utils.mouseClick( 0, 0, Qt::LeftButton ); |
| 119 | + utils.mouseMove( 2, 1 ); |
| 120 | + utils.mouseClick( 2, 1, Qt::RightButton ); |
| 121 | + QgsFeatureId newFid = utils.newFeatureId(); |
| 122 | + |
| 123 | + QCOMPARE( mLayer->featureCount(), ( long )1 ); |
| 124 | + QgsFeature f = mLayer->getFeature( newFid ); |
| 125 | + |
| 126 | + QString wkt = "LineStringZ (1 2 222, 3 -0 222, 1 -2 222, -1 -2 222, -3 0 222, -1 2 222, 1 2 222)"; |
| 127 | + QCOMPARE( f.geometry().asWkt( 0 ), wkt ); |
| 128 | + |
| 129 | + mLayer->rollBack(); |
| 130 | + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 ); |
| 131 | +} |
| 132 | + |
| 133 | +void TestQgsMapToolRegularPolygon::testRegularPolygonFromCenterAndCroner() |
| 134 | +{ |
| 135 | + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 ); |
| 136 | + mLayer->startEditing(); |
| 137 | + |
| 138 | + QgsMapToolRegularPolygonCenterCorner mapTool( mParentTool, mCanvas ); |
| 139 | + mCanvas->setMapTool( &mapTool ); |
| 140 | + |
| 141 | + TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool ); |
| 142 | + utils.mouseClick( 0, 0, Qt::LeftButton ); |
| 143 | + utils.mouseMove( 2, 1 ); |
| 144 | + utils.mouseClick( 2, 1, Qt::RightButton ); |
| 145 | + QgsFeatureId newFid = utils.newFeatureId(); |
| 146 | + |
| 147 | + QCOMPARE( mLayer->featureCount(), ( long )1 ); |
| 148 | + QgsFeature f = mLayer->getFeature( newFid ); |
| 149 | + |
| 150 | + QString wkt = "LineStringZ (2 1 111, 2 -1 111, -0 -2 111, -2 -1 111, -2 1 111, 0 2 111, 2 1 111)"; |
| 151 | + QCOMPARE( f.geometry().asWkt( 0 ), wkt ); |
| 152 | + |
| 153 | + mLayer->rollBack(); |
| 154 | + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 ); |
| 155 | +} |
| 156 | + |
| 157 | +QGSTEST_MAIN( TestQgsMapToolRegularPolygon ) |
| 158 | +#include "testqgsmaptoolregularpolygon.moc" |
0 commit comments