Getter/setter for linking a map canvas with its QgsProject; link app canvases to QgsProject::instance(); rationale: using the QgsProject singleton is discouraged

This commit is contained in:
Germán Carrillo 2020-06-10 22:26:50 -05:00 committed by Nyall Dawson
parent 45125979b2
commit f3d473cb27
4 changed files with 48 additions and 0 deletions

View File

@ -337,6 +337,21 @@ You don't have to call it manually, QgsMapTool takes care of it.
QgsMapTool *mapTool(); QgsMapTool *mapTool();
%Docstring %Docstring
Returns the currently active tool 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 %End
void setCanvasColor( const QColor &_newVal ); void setCanvasColor( const QColor &_newVal );

View File

@ -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 myGreen = settings.value( QStringLiteral( "qgis/default_canvas_color_green" ), 255 ).toInt();
int myBlue = settings.value( QStringLiteral( "qgis/default_canvas_color_blue" ), 255 ).toInt(); int myBlue = settings.value( QStringLiteral( "qgis/default_canvas_color_blue" ), 255 ).toInt();
mMapCanvas->setCanvasColor( QColor( myRed, myGreen, myBlue ) ); mMapCanvas->setCanvasColor( QColor( myRed, myGreen, myBlue ) );
// set project linked to main canvas
mMapCanvas->setProject( QgsProject::instance() );
endProfile(); endProfile();
// what type of project to auto-open // what type of project to auto-open
@ -4481,6 +4484,7 @@ QgsMapCanvasDockWidget *QgisApp::createNewMapCanvasDock( const QString &name )
QgsMapCanvas *mapCanvas = mapCanvasWidget->mapCanvas(); QgsMapCanvas *mapCanvas = mapCanvasWidget->mapCanvas();
mapCanvas->freeze( true ); mapCanvas->freeze( true );
mapCanvas->setObjectName( name ); mapCanvas->setObjectName( name );
mapCanvas->setProject( QgsProject::instance() );
connect( mapCanvas, &QgsMapCanvas::messageEmitted, this, &QgisApp::displayMessage ); connect( mapCanvas, &QgsMapCanvas::messageEmitted, this, &QgisApp::displayMessage );
connect( mLayerTreeCanvasBridge, &QgsLayerTreeMapCanvasBridge::canvasLayersChanged, mapCanvas, &QgsMapCanvas::setLayers ); connect( mLayerTreeCanvasBridge, &QgsLayerTreeMapCanvasBridge::canvasLayersChanged, mapCanvas, &QgsMapCanvas::setLayers );

View File

@ -2030,6 +2030,11 @@ void QgsMapCanvas::unsetMapTool( QgsMapTool *tool )
} }
} }
void QgsMapCanvas::setProject( QgsProject *project )
{
mProject = project;
}
void QgsMapCanvas::setCanvasColor( const QColor &color ) void QgsMapCanvas::setCanvasColor( const QColor &color )
{ {
if ( canvasColor() == color ) if ( canvasColor() == color )
@ -2247,6 +2252,11 @@ QgsMapTool *QgsMapCanvas::mapTool()
return mMapTool; return mMapTool;
} }
QgsProject *QgsMapCanvas::project()
{
return mProject;
}
void QgsMapCanvas::panActionEnd( QPoint releasePoint ) void QgsMapCanvas::panActionEnd( QPoint releasePoint )
{ {
// move map image and other items to standard position // move map image and other items to standard position

View File

@ -28,6 +28,7 @@
#include "qgscustomdrophandler.h" #include "qgscustomdrophandler.h"
#include "qgstemporalrangeobject.h" #include "qgstemporalrangeobject.h"
#include "qgsmapcanvasinteractionblocker.h" #include "qgsmapcanvasinteractionblocker.h"
#include "qgsproject.h"
#include <QDomDocument> #include <QDomDocument>
#include <QGraphicsView> #include <QGraphicsView>
@ -355,6 +356,21 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! Returns the currently active tool //! Returns the currently active tool
QgsMapTool *mapTool(); 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. //! Write property of QColor bgColor.
void setCanvasColor( const QColor &_newVal ); void setCanvasColor( const QColor &_newVal );
//! Read property of QColor bgColor. //! Read property of QColor bgColor.
@ -1112,6 +1128,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! previous tool if current is for zooming/panning //! previous tool if current is for zooming/panning
QgsMapTool *mLastNonZoomMapTool = nullptr; QgsMapTool *mLastNonZoomMapTool = nullptr;
//! Pointer to project linked to this canvas
QgsProject *mProject = nullptr;
//! Context menu //! Context menu
QMenu *mMenu = nullptr; QMenu *mMenu = nullptr;