Skip to content

Commit

Permalink
Fix reshape for snapped point with Z and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti committed Jun 6, 2019
1 parent e0d04bb commit edbb2fe
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 24 deletions.
10 changes: 6 additions & 4 deletions src/app/qgsmaptoolreshape.cpp
Expand Up @@ -121,9 +121,11 @@ void QgsMapToolReshape::reshape( QgsVectorLayer *vlayer )
bbox.combineExtentWith( points().at( i ).x(), points().at( i ).y() );
}

QgsLineString reshapeLineString( points() );
if ( QgsWkbTypes::hasZ( vlayer->wkbType() ) )
reshapeLineString.addZValue( defaultZValue() );

QgsPointSequence pts;
QVector<QgsPoint> points;
captureCurve()->points( pts );
QgsLineString reshapeLineString( pts );

//query all the features that intersect bounding box of capture line
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( bbox ).setNoAttributes() );
Expand All @@ -143,7 +145,7 @@ void QgsMapToolReshape::reshape( QgsVectorLayer *vlayer )
{
// in case of a binding line, we just want to update the line from
// the starting point and not both side
if ( isBinding && !geom.asPolyline().contains( points().first() ) )
if ( isBinding && !geom.asPolyline().contains( pts.constFirst() ) )
continue;

reshapeReturn = geom.reshapeGeometry( reshapeLineString );
Expand Down
118 changes: 98 additions & 20 deletions tests/src/app/testqgsmaptoolreshape.cpp
Expand Up @@ -14,61 +14,138 @@
***************************************************************************/

#include "qgstest.h"
#include "qgsapplication.h"

#include "qgisapp.h"
#include "qgsadvanceddigitizingdockwidget.h"
#include "qgsgeometry.h"
#include "qgsmapcanvas.h"
#include "qgsvectorlayer.h"
#include "qgslinestring.h"
#include "qgsmapcanvassnappingutils.h"
#include "qgssnappingconfig.h"
#include "qgssnappingutils.h"
#include "qgsmaptoolreshape.h"
#include "qgisapp.h"
#include "qgsproject.h"
#include "qgssettings.h"
#include "qgsvectorlayer.h"
#include "qgsmapmouseevent.h"
#include "testqgsmaptoolutils.h"


class TestQgsMapToolReshape : public QObject
/**
* \ingroup UnitTests
* This is a unit test for the vertex tool
*/
class TestQgsMapToolReshape: public QObject
{
Q_OBJECT
public:
TestQgsMapToolReshape() = default;
TestQgsMapToolReshape();

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 init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.

void testReshapeZ();
void reshapeWithBindingLine();

private:
QgisApp *mQgisApp = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QgsMapToolReshape *mCaptureTool = nullptr;
QgsVectorLayer *mLayerLineZ = nullptr;
};

TestQgsMapToolReshape::TestQgsMapToolReshape() = default;


//runs before all tests
void TestQgsMapToolReshape::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 QgsSettings environment
// Set up the QSettings environment
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );

QgsApplication::showSettings();
mQgisApp = new QgisApp();

// enforce C locale because the tests expect it
// (decimal separators / thousand separators)
QLocale::setDefault( QLocale::c() );
mCanvas = new QgsMapCanvas();

mQgisApp = new QgisApp();
mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3946" ) ) );

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
mLayerLineZ = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:3946" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) );
QVERIFY( mLayerLineZ->isValid() );
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerLineZ );

mLayerLineZ->startEditing();
QString wkt1 = "LineStringZ (0 0 0, 1 1 0, 1 2 0)";
QString wkt2 = "LineStringZ (2 1 5, 3 3 5)";
QgsFeature f1;
f1.setGeometry( QgsGeometry::fromWkt( wkt1 ) );
QgsFeature f2;
f2.setGeometry( QgsGeometry::fromWkt( wkt2 ) );

QgsFeatureList flist;
flist << f1 << f2;
mLayerLineZ->dataProvider()->addFeatures( flist );
QCOMPARE( mLayerLineZ->featureCount(), ( long )2 );
QCOMPARE( mLayerLineZ->getFeature( 1 ).geometry().asWkt(), wkt1 );
QCOMPARE( mLayerLineZ->getFeature( 2 ).geometry().asWkt(), wkt2 );

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<QgsMapLayer *>() << mLayerLineZ );
mCanvas->setCurrentLayer( mLayerLineZ );

// create the tool
mCaptureTool = new QgsMapToolReshape( mCanvas );
mCanvas->setMapTool( mCaptureTool );

QCOMPARE( mCanvas->mapSettings().outputSize(), QSize( 512, 512 ) );
QCOMPARE( mCanvas->mapSettings().visibleExtent(), QgsRectangle( 0, 0, 8, 8 ) );
}

//runs after all tests
void TestQgsMapToolReshape::cleanupTestCase()
{
delete mCaptureTool;
delete mCanvas;
QgsApplication::exitQgis();
}

void TestQgsMapToolReshape::init()
void TestQgsMapToolReshape::testReshapeZ()
{
}
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );

// test with default Z value = 333
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );

QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();

utils.mouseClick( 1, 2, Qt::LeftButton, Qt::KeyboardModifiers(), true );
utils.mouseClick( 2, 1, Qt::LeftButton, Qt::KeyboardModifiers(), true );
utils.mouseClick( 2, 1, Qt::RightButton );

QString wkt = "LineStringZ (0 0 0, 1 1 0, 1 2 0, 2 1 5)";
QCOMPARE( mLayerLineZ->getFeature( 1 ).geometry().asWkt(), wkt );

mLayerLineZ->undoStack()->undo();

void TestQgsMapToolReshape::cleanup()
{
}

void TestQgsMapToolReshape::reshapeWithBindingLine()
Expand Down Expand Up @@ -139,5 +216,6 @@ void TestQgsMapToolReshape::reshapeWithBindingLine()
vl->rollBack();
}


QGSTEST_MAIN( TestQgsMapToolReshape )
#include "testqgsmaptoolreshape.moc"

0 comments on commit edbb2fe

Please sign in to comment.