Skip to content

Commit 2144be0

Browse files
committed
Add Python bindings for map layer style manager
1 parent f00f4fd commit 2144be0

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

python/core/core.sip

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
%Include qgsmaplayerlegend.sip
5555
%Include qgsmaplayerregistry.sip
5656
%Include qgsmaplayerrenderer.sip
57+
%Include qgsmaplayerstylemanager.sip
5758
%Include qgsmaprenderer.sip
5859
%Include qgsmaprenderercache.sip
5960
%Include qgsmaprenderercustompainterjob.sip

python/core/qgsmaplayer.sip

+20
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,21 @@ class QgsMapLayer : QObject
370370
*/
371371
QgsMapLayerLegend* legend() const;
372372

373+
/**
374+
* Enable or disable layer's style manager. When disabled (default), the styleManager() will return null pointer.
375+
* By enabling the style manager will be created with one default style (same as the layer's active style).
376+
* By disabling the style manager all associated styles will be lost (only the layer's active style will stay).
377+
* @note added in 2.8
378+
*/
379+
void enableStyleManager( bool enable = true );
380+
381+
/**
382+
* Get access to the layer's style manager. Style manager allows switching between multiple styles.
383+
* If the style manager is not enabled, null pointer will be returned.
384+
* @note added in 2.8
385+
*/
386+
QgsMapLayerStyleManager* styleManager() const;
387+
373388
/**Returns the minimum scale denominator at which the layer is visible.
374389
* Scale based visibility is only used if hasScaleBasedVisibility is true.
375390
* @returns minimum scale denominator at which the layer will render
@@ -517,6 +532,11 @@ class QgsMapLayer : QObject
517532
/** Write custom properties to project file. */
518533
void writeCustomProperties( QDomNode & layerNode, QDomDocument & doc ) const;
519534

535+
/** Read style manager's configuration (if any). To be called by subclasses. */
536+
void readStyleManager( const QDomNode& layerNode );
537+
/** Write style manager's configuration (if exists). To be called by subclasses. */
538+
void writeStyleManager( QDomNode& layerNode, QDomDocument& doc ) const;
539+
520540
/** debugging member - invoked when a connect() is made to this object */
521541
void connectNotify( const char * signal );
522542

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
class QgsMapLayerStyle
3+
{
4+
%TypeHeaderCode
5+
#include <qgsmaplayerstylemanager.h>
6+
%End
7+
public:
8+
//! construct invalid style
9+
QgsMapLayerStyle();
10+
11+
//! Tell whether the style is valid (i.e. there is something stored in it)
12+
bool isValid() const;
13+
14+
//! Return information about the style - for debugging purposes only
15+
QString dump() const;
16+
17+
//! Store layer's active style information in the instance
18+
void readFromLayer( QgsMapLayer* layer );
19+
//! Apply stored layer's style information to the layer
20+
void writeToLayer( QgsMapLayer* layer ) const;
21+
22+
//! Read style configuration (for project file reading)
23+
void readXml( const QDomElement& styleElement );
24+
//! Write style configuration (for project file writing)
25+
void writeXml( QDomElement& styleElement ) const;
26+
};
27+
28+
29+
class QgsMapLayerStyleManager
30+
{
31+
%TypeHeaderCode
32+
#include <qgsmaplayerstylemanager.h>
33+
%End
34+
public:
35+
//! Construct a style manager associated with a map layer (must not be null)
36+
QgsMapLayerStyleManager( QgsMapLayer* layer );
37+
38+
//! Read configuration (for project loading)
39+
void readXml( const QDomElement& mgrElement );
40+
//! Write configuration (for project saving)
41+
void writeXml( QDomElement& mgrElement ) const;
42+
43+
//! Return list of all defined style names
44+
QStringList styles() const;
45+
//! Return data of a stored style - accessed by its unique name
46+
QgsMapLayerStyle style( const QString& name ) const;
47+
48+
//! Add a style with given name and data
49+
//! @return true on success (name is unique and style is valid)
50+
bool addStyle( const QString& name, const QgsMapLayerStyle& style );
51+
//! Add style by cloning the current one
52+
//! @return true on success
53+
bool addStyleFromLayer( const QString& name );
54+
//! Remove a stored style
55+
//! @return true on success (style exists and it is not the last one)
56+
bool removeStyle( const QString& name );
57+
58+
//! Return name of the current style
59+
QString currentStyle() const;
60+
//! Set a different style as the current style - will apply it to the layer
61+
//! @return true on success
62+
bool setCurrentStyle( const QString& name );
63+
};

0 commit comments

Comments
 (0)