Skip to content

Commit

Permalink
Fix lockup when using rotate feature tool
Browse files Browse the repository at this point in the history
And add unit tests

Fixes #32123
  • Loading branch information
nyalldawson committed Nov 5, 2019
1 parent 9d61bf8 commit c1cfae2
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolrotatefeature.cpp
Expand Up @@ -398,7 +398,7 @@ void QgsMapToolRotateFeature::applyRotation( double rotation )
i = start;

QgsPointXY vertex = geom.vertexAt( i );
while ( vertex != QgsPointXY( 0, 0 ) )
while ( !vertex.isEmpty() )
{
double newX = a * vertex.x() + b * vertex.y() + c;
double newY = d * vertex.x() + ee * vertex.y() + f;
Expand Down
1 change: 1 addition & 0 deletions tests/src/app/CMakeLists.txt
Expand Up @@ -114,6 +114,7 @@ ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp)
ADD_QGIS_TEST(maptoollabel testqgsmaptoollabel.cpp)
ADD_QGIS_TEST(maptoolselect testqgsmaptoolselect.cpp)
ADD_QGIS_TEST(maptoolreshape testqgsmaptoolreshape.cpp)
ADD_QGIS_TEST(maptoolrotatefeature testqgsmaptoolrotatefeature.cpp)
ADD_QGIS_TEST(maptoolcircularstringtest testqgsmaptoolcircularstring.cpp)
ADD_QGIS_TEST(maptoolcircletest testqgsmaptoolcircle.cpp)
ADD_QGIS_TEST(maptoolmovefeaturetest testqgsmaptoolmovefeature.cpp)
Expand Down
145 changes: 145 additions & 0 deletions tests/src/app/testqgsmaptoolrotatefeature.cpp
@@ -0,0 +1,145 @@
/***************************************************************************
testqgsmaptoolrotatefeature.cpp
--------------------------------
Date : November 2019
Copyright : (C) 2019 by Nyall Dawson
Email : nyall dot dawson 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 "qgsgeometry.h"
#include "qgsmapcanvas.h"
#include "qgsmapcanvassnappingutils.h"
#include "qgssnappingconfig.h"
#include "qgssnappingutils.h"
#include "qgsmaptoolrotatefeature.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 TestQgsMapToolRotateFeature: public QObject
{
Q_OBJECT
public:
TestQgsMapToolRotateFeature();

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 testRotateFeature();

private:
QgisApp *mQgisApp = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QgsMapToolRotateFeature *mRotateTool = nullptr;
QgsVectorLayer *mLayerBase = nullptr;
};

TestQgsMapToolRotateFeature::TestQgsMapToolRotateFeature() = default;


//runs before all tests
void TestQgsMapToolRotateFeature::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: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
mLayerBase = new QgsVectorLayer( QStringLiteral( "Polygon?crs=EPSG:3946" ), QStringLiteral( "baselayer" ), QStringLiteral( "memory" ) );
QVERIFY( mLayerBase->isValid() );
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerBase );

mLayerBase->startEditing();
QString wkt1 = QStringLiteral( "Polygon ((0 0, 0 1, 1 1, 1 0))" );
QgsFeature f1;
f1.setGeometry( QgsGeometry::fromWkt( wkt1 ) );
QString wkt2 = QStringLiteral( "Polygon ((1.1 0, 1.1 5, 2.1 5, 2.1 0))" );
QgsFeature f2;
f2.setGeometry( QgsGeometry::fromWkt( wkt2 ) );

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

QgsSnappingConfig cfg = mCanvas->snappingUtils()->config();
cfg.setMode( QgsSnappingConfig::AllLayers );
cfg.setTolerance( 1 );
cfg.setType( QgsSnappingConfig::VertexAndSegment );
cfg.setEnabled( true );
mCanvas->snappingUtils()->setConfig( cfg );

mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerBase );
mCanvas->setCurrentLayer( mLayerBase );

// create the tool
mRotateTool = new QgsMapToolRotateFeature( mCanvas );
mCanvas->setMapTool( mRotateTool );

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

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

void TestQgsMapToolRotateFeature::testRotateFeature()
{
TestQgsMapToolUtils utils( mRotateTool );

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

QCOMPARE( mLayerBase->getFeature( 1 ).geometry().asWkt( 2 ), QStringLiteral( "Polygon ((0.72 -0.17, 0.28 1.17, 1.17 0.72, 0.72 -0.17))" ) );
QCOMPARE( mLayerBase->getFeature( 2 ).geometry().asWkt( 2 ), QStringLiteral( "Polygon ((1.1 0, 1.1 5, 2.1 5, 2.1 0))" ) );

mLayerBase->undoStack()->undo();
}


QGSTEST_MAIN( TestQgsMapToolRotateFeature )
#include "testqgsmaptoolrotatefeature.moc"
88 changes: 88 additions & 0 deletions tests/src/app/testqgsmaptoolutils.h
Expand Up @@ -22,6 +22,7 @@
#include "qgsmapcanvas.h"
#include "qgsmapmouseevent.h"
#include "qgssnappingutils.h"
#include "qgsmaptooladvanceddigitizing.h"

/**
* \ingroup UnitTests
Expand Down Expand Up @@ -108,3 +109,90 @@ class TestQgsMapToolAdvancedDigitizingUtils
private:
QgsMapToolAdvancedDigitizing *mMapTool = nullptr;
};

/**
* \ingroup UnitTests
*/
class TestQgsMapToolUtils
{
public:
TestQgsMapToolUtils( QgsMapTool *mapTool )
: mMapTool( mapTool )
{
}

QSet<QgsFeatureId> existingFeatureIds()
{
QSet<QgsFeatureId> fids;
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mMapTool->canvas()->currentLayer() );

if ( vl )
{
QgsFeature f;
QgsFeatureIterator it = vl->getFeatures();
while ( it.nextFeature( f ) )
fids << f.id();
}

return fids;
}

