QGIS API Documentation 3.99.0-Master (d270888f95f)
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
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31 : mMapSettings( std::make_unique<QgsMapSettings>( settings ) )
32{
33 mLayers = _qgis_listRawToQPointer( mMapSettings->layers( true ) );
34}
35
37
39 : mLayerFilterExpressions( other.mLayerFilterExpressions )
40 , mMapSettings( other.mMapSettings ? new QgsMapSettings( *other.mMapSettings ) : nullptr )
41 , mFilterPolygon( other.mFilterPolygon )
42 , mFlags( other.mFlags )
43 , mLayers( other.mLayers )
44 , mLayerExtents( other.mLayerExtents )
45{
46
47}
48
50{
51 if ( &other == this )
52 return *this;
53
54 mLayerFilterExpressions = other.mLayerFilterExpressions;
55 mMapSettings.reset( other.mMapSettings ? new QgsMapSettings( *other.mMapSettings ) : nullptr );
56 mFilterPolygon = other.mFilterPolygon;
57 mFlags = other.mFlags;
58 mLayers = other.mLayers;
59 mLayerExtents = other.mLayerExtents;
60 return *this;
61}
62
64{
65 return *mMapSettings.get();
66}
67
69{
70 return mLayerFilterExpressions;
71}
72
73void QgsLayerTreeFilterSettings::setLayerFilterExpressions( const QMap<QString, QString> &expressions )
74{
75 mLayerFilterExpressions = expressions;
76}
77
79{
80 QMap<QString, QString> legendFilterExpressions;
81 const QList<QgsLayerTreeLayer *> layers = tree->findLayers();
82 for ( QgsLayerTreeLayer *nodeLayer : layers )
83 {
84 bool enabled = false;
85 const QString legendFilterExpression = QgsLayerTreeUtils::legendFilterByExpression( *nodeLayer, &enabled );
86 if ( enabled && !legendFilterExpression.isEmpty() )
87 {
88 legendFilterExpressions[nodeLayer->layerId()] = legendFilterExpression;
89 }
90 }
91 mLayerFilterExpressions = legendFilterExpressions;
92}
93
94QString QgsLayerTreeFilterSettings::layerFilterExpression( const QString &layerId ) const
95{
96 return mLayerFilterExpressions.value( layerId );
97}
98
100{
101 return mFilterPolygon;
102}
103
105{
106 mFilterPolygon = newFilterPolygon;
107}
108
113
118
120{
121 const QgsCoordinateTransform polygonToLayerTransform( polygon.crs(), layer->crs(), mMapSettings->transformContext() );
122 try
123 {
124 QgsGeometry transformedPoly = polygon;
125 transformedPoly.transform( polygonToLayerTransform );
126 mLayerExtents[ layer->id() ].append( transformedPoly );
127 }
128 catch ( QgsCsException & )
129 {
130 QgsDebugError( u"Error transforming polygon to layer CRS for legend filtering"_s );
131 }
132 if ( !mLayers.contains( layer ) )
133 mLayers << layer;
134}
135
137{
138 QVector< QgsGeometry > parts;
139
140 if ( mMapSettings->layerIds( true ).contains( layer->id() ) )
141 {
142 // add visible polygon in layer CRS
143 QgsGeometry mapExtent = mFilterPolygon;
144 if ( mapExtent.isEmpty() )
145 {
146 mapExtent = QgsGeometry::fromQPolygonF( mMapSettings->visiblePolygon() );
147 }
148
149 const QgsCoordinateTransform layerToMapTransform = mMapSettings->layerTransform( layer );
150 try
151 {
152 mapExtent.transform( layerToMapTransform, Qgis::TransformDirection::Reverse );
153 parts << mapExtent;
154 }
155 catch ( QgsCsException & )
156 {
157 QgsDebugError( u"Error transforming map extent to layer CRS for legend filtering"_s );
158 }
159 }
160
161 auto additionalIt = mLayerExtents.constFind( layer->id() );
162 if ( additionalIt != mLayerExtents.constEnd() )
163 {
164 parts.append( additionalIt.value() );
165 }
166
167 if ( !parts.empty() )
168 return QgsGeometry::unaryUnion( parts );
169 else
170 return QgsGeometry();
171}
172
173QList<QgsMapLayer *> QgsLayerTreeFilterSettings::layers() const
174{
175 return _qgis_listQPointerToRaw( mLayers );
176}
QFlags< LayerTreeFilterFlag > LayerTreeFilterFlags
Layer tree filter flags.
Definition qgis.h:4604
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2731
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:83
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
QString id
Definition qgsmaplayer.h:86
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:59