Skip to content

Commit 52eef90

Browse files
committed
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
1 parent a673fa8 commit 52eef90

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/core/qgslegendrenderer.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,19 @@ void QgsLegendRenderer::setColumns( QList<Atom>& atomList )
295295
currentHeight += spaceAboveAtom( atom );
296296
currentHeight += atom.size.height();
297297

298-
// Recalc average height for remaining columns including current
299-
avgColumnHeight = ( totalHeight - closedColumnsHeight ) / ( mSettings.columnCount() - currentColumn );
300-
if (( currentHeight - avgColumnHeight ) > atom.size.height() / 2 // center of current atom is over average height
301-
&& currentColumnAtomCount > 0 // do not leave empty column
302-
&& currentHeight > maxAtomHeight // no sense to make smaller columns than max atom height
303-
&& currentHeight > maxColumnHeight // no sense to make smaller columns than max column already created
304-
&& currentColumn < mSettings.columnCount() - 1 ) // must not exceed max number of columns
298+
bool canCreateNewColumn = ( currentColumnAtomCount > 0 ) // do not leave empty column
299+
&& ( currentColumn < mSettings.columnCount() - 1 ); // must not exceed max number of columns
300+
301+
bool shouldCreateNewColumn = ( currentHeight - avgColumnHeight ) > atom.size.height() / 2 // center of current atom is over average height
302+
&& currentColumnAtomCount > 0 // do not leave empty column
303+
&& currentHeight > maxAtomHeight // no sense to make smaller columns than max atom height
304+
&& currentHeight > maxColumnHeight; // no sense to make smaller columns than max column already created
305+
306+
// also should create a new column if the number of items left < number of columns left
307+
// in this case we should spread the remaining items out over the remaining columns
308+
shouldCreateNewColumn |= ( atomList.size() - i < mSettings.columnCount() - currentColumn );
309+
310+
if ( canCreateNewColumn && shouldCreateNewColumn )
305311
{
306312
// New column
307313
currentColumn++;

0 commit comments

Comments
 (0)