KD Reports API Documentation  2.0
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-2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDAB-KDReports OR LicenseRef-KDAB-KDReports-US
8 **
9 ** Licensees holding valid commercial KD Reports licenses may use this file in
10 ** accordance with the KD Reports Commercial License Agreement provided with
11 ** the Software.
12 **
13 ** Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 **
15 ****************************************************************************/
16 
18 
19 using namespace KDReports;
20 
22  : m_pages(1)
23 {
24 }
25 
26 //@cond PRIVATE
27 void TableBreakingLogic::setColumnWidths(const WidthVector &widths)
28 {
29  m_widths = widths;
30 }
31 //@endcond
32 
34 {
35  m_pages = pages;
36 }
37 
39 {
40  QVector<int> columnsForPage;
41  if (m_pages == 0)
42  return columnsForPage;
43  const int numColumnsForPage = m_widths.count();
44  qreal totalWidth = 0;
45  for (int i = 0; i < numColumnsForPage; ++i) {
46  totalWidth += m_widths[i];
47  }
48  const qreal idealPageWidth = totalWidth / m_pages;
49  // qDebug() << "idealPageWidth=" << idealPageWidth;
50  qreal currentWidth = 0;
51  columnsForPage.resize(m_pages);
52  int columnsInThisPage = 0;
53  int pageNumber = 0;
54  for (int i = 0; i < numColumnsForPage; ++i) {
55  const qreal columnWidth = m_widths[i];
56  const bool lastPage = pageNumber == m_pages - 1;
57  if (currentWidth + columnWidth > idealPageWidth && !lastPage) {
58  // Better to include or exclude the column?
59  // That's the tricky part. If we exclude too much
60  // the last page will be crammed, while if we include
61  // a huge column this one might be crammed.
62  // Let's have a threshold then: include it if we won't go over-board
63  // more than 1/3rd of the page width.
64  if (currentWidth + columnWidth < idealPageWidth * 4 / 3 || currentWidth == 0) { // Avoid infinite loop if huge column
65  columnsInThisPage += 1;
66  } else {
67  // current column will be for next page.
68  --i;
69  }
70  columnsForPage[pageNumber++] = columnsInThisPage;
71 
72  currentWidth = 0;
73  columnsInThisPage = 0;
74  } else {
75  currentWidth += columnWidth;
76  columnsInThisPage += 1;
77  }
78  }
79  if (pageNumber < m_pages && columnsInThisPage > 0) {
80  columnsForPage[pageNumber++] = columnsInThisPage;
81  }
82  columnsForPage.resize(pageNumber);
83 #ifdef DEBUG_TABLEBREAKINGLOGIC
84  qDebug() << "Result of optimized table breaking:" << columnsForPage;
85 #endif
86  return columnsForPage;
87 }
88 
90 {
91  Q_ASSERT(colPerPage.size() <= m_pages);
92  int startColumn = 0;
93  WidthVector widths;
94  widths.resize(colPerPage.size());
95  for (int page = 0; page < colPerPage.size(); ++page) {
96  const int numColumnsForPage = colPerPage[page];
97  qreal pageTotalWidth = 0;
98  for (int i = startColumn; i < startColumn + numColumnsForPage; ++i) {
99  pageTotalWidth += m_widths[i];
100  }
101  startColumn += numColumnsForPage;
102  widths[page] = pageTotalWidth;
103  }
104  return widths;
105 }
KDReportsTableBreakingLogic_p.h
KDReports::TableBreakingLogic::TableBreakingLogic
TableBreakingLogic()
Definition: KDReportsTableBreakingLogic.cpp:21
KDReports::TableBreakingLogic::widthPerPage
WidthVector widthPerPage(const QVector< int > &colPerPage) const
Definition: KDReportsTableBreakingLogic.cpp:89
KDReports::TableBreakingLogic::setColumnWidths
void setColumnWidths(const WidthVector &widths)
KDReports::TableBreakingLogic::setPageCount
void setPageCount(int pages)
Definition: KDReportsTableBreakingLogic.cpp:33
KDReports::TableBreakingLogic::WidthVector
QVector< qreal > WidthVector
Definition: KDReportsTableBreakingLogic_p.h:59
KDReports::TableBreakingLogic::columnsPerPage
QVector< int > columnsPerPage() const
Definition: KDReportsTableBreakingLogic.cpp:38
KDReports
Definition: KDReportsAbstractReportLayout_p.h:30

© 2007-2021 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 on Sat Jan 8 2022 02:38:32 for KD Reports API Documentation by doxygen 1.8.17