Skip to content

Commit

Permalink
Address review, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jun 4, 2024
1 parent 579bd2b commit bee64cd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,28 +285,28 @@ points that will not be part of a geometry being digitized.
%Docstring
Returns the vector layer within which construction guides are stored.

.. versionadded:: 3.38
.. versionadded:: 3.40
%End

bool showConstructionGuides() const;
%Docstring
Returns whether the construction guides are visible.

.. versionadded:: 3.38
.. versionadded:: 3.40
%End

bool snapToConstructionGuides() const;
%Docstring
Returns whether points should snap to construction guides.

.. versionadded:: 3.38
.. versionadded:: 3.40
%End

bool recordConstructionGuides() const;
%Docstring
Returns whether construction guides are being recorded.

.. versionadded:: 3.38
.. versionadded:: 3.40
%End

Qgis::BetweenLineConstraint betweenLineConstraint() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,28 +285,28 @@ points that will not be part of a geometry being digitized.
%Docstring
Returns the vector layer within which construction guides are stored.

.. versionadded:: 3.38
.. versionadded:: 3.40
%End

bool showConstructionGuides() const;
%Docstring
Returns whether the construction guides are visible.

.. versionadded:: 3.38
.. versionadded:: 3.40
%End

bool snapToConstructionGuides() const;
%Docstring
Returns whether points should snap to construction guides.

.. versionadded:: 3.38
.. versionadded:: 3.40
%End

bool recordConstructionGuides() const;
%Docstring
Returns whether construction guides are being recorded.

.. versionadded:: 3.38
.. versionadded:: 3.40
%End

Qgis::BetweenLineConstraint betweenLineConstraint() const;
Expand Down
10 changes: 3 additions & 7 deletions src/gui/qgsadvanceddigitizingcanvasitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ void QgsAdvancedDigitizingCanvasItem::paint( QPainter *painter )
painter->setPen( mConstructionGuidesPen );
while ( it.nextFeature( feature ) )
{
QgsPolylineXY line = feature.geometry().asPolyline();
QPolygonF polygon( line.size() );
for ( int i = 0; i < line.size(); i++ )
{
polygon[i] = toCanvasCoordinates( line[i] );
}

QgsGeometry geom = feature.geometry();
geom.mapToPixel( *mMapCanvas->getCoordinateTransform() );
const QPolygonF polygon = geom.asQPolygonF();
painter->drawPolyline( polygon );
}
}
Expand Down
29 changes: 14 additions & 15 deletions src/gui/qgsadvanceddigitizingdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ QgsAdvancedDigitizingDockWidget::QgsAdvancedDigitizingDockWidget( QgsMapCanvas *
// Construction modes
QMenu *constructionSettingsMenu = new QMenu( this );

mRecordConstructionGuides = new QAction( tr( "Record construction guides" ), constructionSettingsMenu );
mRecordConstructionGuides = new QAction( tr( "Record Construction Guides" ), constructionSettingsMenu );
mRecordConstructionGuides->setCheckable( true );
mRecordConstructionGuides->setChecked( settingsCadRecordConstructionGuides->value() );
constructionSettingsMenu->addAction( mRecordConstructionGuides );
connect( mRecordConstructionGuides, &QAction::triggered, this, [ = ]() { settingsCadRecordConstructionGuides->setValue( mRecordConstructionGuides->isChecked() ); } );

mShowConstructionGuides = new QAction( tr( "Show construction guides" ), constructionSettingsMenu );
mShowConstructionGuides = new QAction( tr( "Show Construction Guides" ), constructionSettingsMenu );
mShowConstructionGuides->setCheckable( true );
mShowConstructionGuides->setChecked( settingsCadShowConstructionGuides->value() );
constructionSettingsMenu->addAction( mShowConstructionGuides );
Expand All @@ -216,15 +216,15 @@ QgsAdvancedDigitizingDockWidget::QgsAdvancedDigitizingDockWidget( QgsMapCanvas *
updateCadPaintItem();
} );

mSnapToConstructionGuides = new QAction( tr( "Snap to visible construction guides" ), constructionSettingsMenu );
mSnapToConstructionGuides = new QAction( tr( "Snap to Visible Construction Guides" ), constructionSettingsMenu );
mSnapToConstructionGuides->setCheckable( true );
mSnapToConstructionGuides->setChecked( settingsCadSnapToConstructionGuides->value() );
constructionSettingsMenu->addAction( mSnapToConstructionGuides );
connect( mSnapToConstructionGuides, &QAction::triggered, this, [ = ]() { settingsCadSnapToConstructionGuides->setValue( mSnapToConstructionGuides->isChecked() ); } );

constructionSettingsMenu->addSeparator();

