17#include "moc_qgsmaplayerstylemanager.cpp"
33QString QgsMapLayerStyleManager::defaultStyleName()
35 return tr(
"default" );
42 mCurrentStyle = defaultStyleName();
47 mCurrentStyle = mgrElement.attribute( QStringLiteral(
"current" ) );
48 if ( mCurrentStyle.isEmpty() )
51 mCurrentStyle = defaultStyleName();
55 QDomElement ch = mgrElement.firstChildElement( QStringLiteral(
"map-layer-style" ) );
56 while ( !ch.isNull() )
58 QString name = ch.attribute( QStringLiteral(
"name" ) );
62 name = defaultStyleName();
66 mStyles.insert( name,
style );
68 ch = ch.nextSiblingElement( QStringLiteral(
"map-layer-style" ) );
74 QDomDocument doc = mgrElement.ownerDocument();
75 mgrElement.setAttribute( QStringLiteral(
"current" ), mCurrentStyle );
77 const auto constStyles =
styles();
78 for (
const QString &name : constStyles )
80 QDomElement ch = doc.createElement( QStringLiteral(
"map-layer-style" ) );
81 ch.setAttribute( QStringLiteral(
"name" ), name );
82 mStyles[name].writeXml( ch );
83 mgrElement.appendChild( ch );
89 return mStyles.keys();
99 if ( name == mCurrentStyle )
107 return mStyles.value( name );
112 if ( mStyles.contains( name ) )
117 mStyles.insert( name,
style );
131 if ( !mStyles.contains( name ) )
133 if ( mStyles.count() == 1 )
137 if ( mCurrentStyle == name )
139 QStringList keys = mStyles.keys();
140 QString newCurrent = keys[0];
141 if ( newCurrent == name )
142 newCurrent = keys[1];
146 mStyles.remove( name );
153 if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
156 if ( name == mCurrentStyle )
157 mCurrentStyle = newName;
159 mStyles[newName] = mStyles[name];
160 mStyles.remove( name );
167 return mCurrentStyle;
172 if ( !mStyles.contains( name ) )
175 if ( mCurrentStyle == name )
178 mStyles[mCurrentStyle].readFromLayer( mLayer );
179 mCurrentStyle = name;
180 mStyles[mCurrentStyle].writeToLayer( mLayer );
181 mStyles[mCurrentStyle].clear();
190 if ( mOverriddenOriginalStyle )
193 mLayer->blockSignals(
true );
194 if ( mStyles.contains( styleDef ) )
200 mStyles[styleDef].writeToLayer( mLayer );
202 else if ( styleDef.startsWith(
'<' ) )
211 mLayer->blockSignals(
false );
218 if ( !mOverriddenOriginalStyle )
221 mLayer->blockSignals(
true );
223 mLayer->blockSignals(
false );
225 delete mOverriddenOriginalStyle;
226 mOverriddenOriginalStyle =
nullptr;
232 return styleName == defaultStyleName();
237 const QStringList styleNames = other->mStyles.keys();
239 for (
const QString &styleName : styleNames )
241 mStyles.remove( styleName );
Management of styles for use with one map layer.
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()
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 readXml(const QDomElement &styleElement)
Read style configuration (for project file reading)
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.
bool isValid() const
Tell whether the style is valid (i.e. there is something stored in it)
Base class for all map layer types.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.