Skip to content

Commit f8c1c16

Browse files
committedFeb 27, 2023
fix duplicate layer
1 parent a1b2eac commit f8c1c16

File tree

7 files changed

+108
-15
lines changed

7 files changed

+108
-15
lines changed
 

‎python/core/auto_generated/symbology/qgsfillsymbollayer.sip.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ Returns the units used for the offset of the shapeburst fill.
803803
QgsShapeburstFillSymbolLayer( const QgsShapeburstFillSymbolLayer &other );
804804
};
805805

806-
class QgsImageFillSymbolLayer: QgsFillSymbolLayer
806+
class QgsImageFillSymbolLayer: QgsFillSymbolLayer /Abstract/
807807
{
808808
%Docstring(signature="appended")
809809
Base class for polygon renderers generating texture images
@@ -2818,8 +2818,6 @@ Sets whether point markers should be ``clipped`` to the current part boundary on
28182818
QgsCentroidFillSymbolLayer( const QgsCentroidFillSymbolLayer &other );
28192819
};
28202820

2821-
2822-
28232821
/************************************************************************
28242822
* This file has been generated automatically from *
28252823
* *

‎python/core/auto_generated/symbology/qgssymbollayerutils.sip.in

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,27 @@ to new ones.
10491049
%Docstring
10501050
Remove recursively unique id from all ``symbol`` symbol layers and set an empty string instead
10511051

1052+
.. versionadded:: 3.30
1053+
%End
1054+
1055+
static void clearSymbolLayerIds( QgsSymbolLayer *symbolLayer );
1056+
%Docstring
1057+
Remove recursively unique id from ``symbolLayer`` and its children and set an empty string instead
1058+
1059+
.. versionadded:: 3.30
1060+
%End
1061+
1062+
static void resetSymbolLayerIds( QgsSymbol *symbol );
1063+
%Docstring
1064+
Regenerate recursively unique id from all ``symbol`` symbol layers
1065+
1066+
.. versionadded:: 3.30
1067+
%End
1068+
1069+
static void resetSymbolLayerIds( QgsSymbolLayer *symbolLayer );
1070+
%Docstring
1071+
Regenerate recursively unique id from ``symbolLayer`` and its children
1072+
10521073
.. versionadded:: 3.30
10531074
%End
10541075

‎src/core/symbology/qgsfillsymbollayer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ class CORE_EXPORT QgsShapeburstFillSymbolLayer : public QgsFillSymbolLayer
776776
* \ingroup core
777777
* \brief Base class for polygon renderers generating texture images
778778
*/
779-
class CORE_EXPORT QgsImageFillSymbolLayer: public QgsFillSymbolLayer
779+
class CORE_EXPORT QgsImageFillSymbolLayer: public QgsFillSymbolLayer SIP_ABSTRACT
780780
{
781781
public:
782782

@@ -2550,5 +2550,3 @@ class CORE_EXPORT QgsCentroidFillSymbolLayer : public QgsFillSymbolLayer
25502550
};
25512551

25522552
#endif
2553-
2554-

‎src/core/symbology/qgssymbollayerutils.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5446,20 +5446,43 @@ void QgsSymbolLayerUtils::fixOldSymbolLayerReferences( const QMap<QString, QgsMa
54465446
}
54475447
}
54485448

5449-
void QgsSymbolLayerUtils::clearSymbolLayerIds( QgsSymbol *symbol )
5449+
template <typename Functor>
5450+
void changeSymbolLayerIds( QgsSymbolLayer *sl, Functor &&generateId )
5451+
{
5452+
sl->setId( generateId() );
5453+
5454+
// recurse over sub symbols
5455+
QgsSymbol *subSymbol = sl->subSymbol();
5456+
if ( subSymbol )
5457+
changeSymbolLayerIds( subSymbol, generateId );
5458+
}
5459+
5460+
template <typename Functor>
5461+
void changeSymbolLayerIds( QgsSymbol *symbol, Functor &&generateId )
54505462
{
54515463
if ( !symbol )
54525464
return;
54535465

54545466
for ( int idx = 0; idx < symbol->symbolLayerCount(); idx++ )
5455-
{
5456-
QgsSymbolLayer *sl = symbol->symbolLayer( idx );
5467+
changeSymbolLayerIds( symbol->symbolLayer( idx ), generateId );
5468+
}
5469+
5470+
void QgsSymbolLayerUtils::clearSymbolLayerIds( QgsSymbol *symbol )
5471+
{
5472+
changeSymbolLayerIds( symbol, []() { return QString(); } );
5473+
}
54575474

5458-
sl->setId( QString() );
5475+
void QgsSymbolLayerUtils::clearSymbolLayerIds( QgsSymbolLayer *symbolLayer )
5476+
{
5477+
changeSymbolLayerIds( symbolLayer, []() { return QString(); } );
5478+
}
54595479

5460-
// recurse over sub symbols
5461-
QgsSymbol *subSymbol = const_cast<QgsSymbolLayer *>( sl )->subSymbol();
5462-
if ( subSymbol )
5463-
clearSymbolLayerIds( subSymbol );
5464-
}
5480+
void QgsSymbolLayerUtils::resetSymbolLayerIds( QgsSymbolLayer *symbolLayer )
5481+
{
5482+
changeSymbolLayerIds( symbolLayer, []() { return QUuid::createUuid().toString(); } );
5483+
}
5484+
5485+
void QgsSymbolLayerUtils::resetSymbolLayerIds( QgsSymbol *symbol )
5486+
{
5487+
changeSymbolLayerIds( symbol, []() { return QUuid::createUuid().toString(); } );
54655488
}

