Skip to content

Commit

Permalink
Getter/setter for linking a map canvas with its QgsProject; link app …
Browse files Browse the repository at this point in the history
…canvases to QgsProject::instance(); rationale: using the QgsProject singleton is discouraged
  • Loading branch information
gacarrillor authored and nyalldawson committed Jun 19, 2020
1 parent 4512597 commit f3d473c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
15 changes: 15 additions & 0 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );

Expand Down
10 changes: 10 additions & 0 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/gui/qgsmapcanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "qgscustomdrophandler.h"
#include "qgstemporalrangeobject.h"
#include "qgsmapcanvasinteractionblocker.h"
#include "qgsproject.h"

#include <QDomDocument>
#include <QGraphicsView>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit f3d473c

Please sign in to comment.