Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[vertex editor] set extent of map canvas on the whole selection of nodes
  • Loading branch information
3nids committed Jun 7, 2018
1 parent e46f789 commit 59202ac
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/geometry/qgsrectangle.sip.in
Expand Up @@ -235,6 +235,13 @@ Expand the rectangle so that covers both the original rectangle and the given re
void combineExtentWith( double x, double y );
%Docstring
Expand the rectangle so that covers both the original rectangle and the given point.
%End

void combineExtentWith( const QgsPointXY &point );
%Docstring
Expand the rectangle so that covers both the original rectangle and the given point.

.. versionadded:: 3.2
%End

QgsRectangle operator-( QgsVector v ) const;
Expand Down
62 changes: 27 additions & 35 deletions src/app/vertextool/qgsvertexeditor.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgsvectorlayer.h"
#include "qgsgeometryutils.h"
#include "qgsproject.h"
#include "qgscoordinatetransform.h"

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

mSelectedFeature->deselectAllVertices();
Q_FOREACH ( const QModelIndex &index, mTableView->selectionModel()->selectedRows() )

QgsCoordinateTransform t( mLayer->crs(), mCanvas->mapSettings().destinationCrs(), QgsProject::instance() );
QgsRectangle *bbox = nullptr;
QModelIndexList indexList = selected.indexes();
for ( int i = 0; i < indexList.length(); ++i )
{
int vertexIdx = index.row();
int vertexIdx = indexList.at( i ).row();
mSelectedFeature->selectVertex( vertexIdx );
}

//ensure that newly selected vertex is visible in canvas
if ( !selected.indexes().isEmpty() )
{
int newRow = selected.indexes().first().row();
zoomToVertex( newRow );
// create a bounding box of selected vertices
QgsPointXY point( mSelectedFeature->vertexMap().at( vertexIdx )->point() );
if ( !bbox )
bbox = new QgsRectangle( point, point );
else
bbox->combineExtentWith( point );
}

mUpdatingVertexSelection = false;
}

void QgsVertexEditor::zoomToVertex( int idx )
{
double x = mSelectedFeature->vertexMap().at( idx )->point().x();
double y = mSelectedFeature->vertexMap().at( idx )->point().y();
QgsPointXY newCenter( x, y );

QgsCoordinateTransform t( mLayer->crs(), mCanvas->mapSettings().destinationCrs(), QgsProject::instance() );
QgsPointXY tCenter;
try
//ensure that newly selected vertices are visible in canvas
if ( bbox )
{
tCenter = t.transform( newCenter );
}
catch ( QgsCsException & )
{
return;
try
{
QgsRectangle transformedBbox = t.transform( *bbox );
QgsRectangle canvasExtent = mCanvas->mapSettings().extent();
transformedBbox.combineExtentWith( canvasExtent );
mCanvas->setExtent( transformedBbox );
}
catch ( QgsCsException & )
{
}
delete bbox;
}

QPolygonF ext = mCanvas->mapSettings().visiblePolygon();
//close polygon
ext.append( ext.first() );
QgsGeometry extGeom( QgsGeometry::fromQPolygonF( ext ) );
QgsGeometry vertexGeom( QgsGeometry::fromPointXY( tCenter ) );
if ( !vertexGeom.within( extGeom ) )
{
mCanvas->setCenter( tCenter );
mCanvas->refresh();
}
mUpdatingVertexSelection = false;
}

void QgsVertexEditor::keyPressEvent( QKeyEvent *e )
Expand Down
1 change: 0 additions & 1 deletion src/app/vertextool/qgsvertexeditor.h
Expand Up @@ -93,7 +93,6 @@ class QgsVertexEditor : public QgsDockWidget
private slots:
void updateTableSelection();
void updateVertexSelection( const QItemSelection &selected, const QItemSelection &deselected );
void zoomToVertex( int idx );

private:

Expand Down
5 changes: 5 additions & 0 deletions src/core/geometry/qgsrectangle.cpp
Expand Up @@ -245,6 +245,11 @@ void QgsRectangle::combineExtentWith( double x, double y )
}
}

void QgsRectangle::combineExtentWith( const QgsPointXY &point )
{
combineExtentWith( point.x(), point.y() );
}

QgsRectangle QgsRectangle::operator-( const QgsVector v ) const
{
double xmin = mXmin - v.x();
Expand Down
6 changes: 6 additions & 0 deletions src/core/geometry/qgsrectangle.h
Expand Up @@ -228,6 +228,12 @@ class CORE_EXPORT QgsRectangle
*/
void combineExtentWith( double x, double y );

/**
* Expand the rectangle so that covers both the original rectangle and the given point.
* \since QGIS 3.2
*/
void combineExtentWith( const QgsPointXY &point );

/**
* Returns a rectangle offset from this one in the direction of the reversed vector.
* \since QGIS 3.0
Expand Down

0 comments on commit 59202ac

Please sign in to comment.