QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
Signals | Public Member Functions | Static Public Member Functions | List of all members
QgsMapLayerStyleManager Class Reference

Management of styles for use with one map layer. More...

#include <qgsmaplayerstylemanager.h>

Inheritance diagram for QgsMapLayerStyleManager:
Inheritance graph
[legend]

Signals

void currentStyleChanged (const QString &currentName)
 Emitted when the current style has been changed.
 
void styleAdded (const QString &name)
 Emitted when a new style has been added.
 
void styleRemoved (const QString &name)
 Emitted when a style has been removed.
 
void styleRenamed (const QString &oldName, const QString &newName)
 Emitted when a style has been renamed.
 

Public Member Functions

 QgsMapLayerStyleManager (QgsMapLayer *layer)
 Construct a style manager associated with a map layer (must not be nullptr).
 
bool addStyle (const QString &name, const QgsMapLayerStyle &style)
 Add a style with given name and data.
 
bool addStyleFromLayer (const QString &name)
 Add style by cloning the current one.
 
void copyStylesFrom (QgsMapLayerStyleManager *other)
 Copies all styles from other.
 
QString currentStyle () const
 Returns name of the current style.
 
QgsMapLayerlayer () const
 Gets pointer to the associated map layer.
 
QMap< QString, QgsMapLayerStylemapLayerStyles () const
 Gets available styles for the associated map layer.
 
void readXml (const QDomElement &mgrElement)
 Read configuration (for project loading)
 
bool removeStyle (const QString &name)
 Remove a stored style.
 
bool renameStyle (const QString &name, const QString &newName)
 Rename a stored style to a different name.
 
void reset ()
 Reset the style manager to a basic state - with one default style which is set as current.
 
bool restoreOverrideStyle ()
 Restore the original store after a call to setOverrideStyle()
 
bool setCurrentStyle (const QString &name)
 Set a different style as the current style - will apply it to the 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.
 
QStringList styles () const
 Returns list of all defined style names.
 
void writeXml (QDomElement &mgrElement) const
 Write configuration (for project saving)
 

Static Public Member Functions

static bool isDefault (const QString &styleName)
 Returns true if this is the default style.
 

Detailed Description

Management of styles for use with one map layer.

Stored styles are identified by their names. The manager always keep track of which style of the stored ones is currently active. When the current style is changed, the new style is applied to the associated layer.

The class takes care of updating itself when the layer's current style configuration changes. When some of layer style's properties change (e.g. opacity / colors), the style manager will record them in the currently active style without any extra effort required.

When an instance is created, it creates "default" style (with empty name) recorded from the associated map layer and it is set as the current style. The instance must always contain at least one style (which is the current).

The style which is marked as current has no style data stored in its entry. This is to avoid duplication of data as all data is stored in map layer and can be retrieved easily. Also when saving, the entry for the current style will be saved with no data because everything is already saved by the map layer itself.

The class also features support for temporary change of the layer's style, ideal for short-term use of a custom style followed by restoration of the original style (for example, when rendering a map with a different than current style).

Definition at line 56 of file qgsmaplayerstylemanager.h.

Constructor & Destructor Documentation

◆ QgsMapLayerStyleManager()

QgsMapLayerStyleManager::QgsMapLayerStyleManager ( QgsMapLayer layer)

Construct a style manager associated with a map layer (must not be nullptr).

The style manager will be parented to layer.

Definition at line 26 of file qgsmaplayerstylemanager.cpp.

Member Function Documentation

◆ addStyle()

bool QgsMapLayerStyleManager::addStyle ( const QString &  name,
const QgsMapLayerStyle style 
)

Add a style with given name and data.

Returns
true on success (name is unique and style is valid)

Definition at line 110 of file qgsmaplayerstylemanager.cpp.

◆ addStyleFromLayer()

bool QgsMapLayerStyleManager::addStyleFromLayer ( const QString &  name)

Add style by cloning the current one.

Returns
true on success

Definition at line 122 of file qgsmaplayerstylemanager.cpp.

◆ copyStylesFrom()

void QgsMapLayerStyleManager::copyStylesFrom ( QgsMapLayerStyleManager other)

Copies all styles from other.

