QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 "qgsmaplayer.h"
22
23#include <QMap>
24#include <QObject>
25#include <QPointer>
26#include <QSet>
27#include <QStringList>
28
29class QDomDocument;
34class QgsProject;
35
42
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:
55 class CORE_EXPORT MapThemeLayerRecord
56 {
57 public:
60 : mLayer( l )
61 {}
62
63 // TODO c++20 - replace with = default
65 {
66 return mLayer == other.mLayer
67 && isVisible == other.isVisible
69 && currentStyle == other.currentStyle
74 }
75 bool operator!=( const QgsMapThemeCollection::MapThemeLayerRecord &other ) const { return !( *this == other ); }
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
111 private:
114 };
115
121 class CORE_EXPORT MapThemeRecord
122 {
123 public:
125 {
126 return validLayerRecords() == other.validLayerRecords()
127 && mHasExpandedStateInfo == other.mHasExpandedStateInfo
128 && mExpandedGroupNodes == other.mExpandedGroupNodes
129 && mCheckedGroupNodes == other.mCheckedGroupNodes;
130 }
131 bool operator!=( const QgsMapThemeCollection::MapThemeRecord &other ) const { return !( *this == other ); }
132
134 QList<QgsMapThemeCollection::MapThemeLayerRecord> layerRecords() const { return mLayerRecords; }
135
137 void setLayerRecords( const QList<QgsMapThemeCollection::MapThemeLayerRecord> &records ) { mLayerRecords = records; }
138
140 void removeLayerRecord( QgsMapLayer *layer );
141
143 void addLayerRecord( const QgsMapThemeCollection::MapThemeLayerRecord &record );
144
148 QHash<QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord> validLayerRecords() const SIP_SKIP;
149
155 bool hasExpandedStateInfo() const { return mHasExpandedStateInfo; }
156
162 bool hasCheckedStateInfo() const { return mHasCheckedStateInfo; };
163
168 void setHasExpandedStateInfo( bool hasInfo ) { mHasExpandedStateInfo = hasInfo; }
169
174 void setHasCheckedStateInfo( bool hasInfo ) { mHasCheckedStateInfo = hasInfo; };
175
183 QSet<QString> expandedGroupNodes() const { return mExpandedGroupNodes; }
184
192 QSet<QString> checkedGroupNodes() const { return mCheckedGroupNodes; }
193
198 void setExpandedGroupNodes( const QSet<QString> &expandedGroupNodes ) { mExpandedGroupNodes = expandedGroupNodes; }
199
204 void setCheckedGroupNodes( const QSet<QString> &checkedGroupNodes ) { mCheckedGroupNodes = checkedGroupNodes; }
205
213 static MapThemeRecord readXml( const QDomElement &element, const QgsProject *project );
214
222 void writeXml( QDomElement element, QDomDocument &document ) const;
223
224 private:
226 QList<MapThemeLayerRecord> mLayerRecords;
227
229 bool mHasExpandedStateInfo = false;
231 bool mHasCheckedStateInfo = false;
232
237 QSet<QString> mExpandedGroupNodes;
238
243 QSet<QString> mCheckedGroupNodes;
244
246 };
247
252
256 bool hasMapTheme( const QString &name ) const;
257
262 void insert( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
263
270 void update( const QString &name, const QgsMapThemeCollection::MapThemeRecord &state );
271
275 void removeMapTheme( const QString &name );
276
282 bool renameMapTheme( const QString &name, const QString &newName );
283
285 void clear();
286
290 QStringList mapThemes() const;
291
295 QgsMapThemeCollection::MapThemeRecord mapThemeState( const QString &name ) const { return mMapThemes[name]; }
296
303 QStringList mapThemeVisibleLayerIds( const QString &name ) const;
304
311 QList<QgsMapLayer *> mapThemeVisibleLayers( const QString &name ) const;
312
316 QMap<QString, QString> mapThemeStyleOverrides( const QString &name );
317
323 void readXml( const QDomDocument &doc );
324
330 void writeXml( QDomDocument &doc ) const;
331
336 static QgsMapThemeCollection::MapThemeRecord createThemeFromCurrentState( QgsLayerTreeGroup *root, QgsLayerTreeModel *model );
337
342 void applyTheme( const QString &name, QgsLayerTreeGroup *root, QgsLayerTreeModel *model );
343
351
359
365 QList< QgsMapLayer * > masterLayerOrder() const;
366
373 QList< QgsMapLayer * > masterVisibleLayers() const;
374
375 signals:
376
381
385 void mapThemeChanged( const QString &theme );
386
391 void mapThemeRenamed( const QString &name, const QString &newName );
392
400
401 private slots:
402
406 void registryLayersRemoved( const QStringList &layerIDs );
407
409 void layerStyleRenamed( const QString &oldName, const QString &newName );
410
411 private:
415 void applyMapThemeCheckedLegendNodesToLayer( const MapThemeLayerRecord &layerRec, QgsMapLayer *layer );
416
420 void reconnectToLayersStyleManager();
421
422 static bool findRecordForLayer( QgsMapLayer *layer, const MapThemeRecord &rec, MapThemeLayerRecord &layerRec );
423 static MapThemeLayerRecord createThemeLayerRecord( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model );
425 static void applyThemeToLayer( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model, const MapThemeRecord &rec );
426 static void applyThemeToGroup( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, const MapThemeRecord &rec );
427
428 typedef QMap<QString, MapThemeRecord> MapThemeRecordMap;
429 MapThemeRecordMap mMapThemes;
431 QgsProject *mProject = nullptr;
432};
433
434
435#endif // QGSMAPTHEMECOLLECTION_H
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
A model representing the layer tree, including layers and groups of layers.
Base class for nodes in a layer tree.
Base class for all map layer types.
Definition qgsmaplayer.h:83
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.
bool hasExpandedStateInfo() const
Returns whether information about expanded/collapsed state of nodes has been recorded and thus whethe...
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...
void mapThemesChanged()
Emitted when map themes within the collection are changed.
static QgsMapThemeCollection::MapThemeRecord createThemeFromCurrentState(QgsLayerTreeGroup *root, QgsLayerTreeModel *model)
Static method to create theme from the current state of layer visibilities in layer tree,...
void insert(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Inserts a new map theme to the collection.
bool renameMapTheme(const QString &name, const QString &newName)
Renames the existing map theme called name to newName.
void mapThemeRenamed(const QString &name, const QString &newName)
Emitted when a map theme within the collection is renamed.
void clear()
Removes all map themes from the collection.
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
void removeMapTheme(const QString &name)
Removes an existing map theme from collection.
QgsMapThemeCollection::MapThemeRecord mapThemeState(const QString &name) const
Returns the recorded state of a map theme.
void setProject(QgsProject *project)
Sets the project on which this map theme collection works.
QList< QgsMapLayer * > masterLayerOrder() const
Returns the master layer order (this will always match the project's QgsProject::layerOrder() ).
void writeXml(QDomDocument &doc) const
Writes the map theme collection state to XML.
QList< QgsMapLayer * > masterVisibleLayers() const
Returns the master list of visible layers.
void update(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Updates a map theme within the collection.
void mapThemeChanged(const QString &theme)
Emitted when a map theme changes definition.
QgsProject * project()
Returns the QgsProject on which this map theme collection works.
QgsMapThemeCollection(QgsProject *project=nullptr)
Create map theme collection that handles themes of the given project.
void projectChanged()
Emitted when the project changes.
void readXml(const QDomDocument &doc)
Reads the map theme collection state from XML.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:113
#define SIP_SKIP
Definition qgis_sip.h:133
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.