QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
19#include "qgslayertree.h"
20#include "qgslayertreeutils.h"
21#include "qgslogger.h"
23#include "qgsmapsettings.h"
25
27 : mMapSettings( std::make_unique<QgsMapSettings>( settings ) )
28{
29 mLayers = _qgis_listRawToQPointer( mMapSettings->layers( true ) );
30}
31
33
35 : mLayerFilterExpressions( other.mLayerFilterExpressions )
36 , mMapSettings( other.mMapSettings ? new QgsMapSettings( *other.mMapSettings ) : nullptr )
37 , mFilterPolygon( other.mFilterPolygon )
38 , mFlags( other.mFlags )
39 , mLayers( other.mLayers )
40 , mLayerExtents( other.mLayerExtents )
41{
42
43}
44
46{
47 if ( &other == this )
48 return *this;
49
50 mLayerFilterExpressions = other.mLayerFilterExpressions;
51 mMapSettings.reset( other.mMapSettings ? new QgsMapSettings( *other.mMapSettings ) : nullptr );
52 mFilterPolygon = other.mFilterPolygon;
53 mFlags = other.mFlags;
54 mLayers = other.mLayers;
55 mLayerExtents = other.mLayerExtents;
56 return *this;
57}
58
60{
61 return *mMapSettings.get();
62}
63
65{
66 return mLayerFilterExpressions;
67}
68
69void QgsLayerTreeFilterSettings::setLayerFilterExpressions( const QMap<QString, QString> &expressions )
70{
71 mLayerFilterExpressions = expressions;
72}
73
75{
76 QMap<QString, QString> legendFilterExpressions;
77 const QList<QgsLayerTreeLayer *> layers = tree->findLayers();
78 for ( QgsLayerTreeLayer *nodeLayer : layers )
79 {
80 bool enabled = false;
81 const QString legendFilterExpression = QgsLayerTreeUtils::legendFilterByExpression( *nodeLayer, &enabled );
82 if ( enabled && !legendFilterExpression.isEmpty() )
83 {
84 legendFilterExpressions[nodeLayer->layerId()] = legendFilterExpression;
85 }
86 }
87 mLayerFilterExpressions = legendFilterExpressions;
88}
89
90QString QgsLayerTreeFilterSettings::layerFilterExpression( const QString &layerId ) const
91{
92 return mLayerFilterExpressions.value( layerId );
93}
94
96{
97 return mFilterPolygon;
98}
99
101{
102 mFilterPolygon = newFilterPolygon;
103}
104
109
114
116{
117 const QgsCoordinateTransform polygonToLayerTransform( polygon.crs(), layer->crs(), mMapSettings->transformContext() );
118 try
119 {
120 QgsGeometry transformedPoly = polygon;
121 transformedPoly.transform( polygonToLayerTransform );
122 mLayerExtents[ layer->id() ].append( transformedPoly );
123 }
124 catch ( QgsCsException & )
125 {
126 QgsDebugError( QStringLiteral( "Error transforming polygon to layer CRS for legend filtering" ) );
127 }
128 if ( !mLayers.contains( layer ) )
129 mLayers << layer;
130}
131
133{
134 QVector< QgsGeometry > parts;
135
136 if ( mMapSettings->layerIds( true ).contains( layer->id() ) )
137 {
138 // add visible polygon in layer CRS
139 QgsGeometry mapExtent = mFilterPolygon;
140 if ( mapExtent.isEmpty() )
141 {
142 mapExtent = QgsGeometry::fromQPolygonF( mMapSettings->visiblePolygon() );
143 }
144
145 const QgsCoordinateTransform layerToMapTransform = mMapSettings->layerTransform( layer );
146 try
147 {
148 mapExtent.transform( layerToMapTransform, Qgis::TransformDirection::Reverse );
149 parts << mapExtent;
150 }
151 catch ( QgsCsException & )
152 {
153 QgsDebugError( QStringLiteral( "Error transforming map extent to layer CRS for legend filtering" ) );
154 }
155 }
156
157 auto additionalIt = mLayerExtents.constFind( layer->id() );
158 if ( additionalIt != mLayerExtents.constEnd() )
159 {
160 parts.append( additionalIt.value() );
161 }
162
163 if ( !parts.empty() )
164 return QgsGeometry::unaryUnion( parts );
165 else
166 return QgsGeometry();
167}
168
169QList<QgsMapLayer *> QgsLayerTreeFilterSettings::layers() const
170{
171 return _qgis_listQPointerToRaw( mLayers );
172}
QFlags< LayerTreeFilterFlag > LayerTreeFilterFlags
Layer tree filter flags.
Definition qgis.h:4545
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2673
Handles coordinate transforms between two 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.
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:80
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:87
QString id
Definition qgsmaplayer.h:83
Contains configuration for rendering maps.
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:57