Skip to content

Commit 3b3d304

Browse files
committedDec 20, 2017
redigitize on feature duplication
there are two actions duplication and duplication redigitize. in redigitize you can set a new geometry for the duplication.
1 parent e1b6daa commit 3b3d304

File tree

7 files changed

+485
-290
lines changed

7 files changed

+485
-290
lines changed
 

‎src/app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ SET(QGIS_APP_SRCS
6969
qgswelcomepage.cpp
7070

7171
qgsmaptooladdfeature.cpp
72+
qgsmaptooldigitizefeature.cpp
7273
qgsmaptooladdpart.cpp
7374
qgsmaptooladdring.cpp
7475
qgsmaptoolfillring.cpp
@@ -294,6 +295,7 @@ SET (QGIS_APP_MOC_HDRS
294295
qgswelcomepage.h
295296

296297
qgsmaptooladdfeature.h
298+
qgsmaptooldigitizefeature.h
297299
qgsmaptoolannotation.h
298300
qgsmaptoolcircularstringradius.h
299301
qgsmaptooladdpart.h

‎src/app/qgisapp.cpp

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
343343
//
344344
// Map tools
345345
//
346-
#include "qgsmaptooladdfeature.h"
347346
#include "qgsmaptooladdpart.h"
347+
#include "qgsmaptooladdfeature.h"
348348
#include "qgsmaptooladdring.h"
349349
#include "qgsmaptoolfillring.h"
350350
#include "qgsmaptoolannotation.h"
@@ -1382,6 +1382,7 @@ QgisApp::~QgisApp()
13821382
delete mMapTools.mRegularPolygonCenterPoint;
13831383
delete mMapTools.mRegularPolygonCenterCorner;
13841384
delete mMapTools.mAddFeature;
1385+
delete mMapTools.mDigitizeFeature;
13851386
delete mpMaptip;
13861387

13871388
delete mpGpsWidget;
@@ -3298,6 +3299,7 @@ void QgisApp::createCanvasTools()
32983299
mMapTools.mAnnotation->setAction( mActionAnnotation );
32993300
mMapTools.mAddFeature = new QgsMapToolAddFeature( mMapCanvas, QgsMapToolCapture::CaptureNone );
33003301
mMapTools.mAddFeature->setAction( mActionAddFeature );
3302+
mMapTools.mDigitizeFeature = new QgsMapToolDigitizeFeature( mMapCanvas, QgsMapToolCapture::CaptureNone );
33013303
mMapTools.mCircularStringCurvePoint = new QgsMapToolCircularStringCurvePoint( mMapTools.mAddFeature, mMapCanvas );
33023304
mMapTools.mCircularStringCurvePoint->setAction( mActionCircularStringCurvePoint );
33033305
mMapTools.mCircularStringRadius = new QgsMapToolCircularStringRadius( mMapTools.mAddFeature, mMapCanvas );
@@ -7621,6 +7623,17 @@ void QgisApp::setupDuplicateFeaturesAction()
76217623
duplicateFeatures( layer, feat );
76227624
}
76237625
);
7626+
7627+
action = new QgsMapLayerAction( QString( tr( "Duplicate feature redigitized" ) ),
7628+
this, QgsMapLayerAction::AllActions,
7629+
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAms.svg" ) ) );
7630+
7631+
QgsGui::mapLayerActionRegistry()->addMapLayerAction( action );
7632+
connect( action, &QgsMapLayerAction::triggeredForFeature, this, [this]( QgsMapLayer * layer, const QgsFeature & feat )
7633+
{
7634+
duplicateFeatureDigitized( layer, feat );
7635+
}
7636+
);
76247637
}
76257638

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

13328-
layer->startEditing();
13339+
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mlayer );
1332913340

13330-
QgsFeatureList featureList;
13341+
layer->startEditing();
13342+
13343+
QgsFeatureList featureList;
1333113344

