20 #include <QDomElement> 21 #include <QTextStream> 30 QString QgsMapLayerStyleManager::defaultStyleName()
const 32 return tr(
"default" );
39 mCurrentStyle = defaultStyleName();
44 mCurrentStyle = mgrElement.attribute( QStringLiteral(
"current" ) );
45 if ( mCurrentStyle.isEmpty() )
48 mCurrentStyle = defaultStyleName();
52 QDomElement ch = mgrElement.firstChildElement( QStringLiteral(
"map-layer-style" ) );
53 while ( !ch.isNull() )
55 QString name = ch.attribute( QStringLiteral(
"name" ) );
59 name = defaultStyleName();
63 mStyles.insert( name, style );
65 ch = ch.nextSiblingElement( QStringLiteral(
"map-layer-style" ) );
71 QDomDocument doc = mgrElement.ownerDocument();
72 mgrElement.setAttribute( QStringLiteral(
"current" ), mCurrentStyle );
74 Q_FOREACH (
const QString &name,
styles() )
76 QDomElement ch = doc.createElement( QStringLiteral(
"map-layer-style" ) );
77 ch.setAttribute( QStringLiteral(
"name" ), name );
78 mStyles[name].writeXml( ch );
79 mgrElement.appendChild( ch );
85 return mStyles.keys();
95 if ( name == mCurrentStyle )
103 return mStyles.value( name );
108 if ( mStyles.contains( name ) )
113 mStyles.insert( name, style );
127 if ( !mStyles.contains( name ) )
129 if ( mStyles.count() == 1 )
133 if ( mCurrentStyle == name )
135 QStringList keys = mStyles.keys();
136 QString newCurrent = keys[0];
137 if ( newCurrent == name )
138 newCurrent = keys[1];
142 mStyles.remove( name );
149 if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
152 if ( name == mCurrentStyle )
153 mCurrentStyle = newName;
155 mStyles[newName] = mStyles[name];
156 mStyles.remove( name );
163 return mCurrentStyle;
168 if ( !mStyles.contains( name ) )
171 if ( mCurrentStyle == name )
174 mStyles[mCurrentStyle].readFromLayer( mLayer );
175 mCurrentStyle = name;
176 mStyles[mCurrentStyle].writeToLayer( mLayer );
177 mStyles[mCurrentStyle].clear();
186 if ( mOverriddenOriginalStyle )
189 mLayer->blockSignals(
true );
190 if ( mStyles.contains( styleDef ) )
196 mStyles[styleDef].writeToLayer( mLayer );
198 else if ( styleDef.startsWith(
'<' ) )
207 mLayer->blockSignals(
false );
214 if ( !mOverriddenOriginalStyle )
217 mLayer->blockSignals(
true );
219 mLayer->blockSignals(
false );
221 delete mOverriddenOriginalStyle;
222 mOverriddenOriginalStyle =
nullptr;
228 return styleName == defaultStyleName();
235 : mXmlData( xmlData )
241 return !mXmlData.isEmpty();
259 if ( !errorMsg.isEmpty() )
261 QgsDebugMsg(
"Failed to export style from layer: " + errorMsg );
266 QTextStream stream( &mXmlData );
267 doc.documentElement().save( stream, 0 );
277 QDomDocument doc( QStringLiteral(
"qgis" ) );
278 if ( !doc.setContent( mXmlData ) )
280 QgsDebugMsg(
"Failed to parse XML of previously stored XML data - this should not happen!" );
287 QgsDebugMsg(
"Failed to import style to layer: " + errorMsg );
294 QTextStream stream( &mXmlData );
295 styleElement.firstChildElement().save( stream, 0 );
305 docX.setContent( mXmlData );
306 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
Returns 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
Returns 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
Returns 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
Returns 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.