Skip to content

Commit 97b9fc7

Browse files
committed
Add unit selection for extent buffer
1 parent c103bb2 commit 97b9fc7

File tree

10 files changed

+119
-10
lines changed

10 files changed

+119
-10
lines changed

python/PyQt6/core/auto_generated/symbology/qgssymbol.sip.in

+15-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ Returns the symbol's extent buffer.
879879
%Docstring
880880
Sets the symbol's extent buffer.
881881

882-
:param extentBuffer: buffer distance in map units
882+
:param extentBuffer: buffer distance.
883883

884884
.. seealso:: :py:func:`extentBuffer`
885885

@@ -888,6 +888,20 @@ Sets the symbol's extent buffer.
888888
Negative values are not supported and will be changed to 0.
889889

890890
.. versionadded:: 3.42
891+
%End
892+
893+
Qgis::RenderUnit extentBufferSizeUnit() const;
894+
%Docstring
895+
Returns the units for the buffer size.
896+
897+
.. seealso:: :py:func:`setExtentBufferSizeUnit`
898+
%End
899+
900+
void setExtentBufferSizeUnit( Qgis::RenderUnit unit );
901+
%Docstring
902+
Sets the ``unit`` used for the extent buffer.
903+
904+
.. seealso:: :py:func:`extentBufferSizeUnit`
891905
%End
892906

893907
protected:

python/core/auto_generated/symbology/qgssymbol.sip.in

+15-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ Returns the symbol's extent buffer.
879879
%Docstring
880880
Sets the symbol's extent buffer.
881881

882-
:param extentBuffer: buffer distance in map units
882+
:param extentBuffer: buffer distance.
883883

884884
.. seealso:: :py:func:`extentBuffer`
885885

@@ -888,6 +888,20 @@ Sets the symbol's extent buffer.
888888
Negative values are not supported and will be changed to 0.
889889

890890
.. versionadded:: 3.42
891+
%End
892+
893+
Qgis::RenderUnit extentBufferSizeUnit() const;
894+
%Docstring
895+
Returns the units for the buffer size.
896+
897+
.. seealso:: :py:func:`setExtentBufferSizeUnit`
898+
%End
899+
900+
void setExtentBufferSizeUnit( Qgis::RenderUnit unit );
901+
%Docstring
902+
Sets the ``unit`` used for the extent buffer.
903+
904+
.. seealso:: :py:func:`extentBufferSizeUnit`
891905
%End
892906

893907
protected:

src/core/symbology/qgsrenderer.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,27 @@ double QgsFeatureRenderer::maximumExtentBuffer( QgsRenderContext &context ) cons
427427

428428
const QgsExpressionContext &expContext = context.expressionContext();
429429

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

434+
double value = 0.0;
435+
434436
if ( property.isActive() )
435437
{
436-
return sym->dataDefinedProperties().valueAsDouble( QgsSymbol::Property::ExtentBuffer, expContext, sym->extentBuffer() );
438+
value = sym->dataDefinedProperties().valueAsDouble( QgsSymbol::Property::ExtentBuffer, expContext, sym->extentBuffer() );
439+
}
440+
else
441+
{
442+
value = sym->extentBuffer();
443+
}
444+
445+
if ( sym->extentBufferSizeUnit() != Qgis::RenderUnit::MapUnits )
446+
{
447+
value = context.convertToMapUnits( value, sym->extentBufferSizeUnit(), sym->mapUnitScale() );
437448
}
438449

439-
return sym->extentBuffer();
450+
return value;
440451
};
441452

442453
if ( symbolList.size() == 1 )

src/core/symbology/qgssymbol.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,7 @@ void QgsSymbol::copyCommonProperties( const QgsSymbol *other )
23152315
mSymbolFlags = other->mSymbolFlags;
23162316
mAnimationSettings = other->mAnimationSettings;
23172317
mExtentBuffer = other->mExtentBuffer;
2318+
mExtentBufferSizeUnit = other->mExtentBufferSizeUnit;
23182319
if ( other->mBufferSettings )
23192320
mBufferSettings = std::make_unique< QgsSymbolBufferSettings >( *other->mBufferSettings );
23202321
else

src/core/symbology/qgssymbol.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -879,13 +879,27 @@ class CORE_EXPORT QgsSymbol
879879
/**
880880
* Sets the symbol's extent buffer.
881881
*
882-
* \param extentBuffer buffer distance in map units
882+
* \param extentBuffer buffer distance.
883883
* \see extentBuffer()
884884
* \note Negative values are not supported and will be changed to 0.
885885
* \since QGIS 3.42
886886
*/
887887
void setExtentBuffer( double extentBuffer );
888888

