QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgssymbollayerreference.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssymbollayerreference.cpp
3 ---------------------
4 begin : July 2019
5 copyright : (C) 2019 by Hugo Mercier / Oslandia
6 email : infos at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgis.h"
19
20#include <QRegularExpression>
21
23{
24 QStringList slst;
25 slst.reserve( lst.size() * 2 );
26 for ( const QgsSymbolLayerReference &ref : lst )
27 {
28 slst << ref.layerId();
29 slst << ref.symbolLayerIdV2();
30 }
31 return slst.join( ';' );
32}
33
35{
37 if ( str.isEmpty() )
38 return lst;
39
40 if ( str.contains( ',' ) )
41 {
42 // TODO QGIS 4 : remove this if branch, keep only else part
44
45 // old masked symbol layer format (before 3.30), we use unique id now!
46 // we load it the old fashion way and we will update the new one later when
47 // the whole project is loaded
48
49 // when saving we used ; as a concatenator... but that was silly, cos maybe the symbol keys contain this string!
50 // try to handle this gracefully via regex...
51 const thread_local QRegularExpression partsRx( QStringLiteral( "((?:.*?),(?:.*?),(?:(?:\\d+,)+)?(?:\\d+);)" ) );
52 QRegularExpressionMatchIterator partsIt = partsRx.globalMatch( str + ';' );
53
54 while ( partsIt.hasNext() )
55 {
56 const QRegularExpressionMatch partMatch = partsIt.next();
57 const QString tuple = partMatch.captured( 1 );
58
59 // We should have "layer_id,symbol_key,symbol_layer_index0,symbol_layer_index1,..."
60 // EXCEPT that the symbol_key CAN have commas, so this whole logic is extremely broken.
61 // Let's see if a messy regex can save the day!
62 const thread_local QRegularExpression rx( QStringLiteral( "(.*?),(.*?),((?:\\d+,)+)?(\\d+)" ) );
63
64 const QRegularExpressionMatch match = rx.match( tuple );
65 if ( !match.hasMatch() )
66 continue;
67
68 const QString layerId = match.captured( 1 );
69 const QString symbolKey = match.captured( 2 );
70 const QStringList indices = QString( match.captured( 3 ) + match.captured( 4 ) ).split( ',' );
71
72 QVector<int> indexPath;
73 indexPath.reserve( indices.size() );
74 for ( const QString &index : indices )
75 {
76 indexPath.append( index.toInt() );
77 }
78 lst.append( QgsSymbolLayerReference( layerId, QgsSymbolLayerId( symbolKey, indexPath ) ) );
80 }
81 }
82 else
83 {
84 const QStringList elems = str.split( ';' );
85 for ( int i = 0; i < elems.size(); )
86 {
87 lst << QgsSymbolLayerReference( elems[i], elems[i + 1] );
88 i += 2;
89 }
90 }
91
92 return lst;
93}
We may need stable references to symbol layers, when pointers to symbol layers are not usable (when a...
Type used to refer to a specific symbol layer in a symbol of a layer.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169
QString symbolLayerReferenceListToString(const QgsSymbolLayerReferenceList &lst)
Utility function to turn a QgsSymbolLayerReferenceList into a string.
QgsSymbolLayerReferenceList stringToSymbolLayerReferenceList(const QString &str)
Utility function to parse a string originated from symbolLayerReferenceListToString into a QgsSymbolL...
QList< QgsSymbolLayerReference > QgsSymbolLayerReferenceList