QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsmaplayerstylemanager.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayerstylemanager.h
3  --------------------------------------
4  Date : January 2015
5  Copyright : (C) 2015 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSMAPLAYERSTYLEMANAGER_H
17 #define QGSMAPLAYERSTYLEMANAGER_H
18 
19 #include "qgis_core.h"
20 #include "qgis_sip.h"
21 #include "qgsmaplayer.h"
22 
23 #include <QByteArray>
24 #include <QMap>
25 #include <QStringList>
26 #include <QObject>
27 
28 
29 class QDomElement;
30 
40 class CORE_EXPORT QgsMapLayerStyle
41 {
42  public:
44  QgsMapLayerStyle() = default;
45 
47  explicit QgsMapLayerStyle( const QString &xmlData );
48 
50  bool isValid() const;
51 
53  void clear();
54 
56  QString xmlData() const;
57 
59  void readFromLayer( QgsMapLayer *layer );
61  void writeToLayer( QgsMapLayer *layer ) const;
62 
64  void readXml( const QDomElement &styleElement );
66  void writeXml( QDomElement &styleElement ) const;
67 
68  private:
69  QString mXmlData;
70 };
71 
72 
95 class CORE_EXPORT QgsMapLayerStyleManager : public QObject
96 {
97  Q_OBJECT
98  public:
99 
105 
107  QgsMapLayer *layer() const { return mLayer; }
108 
110  void reset();
111 
113  void readXml( const QDomElement &mgrElement );
115  void writeXml( QDomElement &mgrElement ) const;
116 
118  QStringList styles() const;
119 
125  QMap<QString, QgsMapLayerStyle> mapLayerStyles() const;
126 
128  QgsMapLayerStyle style( const QString &name ) const;
129 
134  bool addStyle( const QString &name, const QgsMapLayerStyle &style );
135 
140  bool addStyleFromLayer( const QString &name );
141 
146  bool removeStyle( const QString &name );
147 
152  bool renameStyle( const QString &name, const QString &newName );
153 
155  QString currentStyle() const;
156 
161  bool setCurrentStyle( const QString &name );
162 
168  bool setOverrideStyle( const QString &styleDef );
170  bool restoreOverrideStyle();
171 
177  bool isDefault( const QString &styleName ) const;
178 
179  signals:
181  void styleAdded( const QString &name );
183  void styleRemoved( const QString &name );
185  void styleRenamed( const QString &oldName, const QString &newName );
187  void currentStyleChanged( const QString &currentName );
188 
189  private:
190  QgsMapLayer *mLayer = nullptr;
191  QMap<QString, QgsMapLayerStyle> mStyles;
192  QString mCurrentStyle;
193  QgsMapLayerStyle *mOverriddenOriginalStyle = nullptr;
194  QString defaultStyleName() const;
195 };
196 
197 
204 class CORE_EXPORT QgsMapLayerStyleOverride
205 {
206  public:
207 
213  : mLayer( layer )
214  {
215  }
216 
218  {
219  if ( mLayer && mStyleOverridden )
220  mLayer->styleManager()->restoreOverrideStyle();
221  }
222 
227  void setOverrideStyle( const QString &style )
228  {
229  if ( mLayer )
230  {
231  if ( mStyleOverridden )
232  mLayer->styleManager()->restoreOverrideStyle();
233 
234  mLayer->styleManager()->setOverrideStyle( style );
235  mStyleOverridden = true;
236  }
237  }
238 
239  private:
240 
241  QgsMapLayer *mLayer = nullptr;
242  bool mStyleOverridden = false;
243 };
244 #endif // QGSMAPLAYERSTYLEMANAGER_H
Base class for all map layer types.
Definition: qgsmaplayer.h:61
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
Restore overridden layer style on destruction.
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer...
QgsMapLayer * layer() const
Gets pointer to the associated map layer.
void setOverrideStyle(const QString &style)
Temporarily apply a different style to the layer.
Management of styles for use with one map layer.
QgsMapLayerStyleOverride(QgsMapLayer *layer)
Construct a style override object associated with a map layer.