21 #include <QDomElement>
22 #include <QTextStream>
26 , mOverriddenOriginalStyle( 0 )
34 mCurrentStyle.clear();
39 mCurrentStyle = mgrElement.attribute(
"current" );
42 QDomElement ch = mgrElement.firstChildElement(
"map-layer-style" );
43 while ( !ch.isNull() )
45 QString name = ch.attribute(
"name" );
48 mStyles.insert( name, style );
50 ch = ch.nextSiblingElement(
"map-layer-style" );
56 QDomDocument doc = mgrElement.ownerDocument();
57 mgrElement.setAttribute(
"current", mCurrentStyle );
59 foreach (
const QString& name,
styles() )
61 QDomElement ch = doc.createElement(
"map-layer-style" );
62 ch.setAttribute(
"name", name );
63 mStyles[name].writeXml( ch );
64 mgrElement.appendChild( ch );
70 return mStyles.keys();
75 if ( name == mCurrentStyle )
83 return mStyles.value( name );
88 if ( mStyles.contains( name ) )
93 mStyles.insert( name, style );
107 if ( !mStyles.contains( name ) )
109 if ( mStyles.count() == 1 )
113 if ( mCurrentStyle == name )
115 QStringList keys = mStyles.keys();
116 QString newCurrent = keys[0];
117 if ( newCurrent == name )
118 newCurrent = keys[1];
122 mStyles.remove( name );
129 if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
132 if ( name == mCurrentStyle )
133 mCurrentStyle = newName;
135 mStyles[newName] = mStyles[name];
136 mStyles.remove( name );
143 return mCurrentStyle;
148 if ( !mStyles.contains( name ) )
151 if ( mCurrentStyle == name )
154 mStyles[mCurrentStyle].readFromLayer( mLayer );
155 mCurrentStyle = name;
156 mStyles[mCurrentStyle].writeToLayer( mLayer );
157 mStyles[mCurrentStyle].clear();
166 if ( mOverriddenOriginalStyle )
169 if ( mStyles.contains( styleDef ) )
175 mStyles[styleDef].writeToLayer( mLayer );
177 else if ( styleDef.startsWith(
'<' ) )
192 if ( !mOverriddenOriginalStyle )
196 delete mOverriddenOriginalStyle;
197 mOverriddenOriginalStyle = 0;
209 : mXmlData( xmlData )
215 return !mXmlData.isEmpty();
233 if ( !errorMsg.isEmpty() )
235 QgsDebugMsg(
"Failed to export style from layer: " + errorMsg );
240 QTextStream stream( &mXmlData );
241 doc.documentElement().save( stream, 0 );
246 QDomDocument doc(
"qgis" );
247 if ( !doc.setContent( mXmlData ) )
249 QgsDebugMsg(
"Failed to parse XML of previously stored XML data - this should not happen!" );
256 QgsDebugMsg(
"Failed to import style to layer: " + errorMsg );
263 QTextStream stream( &mXmlData );
264 styleElement.firstChildElement().save( stream, 0 );
274 docX.setContent( mXmlData );
275 styleElement.appendChild( docX.documentElement() );