Skip to content

Commit

Permalink
improved signal/slot and flexible layer handling
Browse files Browse the repository at this point in the history
two signals now digitizingCompleted and digitizingFinished using deleteLater() for no conflicts
the addFeature is using the currentLayer. when the duplicatedigitized-action has a given layer it uses the given. otherwise the current. the given will be set as current and on decativation of the digitizing it's set back.
  • Loading branch information
signedav committed Dec 20, 2017
1 parent 1295efc commit 0b05030
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 40 deletions.
4 changes: 4 additions & 0 deletions python/gui/qgsmaptoolcapture.sip
Expand Up @@ -96,6 +96,10 @@ Clean a temporary rubberband
convenient method to clean members
%End

public slots:
void setCurrentLayer( QgsMapLayer *layer );


protected:

int nextPoint( const QgsPoint &mapPoint, QgsPoint &layerPoint );
Expand Down
17 changes: 5 additions & 12 deletions src/app/qgisapp.cpp
Expand Up @@ -1382,7 +1382,6 @@ QgisApp::~QgisApp()
delete mMapTools.mRegularPolygonCenterPoint;
delete mMapTools.mRegularPolygonCenterCorner;
delete mMapTools.mAddFeature;
delete mMapTools.mDigitizeFeature;
delete mpMaptip;

