Skip to content

Commit

Permalink
[FEATURE] possibility to set a maximum marker size in layout legend
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 29, 2020
1 parent 9b8e983 commit f0ad33c
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 22 deletions.
12 changes: 12 additions & 0 deletions python/core/auto_generated/layout/qgslayoutitemlegend.sip.in
Expand Up @@ -304,6 +304,18 @@ Sets the legend symbol ``width``.
.. seealso:: :py:func:`symbolWidth`
%End

double maxMarkerSymbolSize() const;
%Docstring
Returns the legend maximum marker symbol size (in mm). 0.0 means there is no maximum set
%End

void setMaxMarkerSymbolSize( double size );
%Docstring
set maximum symbol size for point markers
@param size maximum marker size (in mm) or 0.0 if no maximum
%End


void setSymbolAlignment( Qt::AlignmentFlag alignment );
%Docstring
Sets the ``alignment`` for placement of legend symbols.
Expand Down
13 changes: 13 additions & 0 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -557,10 +557,23 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC
double widthOffset = 0;
double heightOffset = 0;

std::unique_ptr<QgsMarkerSymbol> maxSizeMarkerSymbol;
if ( QgsMarkerSymbol *markerSymbol = dynamic_cast<QgsMarkerSymbol *>( s ) )
{
// allow marker symbol to occupy bigger area if necessary
double size = markerSymbol->size( *context ) / context->scaleFactor();

//If marker size exceeds max marker size, clone symbol and set max size in mm
double maxMarkerSymbolSize = settings.maxMarkerSymbolSize();
if ( maxMarkerSymbolSize > 0 && size > maxMarkerSymbolSize )
{
maxSizeMarkerSymbol.reset( dynamic_cast<QgsMarkerSymbol *>( s->clone() ) );
maxSizeMarkerSymbol->setSize( maxMarkerSymbolSize );
maxSizeMarkerSymbol->setSizeUnit( QgsUnitTypes::RenderMillimeters );
s = maxSizeMarkerSymbol.get();
size = maxMarkerSymbolSize;
}

height = size;
width = size;
if ( width < desiredWidth )
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -252,6 +252,7 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
UndoLegendColumnCount, //!< Legend column count
UndoLegendSymbolWidth, //!< Legend symbol width
UndoLegendSymbolHeight, //!< Legend symbol height
UndoLegendMaxMarkerSymbolSize, //!< Max marker symbol size
UndoLegendWmsLegendWidth, //!< Legend WMS width
UndoLegendWmsLegendHeight, //!< Legend WMS height
UndoLegendTitleSpaceBottom, //!< Legend title space
Expand Down
13 changes: 13 additions & 0 deletions src/core/layout/qgslayoutitemlegend.cpp
Expand Up @@ -394,6 +394,16 @@ void QgsLayoutItemLegend::setSymbolWidth( double w )
mSettings.setSymbolSize( QSizeF( w, mSettings.symbolSize().height() ) );
}

double QgsLayoutItemLegend::maxMarkerSymbolSize() const
{
return mSettings.maxMarkerSymbolSize();
}

void QgsLayoutItemLegend::setMaxMarkerSymbolSize( double size )
{
mSettings.setMaxMarkerSymbolSize( size );
}

void QgsLayoutItemLegend::setSymbolAlignment( Qt::AlignmentFlag alignment )
{
mSettings.setSymbolAlignment( alignment );
Expand Down Expand Up @@ -526,6 +536,7 @@ bool QgsLayoutItemLegend::writePropertiesToElement( QDomElement &legendElem, QDo

legendElem.setAttribute( QStringLiteral( "symbolWidth" ), QString::number( mSettings.symbolSize().width() ) );
legendElem.setAttribute( QStringLiteral( "symbolHeight" ), QString::number( mSettings.symbolSize().height() ) );
legendElem.setAttribute( QStringLiteral( "maxMarkerSymbolSize" ), QString::number( mSettings.maxMarkerSymbolSize() ) );

legendElem.setAttribute( QStringLiteral( "symbolAlignment" ), mSettings.symbolAlignment() );

Expand Down Expand Up @@ -621,6 +632,8 @@ bool QgsLayoutItemLegend::readPropertiesFromElement( const QDomElement &itemElem
mSettings.setSymbolSize( QSizeF( itemElem.attribute( QStringLiteral( "symbolWidth" ), QStringLiteral( "7.0" ) ).toDouble(), itemElem.attribute( QStringLiteral( "symbolHeight" ), QStringLiteral( "14.0" ) ).toDouble() ) );
mSettings.setSymbolAlignment( static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "symbolAlignment" ), QString::number( Qt::AlignLeft ) ).toInt() ) );

