Skip to content

Commit

Permalink
Handle indentation of right-aligned legends
Browse files Browse the repository at this point in the history
  • Loading branch information
jdugge committed Jan 16, 2021
1 parent 09956c8 commit 7de030a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 6 deletions.
4 changes: 2 additions & 2 deletions python/core/auto_generated/qgslegendstyle.sip.in
Expand Up @@ -107,7 +107,7 @@ Sets the alignment for the legend component.

double indent();
%Docstring
Returns the indent of a group or subgroup.
Returns the indent (in mm) of a group or subgroup.

.. seealso:: :py:func:`indent`

Expand All @@ -116,7 +116,7 @@ Returns the indent of a group or subgroup.

void setIndent( double indent );
%Docstring
Sets the indent of a group or subgroup.
Sets the indent (in mm) of a group or subgroup.

.. seealso:: :py:func:`indent`

Expand Down
21 changes: 19 additions & 2 deletions src/core/qgslegendrenderer.cpp
Expand Up @@ -284,6 +284,7 @@ QList<QgsLegendRenderer::LegendComponentGroup> QgsLegendRenderer::createComponen
{
QgsLayerTreeGroup *nodeGroup = QgsLayerTree::toGroup( node );
QString style = node->customProperty( QStringLiteral( "legend/title-style" ) ).toString();
// Update the required indent for the group/subgroup items, starting from the indent accumulated from parent groups
double newIndent = indent;
if ( style == QLatin1String( "subgroup" ) )
{
Expand Down Expand Up @@ -680,13 +681,22 @@ QSizeF QgsLegendRenderer::drawGroup( const LegendComponentGroup &group, QgsRende
double indentWidth = component.indent;
if ( s == QgsLegendStyle::Subgroup )
{
// Remove indent - the subgroup items should be indented, not the subgroup title
indentWidth -= mSettings.style( QgsLegendStyle::Subgroup ).indent( );
}
else
{
// Remove indent - the group items should be indented, not the group title
indentWidth -= mSettings.style( QgsLegendStyle::Group ).indent( );
}
columnContextForItem.left += indentWidth;
if ( mSettings.style( QgsLegendStyle::SymbolLabel ).alignment() == Qt::AlignLeft )
{
columnContextForItem.left += indentWidth;
}
if ( mSettings.style( QgsLegendStyle::SymbolLabel ).alignment() == Qt::AlignRight )
{
columnContextForItem.right -= indentWidth;
}
groupSize = drawGroupTitle( groupItem, context, columnContextForItem, currentY );
size.rwidth() = std::max( groupSize.width(), size.width() );
}
Expand Down Expand Up @@ -719,7 +729,14 @@ QSizeF QgsLegendRenderer::drawGroup( const LegendComponentGroup &group, QgsRende
ColumnContext columnContextForItem = columnContext;
double indentWidth = 0;
indentWidth = component.indent;
columnContextForItem.left += indentWidth;
if ( mSettings.style( QgsLegendStyle::SymbolLabel ).alignment() == Qt::AlignLeft )
{
columnContextForItem.left += indentWidth;
}
if ( mSettings.style( QgsLegendStyle::SymbolLabel ).alignment() == Qt::AlignRight )
{
columnContextForItem.right -= indentWidth;
}

LegendComponent symbolComponent = drawSymbolItem( legendNode, context, columnContextForItem, currentY, component.maxSiblingSymbolWidth );
// expand width, it may be wider because of label offsets
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgslegendrenderer.h
Expand Up @@ -144,6 +144,10 @@ class CORE_EXPORT QgsLegendRenderer
//! Component size
QSizeF size;

/**
* Starting indent for groups/subgroups nested in other groups/subgroups.
* This value is the sum of the indents of all parent groups/subgroups.
*/
double indent = 0;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgslegendstyle.h
Expand Up @@ -122,15 +122,15 @@ class CORE_EXPORT QgsLegendStyle
void setAlignment( Qt::Alignment alignment ) { mAlignment = alignment; }

/**
* Returns the indent of a group or subgroup.
* Returns the indent (in mm) of a group or subgroup.
*
* \see indent()
* \since QGIS 3.18
*/
double indent() { return mIndent; }

/**
* Sets the indent of a group or subgroup.
* Sets the indent (in mm) of a group or subgroup.
*
* \see indent()
* \since QGIS 3.18
Expand Down
19 changes: 19 additions & 0 deletions tests/src/core/testqgslegendrenderer.cpp
Expand Up @@ -722,6 +722,25 @@ void TestQgsLegendRenderer::testGroupIndent()
_renderLegend( QStringLiteral( "legend_group_indent" ), &legendModel, settings );
QVERIFY( _verifyImage( QStringLiteral( "legend_group_indent" ), mReport ) );

settings.rstyle( QgsLegendStyle::Group ).setAlignment( Qt::AlignRight );
settings.rstyle( QgsLegendStyle::Subgroup ).setAlignment( Qt::AlignRight );
settings.rstyle( QgsLegendStyle::SymbolLabel ).setAlignment( Qt::AlignRight );
_renderLegend( QStringLiteral( "legend_group_indent_right_align_text" ), &legendModel, settings );
QVERIFY( _verifyImage( QStringLiteral( "legend_group_indent_right_align_text" ), mReport ) );

settings.rstyle( QgsLegendStyle::Group ).setAlignment( Qt::AlignLeft );
settings.rstyle( QgsLegendStyle::Subgroup ).setAlignment( Qt::AlignLeft );
settings.rstyle( QgsLegendStyle::SymbolLabel ).setAlignment( Qt::AlignLeft );
settings.setSymbolAlignment( Qt::AlignRight );
_renderLegend( QStringLiteral( "legend_group_indent_right_align_symbol" ), &legendModel, settings );
QVERIFY( _verifyImage( QStringLiteral( "legend_group_indent_right_align_symbol" ), mReport ) );

settings.rstyle( QgsLegendStyle::Group ).setAlignment( Qt::AlignRight );
settings.rstyle( QgsLegendStyle::Subgroup ).setAlignment( Qt::AlignRight );
settings.rstyle( QgsLegendStyle::SymbolLabel ).setAlignment( Qt::AlignRight );
settings.setSymbolAlignment( Qt::AlignRight );
_renderLegend( QStringLiteral( "legend_group_indent_right_align_symbol_right_align_text" ), &legendModel, settings );
QVERIFY( _verifyImage( QStringLiteral( "legend_group_indent_right_align_symbol_right_align_text" ), mReport ) );
}

void TestQgsLegendRenderer::testRightAlignText()
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7de030a

Please sign in to comment.