mClearConstructionGuides = new QAction( tr( "Clear construction guides" ), constructionSettingsMenu );
mClearConstructionGuides = new QAction( tr( "Clear Construction Guides" ), constructionSettingsMenu );
constructionSettingsMenu->addAction( mClearConstructionGuides );
connect( mClearConstructionGuides, &QAction::triggered, this, [ = ]()
{
Expand Down Expand Up @@ -1597,7 +1597,7 @@ void QgsAdvancedDigitizingDockWidget::keyPressEvent( QKeyEvent *e )

if ( mConstructionGuideLine.numPoints() >= 2 )
{
mConstructionGuidesLayer->deleteFeature( mConstructionGuideId );
mConstructionGuidesLayer->dataProvider()->deleteFeatures( QgsFeatureIds() << mConstructionGuideId );
mConstructionGuideLine.clear();
}

Expand Down Expand Up @@ -1656,7 +1656,7 @@ bool QgsAdvancedDigitizingDockWidget::filterKeyPress( QKeyEvent *e )
{
if ( mConstructionMode && mConstructionGuideLine.numPoints() >= 2 )
{
mConstructionGuidesLayer->deleteFeature( mConstructionGuideId );
mConstructionGuidesLayer->dataProvider()->deleteFeatures( QgsFeatureIds() << mConstructionGuideId );
mConstructionGuideLine.clear();

if ( mCadPointList.size() > 1 )
Expand Down Expand Up @@ -1957,7 +1957,7 @@ void QgsAdvancedDigitizingDockWidget::enable()
resetConstructionGuides();
}

if ( mDeferedUpdateConstructionGuidesCrs )
if ( mDeferredUpdateConstructionGuidesCrs )
{
updateConstructionGuidesCrs();
}
Expand Down Expand Up @@ -2020,13 +2020,13 @@ void QgsAdvancedDigitizingDockWidget::addPoint( const QgsPointXY &point )
QgsFeature feature;
QgsGeometry geom( mConstructionGuideLine.clone() );
feature.setGeometry( geom );
mConstructionGuidesLayer->addFeature( feature, QgsFeatureSink::FastInsert );
mConstructionGuidesLayer->dataProvider()->addFeature( feature );
mConstructionGuideId = feature.id();
}
else if ( mConstructionGuideLine.numPoints() > 2 )
{
QgsGeometry geom( mConstructionGuideLine.clone() );
mConstructionGuidesLayer->changeGeometry( mConstructionGuideId, geom );
mConstructionGuidesLayer->dataProvider()->changeGeometryValues( { { mConstructionGuideId, geom } } );
}
}
else
Expand All @@ -2036,7 +2036,7 @@ void QgsAdvancedDigitizingDockWidget::addPoint( const QgsPointXY &point )
mConstructionGuideLine.addVertex( pt );

QgsGeometry geom( mConstructionGuideLine.clone() );
mConstructionGuidesLayer->changeGeometry( mConstructionGuideId, geom );
mConstructionGuidesLayer->dataProvider()->changeGeometryValues( { { mConstructionGuideId, geom } } );
mConstructionGuideLine.clear();
}
}
Expand Down Expand Up @@ -2310,21 +2310,21 @@ void QgsAdvancedDigitizingDockWidget::updateConstructionGuidesCrs()

if ( !cadEnabled() )
{
mDeferedUpdateConstructionGuidesCrs = true;
mDeferredUpdateConstructionGuidesCrs = true;
}

QgsCoordinateTransform transform = QgsCoordinateTransform( mConstructionGuidesLayer->crs(), mMapCanvas->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
mConstructionGuidesLayer->setCrs( mMapCanvas->mapSettings().destinationCrs() );
QgsFeatureIterator it = mConstructionGuidesLayer->getFeatures();
QgsFeatureIterator it = mConstructionGuidesLayer->getFeatures( QgsFeatureRequest().setNoAttributes() );
QgsFeature feature;
while ( it.nextFeature( feature ) )
{
QgsGeometry geom = feature.geometry();
geom.transform( transform );
mConstructionGuidesLayer->changeGeometry( feature.id(), geom );
mConstructionGuidesLayer->dataProvider()->changeGeometryValues( { { feature.id(), geom } } );
}

mDeferedUpdateConstructionGuidesCrs = false;
mDeferredUpdateConstructionGuidesCrs = false;
}

void QgsAdvancedDigitizingDockWidget::resetConstructionGuides()
Expand All @@ -2339,5 +2339,4 @@ void QgsAdvancedDigitizingDockWidget::resetConstructionGuides()
QStringLiteral( "constructionGuides" ),
QStringLiteral( "memory" ),
options );
mConstructionGuidesLayer->startEditing();
}
10 changes: 5 additions & 5 deletions src/gui/qgsadvanceddigitizingdockwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,25 +313,25 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private