mSettings.setMaxMarkerSymbolSize( itemElem.attribute( QStringLiteral( "maxMarkerSymbolSize" ), QStringLiteral( "0.0" ) ).toDouble() );

mSettings.setWmsLegendSize( QSizeF( itemElem.attribute( QStringLiteral( "wmsLegendWidth" ), QStringLiteral( "50" ) ).toDouble(), itemElem.attribute( QStringLiteral( "wmsLegendHeight" ), QStringLiteral( "25" ) ).toDouble() ) );
mSettings.setLineSpacing( itemElem.attribute( QStringLiteral( "lineSpacing" ), QStringLiteral( "1.0" ) ).toDouble() );

Expand Down
12 changes: 12 additions & 0 deletions src/core/layout/qgslayoutitemlegend.h
Expand Up @@ -318,6 +318,18 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
*/
void setSymbolWidth( double width );

/**
* Returns the legend maximum marker symbol size (in mm). 0.0 means there is no maximum set
*/
double maxMarkerSymbolSize() const;

/**
* @brief set maximum symbol size for point markers
* @param size maximum marker size (in mm) or 0.0 if no maximum
*/
void setMaxMarkerSymbolSize( double size );


/**
* Sets the \a alignment for placement of legend symbols.
*
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgslegendsettings.h
Expand Up @@ -234,6 +234,17 @@ class CORE_EXPORT QgsLegendSettings
*/
void setSymbolSize( QSizeF s ) {mSymbolSize = s;}

/**
* Returns the maximum marker symbol size in mm
*/
double maxMarkerSymbolSize() const {return mMaxMarkerSymbolSize; }

/**
* Sets the maximum marker symbol size in mm
* @param size maximum marker size in mm
*/
void setMaxMarkerSymbolSize( double size ) { mMaxMarkerSymbolSize = size;}

