Skip to content

Commit ff3cf2b

Browse files
committed
Fix multi column legends with odd number of items would place
more items in rightmost columns instead of leftmost columns Eg a 2 column legend with 3 items would put 1 item in the first column and 2 in the second. This was ugly, and now it places 2 in the first column and 1 in the second. The legend column assigner was incorrectly adding padding above the first item in a column during column size calculation (padding which is not present when actually rendering the column) (cherry-picked from a673fa8)
1 parent 6ed5b66 commit ff3cf2b

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/core/qgslegendrenderer.cpp

+9-17
Original file line numberDiff line numberDiff line change
@@ -266,40 +266,34 @@ void QgsLegendRenderer::setColumns( QList<Atom>& atomList )
266266

267267
// Divide atoms to columns
268268
double totalHeight = 0;
269-
// bool first = true;
270269
qreal maxAtomHeight = 0;
271270
Q_FOREACH ( const Atom& atom, atomList )
272271
{
273-
//if ( !first )
274-
//{
275272
totalHeight += spaceAboveAtom( atom );
276-
//}
277273
totalHeight += atom.size.height();
278274
maxAtomHeight = qMax( atom.size.height(), maxAtomHeight );
279-
// first = false;
280275
}
281276

282277
// We know height of each atom and we have to split them into columns
283278
// minimizing max column height. It is sort of bin packing problem, NP-hard.
284279
// We are using simple heuristic, brute fore appeared to be to slow,
285280
// the number of combinations is N = n!/(k!*(n-k)!) where n = atomsCount-1
286281
// and k = columnsCount-1
287-
288-
double avgColumnHeight = totalHeight / mSettings.columnCount();
282+
double maxColumnHeight = 0;
289283
int currentColumn = 0;
290284
int currentColumnAtomCount = 0; // number of atoms in current column
291285
double currentColumnHeight = 0;
292-
double maxColumnHeight = 0;
293286
double closedColumnsHeight = 0;
294-
// first = true; // first in column
287+
295288
for ( int i = 0; i < atomList.size(); i++ )
296289
{
297-
Atom atom = atomList[i];
290+
// Recalc average height for remaining columns including current
291+
double avgColumnHeight = ( totalHeight - closedColumnsHeight ) / ( mSettings.columnCount() - currentColumn );
292+
293+
Atom atom = atomList.at( i );
298294
double currentHeight = currentColumnHeight;
299-
//if ( !first )
300-
//{
301-
currentHeight += spaceAboveAtom( atom );
302-
//}
295+
if ( currentColumnAtomCount > 0 )
296+
currentHeight += spaceAboveAtom( atom );
303297
currentHeight += atom.size.height();
304298

305299
// Recalc average height for remaining columns including current
@@ -323,11 +317,9 @@ void QgsLegendRenderer::setColumns( QList<Atom>& atomList )
323317
atomList[i].column = currentColumn;
324318
currentColumnAtomCount++;
325319
maxColumnHeight = qMax( currentColumnHeight, maxColumnHeight );
326-
327-
// first = false;
328320
}
329321

330-
// Alling labels of symbols for each layr/column to the same labelXOffset
322+
// Align labels of symbols for each layr/column to the same labelXOffset
331323
QMap<QString, qreal> maxSymbolWidth;
332324
for ( int i = 0; i < atomList.size(); i++ )
333325
{

0 commit comments

Comments
 (0)