QGIS API Documentation  3.17.0-Master (a035f434f4)
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 && isVisible == other.isVisible &&
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 
86  bool isVisible = true;
87 
89  bool usingCurrentStyle = false;
91  QString currentStyle;
93  bool usingLegendItems = false;
95  QSet<QString> checkedLegendItems;
96 
101  QSet<QString> expandedLegendItems;
102 
108  bool expandedLayerNode = false;
109  private:
111  QgsWeakMapLayerPointer mLayer;
112  };
113 
120  class CORE_EXPORT MapThemeRecord
121  {
122  public:
123 
125  {
126  return validLayerRecords() == other.validLayerRecords() &&
127  mHasExpandedStateInfo == other.mHasExpandedStateInfo &&
128  mExpandedGroupNodes == other.mExpandedGroupNodes && mCheckedGroupNodes == other.mCheckedGroupNodes;
129  }
131  {
132  return !( *this == other );
133  }
134 
136  QList<QgsMapThemeCollection::MapThemeLayerRecord> layerRecords() const { return mLayerRecords; }
137 
139  void setLayerRecords( const QList<QgsMapThemeCollection::MapThemeLayerRecord> &records ) { mLayerRecords = records; }
140 
142  void removeLayerRecord( QgsMapLayer *layer );
143 
145  void addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord &record );
146 
150  QHash<QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord> validLayerRecords() const SIP_SKIP;
151 
157  bool hasExpandedStateInfo() const { return mHasExpandedStateInfo; }
158 
165  bool hasCheckedStateInfo() const { return mHasCheckedStateInfo; } SIP_SKIP;
166 
171  void setHasExpandedStateInfo( bool hasInfo ) { mHasExpandedStateInfo = hasInfo; }
172 
178  void setHasCheckedStateInfo( bool hasInfo ) { mHasCheckedStateInfo = hasInfo; } SIP_SKIP;
179 
187  QSet<QString> expandedGroupNodes() const { return mExpandedGroupNodes; }
188 
196  QSet<QString> checkedGroupNodes() const { return mCheckedGroupNodes; }
197 
202  void setExpandedGroupNodes( const QSet<QString> &expandedGroupNodes ) { mExpandedGroupNodes = expandedGroupNodes; }
203 
208  void setCheckedGroupNodes( const QSet<QString> &checkedGroupNodes ) { mCheckedGroupNodes = checkedGroupNodes; }
209 
210  private:
212  QList<MapThemeLayerRecord> mLayerRecords;
213 
215  bool mHasExpandedStateInfo = false;
217  bool mHasCheckedStateInfo = false;
218 
223  QSet<QString> mExpandedGroupNodes;
224 
229  QSet<QString> mCheckedGroupNodes;
230 
231  friend class QgsMapThemeCollection;
232  };
233 
238 
243  bool hasMapTheme( const QString &name ) const;
244 
249  void insert( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
250 
257  void update( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
258 
263  void removeMapTheme( const QString &name );
264 
270  bool renameMapTheme( const QString &name, const QString &newName );
271 
273  void clear();
274 
279  QStringList mapThemes() const;
280 
285  QgsMapThemeCollection::MapThemeRecord mapThemeState( const QString &name ) const { return mMapThemes[name]; }
286 
294  QStringList mapThemeVisibleLayerIds( const QString &name ) const;
295 
303  QList<QgsMapLayer *> mapThemeVisibleLayers( const QString &name ) const;
304 
309  QMap<QString, QString> mapThemeStyleOverrides( const QString &name );
310 
316  void readXml( const QDomDocument &doc );
317 
323  void writeXml( QDomDocument &doc );
324 
331 
337  void applyTheme( const QString &name, QgsLayerTreeGroup *root, QgsLayerTreeModel *model );
338 
344  QgsProject *project();
345 
350  void setProject( QgsProject *project );
351 
358  QList< QgsMapLayer * > masterLayerOrder() const;
359 
367  QList< QgsMapLayer * > masterVisibleLayers() const;
368 
369  signals:
370 
375  void mapThemesChanged();
376 
381  void mapThemeChanged( const QString &theme );
382 
387  void mapThemeRenamed( const QString &name, const QString &newName );
388 
395  void projectChanged();
396 
397  private slots:
398 
402  void registryLayersRemoved( const QStringList &layerIDs );
403 
405  void layerStyleRenamed( const QString &oldName, const QString &newName );
406 
407  private:
408 
412  void applyMapThemeCheckedLegendNodesToLayer( const MapThemeLayerRecord &layerRec, QgsMapLayer *layer );
413 
417  void reconnectToLayersStyleManager();
418 
419  static bool findRecordForLayer( QgsMapLayer *layer, const MapThemeRecord &rec, MapThemeLayerRecord &layerRec );
420  static MapThemeLayerRecord createThemeLayerRecord( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model );
422  static void applyThemeToLayer( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model, const MapThemeRecord &rec );
423  static void applyThemeToGroup( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, const MapThemeRecord &rec );
424 
425  typedef QMap<QString, MapThemeRecord> MapThemeRecordMap;
426  MapThemeRecordMap mMapThemes;
428  QgsProject *mProject = nullptr;
429 };
430 
431 
432 #endif // QGSMAPTHEMECOLLECTION_H
Layer tree group node serves as a container for layers and further groups.
bool operator!=(const QgsMapThemeCollection::MapThemeLayerRecord &other) const
QgsMapLayer * layer() const
Returns map layer or nullptr if the layer does not exist anymore.
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QSet< QString > checkedGroupNodes() const
Returns a set of group identifiers for group nodes that should have checked state (other group nodes ...
void setProject(QgsProject *project)
The QgsProject on which this map theme collection works.
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
bool renameMapTheme(const QString &name, const QString &newName)
Renames the existing map theme called name to newName.
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
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.
QgsMapThemeCollection::MapThemeRecord mapThemeState(const QString &name) const
Returns the recorded state of a map theme.
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.
void clear()
Removes 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.
void setHasCheckedStateInfo(bool hasInfo)
Sets whether the map theme contains valid checked/unchecked state of group nodes. ...
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1737
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.
QStringList mapThemeVisibleLayerIds(const QString &name) const
Returns the list of layer IDs that are visible for the specified map theme.
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.
QList< QgsMapLayer *> masterVisibleLayers() const
Returns the master list of visible layers.
void mapThemeChanged(const QString &theme)
Emitted when a map theme changes definition.
bool operator==(const QgsMapThemeCollection::MapThemeLayerRecord &other) const
bool hasCheckedStateInfo() const
Returns whether information about checked/unchecked state of groups has been recorded and thus whethe...
QgsMapThemeCollection(QgsProject *project=nullptr)
Create map theme collection that handles themes of the given project.
#define SIP_SKIP
Definition: qgis_sip.h:126
QList< QgsMapLayer *> masterLayerOrder() const
Returns the master layer order (this will always match the project&#39;s QgsProject::layerOrder() )...
void removeMapTheme(const QString &name)
Removes 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.
QStringList mapThemes() const
Returns a list of existing map theme names.
This class is a base class for nodes in a layer tree.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:94
bool operator!=(const QgsMapThemeCollection::MapThemeRecord &other) const
bool isVisible
true if the layer is visible in the associated theme.
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 hasExpandedStateInfo() const
Returns whether information about expanded/collapsed state of nodes has been recorded and thus whethe...
void insert(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Inserts a new map theme to the collection.
void setCheckedGroupNodes(const QSet< QString > &checkedGroupNodes)
Sets a set of group identifiers for group nodes that should have checked state.
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< QgsMapThemeCollection::MapThemeLayerRecord > layerRecords() const
Returns a list of records for all visible layer belonging to the theme.
QSet< QString > expandedGroupNodes() const
Returns a set of group identifiers for group nodes that should have expanded state (other group nodes...
bool expandedLayerNode
Whether the layer&#39;s tree node is expanded (only to be applied if the parent MapThemeRecord has the in...
void mapThemeRenamed(const QString &name, const QString &newName)
Emitted when a map theme within the collection is renamed.
Container class that allows storage of map themes consisting of visible map layers and layer styles...
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 operator==(const QgsMapThemeCollection::MapThemeRecord &other) const
bool usingLegendItems
Whether checkedLegendItems should be applied.
Layer tree node points to a map layer.