22 #include <QDomElement> 23 #include <QTextStream> 32 QString QgsMapLayerStyleManager::defaultStyleName()
const 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 Q_FOREACH (
const QString &name,
styles() )
78 QDomElement ch = doc.createElement( QStringLiteral(
"map-layer-style" ) );
79 ch.setAttribute( QStringLiteral(
"name" ), name );
80 mStyles[name].writeXml( ch );
81 mgrElement.appendChild( ch );
87 return mStyles.keys();
97 if ( name == mCurrentStyle )
105 return mStyles.value( name );
110 if ( mStyles.contains( name ) )
115 mStyles.insert( name, style );
129 if ( !mStyles.contains( name ) )
131 if ( mStyles.count() == 1 )
135 if ( mCurrentStyle == name )
137 QStringList keys = mStyles.keys();
138 QString newCurrent = keys[0];
139 if ( newCurrent == name )
140 newCurrent = keys[1];
144 mStyles.remove( name );
151 if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
154 if ( name == mCurrentStyle )
155 mCurrentStyle = newName;
157 mStyles[newName] = mStyles[name];
158 mStyles.remove( name );
165 return mCurrentStyle;
170 if ( !mStyles.contains( name ) )
173 if ( mCurrentStyle == name )
176 mStyles[mCurrentStyle].readFromLayer( mLayer );
177 mCurrentStyle = name;
178 mStyles[mCurrentStyle].writeToLayer( mLayer );
179 mStyles[mCurrentStyle].clear();
188 if ( mOverriddenOriginalStyle )
191 mLayer->blockSignals(
true );
192 if ( mStyles.contains( styleDef ) )
198 mStyles[styleDef].writeToLayer( mLayer );
200 else if ( styleDef.startsWith(
'<' ) )
209 mLayer->blockSignals(
false );
216 if ( !mOverriddenOriginalStyle )
219 mLayer->blockSignals(
true );
221 mLayer->blockSignals(
false );
223 delete mOverriddenOriginalStyle;
224 mOverriddenOriginalStyle =
nullptr;
230 return styleName == defaultStyleName();
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
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, opacity, labeling, diagrams etc.) applicable to a map layer...
bool removeStyle(const QString &name)
Remove a stored style.
bool isDefault(const QString &styleName) const
Returns true if this is the default 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.
QMap< QString, QgsMapLayerStyle > mapLayerStyles() const
Gets available styles for the associated map layer.
QString currentStyle() const
Returns name of the current style.
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
bool isValid() const
Tell whether the style is valid (i.e. there is something stored in it)
QgsMapLayerStyle style(const QString &name) const
Returns data of a stored style - accessed by its unique name.
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
Returns list of all defined style names.