QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsmapthemecollection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmapthemecollection.cpp
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
17
18#include "qgslayertree.h"
19#include "qgslayertreemodel.h"
22#include "qgsproject.h"
23#include "qgsrenderer.h"
24#include "qgsvectorlayer.h"
25
26#include <QString>
27
28#include "moc_qgsmapthemecollection.cpp"
29
30using namespace Qt::StringLiterals;
31
33 : mProject( project )
34{
35 connect( project, static_cast<void ( QgsProject::* )( const QStringList & )>( &QgsProject::layersWillBeRemoved ), this, &QgsMapThemeCollection::registryLayersRemoved );
36}
37
38QgsMapThemeCollection::MapThemeLayerRecord QgsMapThemeCollection::createThemeLayerRecord( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model )
39{
40 MapThemeLayerRecord layerRec( nodeLayer->layer() );
41 layerRec.isVisible = nodeLayer->isVisible();
42 layerRec.usingCurrentStyle = true;
43 layerRec.currentStyle = nodeLayer->layer()->styleManager()->currentStyle();
44 layerRec.expandedLayerNode = nodeLayer->isExpanded();
45 const QStringList expandedLegendNodes = nodeLayer->customProperty( u"expandedLegendNodes"_s ).toStringList();
46 layerRec.expandedLegendItems = QSet<QString>( expandedLegendNodes.begin(), expandedLegendNodes.end() );
47
48 // get checked legend items
49 bool hasCheckableItems = false;
50 bool someItemsUnchecked = false;
51 QSet<QString> checkedItems;
52 const QList<QgsLayerTreeModelLegendNode *> layerLegendNodes = model->layerLegendNodes( nodeLayer, true );
53 for ( QgsLayerTreeModelLegendNode *legendNode : layerLegendNodes )
54 {
55 if ( legendNode->flags() & Qt::ItemIsUserCheckable )
56 {
57 hasCheckableItems = true;
58
59 if ( legendNode->data( Qt::CheckStateRole ).toInt() == Qt::Checked )
60 checkedItems << legendNode->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString();
61 else
62 someItemsUnchecked = true;
63 }
64 }
65
66 if ( hasCheckableItems && someItemsUnchecked )
67 {
68 layerRec.usingLegendItems = true;
69 layerRec.checkedLegendItems = checkedItems;
70 }
71 return layerRec;
72}
73
74static QString _groupId( QgsLayerTreeNode *node )
75{
76 QStringList lst;
77 while ( node->parent() )
78 {
79 lst.prepend( node->name() );
80 node = node->parent();
81 }
82 return lst.join( '/' );
83}
84
86{
87 const QList<QgsLayerTreeNode *> constChildren = parent->children();
88 for ( QgsLayerTreeNode *node : constChildren )
89 {
90 if ( QgsLayerTree::isGroup( node ) )
91 {
93 if ( node->isExpanded() )
94 rec.mExpandedGroupNodes.insert( _groupId( node ) );
95 if ( node->itemVisibilityChecked() != Qt::Unchecked )
96 rec.mCheckedGroupNodes.insert( _groupId( node ) );
97 }
98 else if ( QgsLayerTree::isLayer( node ) )
99 {
100 QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
101 if ( node->itemVisibilityChecked() != Qt::Unchecked && nodeLayer->layer() )
102 rec.mLayerRecords << createThemeLayerRecord( nodeLayer, model );
103 }
104 }
105}
106
108{
110 rec.setHasExpandedStateInfo( true ); // all newly created theme records have expanded state info
111 rec.setHasCheckedStateInfo( true ); // all newly created theme records have checked state info
112 createThemeFromCurrentState( root, model, rec );
113 return rec;
114}
115
116bool QgsMapThemeCollection::findRecordForLayer( QgsMapLayer *layer, const QgsMapThemeCollection::MapThemeRecord &rec, QgsMapThemeCollection::MapThemeLayerRecord &layerRec )
117{
118 for ( const QgsMapThemeCollection::MapThemeLayerRecord &lr : std::as_const( rec.mLayerRecords ) )
119 {
120 if ( lr.layer() == layer )
121 {
122 layerRec = lr;
123 return true;
124 }
125 }
126 return false;
127}
128
129void QgsMapThemeCollection::applyThemeToLayer( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model, const QgsMapThemeCollection::MapThemeRecord &rec )
130{
131 MapThemeLayerRecord layerRec;
132 const bool recordExists = findRecordForLayer( nodeLayer->layer(), rec, layerRec );
133
134 // Make sure the whole tree is visible
135 if ( recordExists )
136 {
137 if ( rec.hasCheckedStateInfo() )
138 nodeLayer->setItemVisibilityChecked( true );
139 else
141 }
142 else
143 nodeLayer->setItemVisibilityChecked( false );
144
145 if ( !recordExists )
146 return;
147
148 if ( layerRec.usingCurrentStyle )
149 {
150 // apply desired style first
151 nodeLayer->layer()->styleManager()->setCurrentStyle( layerRec.currentStyle );
152 }
153
154 if ( layerRec.usingLegendItems )
155 {
156 // some nodes are not checked
157 const QList<QgsLayerTreeModelLegendNode *> constLayerLegendNodes = model->layerLegendNodes( nodeLayer, true );
158 for ( QgsLayerTreeModelLegendNode *legendNode : constLayerLegendNodes )
159 {
160 QString ruleKey = legendNode->data( static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) ).toString();
161 Qt::CheckState shouldHaveState = layerRec.checkedLegendItems.contains( ruleKey ) ? Qt::Checked : Qt::Unchecked;
162 if ( ( legendNode->flags() & Qt::ItemIsUserCheckable ) &&
163 legendNode->data( Qt::CheckStateRole ).toInt() != shouldHaveState )
164 legendNode->setData( shouldHaveState, Qt::CheckStateRole );
165 }
166 }
167 else
168 {
169 // all nodes should be checked
170 const QList<QgsLayerTreeModelLegendNode *> constLayerLegendNodes = model->layerLegendNodes( nodeLayer, true );
171 for ( QgsLayerTreeModelLegendNode *legendNode : constLayerLegendNodes )
172 {
173 if ( ( legendNode->flags() & Qt::ItemIsUserCheckable ) &&
174 legendNode->data( Qt::CheckStateRole ).toInt() != Qt::Checked )
175 legendNode->setData( Qt::Checked, Qt::CheckStateRole );
176 }
177 }
178
179 // apply expanded/collapsed state to the layer and its legend nodes
180 if ( rec.hasExpandedStateInfo() )
181 {
182 nodeLayer->setExpanded( layerRec.expandedLayerNode );
183 nodeLayer->setCustomProperty( u"expandedLegendNodes"_s, QStringList( layerRec.expandedLegendItems.constBegin(), layerRec.expandedLegendItems.constEnd() ) );
184 }
185}
186
187
188void QgsMapThemeCollection::applyThemeToGroup( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, const QgsMapThemeCollection::MapThemeRecord &rec )
189{
190 const QList<QgsLayerTreeNode *> constChildren = parent->children();
191 for ( QgsLayerTreeNode *node : constChildren )
192 {
193 if ( QgsLayerTree::isGroup( node ) )
194 {
195 applyThemeToGroup( QgsLayerTree::toGroup( node ), model, rec );
196 if ( rec.hasExpandedStateInfo() )
197 node->setExpanded( rec.expandedGroupNodes().contains( _groupId( node ) ) );
198 if ( rec.hasCheckedStateInfo() )
199 node->setItemVisibilityChecked( rec.checkedGroupNodes().contains( _groupId( node ) ) );
200 }
201 else if ( QgsLayerTree::isLayer( node ) )
202 applyThemeToLayer( QgsLayerTree::toLayer( node ), model, rec );
203 }
204}
205
206
208{
209 applyThemeToGroup( root, model, mapThemeState( name ) );
210
211 // also make sure that the preset is up-to-date (not containing any non-existent legend items)
212 update( name, createThemeFromCurrentState( root, model ) );
213}
214
216{
217 return mProject;
218}
219
221{
222 if ( project == mProject )
223 return;
224
225 disconnect( mProject, static_cast<void ( QgsProject::* )( const QStringList & )>( &QgsProject::layersWillBeRemoved ), this, &QgsMapThemeCollection::registryLayersRemoved );
226 mProject = project;
227 connect( mProject, static_cast<void ( QgsProject::* )( const QStringList & )>( &QgsProject::layersWillBeRemoved ), this, &QgsMapThemeCollection::registryLayersRemoved );
228 emit projectChanged();
229}
230
231QList<QgsMapLayer *> QgsMapThemeCollection::masterLayerOrder() const
232{
233 if ( !mProject )
234 return QList< QgsMapLayer * >();
235
236 return mProject->layerTreeRoot()->layerOrder();
237}
238
240{
241 const QList< QgsMapLayer *> allLayers = masterLayerOrder();
242 const QList< QgsMapLayer * > visibleLayers = mProject->layerTreeRoot()->checkedLayers();
243
244 if ( allLayers.isEmpty() )
245 {
246 // no project layer order set
247 return visibleLayers;
248 }
249 else
250 {
251 QList< QgsMapLayer * > orderedVisibleLayers;
252 for ( QgsMapLayer *layer : allLayers )
253 {
254 if ( visibleLayers.contains( layer ) )
255 orderedVisibleLayers << layer;
256 }
257 return orderedVisibleLayers;
258 }
259}
260
261
262bool QgsMapThemeCollection::hasMapTheme( const QString &name ) const
263{
264 return mMapThemes.contains( name );
265}
266
268{
269 mMapThemes.insert( name, state );
270
271 reconnectToLayersStyleManager();
272 emit mapThemeChanged( name );
273 emit mapThemesChanged();
274}
275
276void QgsMapThemeCollection::update( const QString &name, const MapThemeRecord &state )
277{
278 if ( !mMapThemes.contains( name ) )
279 return;
280
281 mMapThemes[name] = state;
282
283 reconnectToLayersStyleManager();
284 emit mapThemeChanged( name );
285 emit mapThemesChanged();
286}
287
288bool QgsMapThemeCollection::renameMapTheme( const QString &name, const QString &newName )
289{
290 if ( !mMapThemes.contains( name ) || mMapThemes.contains( newName ) )
291 return false;
292
293 const MapThemeRecord state = mMapThemes[name];
294 const MapThemeRecord newState = state;
295 insert( newName, newState );
296 emit mapThemeRenamed( name, newName );
297 removeMapTheme( name );
298 return true;
299}
300
301void QgsMapThemeCollection::removeMapTheme( const QString &name )
302{
303 if ( !mMapThemes.contains( name ) )
304 return;
305
306 mMapThemes.remove( name );
307
308 reconnectToLayersStyleManager();
309 emit mapThemesChanged();
310}
311
313{
314 mMapThemes.clear();
315
316 reconnectToLayersStyleManager();
317 emit mapThemesChanged();
318}
319
321{
322 return mMapThemes.keys();
323}
324
325QStringList QgsMapThemeCollection::mapThemeVisibleLayerIds( const QString &name ) const
326{
327 QStringList layerIds;
328 const QList<QgsMapLayer *> constMapThemeVisibleLayers = mapThemeVisibleLayers( name );
329 for ( QgsMapLayer *layer : constMapThemeVisibleLayers )
330 {
331 layerIds << layer->id();
332 }
333 return layerIds;
334}
335
336QList<QgsMapLayer *> QgsMapThemeCollection::mapThemeVisibleLayers( const QString &name ) const
337{
338 QList<QgsMapLayer *> layers;
339 const QList<MapThemeLayerRecord> recs = mMapThemes.value( name ).mLayerRecords;
340 const QList<QgsMapLayer *> layerOrder = masterLayerOrder();
341 if ( layerOrder.isEmpty() )
342 {
343 // no master layer order - so we have to just use the stored theme layer order as a fallback
344 const QList<MapThemeLayerRecord> records { mMapThemes.value( name ).mLayerRecords };
345 for ( const MapThemeLayerRecord &layerRec : records )
346 {
347 if ( layerRec.isVisible && layerRec.layer() )
348 layers << layerRec.layer();
349 }
350 }
351 else
352 {
353 for ( QgsMapLayer *layer : layerOrder )
354 {
355 for ( const MapThemeLayerRecord &layerRec : recs )
356 {
357 if ( layerRec.isVisible && layerRec.layer() == layer )
358 layers << layerRec.layer();
359 }
360 }
361 }
362
363 return layers;
364}
365
366
367void QgsMapThemeCollection::applyMapThemeCheckedLegendNodesToLayer( const MapThemeLayerRecord &layerRec, QgsMapLayer *layer )
368{
369 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
370 if ( !vlayer || !vlayer->renderer() )
371 return;
372
373 QgsFeatureRenderer *renderer = vlayer->renderer();
374 if ( !renderer->legendSymbolItemsCheckable() )
375 return; // no need to do anything
376
377 bool someNodesUnchecked = layerRec.usingLegendItems;
378
379 const auto constLegendSymbolItems = vlayer->renderer()->legendSymbolItems();
380 for ( const QgsLegendSymbolItem &item : constLegendSymbolItems )
381 {
382 bool checked = renderer->legendSymbolItemChecked( item.ruleKey() );
383 bool shouldBeChecked = someNodesUnchecked ? layerRec.checkedLegendItems.contains( item.ruleKey() ) : true;
384 if ( checked != shouldBeChecked )
385 renderer->checkLegendSymbolItem( item.ruleKey(), shouldBeChecked );
386 }
387}
388
389
390QMap<QString, QString> QgsMapThemeCollection::mapThemeStyleOverrides( const QString &presetName )
391{
392 QMap<QString, QString> styleOverrides;
393 if ( !mMapThemes.contains( presetName ) )
394 return styleOverrides;
395
396 const QList<MapThemeLayerRecord> records {mMapThemes.value( presetName ).mLayerRecords};
397 for ( const MapThemeLayerRecord &layerRec : records )
398 {
399 if ( !layerRec.layer() )
400 continue;
401
402 if ( layerRec.usingCurrentStyle )
403 {
404 QgsMapLayer *layer = layerRec.layer();
405 QgsMapLayerStyleOverride styleOverride( layer );
406 styleOverride.setOverrideStyle( layerRec.currentStyle );
407
408 // set the checked legend nodes
409 applyMapThemeCheckedLegendNodesToLayer( layerRec, layer );
410
411 // save to overrides
412 QgsMapLayerStyle layerStyle;
413 layerStyle.readFromLayer( layer );
414 styleOverrides[layer->id()] = layerStyle.xmlData();
415 }
416 }
417 return styleOverrides;
418}
419
420void QgsMapThemeCollection::reconnectToLayersStyleManager()
421{
422 QSet<QgsMapLayer *> layers;
423 for ( const MapThemeRecord &rec : std::as_const( mMapThemes ) )
424 {
425 for ( const MapThemeLayerRecord &layerRec : std::as_const( rec.mLayerRecords ) )
426 {
427 if ( auto *lLayer = layerRec.layer() )
428 layers << lLayer;
429 }
430 }
431
432 const QSet<QgsMapLayer *> constLayers = layers;
433 for ( QgsMapLayer *ml : constLayers )
434 {
435 connect( ml->styleManager(), &QgsMapLayerStyleManager::styleRenamed, this, &QgsMapThemeCollection::layerStyleRenamed );
436 }
437}
438
439void QgsMapThemeCollection::readXml( const QDomDocument &doc )
440{
441 clear();
442
443 QDomElement visPresetsElem = doc.firstChildElement( u"qgis"_s ).firstChildElement( u"visibility-presets"_s );
444 if ( visPresetsElem.isNull() )
445 return;
446
447 QDomElement visPresetElem = visPresetsElem.firstChildElement( u"visibility-preset"_s );
448 while ( !visPresetElem.isNull() )
449 {
450 QString presetName = visPresetElem.attribute( u"name"_s );
452 mMapThemes.insert( presetName, rec );
453 emit mapThemeChanged( presetName );
454 visPresetElem = visPresetElem.nextSiblingElement( u"visibility-preset"_s );
455 }
456
457 reconnectToLayersStyleManager();
458 emit mapThemesChanged();
459}
460
461void QgsMapThemeCollection::writeXml( QDomDocument &doc ) const
462{
463 QDomElement visPresetsElem = doc.createElement( u"visibility-presets"_s );
464
465 QList< QString > keys = mMapThemes.keys();
466
467 std::sort( keys.begin(), keys.end() );
468
469 for ( const QString &grpName : std::as_const( keys ) )
470 {
471 const MapThemeRecord &rec = mMapThemes.value( grpName );
472 QDomElement visPresetElem = doc.createElement( u"visibility-preset"_s );
473
474 visPresetElem.setAttribute( u"name"_s, grpName );
475 rec.writeXml( visPresetElem, doc );
476
477 visPresetsElem.appendChild( visPresetElem );
478 }
479
480 doc.firstChildElement( u"qgis"_s ).appendChild( visPresetsElem );
481}
482
483void QgsMapThemeCollection::registryLayersRemoved( const QStringList &layerIDs )
484{
485 // while layers are stored as weak pointers, this triggers the mapThemeChanged signal for
486 // affected themes
487 QSet< QString > changedThemes;
488 MapThemeRecordMap::iterator it = mMapThemes.begin();
489 for ( ; it != mMapThemes.end(); ++it )
490 {
491 MapThemeRecord &rec = it.value();
492 for ( int i = 0; i < rec.mLayerRecords.count(); ++i )
493 {
494 MapThemeLayerRecord &layerRec = rec.mLayerRecords[i];
495 if ( layerRec.layer() && layerIDs.contains( layerRec.layer()->id() ) )
496 {
497 rec.mLayerRecords.removeAt( i-- );
498 changedThemes << it.key();
499 }
500 }
501 }
502
503 for ( const QString &theme : std::as_const( changedThemes ) )
504 {
505 emit mapThemeChanged( theme );
506 }
507 emit mapThemesChanged();
508}
509
510void QgsMapThemeCollection::layerStyleRenamed( const QString &oldName, const QString &newName )
511{
512 QgsMapLayerStyleManager *styleMgr = qobject_cast<QgsMapLayerStyleManager *>( sender() );
513 if ( !styleMgr )
514 return;
515
516 QSet< QString > changedThemes;
517
518 MapThemeRecordMap::iterator it = mMapThemes.begin();
519 for ( ; it != mMapThemes.end(); ++it )
520 {
521 MapThemeRecord &rec = it.value();
522 for ( int i = 0; i < rec.mLayerRecords.count(); ++i )
523 {
524 MapThemeLayerRecord &layerRec = rec.mLayerRecords[i];
525 if ( layerRec.layer() == styleMgr->layer() )
526 {
527 if ( layerRec.currentStyle == oldName )
528 {
529 layerRec.currentStyle = newName;
530 changedThemes << it.key();
531 }
532 }
533 }
534 }
535
536 for ( const QString &theme : std::as_const( changedThemes ) )
537 {
538 emit mapThemeChanged( theme );
539 }
540 emit mapThemesChanged();
541}
542
544{
545 for ( int i = 0; i < mLayerRecords.length(); ++i )
546 {
547 if ( mLayerRecords.at( i ).layer() == layer )
548 mLayerRecords.removeAt( i );
549 }
550}
551
553{
554 mLayerRecords.append( record );
555}
556
557QHash<QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord> QgsMapThemeCollection::MapThemeRecord::validLayerRecords() const
558{
559 QHash<QgsMapLayer *, MapThemeLayerRecord> validSet;
560 for ( const MapThemeLayerRecord &layerRec : mLayerRecords )
561 {
562 if ( auto *lLayer = layerRec.layer() )
563 validSet.insert( lLayer, layerRec );
564 }
565 return validSet;
566}
567
569{
570 QHash<QString, MapThemeLayerRecord> layerRecords; // key = layer ID
571
572 bool expandedStateInfo = false;
573 if ( element.hasAttribute( u"has-expanded-info"_s ) )
574 expandedStateInfo = element.attribute( u"has-expanded-info"_s ).toInt();
575
576 bool checkedStateInfo = false;
577 if ( element.hasAttribute( u"has-checked-group-info"_s ) )
578 checkedStateInfo = element.attribute( u"has-checked-group-info"_s ).toInt();
579
580 QDomElement visPresetLayerElem = element.firstChildElement( u"layer"_s );
581 while ( !visPresetLayerElem.isNull() )
582 {
583 QString layerID = visPresetLayerElem.attribute( u"id"_s );
584 if ( QgsMapLayer *layer = project->mapLayer( layerID ) )
585 {
586 layerRecords[layerID] = MapThemeLayerRecord( layer );
587 layerRecords[layerID].isVisible = visPresetLayerElem.attribute( u"visible"_s, u"1"_s ).toInt();
588
589 if ( visPresetLayerElem.hasAttribute( u"style"_s ) )
590 {
591 layerRecords[layerID].usingCurrentStyle = true;
592 layerRecords[layerID].currentStyle = visPresetLayerElem.attribute( u"style"_s );
593 }
594
595 if ( visPresetLayerElem.hasAttribute( u"expanded"_s ) )
596 layerRecords[layerID].expandedLayerNode = visPresetLayerElem.attribute( u"expanded"_s ).toInt();
597 }
598 visPresetLayerElem = visPresetLayerElem.nextSiblingElement( u"layer"_s );
599 }
600
601 QDomElement checkedLegendNodesElem = element.firstChildElement( u"checked-legend-nodes"_s );
602 while ( !checkedLegendNodesElem.isNull() )
603 {
604 QSet<QString> checkedLegendNodes;
605
606 QDomElement checkedLegendNodeElem = checkedLegendNodesElem.firstChildElement( u"checked-legend-node"_s );
607 while ( !checkedLegendNodeElem.isNull() )
608 {
609 checkedLegendNodes << checkedLegendNodeElem.attribute( u"id"_s );
610 checkedLegendNodeElem = checkedLegendNodeElem.nextSiblingElement( u"checked-legend-node"_s );
611 }
612
613 QString layerID = checkedLegendNodesElem.attribute( u"id"_s );
614 if ( project->mapLayer( layerID ) ) // only use valid IDs
615 {
616 layerRecords[layerID].usingLegendItems = true;
617 layerRecords[layerID].checkedLegendItems = checkedLegendNodes;
618 }
619 checkedLegendNodesElem = checkedLegendNodesElem.nextSiblingElement( u"checked-legend-nodes"_s );
620 }
621
622 QSet<QString> expandedGroupNodes;
623 if ( expandedStateInfo )
624 {
625 // expanded state of legend nodes
626 QDomElement expandedLegendNodesElem = element.firstChildElement( u"expanded-legend-nodes"_s );
627 while ( !expandedLegendNodesElem.isNull() )
628 {
629 QSet<QString> expandedLegendNodes;
630
631 QDomElement expandedLegendNodeElem = expandedLegendNodesElem.firstChildElement( u"expanded-legend-node"_s );
632 while ( !expandedLegendNodeElem.isNull() )
633 {
634 expandedLegendNodes << expandedLegendNodeElem.attribute( u"id"_s );
635 expandedLegendNodeElem = expandedLegendNodeElem.nextSiblingElement( u"expanded-legend-node"_s );
636 }
637
638 QString layerID = expandedLegendNodesElem.attribute( u"id"_s );
639 if ( project->mapLayer( layerID ) ) // only use valid IDs
640 {
641 layerRecords[layerID].expandedLegendItems = expandedLegendNodes;
642 }
643 expandedLegendNodesElem = expandedLegendNodesElem.nextSiblingElement( u"expanded-legend-nodes"_s );
644 }
645
646 // expanded state of group nodes
647 QDomElement expandedGroupNodesElem = element.firstChildElement( u"expanded-group-nodes"_s );
648 if ( !expandedGroupNodesElem.isNull() )
649 {
650 QDomElement expandedGroupNodeElem = expandedGroupNodesElem.firstChildElement( u"expanded-group-node"_s );
651 while ( !expandedGroupNodeElem.isNull() )
652 {
653 expandedGroupNodes << expandedGroupNodeElem.attribute( u"id"_s );
654 expandedGroupNodeElem = expandedGroupNodeElem.nextSiblingElement( u"expanded-group-node"_s );
655 }
656 }
657 }
658
659 QSet<QString> checkedGroupNodes;
660 if ( checkedStateInfo )
661 {
662 // expanded state of legend nodes
663 QDomElement checkedGroupNodesElem = element.firstChildElement( u"checked-group-nodes"_s );
664 if ( !checkedGroupNodesElem.isNull() )
665 {
666 QDomElement checkedGroupNodeElem = checkedGroupNodesElem.firstChildElement( u"checked-group-node"_s );
667 while ( !checkedGroupNodeElem.isNull() )
668 {
669 checkedGroupNodes << checkedGroupNodeElem.attribute( u"id"_s );
670 checkedGroupNodeElem = checkedGroupNodeElem.nextSiblingElement( u"checked-group-node"_s );
671 }
672 }
673 }
674
675 MapThemeRecord rec;
676 rec.setLayerRecords( layerRecords.values() );
677 rec.setHasExpandedStateInfo( expandedStateInfo );
679 rec.setHasCheckedStateInfo( checkedStateInfo );
681
682 return rec;
683}
684
685void QgsMapThemeCollection::MapThemeRecord::writeXml( QDomElement element, QDomDocument &document ) const
686{
687 if ( hasExpandedStateInfo() )
688 element.setAttribute( u"has-expanded-info"_s, u"1"_s );
689 if ( hasCheckedStateInfo() )
690 element.setAttribute( u"has-checked-group-info"_s, u"1"_s );
691 for ( const MapThemeLayerRecord &layerRec : std::as_const( mLayerRecords ) )
692 {
693 if ( !layerRec.layer() )
694 continue;
695 QString layerID = layerRec.layer()->id();
696 QDomElement layerElem = document.createElement( u"layer"_s );
697 layerElem.setAttribute( u"id"_s, layerID );
698 layerElem.setAttribute( u"visible"_s, layerRec.isVisible ? u"1"_s : u"0"_s );
699 if ( layerRec.usingCurrentStyle )
700 layerElem.setAttribute( u"style"_s, layerRec.currentStyle );
701 element.appendChild( layerElem );
702
703 if ( layerRec.usingLegendItems )
704 {
705 QDomElement checkedLegendNodesElem = document.createElement( u"checked-legend-nodes"_s );
706 checkedLegendNodesElem.setAttribute( u"id"_s, layerID );
707 for ( const QString &checkedLegendNode : std::as_const( layerRec.checkedLegendItems ) )
708 {
709 QDomElement checkedLegendNodeElem = document.createElement( u"checked-legend-node"_s );
710 checkedLegendNodeElem.setAttribute( u"id"_s, checkedLegendNode );
711 checkedLegendNodesElem.appendChild( checkedLegendNodeElem );
712 }
713 element.appendChild( checkedLegendNodesElem );
714 }
715
716 if ( hasExpandedStateInfo() )
717 {
718 layerElem.setAttribute( u"expanded"_s, layerRec.expandedLayerNode ? u"1"_s : u"0"_s );
719
720 QDomElement expandedLegendNodesElem = document.createElement( u"expanded-legend-nodes"_s );
721 expandedLegendNodesElem.setAttribute( u"id"_s, layerID );
722 for ( const QString &expandedLegendNode : std::as_const( layerRec.expandedLegendItems ) )
723 {
724 QDomElement expandedLegendNodeElem = document.createElement( u"expanded-legend-node"_s );
725 expandedLegendNodeElem.setAttribute( u"id"_s, expandedLegendNode );
726 expandedLegendNodesElem.appendChild( expandedLegendNodeElem );
727 }
728 element.appendChild( expandedLegendNodesElem );
729 }
730 }
731
732 if ( hasCheckedStateInfo() )
733 {
734 QDomElement checkedGroupElems = document.createElement( u"checked-group-nodes"_s );
735 const QSet<QString> _checkedGroupNodes = checkedGroupNodes();
736 for ( const QString &groupId : _checkedGroupNodes )
737 {
738 QDomElement checkedGroupElem = document.createElement( u"checked-group-node"_s );
739 checkedGroupElem.setAttribute( u"id"_s, groupId );
740 checkedGroupElems.appendChild( checkedGroupElem );
741 }
742 element.appendChild( checkedGroupElems );
743 }
744
745 if ( hasExpandedStateInfo() )
746 {
747 QDomElement expandedGroupElems = document.createElement( u"expanded-group-nodes"_s );
748 const QSet<QString> _expandedGroupNodes = expandedGroupNodes();
749 for ( const QString &groupId : _expandedGroupNodes )
750 {
751 QDomElement expandedGroupElem = document.createElement( u"expanded-group-node"_s );
752 expandedGroupElem.setAttribute( u"id"_s, groupId );
753 expandedGroupElems.appendChild( expandedGroupElem );
754 }
755 element.appendChild( expandedGroupElems );
756 }
757}
758
Abstract base class for all 2D vector feature renderers.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
virtual bool legendSymbolItemsCheckable() const
Returns true if symbology items in legend are checkable.
virtual bool legendSymbolItemChecked(const QString &key)
Returns true if the legend symbology item with the specified key is checked.
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
Sets whether the legend symbology item with the specified ley should be checked.
Layer tree group node serves as a container for layers and further groups.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
An abstract interface for legend items returned from QgsMapLayerLegend implementation.
virtual QVariant data(int role) const =0
Returns data associated with the item. Must be implemented in derived class.
virtual Qt::ItemFlags flags() const
Returns item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
virtual bool setData(const QVariant &value, int role)
Sets some data associated with the item. Default implementation does nothing and returns false.
A model representing the layer tree, including layers and groups of layers.
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Returns filtered list of active legend nodes attached to a particular layer node (by default it retur...
Base class for nodes in a layer tree.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well).
void setCustomProperty(const QString &key, const QVariant &value)
Sets a custom property for the node. Properties are stored in a map and saved in project file.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
void setExpanded(bool expanded)
Sets whether the node should be shown as expanded or collapsed in GUI.
virtual QString name() const =0
Returns name of the node.
QgsLayerTreeNode * parent()
Gets pointer to the parent. If parent is nullptr, the node is a root node.
bool isExpanded() const
Returns whether the node should be shown as expanded or collapsed in GUI.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children).
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children).
void setItemVisibilityCheckedParentRecursive(bool checked)
Check or uncheck a node and all its parents.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Stores information about one class/rule of a vector layer renderer in a unified way that can be used ...
QString currentStyle() const
Returns name of the current style.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.
void styleRenamed(const QString &oldName, const QString &newName)
Emitted when a style has been renamed.
QgsMapLayer * layer() const
Gets pointer to the associated map layer.
Restore overridden layer style on destruction.
void setOverrideStyle(const QString &style)
Temporarily apply a different style to the layer.
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer.
void readFromLayer(QgsMapLayer *layer)
Store layer's active style information in the instance.
QString xmlData() const
Returns XML content of the style.
Base class for all map layer types.
Definition qgsmaplayer.h:83
QString id
Definition qgsmaplayer.h:86
QgsMapLayerStyleManager * styleManager() const
Gets access to the layer's style manager.
Individual record of a visible layer in a map theme record.
void setLayer(QgsMapLayer *layer)
Sets the map layer for this record.
QgsMapLayer * layer() const
Returns map layer or nullptr if the layer does not exist anymore.
Individual map theme record of visible layers and styles.
void setExpandedGroupNodes(const QSet< QString > &expandedGroupNodes)
Sets a set of group identifiers for group nodes that should have expanded state.
void writeXml(QDomElement element, QDomDocument &document) const
Writes the map theme record to XML.
void removeLayerRecord(QgsMapLayer *layer)
Removes a record for layer if present.
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.
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.
static MapThemeRecord readXml(const QDomElement &element, const QgsProject *project)
Reads the map theme record from XML.
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.
void addLayerRecord(const QgsMapThemeCollection::MapThemeLayerRecord &record)
Add a new record for a layer.
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.
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
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.
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...
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
QList< QgsMapLayer * > masterVisibleLayers() const
Returns the master list of visible layers.
QStringList mapThemeVisibleLayerIds(const QString &name) const
Returns the list of layer IDs that are visible for the specified map theme.
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.
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:112
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the registry.
Represents a vector layer which manages a vector based dataset.
QgsFeatureRenderer * renderer()
Returns the feature renderer used for rendering the features in the layer in 2D map views.
QgsLayerTreeModelLegendNode * legendNode(const QString &rule, QgsLayerTreeModel &model)