delete mpGpsWidget;
Expand Down Expand Up @@ -3299,7 +3298,6 @@ void QgisApp::createCanvasTools()
mMapTools.mAnnotation->setAction( mActionAnnotation );
mMapTools.mAddFeature = new QgsMapToolAddFeature( mMapCanvas, QgsMapToolCapture::CaptureNone );
mMapTools.mAddFeature->setAction( mActionAddFeature );
mMapTools.mDigitizeFeature = new QgsMapToolDigitizeFeature( mMapCanvas, QgsMapToolCapture::CaptureNone );
mMapTools.mCircularStringCurvePoint = new QgsMapToolCircularStringCurvePoint( mMapTools.mAddFeature, mMapCanvas );
mMapTools.mCircularStringCurvePoint->setAction( mActionCircularStringCurvePoint );
mMapTools.mCircularStringRadius = new QgsMapToolCircularStringRadius( mMapTools.mAddFeature, mMapCanvas );
Expand Down Expand Up @@ -13388,17 +13386,17 @@ QgsFeature QgisApp::duplicateFeatureDigitized( QgsMapLayer *mlayer, const QgsFea

layer->startEditing();

QgsMapToolDigitizeFeature *digitizeFeature = new QgsMapToolDigitizeFeature( mMapCanvas, QgsMapToolCapture::CaptureNone );
QgsMapToolDigitizeFeature *digitizeFeature = new QgsMapToolDigitizeFeature( mMapCanvas, mlayer, QgsMapToolCapture::CaptureNone );

mMapCanvas->setMapTool( digitizeFeature );
mMapCanvas->window()->raise();
mMapCanvas->activateWindow();
mMapCanvas->setFocus();

QString msg = tr( "Digitize the duplicate, please." ).arg( layer->name() );
QString msg = tr( "Digitize the duplicate on layer %1" ).arg( layer->name() );
messageBar()->pushMessage( msg, QgsMessageBar::INFO, 3 );

connect( digitizeFeature, static_cast<void ( QgsMapToolDigitizeFeature::* )( const QgsFeature & )>( &QgsMapToolDigitizeFeature::digitizingFinished ), this, [this, layer, feature, digitizeFeature]( const QgsFeature & digitizedFeature )
connect( digitizeFeature, static_cast<void ( QgsMapToolDigitizeFeature::* )( const QgsFeature & )>( &QgsMapToolDigitizeFeature::digitizingCompleted ), this, [this, layer, feature, digitizeFeature]( const QgsFeature & digitizedFeature )
{
QString msg = tr( "Duplicate digitized" );
messageBar()->pushMessage( msg, QgsMessageBar::INFO, 1 );
Expand All @@ -13418,18 +13416,13 @@ QgsFeature QgisApp::duplicateFeatureDigitized( QgsMapLayer *mlayer, const QgsFea

messageBar()->pushMessage( tr( "Feature on layer %2 duplicated\n%3" ).arg( layer->name() ).arg( childrenInfo ), QgsMessageBar::SUCCESS, 5 );

}
);

connect( digitizeFeature, static_cast<void ( QgsMapToolDigitizeFeature::* )()>( &QgsMapToolDigitizeFeature::digitizingFinalized ), this, [this, digitizeFeature]()
{
mMapCanvas->unsetMapTool( digitizeFeature );
}
);

connect( digitizeFeature, static_cast<void ( QgsMapToolDigitizeFeature::* )()>( &QgsMapToolDigitizeFeature::digitizingAborted ), this, [this, digitizeFeature]()
connect( digitizeFeature, static_cast<void ( QgsMapToolDigitizeFeature::* )()>( &QgsMapToolDigitizeFeature::digitizingFinished ), this, [this, digitizeFeature]()
{
delete digitizeFeature;
digitizeFeature->deleteLater();
}
);

Expand Down
1 change: 0 additions & 1 deletion src/app/qgisapp.h
Expand Up @@ -1941,7 +1941,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QgsMapTool *mMeasureArea = nullptr;
QgsMapTool *mMeasureAngle = nullptr;
QgsMapToolAddFeature *mAddFeature = nullptr;
QgsMapToolDigitizeFeature *mDigitizeFeature = nullptr;
QgsMapTool *mCircularStringCurvePoint = nullptr;
QgsMapTool *mCircularStringRadius = nullptr;
QgsMapTool *mCircle2Points = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsmaptooladdfeature.cpp
Expand Up @@ -37,7 +37,7 @@
#include <QSettings>

QgsMapToolAddFeature::QgsMapToolAddFeature( QgsMapCanvas *canvas, CaptureMode mode )
: QgsMapToolDigitizeFeature( canvas, mode )
: QgsMapToolDigitizeFeature( canvas, canvas->currentLayer(), mode )
, mCheckGeometryType( true )
{
mToolName = tr( "Add feature" );
Expand All @@ -55,10 +55,10 @@ bool QgsMapToolAddFeature::addFeature( QgsVectorLayer *vlayer, QgsFeature *f, bo
return res;
}

void QgsMapToolAddFeature::digitized( QgsFeature *f )
void QgsMapToolAddFeature::digitized( const QgsFeature *f )
{
QgsVectorLayer *vlayer = currentVectorLayer();
bool res = addFeature( vlayer, f, false );
bool res = addFeature( vlayer, const_cast<QgsFeature *>( f ), false );

if ( res && ( mode() == CaptureLine || mode() == CapturePolygon ) )
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptooladdfeature.h
Expand Up @@ -25,7 +25,7 @@ class APP_EXPORT QgsMapToolAddFeature : public QgsMapToolDigitizeFeature

bool addFeature( QgsVectorLayer *vlayer, QgsFeature *f, bool showModal = true );

void digitized( QgsFeature *f ) override;
void digitized( const QgsFeature *f ) override;

protected:

Expand Down
40 changes: 25 additions & 15 deletions src/app/qgsmaptooldigitizefeature.cpp
Expand Up @@ -37,41 +37,53 @@
#include <QMouseEvent>
#include <QSettings>

QgsMapToolDigitizeFeature::QgsMapToolDigitizeFeature( QgsMapCanvas *canvas, CaptureMode mode )
QgsMapToolDigitizeFeature::QgsMapToolDigitizeFeature( QgsMapCanvas *canvas, QgsMapLayer *layer, CaptureMode mode )
: QgsMapToolCapture( canvas, QgisApp::instance()->cadDockWidget(), mode )
, mCheckGeometryType( true )
{
mLayer = layer;
mToolName = tr( "Digitize feature" );
connect( QgisApp::instance(), &QgisApp::newProject, this, &QgsMapToolDigitizeFeature::stopCapturing );
connect( QgisApp::instance(), &QgisApp::projectRead, this, &QgsMapToolDigitizeFeature::stopCapturing );
}

void QgsMapToolDigitizeFeature::digitized( QgsFeature *f )
void QgsMapToolDigitizeFeature::digitized( const QgsFeature *f )
{
emit digitizingFinished( static_cast< const QgsFeature & >( *f ) );
emit digitizingCompleted( static_cast< const QgsFeature & >( *f ) );
}

void QgsMapToolDigitizeFeature::activate()
{
QgsVectorLayer *vlayer = currentVectorLayer();
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );
if ( !vlayer )
vlayer = currentVectorLayer();

if ( vlayer && vlayer->geometryType() == QgsWkbTypes::NullGeometry )
{
QgsFeature f;
digitized( &f );
emit digitizingFinalized();
return;
}

//refresh the layer, with the current layer - so capturemode will be set at activate
canvas()->setCurrentLayer( canvas()->currentLayer() );
if ( mLayer )
{
//remember current layer
mCurrentLayer = mCanvas->currentLayer();
//set the layer with the given
mCanvas->setCurrentLayer( mLayer );
}

QgsMapToolCapture::activate();
}

void QgsMapToolDigitizeFeature::deactivate()
{
QgsMapToolCapture::deactivate();
emit digitizingAborted();

if ( mCurrentLayer )
//set the layer back to the one remembered
mCanvas->setCurrentLayer( mCurrentLayer );
emit digitizingFinished();
}

bool QgsMapToolDigitizeFeature::checkGeometryType() const
Expand All @@ -86,7 +98,11 @@ void QgsMapToolDigitizeFeature::setCheckGeometryType( bool checkGeometryType )

void QgsMapToolDigitizeFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
{
QgsVectorLayer *vlayer = currentVectorLayer();
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mLayer );

if ( !vlayer )
//if no given layer take the current from canvas
vlayer = currentVectorLayer();

if ( !vlayer )
{
Expand Down Expand Up @@ -123,8 +139,6 @@ void QgsMapToolDigitizeFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
return;
}



QgsPointXY savePoint; //point in layer coordinates
try
{
Expand Down Expand Up @@ -192,8 +206,6 @@ void QgsMapToolDigitizeFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e )

// we are done with digitizing for now so instruct advanced digitizing dock to reset its CAD points
cadDockWidget()->clearPoints();

emit digitizingFinalized();
}
}

Expand Down Expand Up @@ -315,8 +327,6 @@ void QgsMapToolDigitizeFeature::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
digitized( f.get() );

stopCapturing();

emit digitizingFinalized();
}
}
}
19 changes: 14 additions & 5 deletions src/app/qgsmaptooldigitizefeature.h
Expand Up @@ -25,19 +25,18 @@ class APP_EXPORT QgsMapToolDigitizeFeature : public QgsMapToolCapture
Q_OBJECT
public:
//! \since QGIS 3.2
QgsMapToolDigitizeFeature( QgsMapCanvas *canvas, CaptureMode mode );
QgsMapToolDigitizeFeature( QgsMapCanvas *canvas, QgsMapLayer *layer, CaptureMode mode );

