QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
64  // TODO c++20 - replace with = default
66  {
67  return mLayer == other.mLayer && isVisible == other.isVisible &&
68  usingCurrentStyle == other.usingCurrentStyle && currentStyle == other.currentStyle &&
69  usingLegendItems == other.usingLegendItems && checkedLegendItems == other.checkedLegendItems &&
70  expandedLegendItems == other.expandedLegendItems && expandedLayerNode == other.expandedLayerNode;
71  }
73  {
74  return !( *this == other );
75  }
76 
78  QgsMapLayer *layer() const { return mLayer; }
79 
81  void setLayer( QgsMapLayer *layer );
82 
87  bool isVisible = true;
88 
90  bool usingCurrentStyle = false;
92  QString currentStyle;
94  bool usingLegendItems = false;
96  QSet<QString> checkedLegendItems;
97 
102  QSet<QString> expandedLegendItems;
103 
109  bool expandedLayerNode = false;
110  private:
112  QgsWeakMapLayerPointer mLayer;
113  };
114 
121  class CORE_EXPORT MapThemeRecord
122  {
123  public:
124 
126  {
127  return validLayerRecords() == other.validLayerRecords() &&
128  mHasExpandedStateInfo == other.mHasExpandedStateInfo &&
129  mExpandedGroupNodes == other.mExpandedGroupNodes && mCheckedGroupNodes == other.mCheckedGroupNodes;
130  }
132  {
133  return !( *this == other );
134  }
135 
137  QList<QgsMapThemeCollection::MapThemeLayerRecord> layerRecords() const { return mLayerRecords; }
138 
140  void setLayerRecords( const QList<QgsMapThemeCollection::MapThemeLayerRecord> &records ) { mLayerRecords = records; }
141 
143  void removeLayerRecord( QgsMapLayer *layer );
144 
146  void addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord &record );
147 
151  QHash<QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord> validLayerRecords() const SIP_SKIP;
152 
158  bool hasExpandedStateInfo() const { return mHasExpandedStateInfo; }
159 
166  bool hasCheckedStateInfo() const { return mHasCheckedStateInfo; } SIP_SKIP;
167 
172  void setHasExpandedStateInfo( bool hasInfo ) { mHasExpandedStateInfo = hasInfo; }
173 
179  void setHasCheckedStateInfo( bool hasInfo ) { mHasCheckedStateInfo = hasInfo; } SIP_SKIP;
180 
188  QSet<QString> expandedGroupNodes() const { return mExpandedGroupNodes; }
189 
197  QSet<QString> checkedGroupNodes() const { return mCheckedGroupNodes; }
198 
203  void setExpandedGroupNodes( const QSet<QString> &expandedGroupNodes ) { mExpandedGroupNodes = expandedGroupNodes; }
204 
209  void setCheckedGroupNodes( const QSet<QString> &checkedGroupNodes ) { mCheckedGroupNodes = checkedGroupNodes; }
210 
211  private:
213  QList<MapThemeLayerRecord> mLayerRecords;
214 
216  bool mHasExpandedStateInfo = false;
218  bool mHasCheckedStateInfo = false;
219 
224  QSet<QString> mExpandedGroupNodes;
225 
230  QSet<QString> mCheckedGroupNodes;
231 
232  friend class QgsMapThemeCollection;
233  };
234 
239 
244  bool hasMapTheme( const QString &name ) const;
245 
250  void insert( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
251 
258  void update( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
259 
264  void removeMapTheme( const QString &name );
265 
271  bool renameMapTheme( const QString &name, const QString &newName );
272 
274  void clear();
275 
280  QStringList mapThemes() const;
281 
286  QgsMapThemeCollection::MapThemeRecord mapThemeState( const QString &name ) const { return mMapThemes[name]; }
287 
295  QStringList mapThemeVisibleLayerIds( const QString &name ) const;
296 
304  QList<QgsMapLayer *> mapThemeVisibleLayers( const QString &name ) const;
305 
310  QMap<QString, QString> mapThemeStyleOverrides( const QString &name );
311 
317  void readXml( const QDomDocument &doc );
318 
324  void writeXml( QDomDocument &doc );
325 
332 
338  void applyTheme( const QString &name, QgsLayerTreeGroup *root, QgsLayerTreeModel *model );
339 
345  QgsProject *project();
346 
351  void setProject( QgsProject *project );
352 
359  QList< QgsMapLayer * > masterLayerOrder() const;
360 
368  QList< QgsMapLayer * > masterVisibleLayers() const;
369 
370  signals:
371 
376  void mapThemesChanged();
377 
382  void mapThemeChanged( const QString &theme );
383 
388  void mapThemeRenamed( const QString &name, const QString &newName );
389 
396  void projectChanged();
397 
398  private slots:
399 
403  void registryLayersRemoved( const QStringList &layerIDs );
404 
406  void layerStyleRenamed( const QString &oldName, const QString &newName );
407 
408  private:
409 
413  void applyMapThemeCheckedLegendNodesToLayer( const MapThemeLayerRecord &layerRec, QgsMapLayer *layer );
414 
418  void reconnectToLayersStyleManager();
419 
420  static bool findRecordForLayer( QgsMapLayer *layer, const MapThemeRecord &rec, MapThemeLayerRecord &layerRec );
421  static MapThemeLayerRecord createThemeLayerRecord( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model );
422  static void createThemeFromCurrentState( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, MapThemeRecord &rec );
423  static void applyThemeToLayer( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model, const MapThemeRecord &rec );
424  static void applyThemeToGroup( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, const MapThemeRecord &rec );
425 
426  typedef QMap<QString, MapThemeRecord> MapThemeRecordMap;
427  MapThemeRecordMap mMapThemes;
429  QgsProject *mProject = nullptr;
430 };
431 
432 
433 #endif // QGSMAPTHEMECOLLECTION_H
QgsMapThemeCollection::MapThemeRecord::operator==
bool operator==(const QgsMapThemeCollection::MapThemeRecord &other) const
Definition: qgsmapthemecollection.h:125
QgsMapThemeCollection::mapThemes
QStringList mapThemes
Definition: qgsmapthemecollection.h:48
QgsLayerTreeNode
This class is a base class for nodes in a layer tree.
Definition: qgslayertreenode.h:75
QgsMapThemeCollection::mapThemeRenamed
void mapThemeRenamed(const QString &name, const QString &newName)
Emitted when a map theme within the collection is renamed.
QgsMapThemeCollection::mapThemeVisibleLayerIds
QStringList mapThemeVisibleLayerIds(const QString &name) const
Returns the list of layer IDs that are visible for the specified map theme.
Definition: qgsmapthemecollection.cpp:320
QgsMapThemeCollection::readXml
void readXml(const QDomDocument &doc)
Reads the map theme collection state from XML.
Definition: qgsmapthemecollection.cpp:436
QgsMapThemeCollection::masterVisibleLayers
QList< QgsMapLayer * > masterVisibleLayers() const
Returns the master list of visible layers.
Definition: qgsmapthemecollection.cpp:234
QgsMapThemeCollection::MapThemeRecord
Individual map theme record of visible layers and styles.
Definition: qgsmapthemecollection.h:121
QgsMapThemeCollection::mapThemeChanged
void mapThemeChanged(const QString &theme)
Emitted when a map theme changes definition.
QgsMapThemeCollection::QgsMapThemeCollection
QgsMapThemeCollection(QgsProject *project=nullptr)
Create map theme collection that handles themes of the given project.
Definition: qgsmapthemecollection.cpp:28
QgsMapThemeCollection::MapThemeRecord::operator!=
bool operator!=(const QgsMapThemeCollection::MapThemeRecord &other) const
Definition: qgsmapthemecollection.h:131
QgsMapThemeCollection::MapThemeRecord::validLayerRecords
QHash< QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord > validLayerRecords() const
Returns set with only records for valid layers.
Definition: qgsmapthemecollection.cpp:733
QgsMapThemeCollection::MapThemeRecord::layerRecords
QList< QgsMapThemeCollection::MapThemeLayerRecord > layerRecords() const
Returns a list of records for all visible layer belonging to the theme.
Definition: qgsmapthemecollection.h:137
QgsMapThemeCollection::MapThemeLayerRecord::usingLegendItems
bool usingLegendItems
Whether checkedLegendItems should be applied.
Definition: qgsmapthemecollection.h:94
QgsMapThemeCollection::MapThemeLayerRecord::operator==
bool operator==(const QgsMapThemeCollection::MapThemeLayerRecord &other) const
Definition: qgsmapthemecollection.h:65
QgsMapThemeCollection::removeMapTheme
void removeMapTheme(const QString &name)
Removes an existing map theme from collection.
Definition: qgsmapthemecollection.cpp:296
QgsLayerTreeModel
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Definition: qgslayertreemodel.h:55
QgsMapThemeCollection::MapThemeRecord::setLayerRecords
void setLayerRecords(const QList< QgsMapThemeCollection::MapThemeLayerRecord > &records)
Sets layer records for the theme.
Definition: qgsmapthemecollection.h:140
QgsMapThemeCollection::MapThemeLayerRecord::expandedLegendItems
QSet< QString > expandedLegendItems
Rule keys of expanded legend items in layer tree view.
Definition: qgsmapthemecollection.h:102
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
QgsMapThemeCollection::mapThemeVisibleLayers
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
Definition: qgsmapthemecollection.cpp:331
QgsMapThemeCollection::MapThemeRecord::setCheckedGroupNodes
void setCheckedGroupNodes(const QSet< QString > &checkedGroupNodes)
Sets a set of group identifiers for group nodes that should have checked state.
Definition: qgsmapthemecollection.h:209
QgsMapThemeCollection::mapThemeStyleOverrides
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
Definition: qgsmapthemecollection.cpp:385
QgsMapThemeCollection::applyTheme
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...
Definition: qgsmapthemecollection.cpp:202
QgsMapThemeCollection::project
QgsProject project
Definition: qgsmapthemecollection.h:49
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsMapThemeCollection::MapThemeRecord::checkedGroupNodes
QSet< QString > checkedGroupNodes() const
Returns a set of group identifiers for group nodes that should have checked state (other group nodes ...
Definition: qgsmapthemecollection.h:197
QgsMapThemeCollection::insert
void insert(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Inserts a new map theme to the collection.
Definition: qgsmapthemecollection.cpp:262
QgsMapThemeCollection::clear
void clear()
Removes all map themes from the collection.
Definition: qgsmapthemecollection.cpp:307
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:43
qgsmaplayer.h
QgsMapThemeCollection::mapThemesChanged
void mapThemesChanged()
Emitted when map themes within the collection are changed.
qgis_sip.h
QgsMapThemeCollection::MapThemeRecord::setHasExpandedStateInfo
void setHasExpandedStateInfo(bool hasInfo)
Sets whether the map theme contains valid expanded/collapsed state of nodes.
Definition: qgsmapthemecollection.h:172
QgsLayerTreeGroup
Layer tree group node serves as a container for layers and further groups.
Definition: qgslayertreegroup.h:40
QgsMapThemeCollection::MapThemeRecord::expandedGroupNodes
QSet< QString > expandedGroupNodes() const
Returns a set of group identifiers for group nodes that should have expanded state (other group nodes...
Definition: qgsmapthemecollection.h:188
QgsMapThemeCollection::MapThemeLayerRecord::currentStyle
QString currentStyle
Name of the current style of the layer.
Definition: qgsmapthemecollection.h:92
QgsMapThemeCollection::projectChanged
void projectChanged()
Emitted when the project changes.
QgsWeakMapLayerPointer
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:2146
QgsMapThemeCollection::MapThemeLayerRecord::operator!=
bool operator!=(const QgsMapThemeCollection::MapThemeLayerRecord &other) const
Definition: qgsmapthemecollection.h:72
QgsMapThemeCollection::MapThemeLayerRecord::usingCurrentStyle
bool usingCurrentStyle
Whether current style is valid and should be applied.
Definition: qgsmapthemecollection.h:90
QgsMapThemeCollection::masterLayerOrder
QList< QgsMapLayer * > masterLayerOrder() const
Returns the master layer order (this will always match the project's QgsProject::layerOrder() ).
Definition: qgsmapthemecollection.cpp:226
QgsMapThemeCollection::createThemeFromCurrentState
static QgsMapThemeCollection::MapThemeRecord createThemeFromCurrentState(QgsLayerTreeGroup *root, QgsLayerTreeModel *model)
Static method to create theme from the current state of layer visibilities in layer tree,...
Definition: qgsmapthemecollection.cpp:102
QgsMapThemeCollection::MapThemeLayerRecord
Individual record of a visible layer in a map theme record.
Definition: qgsmapthemecollection.h:58
QgsMapThemeCollection::writeXml
void writeXml(QDomDocument &doc)
Writes the map theme collection state to XML.
Definition: qgsmapthemecollection.cpp:569
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsMapThemeCollection::MapThemeRecord::setExpandedGroupNodes
void setExpandedGroupNodes(const QSet< QString > &expandedGroupNodes)
Sets a set of group identifiers for group nodes that should have expanded state.
Definition: qgsmapthemecollection.h:203
QgsMapThemeCollection::MapThemeRecord::setHasCheckedStateInfo
void setHasCheckedStateInfo(bool hasInfo)
Sets whether the map theme contains valid checked/unchecked state of group nodes.
Definition: qgsmapthemecollection.h:179
QgsMapThemeCollection::MapThemeLayerRecord::MapThemeLayerRecord
MapThemeLayerRecord(QgsMapLayer *l=nullptr)
Initialize layer record with a map layer - it will be stored as a weak pointer.
Definition: qgsmapthemecollection.h:62
QgsMapThemeCollection::setProject
void setProject(QgsProject *project)
Definition: qgsmapthemecollection.cpp:215
QgsMapThemeCollection::hasMapTheme
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
Definition: qgsmapthemecollection.cpp:257
QgsMapThemeCollection
Container class that allows storage of map themes consisting of visible map layers and layer styles.
Definition: qgsmapthemecollection.h:44
QgsMapThemeCollection::renameMapTheme
bool renameMapTheme(const QString &name, const QString &newName)
Renames the existing map theme called name to newName.
Definition: qgsmapthemecollection.cpp:283
QgsMapThemeCollection::MapThemeLayerRecord::layer
QgsMapLayer * layer() const
Returns map layer or nullptr if the layer does not exist anymore.
Definition: qgsmapthemecollection.h:78
QgsMapThemeCollection::MapThemeRecord::hasCheckedStateInfo
bool hasCheckedStateInfo() const
Returns whether information about checked/unchecked state of groups has been recorded and thus whethe...
Definition: qgsmapthemecollection.h:166
QgsMapThemeCollection::MapThemeLayerRecord::isVisible
bool isVisible
true if the layer is visible in the associated theme.
Definition: qgsmapthemecollection.h:87
QgsMapThemeCollection::mapThemeState
QgsMapThemeCollection::MapThemeRecord mapThemeState(const QString &name) const
Returns the recorded state of a map theme.
Definition: qgsmapthemecollection.h:286
QgsMapThemeCollection::MapThemeLayerRecord::checkedLegendItems
QSet< QString > checkedLegendItems
Rule keys of check legend items in layer tree model.
Definition: qgsmapthemecollection.h:96
QgsMapThemeCollection::update
void update(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Updates a map theme within the collection.
Definition: qgsmapthemecollection.cpp:271
QgsMapThemeCollection::MapThemeLayerRecord::expandedLayerNode
bool expandedLayerNode
Whether the layer's tree node is expanded (only to be applied if the parent MapThemeRecord has the in...
Definition: qgsmapthemecollection.h:109