Skip to content
Permalink
Browse files

Merge pull request #7199 from 3nids/vertex_tool

vertex tool fixes
  • Loading branch information
3nids committed Jun 12, 2018
2 parents f4065d8 + 5cb86be commit e899849defcdbd04ceae7d32ba0d25809bef839a
@@ -229,12 +229,19 @@ Returns true when rectangle contains a point.

void combineExtentWith( const QgsRectangle &rect );
%Docstring
Expand the rectangle so that covers both the original rectangle and the given rectangle.
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
%End

void combineExtentWith( double x, double y );
%Docstring
Expand the rectangle so that covers both the original rectangle and the given point.
Expands the rectangle so that it covers both the original rectangle and the given point.
%End

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

.. versionadded:: 3.2
%End

QgsRectangle operator-( QgsVector v ) const;
@@ -60,7 +60,7 @@ QgsSelectedFeature::~QgsSelectedFeature()

void QgsSelectedFeature::currentLayerChanged( QgsMapLayer *layer )
{
if ( layer == mVlayer )
if ( layer == mLayer )
deleteLater();
}

@@ -71,7 +71,7 @@ void QgsSelectedFeature::updateGeometry( const QgsGeometry *geom )
if ( !geom )
{
QgsFeature f;
mVlayer->getFeatures( QgsFeatureRequest().setFilterFid( mFeatureId ) ).nextFeature( f );
mLayer->getFeatures( QgsFeatureRequest().setFilterFid( mFeatureId ) ).nextFeature( f );
if ( f.hasGeometry() )
mGeometry = new QgsGeometry( f.geometry() );
else
@@ -83,10 +83,10 @@ void QgsSelectedFeature::updateGeometry( const QgsGeometry *geom )
}
}

void QgsSelectedFeature::setSelectedFeature( QgsFeatureId featureId, QgsVectorLayer *vlayer, QgsMapCanvas *canvas )
void QgsSelectedFeature::setSelectedFeature( QgsFeatureId featureId, QgsVectorLayer *layer, QgsMapCanvas *canvas )
{
mFeatureId = featureId;
mVlayer = vlayer;
mLayer = layer;
mCanvas = canvas;

delete mGeometry;
@@ -96,24 +96,24 @@ void QgsSelectedFeature::setSelectedFeature( QgsFeatureId featureId, QgsVectorLa
connect( QgisApp::instance()->layerTreeView(), &QgsLayerTreeView::currentLayerChanged, this, &QgsSelectedFeature::currentLayerChanged );

// feature was deleted
connect( mVlayer, &QgsVectorLayer::featureDeleted, this, &QgsSelectedFeature::featureDeleted );
connect( mLayer, &QgsVectorLayer::featureDeleted, this, &QgsSelectedFeature::featureDeleted );

// rolling back
connect( mVlayer, &QgsVectorLayer::beforeRollBack, this, &QgsSelectedFeature::beforeRollBack );
connect( mLayer, &QgsVectorLayer::beforeRollBack, this, &QgsSelectedFeature::beforeRollBack );

// projection or extents changed
connect( canvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsSelectedFeature::updateVertexMarkersPosition );
connect( canvas, &QgsMapCanvas::extentsChanged, this, &QgsSelectedFeature::updateVertexMarkersPosition );

// geometry was changed
connect( mVlayer, &QgsVectorLayer::geometryChanged, this, &QgsSelectedFeature::geometryChanged );
connect( mLayer, &QgsVectorLayer::geometryChanged, this, &QgsSelectedFeature::geometryChanged );

replaceVertexMap();
}

void QgsSelectedFeature::beforeRollBack()
{
disconnect( mVlayer, &QgsVectorLayer::geometryChanged, this, &QgsSelectedFeature::geometryChanged );
disconnect( mLayer, &QgsVectorLayer::geometryChanged, this, &QgsSelectedFeature::geometryChanged );
deleteVertexMap();
}

@@ -122,15 +122,15 @@ void QgsSelectedFeature::beginGeometryChange()
Q_ASSERT( !mChangingGeometry );
mChangingGeometry = true;

disconnect( mVlayer, &QgsVectorLayer::geometryChanged, this, &QgsSelectedFeature::geometryChanged );
disconnect( mLayer, &QgsVectorLayer::geometryChanged, this, &QgsSelectedFeature::geometryChanged );
}

