QGIS API Documentation  3.0.2-Girona (307d082)
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 
16 #include "qgsmapthemecollection.h"
17 
18 #include "qgslayertree.h"
19 #include "qgslayertreemodel.h"
21 #include "qgsmaplayerlistutils.h"
23 #include "qgsproject.h"
24 #include "qgsrenderer.h"
25 #include "qgsvectorlayer.h"
26 
27 #include <QInputDialog>
28 
30  : mProject( project )
31 {
32  connect( project, static_cast<void ( QgsProject::* )( const QStringList & )>( &QgsProject::layersWillBeRemoved ), this, &QgsMapThemeCollection::registryLayersRemoved );
33 }
34 
35 QgsMapThemeCollection::MapThemeLayerRecord QgsMapThemeCollection::createThemeLayerRecord( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model )
36 {
37  MapThemeLayerRecord layerRec( nodeLayer->layer() );
38  layerRec.usingCurrentStyle = true;
39  layerRec.currentStyle = nodeLayer->layer()->styleManager()->currentStyle();
40 
41  // get checked legend items
42  bool hasCheckableItems = false;
43  bool someItemsUnchecked = false;
44  QSet<QString> checkedItems;
45  Q_FOREACH ( QgsLayerTreeModelLegendNode *legendNode, model->layerLegendNodes( nodeLayer, true ) )
46  {
47  if ( legendNode->flags() & Qt::ItemIsUserCheckable )
48  {
49  hasCheckableItems = true;
50 
51  if ( legendNode->data( Qt::CheckStateRole ).toInt() == Qt::Checked )
52  checkedItems << legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
53  else
54  someItemsUnchecked = true;
55  }
56  }
57 
58  if ( hasCheckableItems && someItemsUnchecked )
59  {
60  layerRec.usingLegendItems = true;
61  layerRec.checkedLegendItems = checkedItems;
62  }
63  return layerRec;
64 }
65 
67 {
68  Q_FOREACH ( QgsLayerTreeNode *node, parent->children() )
69  {
70  if ( QgsLayerTree::isGroup( node ) )
72  else if ( QgsLayerTree::isLayer( node ) )
73  {
74  QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
75  if ( nodeLayer->isVisible() )
76  rec.mLayerRecords << createThemeLayerRecord( nodeLayer, model );
77  }
78  }
79 }
80 
82 {
84  createThemeFromCurrentState( root, model, rec );
85  return rec;
86 }
87 
88 bool QgsMapThemeCollection::findRecordForLayer( QgsMapLayer *layer, const QgsMapThemeCollection::MapThemeRecord &rec, QgsMapThemeCollection::MapThemeLayerRecord &layerRec )
89 {
90  Q_FOREACH ( const QgsMapThemeCollection::MapThemeLayerRecord &lr, rec.mLayerRecords )
91  {
92  if ( lr.layer() == layer )
93  {
94  layerRec = lr;
95  return true;
96  }
97  }
98  return false;
99 }
100 
101 void QgsMapThemeCollection::applyThemeToLayer( QgsLayerTreeLayer *nodeLayer, QgsLayerTreeModel *model, const QgsMapThemeCollection::MapThemeRecord &rec )
102 {
103  MapThemeLayerRecord layerRec;
104  bool isVisible = findRecordForLayer( nodeLayer->layer(), rec, layerRec );
105 
106  // Make sure the whole tree is visible
107  if ( isVisible )
108  nodeLayer->setItemVisibilityCheckedParentRecursive( isVisible );
109  else
110  nodeLayer->setItemVisibilityChecked( isVisible );
111 
112  if ( !isVisible )
113  return;
114 
115  if ( layerRec.usingCurrentStyle )
116  {
117  // apply desired style first
118  nodeLayer->layer()->styleManager()->setCurrentStyle( layerRec.currentStyle );
119  }
120 
121  if ( layerRec.usingLegendItems )
122  {
123  // some nodes are not checked
124  Q_FOREACH ( QgsLayerTreeModelLegendNode *legendNode, model->layerLegendNodes( nodeLayer, true ) )
125  {
126  QString ruleKey = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
127  Qt::CheckState shouldHaveState = layerRec.checkedLegendItems.contains( ruleKey ) ? Qt::Checked : Qt::Unchecked;
128  if ( ( legendNode->flags() & Qt::ItemIsUserCheckable ) &&
129  legendNode->data( Qt::CheckStateRole ).toInt() != shouldHaveState )
130  legendNode->setData( shouldHaveState, Qt::CheckStateRole );
131  }
132  }
133  else
134  {
135  // all nodes should be checked
136  Q_FOREACH ( QgsLayerTreeModelLegendNode *legendNode, model->layerLegendNodes( nodeLayer, true ) )
137  {
138  if ( ( legendNode->flags() & Qt::ItemIsUserCheckable ) &&
139  legendNode->data( Qt::CheckStateRole ).toInt() != Qt::Checked )
140  legendNode->setData( Qt::Checked, Qt::CheckStateRole );
141  }
142  }
143 }
144 
145 
146 void QgsMapThemeCollection::applyThemeToGroup( QgsLayerTreeGroup *parent, QgsLayerTreeModel *model, const QgsMapThemeCollection::MapThemeRecord &rec )
147 {
148  Q_FOREACH ( QgsLayerTreeNode *node, parent->children() )
149  {
150  if ( QgsLayerTree::isGroup( node ) )
151  applyThemeToGroup( QgsLayerTree::toGroup( node ), model, rec );
152  else if ( QgsLayerTree::isLayer( node ) )
153  applyThemeToLayer( QgsLayerTree::toLayer( node ), model, rec );
154  }
155 }
156 
157 
159 {
160  applyThemeToGroup( root, model, mapThemeState( name ) );
161 
162  // also make sure that the preset is up-to-date (not containing any non-existent legend items)
163  update( name, createThemeFromCurrentState( root, model ) );
164 }
165 
167 {
168  return mProject;
169 }
170 
172 {
173  if ( project == mProject )
174  return;
175 
176  disconnect( mProject, static_cast<void ( QgsProject::* )( const QStringList & )>( &QgsProject::layersWillBeRemoved ), this, &QgsMapThemeCollection::registryLayersRemoved );
177  mProject = project;
178  connect( mProject, static_cast<void ( QgsProject::* )( const QStringList & )>( &QgsProject::layersWillBeRemoved ), this, &QgsMapThemeCollection::registryLayersRemoved );
179  emit projectChanged();
180 }
181 
182 QList<QgsMapLayer *> QgsMapThemeCollection::masterLayerOrder() const
183 {
184  if ( !mProject )
185  return QList< QgsMapLayer * >();
186 
187  return mProject->layerTreeRoot()->layerOrder();
188 }
189 
190 QList<QgsMapLayer *> QgsMapThemeCollection::masterVisibleLayers() const
191 {
192  QList< QgsMapLayer *> allLayers = masterLayerOrder();
193  QList< QgsMapLayer * > visibleLayers = mProject->layerTreeRoot()->checkedLayers();
194 
195  if ( allLayers.isEmpty() )
196  {
197  // no project layer order set
198  return visibleLayers;
199  }
200  else
201  {
202  QList< QgsMapLayer * > orderedVisibleLayers;
203  Q_FOREACH ( QgsMapLayer *layer, allLayers )
204  {
205  if ( visibleLayers.contains( layer ) )
206  orderedVisibleLayers << layer;
207  }
208  return orderedVisibleLayers;
209  }
210 }
211 
212 
213 bool QgsMapThemeCollection::hasMapTheme( const QString &name ) const
214 {
215  return mMapThemes.contains( name );
216 }
217 
219 {
220  mMapThemes.insert( name, state );
221 
222  reconnectToLayersStyleManager();
223  emit mapThemeChanged( name );
224  emit mapThemesChanged();
225 }
226 
227 void QgsMapThemeCollection::update( const QString &name, const MapThemeRecord &state )
228 {
229  if ( !mMapThemes.contains( name ) )
230  return;
231 
232  mMapThemes[name] = state;
233 
234  reconnectToLayersStyleManager();
235  emit mapThemeChanged( name );
236  emit mapThemesChanged();
237 }
238 
239 void QgsMapThemeCollection::removeMapTheme( const QString &name )
240 {
241  if ( !mMapThemes.contains( name ) )
242  return;
243 
244  mMapThemes.remove( name );
245 
246  reconnectToLayersStyleManager();
247  emit mapThemesChanged();
248 }
249 
251 {
252  mMapThemes.clear();
253 
254  reconnectToLayersStyleManager();
255  emit mapThemesChanged();
256 }
257 
258 QStringList QgsMapThemeCollection::mapThemes() const
259 {
260  return mMapThemes.keys();
261 }
262 
263 QStringList QgsMapThemeCollection::mapThemeVisibleLayerIds( const QString &name ) const
264 {
265  QStringList layerIds;
266  Q_FOREACH ( QgsMapLayer *layer, mapThemeVisibleLayers( name ) )
267  {
268  layerIds << layer->id();
269  }
270  return layerIds;
271 }
272 
273 QList<QgsMapLayer *> QgsMapThemeCollection::mapThemeVisibleLayers( const QString &name ) const
274 {
275  QList<QgsMapLayer *> layers;
276  const QList<MapThemeLayerRecord> &recs = mMapThemes.value( name ).mLayerRecords;
277  QList<QgsMapLayer *> layerOrder = masterLayerOrder();
278  if ( layerOrder.isEmpty() )
279  {
280  // no master layer order - so we have to just use the stored theme layer order as a fallback
281  Q_FOREACH ( const MapThemeLayerRecord &layerRec, mMapThemes.value( name ).mLayerRecords )
282  {
283  if ( layerRec.layer() )
284  layers << layerRec.layer();
285  }
286  }
287  else
288  {
289  Q_FOREACH ( QgsMapLayer *layer, layerOrder )
290  {
291  Q_FOREACH ( const MapThemeLayerRecord &layerRec, recs )
292  {
293  if ( layerRec.layer() == layer )
294  layers << layerRec.layer();
295  }
296  }
297  }
298 
299  return layers;
300 }
301 
302 
303 void QgsMapThemeCollection::applyMapThemeCheckedLegendNodesToLayer( const MapThemeLayerRecord &layerRec, QgsMapLayer *layer )
304 {
305  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
306  if ( !vlayer || !vlayer->renderer() )
307  return;
308 
309  QgsFeatureRenderer *renderer = vlayer->renderer();
310  if ( !renderer->legendSymbolItemsCheckable() )
311  return; // no need to do anything
312 
313  bool someNodesUnchecked = layerRec.usingLegendItems;
314 
315  Q_FOREACH ( const QgsLegendSymbolItem &item, vlayer->renderer()->legendSymbolItems() )
316  {
317  bool checked = renderer->legendSymbolItemChecked( item.ruleKey() );
318  bool shouldBeChecked = someNodesUnchecked ? layerRec.checkedLegendItems.contains( item.ruleKey() ) : true;
319  if ( checked != shouldBeChecked )
320  renderer->checkLegendSymbolItem( item.ruleKey(), shouldBeChecked );
321  }
322 }
323 
324 
325 QMap<QString, QString> QgsMapThemeCollection::mapThemeStyleOverrides( const QString &presetName )
326 {
327  QMap<QString, QString> styleOverrides;
328  if ( !mMapThemes.contains( presetName ) )
329  return styleOverrides;
330 
331  Q_FOREACH ( const MapThemeLayerRecord &layerRec, mMapThemes.value( presetName ).mLayerRecords )
332  {
333  if ( !layerRec.layer() )
334  continue;
335 
336  if ( layerRec.usingCurrentStyle )
337  {
338  QgsMapLayer *layer = layerRec.layer();
339  layer->styleManager()->setOverrideStyle( layerRec.currentStyle );
340 
341  // set the checked legend nodes
342  applyMapThemeCheckedLegendNodesToLayer( layerRec, layer );
343 
344  // save to overrides
345  QgsMapLayerStyle layerStyle;
346  layerStyle.readFromLayer( layer );
347  styleOverrides[layer->id()] = layerStyle.xmlData();
348 
350  }
351  }
352  return styleOverrides;
353 }
354 
355 void QgsMapThemeCollection::reconnectToLayersStyleManager()
356 {
357  // disconnect( 0, 0, this, SLOT( layerStyleRenamed( QString, QString ) ) );
358 
359  QSet<QgsMapLayer *> layers;
360  Q_FOREACH ( const MapThemeRecord &rec, mMapThemes )
361  {
362  Q_FOREACH ( const MapThemeLayerRecord &layerRec, rec.mLayerRecords )
363  {
364  if ( layerRec.layer() )
365  layers << layerRec.layer();
366  }
367  }
368 
369  Q_FOREACH ( QgsMapLayer *ml, layers )
370  {
371  connect( ml->styleManager(), &QgsMapLayerStyleManager::styleRenamed, this, &QgsMapThemeCollection::layerStyleRenamed );
372  }
373 }
374 
375 void QgsMapThemeCollection::readXml( const QDomDocument &doc )
376 {
377  clear();
378 
379  QDomElement visPresetsElem = doc.firstChildElement( QStringLiteral( "qgis" ) ).firstChildElement( QStringLiteral( "visibility-presets" ) );
380  if ( visPresetsElem.isNull() )
381  return;
382 
383  QDomElement visPresetElem = visPresetsElem.firstChildElement( QStringLiteral( "visibility-preset" ) );
384  while ( !visPresetElem.isNull() )
385  {
386  QHash<QString, MapThemeLayerRecord> layerRecords; // key = layer ID
387 
388  QString presetName = visPresetElem.attribute( QStringLiteral( "name" ) );
389  QDomElement visPresetLayerElem = visPresetElem.firstChildElement( QStringLiteral( "layer" ) );
390  while ( !visPresetLayerElem.isNull() )
391  {
392  QString layerID = visPresetLayerElem.attribute( QStringLiteral( "id" ) );
393  if ( QgsMapLayer *layer = mProject->mapLayer( layerID ) )
394  {
395  layerRecords[layerID] = MapThemeLayerRecord( layer );
396 
397  if ( visPresetLayerElem.hasAttribute( QStringLiteral( "style" ) ) )
398  {
399  layerRecords[layerID].usingCurrentStyle = true;
400  layerRecords[layerID].currentStyle = visPresetLayerElem.attribute( QStringLiteral( "style" ) );
401  }
402  }
403  visPresetLayerElem = visPresetLayerElem.nextSiblingElement( QStringLiteral( "layer" ) );
404  }
405 
406  QDomElement checkedLegendNodesElem = visPresetElem.firstChildElement( QStringLiteral( "checked-legend-nodes" ) );
407  while ( !checkedLegendNodesElem.isNull() )
408  {
409  QSet<QString> checkedLegendNodes;
410 
411  QDomElement checkedLegendNodeElem = checkedLegendNodesElem.firstChildElement( QStringLiteral( "checked-legend-node" ) );
412  while ( !checkedLegendNodeElem.isNull() )
413  {
414  checkedLegendNodes << checkedLegendNodeElem.attribute( QStringLiteral( "id" ) );
415  checkedLegendNodeElem = checkedLegendNodeElem.nextSiblingElement( QStringLiteral( "checked-legend-node" ) );
416  }
417 
418  QString layerID = checkedLegendNodesElem.attribute( QStringLiteral( "id" ) );
419  if ( mProject->mapLayer( layerID ) ) // only use valid IDs
420  {
421  layerRecords[layerID].usingLegendItems = true;
422  layerRecords[layerID].checkedLegendItems = checkedLegendNodes;
423  }
424  checkedLegendNodesElem = checkedLegendNodesElem.nextSiblingElement( QStringLiteral( "checked-legend-nodes" ) );
425  }
426 
427  MapThemeRecord rec;
428  rec.setLayerRecords( layerRecords.values() );
429  mMapThemes.insert( presetName, rec );
430  emit mapThemeChanged( presetName );
431 
432  visPresetElem = visPresetElem.nextSiblingElement( QStringLiteral( "visibility-preset" ) );
433  }
434 
435  reconnectToLayersStyleManager();
436  emit mapThemesChanged();
437 }
438 
439 void QgsMapThemeCollection::writeXml( QDomDocument &doc )
440 {
441  QDomElement visPresetsElem = doc.createElement( QStringLiteral( "visibility-presets" ) );
442  MapThemeRecordMap::const_iterator it = mMapThemes.constBegin();
443  for ( ; it != mMapThemes.constEnd(); ++ it )
444  {
445  QString grpName = it.key();
446  const MapThemeRecord &rec = it.value();
447  QDomElement visPresetElem = doc.createElement( QStringLiteral( "visibility-preset" ) );
448  visPresetElem.setAttribute( QStringLiteral( "name" ), grpName );
449  Q_FOREACH ( const MapThemeLayerRecord &layerRec, rec.mLayerRecords )
450  {
451  if ( !layerRec.layer() )
452  continue;
453  QString layerID = layerRec.layer()->id();
454  QDomElement layerElem = doc.createElement( QStringLiteral( "layer" ) );
455  layerElem.setAttribute( QStringLiteral( "id" ), layerID );
456  if ( layerRec.usingCurrentStyle )
457  layerElem.setAttribute( QStringLiteral( "style" ), layerRec.currentStyle );
458  visPresetElem.appendChild( layerElem );
459 
460  if ( layerRec.usingLegendItems )
461  {
462  QDomElement checkedLegendNodesElem = doc.createElement( QStringLiteral( "checked-legend-nodes" ) );
463  checkedLegendNodesElem.setAttribute( QStringLiteral( "id" ), layerID );
464  Q_FOREACH ( const QString &checkedLegendNode, layerRec.checkedLegendItems )
465  {
466  QDomElement checkedLegendNodeElem = doc.createElement( QStringLiteral( "checked-legend-node" ) );
467  checkedLegendNodeElem.setAttribute( QStringLiteral( "id" ), checkedLegendNode );
468  checkedLegendNodesElem.appendChild( checkedLegendNodeElem );
469  }
470  visPresetElem.appendChild( checkedLegendNodesElem );
471  }
472  }
473 
474  visPresetsElem.appendChild( visPresetElem );
475  }
476 
477  doc.firstChildElement( QStringLiteral( "qgis" ) ).appendChild( visPresetsElem );
478 }
479 
480 void QgsMapThemeCollection::registryLayersRemoved( const QStringList &layerIDs )
481 {
482  // while layers are stored as weak pointers, this triggers the mapThemeChanged signal for
483  // affected themes
484  QSet< QString > changedThemes;
485  MapThemeRecordMap::iterator it = mMapThemes.begin();
486  for ( ; it != mMapThemes.end(); ++it )
487  {
488  MapThemeRecord &rec = it.value();
489  for ( int i = 0; i < rec.mLayerRecords.count(); ++i )
490  {
491  MapThemeLayerRecord &layerRec = rec.mLayerRecords[i];
492  if ( layerRec.layer() && layerIDs.contains( layerRec.layer()->id() ) )
493  {
494  rec.mLayerRecords.removeAt( i-- );
495  changedThemes << it.key();
496  }
497  }
498  }
499 
500  Q_FOREACH ( const QString &theme, changedThemes )
501  {
502  emit mapThemeChanged( theme );
503  }
504  emit mapThemesChanged();
505 }
506 
507 void QgsMapThemeCollection::layerStyleRenamed( const QString &oldName, const QString &newName )
508 {
509  QgsMapLayerStyleManager *styleMgr = qobject_cast<QgsMapLayerStyleManager *>( sender() );
510  if ( !styleMgr )
511  return;
512 
513  QSet< QString > changedThemes;
514 
515  MapThemeRecordMap::iterator it = mMapThemes.begin();
516  for ( ; it != mMapThemes.end(); ++it )
517  {
518  MapThemeRecord &rec = it.value();
519  for ( int i = 0; i < rec.mLayerRecords.count(); ++i )
520  {
521  MapThemeLayerRecord &layerRec = rec.mLayerRecords[i];
522  if ( layerRec.layer() == styleMgr->layer() )
523  {
524  if ( layerRec.currentStyle == oldName )
525  {
526  layerRec.currentStyle = newName;
527  changedThemes << it.key();
528  }
529  }
530  }
531  }
532  Q_FOREACH ( const QString &theme, changedThemes )
533  {
534  emit mapThemeChanged( theme );
535  }
536  emit mapThemesChanged();
537 }
538 
540 {
541  for ( int i = 0; i < mLayerRecords.length(); ++i )
542  {
543  if ( mLayerRecords.at( i ).layer() == layer )
544  mLayerRecords.removeAt( i );
545  }
546 }
547 
549 {
550  mLayerRecords.append( record );
551 }
552 
553 QHash<QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord> QgsMapThemeCollection::MapThemeRecord::validLayerRecords() const
554 {
555  QHash<QgsMapLayer *, MapThemeLayerRecord> validSet;
556  Q_FOREACH ( const MapThemeLayerRecord &layerRec, mLayerRecords )
557  {
558  if ( layerRec.layer() )
559  validSet.insert( layerRec.layer(), layerRec );
560  }
561  return validSet;
562 }
563 
565 {
566  mLayer = layer;
567 }
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
Layer tree group node serves as a container for layers and further groups.
virtual QgsLegendSymbolList legendSymbolItems() const
Returns a list of symbology items for the legend.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:75
QgsMapLayer * layer() const
Returns map layer or null if the layer does not exist anymore.
Base class for all map layer types.
Definition: qgsmaplayer.h:56
void setLayer(QgsMapLayer *layer)
Set the map layer for this record.
void setProject(QgsProject *project)
The QgsProject on which this map theme collection works.
virtual Qt::ItemFlags flags() const
Return item flags associated with the item. Default implementation returns Qt::ItemIsEnabled.
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:43
void styleRenamed(const QString &oldName, const QString &newName)
Emitted when a style has been renamed.
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
QString ruleKey() const
Return unique identifier of the rule for identification of the item within renderer.
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Get layer style overrides (for QgsMapSettings) of the visible layers for given map theme...
QString xmlData() const
Return XML content of the style.
QHash< QgsMapLayer *, QgsMapThemeCollection::MapThemeLayerRecord > validLayerRecords() const
Return set with only records for valid layers.
virtual bool setData(const QVariant &value, int role)
Set some data associated with the item. Default implementation does nothing and returns false...
QgsMapThemeCollection::MapThemeRecord mapThemeState(const QString &name) const
Returns the recorded state of a map theme.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Definition: qgslayertree.h:64
Individual map theme record of visible layers and styles.
void update(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Updates a map theme within the collection.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well) ...
void clear()
Remove all map themes from the collection.
Individual record of a visible layer in a map theme record.
void mapThemesChanged()
Emitted when map themes within the collection are changed.
QList< QgsMapLayer *> checkedLayers() const
Returns a list of any checked layers which belong to this node or its children.
void removeLayerRecord(QgsMapLayer *layer)
Removes a record for layer if present.
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.
QgsMapLayerStyleManager * styleManager() const
Get access to the layer&#39;s style manager.
void projectChanged()
Emitted when the project changes.
void readFromLayer(QgsMapLayer *layer)
Store layer&#39;s active style information in the instance.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Stores style information (renderer, opacity, labeling, diagrams etc.) applicable to a map layer...
QList< QgsMapLayer *> masterVisibleLayers() const
Returns the master list of visible layers.
void mapThemeChanged(const QString &theme)
Emitted when a map theme changes definition.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
QList< QgsLayerTreeNode * > children()
Get list of children of the node. Children are owned by the parent.
QgsMapThemeCollection(QgsProject *project=nullptr)
Create map theme collection that handles themes of the given project.
QList< QgsMapLayer *> masterLayerOrder() const
Returns the master layer order (this will always match the project&#39;s QgsProject::layerOrder() )...
void removeMapTheme(const QString &name)
Remove 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.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
This class is a base class for nodes in a layer tree.
Reads and writes project states.
Definition: qgsproject.h:82
virtual bool legendSymbolItemChecked(const QString &key)
items of symbology items in legend is checked
QgsFeatureRenderer * renderer()
Return renderer.
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...
virtual void checkLegendSymbolItem(const QString &key, bool state=true)
item in symbology was checked
void insert(const QString &name, const QgsMapThemeCollection::MapThemeRecord &state)
Inserts a new map theme to the collection.
void setItemVisibilityCheckedParentRecursive(bool checked)
Check or uncheck a node and all its parents.
QgsLayerTree * layerTreeRoot() const
Return pointer to the root (invisible) node of the project&#39;s layer tree.
QgsMapLayer * layer() const
Get pointer to the associated map layer.
QgsMapLayer * layer() const
QString currentStyle() const
Return name of the current style.
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the registry.
QgsProject * project()
The QgsProject on which this map theme collection works.
virtual QVariant data(int role) const =0
Return data associated with the item. Must be implemented in derived class.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
QList< QgsLayerTreeModelLegendNode * > layerLegendNodes(QgsLayerTreeLayer *nodeLayer, bool skipNodeEmbeddedInParent=false)
Return filtered list of active legend nodes attached to a particular layer node (by default it return...
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
virtual bool legendSymbolItemsCheckable() const
items of symbology items in legend should be checkable
static QgsMapThemeCollection::MapThemeRecord createThemeFromCurrentState(QgsLayerTreeGroup *root, QgsLayerTreeModel *model)
Static method to create theme from the current state of layer visibilities in layer tree...
Management of styles for use with one map layer.
Represents a vector layer which manages a vector based data sets.
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.
void addLayerRecord(const QgsMapThemeCollection::MapThemeLayerRecord &record)
Add a new record for a layer.
bool setCurrentStyle(const QString &name)
Set a different style as the current style - will apply it to the layer.
bool usingLegendItems
Whether checkedLegendItems should be applied.
Layer tree node points to a map layer.