32#include "moc_qgslayertreemapcanvasbridge.cpp"
34using namespace Qt::StringLiterals;
40 , mHasLayersLoaded( !root->findLayers().isEmpty() )
53 QList<QgsMapLayer *> canvasLayers, overviewLayers, allLayerOrder;
55 if ( mRoot->hasCustomLayerOrder() )
57 const QList<QgsMapLayer *> customOrderLayers = mRoot->customLayerOrder();
58 for (
const QgsMapLayer *layer : customOrderLayers )
66 allLayerOrder << nodeLayer->
layer();
68 canvasLayers << nodeLayer->
layer();
70 overviewLayers << nodeLayer->
layer();
79 const QList<QgsLayerTreeLayer *> layerNodes = mRoot->findLayers();
80 int currentSpatialLayerCount = 0;
81 int currentValidSpatialLayerCount = 0;
84 if ( layerNode->layer() && layerNode->layer()->isSpatial() )
86 currentSpatialLayerCount++;
87 if ( layerNode->layer()->isValid() )
88 currentValidSpatialLayerCount++;
92 const bool firstLayers = mAutoSetupOnFirstLayer && !mHasLayersLoaded && currentSpatialLayerCount != 0;
93 const bool firstValidLayers = mAutoSetupOnFirstLayer && !mHasValidLayersLoaded && currentValidSpatialLayerCount != 0;
95 mCanvas->setLayers( canvasLayers );
96 if ( mOverviewCanvas )
97 mOverviewCanvas->setLayers( overviewLayers );
99 if ( !mFirstCRS.isValid() )
104 if ( layerNode->layer() && layerNode->layer()->crs().isValid() )
106 mFirstCRS = layerNode->layer()->crs();
112 if ( mFirstCRS.isValid() && firstLayers )
115 switch ( projectCrsBehavior )
132 if ( firstValidLayers )
135 mCanvas->zoomToProjectExtent();
138 mHasLayersLoaded = currentSpatialLayerCount;
139 mHasValidLayersLoaded = currentValidSpatialLayerCount;
140 if ( currentSpatialLayerCount == 0 )
143 mPendingCanvasUpdate =
false;
155 allLayers << nodeLayer->
layer();
157 canvasLayers << nodeLayer->
layer();
159 overviewLayers << nodeLayer->
layer();
163 const QList<QgsLayerTreeNode *> children = node->
children();
164 for ( QgsLayerTreeNode *child : children )
170 if ( child->isVisible() )
171 canvasLayers << groupLayer;
179void QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers()
181 if ( mPendingCanvasUpdate )
184 mPendingCanvasUpdate =
true;
185 QMetaObject::invokeMethod(
this,
"setCanvasLayers", Qt::QueuedConnection );
188void QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged()
190 deferredSetCanvasLayers();
193void QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged(
QgsLayerTreeNode *node,
const QString &key )
196 if ( key ==
"overview"_L1 )
197 deferredSetCanvasLayers();
200void QgsLayerTreeMapCanvasBridge::layersAdded(
const QList<QgsMapLayer *> &layers )
202 for ( QgsMapLayer *l : layers )
207 if ( l->isValid() && l->isSpatial() && mAutoSetupOnFirstLayer && !mHasValidLayersLoaded )
209 mHasValidLayersLoaded =
true;
211 mCanvas->zoomToProjectExtent();
213 deferredSetCanvasLayers();
Represents a coordinate reference system (CRS).
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.
@ UseCrsOfFirstLayerAdded
Set the project CRS to the CRS of the first layer added to a new project.
@ UsePresetCrs
Always set new projects to use a preset default CRS.
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.
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.
static const QgsSettingsEntryBool * settingsMeasurePlanimetric
Settings entry for whether measurements should be planimetric (ellipsoid off) or use the ellipsoid.
Stores settings for use within QGIS.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.