QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "qgis.h"
18#include <QRegularExpression>
19
21{
22 QStringList slst;
23 slst.reserve( lst.size() * 2 );
24 for ( const QgsSymbolLayerReference &ref : lst )
25 {
26 slst << ref.layerId();
27 slst << ref.symbolLayerIdV2();
28 }
29 return slst.join( ';' );
30}
31
33{
35 if ( str.isEmpty() )
36 return lst;
37
38 if ( str.contains( ',' ) )
39 {
40 // TODO QGIS 4 : remove this if branch, keep only else part
42
43 // old masked symbol layer format (before 3.30), we use unique id now!
44 // we load it the old fashion way and we will update the new one later when
45 // the whole project is loaded
46
47 // when saving we used ; as a concatenator... but that was silly, cos maybe the symbol keys contain this string!
48 // try to handle this gracefully via regex...
49 const thread_local QRegularExpression partsRx( QStringLiteral( "((?:.*?),(?:.*?),(?:(?:\\d+,)+)?(?:\\d+);)" ) );
50 QRegularExpressionMatchIterator partsIt = partsRx.globalMatch( str + ';' );
51
52 while ( partsIt.hasNext() )
53 {
54 const QRegularExpressionMatch partMatch = partsIt.next();
55 const QString tuple = partMatch.captured( 1 );
56
57 // We should have "layer_id,symbol_key,symbol_layer_index0,symbol_layer_index1,..."
58 // EXCEPT that the symbol_key CAN have commas, so this whole logic is extremely broken.
59 // Let's see if a messy regex can save the day!
60 const thread_local QRegularExpression rx( QStringLiteral( "(.*?),(.*?),((?:\\d+,)+)?(\\d+)" ) );
61
62 const QRegularExpressionMatch match = rx.match( tuple );
63 if ( !match.hasMatch() )
64 continue;
65
66 const QString layerId = match.captured( 1 );
67 const QString symbolKey = match.captured( 2 );
68 const QStringList indices = QString( match.captured( 3 ) + match.captured( 4 ) ).split( ',' );
69
70 QVector<int> indexPath;
71 indexPath.reserve( indices.size() );
72 for ( const QString &index : indices )
73 {
74 indexPath.append( index.toInt() );
75 }
76 lst.append( QgsSymbolLayerReference( layerId, QgsSymbolLayerId( symbolKey, indexPath ) ) );
78 }
79 }
80 else
81 {
82 const QStringList elems = str.split( ';' );
83 for ( int i = 0; i < elems.size(); )
84 {
85 lst << QgsSymbolLayerReference( elems[i], elems[i + 1] );
86 i += 2;
87 }
88 }
89
90 return lst;
91}
We may need stable references to symbol layers, when pointers to symbol layers is 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:6601
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6600
QString symbolLayerReferenceListToString(const QgsSymbolLayerReferenceList &lst)
Utilitary function to turn a QgsSymbolLayerReferenceList into a string.
QgsSymbolLayerReferenceList stringToSymbolLayerReferenceList(const QString &str)
Utilitary function to parse a string originated from symbolLayerReferenceListToString into a QgsSymbo...
QList< QgsSymbolLayerReference > QgsSymbolLayerReferenceList