/**
* Returns the vector layer within which construction guides are stored.
* \since QGIS 3.38
* \since QGIS 3.40
*/
QgsVectorLayer *constructionGuidesLayer() const { return mConstructionGuidesLayer.get(); }

/**
* Returns whether the construction guides are visible.
* \since QGIS 3.38
* \since QGIS 3.40
*/
bool showConstructionGuides() const;

/**
* Returns whether points should snap to construction guides.
* \since QGIS 3.38
* \since QGIS 3.40
*/
bool snapToConstructionGuides() const;

/**
* Returns whether construction guides are being recorded.
* \since QGIS 3.38
* \since QGIS 3.40
*/
bool recordConstructionGuides() const;

Expand Down Expand Up @@ -1100,7 +1100,7 @@ class GUI_EXPORT QgsAdvancedDigitizingDockWidget : public QgsDockWidget, private
std::unique_ptr<QgsVectorLayer> mConstructionGuidesLayer;
QgsFeatureId mConstructionGuideId;
QgsLineString mConstructionGuideLine;
bool mDeferedUpdateConstructionGuidesCrs = false;
bool mDeferredUpdateConstructionGuidesCrs = false;

// Error message
std::unique_ptr<QgsMessageBarItem> mErrorMessage;
Expand Down
52 changes: 52 additions & 0 deletions tests/src/app/testqgsadvanceddigitizing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class TestQgsAdvancedDigitizing: public QObject

void releaseLockAfterDisable();

void constructionGuides();

private:
TestQgsMapToolAdvancedDigitizingUtils getMapToolDigitizingUtils( QgsVectorLayer *layer );
QString getWktFromLastAddedFeature( TestQgsMapToolAdvancedDigitizingUtils utils, QSet<QgsFeatureId> oldFeatures );
Expand Down Expand Up @@ -1163,5 +1165,55 @@ void TestQgsAdvancedDigitizing::releaseLockAfterDisable()
mAdvancedDigitizingDockWidget->enableAction()->trigger();
}

void TestQgsAdvancedDigitizing::constructionGuides()
{
auto utils = getMapToolDigitizingUtils( mLayer3950 );

QVERIFY( mAdvancedDigitizingDockWidget->cadEnabled() );

QCOMPARE( mAdvancedDigitizingDockWidget->constructionGuidesLayer()->featureCount(), 0 );

mAdvancedDigitizingDockWidget->mRecordConstructionGuides->setChecked( true );
mAdvancedDigitizingDockWidget->setConstructionMode( true );

// enter a few construction steps while guide recording is on
utils.mouseClick( 10, 10, Qt::LeftButton );
utils.mouseClick( 10, 11, Qt::LeftButton );
utils.mouseClick( 10, 12, Qt::LeftButton );
utils.mouseClick( 10, 13, Qt::LeftButton );
utils.mouseClick( 10, 14, Qt::LeftButton );
utils.mouseClick( 20, 01, Qt::RightButton );

QCOMPARE( mAdvancedDigitizingDockWidget->constructionGuidesLayer()->featureCount(), 1 );

mAdvancedDigitizingDockWidget->mRecordConstructionGuides->setChecked( false );

// enter a few construction steps while guide recording is off
utils.mouseClick( 10, 10, Qt::LeftButton );
utils.mouseClick( 10, 11, Qt::LeftButton );
utils.mouseClick( 10, 12, Qt::LeftButton );
utils.mouseClick( 10, 13, Qt::LeftButton );
utils.mouseClick( 10, 14, Qt::LeftButton );
utils.mouseClick( 20, 01, Qt::RightButton );

QCOMPARE( mAdvancedDigitizingDockWidget->constructionGuidesLayer()->featureCount(), 1 );

QgsSnappingConfig snapConfig = mCanvas->snappingUtils()->config();
snapConfig.setEnabled( true );
mCanvas->snappingUtils()->setConfig( snapConfig );

mAdvancedDigitizingDockWidget->mSnapToConstructionGuides->setChecked( true );

// snap on an existing constructio guide vertex
utils.mouseMove( 10.1, 10 );
QCOMPARE( mAdvancedDigitizingDockWidget->currentPointV2(), QgsPoint( 10, 10 ) );

mAdvancedDigitizingDockWidget->mSnapToConstructionGuides->setChecked( false );

// do not snap on an existing construction guide vertex
utils.mouseMove( 10.5, 14.5 );
QGSCOMPARENEARPOINT( mAdvancedDigitizingDockWidget->currentPointV2(), QgsPoint( 10.5, 14.5 ), 0.1 );
}

QGSTEST_MAIN( TestQgsAdvancedDigitizing )
#include "testqgsadvanceddigitizing.moc"

0 comments on commit bee64cd

Please sign in to comment.