In case there is already a style with the same name it will be overwritten.

Since
QGIS 3.14

Definition at line 235 of file qgsmaplayerstylemanager.cpp.

◆ currentStyle()

QString QgsMapLayerStyleManager::currentStyle ( ) const

Returns name of the current style.

Definition at line 165 of file qgsmaplayerstylemanager.cpp.

◆ currentStyleChanged

void QgsMapLayerStyleManager::currentStyleChanged ( const QString &  currentName)
signal

Emitted when the current style has been changed.

◆ isDefault()

bool QgsMapLayerStyleManager::isDefault ( const QString &  styleName)
static

Returns true if this is the default style.

Definition at line 230 of file qgsmaplayerstylemanager.cpp.

◆ layer()

QgsMapLayer * QgsMapLayerStyleManager::layer ( ) const
inline

Gets pointer to the associated map layer.

Definition at line 68 of file qgsmaplayerstylemanager.h.

◆ mapLayerStyles()

QMap< QString, QgsMapLayerStyle > QgsMapLayerStyleManager::mapLayerStyles ( ) const

Gets available styles for the associated map layer.

Returns
A map of map layer style by style name

Definition at line 92 of file qgsmaplayerstylemanager.cpp.

◆ readXml()

void QgsMapLayerStyleManager::readXml ( const QDomElement &  mgrElement)

Read configuration (for project loading)

Definition at line 45 of file qgsmaplayerstylemanager.cpp.

◆ removeStyle()

bool QgsMapLayerStyleManager::removeStyle ( const QString &  name)

Remove a stored style.

Returns
true on success (style exists and it is not the last one)

Definition at line 129 of file qgsmaplayerstylemanager.cpp.

◆ renameStyle()

bool QgsMapLayerStyleManager::renameStyle ( const QString &  name,
const QString &  newName 
)

Rename a stored style to a different name.

Returns
true on success (style exists and new name is unique)

Definition at line 151 of file qgsmaplayerstylemanager.cpp.

◆ reset()

void QgsMapLayerStyleManager::reset ( )

Reset the style manager to a basic state - with one default style which is set as current.

Definition at line 39 of file qgsmaplayerstylemanager.cpp.

◆ restoreOverrideStyle()

bool QgsMapLayerStyleManager::restoreOverrideStyle ( )

Restore the original store after a call to setOverrideStyle()

Definition at line 216 of file qgsmaplayerstylemanager.cpp.

◆ setCurrentStyle()

bool QgsMapLayerStyleManager::setCurrentStyle ( const QString &  name)

Set a different style as the current style - will apply it to the layer.

Returns
true on success

Definition at line 170 of file qgsmaplayerstylemanager.cpp.

◆ setOverrideStyle()

bool QgsMapLayerStyleManager::setOverrideStyle ( const QString &  styleDef)

Temporarily apply a different style to the layer.

The argument can be either a style name or a full QML style definition. Each call must be paired with restoreOverrideStyle()

Definition at line 188 of file qgsmaplayerstylemanager.cpp.

◆ style()

QgsMapLayerStyle QgsMapLayerStyleManager::style ( const QString &  name) const

Returns data of a stored style - accessed by its unique name.

Definition at line 97 of file qgsmaplayerstylemanager.cpp.

◆ styleAdded

void QgsMapLayerStyleManager::styleAdded ( const QString &  name)
signal

Emitted when a new style has been added.

◆ styleRemoved

void QgsMapLayerStyleManager::styleRemoved ( const QString &  name)
signal

Emitted when a style has been removed.

◆ styleRenamed

void QgsMapLayerStyleManager::styleRenamed ( const QString &  oldName,
const QString &  newName 
)
signal

Emitted when a style has been renamed.

◆ styles()

QStringList QgsMapLayerStyleManager::styles ( ) const

Returns list of all defined style names.

Definition at line 87 of file qgsmaplayerstylemanager.cpp.

◆ writeXml()

void QgsMapLayerStyleManager::writeXml ( QDomElement &  mgrElement) const

Write configuration (for project saving)

Definition at line 72 of file qgsmaplayerstylemanager.cpp.


The documentation for this class was generated from the following files: