Skip to content

Commit 9ddb5d9

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 (cherry-picked from 52eef90)
1 parent 430abed commit 9ddb5d9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/core/qgslegendrenderer.cpp

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

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

0 commit comments

Comments
 (0)