Branch data Line data Source code
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 "qgsmaplayerstyle.h" 22 : : 23 : : #include <QByteArray> 24 : : #include <QMap> 25 : : #include <QStringList> 26 : : #include <QObject> 27 : : 28 : : 29 : : class QDomElement; 30 : : 31 : : class QgsMapLayer; 32 : : 33 : : /** 34 : : * \ingroup core 35 : : * \brief Management of styles for use with one map layer 36 : : * 37 : : * Stored styles are identified by their names. The manager 38 : : * always keep track of which style of the stored ones is currently active. When the current style is changed, 39 : : * the new style is applied to the associated layer. 40 : : * 41 : : * The class takes care of updating itself when the layer's current style configuration changes. 42 : : * When some of layer style's properties change (e.g. opacity / colors), the style manager will 43 : : * record them in the currently active style without any extra effort required. 44 : : * 45 : : * When an instance is created, it creates "default" style (with empty name) recorded from the associated map layer 46 : : * and it is set as the current style. The instance must always contain at least one style (which is the current). 47 : : * 48 : : * The style which is marked as current has no style data stored in its entry. This is to avoid duplication 49 : : * of data as all data is stored in map layer and can be retrieved easily. Also when saving, the entry for 50 : : * the current style will be saved with no data because everything is already saved by the map layer itself. 51 : : * 52 : : * The class also features support for temporary change of the layer's style, ideal for short-term use of a custom 53 : : * style followed by restoration of the original style (for example, when rendering a map with a different than current style). 54 : : * 55 : : * \since QGIS 2.8 56 : : */ 57 : : class CORE_EXPORT QgsMapLayerStyleManager : public QObject 58 : : { 59 : 166 : Q_OBJECT 60 : : public: 61 : : 62 : : /** 63 : : * Construct a style manager associated with a map layer (must not be NULLPTR). 64 : : * The style manager will be parented to \a layer. 65 : : */ 66 : : QgsMapLayerStyleManager( QgsMapLayer *layer SIP_TRANSFERTHIS ); 67 : : 68 : : //! Gets pointer to the associated map layer 69 : 0 : QgsMapLayer *layer() const { return mLayer; } 70 : : 71 : : //! Reset the style manager to a basic state - with one default style which is set as current 72 : : void reset(); 73 : : 74 : : //! Read configuration (for project loading) 75 : : void readXml( const QDomElement &mgrElement ); 76 : : //! Write configuration (for project saving) 77 : : void writeXml( QDomElement &mgrElement ) const; 78 : : 79 : : //! Returns list of all defined style names 80 : : QStringList styles() const; 81 : : 82 : : /** 83 : : * Gets available styles for the associated map layer. 84 : : * \returns A map of map layer style by style name 85 : : * \since QGIS 3.0 86 : : */ 87 : : QMap<QString, QgsMapLayerStyle> mapLayerStyles() const; 88 : : 89 : : //! Returns data of a stored style - accessed by its unique name 90 : : QgsMapLayerStyle style( const QString &name ) const; 91 : : 92 : : /** 93 : : * Add a style with given name and data 94 : : * \returns TRUE on success (name is unique and style is valid) 95 : : */ 96 : : bool addStyle( const QString &name, const QgsMapLayerStyle &style ); 97 : : 98 : : /** 99 : : * Add style by cloning the current one 100 : : * \returns TRUE on success 101 : : */ 102 : : bool addStyleFromLayer( const QString &name ); 103 : : 104 : : /** 105 : : * Remove a stored style 106 : : * \returns TRUE on success (style exists and it is not the last one) 107 : : */ 108 : : bool removeStyle( const QString &name ); 109 : : 110 : : /** 111 : : * Rename a stored style to a different name 112 : : * \returns TRUE on success (style exists and new name is unique) 113 : : */ 114 : : bool renameStyle( const QString &name, const QString &newName ); 115 : : 116 : : //! Returns name of the current style 117 : : QString currentStyle() const; 118 : : 119 : : /** 120 : : * Set a different style as the current style - will apply it to the layer 121 : : * \returns TRUE on success 122 : : */ 123 : : bool setCurrentStyle( const QString &name ); 124 : : 125 : : /** 126 : : * Temporarily apply a different style to the layer. The argument 127 : : * can be either a style name or a full QML style definition. 128 : : * Each call must be paired with restoreOverrideStyle() 129 : : */ 130 : : bool setOverrideStyle( const QString &styleDef ); 131 : : //! Restore the original store after a call to setOverrideStyle() 132 : : bool restoreOverrideStyle(); 133 : : 134 : : /** 135 : : * Returns TRUE if this is the default style 136 : : * 137 : : * \since QGIS 3.0 138 : : */ 139 : : bool isDefault( const QString &styleName ) const; 140 : : 141 : : /** 142 : : * Copies all styles from \a other. 143 : : * In case there is already a style with the same name it will be overwritten. 144 : : * 145 : : * \since QGIS 3.14 146 : : */ 147 : : void copyStylesFrom( QgsMapLayerStyleManager *other ); 148 : : 149 : : signals: 150 : : //! Emitted when a new style has been added 151 : : void styleAdded( const QString &name ); 152 : : //! Emitted when a style has been removed 153 : : void styleRemoved( const QString &name ); 154 : : //! Emitted when a style has been renamed 155 : : void styleRenamed( const QString &oldName, const QString &newName ); 156 : : //! Emitted when the current style has been changed 157 : : void currentStyleChanged( const QString ¤tName ); 158 : : 159 : : private: 160 : : QgsMapLayer *mLayer = nullptr; 161 : : QMap<QString, QgsMapLayerStyle> mStyles; 162 : : QString mCurrentStyle; 163 : : QgsMapLayerStyle *mOverriddenOriginalStyle = nullptr; 164 : : QString defaultStyleName() const; 165 : : }; 166 : : 167 : : #endif // QGSMAPLAYERSTYLEMANAGER_H