QgsFeatureId newFeatureId( QSet<QgsFeatureId> oldFids = QSet<QgsFeatureId>() )
{
QSet<QgsFeatureId> newFids = existingFeatureIds();
QSet<QgsFeatureId> diffFids = newFids.subtract( oldFids );
Q_ASSERT( diffFids.count() == 1 );
return *diffFids.constBegin();
}

QPoint mapToScreen( double mapX, double mapY )
{
QgsPointXY pt = mMapTool->canvas()->mapSettings().mapToPixel().transform( mapX, mapY );
return QPoint( std::round( pt.x() ), std::round( pt.y() ) );
}

void mouseMove( double mapX, double mapY )
{
QgsMapMouseEvent e( mMapTool->canvas(), QEvent::MouseMove, mapToScreen( mapX, mapY ) );
mMapTool->canvasMoveEvent( &e );
}

void mousePress( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
{
QgsMapMouseEvent e1( mMapTool->canvas(), QEvent::MouseButtonPress, mapToScreen( mapX, mapY ), button, button, stateKey );

if ( snap )
e1.snapPoint();

mMapTool->canvasPressEvent( &e1 );
}

void mouseRelease( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
{
QgsMapMouseEvent e2( mMapTool->canvas(), QEvent::MouseButtonRelease, mapToScreen( mapX, mapY ), button, Qt::MouseButton(), stateKey );

if ( snap )
e2.snapPoint();

mMapTool->canvasReleaseEvent( &e2 );
}

void mouseClick( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
{
mousePress( mapX, mapY, button, stateKey, snap );
mouseRelease( mapX, mapY, button, stateKey, snap );
}

void keyClick( int key )
{
QKeyEvent e1( QEvent::KeyPress, key, Qt::KeyboardModifiers() );
mMapTool->keyPressEvent( &e1 );

QKeyEvent e2( QEvent::KeyRelease, key, Qt::KeyboardModifiers() );
mMapTool->keyReleaseEvent( &e2 );
}

private:
QgsMapTool *mMapTool = nullptr;
};

0 comments on commit c1cfae2

Please sign in to comment.