Skip to content

Commit 59202ac

Browse files
committed
[vertex editor] set extent of map canvas on the whole selection of nodes
1 parent e46f789 commit 59202ac

File tree

5 files changed

+45
-36
lines changed

5 files changed

+45
-36
lines changed

python/core/auto_generated/geometry/qgsrectangle.sip.in

+7
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ Expand the rectangle so that covers both the original rectangle and the given re
235235
void combineExtentWith( double x, double y );
236236
%Docstring
237237
Expand the rectangle so that covers both the original rectangle and the given point.
238+
%End
239+
240+
void combineExtentWith( const QgsPointXY &point );
241+
%Docstring
242+
Expand the rectangle so that covers both the original rectangle and the given point.
243+
244+
.. versionadded:: 3.2
238245
%End
239246

240247
QgsRectangle operator-( QgsVector v ) const;

src/app/vertextool/qgsvertexeditor.cpp

+27-35
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsvectorlayer.h"
2424
#include "qgsgeometryutils.h"
2525
#include "qgsproject.h"
26+
#include "qgscoordinatetransform.h"
2627

2728
#include <QTableWidget>
2829
#include <QHeaderView>
@@ -359,49 +360,40 @@ void QgsVertexEditor::updateVertexSelection( const QItemSelection &selected, con
359360
mUpdatingVertexSelection = true;
360361

361362
mSelectedFeature->deselectAllVertices();
362-
Q_FOREACH ( const QModelIndex &index, mTableView->selectionModel()->selectedRows() )
363+
364+
QgsCoordinateTransform t( mLayer->crs(), mCanvas->mapSettings().destinationCrs(), QgsProject::instance() );
365+
QgsRectangle *bbox = nullptr;
366+
QModelIndexList indexList = selected.indexes();
367+
for ( int i = 0; i < indexList.length(); ++i )
363368
{
364-
int vertexIdx = index.row();
369+
int vertexIdx = indexList.at( i ).row();
365370
mSelectedFeature->selectVertex( vertexIdx );
366-
}
367371

368-
//ensure that newly selected vertex is visible in canvas
369-
if ( !selected.indexes().isEmpty() )
370-
{
371-
int newRow = selected.indexes().first().row();
372-
zoomToVertex( newRow );
372+
// create a bounding box of selected vertices
373+
QgsPointXY point( mSelectedFeature->vertexMap().at( vertexIdx )->point() );
374+
if ( !bbox )
375+
bbox = new QgsRectangle( point, point );
376+
else
377+
bbox->combineExtentWith( point );
373378
}
374379

375-
mUpdatingVertexSelection = false;
376-
}
377-
378-
void QgsVertexEditor::zoomToVertex( int idx )
379-
{
380-
double x = mSelectedFeature->vertexMap().at( idx )->point().x();
381-
double y = mSelectedFeature->vertexMap().at( idx )->point().y();
382-
QgsPointXY newCenter( x, y );
383-
384-
QgsCoordinateTransform t( mLayer->crs(), mCanvas->mapSettings().destinationCrs(), QgsProject::instance() );
385-
QgsPointXY tCenter;
386-
try
380+
//ensure that newly selected vertices are visible in canvas
381+
if ( bbox )
387382
{
388-
tCenter = t.transform( newCenter );
389-
}
390-
catch ( QgsCsException & )
391-
{
392-
return;
383+
try
384+
{
385+
QgsRectangle transformedBbox = t.transform( *bbox );
386+
QgsRectangle canvasExtent = mCanvas->mapSettings().extent();
387+
transformedBbox.combineExtentWith( canvasExtent );
388+
mCanvas->setExtent( transformedBbox );
389+
}
390+
catch ( QgsCsException & )
391+
{
392+
}
393+
delete bbox;
393394
}
394395

395-
QPolygonF ext = mCanvas->mapSettings().visiblePolygon();
396-
//close polygon
397-
ext.append( ext.first() );
398-
QgsGeometry extGeom( QgsGeometry::fromQPolygonF( ext ) );
399-
QgsGeometry vertexGeom( QgsGeometry::fromPointXY( tCenter ) );
400-
if ( !vertexGeom.within( extGeom ) )
401-
{
402-
mCanvas->setCenter( tCenter );
403-
mCanvas->refresh();
404-
}
396+
mUpdatingVertexSelection = false;
405397
}
406398

407399
void QgsVertexEditor::keyPressEvent( QKeyEvent *e )

src/app/vertextool/qgsvertexeditor.h

-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ class QgsVertexEditor : public QgsDockWidget
9393
private slots:
9494
void updateTableSelection();
9595
void updateVertexSelection( const QItemSelection &selected, const QItemSelection &deselected );
96-
void zoomToVertex( int idx );
9796

9897
private:
9998

src/core/geometry/qgsrectangle.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ void QgsRectangle::combineExtentWith( double x, double y )
245245
}
246246
}
247247

248+
void QgsRectangle::combineExtentWith( const QgsPointXY &point )
249+
{
250+
combineExtentWith( point.x(), point.y() );
251+
}
252+
248253
QgsRectangle QgsRectangle::operator-( const QgsVector v ) const
249254
{
250255
double xmin = mXmin - v.x();

src/core/geometry/qgsrectangle.h

+6
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ class CORE_EXPORT QgsRectangle
228228
*/
229229
void combineExtentWith( double x, double y );
230230

231+
/**
232+
* Expand the rectangle so that covers both the original rectangle and the given point.
233+
* \since QGIS 3.2
234+
*/
235+
void combineExtentWith( const QgsPointXY &point );
236+
231237
/**
232238
* Returns a rectangle offset from this one in the direction of the reversed vector.
233239
* \since QGIS 3.0

0 commit comments

Comments
 (0)