889+
/**
890+
* Returns the units for the buffer size.
891+
*
892+
* \see setExtentBufferSizeUnit()
893+
*/
894+
Qgis::RenderUnit extentBufferSizeUnit() const { return mExtentBufferSizeUnit; }
895+
896+
/**
897+
* Sets the \a unit used for the extent buffer.
898+
*
899+
* \see extentBufferSizeUnit()
900+
*/
901+
void setExtentBufferSizeUnit( Qgis::RenderUnit unit ) { mExtentBufferSizeUnit = unit; }
902+
889903
protected:
890904

891905
/**
@@ -975,6 +989,7 @@ class CORE_EXPORT QgsSymbol
975989
QgsSymbolLayerList mLayers;
976990

977991
double mExtentBuffer = 0;
992+
Qgis::RenderUnit mExtentBufferSizeUnit = Qgis::RenderUnit::MapUnits;
978993

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

src/core/symbology/qgssymbollayerutils.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,7 @@ QgsSymbol *QgsSymbolLayerUtils::loadSymbol( const QDomElement &element, const Qg
13351335
}
13361336
symbol->setOpacity( element.attribute( QStringLiteral( "alpha" ), QStringLiteral( "1.0" ) ).toDouble() );
13371337
symbol->setExtentBuffer( element.attribute( QStringLiteral( "extent_buffer" ), QStringLiteral( "0.0" ) ).toDouble() );
1338+
symbol->setExtentBufferSizeUnit( QgsUnitTypes::decodeRenderUnit( element.attribute( QStringLiteral( "extent_buffer_unit" ), QStringLiteral( "MapUnit" ) ) ) );
13381339
symbol->setClipFeaturesToExtent( element.attribute( QStringLiteral( "clip_to_extent" ), QStringLiteral( "1" ) ).toInt() );
13391340
symbol->setForceRHR( element.attribute( QStringLiteral( "force_rhr" ), QStringLiteral( "0" ) ).toInt() );
13401341
Qgis::SymbolFlags flags;
@@ -1454,7 +1455,10 @@ QDomElement QgsSymbolLayerUtils::saveSymbol( const QString &name, const QgsSymbo
14541455
symEl.setAttribute( QStringLiteral( "alpha" ), QString::number( symbol->opacity() ) );
14551456
symEl.setAttribute( QStringLiteral( "clip_to_extent" ), symbol->clipFeaturesToExtent() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
14561457
if ( !qgsDoubleNear( symbol->extentBuffer(), 0 ) )
1458+
{
14571459
symEl.setAttribute( QStringLiteral( "extent_buffer" ), QString::number( symbol->extentBuffer() ) );
1460+
symEl.setAttribute( QStringLiteral( "extent_buffer_unit" ), QgsUnitTypes::encodeUnit( symbol->extentBufferSizeUnit() ) );
1461+
}
14581462
symEl.setAttribute( QStringLiteral( "force_rhr" ), symbol->forceRHR() ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
14591463
if ( symbol->flags() & Qgis::SymbolFlag::RendererShouldUseSymbolLevels )
14601464
symEl.setAttribute( QStringLiteral( "renderer_should_use_levels" ), QStringLiteral( "1" ) );

src/gui/symbology/qgsextentbufferdialog.cpp

+30-2
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,35 @@
2121
#include "qgspanelwidget.h"
2222
#include "qgssymbol.h"
2323
#include "qgssymbolwidgetcontext.h"
24+
#include "qgsunittypes.h"
2425
#include "qgsvectorlayer.h"
2526

2627
QgsExtentBufferWidget::QgsExtentBufferWidget( QgsSymbol *symbol, QgsVectorLayer *layer, QWidget *parent )
2728
: QgsPanelWidget( parent ), mSymbol( symbol ), mLayer( layer )
2829
{
2930
setupUi( this );
3031

31-
mExtentBufferSpinBox->setValue( mSymbol-> extentBuffer() );
32+
mExtentBufferSpinBox->setValue( mSymbol->extentBuffer() );
33+
34+
mExtentBufferUnitSelectionWidget->setShowMapScaleButton( false );
35+
mExtentBufferUnitSelectionWidget->setUnits( { Qgis::RenderUnit::Millimeters,
36+
Qgis::RenderUnit::MetersInMapUnits,
37+
Qgis::RenderUnit::MapUnits,
38+
Qgis::RenderUnit::Pixels,
39+
Qgis::RenderUnit::Points,
40+
Qgis::RenderUnit::Inches } );
41+
mExtentBufferUnitSelectionWidget->setUnit( mSymbol->extentBufferSizeUnit() );
3242

3343
connect( mExtentBufferSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, [ = ]()
3444
{
3545
emit widgetChanged();
3646
} );
3747

48+
connect( mExtentBufferUnitSelectionWidget, &QgsUnitSelectionWidget::changed, this, [ = ]()
49+
{
50+
emit widgetChanged();
51+
} );
52+
3853
registerDataDefinedButton( mExtentBufferDDButton, QgsSymbol::Property::ExtentBuffer );
3954
}
4055

@@ -97,7 +112,7 @@ QgsExtentBufferDialog::QgsExtentBufferDialog( QgsSymbol *symbol, QgsVectorLayer
97112
vLayout->addWidget( bbox );
98113
setLayout( vLayout );
99114

100-
setWindowTitle( tr( "Extent buffer" ) );
115+
setWindowTitle( tr( "Extent Buffer" ) );
101116
}
102117

103118
double QgsExtentBufferDialog::extentBuffer() const
@@ -108,6 +123,19 @@ double QgsExtentBufferDialog::extentBuffer() const
108123
return mWidget->extentBuffer();
109124
}
110125

126+
Qgis::RenderUnit QgsExtentBufferWidget::sizeUnit() const
127+
{
128+
return mExtentBufferUnitSelectionWidget->unit();
129+
}
130+
131+
Qgis::RenderUnit QgsExtentBufferDialog::sizeUnit() const
132+
{
133+
if ( !mWidget )
134+
return Qgis::RenderUnit::MapUnits;
135+
136+
return mWidget->sizeUnit();
137+
}
138+
111139
QgsProperty QgsExtentBufferDialog::dataDefinedProperty() const
112140
{
113141
if ( !mWidget )

src/gui/symbology/qgsextentbufferdialog.h

+11
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ class GUI_EXPORT QgsExtentBufferWidget : public QgsPanelWidget, public QgsExpres
7171
*/
7272
QgsSymbolWidgetContext context() const;
7373

