Skip to content
Permalink
Browse files

Getter/setter for linking a map canvas with its QgsProject; link app …

…canvases to QgsProject::instance(); rationale: using the QgsProject singleton is discouraged
  • Loading branch information
gacarrillor authored and nyalldawson committed Jun 11, 2020
1 parent 4512597 commit f3d473cb27b16b686aee6368d5fac9b32dd968a2
Showing with 48 additions and 0 deletions.
  1. +15 −0 python/gui/auto_generated/qgsmapcanvas.sip.in
  2. +4 −0 src/app/qgisapp.cpp
  3. +10 −0 src/gui/qgsmapcanvas.cpp
  4. +19 −0 src/gui/qgsmapcanvas.h
@@ -337,6 +337,21 @@ You don't have to call it manually, QgsMapTool takes care of it.
QgsMapTool *mapTool();
%Docstring
Returns the currently active tool
%End

void setProject( QgsProject *project );
%Docstring
Sets the ``project`` linked to this canvas.

.. versionadded:: 3.14
%End

QgsProject *project();
%Docstring
Returns the project linked to this canvas.
The returned value may be ``None``.

.. versionadded:: 3.14
%End

void setCanvasColor( const QColor &_newVal );
@@ -940,6 +940,9 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
int myGreen = settings.value( QStringLiteral( "qgis/default_canvas_color_green" ), 255 ).toInt();
int myBlue = settings.value( QStringLiteral( "qgis/default_canvas_color_blue" ), 255 ).toInt();
mMapCanvas->setCanvasColor( QColor( myRed, myGreen, myBlue ) );

// set project linked to main canvas
mMapCanvas->setProject( QgsProject::instance() );
endProfile();

// what type of project to auto-open
@@ -4481,6 +4484,7 @@ QgsMapCanvasDockWidget *QgisApp::createNewMapCanvasDock( const QString &name )
QgsMapCanvas *mapCanvas = mapCanvasWidget->mapCanvas();
mapCanvas->freeze( true );
mapCanvas->setObjectName( name );
mapCanvas->setProject( QgsProject::instance() );
connect( mapCanvas, &QgsMapCanvas::messageEmitted, this, &QgisApp::displayMessage );
connect( mLayerTreeCanvasBridge, &QgsLayerTreeMapCanvasBridge::canvasLayersChanged, mapCanvas, &QgsMapCanvas::setLayers );

@@ -2030,6 +2030,11 @@ void QgsMapCanvas::unsetMapTool( QgsMapTool *tool )
}
}

void QgsMapCanvas::setProject( QgsProject *project )
{
mProject = project;
}

void QgsMapCanvas::setCanvasColor( const QColor &color )
{
if ( canvasColor() == color )
@@ -2247,6 +2252,11 @@ QgsMapTool *QgsMapCanvas::mapTool()
return mMapTool;
}

QgsProject *QgsMapCanvas::project()
{
return mProject;
}

void QgsMapCanvas::panActionEnd( QPoint releasePoint )
{
// move map image and other items to standard position
@@ -28,6 +28,7 @@
#include "qgscustomdrophandler.h"
#include "qgstemporalrangeobject.h"
#include "qgsmapcanvasinteractionblocker.h"
#include "qgsproject.h"

#include <QDomDocument>
#include <QGraphicsView>
@@ -355,6 +356,21 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! Returns the currently active tool
QgsMapTool *mapTool();

/**
* Sets the \a project linked to this canvas.
*
* \since QGIS 3.14
*/
void setProject( QgsProject *project );

/**
* Returns the project linked to this canvas.
* The returned value may be NULLPTR.
*
* \since QGIS 3.14
*/
QgsProject *project();

//! Write property of QColor bgColor.
void setCanvasColor( const QColor &_newVal );
//! Read property of QColor bgColor.
@@ -1112,6 +1128,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! previous tool if current is for zooming/panning
QgsMapTool *mLastNonZoomMapTool = nullptr;

//! Pointer to project linked to this canvas
QgsProject *mProject = nullptr;

//! Context menu
QMenu *mMenu = nullptr;

0 comments on commit f3d473c

Please sign in to comment.
You can’t perform that action at this time.