27 #include <QInputDialog> 42 bool hasCheckableItems =
false;
43 bool someItemsUnchecked =
false;
44 QSet<QString> checkedItems;
47 if ( legendNode->
flags() & Qt::ItemIsUserCheckable )
49 hasCheckableItems =
true;
51 if ( legendNode->
data( Qt::CheckStateRole ).toInt() == Qt::Checked )
54 someItemsUnchecked =
true;
58 if ( hasCheckableItems && someItemsUnchecked )
60 layerRec.usingLegendItems =
true;
61 layerRec.checkedLegendItems = checkedItems;
76 rec.mLayerRecords << createThemeLayerRecord( nodeLayer, model );
92 if ( lr.
layer() == layer )
104 bool isVisible = findRecordForLayer( nodeLayer->
layer(), rec, layerRec );
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 );
138 if ( ( legendNode->
flags() & Qt::ItemIsUserCheckable ) &&
139 legendNode->
data( Qt::CheckStateRole ).toInt() != Qt::Checked )
140 legendNode->
setData( Qt::Checked, Qt::CheckStateRole );
173 if ( project == mProject )
185 return QList< QgsMapLayer * >();
195 if ( allLayers.isEmpty() )
198 return visibleLayers;
202 QList< QgsMapLayer * > orderedVisibleLayers;
205 if ( visibleLayers.contains( layer ) )
206 orderedVisibleLayers << layer;
208 return orderedVisibleLayers;
215 return mMapThemes.contains( name );
220 mMapThemes.insert( name, state );
222 reconnectToLayersStyleManager();
229 if ( !mMapThemes.contains( name ) )
232 mMapThemes[name] = state;
234 reconnectToLayersStyleManager();
241 if ( !mMapThemes.contains( name ) )
244 mMapThemes.remove( name );
246 reconnectToLayersStyleManager();
254 reconnectToLayersStyleManager();
260 return mMapThemes.keys();
265 QStringList layerIds;
268 layerIds << layer->
id();
275 QList<QgsMapLayer *> layers;
276 const QList<MapThemeLayerRecord> &recs = mMapThemes.value( name ).mLayerRecords;
278 if ( layerOrder.isEmpty() )
283 if ( layerRec.
layer() )
284 layers << layerRec.
layer();
293 if ( layerRec.
layer() == layer )
294 layers << layerRec.
layer();
306 if ( !vlayer || !vlayer->
renderer() )
319 if ( checked != shouldBeChecked )
327 QMap<QString, QString> styleOverrides;
328 if ( !mMapThemes.contains( presetName ) )
329 return styleOverrides;
331 Q_FOREACH (
const MapThemeLayerRecord &layerRec, mMapThemes.value( presetName ).mLayerRecords )
333 if ( !layerRec.
layer() )
342 applyMapThemeCheckedLegendNodesToLayer( layerRec, layer );
347 styleOverrides[layer->
id()] = layerStyle.
xmlData();
352 return styleOverrides;
355 void QgsMapThemeCollection::reconnectToLayersStyleManager()
359 QSet<QgsMapLayer *> layers;
364 if ( layerRec.
layer() )
365 layers << layerRec.
layer();
379 QDomElement visPresetsElem = doc.firstChildElement( QStringLiteral(
"qgis" ) ).firstChildElement( QStringLiteral(
"visibility-presets" ) );
380 if ( visPresetsElem.isNull() )
383 QDomElement visPresetElem = visPresetsElem.firstChildElement( QStringLiteral(
"visibility-preset" ) );
384 while ( !visPresetElem.isNull() )
386 QHash<QString, MapThemeLayerRecord> layerRecords;
388 QString presetName = visPresetElem.attribute( QStringLiteral(
"name" ) );
389 QDomElement visPresetLayerElem = visPresetElem.firstChildElement( QStringLiteral(
"layer" ) );
390 while ( !visPresetLayerElem.isNull() )
392 QString layerID = visPresetLayerElem.attribute( QStringLiteral(
"id" ) );
397 if ( visPresetLayerElem.hasAttribute( QStringLiteral(
"style" ) ) )
399 layerRecords[layerID].usingCurrentStyle =
true;
400 layerRecords[layerID].currentStyle = visPresetLayerElem.attribute( QStringLiteral(
"style" ) );
403 visPresetLayerElem = visPresetLayerElem.nextSiblingElement( QStringLiteral(
"layer" ) );
406 QDomElement checkedLegendNodesElem = visPresetElem.firstChildElement( QStringLiteral(
"checked-legend-nodes" ) );
407 while ( !checkedLegendNodesElem.isNull() )
409 QSet<QString> checkedLegendNodes;
411 QDomElement checkedLegendNodeElem = checkedLegendNodesElem.firstChildElement( QStringLiteral(
"checked-legend-node" ) );
412 while ( !checkedLegendNodeElem.isNull() )
414 checkedLegendNodes << checkedLegendNodeElem.attribute( QStringLiteral(
"id" ) );
415 checkedLegendNodeElem = checkedLegendNodeElem.nextSiblingElement( QStringLiteral(
"checked-legend-node" ) );
418 QString layerID = checkedLegendNodesElem.attribute( QStringLiteral(
"id" ) );
419 if ( mProject->
mapLayer( layerID ) )
421 layerRecords[layerID].usingLegendItems =
true;
422 layerRecords[layerID].checkedLegendItems = checkedLegendNodes;
424 checkedLegendNodesElem = checkedLegendNodesElem.nextSiblingElement( QStringLiteral(
"checked-legend-nodes" ) );
429 mMapThemes.insert( presetName, rec );
432 visPresetElem = visPresetElem.nextSiblingElement( QStringLiteral(
"visibility-preset" ) );
435 reconnectToLayersStyleManager();
441 QDomElement visPresetsElem = doc.createElement( QStringLiteral(
"visibility-presets" ) );
442 MapThemeRecordMap::const_iterator it = mMapThemes.constBegin();
443 for ( ; it != mMapThemes.constEnd(); ++ it )
445 QString grpName = it.key();
447 QDomElement visPresetElem = doc.createElement( QStringLiteral(
"visibility-preset" ) );
448 visPresetElem.setAttribute( QStringLiteral(
"name" ), grpName );
451 if ( !layerRec.
layer() )
453 QString layerID = layerRec.
layer()->
id();
454 QDomElement layerElem = doc.createElement( QStringLiteral(
"layer" ) );
455 layerElem.setAttribute( QStringLiteral(
"id" ), layerID );
457 layerElem.setAttribute( QStringLiteral(
"style" ), layerRec.
currentStyle );
458 visPresetElem.appendChild( layerElem );
462 QDomElement checkedLegendNodesElem = doc.createElement( QStringLiteral(
"checked-legend-nodes" ) );
463 checkedLegendNodesElem.setAttribute( QStringLiteral(
"id" ), layerID );
466 QDomElement checkedLegendNodeElem = doc.createElement( QStringLiteral(
"checked-legend-node" ) );
467 checkedLegendNodeElem.setAttribute( QStringLiteral(
"id" ), checkedLegendNode );
468 checkedLegendNodesElem.appendChild( checkedLegendNodeElem );
470 visPresetElem.appendChild( checkedLegendNodesElem );
474 visPresetsElem.appendChild( visPresetElem );
477 doc.firstChildElement( QStringLiteral(
"qgis" ) ).appendChild( visPresetsElem );
480 void QgsMapThemeCollection::registryLayersRemoved(
const QStringList &layerIDs )
484 QSet< QString > changedThemes;
485 MapThemeRecordMap::iterator it = mMapThemes.begin();
486 for ( ; it != mMapThemes.end(); ++it )
489 for (
int i = 0; i < rec.mLayerRecords.count(); ++i )
492 if ( layerRec.
layer() && layerIDs.contains( layerRec.
layer()->
id() ) )
494 rec.mLayerRecords.removeAt( i-- );
495 changedThemes << it.key();
500 Q_FOREACH (
const QString &theme, changedThemes )
507 void QgsMapThemeCollection::layerStyleRenamed(
const QString &oldName,
const QString &newName )
513 QSet< QString > changedThemes;
515 MapThemeRecordMap::iterator it = mMapThemes.begin();
516 for ( ; it != mMapThemes.end(); ++it )
519 for (
int i = 0; i < rec.mLayerRecords.count(); ++i )
527 changedThemes << it.key();
532 Q_FOREACH (
const QString &theme, changedThemes )
541 for (
int i = 0; i < mLayerRecords.length(); ++i )
543 if ( mLayerRecords.at( i ).layer() == layer )
544 mLayerRecords.removeAt( i );
550 mLayerRecords.append( record );
555 QHash<QgsMapLayer *, MapThemeLayerRecord> validSet;
558 if ( layerRec.
layer() )
559 validSet.insert( layerRec.
layer(), layerRec );
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.
QgsMapLayer * layer() const
Returns map layer or null if the layer does not exist anymore.
Base class for all map layer types.
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.
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.
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's style manager.
void projectChanged()
Emitted when the project changes.
void readFromLayer(QgsMapLayer *layer)
Store layer'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'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.
Rule key of the node (QString)
QList< QgsMapLayer *> masterLayerOrder() const
Returns the master layer order (this will always match the project'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.
This class is a base class for nodes in a layer tree.
Reads and writes project states.
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'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.