QGIS API Documentation  2.99.0-Master (0cba29c)
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 
27  : QObject( parent )
28  , mRoot( root )
29  , mCanvas( canvas )
30  , mOverviewCanvas( nullptr )
31  , mPendingCanvasUpdate( false )
32  , mAutoSetupOnFirstLayer( true )
33  , mLastLayerCount( !root->findLayers().isEmpty() )
34 {
35  connect( root, &QgsLayerTreeGroup::customPropertyChanged, this, &QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged );
36  connect( root, &QgsLayerTreeNode::visibilityChanged, this, &QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged );
37  connect( root, &QgsLayerTree::layerOrderChanged, this, &QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers );
38 
40 }
41 
43 {
44  QList<QgsMapLayer *> canvasLayers, overviewLayers, allLayerOrder;
45 
46  if ( mRoot->hasCustomLayerOrder() )
47  {
48  Q_FOREACH ( QgsMapLayer *layer, mRoot->customLayerOrder() )
49  {
50  QgsLayerTreeLayer *nodeLayer = mRoot->findLayer( layer->id() );
51  if ( nodeLayer )
52  {
53  allLayerOrder << nodeLayer->layer();
54  if ( nodeLayer->isVisible() )
55  canvasLayers << nodeLayer->layer();
56  if ( nodeLayer->customProperty( QStringLiteral( "overview" ), 0 ).toInt() )
57  overviewLayers << nodeLayer->layer();
58  }
59  }
60  }
61  else
62  setCanvasLayers( mRoot, canvasLayers, overviewLayers, allLayerOrder );
63 
64  QList<QgsLayerTreeLayer *> layerNodes = mRoot->findLayers();
65  int currentLayerCount = layerNodes.count();
66  bool firstLayers = mAutoSetupOnFirstLayer && mLastLayerCount == 0 && currentLayerCount != 0;
67 
68  mCanvas->setLayers( canvasLayers );
69  if ( mOverviewCanvas )
70  mOverviewCanvas->setLayers( overviewLayers );
71 
72  if ( firstLayers )
73  {
74  // if we are moving from zero to non-zero layers, let's zoom to those data
75  mCanvas->zoomToFullExtent();
76  }
77 
78  if ( !mFirstCRS.isValid() )
79  {
80  // find out what is the first used CRS in case we may need to turn on OTF projections later
81  Q_FOREACH ( QgsLayerTreeLayer *layerNode, layerNodes )
82  {
83  if ( layerNode->layer() && layerNode->layer()->crs().isValid() )
84  {
85  mFirstCRS = layerNode->layer()->crs();
86  break;
87  }
88  }
89  }
90 
91  if ( mFirstCRS.isValid() && firstLayers )
92  {
93  QgsProject::instance()->setCrs( mFirstCRS );
94  }
95 
96  mLastLayerCount = currentLayerCount;
97  if ( currentLayerCount == 0 )
98  mFirstCRS = QgsCoordinateReferenceSystem();
99 
100  mPendingCanvasUpdate = false;
101 
102  emit canvasLayersChanged( canvasLayers );
103 }
104 
105 void QgsLayerTreeMapCanvasBridge::setCanvasLayers( QgsLayerTreeNode *node, QList<QgsMapLayer *> &canvasLayers, QList<QgsMapLayer *> &overviewLayers, QList<QgsMapLayer *> &allLayers )
106 {
107  if ( QgsLayerTree::isLayer( node ) )
108  {
109  QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
110  allLayers << nodeLayer->layer();
111  if ( nodeLayer->isVisible() )
112  canvasLayers << nodeLayer->layer();
113  if ( nodeLayer->customProperty( QStringLiteral( "overview" ), 0 ).toInt() )
114  overviewLayers << nodeLayer->layer();
115  }
116 
117  Q_FOREACH ( QgsLayerTreeNode *child, node->children() )
118  setCanvasLayers( child, canvasLayers, overviewLayers, allLayers );
119 }
120 
121 void QgsLayerTreeMapCanvasBridge::deferredSetCanvasLayers()
122 {
123  if ( mPendingCanvasUpdate )
124  return;
125 
126  mPendingCanvasUpdate = true;
127  QMetaObject::invokeMethod( this, "setCanvasLayers", Qt::QueuedConnection );
128 }
129 
130 void QgsLayerTreeMapCanvasBridge::nodeVisibilityChanged()
131 {
132  deferredSetCanvasLayers();
133 }
134 
135 void QgsLayerTreeMapCanvasBridge::nodeCustomPropertyChanged( QgsLayerTreeNode *node, const QString &key )
136 {
137  Q_UNUSED( node );
138  if ( key == QLatin1String( "overview" ) )
139  deferredSetCanvasLayers();
140 }
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:74
Base class for all map layer types.
Definition: qgsmaplayer.h:54
void layerOrderChanged()
Emitted when the layer order has changed.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well) ...
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:73
QList< QgsMapLayer * > customLayerOrder() const
The order in which layers will be rendered on the canvas.
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
QList< QgsLayerTreeNode * > children()
Get list of children of the node. Children are owned by the parent.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:31
void setLayers(const QList< QgsMapLayer *> &layers)
updates layer set for overview
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 setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the project&#39;s native coordinate reference system.
Definition: qgsproject.cpp:445
QgsCoordinateReferenceSystem crs() const
Returns the layer&#39;s spatial reference system.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:52
This class is a base class for nodes in a layer tree.
bool hasCustomLayerOrder() const
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
void canvasLayersChanged(const QList< QgsMapLayer * > &layers)
Emitted when the set of layers (or order of layers) visible in the canvas changes.
QgsMapLayer * layer() const
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
void setLayers(const QList< QgsMapLayer *> &layers)
Sets the list of layers that should be shown in the canvas.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:377
void zoomToFullExtent()
Zoom to the full extent of all layers.
This class represents a coordinate reference system (CRS).
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
QgsLayerTreeMapCanvasBridge(QgsLayerTree *root, QgsMapCanvas *canvas, QObject *parent=0)
Constructor: does not take ownership of the layer tree nor canvas.
Layer tree node points to a map layer.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.