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

© 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