KD Reports API Documentation  2.0
KDReportsTextDocReportLayout.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 #include <QAbstractTextDocumentLayout>
19 #include <QDebug>
20 #include <QPainter>
21 #include <QTextBlock>
22 
24  : m_textDocument()
25  , m_builder(m_textDocument.contentDocumentData(), QTextCursor(&m_textDocument.contentDocument()), report)
26 
27 {
28 }
29 
31 
32 void KDReports::TextDocReportLayout::paintPageContent(int pageNumber, QPainter &painter)
33 {
34  painter.translate(0, -pageNumber * m_textDocument.contentDocument().pageSize().height());
35 
36  // Instead of using drawContents directly, we have to fork it in order to fix the palette (to avoid white-on-white in dark color schemes)
37  // m_textDocument.contentDocument().drawContents(&painter, painter.clipRegion().boundingRect());
38  // This even allows us to optimize it a bit (painter clip rect already set)
39 
40  QAbstractTextDocumentLayout::PaintContext ctx;
41  ctx.clip = painter.clipRegion().boundingRect();
42  ctx.palette.setColor(QPalette::Text, Qt::black);
43  m_textDocument.contentDocument().documentLayout()->draw(&painter, ctx);
44 }
45 
46 //@cond PRIVATE
48 {
49  // qDebug() << "page height" << m_textDocument.contentDocument().pageSize().height();
50  // qDebug() << "doc height" << m_textDocument.contentDocument().size().height();
51  return m_textDocument.contentDocument().pageCount();
52 }
53 //@endcond
54 
56 {
57  m_textDocument.setPageSize(size);
58 }
59 
61 
63 {
64  return m_textDocument.asHtml();
65 }
66 
68 {
69  m_textDocument.layoutWithTextWidth(docWidth);
70  qreal docHeight = m_textDocument.contentDocument().size().height();
71 
72  // We need to get rid of all page breaks...
73  // Unittest: PageLayout::testEndlessPrinterWithPageBreaks()
74  QTextCursor c(&m_textDocument.contentDocument());
75  c.beginEditBlock();
76  QTextBlock block = m_textDocument.contentDocument().firstBlock();
77  do {
78  // qDebug() << "addBlock: Looking at block" << block.blockNumber();
79  QTextBlockFormat format = block.blockFormat();
80  if (format.pageBreakPolicy() != QTextBlockFormat::PageBreak_Auto)
81  format.setPageBreakPolicy(QTextBlockFormat::PageBreak_Auto);
82  c.setPosition(block.position());
83  c.setBlockFormat(format);
84  block = block.next();
85  } while (block.isValid());
86  c.endEditBlock();
87 
88  setPageContentSize(QSizeF(docWidth, docHeight));
89  qDebug() << "m_textDocument.layoutDocument().setPageSize" << docWidth << "x" << docHeight << numberOfPages() << "pages";
90  qreal newDocHeight = m_textDocument.contentDocument().size().height();
91  if (newDocHeight > docHeight) {
92  // QTextDocument is playing tricks on us; it was able to layout as docWidth x docHeight
93  // but once we set that as the page size, we end up with more height...
94  // Unittest: PageLayout::testEndlessPrinterBug()
95  qDebug() << "newDocHeight=" << newDocHeight << "expected" << docHeight;
96  setPageContentSize(QSizeF(docWidth, newDocHeight));
97  newDocHeight = m_textDocument.contentDocument().size().height();
98  qDebug() << "final newDocHeight=" << newDocHeight << numberOfPages() << "pages";
99  }
100 
101  /* cppcheck-suppress assertWithSideEffect */
102  Q_ASSERT(numberOfPages() == 1);
103  return newDocHeight;
104 }
105 
107 {
108  m_textDocument.contentDocumentData().saveResourcesToFiles();
109 }
110 
112 {
113  m_textDocument.contentDocument().setDefaultFont(font);
114  m_builder.setDefaultFont(font);
115 }
116 
118 {
119  return m_textDocument.defaultFont();
120 }
121 
122 void KDReports::TextDocReportLayout::updateTextValue(const QString &id, const QString &newValue)
123 {
124  m_textDocument.updateTextValue(id, newValue);
125 }
126 
127 //@cond PRIVATE
129 {
130  // See Database example for the +1, the right border was missing otherwise.
131  return m_textDocument.contentDocument().idealWidth() + 1; // in pixels
132 }
133 
134 bool KDReports::TextDocReportLayout::scaleTo(int numPagesHorizontally, int numPagesVertically)
135 {
136  Q_UNUSED(numPagesHorizontally);
137  Q_UNUSED(numPagesVertically);
138  qWarning("scaleTo is only implemented in Spreadsheet mode currently");
139  return false;
140 }
141 
143 {
144  Q_UNUSED(height);
145  qWarning("fixed row height is only implemented in Spreadsheet mode");
146 }
147 
149 {
150  return 1;
151 }
152 
154 {
155  return 1; // not implemented
156 }
157 //@endcond
158 
160 {
161  Q_UNUSED(factor);
162  qWarning("font scaling is only implemented in Spreadsheet mode currently");
163 }
164 
166 {
167  return 1; // not implemented
168 }
169 
170 QString KDReports::TextDocReportLayout::anchorAt(int pageNumber, QPoint pos)
171 {
172  const QPointF posInPage = pos + QPointF(0, pageNumber * m_textDocument.contentDocument().pageSize().height());
173  return m_textDocument.contentDocument().documentLayout()->anchorAt(posInPage);
174 }
KDReports::TextDocReportLayout::ensureLayouted
void ensureLayouted() override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:60
KDReports::TextDocReportLayout::setDefaultFont
void setDefaultFont(const QFont &font) override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:111
KDReports::TextDocReportLayout::maximumNumberOfPagesForVerticalScaling
int maximumNumberOfPagesForVerticalScaling() const override
Reimplemented for internal purposes. .
KDReports::TextDocReportLayout::idealWidth
qreal idealWidth() override
Reimplemented for internal purposes. .
KDReportsTextDocReportLayout_p.h
KDReports::TextDocReportLayout::layoutAsOnePage
qreal layoutAsOnePage(qreal width) override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:67
KDReports::TextDocReportLayout::toHtml
QString toHtml() const override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:62
KDReports::TextDocReportLayout::TextDocReportLayout
TextDocReportLayout(KDReports::Report *report)
Definition: KDReportsTextDocReportLayout.cpp:23
KDReports::TextDocReportLayout::defaultFont
QFont defaultFont() const override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:117
KDReports::Report
Definition: KDReportsReport.h:80
KDReports::TextDocReportLayout::finishHtmlExport
void finishHtmlExport() override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:106
KDReports::TextDocReportLayout::maximumNumberOfPagesForHorizontalScaling
int maximumNumberOfPagesForHorizontalScaling() const override
Reimplemented for internal purposes. .
KDReports::TextDocReportLayout::numberOfPages
int numberOfPages() override
Reimplemented for internal purposes. .
KDReports::TextDocReportLayout::setLayoutDirty
void setLayoutDirty() override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:30
KDReports::TextDocReportLayout::updateTextValue
void updateTextValue(const QString &id, const QString &newValue) override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:122
KDReports::TextDocReportLayout::setUserRequestedFontScalingFactor
void setUserRequestedFontScalingFactor(qreal factor) override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:159
KDReports::TextDocReportLayout::setPageContentSize
void setPageContentSize(QSizeF size) override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:55
KDReports::TextDocReportLayout::anchorAt
QString anchorAt(int pageNumber, QPoint pos) override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:170
KDReports::TextDocReportLayout::scaleTo
bool scaleTo(int numPagesHorizontally, int numPagesVertically) override
Reimplemented for internal purposes. .
KDReports::TextDocReportLayout::userRequestedFontScalingFactor
qreal userRequestedFontScalingFactor() const override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:165
KDReports::TextDocReportLayout::setFixedRowHeight
void setFixedRowHeight(qreal height) override
Reimplemented for internal purposes. .
KDReports::TextDocReportLayout::paintPageContent
void paintPageContent(int pageNumber, QPainter &painter) override
Reimplemented for internal purposes. .
Definition: KDReportsTextDocReportLayout.cpp:32

© 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