Skip to content
Permalink
Browse files

Round up number of items per column so earlier columns are taller

  • Loading branch information
jdugge authored and nyalldawson committed Oct 5, 2020
1 parent b33c2cb commit ee4c841819fd1c822fb587e8f4395f8b24670559
Showing with 10 additions and 2 deletions.
  1. +10 −2 src/core/qgslegendrenderer.cpp
@@ -453,6 +453,7 @@ int QgsLegendRenderer::setColumns( QList<LegendComponentGroup> &componentGroups
if ( group.placeColumnBreakBeforeGroup )
forcedColumnBreaks++;
}
double averageGroupHeight = (totalHeight - totalSpaceAboveGroups) / componentGroups.size();

if ( mSettings.columnCount() == 0 && forcedColumnBreaks == 0 )
return 0;
@@ -490,14 +491,21 @@ int QgsLegendRenderer::setColumns( QList<LegendComponentGroup> &componentGroups
currentHeight += spaceAboveGroup( group );
currentHeight += group.size.height();

int numberRemainingGroups = componentGroups.size() - i;

// Recalc average height for remaining columns including current
int numberRemainingColumns = numberAutoPlacedBreaks + 1 - autoPlacedBreaks;
double avgColumnHeight = ( totalHeight - closedColumnsHeight ) / numberRemainingColumns;
double avgColumnHeight = ( currentHeight + numberRemainingGroups * averageGroupHeight + (numberRemainingGroups - numberRemainingColumns - 1) * averageSpaceAboveGroups ) / numberRemainingColumns;
// Round up to the next full number of groups to put in one column
// This ensures that earlier columns contain more elements than later columns
int averageGroupsPerColumn = std::ceil(avgColumnHeight / (averageGroupHeight + averageSpaceAboveGroups));
avgColumnHeight = averageGroupsPerColumn * (averageGroupHeight + averageSpaceAboveGroups) - averageSpaceAboveGroups;

bool canCreateNewColumn = ( currentColumnGroupCount > 0 ) // do not leave empty column
&& ( currentColumn < targetNumberColumns - 1 ) // must not exceed max number of columns
&& ( autoPlacedBreaks < numberAutoPlacedBreaks );

bool shouldCreateNewColumn = ( currentHeight - avgColumnHeight ) > group.size.height() / 2 // center of current group is over average height
bool shouldCreateNewColumn = currentHeight > avgColumnHeight // current group height is greater than expected group height
&& currentColumnGroupCount > 0 // do not leave empty column
&& currentHeight > maxGroupHeight // no sense to make smaller columns than max group height
&& currentHeight > maxColumnHeight; // no sense to make smaller columns than max column already created

0 comments on commit ee4c841

Please sign in to comment.
You can’t perform that action at this time.