Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport 55371 to release 3 34 test #148

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/gui/qgsmaptoolcapture.cpp
Original file line number Diff line number Diff line change
@@ -1391,6 +1391,27 @@ void QgsMapToolCapture::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
}
else
{

//does compoundcurve contain circular strings?
//does provider support circular strings?
if ( QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer() ) )
{
const bool hasCurvedSegments = captureCurve()->hasCurvedSegments();
const bool providerSupportsCurvedSegments = vlayer->dataProvider()->capabilities() & QgsVectorDataProvider::CircularGeometries;

if ( hasCurvedSegments && providerSupportsCurvedSegments )
{
curveToAdd = captureCurve()->clone();
}
else
{
curveToAdd = captureCurve()->curveToLine();
}
}
else
{
curveToAdd = captureCurve()->clone();
}
QgsCurvePolygon *poly = new QgsCurvePolygon();
poly->setExteriorRing( curveToAdd );
g = QgsGeometry( poly );
1 change: 1 addition & 0 deletions tests/src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ set(TESTS
testqgslayerpropertiesdialogs.cpp
testqgsmapcanvasdockwidget.cpp
testqgsmaptooladdpart.cpp
testqgsmaptooladdring.cpp
testqgsmaptooleditannotation.cpp
testqgsmaptoolidentifyaction.cpp
testqgsmaptoollabel.cpp
167 changes: 167 additions & 0 deletions tests/src/app/testqgsmaptooladdring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/***************************************************************************
testqgsmaptooladdring.cpp
--------------------------------
Date : 2023-11-22
Copyright : (C) 2023 by Loïc Bartoletti
Email : loic dot bartoletti at oslandia 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 "qgsmaptooladdring.h"
#include "qgsproject.h"
#include "qgssettingsregistrycore.h"
#include "qgsvectorlayer.h"
#include "qgsmapmouseevent.h"
#include "testqgsmaptoolutils.h"


/**
* \ingroup UnitTests
* This is a unit test for the add ring map tool
*/
class TestQgsMapToolAddRing: public QObject
{
Q_OBJECT
public:
TestQgsMapToolAddRing();

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

private:
QPoint mapToPoint( double x, double y );

QgisApp *mQgisApp = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QgsMapToolAddRing *mCaptureTool = nullptr;
QgsVectorLayer *mLayerMultiPolygon = nullptr;
};

TestQgsMapToolAddRing::TestQgsMapToolAddRing() = default;


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

mLayerMultiPolygon->startEditing();
QgsFeature f;
const QString wkt( "MultiPolygon (((0 0, 5 0, 5 5, 0 5, 0 0)))" );
f.setGeometry( QgsGeometry::fromWkt( wkt ) );
mLayerMultiPolygon->dataProvider()->addFeatures( QgsFeatureList() << f );
QCOMPARE( mLayerMultiPolygon->featureCount(), ( long )1 );
QCOMPARE( mLayerMultiPolygon->getFeature( 1 ).geometry().asWkt(), wkt );

mCanvas->setCurrentLayer( mLayerMultiPolygon );

// create the tool
mCaptureTool = new QgsMapToolAddRing( 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 TestQgsMapToolAddRing::cleanupTestCase()
{
delete mCaptureTool;
delete mCanvas;
QgsApplication::exitQgis();
}

QPoint TestQgsMapToolAddRing::mapToPoint( double x, double y )
{
const QgsPointXY mapPoint = mCanvas->mapSettings().mapToPixel().transform( x, y );

return QPoint( static_cast<int>( std::round( mapPoint.x() ) ), static_cast<int>( std::round( mapPoint.y() ) ) );
}

void TestQgsMapToolAddRing::testAddRing()
{
mLayerMultiPolygon->select( 1 );

std::unique_ptr< QgsMapMouseEvent > event( new QgsMapMouseEvent(
mCanvas,
QEvent::MouseButtonRelease,
mapToPoint( 1, 1 ),
Qt::LeftButton
) );
mCaptureTool->cadCanvasReleaseEvent( event.get() );

event.reset( new QgsMapMouseEvent(
mCanvas,
QEvent::MouseButtonRelease,
mapToPoint( 1, 2 ),
Qt::LeftButton
) );
mCaptureTool->cadCanvasReleaseEvent( event.get() );

event.reset( new QgsMapMouseEvent(
mCanvas,
QEvent::MouseButtonRelease,
mapToPoint( 2, 2 ),
Qt::LeftButton
) );
mCaptureTool->cadCanvasReleaseEvent( event.get() );

event.reset( new QgsMapMouseEvent(
mCanvas,
QEvent::MouseButtonRelease,
mapToPoint( 2, 1 ),
Qt::LeftButton
) );
mCaptureTool->cadCanvasReleaseEvent( event.get() );

event.reset( new QgsMapMouseEvent(
mCanvas,
QEvent::MouseButtonRelease,
mapToPoint( 1, 1 ),
Qt::RightButton
) );
mCaptureTool->cadCanvasReleaseEvent( event.get() );

const QString wkt = "MultiPolygon (((0 0, 5 0, 5 5, 0 5, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1)))";
QCOMPARE( mLayerMultiPolygon->getFeature( 1 ).geometry().asWkt(), wkt );
}
QGSTEST_MAIN( TestQgsMapToolAddRing )
#include "testqgsmaptooladdring.moc"