Skip to content

Commit

Permalink
Merge pull request #7199 from 3nids/vertex_tool
Browse files Browse the repository at this point in the history
vertex tool fixes
  • Loading branch information
3nids committed Jun 12, 2018
2 parents f4065d8 + 5cb86be commit e899849
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 66 deletions.
11 changes: 9 additions & 2 deletions python/core/auto_generated/geometry/qgsrectangle.sip.in
Expand Up @@ -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;
Expand Down
30 changes: 15 additions & 15 deletions src/app/vertextool/qgsselectedfeature.cpp
Expand Up @@ -60,7 +60,7 @@ QgsSelectedFeature::~QgsSelectedFeature()

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

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

Expand All @@ -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()
Expand All @@ -148,7 +148,7 @@ void QgsSelectedFeature::geometryChanged( QgsFeatureId fid, const QgsGeometry &g
{
QgsDebugCall;

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

updateGeometry( &geom );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 ) ) );
}
}

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

QgsVectorLayer *QgsSelectedFeature::vlayer()
QgsVectorLayer *QgsSelectedFeature::layer()
{
return mVlayer;
return mLayer;
}
6 changes: 3 additions & 3 deletions src/app/vertextool/qgsselectedfeature.h
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
82 changes: 42 additions & 40 deletions src/app/vertextool/qgsvertexeditor.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -293,8 +305,6 @@ QgsVertexEditor::QgsVertexEditor(

setWidget( mTableView );

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

updateEditor( layer, selectedFeature );
}

Expand All @@ -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 );
}
Expand Down Expand Up @@ -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 )
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
13 changes: 10 additions & 3 deletions src/app/vertextool/qgsvertextool.cpp
Expand Up @@ -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 ) );
Expand All @@ -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()
Expand Down Expand Up @@ -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 ) )
Expand Down Expand Up @@ -1116,7 +1123,7 @@ void QgsVertexTool::deleteVertexEditorSelection()

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


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
10 changes: 8 additions & 2 deletions src/core/geometry/qgsrectangle.h
Expand Up @@ -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
Expand Down

0 comments on commit e899849

Please sign in to comment.