0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-25 00:54:39 +03:00

Add comments explaining calculations

This commit is contained in:
Emil Sawicki 2022-03-05 16:40:12 +01:00
parent f03ccdd23b
commit 2ce293f85d
2 changed files with 29 additions and 9 deletions

View File

@ -1121,8 +1121,15 @@ QString AbstractSeriesChart::axisLabel(int i, const AxisData &axisData)
bool AbstractSeriesChart::calculateLegendColumnWidths(qreal indicatorWidth, qreal maxWidth, const QFontMetrics &fm) bool AbstractSeriesChart::calculateLegendColumnWidths(qreal indicatorWidth, qreal maxWidth, const QFontMetrics &fm)
{ {
// This method is called in the loop, because to handle case when we get
// 3 small series names in first row and then in second row small name and long name.
// In this case we need to set maximum column count to 2 and iterate from the start to recalculate
// all the sizes
qreal currentRowWidth = 0; qreal currentRowWidth = 0;
int currentColumn = 0; int currentColumn = 0;
// During first iteration it is updated when moving to second row
// After first iteration some column width are already calculated and are set as max,
// because all rows need to have same column count (except last one)
int maxColumnCount = m_legendColumnWidths.size(); int maxColumnCount = m_legendColumnWidths.size();
if (m_chartItem->series().isEmpty()) { if (m_chartItem->series().isEmpty()) {
for (int i=0 ; i < m_designLabels.size() ; ++i) { for (int i=0 ; i < m_designLabels.size() ; ++i) {
@ -1143,32 +1150,45 @@ bool AbstractSeriesChart::calculateLegendColumnWidths(qreal indicatorWidth, qrea
return true; return true;
} }
bool AbstractSeriesChart::calculateLegendSingleColumnWidth(qreal &currentRowWidth, int &currentColumn, int &maxColumnCount, const qreal itemWidth, const qreal maxWidth) bool AbstractSeriesChart::calculateLegendSingleColumnWidth(qreal &currentRowWidth, int &currentColumn, int &maxColumnCount,
const qreal itemWidth, const qreal maxRowWidth)
{ {
// TODO_ES optimise ?, it's hard to read const bool maxColumnCountDefined = maxColumnCount > 0;
const bool isEnoughSpaceInRowForItem = currentRowWidth + itemWidth > maxWidth; // Check if there is enough space for current item in the row
const bool lastColumnReached = (maxColumnCount > 0 && currentColumn >= maxColumnCount); const bool isEnoughSpaceInRowForItem = currentRowWidth + itemWidth > maxRowWidth;
// Check if it is last column already
const bool lastColumnReached = (maxColumnCountDefined && currentColumn >= maxColumnCount);
if (isEnoughSpaceInRowForItem || lastColumnReached) { if (isEnoughSpaceInRowForItem || lastColumnReached) {
// Move to next row
currentColumn = 0; currentColumn = 0;
if (maxColumnCount == 0) { // Set column count when moving to second row (next rows cannot have more columns)
if (!maxColumnCountDefined) {
maxColumnCount = currentColumn + 1; maxColumnCount = currentColumn + 1;
} }
currentRowWidth = itemWidth; currentRowWidth = itemWidth;
} else { } else {
// Add next column in the row
currentRowWidth += itemWidth; currentRowWidth += itemWidth;
} }
// Add new column or update already existing column width
if (currentColumn >= m_legendColumnWidths.size()) { if (currentColumn >= m_legendColumnWidths.size()) {
// Append new column
m_legendColumnWidths.append(itemWidth); m_legendColumnWidths.append(itemWidth);
} else if (m_legendColumnWidths.at(currentColumn) < itemWidth) { } else if (m_legendColumnWidths.at(currentColumn) < itemWidth) {
// Update size if item in column is bigger than items in same column in previous rows
m_legendColumnWidths[currentColumn] = itemWidth; m_legendColumnWidths[currentColumn] = itemWidth;
qreal tmpWidth = itemWidth; // After any updating column size we must recheck if all columns fit in the max row width
qreal rowWidth = itemWidth;
for (int c = 1 ; c < m_legendColumnWidths.size() ; c++) { for (int c = 1 ; c < m_legendColumnWidths.size() ; c++) {
tmpWidth += m_legendColumnWidths.at(c); rowWidth += m_legendColumnWidths.at(c);
if (tmpWidth > maxWidth) { // When column widths exceed max row width remove columns at the end
if (rowWidth > maxRowWidth) {
m_legendColumnWidths.remove(c, m_legendColumnWidths.size() - c); m_legendColumnWidths.remove(c, m_legendColumnWidths.size() - c);
break; break;
} }
} }
// Return back and re-iterate from start to make sure everything fits
return false; return false;
} }
++currentColumn; ++currentColumn;

View File

@ -124,7 +124,7 @@ protected:
private: private:
bool calculateLegendColumnWidths(qreal indicatorWidth, qreal maxWidth, const QFontMetrics &fm); bool calculateLegendColumnWidths(qreal indicatorWidth, qreal maxWidth, const QFontMetrics &fm);
bool calculateLegendSingleColumnWidth(qreal &currentRowWidth, int &currentColumn, int &maxColumnCount, bool calculateLegendSingleColumnWidth(qreal &currentRowWidth, int &currentColumn, int &maxColumnCount,
const qreal itemWidth, const qreal maxWidth); const qreal itemWidth, const qreal maxRowWidth);
AxisData m_yAxisData, m_xAxisData; AxisData m_yAxisData, m_xAxisData;
qreal m_designValues [9]; qreal m_designValues [9];
}; };