Skip to content
Permalink
Browse files

[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 59202acc4507870be44e8cf66e8dabb02f7d5e75
@@ -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;
@@ -23,6 +23,7 @@
#include "qgsvectorlayer.h"
#include "qgsgeometryutils.h"
#include "qgsproject.h"
#include "qgscoordinatetransform.h"

#include <QTableWidget>
#include <QHeaderView>
@@ -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 )
@@ -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:

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

0 comments on commit 59202ac

Please sign in to comment.
You can’t perform that action at this time.