File tree Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change 16
16
***************************************************************************/
17
17
18
18
19
- #include < QDateTime>
20
19
#include < QDir>
21
20
#include < QDomDocument>
22
21
#include < QDomElement>
@@ -56,7 +55,6 @@ QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
56
55
: mValid( false ) // assume the layer is invalid
57
56
, mDataSource( source )
58
57
, mLayerOrigName( lyrname ) // store the original name
59
- , mID( QLatin1String( " " ) )
60
58
, mLayerType( type )
61
59
, mBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal blending
62
60
, mLegend( nullptr )
@@ -65,12 +63,13 @@ QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
65
63
// Set the display name = internal name
66
64
mLayerName = capitalizeLayerName ( mLayerOrigName );
67
65
68
- mShortName = QLatin1String ( " " );
69
66
// mShortName.replace( QRegExp( "[\\W]" ), "_" );
70
67
71
68
// Generate the unique ID of this layer
72
- QDateTime dt = QDateTime::currentDateTime ();
73
- mID = lyrname + dt.toString ( QStringLiteral ( " yyyyMMddhhmmsszzz" ) );
69
+ QString uuid = QUuid::createUuid ().toString ();
70
+ // trim { } from uuid
71
+ mID = lyrname + ' _' + uuid.mid ( 1 , uuid.length () - 2 );
72
+
74
73
// Tidy the ID up to avoid characters that may cause problems
75
74
// elsewhere (e.g in some parts of XML). Replaces every non-word
76
75
// character (word characters are the alphabet, numbers and
Original file line number Diff line number Diff line change 25
25
26
26
class TestQgsMapLayer (unittest .TestCase ):
27
27
28
+ def testUniqueId (self ):
29
+ """
30
+ Test that layers created quickly with same name get a unique ID
31
+ """
32
+
33
+ # make 1000 layers quickly
34
+ layers = []
35
+ for i in range (1000 ):
36
+ layer = QgsVectorLayer (
37
+ 'Point?crs=epsg:4326&field=name:string(20)' ,
38
+ 'test' ,
39
+ 'memory' )
40
+ layers .append (layer )
41
+
42
+ # make sure all ids are unique
43
+ ids = set ()
44
+ for l in layers :
45
+ self .assertFalse (l .id () in ids )
46
+ ids .add (l .id ())
47
+
28
48
def copyLayerViaXmlReadWrite (self , source , dest ):
29
49
# write to xml
30
50
doc = QDomDocument ("testdoc" )
You can’t perform that action at this time.
0 commit comments