21 #include <QDomElement> 22 #include <QTextStream> 31 QString QgsMapLayerStyleManager::defaultStyleName()
const 33 return tr(
"default" );
40 mCurrentStyle = defaultStyleName();
45 mCurrentStyle = mgrElement.attribute( QStringLiteral(
"current" ) );
46 if ( mCurrentStyle.isEmpty() )
49 mCurrentStyle = defaultStyleName();
53 QDomElement ch = mgrElement.firstChildElement( QStringLiteral(
"map-layer-style" ) );
54 while ( !ch.isNull() )
56 QString name = ch.attribute( QStringLiteral(
"name" ) );
60 name = defaultStyleName();
64 mStyles.insert( name, style );
66 ch = ch.nextSiblingElement( QStringLiteral(
"map-layer-style" ) );
72 QDomDocument doc = mgrElement.ownerDocument();
73 mgrElement.setAttribute( QStringLiteral(
"current" ), mCurrentStyle );
75 Q_FOREACH (
const QString &name,
styles() )
77 QDomElement ch = doc.createElement( QStringLiteral(
"map-layer-style" ) );
78 ch.setAttribute( QStringLiteral(
"name" ), name );
79 mStyles[name].writeXml( ch );
80 mgrElement.appendChild( ch );
86 return mStyles.keys();
96 if ( name == mCurrentStyle )
104 return mStyles.value( name );
109 if ( mStyles.contains( name ) )
114 mStyles.insert( name, style );
128 if ( !mStyles.contains( name ) )
130 if ( mStyles.count() == 1 )
134 if ( mCurrentStyle == name )
136 QStringList keys = mStyles.keys();
137 QString newCurrent = keys[0];
138 if ( newCurrent == name )
139 newCurrent = keys[1];
143 mStyles.remove( name );
150 if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
153 if ( name == mCurrentStyle )
154 mCurrentStyle = newName;
156 mStyles[newName] = mStyles[name];
157 mStyles.remove( name );
164 return mCurrentStyle;
169 if ( !mStyles.contains( name ) )
172 if ( mCurrentStyle == name )
175 mStyles[mCurrentStyle].readFromLayer( mLayer );
176 mCurrentStyle = name;
177 mStyles[mCurrentStyle].writeToLayer( mLayer );
178 mStyles[mCurrentStyle].clear();
187 if ( mOverriddenOriginalStyle )
190 mLayer->blockSignals(
true );
191 if ( mStyles.contains( styleDef ) )
197 mStyles[styleDef].writeToLayer( mLayer );
199 else if ( styleDef.startsWith(
'<' ) )
208 mLayer->blockSignals(
false );
215 if ( !mOverriddenOriginalStyle )
218 mLayer->blockSignals(
true );
220 mLayer->blockSignals(
false );
222 delete mOverriddenOriginalStyle;
223 mOverriddenOriginalStyle =
nullptr;
229 return styleName == defaultStyleName();
236 : mXmlData( xmlData )
242 return !mXmlData.isEmpty();
260 if ( !errorMsg.isEmpty() )
262 QgsDebugMsg(
"Failed to export style from layer: " + errorMsg );
267 QTextStream stream( &mXmlData );
268 doc.documentElement().save( stream, 0 );
278 QDomDocument doc( QStringLiteral(
"qgis" ) );
279 if ( !doc.setContent( mXmlData ) )
281 QgsDebugMsg(
"Failed to parse XML of previously stored XML data - this should not happen!" );
288 QgsDebugMsg(
"Failed to import style to layer: " + errorMsg );
295 QTextStream stream( &mXmlData );
296 styleElement.firstChildElement().save( stream, 0 );
306 docX.setContent( mXmlData );
307 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.
QStringList styles() const
Return list of all defined style names.
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...
QString xmlData() const
Return XML content of the style.
void readXml(const QDomElement &styleElement)
Read style configuration (for project file reading)
bool isValid() const
Tell whether the style is valid (i.e. there is something stored in it)
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, opacity, labeling, diagrams etc.) applicable to a map layer...
bool removeStyle(const QString &name)
Remove a stored style.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted...
void readXml(const QDomElement &mgrElement)
Read configuration (for project loading)
void styleRemoved(const QString &name)
Emitted when a style has been removed.
QgsMapLayerStyle style(const QString &name) const
Return data of a stored style - accessed by its unique name.
virtual bool importNamedStyle(QDomDocument &doc, QString &errorMsg)
Import the properties of this layer from a QDomDocument.
QgsMapLayerStyle()=default
construct invalid style
void writeToLayer(QgsMapLayer *layer) const
Apply stored layer's style information to the layer.
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
bool isDefault(const QString &styleName) const
Returns true if this is the default style.
QString currentStyle() const
Return name of the current style.
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
void writeXml(QDomElement &styleElement) const
Write style configuration (for project file writing)
QMap< QString, QgsMapLayerStyle > mapLayerStyles() const
Gets available styles for the associated map 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.
void writeXml(QDomElement &mgrElement) const
Write configuration (for project saving)
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg) const
Export the properties of this layer as named style in a QDomDocument.