QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "qgslayertree.h"
19 #include "qgslayertreeutils.h"
20 #include "qgsmaplayer.h"
21 #include "qgsvectorlayer.h"
22 #include "qgsmapcanvas.h"
23 #include "qgsmapoverviewcanvas.h"
24 #include "qgsproject.h"
25 #include "qgssettings.h"
26 #include "qgsgui.h"
27 
29  : QObject( parent )
30  , mRoot( root )
31  , mCanvas( canvas )
32  , mPendingCanvasUpdate( false )
33  , mAutoSetupOnFirstLayer( true )
34  , mLastLayerCount( !root->findLayers().isEmpty() )
35 {
36  connect( root, &QgsLayerTreeGroup::customPropertyChanged, this, &QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged );
37  connect( root, &QgsLayerTreeNode::visibilityChanged, this, &QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged );
38  connect( root, &QgsLayerTree::layerOrderChanged, this, &QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers );
39 
41 }
42 
44 {
45  QList<QgsMapLayer *> canvasLayers, overviewLayers, allLayerOrder;
46 
47  if ( mRoot->hasCustomLayerOrder() )
48  {
49  Q_FOREACH ( QgsMapLayer *layer, mRoot->customLayerOrder() )
50  {
51  QgsLayerTreeLayer *nodeLayer = mRoot->findLayer( layer->id() );
52  if ( nodeLayer )
53  {
54  if ( !nodeLayer->layer()->isSpatial() )
55  continue;
56 
57  allLayerOrder << nodeLayer->layer();
58  if ( nodeLayer->isVisible() )
59  canvasLayers << nodeLayer->layer();
60  if ( nodeLayer->customProperty( QStringLiteral( "overview" ), 0 ).toInt() )
61  overviewLayers << nodeLayer->layer();
62  }
63  }
64  }
65  else
66  setCanvasLayers( mRoot, canvasLayers, overviewLayers, allLayerOrder );
67 
68  const QList<QgsLayerTreeLayer *> layerNodes = mRoot->findLayers();
69  int currentSpatialLayerCount = 0;
70  for ( QgsLayerTreeLayer *layerNode : layerNodes )
71  {
72  if ( layerNode->layer() && layerNode->layer()->isSpatial() )
73  currentSpatialLayerCount++;
74  }
75 
76  bool firstLayers = mAutoSetupOnFirstLayer && mLastLayerCount == 0 && currentSpatialLayerCount != 0;
77 
78  mCanvas->setLayers( canvasLayers );
79  if ( mOverviewCanvas )
80  mOverviewCanvas->setLayers( overviewLayers );
81 
82  if ( firstLayers )
83  {
84  // if we are moving from zero to non-zero layers, let's zoom to those data
85  mCanvas->zoomToFullExtent();
86  }
87 
88  if ( !mFirstCRS.isValid() )
89  {
90  // find out what is the first used CRS in case we may need to turn on OTF projections later
91  Q_FOREACH ( QgsLayerTreeLayer *layerNode, layerNodes )
92  {
93  if ( layerNode->layer() && layerNode->layer()->crs().isValid() )
94  {
95  mFirstCRS = layerNode->layer()->crs();
96  break;
97  }
98  }
99  }
100 
101  if ( mFirstCRS.isValid() && firstLayers )
102  {
103  const QgsGui::ProjectCrsBehavior projectCrsBehavior = QgsSettings().enumValue( QStringLiteral( "/projections/newProjectCrsBehavior" ), QgsGui::UseCrsOfFirstLayerAdded, QgsSettings::App );
104  switch ( projectCrsBehavior )
105  {
107  QgsProject::instance()->setCrs( mFirstCRS );
109  break;
110 
112  break;
113  }
114  }
115 
116  mLastLayerCount = currentSpatialLayerCount;
117  if ( currentSpatialLayerCount == 0 )
118  mFirstCRS = QgsCoordinateReferenceSystem();
119 
120  mPendingCanvasUpdate = false;
121 
122  emit canvasLayersChanged( canvasLayers );
123 }
124 
125 void QgsLayerTreeMapCanvasBridge::setCanvasLayers( QgsLayerTreeNode *node, QList<QgsMapLayer *> &canvasLayers, QList<QgsMapLayer *> &overviewLayers, QList<QgsMapLayer *> &allLayers )
126 {
127  if ( QgsLayerTree::isLayer( node ) )
128  {
129  QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
130  if ( nodeLayer->layer() && nodeLayer->layer()->isSpatial() )
131  {
132  allLayers << nodeLayer->layer();
133  if ( nodeLayer->isVisible() )
134  canvasLayers << nodeLayer->layer();
135  if ( nodeLayer->customProperty( QStringLiteral( "overview" ), 0 ).toInt() )
136  overviewLayers << nodeLayer->layer();
137  }
138  }
139 
140  Q_FOREACH ( QgsLayerTreeNode *child, node->children() )
141  setCanvasLayers( child, canvasLayers, overviewLayers, allLayers );
142 }
143 
144 void QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers()
145 {
146  if ( mPendingCanvasUpdate )
147  return;
148 
149  mPendingCanvasUpdate = true;
150  QMetaObject::invokeMethod( this, "setCanvasLayers", Qt::QueuedConnection );
151 }
152 
153 void QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged()
154 {
155  deferredSetCanvasLayers();
156 }
157 
158 void QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key )
159 {
160  Q_UNUSED( node );
161  if ( key == QLatin1String( "overview" ) )
162  deferredSetCanvasLayers();
163 }
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:75
Base class for all map layer types.
Definition: qgsmaplayer.h:63
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
Set the project CRS to the CRS of the first layer added to a new project.
Definition: qgsgui.h:55
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...
QgsMapLayer * layer() const
Returns the map layer associated with this node.
void layerOrderChanged()
Emitted when the layer order has changed.
Q_INVOKABLE void setCanvasLayers()
force update of canvas layers from the layer tree. Normally this should not be needed to be called...
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers that should be shown in the canvas.
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.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the project&#39;s native coordinate reference system.
Definition: qgsproject.cpp:622
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
This class is a base class for nodes in a layer tree.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
void setEllipsoid(const QString &ellipsoid)
Sets the project&#39;s ellipsoid from a proj string representation, e.g., "WGS84".
Definition: qgsproject.cpp:641
bool hasCustomLayerOrder() const
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
QString ellipsoidAcronym() const
Returns the ellipsoid acronym for the ellipsoid used by the CRS.
void setLayers(const QList< QgsMapLayer * > &layers)
updates layer set for overview
QList< QgsMapLayer * > customLayerOrder() const
The order in which layers will be rendered on the canvas.
void canvasLayersChanged(const QList< QgsMapLayer * > &layers)
Emitted when the set of layers (or order of layers) visible in the canvas changes.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QgsLayerTreeMapCanvasBridge(QgsLayerTree *root, QgsMapCanvas *canvas, QObject *parent=nullptr)
Constructor: does not take ownership of the layer tree nor canvas.
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:411
void zoomToFullExtent()
Zoom to the full extent of all layers.
This class represents a coordinate reference system (CRS).
Always set new projects to use a preset default CRS.
Definition: qgsgui.h:56
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
Definition: qgssettings.h:235
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
ProjectCrsBehavior
Defines the behavior to use when setting the CRS for a newly created project.
Definition: qgsgui.h:53
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:70
Layer tree node points to a map layer.