22 #include <QDomElement>
23 #include <QTextStream>
32 QString QgsMapLayerStyleManager::defaultStyleName()
34 return tr(
"default" );
41 mCurrentStyle = defaultStyleName();
46 mCurrentStyle = mgrElement.attribute( QStringLiteral(
"current" ) );
47 if ( mCurrentStyle.isEmpty() )
50 mCurrentStyle = defaultStyleName();
54 QDomElement ch = mgrElement.firstChildElement( QStringLiteral(
"map-layer-style" ) );
55 while ( !ch.isNull() )
57 QString name = ch.attribute( QStringLiteral(
"name" ) );
61 name = defaultStyleName();
65 mStyles.insert( name,
style );
67 ch = ch.nextSiblingElement( QStringLiteral(
"map-layer-style" ) );
73 QDomDocument doc = mgrElement.ownerDocument();
74 mgrElement.setAttribute( QStringLiteral(
"current" ), mCurrentStyle );
76 const auto constStyles =
styles();
77 for (
const QString &name : constStyles )
79 QDomElement ch = doc.createElement( QStringLiteral(
"map-layer-style" ) );
80 ch.setAttribute( QStringLiteral(
"name" ), name );
81 mStyles[name].writeXml( ch );
82 mgrElement.appendChild( ch );
88 return mStyles.keys();
98 if ( name == mCurrentStyle )
106 return mStyles.value( name );
111 if ( mStyles.contains( name ) )
116 mStyles.insert( name,
style );
130 if ( !mStyles.contains( name ) )
132 if ( mStyles.count() == 1 )
136 if ( mCurrentStyle == name )
138 QStringList keys = mStyles.keys();
139 QString newCurrent = keys[0];
140 if ( newCurrent == name )
141 newCurrent = keys[1];
145 mStyles.remove( name );
152 if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
155 if ( name == mCurrentStyle )
156 mCurrentStyle = newName;
158 mStyles[newName] = mStyles[name];
159 mStyles.remove( name );
166 return mCurrentStyle;
171 if ( !mStyles.contains( name ) )
174 if ( mCurrentStyle == name )
177 mStyles[mCurrentStyle].readFromLayer( mLayer );
178 mCurrentStyle = name;
179 mStyles[mCurrentStyle].writeToLayer( mLayer );
180 mStyles[mCurrentStyle].clear();
189 if ( mOverriddenOriginalStyle )
192 mLayer->blockSignals(
true );
193 if ( mStyles.contains( styleDef ) )
199 mStyles[styleDef].writeToLayer( mLayer );
201 else if ( styleDef.startsWith(
'<' ) )
210 mLayer->blockSignals(
false );
217 if ( !mOverriddenOriginalStyle )
220 mLayer->blockSignals(
true );
222 mLayer->blockSignals(
false );
224 delete mOverriddenOriginalStyle;
225 mOverriddenOriginalStyle =
nullptr;
231 return styleName == defaultStyleName();
236 const QStringList styleNames = other->mStyles.keys();
238 for (
const QString &styleName : styleNames )
240 mStyles.remove( styleName );