From a673fa8393146d9a8cd1a3bf6f0444bfa23352d7 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sat, 10 Sep 2016 10:02:08 +1000 Subject: [PATCH] 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) --- src/core/qgslegendrenderer.cpp | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/core/qgslegendrenderer.cpp b/src/core/qgslegendrenderer.cpp index 0d8f9534d8a..98446314f4c 100644 --- a/src/core/qgslegendrenderer.cpp +++ b/src/core/qgslegendrenderer.cpp @@ -265,17 +265,12 @@ void QgsLegendRenderer::setColumns( QList& atomList ) // Divide atoms to columns double totalHeight = 0; - // bool first = true; qreal maxAtomHeight = 0; Q_FOREACH ( const Atom& atom, atomList ) { - //if ( !first ) - //{ totalHeight += spaceAboveAtom( atom ); - //} totalHeight += atom.size.height(); maxAtomHeight = qMax( atom.size.height(), maxAtomHeight ); - // first = false; } // We know height of each atom and we have to split them into columns @@ -283,22 +278,21 @@ void QgsLegendRenderer::setColumns( QList& atomList ) // We are using simple heuristic, brute fore appeared to be to slow, // the number of combinations is N = n!/(k!*(n-k)!) where n = atomsCount-1 // and k = columnsCount-1 - - double avgColumnHeight = totalHeight / mSettings.columnCount(); + double maxColumnHeight = 0; int currentColumn = 0; int currentColumnAtomCount = 0; // number of atoms in current column double currentColumnHeight = 0; - double maxColumnHeight = 0; double closedColumnsHeight = 0; - // first = true; // first in column + for ( int i = 0; i < atomList.size(); i++ ) { - Atom atom = atomList[i]; + // Recalc average height for remaining columns including current + double avgColumnHeight = ( totalHeight - closedColumnsHeight ) / ( mSettings.columnCount() - currentColumn ); + + Atom atom = atomList.at( i ); double currentHeight = currentColumnHeight; - //if ( !first ) - //{ - currentHeight += spaceAboveAtom( atom ); - //} + if ( currentColumnAtomCount > 0 ) + currentHeight += spaceAboveAtom( atom ); currentHeight += atom.size.height(); // Recalc average height for remaining columns including current @@ -322,11 +316,9 @@ void QgsLegendRenderer::setColumns( QList& atomList ) atomList[i].column = currentColumn; currentColumnAtomCount++; maxColumnHeight = qMax( currentColumnHeight, maxColumnHeight ); - - // first = false; } - // Alling labels of symbols for each layr/column to the same labelXOffset + // Align labels of symbols for each layr/column to the same labelXOffset QMap maxSymbolWidth; for ( int i = 0; i < atomList.size(); i++ ) {