Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable extent buffer to symbols #59630

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/PyQt6/core/auto_additions/qgssymbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
QgsSymbol.Property.PropertyOpacity = QgsSymbol.Property.Opacity
QgsSymbol.PropertyOpacity.is_monkey_patched = True
QgsSymbol.PropertyOpacity.__doc__ = "Opacity"
QgsSymbol.ExtentBuffer = QgsSymbol.Property.ExtentBuffer
QgsSymbol.ExtentBuffer.is_monkey_patched = True
QgsSymbol.ExtentBuffer.__doc__ = "Extent buffer \n.. versionadded:: 3.42"
QgsSymbol.Property.__doc__ = """Data definable properties.

.. versionadded:: 3.18
Expand All @@ -12,6 +15,10 @@

Available as ``QgsSymbol.PropertyOpacity`` in older QGIS releases.

* ``ExtentBuffer``: Extent buffer

.. versionadded:: 3.42


"""
# --
Expand Down
11 changes: 11 additions & 0 deletions python/PyQt6/core/auto_generated/symbology/qgsrenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,17 @@ Currently clones
:param destRenderer: destination renderer for copied effect

.. versionadded:: 3.22
%End

double maximumExtentBuffer( QgsRenderContext &context ) const;
%Docstring
Returns the maximum extent buffer found in this renderer's symbols.

.. note::

Returns 0 if the renderer doesn't have any symbols.

.. versionadded:: 3.42
%End

protected:
Expand Down
40 changes: 40 additions & 0 deletions python/PyQt6/core/auto_generated/symbology/qgssymbol.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Returns the default symbol type required for the specified geometry ``type``.
enum class Property /BaseType=IntEnum/
{
Opacity,
ExtentBuffer,
};

static const QgsPropertiesDefinition &propertyDefinitions();
Expand Down Expand Up @@ -863,6 +864,44 @@ Internally, this notifies all symbol layers which were used via a call to
:py:func:`QgsSymbolLayer.stopFeatureRender()`.

.. versionadded:: 3.20
%End

double extentBuffer() const;
%Docstring
Returns the symbol's extent buffer.

:return: The symbol's extent buffer

.. versionadded:: 3.42
%End

void setExtentBuffer( double extentBuffer );
%Docstring
Sets the symbol's extent buffer.

:param extentBuffer: buffer distance.

.. seealso:: :py:func:`extentBuffer`

.. note::

Negative values are not supported and will be changed to 0.

.. versionadded:: 3.42
%End

Qgis::RenderUnit extentBufferSizeUnit() const;
%Docstring
Returns the units for the buffer size.

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

void setExtentBufferSizeUnit( Qgis::RenderUnit unit );
%Docstring
Sets the ``unit`` used for the extent buffer.

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

protected:
Expand Down Expand Up @@ -934,6 +973,7 @@ Render editing vertex marker at specified point




private:
QgsSymbol( const QgsSymbol & );
};
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_additions/qgssymbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
QgsSymbol.Property.PropertyOpacity = QgsSymbol.Property.Opacity
QgsSymbol.PropertyOpacity.is_monkey_patched = True
QgsSymbol.PropertyOpacity.__doc__ = "Opacity"
QgsSymbol.ExtentBuffer = QgsSymbol.Property.ExtentBuffer
QgsSymbol.ExtentBuffer.is_monkey_patched = True
QgsSymbol.ExtentBuffer.__doc__ = "Extent buffer \n.. versionadded:: 3.42"
QgsSymbol.Property.__doc__ = """Data definable properties.

.. versionadded:: 3.18
Expand All @@ -12,6 +15,10 @@

Available as ``QgsSymbol.PropertyOpacity`` in older QGIS releases.

* ``ExtentBuffer``: Extent buffer

.. versionadded:: 3.42


"""
# --
Expand Down
11 changes: 11 additions & 0 deletions python/core/auto_generated/symbology/qgsrenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,17 @@ Currently clones
:param destRenderer: destination renderer for copied effect

.. versionadded:: 3.22
%End

double maximumExtentBuffer( QgsRenderContext &context ) const;
%Docstring
Returns the maximum extent buffer found in this renderer's symbols.

.. note::

Returns 0 if the renderer doesn't have any symbols.

.. versionadded:: 3.42
%End

protected:
Expand Down
40 changes: 40 additions & 0 deletions python/core/auto_generated/symbology/qgssymbol.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Returns the default symbol type required for the specified geometry ``type``.
enum class Property
{
Opacity,
ExtentBuffer,
};

