Skip to content

Commit

Permalink
Only render main annotation layer in main map canvas or docked maps
Browse files Browse the repository at this point in the history
Don't show it in other map canvas instances, eg the coordinate
bounds preview map

Fixes #58766
  • Loading branch information
nyalldawson committed Oct 3, 2024
1 parent afb31e6 commit 65e3283
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 2 deletions.
14 changes: 14 additions & 0 deletions python/PyQt6/core/auto_additions/qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5958,6 +5958,20 @@
# --
Qgis.RendererUsage.baseClass = Qgis
# monkey patching scoped based enum
Qgis.MapCanvasFlag.ShowMainAnnotationLayer.__doc__ = "The project's main annotation layer should be rendered in the canvas"
Qgis.MapCanvasFlag.__doc__ = """Flags controlling behavior of map canvases.

.. versionadded:: 3.40

* ``ShowMainAnnotationLayer``: The project's main annotation layer should be rendered in the canvas

"""
# --
Qgis.MapCanvasFlag.baseClass = Qgis
Qgis.MapCanvasFlags = lambda flags=0: Qgis.MapCanvasFlag(flags)
Qgis.MapCanvasFlags.baseClass = Qgis
MapCanvasFlags = Qgis # dirty hack since SIP seems to introduce the flags in module
# monkey patching scoped based enum
Qgis.ViewSyncModeFlag.Sync3DTo2D.__doc__ = "Synchronize 3D view camera to the main map canvas extent"
Qgis.ViewSyncModeFlag.Sync2DTo3D.__doc__ = "Update the 2D main canvas extent to include the viewed area from the 3D view"
Qgis.ViewSyncModeFlag.__doc__ = """Synchronization of 2D map canvas and 3D view
Expand Down
10 changes: 10 additions & 0 deletions python/PyQt6/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,14 @@ The development version
Unknown,
};

enum class MapCanvasFlag /BaseType=IntFlag/
{
ShowMainAnnotationLayer,
};

typedef QFlags<Qgis::MapCanvasFlag> MapCanvasFlags;


enum class ViewSyncModeFlag /BaseType=IntFlag/
{
Sync3DTo2D,
Expand Down Expand Up @@ -3374,6 +3382,8 @@ QFlags<Qgis::DataProviderReadFlag> operator|(Qgis::DataProviderReadFlag f1, QFla

QFlags<Qgis::VectorProviderCapability> operator|(Qgis::VectorProviderCapability f1, QFlags<Qgis::VectorProviderCapability> f2);

QFlags<Qgis::MapCanvasFlag> operator|(Qgis::MapCanvasFlag f1, QFlags<Qgis::MapCanvasFlag> f2);




Expand Down
18 changes: 18 additions & 0 deletions python/PyQt6/gui/auto_generated/qgsmapcanvas.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ to first clear the theme association by calling :py:func:`~QgsMapCanvas.setTheme
empty string before :py:func:`~QgsMapCanvas.setLayers` calls can be made.

.. seealso:: :py:func:`layers`
%End

void setFlags( Qgis::MapCanvasFlags flags );
%Docstring
Sets ``flags`` which control how the map canvas behaves.

.. seealso:: :py:func:`flags`

.. versionadded:: 3.40
%End

Qgis::MapCanvasFlags flags() const;
%Docstring
Returns flags which control how the map canvas behaves.

.. seealso:: :py:func:`setFlags`

.. versionadded:: 3.40
%End

void setCurrentLayer( QgsMapLayer *layer );
Expand Down
13 changes: 13 additions & 0 deletions python/core/auto_additions/qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5904,6 +5904,19 @@
# --
Qgis.RendererUsage.baseClass = Qgis
# monkey patching scoped based enum
Qgis.MapCanvasFlag.ShowMainAnnotationLayer.__doc__ = "The project's main annotation layer should be rendered in the canvas"
Qgis.MapCanvasFlag.__doc__ = """Flags controlling behavior of map canvases.

.. versionadded:: 3.40

* ``ShowMainAnnotationLayer``: The project's main annotation layer should be rendered in the canvas

