From 52eef9006183a66d53926f33e73afa1dcd534e59 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sat, 10 Sep 2016 10:05:28 +1000 Subject: [PATCH] Make sure items in legend always occupy the set number of columns In some cases (eg a legend with 4 items and 3 columns) less columns were being created --- src/core/qgslegendrenderer.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/core/qgslegendrenderer.cpp b/src/core/qgslegendrenderer.cpp index 98446314f4c..48875f229a0 100644 --- a/src/core/qgslegendrenderer.cpp +++ b/src/core/qgslegendrenderer.cpp @@ -295,13 +295,19 @@ void QgsLegendRenderer::setColumns( QList& 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++;