Skip to content
Permalink
Browse files
Convenience auto setup features when adding new layers
On first layer: zoom to full extent, setup destination CRS and map units
On first layer with different CRS: enable OTF reprojection

This functionality is not triggered when opening an existing project.
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 97642dd commit f83a11f880ef5a4c1b67fba1c2641fca9d9c76ad
Showing with 90 additions and 4 deletions.
  1. +9 −2 src/app/qgisapp.cpp
  2. +3 −0 src/app/qgisapp.h
  3. +56 −0 src/gui/layertree/qgslayertreemapcanvasbridge.cpp
  4. +22 −2 src/gui/layertree/qgslayertreemapcanvasbridge.h
@@ -2233,8 +2233,8 @@ void QgisApp::initLayerTreeView()
mLayerTreeDock->setWidget( mLayerTreeView );
addDockWidget( Qt::LeftDockWidgetArea, mLayerTreeDock );

QgsLayerTreeMapCanvasBridge* bridge = new QgsLayerTreeMapCanvasBridge( QgsProject::instance()->layerTreeRoot(), mMapCanvas );
bridge->setParent(this);
mLayerTreeCanvasBridge = new QgsLayerTreeMapCanvasBridge( QgsProject::instance()->layerTreeRoot(), mMapCanvas );
mLayerTreeCanvasBridge->setParent(this);
}


@@ -9458,6 +9458,13 @@ void QgisApp::writeProject( QDomDocument &doc )
void QgisApp::readProject( const QDomDocument &doc )
{
projectChanged( doc );

// force update of canvas, without automatic changes to extent and OTF projections
mLayerTreeCanvasBridge->setAutoEnableCrsTransform( false );
mLayerTreeCanvasBridge->setAutoSetupOnFirstLayer( false );
mLayerTreeCanvasBridge->setCanvasLayers();
mLayerTreeCanvasBridge->setAutoEnableCrsTransform( true );
mLayerTreeCanvasBridge->setAutoSetupOnFirstLayer( true );
}

