|
| 1 | +/*************************************************************************** |
| 2 | + testqgsmaptooladdfeaturepoint.cpp |
| 3 | + ---------------------- |
| 4 | + Date : October 2017 |
| 5 | + Copyright : (C) 2017 by Martin Dobias |
| 6 | + Email : wonder dot sk at gmail dot 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 "qgsadvanceddigitizingdockwidget.h" |
| 20 | +#include "qgsgeometry.h" |
| 21 | +#include "qgsmapcanvas.h" |
| 22 | +#include "qgsmapcanvassnappingutils.h" |
| 23 | +#include "qgssnappingconfig.h" |
| 24 | +#include "qgssnappingutils.h" |
| 25 | +#include "qgsmaptooladdfeature.h" |
| 26 | +#include "qgsmapcanvastracer.h" |
| 27 | +#include "qgsproject.h" |
| 28 | +#include "qgssettings.h" |
| 29 | +#include "qgsvectorlayer.h" |
| 30 | +#include "qgsmapmouseevent.h" |
| 31 | +#include "testqgsmaptoolutils.h" |
| 32 | + |
| 33 | + |
| 34 | +/** |
| 35 | + * \ingroup UnitTests |
| 36 | + * This is a unit test for the vertex tool |
| 37 | + */ |
| 38 | +class TestQgsMapToolAddFeaturePoint : public QObject |
| 39 | +{ |
| 40 | + Q_OBJECT |
| 41 | + public: |
| 42 | + TestQgsMapToolAddFeaturePoint(); |
| 43 | + |
| 44 | + private slots: |
| 45 | + void initTestCase();// will be called before the first testfunction is executed. |
| 46 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 47 | + |
| 48 | + void testPointZ(); |
| 49 | + |
| 50 | + private: |
| 51 | + QgisApp *mQgisApp = nullptr; |
| 52 | + QgsMapCanvas *mCanvas = nullptr; |
| 53 | + QgsMapToolAddFeature *mCaptureTool = nullptr; |
| 54 | + QgsVectorLayer *mLayerPointZSnap = nullptr; |
| 55 | + QgsVectorLayer *mLayerPointZ = nullptr; |
| 56 | +}; |
| 57 | + |
| 58 | +TestQgsMapToolAddFeaturePoint::TestQgsMapToolAddFeaturePoint() = default; |
| 59 | + |
| 60 | + |
| 61 | +//runs before all tests |
| 62 | +void TestQgsMapToolAddFeaturePoint::initTestCase() |
| 63 | +{ |
| 64 | + qDebug() << "TestMapToolCapture::initTestCase()"; |
| 65 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 66 | + QgsApplication::init(); |
| 67 | + QgsApplication::initQgis(); |
| 68 | + |
| 69 | + // Set up the QSettings environment |
| 70 | + QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) ); |
| 71 | + QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) ); |
| 72 | + QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) ); |
| 73 | + |
| 74 | + mQgisApp = new QgisApp(); |
| 75 | + |
| 76 | + mCanvas = new QgsMapCanvas(); |
| 77 | + |
| 78 | + mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) ); |
| 79 | + |
| 80 | + mCanvas->setFrameStyle( QFrame::NoFrame ); |
| 81 | + mCanvas->resize( 512, 512 ); |
| 82 | + mCanvas->setExtent( QgsRectangle( 0, 0, 8, 8 ) ); |
| 83 | + mCanvas->show(); // to make the canvas resize |
| 84 | + mCanvas->hide(); |
| 85 | + |
| 86 | + // make testing layers |
| 87 | + mLayerPointZ = new QgsVectorLayer( QStringLiteral( "PointZ?crs=EPSG:27700" ), QStringLiteral( "layer point Z" ), QStringLiteral( "memory" ) ); |
| 88 | + QVERIFY( mLayerPointZ->isValid() ); |
| 89 | + QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerPointZ ); |
| 90 | + |
| 91 | + mLayerPointZ->startEditing(); |
| 92 | + QgsFeature pointFZ; |
| 93 | + QString pointWktZ = "PointZ(7 7 4)"; |
| 94 | + pointFZ.setGeometry( QgsGeometry::fromWkt( pointWktZ ) ); |
| 95 | + |
| 96 | + mLayerPointZ->addFeature( pointFZ ); |
| 97 | + QCOMPARE( mLayerPointZ->featureCount(), ( long )1 ); |
| 98 | + |
| 99 | + // make layer for snapping |
| 100 | + mLayerPointZSnap = new QgsVectorLayer( QStringLiteral( "PointZ?crs=EPSG:27700" ), QStringLiteral( "Snap point" ), QStringLiteral( "memory" ) ); |
| 101 | + QVERIFY( mLayerPointZSnap->isValid() ); |
| 102 | + QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerPointZSnap ); |
| 103 | + |
| 104 | + mLayerPointZSnap->startEditing(); |
| 105 | + QgsFeature pointF; |
| 106 | + QString pointWktZSnap = "PointZ(6 6 3)"; |
| 107 | + pointF.setGeometry( QgsGeometry::fromWkt( pointWktZSnap ) ); |
| 108 | + |
| 109 | + mLayerPointZSnap->addFeature( pointF ); |
| 110 | + QCOMPARE( mLayerPointZSnap->featureCount(), ( long )1 ); |
| 111 | + |
| 112 | + QgsSnappingConfig cfg = mCanvas->snappingUtils()->config(); |
| 113 | + cfg.setMode( QgsSnappingConfig::AllLayers ); |
| 114 | + cfg.setTolerance( 100 ); |
| 115 | + cfg.setType( QgsSnappingConfig::VertexAndSegment ); |
| 116 | + cfg.setEnabled( true ); |
| 117 | + mCanvas->snappingUtils()->setConfig( cfg ); |
| 118 | + |
| 119 | + mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerPointZ << mLayerPointZSnap ); |
| 120 | + mCanvas->setCurrentLayer( mLayerPointZ ); |
| 121 | + |
| 122 | + // create the tool |
| 123 | + mCaptureTool = new QgsMapToolAddFeature( mCanvas, /*mAdvancedDigitizingDockWidget, */ QgsMapToolCapture::CapturePoint ); |
| 124 | + mCanvas->setMapTool( mCaptureTool ); |
| 125 | + |
| 126 | + QCOMPARE( mCanvas->mapSettings().outputSize(), QSize( 512, 512 ) ); |
| 127 | + QCOMPARE( mCanvas->mapSettings().visibleExtent(), QgsRectangle( 0, 0, 8, 8 ) ); |
| 128 | +} |
| 129 | + |
| 130 | +//runs after all tests |
| 131 | +void TestQgsMapToolAddFeaturePoint::cleanupTestCase() |
| 132 | +{ |
| 133 | + delete mCaptureTool; |
| 134 | + delete mCanvas; |
| 135 | + QgsApplication::exitQgis(); |
| 136 | +} |
| 137 | + |
| 138 | +void TestQgsMapToolAddFeaturePoint::testPointZ() |
| 139 | +{ |
| 140 | + TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); |
| 141 | + |
| 142 | + // test with default Z value = 333 |
| 143 | + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 ); |
| 144 | + |
| 145 | + QSet<QgsFeatureId> oldFids = utils.existingFeatureIds(); |
| 146 | + |
| 147 | + utils.mouseClick( 4, 0, Qt::LeftButton, Qt::KeyboardModifiers(), true ); |
| 148 | + QgsFeatureId newFid = utils.newFeatureId( oldFids ); |
| 149 | + |
| 150 | + QCOMPARE( mLayerPointZ->featureCount(), ( long )2 ); |
| 151 | + |
| 152 | + QString wkt = "PointZ (4 0 333)"; |
| 153 | + QCOMPARE( mLayerPointZ->getFeature( newFid ).geometry().asWkt(), wkt ); |
| 154 | + |
| 155 | + mLayerPointZ->undoStack()->undo(); |
| 156 | + |
| 157 | + oldFids = utils.existingFeatureIds(); |
| 158 | + utils.mouseClick( 6, 6, Qt::LeftButton, Qt::KeyboardModifiers(), true ); |
| 159 | + newFid = utils.newFeatureId( oldFids ); |
| 160 | + |
| 161 | + wkt = "PointZ (6 6 3)"; |
| 162 | + QCOMPARE( mLayerPointZ->getFeature( newFid ).geometry().asWkt(), wkt ); |
| 163 | + |
| 164 | + mLayerPointZ->undoStack()->undo(); |
| 165 | +} |
| 166 | + |
| 167 | +QGSTEST_MAIN( TestQgsMapToolAddFeaturePoint ) |
| 168 | +#include "testqgsmaptooladdfeaturepoint.moc" |
0 commit comments