25#include "moc_qgsmaplayerstylemanager.cpp"
34QString QgsMapLayerStyleManager::defaultStyleName()
36 return tr(
"default" );
43 mCurrentStyle = defaultStyleName();
48 mCurrentStyle = mgrElement.attribute( QStringLiteral(
"current" ) );
49 if ( mCurrentStyle.isEmpty() )
52 mCurrentStyle = defaultStyleName();
56 QDomElement ch = mgrElement.firstChildElement( QStringLiteral(
"map-layer-style" ) );
57 while ( !ch.isNull() )
59 QString name = ch.attribute( QStringLiteral(
"name" ) );
63 name = defaultStyleName();
67 mStyles.insert( name,
style );
69 ch = ch.nextSiblingElement( QStringLiteral(
"map-layer-style" ) );
75 QDomDocument doc = mgrElement.ownerDocument();
76 mgrElement.setAttribute( QStringLiteral(
"current" ), mCurrentStyle );
78 const auto constStyles =
styles();
79 for (
const QString &name : constStyles )
81 QDomElement ch = doc.createElement( QStringLiteral(
"map-layer-style" ) );
82 ch.setAttribute( QStringLiteral(
"name" ), name );
83 mStyles[name].writeXml( ch );
84 mgrElement.appendChild( ch );
90 return mStyles.keys();
100 if ( name == mCurrentStyle )
108 return mStyles.value( name );
113 if ( mStyles.contains( name ) )
115 if ( !
style.isValid() )
118 mStyles.insert( name,
style );
126 style.readFromLayer( mLayer );
132 if ( !mStyles.contains( name ) )
134 if ( mStyles.count() == 1 )
138 if ( mCurrentStyle == name )
140 QStringList keys = mStyles.keys();
141 QString newCurrent = keys[0];
142 if ( newCurrent == name )
143 newCurrent = keys[1];
147 mStyles.remove( name );
154 if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
157 if ( name == mCurrentStyle )
158 mCurrentStyle = newName;
160 mStyles[newName] = mStyles[name];
161 mStyles.remove( name );
168 return mCurrentStyle;
173 if ( !mStyles.contains( name ) )
176 if ( mCurrentStyle == name )
179 mStyles[mCurrentStyle].readFromLayer( mLayer );
180 mCurrentStyle = name;
181 mStyles[mCurrentStyle].writeToLayer( mLayer );
182 mStyles[mCurrentStyle].clear();
185 mLayer->triggerRepaint();
191 if ( mOverriddenOriginalStyle )
194 mLayer->blockSignals(
true );
195 if ( mStyles.contains( styleDef ) )
201 mStyles[styleDef].writeToLayer( mLayer );
203 else if ( styleDef.startsWith(
'<' ) )
212 mLayer->blockSignals(
false );
219 if ( !mOverriddenOriginalStyle )
222 mLayer->blockSignals(
true );
223 mOverriddenOriginalStyle->writeToLayer( mLayer );
224 mLayer->blockSignals(
false );
226 delete mOverriddenOriginalStyle;
227 mOverriddenOriginalStyle =
nullptr;
233 return styleName == defaultStyleName();
238 const QStringList styleNames = other->mStyles.keys();
240 for (
const QString &styleName : styleNames )
242 mStyles.remove( styleName );
bool addStyle(const QString &name, const QgsMapLayerStyle &style)
Add a style with given name and data.
QString currentStyle() const
Returns name of the current style.
bool removeStyle(const QString &name)
Remove a stored style.
void copyStylesFrom(QgsMapLayerStyleManager *other)
Copies all styles from other.
QStringList styles() const
Returns list of all defined style names.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.
static bool isDefault(const QString &styleName)
Returns true if this is the default style.
void styleAdded(const QString &name)
Emitted when a new style has been added.
void writeXml(QDomElement &mgrElement) const
Write configuration (for project saving).
void reset()
Reset the style manager to a basic state - with one default style which is set as current.
void styleRenamed(const QString &oldName, const QString &newName)
Emitted when a style has been renamed.
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle().
QgsMapLayer * layer() const
Gets pointer to the associated map layer.
bool addStyleFromLayer(const QString &name)
Add style by cloning the current one.
bool renameStyle(const QString &name, const QString &newName)
Rename a stored style to a different name.
QgsMapLayerStyleManager(QgsMapLayer *layer)
Construct a style manager associated with a map layer (must not be nullptr).
QMap< QString, QgsMapLayerStyle > mapLayerStyles() const
Gets available styles for the associated map layer.
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
QgsMapLayerStyle style(const QString &name) const
Returns data of a stored style - accessed by its unique name.
void readXml(const QDomElement &mgrElement)
Read configuration (for project loading).
void currentStyleChanged(const QString ¤tName)
Emitted when the current style has been changed.
void styleRemoved(const QString &name)
Emitted when a style has been removed.
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer.
void readFromLayer(QgsMapLayer *layer)
Store layer's active style information in the instance.
void writeToLayer(QgsMapLayer *layer) const
Apply stored layer's style information to the layer.
Base class for all map layer types.