void QgisApp::showLayerProperties( QgsMapLayer *ml )
@@ -46,6 +46,7 @@ class QgsComposerManager;
class QgsContrastEnhancement;
class QgsGeometry;
class QgsFeature;
class QgsLayerTreeMapCanvasBridge;
class QgsLayerTreeView;
class QgsLegend;
class QgsLayerOrder;
@@ -1472,6 +1473,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QgsMapCanvas *mMapCanvas;
//! Table of contents (legend) for the map
QgsLayerTreeView* mLayerTreeView;
//! Helper class that connects layer tree with map canvas
QgsLayerTreeMapCanvasBridge* mLayerTreeCanvasBridge;
//! Table of contents (legend) for the map
QgsLegend *mMapLegend;
//! Table of contents (legend) to order layers of the map
@@ -9,6 +9,9 @@ QgsLayerTreeMapCanvasBridge::QgsLayerTreeMapCanvasBridge(QgsLayerTreeGroup *root
, mCanvas(canvas)
, mPendingCanvasUpdate(false)
, mHasCustomLayerOrder(false)
, mAutoSetupOnFirstLayer(true)
, mAutoEnableCrsTransform(true)
, mLastLayerCount(root->findLayers().count())
{
connect(root, SIGNAL(addedChildren(QgsLayerTreeNode*,int,int)), this, SLOT(nodeAddedChildren(QgsLayerTreeNode*,int,int)));
connect(root, SIGNAL(customPropertyChanged(QgsLayerTreeNode*,QString)), this, SLOT(nodeCustomPropertyChanged(QgsLayerTreeNode*,QString)));
@@ -87,6 +90,59 @@ void QgsLayerTreeMapCanvasBridge::setCanvasLayers()

mCanvas->setLayerSet(layers);

QList<QgsLayerTreeLayer*> layerNodes = mRoot->findLayers();

int currentLayerCount = layerNodes.count();
if (mAutoSetupOnFirstLayer && mLastLayerCount == 0 && currentLayerCount != 0)
{
// if we are moving from zero to non-zero layers, let's zoom to those data
mCanvas->zoomToFullExtent();

// also setup destination CRS and map units if the OTF projections are not yet enabled
if (!mCanvas->mapSettings().hasCrsTransformEnabled())
{
foreach (QgsLayerTreeLayer* layerNode, layerNodes)
{
if (layerNode->layer())
{
mCanvas->setDestinationCrs( layerNode->layer()->crs() );
mCanvas->setMapUnits( layerNode->layer()->crs().mapUnits() );
}
}
}
}

if (!mFirstCRS.isValid())
{
// find out what is the first used CRS in case we may need to turn on OTF projections later
foreach (QgsLayerTreeLayer* layerNode, layerNodes)
{
if (layerNode->layer() && layerNode->layer()->crs().isValid())
{
mFirstCRS = layerNode->layer()->crs();
break;
}
}
}

if (mAutoEnableCrsTransform && mFirstCRS.isValid() && !mCanvas->mapSettings().hasCrsTransformEnabled())
{
// check whether all layers still have the same CRS
foreach (QgsLayerTreeLayer* layerNode, layerNodes)
{
if (layerNode->layer() && layerNode->layer()->crs().isValid() && layerNode->layer()->crs() != mFirstCRS)
{
mCanvas->setDestinationCrs( mFirstCRS );
mCanvas->setCrsTransformEnabled( true );
break;
}
}
}

mLastLayerCount = currentLayerCount;
if (currentLayerCount == 0)
mFirstCRS = QgsCoordinateReferenceSystem();

mPendingCanvasUpdate = false;
}

@@ -4,6 +4,8 @@
#include <QObject>
#include <QStringList>

#include "qgscoordinatereferencesystem.h"

class QgsMapCanvas;
class QgsMapCanvasLayer;
class QgsLayerTreeGroup;
@@ -20,10 +22,23 @@ class GUI_EXPORT QgsLayerTreeMapCanvasBridge : public QObject

QStringList defaultLayerOrder() const;

//! if enabled, will automatically set full canvas extent and destination CRS + map units
//! when first layer(s) are added
void setAutoSetupOnFirstLayer(bool enabled) { mAutoSetupOnFirstLayer = enabled; }
bool autoSetupOnFirstLayer() const { return mAutoSetupOnFirstLayer; }

//! if enabled, will automatically turn on on-the-fly reprojection of layers if a layer
//! with different source CRS is added
void setAutoEnableCrsTransform(bool enabled) { mAutoEnableCrsTransform = enabled; }
bool autoEnableCrsTransform() const { return mAutoEnableCrsTransform; }

public slots:
void setHasCustomLayerOrder(bool override);
void setCustomLayerOrder(const QStringList& order);

//! force update of canvas layers from the layer tree. Normally this should not be needed to be called.
void setCanvasLayers();

signals:
void hasCustomLayerOrderChanged(bool);
void customLayerOrderChanged(const QStringList& order);
@@ -42,8 +57,6 @@ protected slots:
void nodeVisibilityChanged();
void nodeCustomPropertyChanged(QgsLayerTreeNode* node, QString key);

void setCanvasLayers();

protected:
QgsLayerTreeGroup* mRoot;
QgsMapCanvas* mCanvas;
@@ -52,6 +65,13 @@ protected slots:

bool mHasCustomLayerOrder;
QStringList mCustomLayerOrder;

bool mAutoSetupOnFirstLayer;
bool mAutoEnableCrsTransform;

bool mHasFirstLayer;
bool mLastLayerCount;
QgsCoordinateReferenceSystem mFirstCRS;
};

#endif // QGSLAYERTREEMAPCANVASBRIDGE_H

0 comments on commit f83a11f

Please sign in to comment.