void cadCanvasReleaseEvent( QgsMapMouseEvent *e ) override;

virtual void digitized( QgsFeature *f );
virtual void digitized( const QgsFeature *f );

virtual void activate() override;
virtual void deactivate() override;

signals:
void digitizingFinished( const QgsFeature & );
void digitizingFinalized( );
void digitizingAborted( );
void digitizingCompleted( const QgsFeature & );
void digitizingFinished( );

protected:

Expand All @@ -55,6 +54,16 @@ class APP_EXPORT QgsMapToolDigitizeFeature : public QgsMapToolCapture

private:

/**
* individual layer per digitizing session
* \since QGIS 3.0 */
QgsMapLayer *mLayer = nullptr;

/**
* layer used before digitizing session
* \since QGIS 3.0 */
QgsMapLayer *mCurrentLayer = nullptr;

/**
* Check if CaptureMode matches layer type. Default is true.
* \since QGIS 2.12 */
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsmaptoolcapture.cpp
Expand Up @@ -53,7 +53,7 @@ QgsMapToolCapture::QgsMapToolCapture( QgsMapCanvas *canvas, QgsAdvancedDigitizin
setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::CapturePoint ) );

connect( canvas, &QgsMapCanvas::currentLayerChanged,
this, &QgsMapToolCapture::currentLayerChanged );
this, &QgsMapToolCapture::setCurrentLayer );
}

QgsMapToolCapture::~QgsMapToolCapture()
Expand Down Expand Up @@ -95,7 +95,7 @@ void QgsMapToolCapture::validationFinished()
}
}

void QgsMapToolCapture::currentLayerChanged( QgsMapLayer *layer )
void QgsMapToolCapture::setCurrentLayer( QgsMapLayer *layer )
{
if ( !mCaptureModeFromLayer )
return;
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgsmaptoolcapture.h
Expand Up @@ -111,9 +111,11 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing

private slots:
void validationFinished();
void currentLayerChanged( QgsMapLayer *layer );
void addError( QgsGeometry::Error );

public slots:
void setCurrentLayer( QgsMapLayer *layer );


protected:

Expand Down

0 comments on commit 0b05030

Please sign in to comment.