Skip to content

Commit

Permalink
redigitize on feature duplication
Browse files Browse the repository at this point in the history
there are two actions duplication and duplication redigitize. in redigitize you can set a new geometry for the duplication.
  • Loading branch information
signedav committed Dec 20, 2017
1 parent e1b6daa commit 3b3d304
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 290 deletions.
2 changes: 2 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -69,6 +69,7 @@ SET(QGIS_APP_SRCS
qgswelcomepage.cpp

qgsmaptooladdfeature.cpp
qgsmaptooldigitizefeature.cpp
qgsmaptooladdpart.cpp
qgsmaptooladdring.cpp
qgsmaptoolfillring.cpp
Expand Down Expand Up @@ -294,6 +295,7 @@ SET (QGIS_APP_MOC_HDRS
qgswelcomepage.h

qgsmaptooladdfeature.h
qgsmaptooldigitizefeature.h
qgsmaptoolannotation.h
qgsmaptoolcircularstringradius.h
qgsmaptooladdpart.h
Expand Down
102 changes: 86 additions & 16 deletions src/app/qgisapp.cpp
Expand Up @@ -343,8 +343,8 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
//
// Map tools
//
#include "qgsmaptooladdfeature.h"
#include "qgsmaptooladdpart.h"
#include "qgsmaptooladdfeature.h"
#include "qgsmaptooladdring.h"
#include "qgsmaptoolfillring.h"
#include "qgsmaptoolannotation.h"
Expand Down Expand Up @@ -1382,6 +1382,7 @@ QgisApp::~QgisApp()
delete mMapTools.mRegularPolygonCenterPoint;
delete mMapTools.mRegularPolygonCenterCorner;
delete mMapTools.mAddFeature;
delete mMapTools.mDigitizeFeature;
delete mpMaptip;

delete mpGpsWidget;
Expand Down Expand Up @@ -3298,6 +3299,7 @@ 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 @@ -7621,6 +7623,17 @@ void QgisApp::setupDuplicateFeaturesAction()
duplicateFeatures( layer, feat );
}
);

action = new QgsMapLayerAction( QString( tr( "Duplicate feature redigitized" ) ),
this, QgsMapLayerAction::AllActions,
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAms.svg" ) ) );

QgsGui::mapLayerActionRegistry()->addMapLayerAction( action );
connect( action, &QgsMapLayerAction::triggeredForFeature, this, [this]( QgsMapLayer * layer, const QgsFeature & feat )
{
duplicateFeatureDigitized( layer, feat );
}
);
}

void QgisApp::setupAtlasMapLayerAction( QgsComposition *composition, bool enableAction )
Expand Down Expand Up @@ -13322,31 +13335,88 @@ QgsFeature QgisApp::duplicateFeatures( QgsMapLayer *mlayer, const QgsFeature &fe
{
if ( mlayer->type() != QgsMapLayer::VectorLayer )
return QgsFeature();
/*
QgsVectorLayer *layer=qobject_cast<QgsVectorLayer *>(mlayer);

layer->startEditing();
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mlayer );

QgsFeatureList featureList;
layer->startEditing();

QgsFeatureList featureList;

if( feature )
if ( feature.isValid() )
{
featureList.append( feature );
}
else
{
for ( const QgsFeature &f : layer->selectedFeatures() )
{
featureList.append( feature );
featureList.append( f );
}
else
}

int featureCount = 0;

QString childrenInfo;

for ( const QgsFeature &f : featureList )
{
QgsVectorLayerUtils::QgsDuplicateFeatureContext duplicateFeatureContext;

QgsVectorLayerUtils::duplicateFeature( layer, f, QgsProject::instance(), 0, duplicateFeatureContext );
featureCount += 1;

for ( QgsVectorLayer *chl : duplicateFeatureContext.layers() )
{
for ( const QgsFeature &f : layer->selectedFeatures() )
{
featureList.append( f );
}
childrenInfo += ( tr( "%1 children on layer %2 duplicated" ).arg( duplicateFeatureContext.duplicatedFeatures( chl ).size() ).arg( chl->name() ) );
}
}

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

return QgsFeature();
}


QgsFeature QgisApp::duplicateFeatureDigitized( QgsMapLayer *mlayer, const QgsFeature &feature )
{
if ( mlayer->type() != QgsMapLayer::VectorLayer )
return QgsFeature();

QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mlayer );

int featureCount=0;
layer->startEditing();
mMapCanvas->setMapTool( mMapTools.mDigitizeFeature );
mMapCanvas->window()->raise();
mMapCanvas->activateWindow();
mMapCanvas->setFocus();

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

QMetaObject::Connection *connDigitizingFinished = new QMetaObject::Connection();
*connDigitizingFinished = connect( mMapTools.mDigitizeFeature, static_cast<void ( QgsMapToolDigitizeFeature::* )( const QgsFeature & )>( &QgsMapToolDigitizeFeature::digitizingFinished ), this, [this, layer, feature, connDigitizingFinished]( const QgsFeature & digitizedFeature )
{
QString msg = tr( "Duplicate digitized" );
messageBar()->pushMessage( msg, QgsMessageBar::INFO, 1 );

QgsVectorLayerUtils::QgsDuplicateFeatureContext duplicateFeatureContext;

for ( const QgsFeature &f : featureList )
QgsFeature newFeature = feature;
newFeature.setGeometry( digitizedFeature.geometry() );
QgsVectorLayerUtils::duplicateFeature( layer, newFeature, QgsProject::instance(), 0, duplicateFeatureContext );

QString childrenInfo;
for ( QgsVectorLayer *chl : duplicateFeatureContext.layers() )
{
//QgsVectorLayerUtils::duplicateFeature( layer, feature, QgsProject::instance(), 0 );
childrenInfo += ( tr( "%1 children on layer %2 duplicated" ).arg( duplicateFeatureContext.duplicatedFeatures( chl ).size() ).arg( chl->name() ) );
}
*/

messageBar()->pushMessage( tr( "Feature on layer %2 duplicated\n%3" ).arg( layer->name() ).arg( childrenInfo ), QgsMessageBar::SUCCESS, 5 );
mMapCanvas->unsetMapTool( mMapTools.mDigitizeFeature );
disconnect( *connDigitizingFinished );
}
);

return QgsFeature();
}

4 changes: 4 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -74,6 +74,7 @@ class QgsMapOverviewCanvas;
class QgsMapTip;
class QgsMapTool;
class QgsMapToolAddFeature;
class QgsMapToolDigitizeFeature;
class QgsMapToolAdvancedDigitizing;
class QgsMapToolIdentifyAction;
class QgsPluginLayer;
Expand Down Expand Up @@ -1940,6 +1941,7 @@ 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 Expand Up @@ -2209,7 +2211,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QgsBrowserModel *mBrowserModel = nullptr;

void setupDuplicateFeaturesAction();

QgsFeature duplicateFeatures( QgsMapLayer *mlayer, const QgsFeature &feature );
QgsFeature duplicateFeatureDigitized( QgsMapLayer *mlayer, const QgsFeature &feature );

friend class TestQgisAppPython;
};
Expand Down

0 comments on commit 3b3d304

Please sign in to comment.