QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsmapthemecollection.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmapthemecollection.h
3  --------------------------------------
4  Date : September 2014
5  Copyright : (C) 2014 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 QGSMAPTHEMECOLLECTION_H
17 #define QGSMAPTHEMECOLLECTION_H
18 
19 #include "qgis_core.h"
20 #include "qgis_sip.h"
21 #include <QMap>
22 #include <QObject>
23 #include <QPointer>
24 #include <QSet>
25 #include <QStringList>
26 
27 #include "qgsmaplayer.h"
28 
29 class QDomDocument;
30 class QgsLayerTreeModel;
31 class QgsLayerTreeNode;
32 class QgsLayerTreeGroup;
33 class QgsLayerTreeLayer;
34 class QgsProject;
35 
44 class CORE_EXPORT QgsMapThemeCollection : public QObject
45 {
46  Q_OBJECT
47 
48  Q_PROPERTY( QStringList mapThemes READ mapThemes NOTIFY mapThemesChanged )
49  Q_PROPERTY( QgsProject *project READ project WRITE setProject NOTIFY projectChanged )
50 
51  public:
52 
58  class CORE_EXPORT MapThemeLayerRecord
59  {
60  public:
62  MapThemeLayerRecord( QgsMapLayer *l = nullptr ): mLayer( l ) {}
63 
65  {
66  return mLayer == other.mLayer &&
67  usingCurrentStyle == other.usingCurrentStyle && currentStyle == other.currentStyle &&
68  usingLegendItems == other.usingLegendItems && checkedLegendItems == other.checkedLegendItems &&
69  expandedLegendItems == other.expandedLegendItems && expandedLayerNode == other.expandedLayerNode;
70  }
72  {
73  return !( *this == other );
74  }
75 
77  QgsMapLayer *layer() const { return mLayer; }
78 
80  void setLayer( QgsMapLayer *layer );
81 
83  bool usingCurrentStyle = false;
85  QString currentStyle;
87  bool usingLegendItems = false;
89  QSet<QString> checkedLegendItems;
90 
95  QSet<QString> expandedLegendItems;
96 
102  bool expandedLayerNode = false;
103  private:
105  QgsWeakMapLayerPointer mLayer;
106  };
107 
114  class CORE_EXPORT MapThemeRecord
115  {
116  public:
117 
119  {
120  return validLayerRecords() == other.validLayerRecords() &&
121  mHasExpandedStateInfo == other.mHasExpandedStateInfo && mExpandedGroupNodes == other.mExpandedGroupNodes;
122  }
124  {
125  return !( *this == other );
126  }
127 
129  QList<QgsMapThemeCollection::MapThemeLayerRecord> layerRecords() const { return mLayerRecords; }
130 
132  void setLayerRecords( const QList<QgsMapThemeCollection::MapThemeLayerRecord> &records ) { mLayerRecords = records; }
133 
135  void removeLayerRecord( QgsMapLayer *layer );
136 
138  void addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord &record );
139 
144  QHash<QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord> validLayerRecords() const SIP_SKIP;
145 
151  bool hasExpandedStateInfo() const { return mHasExpandedStateInfo; }
152 
157  void setHasExpandedStateInfo( bool hasInfo ) { mHasExpandedStateInfo = hasInfo; }
158 
166  QSet<QString> expandedGroupNodes() const { return mExpandedGroupNodes; }
167 
172  void setExpandedGroupNodes( const QSet<QString> &expandedGroupNodes ) { mExpandedGroupNodes = expandedGroupNodes; }
173 
174  private:
176  QList<MapThemeLayerRecord> mLayerRecords;
177 
179  bool mHasExpandedStateInfo = false;
180 
185  QSet<QString> mExpandedGroupNodes;
186 
187  friend class QgsMapThemeCollection;
188  };
189 
194 
199  bool hasMapTheme( const QString &name ) const;
200 
205  void insert( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
206 
213  void update( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
214 
219  void removeMapTheme( const QString &name );
220 
222  void clear();
223 
228  QStringList mapThemes() const;
229 
234  QgsMapThemeCollection::MapThemeRecord mapThemeState( const QString &name ) const { return mMapThemes[name]; }
235 
243  QStringList mapThemeVisibleLayerIds( const QString &name ) const;
244 
252  QList<QgsMapLayer *> mapThemeVisibleLayers( const QString &name ) const;
253 
258  QMap<QString, QString> mapThemeStyleOverrides( const QString &name );
259 
265  void readXml( const QDomDocument &doc );
266 
272  void writeXml( QDomDocument &doc );
273 
280 
286  void applyTheme( const QString &name, QgsLayerTreeGroup *root, QgsLayerTreeModel *model );
287 
293  QgsProject *project();
294 
299  void setProject( QgsProject *project );
300 
307  QList< QgsMapLayer * > masterLayerOrder() const;
308 
316  QList< QgsMapLayer * > masterVisibleLayers() const;
317 
318  signals:
319 
324  void mapThemesChanged();
325 
330  void mapThemeChanged( const QString &theme );
331 
338  void projectChanged();
339 
340  private slots:
341 
345  void registryLayersRemoved( const QStringList &layerIDs );
346 
348  void layerStyleRenamed( const QString &oldName, const QString &newName );
349 
350  private:
351 
355  void applyMapThemeCheckedLegendNodesToLayer( const MapThemeLayerRecord &layerRec, QgsMapLayer *layer );
356 
360  void reconnectToLayersStyleManager();
361 
362  static bool findRecordForLayer( QgsMapLayer *layer, const MapThemeRecord &rec, MapThemeLayerRecord &layerRec );
363  static MapThemeLayerRecord createThemeLayerRecord( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model );
365  static void applyThemeToLayer( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model, const MapThemeRecord &rec );
366  static void applyThemeToGroup( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, const MapThemeRecord &rec );
367 
368  typedef QMap<QString, MapThemeRecord> MapThemeRecordMap;
369  MapThemeRecordMap mMapThemes;
371  QgsProject *mProject = nullptr;
372 };
373 
374 
375 #endif // QGSMAPTHEMECOLLECTION_H
Layer tree group node serves as a container for layers and further groups.
Base class for all map layer types.
Definition: qgsmaplayer.h:63
void setProject(QgsProject *project)
The QgsProject on which this map theme collection works.
QSet< QString > expandedGroupNodes() const
Returns a set of group identifiers for group nodes that should have expanded state (other group nodes...
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme...
QHash< QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord > validLayerRecords() const
Returns set with only records for valid layers.
Individual map theme record of visible layers and styles.
MapThemeLayerRecord(QgsMapLayer *l=nullptr)
Initialize layer record with a map layer - it will be stored as a weak pointer.
void update(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Updates a map theme within the collection.
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
void clear()
Remove all map themes from the collection.
void setExpandedGroupNodes(const QSet< QString > &expandedGroupNodes)
Sets a set of group identifiers for group nodes that should have expanded state.
Individual record of a visible layer in a map theme record.
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1482
bool operator!=(const QgsMapThemeCollection::MapThemeLayerRecord &other) const
void mapThemesChanged()
Emitted when map themes within the collection are changed.
void setHasExpandedStateInfo(bool hasInfo)
Sets whether the map theme contains valid expanded/collapsed state of nodes.
void readXml(const QDomDocument &doc)
Reads the map theme collection state from XML.
void projectChanged()
Emitted when the project changes.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
void mapThemeChanged(const QString &theme)
Emitted when a map theme changes definition.
QgsMapThemeCollection(QgsProject *project=nullptr)
Create map theme collection that handles themes of the given project.
#define SIP_SKIP
Definition: qgis_sip.h:119
void removeMapTheme(const QString &name)
Remove an existing map theme from collection.
void setLayerRecords(const QList< QgsMapThemeCollection::MapThemeLayerRecord > &records)
Sets layer records for the theme.
QSet< QString > checkedLegendItems
Rule keys of check legend items in layer tree model.
This class is a base class for nodes in a layer tree.
Reads and writes project states.
Definition: qgsproject.h:89
QList< QgsMapLayer * > masterLayerOrder() const
Returns the master layer order (this will always match the project&#39;s QgsProject::layerOrder() )...
bool operator==(const QgsMapThemeCollection::MapThemeLayerRecord &other) const
void applyTheme(const QString &name, QgsLayerTreeGroup *root, QgsLayerTreeModel *model)
Apply theme given by its name and modify layer tree, current style of layers and checked legend items...
bool operator!=(const QgsMapThemeCollection::MapThemeRecord &other) const
void insert(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Inserts a new map theme to the collection.
QgsMapLayer * layer() const
Returns map layer or null if the layer does not exist anymore.
QgsMapThemeCollection::MapThemeRecord mapThemeState(const QString &name) const
Returns the recorded state of a map theme.
QStringList mapThemeVisibleLayerIds(const QString &name) const
Returns the list of layer IDs that are visible for the specified map theme.
QgsProject * project()
The QgsProject on which this map theme collection works.
QSet< QString > expandedLegendItems
Rule keys of expanded legend items in layer tree view.
QList< QgsMapLayer * > masterVisibleLayers() const
Returns the master list of visible layers.
QList< QgsMapThemeCollection::MapThemeLayerRecord > layerRecords() const
Returns a list of records for all visible layer belonging to the theme.
bool operator==(const QgsMapThemeCollection::MapThemeRecord &other) const
bool expandedLayerNode
Whether the layer&#39;s tree node is expanded (only to be applied if the parent MapThemeRecord has the in...
Container class that allows storage of map themes consisting of visible map layers and layer styles...
bool hasExpandedStateInfo() const
Returns whether information about expanded/collapsed state of nodes has been recorded and thus whethe...
static QgsMapThemeCollection::MapThemeRecord createThemeFromCurrentState(QgsLayerTreeGroup *root, QgsLayerTreeModel *model)
Static method to create theme from the current state of layer visibilities in layer tree...
QString currentStyle
Name of the current style of the layer.
bool usingCurrentStyle
Whether current style is valid and should be applied.
void writeXml(QDomDocument &doc)
Writes the map theme collection state to XML.
bool usingLegendItems
Whether checkedLegendItems should be applied.
QStringList mapThemes() const
Returns a list of existing map theme names.
Layer tree node points to a map layer.