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() );
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
void clear()
Remove any stored style data (will get invalid)
QgsMapLayerStyleManager(QgsMapLayer *layer)
Construct a style manager associated with a map layer (must not be null)
Base class for all map layer types.
void currentStyleChanged(const QString ¤tName)
Emitted when the current style has been changed.
void styleRenamed(const QString &oldName, const QString &newName)
Emitted when a style has been renamed.
void reset()
Reset the style manager to a basic state - with one default style which is set as current...
void readXml(const QDomElement &styleElement)
Read style configuration (for project file reading)
void styleAdded(const QString &name)
Emitted when a new style has been added.
void readFromLayer(QgsMapLayer *layer)
Store layer's active style information in the instance.
Stores style information (renderer, transparency, labeling, diagrams etc.) applicable to a map layer...
bool removeStyle(const QString &name)
Remove a stored style.
void readXml(const QDomElement &mgrElement)
Read configuration (for project loading)
void styleRemoved(const QString &name)
Emitted when a style has been removed.
void triggerRepaint()
Will advice the map canvas (and any other interested party) that this layer requires to be repainted...
QString currentStyle() const
Return name of the current style.
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg)
Import the properties of this layer from a QDomDocument.
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
QString xmlData() const
Return XML content of the style.
QgsMapLayerStyle()
construct invalid style
bool isValid() const
Tell whether the style is valid (i.e. there is something stored in it)
QgsMapLayerStyle style(const QString &name) const
Return data of a stored style - accessed by its unique name.
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg)
Export the properties of this layer as named style in a QDomDocument.
void writeXml(QDomElement &styleElement) const
Write style configuration (for project file writing)
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
void writeToLayer(QgsMapLayer *layer) const
Apply stored layer's style information to the layer.
bool addStyle(const QString &name, const QgsMapLayerStyle &style)
Add a style with given name and data.
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.
void writeXml(QDomElement &mgrElement) const
Write configuration (for project saving)
QStringList styles() const
Return list of all defined style names.