Skip to content

Commit a4eb6b0

Browse files
authoredJun 12, 2019
Merge pull request #30128 from m-kuhn/30118
Keep rubberband while attribute dialog is open
2 parents 83c8b5b + 655649a commit a4eb6b0

File tree

6 files changed

+54
-32
lines changed

6 files changed

+54
-32
lines changed
 

‎python/gui/auto_generated/qgsmaptoolcapture.sip.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ Clean a temporary rubberband
9292

9393
%Docstring
9494
convenient method to clean members
95+
%End
96+
97+
QgsRubberBand *takeRubberBand() /Factory/;
98+
%Docstring
99+
Returns the rubberBand currently owned by this map tool and
100+
transfers ownership to the caller.
101+
102+
.. versionadded:: 3.8
95103
%End
96104

97105
protected:

‎src/app/qgsfeatureaction.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap &defaultAttributes, boo
246246
{
247247
mLayer->destroyEditCommand();
248248
}
249+
emit addFeatureFinished();
249250
}
250251
else
251252
{
@@ -260,12 +261,14 @@ bool QgsFeatureAction::addFeature( const QgsAttributeMap &defaultAttributes, boo
260261
if ( !showModal )
261262
{
262263
setParent( dialog ); // keep dialog until the dialog is closed and destructed
264+
connect( dialog, &QgsAttributeDialog::finished, this, &QgsFeatureAction::addFeatureFinished );
263265
dialog->show();
264266
mFeature = nullptr;
265267
return true;
266268
}
267269

268270
dialog->exec();
271+
emit addFeatureFinished();
269272
}
270273

271274
// Will be set in the onFeatureSaved SLOT

‎src/app/qgsfeatureaction.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ class APP_EXPORT QgsFeatureAction : public QAction
6161
*/
6262
void setForceSuppressFormPopup( bool force );
6363

64+
signals:
65+
66+
/**
67+
* This signal is emitted when the add feature process is finished.
68+
* Either during the call to addFeature() already or when the dialog is eventually
69+
* closed (accepted or canceled).
70+
*
71+
* \since QGIS 3.8
72+
*/
73+
void addFeatureFinished();
74+
6475
private slots:
6576
void onFeatureSaved( const QgsFeature &feature );
6677

‎src/app/qgsmaptooladdfeature.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ bool QgsMapToolAddFeature::addFeature( QgsVectorLayer *vlayer, QgsFeature *f, bo
4949
{
5050
QgsExpressionContextScope *scope = QgsExpressionContextUtils::mapToolCaptureScope( snappingMatches() );
5151
QgsFeatureAction *action = new QgsFeatureAction( tr( "add feature" ), *f, vlayer, QString(), -1, this );
52+
QgsRubberBand *rb = takeRubberBand();
53+
connect( action, &QgsFeatureAction::addFeatureFinished, rb, &QgsRubberBand::deleteLater );
5254
bool res = action->addFeature( QgsAttributeMap(), showModal, scope );
5355
if ( showModal )
5456
delete action;
@@ -67,12 +69,11 @@ void QgsMapToolAddFeature::digitized( QgsFeature &f )
6769

6870
//use always topological editing for avoidIntersection.
6971
//Otherwise, no way to guarantee the geometries don't have a small gap in between.
70-
QList<QgsVectorLayer *> intersectionLayers = QgsProject::instance()->avoidIntersectionsLayers();
71-
bool avoidIntersection = !intersectionLayers.isEmpty();
72-
if ( avoidIntersection ) //try to add topological points also to background layers
72+
const QList<QgsVectorLayer *> intersectionLayers = QgsProject::instance()->avoidIntersectionsLayers();
73+
74+
if ( !intersectionLayers.isEmpty() ) //try to add topological points also to background layers
7375
{
74-
const auto constIntersectionLayers = intersectionLayers;
75-
for ( QgsVectorLayer *vl : constIntersectionLayers )
76+
for ( QgsVectorLayer *vl : intersectionLayers )
7677
{
7778
//can only add topological points if background layer is editable...
7879
if ( vl->geometryType() == QgsWkbTypes::PolygonGeometry && vl->isEditable() )

‎src/gui/qgsmaptoolcapture.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ bool QgsMapToolCapture::tracingAddVertex( const QgsPointXY &point )
292292
return true;
293293
}
294294

295+
QgsRubberBand *QgsMapToolCapture::takeRubberBand()
296+
{
297+
return mRubberBand.release();
298+
}
299+
295300

296301
void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent *e )
297302
{
@@ -302,7 +307,7 @@ void QgsMapToolCapture::cadCanvasMoveEvent( QgsMapMouseEvent *e )
302307

303308
if ( !mTempRubberBand && mCaptureCurve.numPoints() > 0 )
304309
{
305-
mTempRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, true );
310+
mTempRubberBand.reset( createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, true ) );
306311
QgsPoint pt = mCaptureCurve.endPoint();
307312
mTempRubberBand->addPoint( QgsPointXY( pt.x(), pt.y() ) );
308313
mTempRubberBand->addPoint( point );
@@ -444,12 +449,12 @@ int QgsMapToolCapture::addVertex( const QgsPointXY &point, const QgsPointLocator
444449

445450
if ( !mRubberBand )
446451
{
447-
mRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry );
452+
mRubberBand.reset( createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry ) );
448453
}
449454

450455
if ( !mTempRubberBand )
451456
{
452-
mTempRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, true );
457+
mTempRubberBand.reset( createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, true ) );
453458
}
454459
else
455460
{
@@ -500,7 +505,7 @@ int QgsMapToolCapture::addCurve( QgsCurve *c )
500505

501506
if ( !mRubberBand )
502507
{
503-
mRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry );
508+
mRubberBand.reset( createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry ) );
504509
}
505510

