diff --git a/src/app/qgsmaptooldigitizefeature.cpp b/src/app/qgsmaptooldigitizefeature.cpp index 3a35258976d4..1dddb2a8da28 100644 --- a/src/app/qgsmaptooldigitizefeature.cpp +++ b/src/app/qgsmaptooldigitizefeature.cpp @@ -138,21 +138,31 @@ void QgsMapToolDigitizeFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e ) return; } - QgsPointXY savePoint; //point in layer coordinates + QgsPoint savePoint; //point in layer coordinates + bool isMatchPointZ = false; try { QgsPoint fetchPoint; int res; res = fetchLayerPoint( e->mapPointMatch(), fetchPoint ); + if ( QgsWkbTypes::hasZ( fetchPoint.wkbType() ) ) + isMatchPointZ = true; + if ( res == 0 ) { - savePoint = QgsPointXY( fetchPoint.x(), fetchPoint.y() ); + if ( isMatchPointZ ) + savePoint = fetchPoint; + else + savePoint = QgsPoint( fetchPoint.x(), fetchPoint.y() ); } else { - savePoint = toLayerCoordinates( vlayer, e->mapPoint() ); + QgsPointXY layerPoint = toLayerCoordinates( vlayer, e->mapPoint() ); + if ( isMatchPointZ ) + savePoint = QgsPoint( QgsWkbTypes::PointZ, layerPoint.x(), layerPoint.y(), fetchPoint.z() ); + else + savePoint = QgsPoint( layerPoint.x(), layerPoint.y() ); } - QgsDebugMsg( "savePoint = " + savePoint.toString() ); } catch ( QgsCsException &cse ) { @@ -171,11 +181,11 @@ void QgsMapToolDigitizeFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e ) QgsGeometry g; if ( layerWKBType == QgsWkbTypes::Point ) { - g = QgsGeometry::fromPointXY( savePoint ); + g = QgsGeometry( qgis::make_unique( savePoint ) ); } else if ( !QgsWkbTypes::isMultiType( layerWKBType ) && QgsWkbTypes::hasZ( layerWKBType ) ) { - g = QgsGeometry( new QgsPoint( QgsWkbTypes::PointZ, savePoint.x(), savePoint.y(), defaultZValue() ) ); + g = QgsGeometry( qgis::make_unique( savePoint.x(), savePoint.y(), isMatchPointZ ? savePoint.z() : defaultZValue() ) ); } else if ( QgsWkbTypes::isMultiType( layerWKBType ) && !QgsWkbTypes::hasZ( layerWKBType ) ) { @@ -184,13 +194,13 @@ void QgsMapToolDigitizeFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e ) else if ( QgsWkbTypes::isMultiType( layerWKBType ) && QgsWkbTypes::hasZ( layerWKBType ) ) { QgsMultiPoint *mp = new QgsMultiPoint(); - mp->addGeometry( new QgsPoint( QgsWkbTypes::PointZ, savePoint.x(), savePoint.y(), defaultZValue() ) ); - g = QgsGeometry( mp ); + mp->addGeometry( new QgsPoint( QgsWkbTypes::PointZ, savePoint.x(), savePoint.y(), isMatchPointZ ? savePoint.z() : defaultZValue() ) ); + g.set( mp ); } else { // if layer supports more types (mCheckGeometryType is false) - g = QgsGeometry::fromPointXY( savePoint ); + g = QgsGeometry( qgis::make_unique( savePoint ) ); } if ( QgsWkbTypes::hasM( layerWKBType ) ) diff --git a/tests/src/app/CMakeLists.txt b/tests/src/app/CMakeLists.txt index 5235032517c4..fc87aeef336c 100644 --- a/tests/src/app/CMakeLists.txt +++ b/tests/src/app/CMakeLists.txt @@ -104,7 +104,8 @@ ADD_QGIS_TEST(qgisappclipboard testqgisappclipboard.cpp) ADD_QGIS_TEST(attributetabletest testqgsattributetable.cpp) ADD_QGIS_TEST(applocatorfilters testqgsapplocatorfilters.cpp) ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp) -ADD_QGIS_TEST(maptooladdfeature testqgsmaptooladdfeature.cpp) +ADD_QGIS_TEST(maptooladdfeatureline testqgsmaptooladdfeatureline.cpp) +ADD_QGIS_TEST(maptooladdfeaturepoint testqgsmaptooladdfeaturepoint.cpp) ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp) ADD_QGIS_TEST(maptoolselect testqgsmaptoolselect.cpp) ADD_QGIS_TEST(maptoolreshape testqgsmaptoolreshape.cpp) diff --git a/tests/src/app/testqgsmaptooladdfeature.cpp b/tests/src/app/testqgsmaptooladdfeatureline.cpp similarity index 95% rename from tests/src/app/testqgsmaptooladdfeature.cpp rename to tests/src/app/testqgsmaptooladdfeatureline.cpp index 28a14dc77a38..eab3c027741d 100644 --- a/tests/src/app/testqgsmaptooladdfeature.cpp +++ b/tests/src/app/testqgsmaptooladdfeatureline.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - testqgsmaptooladdfeature.cpp + testqgsmaptooladdfeatureline.cpp ---------------------- Date : October 2017 Copyright : (C) 2017 by Martin Dobias @@ -53,11 +53,11 @@ namespace QTest * \ingroup UnitTests * This is a unit test for the vertex tool */ -class TestQgsMapToolAddFeature : public QObject +class TestQgsMapToolAddFeatureLine : public QObject { Q_OBJECT public: - TestQgsMapToolAddFeature(); + TestQgsMapToolAddFeatureLine(); private slots: void initTestCase();// will be called before the first testfunction is executed. @@ -81,11 +81,11 @@ class TestQgsMapToolAddFeature : public QObject QgsFeatureId mFidLineF1 = 0; }; -TestQgsMapToolAddFeature::TestQgsMapToolAddFeature() = default; +TestQgsMapToolAddFeatureLine::TestQgsMapToolAddFeatureLine() = default; //runs before all tests -void TestQgsMapToolAddFeature::initTestCase() +void TestQgsMapToolAddFeatureLine::initTestCase() { qDebug() << "TestMapToolCapture::initTestCase()"; // init QGIS's paths - true means that all path will be inited from prefix @@ -177,14 +177,14 @@ void TestQgsMapToolAddFeature::initTestCase() } //runs after all tests -void TestQgsMapToolAddFeature::cleanupTestCase() +void TestQgsMapToolAddFeatureLine::cleanupTestCase() { delete mCaptureTool; delete mCanvas; QgsApplication::exitQgis(); } -void TestQgsMapToolAddFeature::testNoTracing() +void TestQgsMapToolAddFeatureLine::testNoTracing() { TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); @@ -205,7 +205,7 @@ void TestQgsMapToolAddFeature::testNoTracing() QCOMPARE( mLayerLine->undoStack()->index(), 1 ); } -void TestQgsMapToolAddFeature::testTracing() +void TestQgsMapToolAddFeatureLine::testTracing() { TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); @@ -250,7 +250,7 @@ void TestQgsMapToolAddFeature::testTracing() mEnableTracingAction->setChecked( false ); } -void TestQgsMapToolAddFeature::testTracingWithOffset() +void TestQgsMapToolAddFeatureLine::testTracingWithOffset() { TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); @@ -328,7 +328,7 @@ void TestQgsMapToolAddFeature::testTracingWithOffset() mEnableTracingAction->setChecked( false ); } -void TestQgsMapToolAddFeature::testZ() +void TestQgsMapToolAddFeatureLine::testZ() { TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); @@ -369,7 +369,7 @@ void TestQgsMapToolAddFeature::testZ() mCanvas->setCurrentLayer( mLayerLine ); } -void TestQgsMapToolAddFeature::testZMSnapping() +void TestQgsMapToolAddFeatureLine::testZMSnapping() { TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); @@ -397,5 +397,5 @@ void TestQgsMapToolAddFeature::testZMSnapping() mCanvas->snappingUtils()->setConfig( cfg ); } -QGSTEST_MAIN( TestQgsMapToolAddFeature ) -#include "testqgsmaptooladdfeature.moc" +QGSTEST_MAIN( TestQgsMapToolAddFeatureLine ) +#include "testqgsmaptooladdfeatureline.moc" diff --git a/tests/src/app/testqgsmaptooladdfeaturepoint.cpp b/tests/src/app/testqgsmaptooladdfeaturepoint.cpp new file mode 100644 index 000000000000..e56bf4b95310 --- /dev/null +++ b/tests/src/app/testqgsmaptooladdfeaturepoint.cpp @@ -0,0 +1,168 @@ +/*************************************************************************** + testqgsmaptooladdfeaturepoint.cpp + ---------------------- + Date : October 2017 + Copyright : (C) 2017 by Martin Dobias + Email : wonder dot sk at gmail dot com + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "qgstest.h" + +#include "qgisapp.h" +#include "qgsadvanceddigitizingdockwidget.h" +#include "qgsgeometry.h" +#include "qgsmapcanvas.h" +#include "qgsmapcanvassnappingutils.h" +#include "qgssnappingconfig.h" +#include "qgssnappingutils.h" +#include "qgsmaptooladdfeature.h" +#include "qgsmapcanvastracer.h" +#include "qgsproject.h" +#include "qgssettings.h" +#include "qgsvectorlayer.h" +#include "qgsmapmouseevent.h" +#include "testqgsmaptoolutils.h" + + +/** + * \ingroup UnitTests + * This is a unit test for the vertex tool + */ +class TestQgsMapToolAddFeaturePoint : public QObject +{ + Q_OBJECT + public: + TestQgsMapToolAddFeaturePoint(); + + private slots: + void initTestCase();// will be called before the first testfunction is executed. + void cleanupTestCase();// will be called after the last testfunction was executed. + + void testPointZ(); + + private: + QgisApp *mQgisApp = nullptr; + QgsMapCanvas *mCanvas = nullptr; + QgsMapToolAddFeature *mCaptureTool = nullptr; + QgsVectorLayer *mLayerPointZSnap = nullptr; + QgsVectorLayer *mLayerPointZ = nullptr; +}; + +TestQgsMapToolAddFeaturePoint::TestQgsMapToolAddFeaturePoint() = default; + + +//runs before all tests +void TestQgsMapToolAddFeaturePoint::initTestCase() +{ + qDebug() << "TestMapToolCapture::initTestCase()"; + // init QGIS's paths - true means that all path will be inited from prefix + QgsApplication::init(); + QgsApplication::initQgis(); + + // Set up the QSettings environment + QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) ); + QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) ); + QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) ); + + mQgisApp = new QgisApp(); + + mCanvas = new QgsMapCanvas(); + + mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) ); + + mCanvas->setFrameStyle( QFrame::NoFrame ); + mCanvas->resize( 512, 512 ); + mCanvas->setExtent( QgsRectangle( 0, 0, 8, 8 ) ); + mCanvas->show(); // to make the canvas resize + mCanvas->hide(); + + // make testing layers + mLayerPointZ = new QgsVectorLayer( QStringLiteral( "PointZ?crs=EPSG:27700" ), QStringLiteral( "layer point Z" ), QStringLiteral( "memory" ) ); + QVERIFY( mLayerPointZ->isValid() ); + QgsProject::instance()->addMapLayers( QList() << mLayerPointZ ); + + mLayerPointZ->startEditing(); + QgsFeature pointFZ; + QString pointWktZ = "PointZ(7 7 4)"; + pointFZ.setGeometry( QgsGeometry::fromWkt( pointWktZ ) ); + + mLayerPointZ->addFeature( pointFZ ); + QCOMPARE( mLayerPointZ->featureCount(), ( long )1 ); + + // make layer for snapping + mLayerPointZSnap = new QgsVectorLayer( QStringLiteral( "PointZ?crs=EPSG:27700" ), QStringLiteral( "Snap point" ), QStringLiteral( "memory" ) ); + QVERIFY( mLayerPointZSnap->isValid() ); + QgsProject::instance()->addMapLayers( QList() << mLayerPointZSnap ); + + mLayerPointZSnap->startEditing(); + QgsFeature pointF; + QString pointWktZSnap = "PointZ(6 6 3)"; + pointF.setGeometry( QgsGeometry::fromWkt( pointWktZSnap ) ); + + mLayerPointZSnap->addFeature( pointF ); + QCOMPARE( mLayerPointZSnap->featureCount(), ( long )1 ); + + QgsSnappingConfig cfg = mCanvas->snappingUtils()->config(); + cfg.setMode( QgsSnappingConfig::AllLayers ); + cfg.setTolerance( 100 ); + cfg.setType( QgsSnappingConfig::VertexAndSegment ); + cfg.setEnabled( true ); + mCanvas->snappingUtils()->setConfig( cfg ); + + mCanvas->setLayers( QList() << mLayerPointZ << mLayerPointZSnap ); + mCanvas->setCurrentLayer( mLayerPointZ ); + + // create the tool + mCaptureTool = new QgsMapToolAddFeature( mCanvas, /*mAdvancedDigitizingDockWidget, */ QgsMapToolCapture::CapturePoint ); + mCanvas->setMapTool( mCaptureTool ); + + QCOMPARE( mCanvas->mapSettings().outputSize(), QSize( 512, 512 ) ); + QCOMPARE( mCanvas->mapSettings().visibleExtent(), QgsRectangle( 0, 0, 8, 8 ) ); +} + +//runs after all tests +void TestQgsMapToolAddFeaturePoint::cleanupTestCase() +{ + delete mCaptureTool; + delete mCanvas; + QgsApplication::exitQgis(); +} + +void TestQgsMapToolAddFeaturePoint::testPointZ() +{ + TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool ); + + // test with default Z value = 333 + QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 ); + + QSet oldFids = utils.existingFeatureIds(); + + utils.mouseClick( 4, 0, Qt::LeftButton, Qt::KeyboardModifiers(), true ); + QgsFeatureId newFid = utils.newFeatureId( oldFids ); + + QCOMPARE( mLayerPointZ->featureCount(), ( long )2 ); + + QString wkt = "PointZ (4 0 333)"; + QCOMPARE( mLayerPointZ->getFeature( newFid ).geometry().asWkt(), wkt ); + + mLayerPointZ->undoStack()->undo(); + + oldFids = utils.existingFeatureIds(); + utils.mouseClick( 6, 6, Qt::LeftButton, Qt::KeyboardModifiers(), true ); + newFid = utils.newFeatureId( oldFids ); + + wkt = "PointZ (6 6 3)"; + QCOMPARE( mLayerPointZ->getFeature( newFid ).geometry().asWkt(), wkt ); + + mLayerPointZ->undoStack()->undo(); +} + +QGSTEST_MAIN( TestQgsMapToolAddFeaturePoint ) +#include "testqgsmaptooladdfeaturepoint.moc"