"""
# --
Qgis.MapCanvasFlag.baseClass = Qgis
Qgis.MapCanvasFlags.baseClass = Qgis
MapCanvasFlags = Qgis # dirty hack since SIP seems to introduce the flags in module
# monkey patching scoped based enum
Qgis.ViewSyncModeFlag.Sync3DTo2D.__doc__ = "Synchronize 3D view camera to the main map canvas extent"
Qgis.ViewSyncModeFlag.Sync2DTo3D.__doc__ = "Update the 2D main canvas extent to include the viewed area from the 3D view"
Qgis.ViewSyncModeFlag.__doc__ = """Synchronization of 2D map canvas and 3D view
Expand Down
10 changes: 10 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,14 @@ The development version
Unknown,
};

enum class MapCanvasFlag
{
ShowMainAnnotationLayer,
};

typedef QFlags<Qgis::MapCanvasFlag> MapCanvasFlags;


enum class ViewSyncModeFlag
{
Sync3DTo2D,
Expand Down Expand Up @@ -3374,6 +3382,8 @@ QFlags<Qgis::DataProviderReadFlag> operator|(Qgis::DataProviderReadFlag f1, QFla

QFlags<Qgis::VectorProviderCapability> operator|(Qgis::VectorProviderCapability f1, QFlags<Qgis::VectorProviderCapability> f2);

QFlags<Qgis::MapCanvasFlag> operator|(Qgis::MapCanvasFlag f1, QFlags<Qgis::MapCanvasFlag> f2);




Expand Down
18 changes: 18 additions & 0 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ to first clear the theme association by calling :py:func:`~QgsMapCanvas.setTheme
empty string before :py:func:`~QgsMapCanvas.setLayers` calls can be made.

.. seealso:: :py:func:`layers`
%End

void setFlags( Qgis::MapCanvasFlags flags );
%Docstring
Sets ``flags`` which control how the map canvas behaves.

.. seealso:: :py:func:`flags`

.. versionadded:: 3.40
%End

Qgis::MapCanvasFlags flags() const;
%Docstring
Returns flags which control how the map canvas behaves.

.. seealso:: :py:func:`setFlags`

.. versionadded:: 3.40
%End

void setCurrentLayer( QgsMapLayer *layer );
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipBadLayers
startProfile( tr( "Creating map canvas" ) );
mMapCanvas = new QgsMapCanvas( centralWidget );
mMapCanvas->setObjectName( QStringLiteral( "theMapCanvas" ) );
mMapCanvas->setFlags( Qgis::MapCanvasFlag::ShowMainAnnotationLayer );

// before anything, let's freeze canvas redraws
QgsCanvasRefreshBlocker refreshBlocker;
Expand Down Expand Up @@ -2083,6 +2084,7 @@ QgisApp::QgisApp()

mInternalClipboard = new QgsClipboard;
mMapCanvas = new QgsMapCanvas();
mMapCanvas->setFlags( Qgis::MapCanvasFlag::ShowMainAnnotationLayer );
connect( mMapCanvas, &QgsMapCanvas::messageEmitted, this, &QgisApp::displayMessage );
QgsCanvasRefreshBlocker refreshBlocker;
mLayerTreeView = new QgsLayerTreeView( this );
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsmapcanvasdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
mToolbar->setIconSize( QgisApp::instance()->iconSize( true ) );

mMapCanvas = new QgsMapCanvas( this );
mMapCanvas->setFlags( Qgis::MapCanvasFlag::ShowMainAnnotationLayer );
mXyMarker = new QgsVertexMarker( mMapCanvas );
mXyMarker->setIconType( QgsVertexMarker::ICON_CIRCLE );
mXyMarker->setIconSize( 6 );
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -3190,6 +3190,25 @@ class CORE_EXPORT Qgis
};
Q_ENUM( RendererUsage )

/**
* Flags controlling behavior of map canvases.
*
* \since QGIS 3.40
*/
enum class MapCanvasFlag : int SIP_ENUM_BASETYPE( IntFlag )
{
ShowMainAnnotationLayer = 1 << 0, //!< The project's main annotation layer should be rendered in the canvas
};
Q_ENUM( MapCanvasFlag )

/**
* Flags controlling behavior of map canvases.
*
* \since QGIS 3.40
*/
Q_DECLARE_FLAGS( MapCanvasFlags, MapCanvasFlag )
Q_FLAG( MapCanvasFlags )

/**
* Synchronization of 2D map canvas and 3D view
*
Expand Down Expand Up @@ -5724,6 +5743,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::DataItemProviderCapabilities )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::VectorRenderingSimplificationFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::DataProviderReadFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::VectorProviderCapabilities )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::MapCanvasFlags )

// hack to workaround warnings when casting void pointers
// retrieved from QLibrary::resolve to function pointers.
Expand Down
16 changes: 14 additions & 2 deletions src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,16 @@ void QgsMapCanvas::setLayers( const QList<QgsMapLayer *> &layers )
setLayersPrivate( layers );
}

void QgsMapCanvas::setFlags( Qgis::MapCanvasFlags flags )
{
mFlags = flags;
}

Qgis::MapCanvasFlags QgsMapCanvas::flags() const
{
return mFlags;
}

void QgsMapCanvas::setLayersPrivate( const QList<QgsMapLayer *> &layers )
{
const QList<QgsMapLayer *> oldLayers = mSettings.layers();
Expand Down Expand Up @@ -825,7 +835,8 @@ void QgsMapCanvas::refreshMap()
// render main annotation layer above all other layers
QgsMapSettings renderSettings = mSettings;
QList<QgsMapLayer *> allLayers = renderSettings.layers();
allLayers.insert( 0, QgsProject::instance()->mainAnnotationLayer() );
if ( mFlags & Qgis::MapCanvasFlag::ShowMainAnnotationLayer )
allLayers.insert( 0, QgsProject::instance()->mainAnnotationLayer() );

renderSettings.setLayers( filterLayersForRender( allLayers ) );

Expand Down Expand Up @@ -3590,7 +3601,8 @@ void QgsMapCanvas::startPreviewJob( int number )

previewLayers << layer;
}
if ( QgsProject::instance()->mainAnnotationLayer()->dataProvider()->renderInPreview( context ) )
if ( ( mFlags & Qgis::MapCanvasFlag::ShowMainAnnotationLayer )
&& QgsProject::instance()->mainAnnotationLayer()->dataProvider()->renderInPreview( context ) )
{
previewLayers.insert( 0, QgsProject::instance()->mainAnnotationLayer() );
}
Expand Down
18 changes: 18 additions & 0 deletions src/gui/qgsmapcanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView, public QgsExpressionContex
*/
void setLayers( const QList<QgsMapLayer *> &layers );

/**
* Sets \a flags which control how the map canvas behaves.
*
* \see flags()
* \since QGIS 3.40
*/
void setFlags( Qgis::MapCanvasFlags flags );

/**
* Returns flags which control how the map canvas behaves.
*
* \see setFlags()
* \since QGIS 3.40
*/
Qgis::MapCanvasFlags flags() const;

void setCurrentLayer( QgsMapLayer *layer );

/**
Expand Down Expand Up @@ -1306,6 +1322,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView, public QgsExpressionContex

QgsOverlayWidgetLayout *mLayout = nullptr;

Qgis::MapCanvasFlags mFlags;

//! encompases all map settings necessary for map rendering
QgsMapSettings mSettings;

Expand Down
14 changes: 14 additions & 0 deletions tests/src/python/test_qgsmapcanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,20 @@ def testMainAnnotationLayerRendered(self):

# annotation must be rendered over other layers
rendered_image = self.canvas_to_image(canvas)

# should NOT be shown, as ShowMainAnnotationLayer flag not set
self.assertFalse(
self.image_check('main_annotation_layer', 'main_annotation_layer', rendered_image,
color_tolerance=2,
allowed_mismatch=20,
expect_fail=True)
)

canvas.setFlags(Qgis.MapCanvasFlag.ShowMainAnnotationLayer)
canvas.refresh()
canvas.waitWhileRendering()
rendered_image = self.canvas_to_image(canvas)
# now annotation should be rendered
self.assertTrue(
self.image_check('main_annotation_layer', 'main_annotation_layer', rendered_image,
color_tolerance=2,
Expand Down

0 comments on commit 65e3283

Please sign in to comment.