static const QgsPropertiesDefinition &propertyDefinitions();
Expand Down Expand Up @@ -863,6 +864,44 @@ Internally, this notifies all symbol layers which were used via a call to
:py:func:`QgsSymbolLayer.stopFeatureRender()`.

.. versionadded:: 3.20
%End

double extentBuffer() const;
%Docstring
Returns the symbol's extent buffer.

:return: The symbol's extent buffer

.. versionadded:: 3.42
%End

void setExtentBuffer( double extentBuffer );
%Docstring
Sets the symbol's extent buffer.

:param extentBuffer: buffer distance.

.. seealso:: :py:func:`extentBuffer`

.. note::

Negative values are not supported and will be changed to 0.

.. versionadded:: 3.42
%End

Qgis::RenderUnit extentBufferSizeUnit() const;
%Docstring
Returns the units for the buffer size.

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

void setExtentBufferSizeUnit( Qgis::RenderUnit unit );
%Docstring
Sets the ``unit`` used for the extent buffer.

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

protected:
Expand Down Expand Up @@ -934,6 +973,7 @@ Render editing vertex marker at specified point




private:
QgsSymbol( const QgsSymbol & );
};
Expand Down
49 changes: 47 additions & 2 deletions src/core/symbology/qgsrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <QDomDocument>
#include <QPolygonF>
#include <QThread>
#include <algorithm>

QgsPropertiesDefinition QgsFeatureRenderer::sPropertyDefinitions;

Expand Down Expand Up @@ -417,6 +418,49 @@ QgsLegendSymbolList QgsFeatureRenderer::legendSymbolItems() const
return QgsLegendSymbolList();
}

double QgsFeatureRenderer::maximumExtentBuffer( QgsRenderContext &context ) const
{
const QgsSymbolList symbolList = symbols( context );

if ( symbolList.empty() )
return 0;

const QgsExpressionContext &expContext = context.expressionContext();

auto getValueFromSymbol = [ &expContext, &context ]( const QgsSymbol * sym ) -> double
{
const QgsProperty property = sym->dataDefinedProperties().property( QgsSymbol::Property::ExtentBuffer );

double value = 0.0;

if ( property.isActive() )
{
value = sym->dataDefinedProperties().valueAsDouble( QgsSymbol::Property::ExtentBuffer, expContext, sym->extentBuffer() );
}
else
{
value = sym->extentBuffer();
}

if ( sym->extentBufferSizeUnit() != Qgis::RenderUnit::MapUnits )
{
value = context.convertToMapUnits( value, sym->extentBufferSizeUnit(), sym->mapUnitScale() );
}

return value;
};

if ( symbolList.size() == 1 )
return getValueFromSymbol( symbolList[0] );

auto it = std::max_element( symbolList.constBegin(), symbolList.constEnd(), [ &getValueFromSymbol ]( const QgsSymbol * a, const QgsSymbol * b ) -> bool
{
return getValueFromSymbol( a ) < getValueFromSymbol( b );
} );

return getValueFromSymbol( *it );
}

