QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgslayertreemapcanvasbridge.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayertreemapcanvasbridge.cpp
3 --------------------------------------
4 Date : May 2014
5 Copyright : (C) 2014 by Martin Dobias
6 Email : wonder dot sk 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
18#include "qgsgui.h"
19#include "qgslayertree.h"
20#include "qgslayertreeutils.h"
21#include "qgsmapcanvas.h"
22#include "qgsmaplayer.h"
24#include "qgsproject.h"
25#include "qgssettings.h"
26#include "qgsvectorlayer.h"
27
28#include <QString>
29
30#include "moc_qgslayertreemapcanvasbridge.cpp"
31
32using namespace Qt::StringLiterals;
33
35 : QObject( parent )
36 , mRoot( root )
37 , mCanvas( canvas )
38 , mHasLayersLoaded( !root->findLayers().isEmpty() )
39{
40 connect( root, &QgsLayerTreeGroup::customPropertyChanged, this, &QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged );
41 connect( root, &QgsLayerTreeNode::visibilityChanged, this, &QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged );
42 connect( root, &QgsLayerTree::layerOrderChanged, this, &QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers );
43
44 connect( QgsProject::instance(), &QgsProject::layersAdded, this, &QgsLayerTreeMapCanvasBridge::layersAdded );
45
47}
48
50{
51 QList<QgsMapLayer *> canvasLayers, overviewLayers, allLayerOrder;
52
53 if ( mRoot->hasCustomLayerOrder() )
54 {
55 const QList<QgsMapLayer *> customOrderLayers = mRoot->customLayerOrder();
56 for ( const QgsMapLayer *layer : customOrderLayers )
57 {
58 QgsLayerTreeLayer *nodeLayer = mRoot->findLayer( layer->id() );
59 if ( nodeLayer )
60 {
61 if ( !nodeLayer->layer()->isSpatial() )
62 continue;
63
64 allLayerOrder << nodeLayer->layer();
65 if ( nodeLayer->isVisible() )
66 canvasLayers << nodeLayer->layer();
67 if ( nodeLayer->customProperty( u"overview"_s, 0 ).toInt() )
68 overviewLayers << nodeLayer->layer();
69 }
70 }
71 }
72 else
73 {
74 setCanvasLayers( mRoot, canvasLayers, overviewLayers, allLayerOrder );
75 }
76
77 const QList<QgsLayerTreeLayer *> layerNodes = mRoot->findLayers();
78 int currentSpatialLayerCount = 0;
79 int currentValidSpatialLayerCount = 0;
80 for ( QgsLayerTreeLayer *layerNode : layerNodes )
81 {
82 if ( layerNode->layer() && layerNode->layer()->isSpatial() )
83 {
84 currentSpatialLayerCount++;
85 if ( layerNode->layer()->isValid() )
86 currentValidSpatialLayerCount++;
87 }
88 }
89
90 const bool firstLayers = mAutoSetupOnFirstLayer && !mHasLayersLoaded && currentSpatialLayerCount != 0;
91 const bool firstValidLayers = mAutoSetupOnFirstLayer && !mHasValidLayersLoaded && currentValidSpatialLayerCount != 0;
92
93 mCanvas->setLayers( canvasLayers );
94 if ( mOverviewCanvas )
95 mOverviewCanvas->setLayers( overviewLayers );
96
97 if ( firstValidLayers )
98 {
99 // if we are moving from zero to non-zero layers, let's zoom to those data (only consider valid layers here!)
100 mCanvas->zoomToProjectExtent();
101 }
102
103 if ( !mFirstCRS.isValid() )
104 {
105 // find out what is the first used CRS in case we may need to turn on OTF projections later
106 for ( const QgsLayerTreeLayer *layerNode : layerNodes )
107 {
108 if ( layerNode->layer() && layerNode->layer()->crs().isValid() )
109 {
110 mFirstCRS = layerNode->layer()->crs();
111 break;
112 }
113 }
114 }
115
116 if ( mFirstCRS.isValid() && firstLayers )
117 {
118 const QgsGui::ProjectCrsBehavior projectCrsBehavior = QgsSettings().enumValue( u"/projections/newProjectCrsBehavior"_s, QgsGui::UseCrsOfFirstLayerAdded, QgsSettings::App );
119 switch ( projectCrsBehavior )
120 {
122 {
123 const bool planimetric = QgsSettings().value( u"measure/planimetric"_s, true, QgsSettings::Core ).toBool();
124 // Only adjust ellipsoid to CRS if it's not set to planimetric
125 QgsProject::instance()->setCrs( mFirstCRS.horizontalCrs(), !planimetric );
126 const QgsCoordinateReferenceSystem vertCrs = mFirstCRS.verticalCrs();
127 if ( vertCrs.isValid() )
128 {
130 }
131 break;
132 }
133
135 break;
136 }
137 }
138
139 mHasLayersLoaded = currentSpatialLayerCount;
140 mHasValidLayersLoaded = currentValidSpatialLayerCount;
141 if ( currentSpatialLayerCount == 0 )
142 mFirstCRS = QgsCoordinateReferenceSystem();
143
144 mPendingCanvasUpdate = false;
145
146 emit canvasLayersChanged( canvasLayers );
147}
148
149void QgsLayerTreeMapCanvasBridge::setCanvasLayers( QgsLayerTreeNode *node, QList<QgsMapLayer *> &canvasLayers, QList<QgsMapLayer *> &overviewLayers, QList<QgsMapLayer *> &allLayers )
150{
151 if ( QgsLayerTree::isLayer( node ) )
152 {
153 QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
154 if ( nodeLayer->layer() && nodeLayer->layer()->isSpatial() )
155 {
156 allLayers << nodeLayer->layer();
157 if ( nodeLayer->isVisible() )
158 canvasLayers << nodeLayer->layer();
159 if ( nodeLayer->customProperty( u"overview"_s, 0 ).toInt() )
160 overviewLayers << nodeLayer->layer();
161 }
162 }
163
164 const QList<QgsLayerTreeNode *> children = node->children();
165 for ( QgsLayerTreeNode *child : children )
166 {
167 if ( QgsLayerTree::isGroup( child ) )
168 {
169 if ( QgsGroupLayer *groupLayer = QgsLayerTree::toGroup( child )->groupLayer() )
170 {
171 if ( child->isVisible() )
172 canvasLayers << groupLayer;
173 continue;
174 }
175 }
176 setCanvasLayers( child, canvasLayers, overviewLayers, allLayers );
177 }
178}
179
180void QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers()
181{
182 if ( mPendingCanvasUpdate )
183 return;
184
185 mPendingCanvasUpdate = true;
186 QMetaObject::invokeMethod( this, "setCanvasLayers", Qt::QueuedConnection );
187}
188
189void QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged()
190{
191 deferredSetCanvasLayers();
192}
193
194void QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key )
195{
196 Q_UNUSED( node )
197 if ( key == "overview"_L1 )
198 deferredSetCanvasLayers();
199}
200
201void QgsLayerTreeMapCanvasBridge::layersAdded( const QList<QgsMapLayer *> &layers )
202{
203 for ( QgsMapLayer *l : layers )
204 {
205 if ( l )
206 {
207 connect( l, &QgsMapLayer::dataSourceChanged, this, [this, l] {
208 if ( l->isValid() && l->isSpatial() && mAutoSetupOnFirstLayer && !mHasValidLayersLoaded )
209 {
210 mHasValidLayersLoaded = true;
211 // if we are moving from zero valid layers to non-zero VALID layers, let's zoom to those data
212 mCanvas->zoomToProjectExtent();
213 }
214 deferredSetCanvasLayers();
215 } );
216 }
217 }
218}
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QgsCoordinateReferenceSystem verticalCrs() const
Returns the vertical CRS associated with this CRS object.
ProjectCrsBehavior
Defines the behavior to use when setting the CRS for a newly created project.
Definition qgsgui.h:82
@ UseCrsOfFirstLayerAdded
Set the project CRS to the CRS of the first layer added to a new project.
Definition qgsgui.h:83
@ UsePresetCrs
Always set new projects to use a preset default CRS.
Definition qgsgui.h:84
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Q_INVOKABLE void setCanvasLayers()
force update of canvas layers from the layer tree. Normally this should not be needed to be called.
void canvasLayersChanged(const QList< QgsMapLayer * > &layers)
Emitted when the set of layers (or order of layers) visible in the canvas changes.
QgsLayerTreeMapCanvasBridge(QgsLayerTree *root, QgsMapCanvas *canvas, QObject *parent SIP_TRANSFERTHIS=nullptr)
Constructor: does not take ownership of the layer tree nor canvas.
Base class for nodes in a layer tree.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well).
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file.
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
Namespace with helper functions for layer tree operations.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
void layerOrderChanged()
Emitted when the layer order has changed.
Map canvas is a class for displaying all GIS data types on a canvas.
Base class for all map layer types.
Definition qgsmaplayer.h:83
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
void dataSourceChanged()
Emitted whenever the layer's data source has been changed.
void setCrs(const QgsCoordinateReferenceSystem &crs, bool adjustEllipsoid=false)
Sets the project's native coordinate reference system.
static QgsProject * instance()
Returns the QgsProject singleton instance.
bool setVerticalCrs(const QgsCoordinateReferenceSystem &crs, QString *errorMessage=nullptr)
Sets the project's vertical coordinate reference system.
void layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the registry.
Stores settings for use within QGIS.
Definition qgssettings.h:68
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.