27#include "moc_qgsmaplayerstylemanager.cpp"
29using namespace Qt::StringLiterals;
38QString QgsMapLayerStyleManager::defaultStyleName()
40 return tr(
"default" );
49 mCurrentStyle = defaultStyleName();
56 mCurrentStyle = mgrElement.attribute( u
"current"_s );
57 if ( mCurrentStyle.isEmpty() )
60 mCurrentStyle = defaultStyleName();
64 QDomElement ch = mgrElement.firstChildElement( u
"map-layer-style"_s );
65 while ( !ch.isNull() )
67 QString name = ch.attribute( u
"name"_s );
71 name = defaultStyleName();
75 mStyles.insert( name,
style );
77 ch = ch.nextSiblingElement( u
"map-layer-style"_s );
85 QDomDocument doc = mgrElement.ownerDocument();
86 mgrElement.setAttribute( u
"current"_s, mCurrentStyle );
88 const auto constStyles =
styles();
89 for (
const QString &name : constStyles )
91 QDomElement ch = doc.createElement( u
"map-layer-style"_s );
92 ch.setAttribute( u
"name"_s, name );
93 mStyles[name].writeXml( ch );
94 mgrElement.appendChild( ch );
102 return mStyles.keys();
116 if ( name == mCurrentStyle )
124 return mStyles.value( name );
131 if ( mStyles.contains( name ) )
133 if ( !
style.isValid() )
136 mStyles.insert( name,
style );
146 style.readFromLayer( mLayer );
154 if ( !mStyles.contains( name ) )
156 if ( mStyles.count() == 1 )
160 if ( mCurrentStyle == name )
162 QStringList keys = mStyles.keys();
163 QString newCurrent = keys[0];
164 if ( newCurrent == name )
165 newCurrent = keys[1];
169 mStyles.remove( name );
178 if ( !mStyles.contains( name ) || mStyles.contains( newName ) )
181 if ( name == mCurrentStyle )
182 mCurrentStyle = newName;
184 mStyles[newName] = mStyles[name];
185 mStyles.remove( name );
194 return mCurrentStyle;
201 if ( !mStyles.contains( name ) )
204 if ( mCurrentStyle == name )
207 mStyles[mCurrentStyle].readFromLayer( mLayer );
208 mCurrentStyle = name;
209 mStyles[mCurrentStyle].writeToLayer( mLayer );
210 mStyles[mCurrentStyle].clear();
213 mLayer->triggerRepaint();
221 if ( mOverriddenOriginalStyle )
224 mLayer->blockSignals(
true );
225 if ( mStyles.contains( styleDef ) )
231 mStyles[styleDef].writeToLayer( mLayer );
233 else if ( styleDef.startsWith(
'<' ) )
242 mLayer->blockSignals(
false );
251 if ( !mOverriddenOriginalStyle )
254 mLayer->blockSignals(
true );
255 mOverriddenOriginalStyle->writeToLayer( mLayer );
256 mLayer->blockSignals(
false );
258 delete mOverriddenOriginalStyle;
259 mOverriddenOriginalStyle =
nullptr;
265 return styleName == defaultStyleName();
272 const QStringList styleNames = other->mStyles.keys();
274 for (
const QString &styleName : styleNames )
276 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.
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS