QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
48{
49 if ( &other == this )
50 return *this;
51
52 mLayerFilterExpressions = other.mLayerFilterExpressions;
53 mMapSettings.reset( other.mMapSettings ? new QgsMapSettings( *other.mMapSettings ) : nullptr );
54 mFilterPolygon = other.mFilterPolygon;
55 mFlags = other.mFlags;
56 mLayers = other.mLayers;
57 mLayerExtents = other.mLayerExtents;
58 return *this;
59}
60
62{
63 return *mMapSettings.get();
64}
65
67{
68 return mLayerFilterExpressions;
69}
70
71void QgsLayerTreeFilterSettings::setLayerFilterExpressions( const QMap<QString, QString> &expressions )
72{
73 mLayerFilterExpressions = expressions;
74}
75
77{
78 QMap<QString, QString> legendFilterExpressions;
79 const QList<QgsLayerTreeLayer *> layers = tree->findLayers();
80 for ( QgsLayerTreeLayer *nodeLayer : layers )
81 {
82 bool enabled = false;
83 const QString legendFilterExpression = QgsLayerTreeUtils::legendFilterByExpression( *nodeLayer, &enabled );
84 if ( enabled && !legendFilterExpression.isEmpty() )
85 {
86 legendFilterExpressions[nodeLayer->layerId()] = legendFilterExpression;
87 }
88 }
89 mLayerFilterExpressions = legendFilterExpressions;
90}
91
92QString QgsLayerTreeFilterSettings::layerFilterExpression( const QString &layerId ) const
93{
94 return mLayerFilterExpressions.value( layerId );
95}
96
98{
99 return mFilterPolygon;
100}
101
103{
104 mFilterPolygon = newFilterPolygon;
105}
106
111
116
118{
119 const QgsCoordinateTransform polygonToLayerTransform( polygon.crs(), layer->crs(), mMapSettings->transformContext() );
120 try
121 {
122 QgsGeometry transformedPoly = polygon;
123 transformedPoly.transform( polygonToLayerTransform );
124 mLayerExtents[layer->id()].append( transformedPoly );
125 }
126 catch ( QgsCsException & )
127 {
128 QgsDebugError( u"Error transforming polygon to layer CRS for legend filtering"_s );
129 }
130 if ( !mLayers.contains( layer ) )
131 mLayers << layer;
132}
133
135{
136 QVector< QgsGeometry > parts;
137
138 if ( mMapSettings->layerIds( true ).contains( layer->id() ) )
139 {
140 // add visible polygon in layer CRS
141 QgsGeometry mapExtent = mFilterPolygon;
142 if ( mapExtent.isEmpty() )
143 {
144 mapExtent = QgsGeometry::fromQPolygonF( mMapSettings->visiblePolygon() );
145 }
146
147 const QgsCoordinateTransform layerToMapTransform = mMapSettings->layerTransform( layer );
148 try
149 {
150 mapExtent.transform( layerToMapTransform, Qgis::TransformDirection::Reverse );
151 parts << mapExtent;
152 }
153 catch ( QgsCsException & )
154 {
155 QgsDebugError( u"Error transforming map extent to layer CRS for legend filtering"_s );
156 }
157 }
158
159 auto additionalIt = mLayerExtents.constFind( layer->id() );
160 if ( additionalIt != mLayerExtents.constEnd() )
161 {
162 parts.append( additionalIt.value() );
163 }
164
165 if ( !parts.empty() )
166 return QgsGeometry::unaryUnion( parts );
167 else
168 return QgsGeometry();
169}
170
171QList<QgsMapLayer *> QgsLayerTreeFilterSettings::layers() const
172{
173 return _qgis_listQPointerToRaw( mLayers );
174}
QFlags< LayerTreeFilterFlag > LayerTreeFilterFlags
Layer tree filter flags.
Definition qgis.h:4689
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2766
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