KD Reports API Documentation  2.2
KDReportsTableBreakingLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Reports library.
4 **
5 ** SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
12 
13 using namespace KDReports;
14 
16  : m_pages(1)
17 {
18 }
19 
20 //@cond PRIVATE
21 void TableBreakingLogic::setColumnWidths(const WidthVector &widths)
22 {
23  m_widths = widths;
24 }
25 //@endcond
26 
28 {
29  m_pages = pages;
30 }
31 
33 {
34  QVector<int> columnsForPage;
35  if (m_pages == 0)
36  return columnsForPage;
37  const int numColumnsForPage = m_widths.count();
38  qreal totalWidth = 0;
39  for (int i = 0; i < numColumnsForPage; ++i) {
40  totalWidth += m_widths[i];
41  }
42  const qreal idealPageWidth = totalWidth / m_pages;
43  // qDebug() << "idealPageWidth=" << idealPageWidth;
44  qreal currentWidth = 0;
45  columnsForPage.resize(m_pages);
46  int columnsInThisPage = 0;
47  int pageNumber = 0;
48  for (int i = 0; i < numColumnsForPage; ++i) {
49  const qreal columnWidth = m_widths[i];
50  const bool lastPage = pageNumber == m_pages - 1;
51  if (currentWidth + columnWidth > idealPageWidth && !lastPage) {
52  // Better to include or exclude the column?
53  // That's the tricky part. If we exclude too much
54  // the last page will be crammed, while if we include
55  // a huge column this one might be crammed.
56  // Let's have a threshold then: include it if we won't go over-board
57  // more than 1/3rd of the page width.
58  if (currentWidth + columnWidth < idealPageWidth * 4 / 3 || currentWidth == 0) { // Avoid infinite loop if huge column
59  columnsInThisPage += 1;
60  } else {
61  // current column will be for next page.
62  --i;
63  }
64  columnsForPage[pageNumber++] = columnsInThisPage;
65 
66  currentWidth = 0;
67  columnsInThisPage = 0;
68  } else {
69  currentWidth += columnWidth;
70  columnsInThisPage += 1;
71  }
72  }
73  if (pageNumber < m_pages && columnsInThisPage > 0) {
74  columnsForPage[pageNumber++] = columnsInThisPage;
75  }
76  columnsForPage.resize(pageNumber);
77 #ifdef DEBUG_TABLEBREAKINGLOGIC
78  qDebug() << "Result of optimized table breaking:" << columnsForPage;
79 #endif
80  return columnsForPage;
81 }
82 
84 {
85  Q_ASSERT(colPerPage.size() <= m_pages);
86  int startColumn = 0;
87  WidthVector widths;
88  widths.resize(colPerPage.size());
89  for (int page = 0; page < colPerPage.size(); ++page) {
90  const int numColumnsForPage = colPerPage[page];
91  qreal pageTotalWidth = 0;
92  for (int i = startColumn; i < startColumn + numColumnsForPage; ++i) {
93  pageTotalWidth += m_widths[i];
94  }
95  startColumn += numColumnsForPage;
96  widths[page] = pageTotalWidth;
97  }
98  return widths;
99 }
void setColumnWidths(const WidthVector &widths)
WidthVector widthPerPage(const QVector< int > &colPerPage) const

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-reports/
Generated by doxygen 1.9.1