Skip to content

Commit a673fa8

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)
1 parent 570e693 commit a673fa8

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/core/qgslegendrenderer.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -265,40 +265,34 @@ void QgsLegendRenderer::setColumns( QList<Atom>& atomList )
265265

266266
// Divide atoms to columns
267267
double totalHeight = 0;
268-
// bool first = true;
269268
qreal maxAtomHeight = 0;
270269
Q_FOREACH ( const Atom& atom, atomList )
271270
{
272-
//if ( !first )
273-
//{
274271
totalHeight += spaceAboveAtom( atom );
275-
//}
276272
totalHeight += atom.size.height();
277273
maxAtomHeight = qMax( atom.size.height(), maxAtomHeight );
278-
// first = false;
279274
}
280275

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

304298
// Recalc average height for remaining columns including current
@@ -322,11 +316,9 @@ void QgsLegendRenderer::setColumns( QList<Atom>& atomList )
322316
atomList[i].column = currentColumn;
323317
currentColumnAtomCount++;
324318
maxColumnHeight = qMax( currentColumnHeight, maxColumnHeight );
325-
326-
// first = false;
327319
}
328320

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

0 commit comments

Comments
 (0)