QList<QgsLayerTreeModelLegendNode *> QgsFeatureRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer ) const
{
QList<QgsLayerTreeModelLegendNode *> nodes;
Expand Down Expand Up @@ -492,8 +536,9 @@ QgsSymbolList QgsFeatureRenderer::symbolsForFeature( const QgsFeature &feature,

void QgsFeatureRenderer::modifyRequestExtent( QgsRectangle &extent, QgsRenderContext &context )
{
Q_UNUSED( extent )
Q_UNUSED( context )
double extentBuffer = maximumExtentBuffer( context );

extent.grow( extentBuffer );
}

QgsSymbolList QgsFeatureRenderer::originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const
Expand Down
8 changes: 8 additions & 0 deletions src/core/symbology/qgsrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,14 @@ class CORE_EXPORT QgsFeatureRenderer
*/
void copyRendererData( QgsFeatureRenderer *destRenderer ) const;

/**
* Returns the maximum extent buffer found in this renderer's symbols.
*
* \note Returns 0 if the renderer doesn't have any symbols.
* \since QGIS 3.42
*/
double maximumExtentBuffer( QgsRenderContext &context ) const;

protected:
QgsFeatureRenderer( const QString &type );

Expand Down
18 changes: 18 additions & 0 deletions src/core/symbology/qgssymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "qgssymbol.h"
#include "qgspolyhedralsurface.h"
#include "qgsrectangle.h"
#include "qgssymbollayer.h"

#include "qgsgeometrygeneratorsymbollayer.h"
Expand Down Expand Up @@ -2223,6 +2224,20 @@ QgsSymbolRenderContext *QgsSymbol::symbolRenderContext()
return mSymbolRenderContext.get();
}

double QgsSymbol::extentBuffer() const
{
return mExtentBuffer;
}

void QgsSymbol::setExtentBuffer( double extentBuffer )
{
if ( extentBuffer < 0 )
mExtentBuffer = 0;
else
mExtentBuffer = extentBuffer;
}


void QgsSymbol::renderVertexMarker( QPointF pt, QgsRenderContext &context, Qgis::VertexMarkerType currentVertexMarkerType, double currentVertexMarkerSize )
{
int markerSize = context.convertToPainterUnits( currentVertexMarkerSize, Qgis::RenderUnit::Millimeters );
Expand All @@ -2239,6 +2254,7 @@ void QgsSymbol::initPropertyDefinitions()
sPropertyDefinitions = QgsPropertiesDefinition
{
{ static_cast< int >( QgsSymbol::Property::Opacity ), QgsPropertyDefinition( "alpha", QObject::tr( "Opacity" ), QgsPropertyDefinition::Opacity, origin )},
{ static_cast< int >( QgsSymbol::Property::ExtentBuffer ), QgsPropertyDefinition( "extent_buffer", QObject::tr( "Extent buffer" ), QgsPropertyDefinition::Double, origin )},
};
}

Expand Down Expand Up @@ -2298,6 +2314,8 @@ void QgsSymbol::copyCommonProperties( const QgsSymbol *other )
mDataDefinedProperties = other->mDataDefinedProperties;
mSymbolFlags = other->mSymbolFlags;
mAnimationSettings = other->mAnimationSettings;
mExtentBuffer = other->mExtentBuffer;
mExtentBufferSizeUnit = other->mExtentBufferSizeUnit;
if ( other->mBufferSettings )
mBufferSettings = std::make_unique< QgsSymbolBufferSettings >( *other->mBufferSettings );
else
Expand Down
36 changes: 36 additions & 0 deletions src/core/symbology/qgssymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ class CORE_EXPORT QgsSymbol
enum class Property SIP_MONKEYPATCH_SCOPEENUM_UNNEST( QgsSymbol, Property ) : int
{
Opacity SIP_MONKEYPATCH_COMPAT_NAME( PropertyOpacity ), //!< Opacity
ExtentBuffer, //!< Extent buffer \since QGIS 3.42
};
// *INDENT-ON*

Expand Down Expand Up @@ -867,6 +868,38 @@ class CORE_EXPORT QgsSymbol
*/
void stopFeatureRender( const QgsFeature &feature, QgsRenderContext &context, int layer = -1 );

/**
* Returns the symbol's extent buffer.
*
* \returns The symbol's extent buffer
* \since QGIS 3.42
*/
double extentBuffer() const;

/**
* Sets the symbol's extent buffer.
*
* \param extentBuffer buffer distance.
* \see extentBuffer()
* \note Negative values are not supported and will be changed to 0.
* \since QGIS 3.42
*/
void setExtentBuffer( double extentBuffer );

/**
* Returns the units for the buffer size.
*
* \see setExtentBufferSizeUnit()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* \see setExtentBufferSizeUnit()
* \see extentBuffer()
* \see setExtentBufferSizeUnit()
*
* \since QGIS 3.42

*/
Qgis::RenderUnit extentBufferSizeUnit() const { return mExtentBufferSizeUnit; }

/**
* Sets the \a unit used for the extent buffer.
*
* \see extentBufferSizeUnit()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* \see extentBufferSizeUnit()
* \see setExtentBuffer()
* \see extentBufferSizeUnit()
*
* \since QGIS 3.42

*/
void setExtentBufferSizeUnit( Qgis::RenderUnit unit ) { mExtentBufferSizeUnit = unit; }

protected:

/**
Expand Down Expand Up @@ -955,6 +988,9 @@ class CORE_EXPORT QgsSymbol
Qgis::SymbolType mType;
QgsSymbolLayerList mLayers;

double mExtentBuffer = 0;
Qgis::RenderUnit mExtentBufferSizeUnit = Qgis::RenderUnit::MapUnits;

//! Symbol opacity (in the range 0 - 1)
qreal mOpacity = 1.0;

Expand Down
Loading
Loading