QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgslayertreefiltersettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreefiltersettings.cpp
3 --------------------------------------
4 Date : March 2023
5 Copyright : (C) 2023 by Nyall Dawson
6 Email : nyall dot dawson 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#include "qgsmapsettings.h"
18#include "qgslayertreeutils.h"
19#include "qgslayertree.h"
23#include "qgslogger.h"
24
26 : mMapSettings( std::make_unique<QgsMapSettings>( settings ) )
27{
28 mLayers = _qgis_listRawToQPointer( mMapSettings->layers( true ) );
29}
30
32
34 : mLayerFilterExpressions( other.mLayerFilterExpressions )
35 , mMapSettings( other.mMapSettings ? new QgsMapSettings( *other.mMapSettings ) : nullptr )
36 , mFilterPolygon( other.mFilterPolygon )
37 , mFlags( other.mFlags )
38 , mLayers( other.mLayers )
39 , mLayerExtents( other.mLayerExtents )
40{
41
42}
43
45{
46 mLayerFilterExpressions = other.mLayerFilterExpressions;
47 mMapSettings.reset( other.mMapSettings ? new QgsMapSettings( *other.mMapSettings ) : nullptr );
48 mFilterPolygon = other.mFilterPolygon;
49 mFlags = other.mFlags;
50 mLayers = other.mLayers;
51 mLayerExtents = other.mLayerExtents;
52 return *this;
53}
54
56{
57 return *mMapSettings.get();
58}
59
61{
62 return mLayerFilterExpressions;
63}
64
65void QgsLayerTreeFilterSettings::setLayerFilterExpressions( const QMap<QString, QString> &expressions )
66{
67 mLayerFilterExpressions = expressions;
68}
69
71{
72 QMap<QString, QString> legendFilterExpressions;
73 const QList<QgsLayerTreeLayer *> layers = tree->findLayers();
74 for ( QgsLayerTreeLayer *nodeLayer : layers )
75 {
76 bool enabled = false;
77 const QString legendFilterExpression = QgsLayerTreeUtils::legendFilterByExpression( *nodeLayer, &enabled );
78 if ( enabled && !legendFilterExpression.isEmpty() )
79 {
80 legendFilterExpressions[nodeLayer->layerId()] = legendFilterExpression;
81 }
82 }
83 mLayerFilterExpressions = legendFilterExpressions;
84}
85
86QString QgsLayerTreeFilterSettings::layerFilterExpression( const QString &layerId ) const
87{
88 return mLayerFilterExpressions.value( layerId );
89}
90
92{
93 return mFilterPolygon;
94}
95
97{
98 mFilterPolygon = newFilterPolygon;
99}
100
101Qgis::LayerTreeFilterFlags QgsLayerTreeFilterSettings::flags() const
102{
103 return mFlags;
104}
105
106void QgsLayerTreeFilterSettings::setFlags( Qgis::LayerTreeFilterFlags flags )
107{
108 mFlags = flags;
109}
110
112{
113 const QgsCoordinateTransform polygonToLayerTransform( polygon.crs(), layer->crs(), mMapSettings->transformContext() );
114 try
115 {
116 QgsGeometry transformedPoly = polygon;
117 transformedPoly.transform( polygonToLayerTransform );
118 mLayerExtents[ layer->id() ].append( transformedPoly );
119 }
120 catch ( QgsCsException & )
121 {
122 QgsDebugError( QStringLiteral( "Error transforming polygon to layer CRS for legend filtering" ) );
123 }
124 if ( !mLayers.contains( layer ) )
125 mLayers << layer;
126}
127
129{
130 QVector< QgsGeometry > parts;
131
132 if ( mMapSettings->layerIds( true ).contains( layer->id() ) )
133 {
134 // add visible polygon in layer CRS
135 QgsGeometry mapExtent = mFilterPolygon;
136 if ( mapExtent.isEmpty() )
137 {
138 mapExtent = QgsGeometry::fromQPolygonF( mMapSettings->visiblePolygon() );
139 }
140
141 const QgsCoordinateTransform layerToMapTransform = mMapSettings->layerTransform( layer );
142 try
143 {
144 mapExtent.transform( layerToMapTransform, Qgis::TransformDirection::Reverse );
145 parts << mapExtent;
146 }
147 catch ( QgsCsException & )
148 {
149 QgsDebugError( QStringLiteral( "Error transforming map extent to layer CRS for legend filtering" ) );
150 }
151 }
152
153 auto additionalIt = mLayerExtents.constFind( layer->id() );
154 if ( additionalIt != mLayerExtents.constEnd() )
155 {
156 parts.append( additionalIt.value() );
157 }
158
159 if ( !parts.empty() )
160 return QgsGeometry::unaryUnion( parts );
161 else
162 return QgsGeometry();
163}
164
165QList<QgsMapLayer *> QgsLayerTreeFilterSettings::layers() const
166{
167 return _qgis_listQPointerToRaw( mLayers );
168}
@ Reverse
Reverse/inverse transform (from destination to source)
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
A geometry is the spatial representation of a feature.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters &parameters=QgsGeometryParameters())
Compute the unary union on a list of geometries.
Contains settings relating to filtering the contents of QgsLayerTreeModel and views.
QgsMapSettings & mapSettings()
Returns the map settings used to filter the legend content.
QgsGeometry filterPolygon() const
Returns the optional filter polygon, used when testing for symbols to show in the legend.
void addVisibleExtentForLayer(QgsMapLayer *layer, const QgsReferencedGeometry &polygon)
Adds a visible extent polygon for a map layer.
QString layerFilterExpression(const QString &layerId) const
Returns the filter expression to use for the layer with the specified layerId, or an empty string if ...
QList< QgsMapLayer * > layers() const
Returns the layers which should be shown in the legend.
Qgis::LayerTreeFilterFlags flags() const
Returns the filter flags.
QMap< QString, QString > layerFilterExpressions() const
Returns the map of layer IDs to legend filter expression.
void setFilterPolygon(const QgsGeometry &polygon)
Sets the optional filter polygon, used when testing for symbols to show in the legend.
void setLayerFilterExpressionsFromLayerTree(QgsLayerTree *tree)
Sets layer filter expressions using a layer tree.
QgsLayerTreeFilterSettings(const QgsMapSettings &settings)
Constructor for QgsLayerTreeFilterSettings, using the specified map settings.
void setFlags(Qgis::LayerTreeFilterFlags flags)
Sets the filter flags.
void setLayerFilterExpressions(const QMap< QString, QString > &expressions)
Sets the map of layer IDs to legend filter expression.
QgsLayerTreeFilterSettings & operator=(const QgsLayerTreeFilterSettings &other)
QgsGeometry combinedVisibleExtentForLayer(const QgsMapLayer *layer)
Returns the combined visible extent for a layer.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
Layer tree node points to a map layer.
static QString legendFilterByExpression(const QgsLayerTreeLayer &layer, bool *enabled=nullptr)
Returns the expression filter of a legend layer.
Namespace with helper functions for layer tree operations.
Base class for all map layer types.
Definition qgsmaplayer.h:74
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:80
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
The QgsMapSettings class contains configuration for rendering of the map.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsGeometry with associated coordinate reference system.
#define QgsDebugError(str)
Definition qgslogger.h:38