Skip to content

Commit

Permalink
Make sure items in legend always occupy the set number of columns
Browse files Browse the repository at this point in the history
In some cases (eg a legend with 4 items and 3 columns) less
columns were being created

(cherry-picked from 52eef90)
  • Loading branch information
nyalldawson committed Sep 14, 2016
1 parent 65ae3f7 commit 1190efe
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/core/qgslegendrenderer.cpp
Expand Up @@ -296,13 +296,19 @@ void QgsLegendRenderer::setColumns( QList<Atom>& atomList )
currentHeight += spaceAboveAtom( atom );
currentHeight += atom.size.height();

// Recalc average height for remaining columns including current
avgColumnHeight = ( totalHeight - closedColumnsHeight ) / ( mSettings.columnCount() - currentColumn );
if (( currentHeight - avgColumnHeight ) > atom.size.height() / 2 // center of current atom is over average height
&& currentColumnAtomCount > 0 // do not leave empty column
&& currentHeight > maxAtomHeight // no sense to make smaller columns than max atom height
&& currentHeight > maxColumnHeight // no sense to make smaller columns than max column already created
&& currentColumn < mSettings.columnCount() - 1 ) // must not exceed max number of columns
bool canCreateNewColumn = ( currentColumnAtomCount > 0 ) // do not leave empty column
&& ( currentColumn < mSettings.columnCount() - 1 ); // must not exceed max number of columns

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

// also should create a new column if the number of items left < number of columns left
// in this case we should spread the remaining items out over the remaining columns
shouldCreateNewColumn |= ( atomList.size() - i < mSettings.columnCount() - currentColumn );

if ( canCreateNewColumn && shouldCreateNewColumn )
{
// New column
currentColumn++;
Expand Down

0 comments on commit 1190efe

Please sign in to comment.