void QgsSelectedFeature::endGeometryChange()
{
Q_ASSERT( mChangingGeometry );
mChangingGeometry = false;

connect( mVlayer, &QgsVectorLayer::geometryChanged, this, &QgsSelectedFeature::geometryChanged );
connect( mLayer, &QgsVectorLayer::geometryChanged, this, &QgsSelectedFeature::geometryChanged );
}

void QgsSelectedFeature::canvasLayersChanged()
@@ -148,7 +148,7 @@ void QgsSelectedFeature::geometryChanged( QgsFeatureId fid, const QgsGeometry &g
{
QgsDebugCall;

if ( !mVlayer || fid != mFeatureId )
if ( !mLayer || fid != mFeatureId )
return;

updateGeometry( &geom );
@@ -205,7 +205,7 @@ void QgsSelectedFeature::addError( QgsGeometry::Error e )
if ( e.hasWhere() )
{
QgsVertexMarker *marker = new QgsVertexMarker( mCanvas );
marker->setCenter( mCanvas->mapSettings().layerToMapCoordinates( mVlayer, e.where() ) );
marker->setCenter( mCanvas->mapSettings().layerToMapCoordinates( mLayer, e.where() ) );
marker->setIconType( QgsVertexMarker::ICON_X );
marker->setColor( Qt::green );
marker->setZValue( marker->zValue() + 1 );
@@ -284,7 +284,7 @@ void QgsSelectedFeature::createVertexMap()
QgsPoint pt;
while ( geom->nextVertex( vertexId, pt ) )
{
mVertexMap.append( new QgsVertexEntry( mCanvas, mVlayer, pt, vertexId, tr( "ring %1, vertex %2" ).arg( vertexId.ring ).arg( vertexId.vertex ) ) );
mVertexMap.append( new QgsVertexEntry( mCanvas, mLayer, pt, vertexId, tr( "ring %1, vertex %2" ).arg( vertexId.ring ).arg( vertexId.vertex ) ) );
}
}

@@ -363,7 +363,7 @@ QList<QgsVertexEntry *> &QgsSelectedFeature::vertexMap()
return mVertexMap;
}

QgsVectorLayer *QgsSelectedFeature::vlayer()
QgsVectorLayer *QgsSelectedFeature::layer()
{
return mVlayer;
return mLayer;
}
@@ -52,7 +52,7 @@ class QgsSelectedFeature: public QObject
* \param vlayer vector layer in which feature is selected
* \param canvas mapCanvas on which we are working
*/
void setSelectedFeature( QgsFeatureId featureId, QgsVectorLayer *vlayer, QgsMapCanvas *canvas );
void setSelectedFeature( QgsFeatureId featureId, QgsVectorLayer *layer, QgsMapCanvas *canvas );

/**
* Function to select vertex with number
@@ -111,7 +111,7 @@ class QgsSelectedFeature: public QObject
* Gets the layer of the selected feature
* \returns used vector layer
*/
QgsVectorLayer *vlayer();
QgsVectorLayer *layer();

/**
* Getter for the current geometry
@@ -193,7 +193,7 @@ class QgsSelectedFeature: public QObject
QgsGeometry *mGeometry = nullptr;
bool mFeatureSelected;
bool mChangingGeometry;
QgsVectorLayer *mVlayer = nullptr;
QgsVectorLayer *mLayer = nullptr;
QList<QgsVertexEntry *> mVertexMap;
QgsMapCanvas *mCanvas = nullptr;

@@ -18,11 +18,13 @@

#include "qgsvertexeditor.h"
#include "qgsmapcanvas.h"
#include "qgsmessagelog.h"
#include "qgsselectedfeature.h"
#include "qgsvertexentry.h"
#include "qgsvectorlayer.h"
#include "qgsgeometryutils.h"
#include "qgsproject.h"
#include "qgscoordinatetransform.h"

#include <QTableWidget>
#include <QHeaderView>
@@ -107,11 +109,21 @@ QVariant QgsVertexEditorModel::data( const QModelIndex &index, int role ) const
{
double r = 0;
double minRadius = 0;
QFont font = mWidgetFont;
bool fontChanged = false;
if ( vertex->isSelected() )
{
font.setBold( true );
fontChanged = true;
}
if ( calcR( index.row(), r, minRadius ) )
{
QFont curvePointFont = mWidgetFont;
curvePointFont.setItalic( true );
return curvePointFont;
font.setItalic( true );
fontChanged = true;;
}
if ( fontChanged )
{
return font;
}
else
{
@@ -293,8 +305,6 @@ QgsVertexEditor::QgsVertexEditor(

setWidget( mTableView );

connect( mTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVertexEditor::updateVertexSelection );

updateEditor( layer, selectedFeature );
}

@@ -311,6 +321,7 @@ void QgsVertexEditor::updateEditor( QgsVectorLayer *layer, QgsSelectedFeature *s
// TODO We really should just update the model itself.
mVertexModel = new QgsVertexEditorModel( mLayer, mSelectedFeature, mCanvas, this );
mTableView->setModel( mVertexModel );
connect( mTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsVertexEditor::updateVertexSelection );

connect( mSelectedFeature, &QgsSelectedFeature::selectionChanged, this, &QgsVertexEditor::updateTableSelection );
}
@@ -350,49 +361,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() );
std::unique_ptr<QgsRectangle> bbox;
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.reset( 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 &cse )
{
QgsMessageLog::logMessage( QObject::tr( "Simplify transform error caught: %1" ).arg( cse.what() ), QObject::tr( "CRS" ) );
}
}

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:

@@ -1036,6 +1036,13 @@ void QgsVertexTool::showVertexEditor() //#spellok
return;

mSelectedFeature.reset( new QgsSelectedFeature( m.featureId(), m.layer(), mCanvas ) );
for ( int i = 0; i < mSelectedVertices.length(); ++i )
{
if ( mSelectedVertices.at( i ).layer == m.layer() && mSelectedVertices.at( i ).fid == m.featureId() )
{
mSelectedFeature->selectVertex( mSelectedVertices.at( i ).vertexId );
}
}
if ( !mVertexEditor )
{
mVertexEditor.reset( new QgsVertexEditor( m.layer(), mSelectedFeature.get(), mCanvas ) );
@@ -1048,7 +1055,7 @@ void QgsVertexTool::showVertexEditor() //#spellok
mVertexEditor->updateEditor( m.layer(), mSelectedFeature.get() );
}

connect( mSelectedFeature.get()->vlayer(), &QgsVectorLayer::featureDeleted, this, &QgsVertexTool::cleanEditor );
connect( mSelectedFeature.get()->layer(), &QgsVectorLayer::featureDeleted, this, &QgsVertexTool::cleanEditor );
}

void QgsVertexTool::cleanupVertexEditor()
@@ -1088,7 +1095,7 @@ void QgsVertexTool::deleteVertexEditorSelection()
// make a list of selected vertices
QList<Vertex> vertices;
QList<QgsVertexEntry *> &selFeatureVertices = mSelectedFeature->vertexMap();
QgsVectorLayer *layer = mSelectedFeature->vlayer();
QgsVectorLayer *layer = mSelectedFeature->layer();
QgsFeatureId fid = mSelectedFeature->featureId();
QgsGeometry geometry = cachedGeometry( layer, fid );
for ( QgsVertexEntry *vertex : qgis::as_const( selFeatureVertices ) )
@@ -1116,7 +1123,7 @@ void QgsVertexTool::deleteVertexEditorSelection()

_safeSelectVertex( *mSelectedFeature, nextVertexToSelect );
}
mSelectedFeature->vlayer()->triggerRepaint();
mSelectedFeature->layer()->triggerRepaint();
}


@@ -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();
@@ -219,15 +219,21 @@ class CORE_EXPORT QgsRectangle
bool contains( const QgsPointXY &p ) const;

/**
* Expand the rectangle so that covers both the original rectangle and the given rectangle.
* Expands the rectangle so that it covers both the original rectangle and the given rectangle.
*/
void combineExtentWith( const QgsRectangle &rect );

/**
* Expand the rectangle so that covers both the original rectangle and the given point.
* Expands the rectangle so that it covers both the original rectangle and the given point.
*/
void combineExtentWith( double x, double y );

/**
* Expands the rectangle so that it 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 e899849

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