13332-
if( feature )
13345+
if ( feature.isValid() )
13346+
{
13347+
featureList.append( feature );
13348+
}
13349+
else
13350+
{
13351+
for ( const QgsFeature &f : layer->selectedFeatures() )
1333313352
{
13334-
featureList.append( feature );
13353+
featureList.append( f );
1333513354
}
13336-
else
13355+
}
13356+
13357+
int featureCount = 0;
13358+
13359+
QString childrenInfo;
13360+
13361+
for ( const QgsFeature &f : featureList )
13362+
{
13363+
QgsVectorLayerUtils::QgsDuplicateFeatureContext duplicateFeatureContext;
13364+
13365+
QgsVectorLayerUtils::duplicateFeature( layer, f, QgsProject::instance(), 0, duplicateFeatureContext );
13366+
featureCount += 1;
13367+
13368+
for ( QgsVectorLayer *chl : duplicateFeatureContext.layers() )
1333713369
{
13338-
for ( const QgsFeature &f : layer->selectedFeatures() )
13339-
{
13340-
featureList.append( f );
13341-
}
13370+
childrenInfo += ( tr( "%1 children on layer %2 duplicated" ).arg( duplicateFeatureContext.duplicatedFeatures( chl ).size() ).arg( chl->name() ) );
1334213371
}
13372+
}
13373+
13374+
messageBar()->pushMessage( tr( "%1 features on layer %2 duplicated\n%3" ).arg( featureCount ).arg( layer->name() ).arg( childrenInfo ), QgsMessageBar::SUCCESS, 5 );
13375+
13376+
return QgsFeature();
13377+
}
13378+
13379+
13380+
QgsFeature QgisApp::duplicateFeatureDigitized( QgsMapLayer *mlayer, const QgsFeature &feature )
13381+
{
13382+
if ( mlayer->type() != QgsMapLayer::VectorLayer )
13383+
return QgsFeature();
13384+
13385+
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( mlayer );
1334313386

13344-
int featureCount=0;
13387+
layer->startEditing();
13388+
mMapCanvas->setMapTool( mMapTools.mDigitizeFeature );
13389+
mMapCanvas->window()->raise();
13390+
mMapCanvas->activateWindow();
13391+
mMapCanvas->setFocus();
13392+
13393+
QString msg = tr( "Digitize the duplicate, please." ).arg( layer->name() );
13394+
messageBar()->pushMessage( msg, QgsMessageBar::INFO, 3 );
13395+
13396+
QMetaObject::Connection *connDigitizingFinished = new QMetaObject::Connection();
13397+
*connDigitizingFinished = connect( mMapTools.mDigitizeFeature, static_cast<void ( QgsMapToolDigitizeFeature::* )( const QgsFeature & )>( &QgsMapToolDigitizeFeature::digitizingFinished ), this, [this, layer, feature, connDigitizingFinished]( const QgsFeature & digitizedFeature )
13398+
{
13399+
QString msg = tr( "Duplicate digitized" );
13400+
messageBar()->pushMessage( msg, QgsMessageBar::INFO, 1 );
13401+
13402+
QgsVectorLayerUtils::QgsDuplicateFeatureContext duplicateFeatureContext;
1334513403

13346-
for ( const QgsFeature &f : featureList )
13404+
QgsFeature newFeature = feature;
13405+
newFeature.setGeometry( digitizedFeature.geometry() );
13406+
QgsVectorLayerUtils::duplicateFeature( layer, newFeature, QgsProject::instance(), 0, duplicateFeatureContext );
13407+
13408+
QString childrenInfo;
13409+
for ( QgsVectorLayer *chl : duplicateFeatureContext.layers() )
1334713410
{
13348-
//QgsVectorLayerUtils::duplicateFeature( layer, feature, QgsProject::instance(), 0 );
13411+
childrenInfo += ( tr( "%1 children on layer %2 duplicated" ).arg( duplicateFeatureContext.duplicatedFeatures( chl ).size() ).arg( chl->name() ) );
1334913412
}
13350-
*/
13413+
13414+
messageBar()->pushMessage( tr( "Feature on layer %2 duplicated\n%3" ).arg( layer->name() ).arg( childrenInfo ), QgsMessageBar::SUCCESS, 5 );
13415+
mMapCanvas->unsetMapTool( mMapTools.mDigitizeFeature );
13416+
disconnect( *connDigitizingFinished );
13417+
}
13418+
);
13419+
1335113420
return QgsFeature();
1335213421
}
13422+

‎src/app/qgisapp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class QgsMapOverviewCanvas;
7474
class QgsMapTip;
7575
class QgsMapTool;
7676
class QgsMapToolAddFeature;
77+
class QgsMapToolDigitizeFeature;
7778
class QgsMapToolAdvancedDigitizing;
7879
class QgsMapToolIdentifyAction;
7980
class QgsPluginLayer;
@@ -1940,6 +1941,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
19401941
QgsMapTool *mMeasureArea = nullptr;
19411942
QgsMapTool *mMeasureAngle = nullptr;
19421943
QgsMapToolAddFeature *mAddFeature = nullptr;
1944+
QgsMapToolDigitizeFeature *mDigitizeFeature = nullptr;
19431945
QgsMapTool *mCircularStringCurvePoint = nullptr;
19441946
QgsMapTool *mCircularStringRadius = nullptr;
19451947
QgsMapTool *mCircle2Points = nullptr;
@@ -2209,7 +2211,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
22092211
QgsBrowserModel *mBrowserModel = nullptr;
22102212

22112213
void setupDuplicateFeaturesAction();
2214+
22122215
QgsFeature duplicateFeatures( QgsMapLayer *mlayer, const QgsFeature &feature );
2216+
QgsFeature duplicateFeatureDigitized( QgsMapLayer *mlayer, const QgsFeature &feature );
22132217

22142218
friend class TestQgisAppPython;
22152219
};

0 commit comments

Comments
 (0)
Please sign in to comment.