QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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
29class QDomDocument;
34class QgsProject;
35
43class CORE_EXPORT QgsMapThemeCollection : public QObject
44{
45 Q_OBJECT
46
47 Q_PROPERTY( QStringList mapThemes READ mapThemes NOTIFY mapThemesChanged )
48 Q_PROPERTY( QgsProject *project READ project WRITE setProject NOTIFY projectChanged )
49
50 public:
51
56 class CORE_EXPORT MapThemeLayerRecord
57 {
58 public:
60 MapThemeLayerRecord( QgsMapLayer *l = nullptr ): mLayer( l ) {}
61
62 // TODO c++20 - replace with = default
64 {
65 return mLayer == other.mLayer && isVisible == other.isVisible &&
66 usingCurrentStyle == other.usingCurrentStyle && currentStyle == other.currentStyle &&
67 usingLegendItems == other.usingLegendItems && checkedLegendItems == other.checkedLegendItems &&
68 expandedLegendItems == other.expandedLegendItems && expandedLayerNode == other.expandedLayerNode;
69 }
71 {
72 return !( *this == other );
73 }
74
76 QgsMapLayer *layer() const { return mLayer; }
77
79 void setLayer( QgsMapLayer *layer );
80
85 bool isVisible = true;
86
88 bool usingCurrentStyle = false;
90 QString currentStyle;
92 bool usingLegendItems = false;
94 QSet<QString> checkedLegendItems;
95
100 QSet<QString> expandedLegendItems;
101
107 bool expandedLayerNode = false;
108
109 private:
112 };
113
119 class CORE_EXPORT MapThemeRecord
120 {
121 public:
122
124 {
125 return validLayerRecords() == other.validLayerRecords() &&
126 mHasExpandedStateInfo == other.mHasExpandedStateInfo &&
127 mExpandedGroupNodes == other.mExpandedGroupNodes && mCheckedGroupNodes == other.mCheckedGroupNodes;
128 }
130 {
131 return !( *this == other );
132 }
133
135 QList<QgsMapThemeCollection::MapThemeLayerRecord> layerRecords() const { return mLayerRecords; }
136
138 void setLayerRecords( const QList<QgsMapThemeCollection::MapThemeLayerRecord> &records ) { mLayerRecords = records; }
139
141 void removeLayerRecord( QgsMapLayer *layer );
142
144 void addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord &record );
145
149 QHash<QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord> validLayerRecords() const SIP_SKIP;
150
156 bool hasExpandedStateInfo() const { return mHasExpandedStateInfo; }
157
163 bool hasCheckedStateInfo() const { return mHasCheckedStateInfo; };
164
169 void setHasExpandedStateInfo( bool hasInfo ) { mHasExpandedStateInfo = hasInfo; }
170
175 void setHasCheckedStateInfo( bool hasInfo ) { mHasCheckedStateInfo = hasInfo; };
176
184 QSet<QString> expandedGroupNodes() const { return mExpandedGroupNodes; }
185
193 QSet<QString> checkedGroupNodes() const { return mCheckedGroupNodes; }
194
199 void setExpandedGroupNodes( const QSet<QString> &expandedGroupNodes ) { mExpandedGroupNodes = expandedGroupNodes; }
200
205 void setCheckedGroupNodes( const QSet<QString> &checkedGroupNodes ) { mCheckedGroupNodes = checkedGroupNodes; }
206
214 static MapThemeRecord readXml( const QDomElement &element, const QgsProject *project );
215
223 void writeXml( QDomElement element, QDomDocument &document ) const;
224
225 private:
227 QList<MapThemeLayerRecord> mLayerRecords;
228
230 bool mHasExpandedStateInfo = false;
232 bool mHasCheckedStateInfo = false;
233
238 QSet<QString> mExpandedGroupNodes;
239
244 QSet<QString> mCheckedGroupNodes;
245
247 };
248
252 QgsMapThemeCollection( QgsProject *project = nullptr );
253
257 bool hasMapTheme( const QString &name ) const;
258
263 void insert( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
264
271 void update( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
272
276 void removeMapTheme( const QString &name );
277
283 bool renameMapTheme( const QString &name, const QString &newName );
284
286 void clear();
287
291 QStringList mapThemes() const;
292
296 QgsMapThemeCollection::MapThemeRecord mapThemeState( const QString &name ) const { return mMapThemes[name]; }
297
304 QStringList mapThemeVisibleLayerIds( const QString &name ) const;
305
312 QList<QgsMapLayer *> mapThemeVisibleLayers( const QString &name ) const;
313
317 QMap<QString, QString> mapThemeStyleOverrides( const QString &name );
318
324 void readXml( const QDomDocument &doc );
325
331 void writeXml( QDomDocument &doc ) const;
332
337 static QgsMapThemeCollection::MapThemeRecord createThemeFromCurrentState( QgsLayerTreeGroup *root, QgsLayerTreeModel *model );
338
343 void applyTheme( const QString &name, QgsLayerTreeGroup *root, QgsLayerTreeModel *model );
344
352
359 void setProject( QgsProject *project );
360
366 QList< QgsMapLayer * > masterLayerOrder() const;
367
374 QList< QgsMapLayer * > masterVisibleLayers() const;
375
376 signals:
377
382
386 void mapThemeChanged( const QString &theme );
387
392 void mapThemeRenamed( const QString &name, const QString &newName );
393
401
402 private slots:
403
407 void registryLayersRemoved( const QStringList &layerIDs );
408
410 void layerStyleRenamed( const QString &oldName, const QString &newName );
411
412 private:
413
417 void applyMapThemeCheckedLegendNodesToLayer( const MapThemeLayerRecord &layerRec, QgsMapLayer *layer );
418
422 void reconnectToLayersStyleManager();
423
424 static bool findRecordForLayer( QgsMapLayer *layer, const MapThemeRecord &rec, MapThemeLayerRecord &layerRec );
425 static MapThemeLayerRecord createThemeLayerRecord( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model );
426 static void createThemeFromCurrentState( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, MapThemeRecord &rec );
427 static void applyThemeToLayer( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model, const MapThemeRecord &rec );
428 static void applyThemeToGroup( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, const MapThemeRecord &rec );
429
430 typedef QMap<QString, MapThemeRecord> MapThemeRecordMap;
431 MapThemeRecordMap mMapThemes;
433 QgsProject *mProject = nullptr;
434};
435
436
437#endif // QGSMAPTHEMECOLLECTION_H
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
This class is a base class for nodes in a layer tree.
Base class for all map layer types.
Definition qgsmaplayer.h:76
Individual record of a visible layer in a map theme record.
QString currentStyle
Name of the current style of the layer.
QSet< QString > expandedLegendItems
Rule keys of expanded legend items in layer tree view.
QSet< QString > checkedLegendItems
Rule keys of check legend items in layer tree model.
bool operator!=(const QgsMapThemeCollection::MapThemeLayerRecord &other) const
bool expandedLayerNode
Whether the layer's tree node is expanded (only to be applied if the parent MapThemeRecord has the in...
MapThemeLayerRecord(QgsMapLayer *l=nullptr)
Initialize layer record with a map layer - it will be stored as a weak pointer.
bool isVisible
true if the layer is visible in the associated theme.
bool usingLegendItems
Whether checkedLegendItems should be applied.
bool usingCurrentStyle
Whether current style is valid and should be applied.
QgsMapLayer * layer() const
Returns map layer or nullptr if the layer does not exist anymore.
bool operator==(const QgsMapThemeCollection::MapThemeLayerRecord &other) const
Individual map theme record of visible layers and styles.
bool operator!=(const QgsMapThemeCollection::MapThemeRecord &other) const
void setExpandedGroupNodes(const QSet< QString > &expandedGroupNodes)
Sets a set of group identifiers for group nodes that should have expanded state.
QSet< QString > expandedGroupNodes() const
Returns a set of group identifiers for group nodes that should have expanded state (other group nodes...
void setCheckedGroupNodes(const QSet< QString > &checkedGroupNodes)
Sets a set of group identifiers for group nodes that should have checked state.
void setHasExpandedStateInfo(bool hasInfo)
Sets whether the map theme contains valid expanded/collapsed state of nodes.
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
QSet< QString > checkedGroupNodes() const
Returns a set of group identifiers for group nodes that should have checked state (other group nodes ...
void setLayerRecords(const QList< QgsMapThemeCollection::MapThemeLayerRecord > &records)
Sets layer records for the theme.
void setHasCheckedStateInfo(bool hasInfo)
Sets whether the map theme contains valid checked/unchecked state of group nodes.
QHash< QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord > validLayerRecords() const
Returns set with only records for valid layers.
bool hasCheckedStateInfo() const
Returns whether information about checked/unchecked state of groups has been recorded and thus whethe...
Container class that allows storage of map themes consisting of visible map layers and layer styles.
void mapThemesChanged()
Emitted when map themes within the collection are changed.
void mapThemeRenamed(const QString &name, const QString &newName)
Emitted when a map theme within the collection is renamed.
QgsMapThemeCollection::MapThemeRecord mapThemeState(const QString &name) const
Returns the recorded state of a map theme.
void mapThemeChanged(const QString &theme)
Emitted when a map theme changes definition.
QgsProject * project()
Returns the QgsProject on which this map theme collection works.
void projectChanged()
Emitted when the project changes.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
#define SIP_SKIP
Definition qgis_sip.h:126
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.