‎src/core/symbology/qgssymbollayerutils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,24 @@ class CORE_EXPORT QgsSymbolLayerUtils
943943
*/
944944
static void clearSymbolLayerIds( QgsSymbol *symbol );
945945

946+
/**
947+
* Remove recursively unique id from \a symbolLayer and its children and set an empty string instead
948+
* \since QGIS 3.30
949+
*/
950+
static void clearSymbolLayerIds( QgsSymbolLayer *symbolLayer );
951+
952+
/**
953+
* Regenerate recursively unique id from all \a symbol symbol layers
954+
* \since QGIS 3.30
955+
*/
956+
static void resetSymbolLayerIds( QgsSymbol *symbol );
957+
958+
/**
959+
* Regenerate recursively unique id from \a symbolLayer and its children
960+
* \since QGIS 3.30
961+
*/
962+
static void resetSymbolLayerIds( QgsSymbolLayer *symbolLayer );
963+
946964
///@cond PRIVATE
947965
#ifndef SIP_RUN
948966
static QgsProperty rotateWholeSymbol( double additionalRotation, const QgsProperty &property )

‎src/gui/symbology/qgssymbolselectordialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ void QgsSymbolSelectorWidget::duplicateLayer()
721721
QgsSymbol *parentSymbol = item->symbol();
722722

723723
QgsSymbolLayer *newLayer = source->clone();
724+
QgsSymbolLayerUtils::resetSymbolLayerIds( newLayer );
724725
if ( insertIdx == -1 )
725726
parentSymbol->appendSymbolLayer( newLayer );
726727
else

‎tests/src/python/test_qgssymbollayerutils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,40 @@ def testTileSize(self):
736736
self.assertEqual(res_size.width(), int(exp_width))
737737
self.assertAlmostEqual(res_angle, exp_angle)
738738

739+
def test_clear_symbollayer_ids(self):
740+
"""
741+
Test we manage to clear all symbol layer ids on a symbol
742+
"""
743+
744+
source = QgsVectorLayer("Polygon?crs=EPSG:4326", 'layer', "memory")
745+
self.assertTrue(source.isValid())
746+
747+
layer = QgsLinePatternFillSymbolLayer()
748+
fill_symbol = QgsFillSymbol([layer])
749+
750+
sub_renderer = QgsSingleSymbolRenderer(fill_symbol)
751+
source.setRenderer(sub_renderer)
752+
753+
self.assertEqual(len(fill_symbol.symbolLayers()), 1)
754+
755+
subsymbol = fill_symbol.symbolLayers()[0].subSymbol()
756+
self.assertTrue(subsymbol)
757+
self.assertEqual(len(subsymbol.symbolLayers()), 1)
758+
759+
child_sl = subsymbol.symbolLayers()[0]
760+
self.assertTrue(child_sl)
761+
762+
old_id = child_sl.id()
763+
self.assertTrue(child_sl.id())
764+
765+
QgsSymbolLayerUtils.resetSymbolLayerIds(fill_symbol)
766+
767+
self.assertTrue(child_sl.id())
768+
self.assertTrue(child_sl.id() != old_id)
769+
770+
QgsSymbolLayerUtils.clearSymbolLayerIds(fill_symbol)
771+
self.assertFalse(child_sl.id())
772+
739773

740774
if __name__ == '__main__':
741775
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.