/**
* Sets the \a alignment for placement of legend symbols.
*
Expand Down Expand Up @@ -479,6 +490,9 @@ class CORE_EXPORT QgsLegendSettings
//! Width and height of symbol icon
QSizeF mSymbolSize;

//! Maximum marker symbol size (in mm)
double mMaxMarkerSymbolSize = 0.0;

//! Width and height of WMS legendGraphic pixmap
QSizeF mWmsLegendSize;

Expand Down
15 changes: 15 additions & 0 deletions src/gui/layout/qgslayoutlegendwidget.cpp
Expand Up @@ -86,6 +86,7 @@ QgsLayoutLegendWidget::QgsLayoutLegendWidget( QgsLayoutItemLegend *legend, QgsMa
connect( mEqualColumnWidthCheckBox, &QCheckBox::toggled, this, &QgsLayoutLegendWidget::mEqualColumnWidthCheckBox_toggled );
connect( mSymbolWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutLegendWidget::mSymbolWidthSpinBox_valueChanged );
connect( mSymbolHeightSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutLegendWidget::mSymbolHeightSpinBox_valueChanged );
connect( mMaxMarkerSizeSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutLegendWidget::mMaxMarkerSizeSpinBox_valueChanged );
connect( mWmsLegendWidthSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutLegendWidget::mWmsLegendWidthSpinBox_valueChanged );
connect( mWmsLegendHeightSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutLegendWidget::mWmsLegendHeightSpinBox_valueChanged );
connect( mTitleSpaceBottomSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutLegendWidget::mTitleSpaceBottomSpinBox_valueChanged );
Expand Down Expand Up @@ -230,6 +231,7 @@ void QgsLayoutLegendWidget::setGuiElements()
mEqualColumnWidthCheckBox->setChecked( mLegend->equalColumnWidth() );
mSymbolWidthSpinBox->setValue( mLegend->symbolWidth() );
mSymbolHeightSpinBox->setValue( mLegend->symbolHeight() );
mMaxMarkerSizeSpinBox->setValue( mLegend->maxMarkerSize() );
mWmsLegendWidthSpinBox->setValue( mLegend->wmsLegendWidth() );
mWmsLegendHeightSpinBox->setValue( mLegend->wmsLegendHeight() );
mTitleSpaceBottomSpinBox->setValue( mLegend->style( QgsLegendStyle::Title ).margin( QgsLegendStyle::Bottom ) );
Expand Down Expand Up @@ -403,6 +405,18 @@ void QgsLayoutLegendWidget::mSymbolWidthSpinBox_valueChanged( double d )
}
}

void QgsLayoutLegendWidget::mMaxMarkerSizeSpinBox_valueChanged( double d )
{
if ( mLegend )
{
mLegend->beginCommand( tr( "Change Legend Maximum Marker Size" ), QgsLayoutItem::UndoLegendMaxMarkerSymbolSize );
mLegend->setMaxMarkerSymbolSize( d );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
}
}

void QgsLayoutLegendWidget::mSymbolHeightSpinBox_valueChanged( double d )
{
if ( mLegend )
Expand Down Expand Up @@ -1221,6 +1235,7 @@ void QgsLayoutLegendWidget::blockAllSignals( bool b )
mEqualColumnWidthCheckBox->blockSignals( b );
mSymbolWidthSpinBox->blockSignals( b );
mSymbolHeightSpinBox->blockSignals( b );
mMaxMarkerSizeSpinBox->blockSignals( b );
mGroupSpaceSpinBox->blockSignals( b );
mSpaceBelowGroupHeadingSpinBox->blockSignals( b );
mGroupSideSpinBox->blockSignals( b );
Expand Down
1 change: 1 addition & 0 deletions src/gui/layout/qgslayoutlegendwidget.h
Expand Up @@ -76,6 +76,7 @@ class GUI_EXPORT QgsLayoutLegendWidget: public QgsLayoutItemBaseWidget, private
void mEqualColumnWidthCheckBox_toggled( bool checked );
void mSymbolWidthSpinBox_valueChanged( double d );
void mSymbolHeightSpinBox_valueChanged( double d );
void mMaxMarkerSizeSpinBox_valueChanged( double d );
void mWmsLegendWidthSpinBox_valueChanged( double d );
void mWmsLegendHeightSpinBox_valueChanged( double d );
void mTitleSpaceBottomSpinBox_valueChanged( double d );
Expand Down
58 changes: 36 additions & 22 deletions src/ui/layout/qgslayoutlegendwidgetbase.ui
Expand Up @@ -63,9 +63,9 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-1283</y>
<width>367</width>
<height>2239</height>
<y>-1389</y>
<width>387</width>
<height>2685</height>
</rect>
</property>
<layout class="QVBoxLayout" name="mainLayout">
Expand Down Expand Up @@ -235,12 +235,12 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="toolTip">
<string>Move item down</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionArrowDown.svg</normaloff>:/images/themes/default/mActionArrowDown.svg</iconset>
Expand All @@ -255,12 +255,12 @@
</item>
<item>
<widget class="QToolButton" name="mMoveUpToolButton">
<property name="text">
<string/>
</property>
<property name="toolTip">
<string>Move item up</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mActionArrowUp.svg</normaloff>:/images/themes/default/mActionArrowUp.svg</iconset>
Expand Down Expand Up @@ -295,12 +295,12 @@
</item>
<item>
<widget class="QToolButton" name="mAddToolButton">
<property name="text">
<string/>
</property>
<property name="toolTip">
<string>Add layer(s) to legend</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyAdd.svg</normaloff>:/images/themes/default/symbologyAdd.svg</iconset>
Expand All @@ -315,12 +315,12 @@
</item>
<item>
<widget class="QToolButton" name="mRemoveToolButton">
<property name="text">
<string/>
</property>
<property name="toolTip">
<string>Remove selected item(s) from legend</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyRemove.svg</normaloff>:/images/themes/default/symbologyRemove.svg</iconset>
Expand All @@ -335,12 +335,12 @@
</item>
<item>
<widget class="QToolButton" name="mEditPushButton">
<property name="text">
<string/>
</property>
<property name="toolTip">
<string>Edit selected item properties</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/symbologyEdit.svg</normaloff>:/images/themes/default/symbologyEdit.svg</iconset>
Expand Down Expand Up @@ -411,12 +411,12 @@
</item>
<item>
<widget class="QgsLegendFilterButton" name="mExpressionFilterButton">
<property name="text">
<string/>
</property>
<property name="toolTip">
<string>Filter expression</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/mIconExpressionFilter.svg</normaloff>:/images/themes/default/mIconExpressionFilter.svg</iconset>
Expand Down Expand Up @@ -788,7 +788,7 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="4" column="0" colspan="2">
<widget class="QgsCollapsibleGroupBoxBasic" name="mRasterStrokeGroupBox">
<property name="title">
<string>Draw stroke for raster symbols</string>
Expand Down Expand Up @@ -887,6 +887,20 @@
</layout>
</widget>
</item>
<item row="3" column="1">
<widget class="QgsDoubleSpinBox" name="mMaxMarkerSizeSpinBox">
<property name="suffix">
<string>mm</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="mMaxMarkerSizeLabel">
<property name="text">
<string>Max marker size</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit f0ad33c

Please sign in to comment.