25 #include "qgssettings.h"
32 , mPendingCanvasUpdate( false )
33 , mAutoSetupOnFirstLayer( true )
34 , mHasLayersLoaded( !root->findLayers().isEmpty() )
47 QList<QgsMapLayer *> canvasLayers, overviewLayers, allLayerOrder;
52 for (
const QgsMapLayer *layer : customOrderLayers )
60 allLayerOrder << nodeLayer->
layer();
62 canvasLayers << nodeLayer->
layer();
63 if ( nodeLayer->
customProperty( QStringLiteral(
"overview" ), 0 ).toInt() )
64 overviewLayers << nodeLayer->
layer();
73 const QList<QgsLayerTreeLayer *> layerNodes = mRoot->
findLayers();
74 int currentSpatialLayerCount = 0;
75 int currentValidSpatialLayerCount = 0;
78 if ( layerNode->layer() && layerNode->layer()->isSpatial() )
80 currentSpatialLayerCount++;
81 if ( layerNode->layer()->isValid() )
82 currentValidSpatialLayerCount++;
86 bool firstLayers = mAutoSetupOnFirstLayer && !mHasLayersLoaded && currentSpatialLayerCount != 0;
87 bool firstValidLayers = mAutoSetupOnFirstLayer && !mHasValidLayersLoaded && currentValidSpatialLayerCount != 0;
90 if ( mOverviewCanvas )
91 mOverviewCanvas->
setLayers( overviewLayers );
93 if ( firstValidLayers )
104 if ( layerNode->layer() && layerNode->layer()->crs().isValid() )
106 mFirstCRS = layerNode->layer()->crs();
112 if ( mFirstCRS.
isValid() && firstLayers )
115 switch ( projectCrsBehavior )
119 const bool planimetric = QgsSettings().value( QStringLiteral(
"measure/planimetric" ),
true, QgsSettings::Core ).toBool();
130 mHasLayersLoaded = currentSpatialLayerCount;
131 mHasValidLayersLoaded = currentValidSpatialLayerCount;
132 if ( currentSpatialLayerCount == 0 )
135 mPendingCanvasUpdate =
false;
147 allLayers << nodeLayer->
layer();
149 canvasLayers << nodeLayer->
layer();
150 if ( nodeLayer->
customProperty( QStringLiteral(
"overview" ), 0 ).toInt() )
151 overviewLayers << nodeLayer->
layer();
155 const QList<QgsLayerTreeNode *> children = node->
children();
160 void QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers()
162 if ( mPendingCanvasUpdate )
165 mPendingCanvasUpdate =
true;
166 QMetaObject::invokeMethod(
this,
"setCanvasLayers", Qt::QueuedConnection );
169 void QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged()
171 deferredSetCanvasLayers();
174 void QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged(
QgsLayerTreeNode *node,
const QString &key )
177 if ( key == QLatin1String(
"overview" ) )
178 deferredSetCanvasLayers();
181 void QgsLayerTreeMapCanvasBridge::layersAdded(
const QList<QgsMapLayer *> &layers )
189 if ( l->isValid() && l->isSpatial() && mAutoSetupOnFirstLayer && !mHasValidLayersLoaded )
191 mHasValidLayersLoaded = true;
193 mCanvas->zoomToProjectExtent();
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
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.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
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.
This class is a 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)
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.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
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.
bool hasCustomLayerOrder() const
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
void layerOrderChanged()
Emitted when the layer order has changed.
QList< QgsMapLayer * > customLayerOrder() const
The order in which layers will be rendered on the canvas.
Map canvas is a class for displaying all GIS data types on a canvas.
void zoomToProjectExtent()
Zoom to the full extent the project associated with this canvas.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers that should be shown in the 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 setLayers(const QList< QgsMapLayer * > &layers)
updates layer set for overview
void setCrs(const QgsCoordinateReferenceSystem &crs, bool adjustEllipsoid=false)
Sets the project's native coordinate reference system.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void layersAdded(const QList< QgsMapLayer * > &layers)
Emitted when one or more layers were added to the registry.