Skip to content

Commit

Permalink
Small code cleanup to warm up
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Feb 1, 2019
1 parent 495568a commit 39887e7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 42 deletions.
16 changes: 5 additions & 11 deletions src/app/vertextool/qgsvertexeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,7 @@ bool QgsVertexEditorModel::calcR( int row, double &r, double &minRadius ) const
}


QgsVertexEditor::QgsVertexEditor(
QgsVectorLayer *layer,
QgsSelectedFeature *selectedFeature,
QgsMapCanvas *canvas )
QgsVertexEditor::QgsVertexEditor( QgsMapCanvas *canvas )
: mCanvas( canvas )
{
setWindowTitle( tr( "Vertex Editor" ) );
Expand Down Expand Up @@ -328,25 +325,22 @@ QgsVertexEditor::QgsVertexEditor(
layout->addWidget( mHintLabel );

setWidget( content );

updateEditor( layer, selectedFeature );
}

void QgsVertexEditor::updateEditor( QgsVectorLayer *layer, QgsSelectedFeature *selectedFeature )
void QgsVertexEditor::updateEditor( QgsSelectedFeature *selectedFeature )
{
if ( mSelectedFeature )
{
delete mVertexModel;
mVertexModel = nullptr;
}

mLayer = layer;
mSelectedFeature = selectedFeature;

if ( mLayer && mSelectedFeature )
if ( mSelectedFeature )
{
// TODO We really should just update the model itself.
mVertexModel = new QgsVertexEditorModel( mLayer, mSelectedFeature, mCanvas, this );
mVertexModel = new QgsVertexEditorModel( mSelectedFeature->layer(), mSelectedFeature, mCanvas, this );
mTableView->setModel( mVertexModel );

mHintLabel->setVisible( false );
Expand Down Expand Up @@ -398,7 +392,7 @@ void QgsVertexEditor::updateVertexSelection( const QItemSelection &selected, con

mSelectedFeature->deselectAllVertices();

QgsCoordinateTransform t( mLayer->crs(), mCanvas->mapSettings().destinationCrs(), QgsProject::instance() );
QgsCoordinateTransform t( mSelectedFeature->layer()->crs(), mCanvas->mapSettings().destinationCrs(), QgsProject::instance() );
std::unique_ptr<QgsRectangle> bbox;
QModelIndexList indexList = selected.indexes();
for ( int i = 0; i < indexList.length(); ++i )
Expand Down
7 changes: 2 additions & 5 deletions src/app/vertextool/qgsvertexeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,10 @@ class QgsVertexEditor : public QgsDockWidget
{
Q_OBJECT
public:
QgsVertexEditor( QgsVectorLayer *layer,
QgsSelectedFeature *selectedFeature,
QgsMapCanvas *canvas );
QgsVertexEditor( QgsMapCanvas *canvas );

public:
void updateEditor( QgsVectorLayer *layer, QgsSelectedFeature *selectedFeature );
QgsVectorLayer *mLayer = nullptr;
void updateEditor( QgsSelectedFeature *selectedFeature );
QgsSelectedFeature *mSelectedFeature = nullptr;
QgsMapCanvas *mCanvas = nullptr;
QTableView *mTableView = nullptr;
Expand Down
48 changes: 24 additions & 24 deletions src/app/vertextool/qgsvertextool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,10 @@ void QgsVertexTool::cadCanvasPressEvent( QgsMapMouseEvent *e )
{
if ( !mSelectionRect && !mDraggingVertex && !mDraggingEdge )
{
if ( mLastMouseMoveMatch.isValid() && mLastMouseMoveMatch.layer() )
QgsPointLocator::Match m = snapToEditableLayer( e );
if ( m.isValid() && m.layer() )
{
showVertexEditor(); //#spellok
updateVertexEditor( m.layer(), m.featureId() );
}
}
}
Expand Down Expand Up @@ -844,8 +845,6 @@ void QgsVertexTool::mouseMoveNotDragging( QgsMapMouseEvent *e )
QgsPointLocator::Match m = snapToEditableLayer( e );
bool targetIsAllowed = ( !mSelectedFeature || ( mSelectedFeature->featureId() == m.featureId() && mSelectedFeature->layer() == m.layer() ) );

mLastMouseMoveMatch = m;

// possibility to move a vertex
if ( m.type() == QgsPointLocator::Vertex && targetIsAllowed )
{
Expand Down Expand Up @@ -1071,7 +1070,7 @@ void QgsVertexTool::onCachedGeometryChanged( QgsFeatureId fid, const QgsGeometry
validateGeometry( layer, fid );

if ( mVertexEditor && mSelectedFeature && mSelectedFeature->featureId() == fid && mSelectedFeature->layer() == layer )
mVertexEditor->updateEditor( mSelectedFeature->layer(), mSelectedFeature.get() );
mVertexEditor->updateEditor( mSelectedFeature.get() );
}

void QgsVertexTool::onCachedGeometryDeleted( QgsFeatureId fid )
Expand All @@ -1086,35 +1085,43 @@ void QgsVertexTool::onCachedGeometryDeleted( QgsFeatureId fid )
setHighlightedVertices( mSelectedVertices );
}


void QgsVertexTool::showVertexEditor() //#spellok
void QgsVertexTool::updateVertexEditor( QgsVectorLayer *layer, QgsFeatureId fid )
{
QgsPointLocator::Match m = mLastMouseMoveMatch;
if ( m.isValid() && m.layer() )
if ( layer )
{
if ( mSelectedFeature && mSelectedFeature->featureId() == m.featureId() && mSelectedFeature->layer() == m.layer() )
if ( mSelectedFeature && mSelectedFeature->featureId() == fid && mSelectedFeature->layer() == layer )
{
// if show feature is called on a feature that's already binded to the vertex editor, toggle it off
mSelectedFeature.reset();
if ( mVertexEditor )
{
mVertexEditor->updateEditor( nullptr, nullptr );
mVertexEditor->updateEditor( nullptr );
}
return;
}

mSelectedFeature.reset( new QgsSelectedFeature( m.featureId(), m.layer(), mCanvas ) );
mSelectedFeature.reset( new QgsSelectedFeature( fid, layer, mCanvas ) );
connect( mSelectedFeature->layer(), &QgsVectorLayer::featureDeleted, this, &QgsVertexTool::cleanEditor );
for ( int i = 0; i < mSelectedVertices.length(); ++i )
{
if ( mSelectedVertices.at( i ).layer == m.layer() && mSelectedVertices.at( i ).fid == m.featureId() )
if ( mSelectedVertices.at( i ).layer == layer && mSelectedVertices.at( i ).fid == fid )
{
mSelectedFeature->selectVertex( mSelectedVertices.at( i ).vertexId );
}
}
}

// make sure the vertex editor is alive and visible
showVertexEditor();

mVertexEditor->updateEditor( mSelectedFeature.get() );
}

void QgsVertexTool::showVertexEditor() //#spellok
{
if ( !mVertexEditor )
{
mVertexEditor.reset( new QgsVertexEditor( m.layer() ? m.layer() : currentVectorLayer(), mSelectedFeature ? mSelectedFeature.get() : nullptr, mCanvas ) );
mVertexEditor.reset( new QgsVertexEditor( mCanvas ) );
if ( !QgisApp::instance()->restoreDockWidget( mVertexEditor.get() ) )
QgisApp::instance()->addDockWidget( Qt::LeftDockWidgetArea, mVertexEditor.get() );

Expand All @@ -1126,16 +1133,9 @@ void QgsVertexTool::showVertexEditor() //#spellok
}
else
{
mVertexEditor->updateEditor( m.layer(), mSelectedFeature.get() );

mVertexEditor->show();
mVertexEditor->raise();
}

if ( mSelectedFeature )
{
connect( mSelectedFeature->layer(), &QgsVectorLayer::featureDeleted, this, &QgsVertexTool::cleanEditor );
}
}

void QgsVertexTool::cleanupVertexEditor()
Expand Down Expand Up @@ -1735,7 +1735,7 @@ void QgsVertexTool::moveVertex( const QgsPointXY &mapPoint, const QgsPointLocato
}

if ( mVertexEditor )
mVertexEditor->updateEditor( dragLayer, mSelectedFeature.get() );
mVertexEditor->updateEditor( mSelectedFeature.get() );

setHighlightedVertices( mSelectedVertices ); // update positions of existing highlighted vertices
setHighlightedVerticesVisible( true ); // time to show highlighted vertices again
Expand Down Expand Up @@ -1838,7 +1838,7 @@ void QgsVertexTool::applyEditsToLayers( QgsVertexTool::VertexEdits &edits )


if ( mVertexEditor )
mVertexEditor->updateEditor( layer, mSelectedFeature.get() );
mVertexEditor->updateEditor( mSelectedFeature.get() );
}
}

Expand Down Expand Up @@ -1999,7 +1999,7 @@ void QgsVertexTool::deleteVertex()
}

if ( mVertexEditor && mSelectedFeature )
mVertexEditor->updateEditor( mSelectedFeature->layer(), mSelectedFeature.get() );
mVertexEditor->updateEditor( mSelectedFeature.get() );
}

void QgsVertexTool::setHighlightedVertices( const QList<Vertex> &listVertices, HighlightMode mode )
Expand Down
5 changes: 3 additions & 2 deletions src/app/vertextool/qgsvertextool.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class APP_EXPORT QgsVertexTool : public QgsMapToolAdvancedDigitizing
//! Toggle the vertex editor
void showVertexEditor(); //#spellok

//! Update vertex editor to show feature from the given match
void updateVertexEditor( QgsVectorLayer *layer, QgsFeatureId fid );

private slots:
//! update geometry of our feature
void onCachedGeometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
Expand Down Expand Up @@ -406,8 +409,6 @@ class APP_EXPORT QgsVertexTool : public QgsMapToolAdvancedDigitizing

// support for vertex editor

//! most recent match when moving mouse
QgsPointLocator::Match mLastMouseMoveMatch;
//! Selected feature for the vertex editor
std::unique_ptr<QgsSelectedFeature> mSelectedFeature;
//! Dock widget which allows editing vertices
Expand Down

0 comments on commit 39887e7

Please sign in to comment.