506511
QgsLineString *lineString = c->curveToLine();
@@ -515,7 +520,7 @@ int QgsMapToolCapture::addCurve( QgsCurve *c )
515520

516521
if ( !mTempRubberBand )
517522
{
518-
mTempRubberBand = createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, true );
523+
mTempRubberBand.reset( createRubberBand( mCaptureMode == CapturePolygon ? QgsWkbTypes::PolygonGeometry : QgsWkbTypes::LineGeometry, true ) );
519524
}
520525
else
521526
{
@@ -622,23 +627,12 @@ bool QgsMapToolCapture::isCapturing() const
622627

623628
void QgsMapToolCapture::stopCapturing()
624629
{
625-
if ( mRubberBand )
626-
{
627-
delete mRubberBand;
628-
mRubberBand = nullptr;
629-
}
630+
mRubberBand.reset();
630631

631-
if ( mTempRubberBand )
632-
{
633-
delete mTempRubberBand;
634-
mTempRubberBand = nullptr;
635-
}
636-
637-
while ( !mGeomErrorMarkers.isEmpty() )
638-
{
639-
delete mGeomErrorMarkers.takeFirst();
640-
}
632+
deleteTempRubberBand();
641633

634+
qDeleteAll( mGeomErrorMarkers );
635+
mGeomErrorMarkers.clear();
642636
mGeomErrors.clear();
643637

644638
mTracingStartPoint = QgsPointXY();
@@ -652,11 +646,7 @@ void QgsMapToolCapture::stopCapturing()
652646

653647
void QgsMapToolCapture::deleteTempRubberBand()
654648
{
655-
if ( mTempRubberBand )
656-
{
657-
delete mTempRubberBand;
658-
mTempRubberBand = nullptr;
659-
}
649+
mTempRubberBand.reset();
660650
}
661651

662652
void QgsMapToolCapture::clean()

‎src/gui/qgsmaptoolcapture.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgspointlocator.h"
2222
#include "qgscompoundcurve.h"
2323
#include "qgsgeometry.h"
24+
#include "qobjectuniqueptr.h"
2425

2526
#include <QPoint>
2627
#include <QList>
@@ -106,6 +107,14 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing
106107
//! convenient method to clean members
107108
void clean() override;
108109

110+
/**
111+
* Returns the rubberBand currently owned by this map tool and
112+
* transfers ownership to the caller.
113+
*
114+
* \since QGIS 3.8
115+
*/
116+
QgsRubberBand *takeRubberBand() SIP_FACTORY;
117+
109118
private slots:
110119
void addError( const QgsGeometry::Error &error );
111120
void currentLayerChanged( QgsMapLayer *layer );
@@ -255,10 +264,10 @@ class GUI_EXPORT QgsMapToolCapture : public QgsMapToolAdvancedDigitizing
255264
bool mCapturing = false;
256265

257266
//! Rubber band for polylines and polygons
258-
QgsRubberBand *mRubberBand = nullptr;
267+
QObjectUniquePtr<QgsRubberBand> mRubberBand;
259268

260269
//! Temporary rubber band for polylines and polygons. this connects the last added point to the mouse cursor position
261-
QgsRubberBand *mTempRubberBand = nullptr;
270+
QObjectUniquePtr<QgsRubberBand> mTempRubberBand;
262271

263272
//! List to store the points of digitized lines and polygons (in layer coordinates)
264273
QgsCompoundCurve mCaptureCurve;

0 commit comments

Comments
 (0)
Please sign in to comment.