Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
clean tests and add a new test for snapping on inivisble feature
  • Loading branch information
lbartoletti committed Apr 30, 2018
1 parent 68f463e commit 9f3d571
Showing 1 changed file with 74 additions and 24 deletions.
98 changes: 74 additions & 24 deletions tests/src/core/testqgssnappingutils.cpp
Expand Up @@ -26,6 +26,8 @@
#include "qgssnappingconfig.h"
#include "qgscategorizedsymbolrenderer.h"
#include "qgssettings.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"

struct FilterExcludePoint : public QgsPointLocator::MatchFilter
{
Expand Down Expand Up @@ -75,19 +77,13 @@ class TestQgsSnappingUtils : public QObject
QgsGeometry polygonGeom = QgsGeometry::fromPolygonXY( polygon );
f1.setGeometry( polygonGeom );
f1.setAttribute( idx, QVariant( 2 ) );

polyline << QgsPointXY( 10, 11 ) << QgsPointXY( 11, 10 ) << QgsPointXY( 11, 11 ) << QgsPointXY( 10, 11 );
polygon << polyline;
polygonGeom = QgsGeometry::fromPolygonXY( polygon );
f2.setGeometry( polygonGeom );
f2.setAttribute( idx, QVariant( 20 ) );
QgsFeatureList flist;
flist << f1 << f2;

flist << f1;

mVL->dataProvider()->addFeatures( flist );

QgsProject::instance()->addMapLayer( mVL );

}

void cleanupTestCase()
Expand All @@ -97,21 +93,13 @@ class TestQgsSnappingUtils : public QObject

void testSnapModeCurrent()
{
QgsSymbol *s1 = QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry );
QgsSymbol *s2 = QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry );
QgsRendererCategory c1( 2, s1, "f1", true );
QgsRendererCategory c2( 20, s2, "f2", false );
QgsCategoryList cl;
cl << c1 << c2;

QgsMapSettings mapSettings;
mapSettings.setOutputSize( QSize( 100, 100 ) );
mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) );
QVERIFY( mapSettings.hasValidSettings() );

QgsSnappingUtils u;
u.setMapSettings( mapSettings );
u.setEnableSnappingForInvisibleFeature( false );
u.setCurrentLayer( mVL );

// first try with no snapping enabled
Expand All @@ -122,7 +110,7 @@ class TestQgsSnappingUtils : public QObject
snappingConfig.setMode( QgsSnappingConfig::ActiveLayer );
u.setConfig( snappingConfig );

QgsPointLocator::Match m0 = u.snapToMap( QPoint( 2, 2 ) );
QgsPointLocator::Match m0 = u.snapToMap( QPoint( 100, 100 ) );
QVERIFY( !m0.isValid() );
QVERIFY( !m0.hasVertex() );

Expand All @@ -131,15 +119,10 @@ class TestQgsSnappingUtils : public QObject
snappingConfig.setType( QgsSnappingConfig::Vertex );
u.setConfig( snappingConfig );

QgsPointLocator::Match m = u.snapToMap( QPoint( 11, 11 ) );
QVERIFY( !m.isValid() );
QVERIFY( !m.hasVertex() );

u.setEnableSnappingForInvisibleFeature( true );
m = u.snapToMap( QPoint( 2, 2 ) );
QgsPointLocator::Match m = u.snapToMap( QPoint( 100, 100 ) );
QVERIFY( m.isValid() );
QVERIFY( m.hasVertex() );
QCOMPARE( m.point(), QgsPointXY( 0, 1 ) );
QCOMPARE( m.point(), QgsPointXY( 1, 0 ) );

QgsPointLocator::Match m2 = u.snapToMap( QPoint( 0, 100 ) );
QVERIFY( !m2.isValid() );
Expand All @@ -155,8 +138,75 @@ class TestQgsSnappingUtils : public QObject
FilterExcludePoint myFilter( QgsPointXY( 1, 0 ) );
QgsPointLocator::Match m3 = u.snapToMap( QPoint( 100, 100 ), &myFilter );
QVERIFY( !m3.isValid() );
}

void testSnapInvisible()
{
QgsCategorizedSymbolRenderer *renderer = new QgsCategorizedSymbolRenderer();
renderer->setClassAttribute( QStringLiteral( "fld" ) );
renderer->setSourceSymbol( QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry ) );
renderer->addCategory( QgsRendererCategory( "2", QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry ), QStringLiteral( "2" ) ) );
mVL->setRenderer( renderer );

//create legend with symbology nodes for categorized renderer
QgsLayerTree *root = new QgsLayerTree();
QgsLayerTreeLayer *n = new QgsLayerTreeLayer( mVL );
root->addChildNode( n );
QgsLayerTreeModel *m = new QgsLayerTreeModel( root, nullptr );
m->refreshLayerLegend( n );

//test that all nodes are initially checked
QList<QgsLayerTreeModelLegendNode *> nodes = m->layerLegendNodes( n );
QCOMPARE( nodes.length(), 1 );
Q_FOREACH ( QgsLayerTreeModelLegendNode *ln, nodes )
{
QVERIFY( ln->data( Qt::CheckStateRole ) == Qt::Checked );
}


QgsMapSettings mapSettings;
mapSettings.setOutputSize( QSize( 100, 100 ) );
mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) );
QVERIFY( mapSettings.hasValidSettings() );

QgsSnappingUtils u;
u.setMapSettings( mapSettings );
u.setEnableSnappingForInvisibleFeature( false );
u.setCurrentLayer( mVL );

// first try with no snapping enabled
QgsSnappingConfig snappingConfig = u.config();
snappingConfig.setEnabled( false );
snappingConfig.setTolerance( 10 );
snappingConfig.setUnits( QgsTolerance::Pixels );
snappingConfig.setMode( QgsSnappingConfig::ActiveLayer );
u.setConfig( snappingConfig );

QgsPointLocator::Match m0 = u.snapToMap( QPoint( 2, 2 ) );
QVERIFY( !m0.isValid() );
QVERIFY( !m0.hasVertex() );

// now enable snapping
snappingConfig.setEnabled( true );
snappingConfig.setType( QgsSnappingConfig::Vertex );
u.setConfig( snappingConfig );

QgsPointLocator::Match m5 = u.snapToMap( QPoint( 2, 2 ) );
QVERIFY( m5.isValid() );
QVERIFY( m5.hasVertex() );
QCOMPARE( m5.point(), QgsPointXY( 0, 1 ) );

//uncheck all and test that all nodes are unchecked
static_cast< QgsSymbolLegendNode * >( nodes.at( 0 ) )->uncheckAllItems();
Q_FOREACH ( QgsLayerTreeModelLegendNode *ln, nodes )
{
QVERIFY( ln->data( Qt::CheckStateRole ) == Qt::Unchecked );
}
mVL->dataChanged(); /* refresh index */

m5 = u.snapToMap( QPoint( 2, 2 ) );
QVERIFY( !m5.isValid() );
QVERIFY( !m5.hasVertex() );
}

void testSnapModeAll()
Expand Down

0 comments on commit 9f3d571

Please sign in to comment.