74+
/**
75+
* Returns the extent buffer unit currently set in the widget.
76+
* \see setContext()
77+
*/
78+
Qgis::RenderUnit sizeUnit() const;
79+
7480
private:
7581
QgsSymbol *mSymbol = nullptr;
7682
QgsVectorLayer *mLayer = nullptr;
@@ -105,6 +111,11 @@ class GUI_EXPORT QgsExtentBufferDialog : public QDialog
105111
*/
106112
double extentBuffer() const;
107113

114+
/**
115+
* Returns the extent buffer unit currently set in the widget.
116+
*/
117+
Qgis::RenderUnit sizeUnit() const;
118+
108119
/**
109120
* Returns the extent buffer value currently set in the widget.
110121
*

src/gui/symbology/qgssymbolslistwidget.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ void QgsSymbolsListWidget::showExtentBufferSettings()
317317
{
318318
mSymbol->setExtentBuffer( widget->extentBuffer() );
319319
mSymbol->setDataDefinedProperty( QgsSymbol::Property::ExtentBuffer, widget->dataDefinedProperty() );
320+
mSymbol->setExtentBufferSizeUnit( widget->sizeUnit() );
320321

321322
emit changed();
322323
} );
@@ -337,6 +338,7 @@ void QgsSymbolsListWidget::showExtentBufferSettings()
337338
{
338339
mSymbol->setExtentBuffer( dlg.extentBuffer() );
339340
mSymbol->setDataDefinedProperty( QgsSymbol::Property::ExtentBuffer, dlg.dataDefinedProperty() );
341+
mSymbol->setExtentBufferSizeUnit( dlg.sizeUnit() );
340342

341343
emit changed();
342344
}

src/ui/qgsextentbufferdialogbase.ui

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<item>
1818
<widget class="QLabel" name="label">
1919
<property name="text">
20-
<string>Define an extent buffer distance in map units. The symbol will be rendered for features which are within the buffered map extent.</string>
20+
<string>Define an extent buffer distance. The symbol will be rendered for features which are within the buffered map extent.</string>
2121
</property>
2222
<property name="wordWrap">
2323
<bool>true</bool>
@@ -36,7 +36,7 @@
3636
<item>
3737
<widget class="QgsDoubleSpinBox" name="mExtentBufferSpinBox">
3838
<property name="minimum">
39-
<double>0</double>
39+
<double>0.000000000000000</double>
4040
</property>
4141
<property name="maximum">
4242
<double>999999999.000000000000000</double>
@@ -49,6 +49,9 @@
4949
</property>
5050
</widget>
5151
</item>
52+
<item>
53+
<widget class="QgsUnitSelectionWidget" name="mExtentBufferUnitSelectionWidget" native="true"/>
54+
</item>
5255
<item>
5356
<widget class="QgsPropertyOverrideButton" name="mExtentBufferDDButton"/>
5457
</item>
@@ -86,6 +89,12 @@
8689
<header>qgspanelwidget.h</header>
8790
<container>1</container>
8891
</customwidget>
92+
<customwidget>
93+
<class>QgsUnitSelectionWidget</class>
94+
<extends>QWidget</extends>
95+
<header>qgsunitselectionwidget.h</header>
96+
<container>1</container>
97+
</customwidget>
8998
</customwidgets>
9099
<resources/>
91100
<connections/>

0 commit comments

Comments
 (0)