KD Reports API Documentation 2.2
Loading...
Searching...
No Matches
KDReportsHeader.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
11#include "KDReportsHeader.h"
13#include "KDReportsHeader_p.h"
15#include "KDReportsReport.h"
16#include "KDReportsReport_p.h"
18
19#include <QDate>
20#include <QDebug>
21#include <QLocale>
22#include <QTextBlock>
23#include <QTime>
24
25KDReports::Header::Header(KDReports::Report *report)
26 : d(new HeaderPrivate(report))
27{
28 d->m_textDocument.contentDocument().setDefaultFont(report->d->m_layout->defaultFont());
29}
30
31KDReports::Header::~Header()
32{
33}
34
35void KDReports::Header::addInlineElement(const Element &element)
36{
37 d->m_builder.addInlineElementPublic(element);
38 // don't add anything else here, it won't be called from the xml parser
39}
40
41void KDReports::Header::addElement(const Element &element, Qt::AlignmentFlag horizontalAlignment)
42{
43 d->m_builder.addBlockElementPublic(element, horizontalAlignment);
44 // don't add anything else here, it won't be called from the xml parser
45}
46
47KDReports::TextDocument &KDReports::Header::doc()
48{
49 return d->m_textDocument;
50}
51
54
55namespace KDReports {
57{
58 switch (type) {
59 case PageNumber:
60 return QString::number(pageNumber + 1);
61 case PageCount:
62 return QString::number(report->numberOfPages());
63 case TextDate:
65 case ISODate:
71 case LocaleDate:
76 case TextTime:
78 case ISOTime:
80 case LocaleTime:
82 default:
83 qWarning() << "Program error, variable" << type << "not implemented";
84 }
85 return QString();
86}
87}
88
89void KDReports::setVariableMarker(QTextDocument &textDoc, int pos, KDReports::VariableType variableType, int valueLength)
90{
91 QTextCursor c(&textDoc);
92 c.setPosition(pos);
94 QTextCharFormat charFormat = c.charFormat();
95 charFormat.clearProperty(ResizableImageProperty);
96 charFormat.setProperty(VariableTypeProperty, variableType);
97 charFormat.setProperty(VariableLengthProperty, valueLength);
98 c.setCharFormat(charFormat);
99}
100
102{
103 charFormat.clearProperty(ResizableImageProperty);
106}
107
108void KDReports::Header::addVariable(VariableType variable)
109{
110 d->m_builder.addVariablePublic(variable);
111}
112
113void KDReports::Header::addVerticalSpacing(qreal space)
114{
115 d->m_builder.addVerticalSpacingPublic(space);
116}
117
118void KDReports::Header::preparePaintingPage(int pageNumber)
119{
120 // qDebug() << "preparePaintingPage" << pageNumber;
121 QTextCursor c(&d->m_textDocument.contentDocument());
122 do {
123 c.movePosition(QTextCursor::NextCharacter);
124 // qDebug() << c.block().text() << c.position();
125 QTextCharFormat format = c.charFormat();
126 if (format.hasProperty(VariableTypeProperty)) {
127 // go back one char, due to charFormat() being the format of the character -before- the cursor!
128 c.movePosition(QTextCursor::PreviousCharacter);
129 const VariableType variableType = static_cast<VariableType>(format.property(VariableTypeProperty).toInt());
130 const int oldLength = format.property(VariableLengthProperty).toInt();
131 // qDebug() << "Found variable type" << variableType << "length" << oldLength << "at pos" << c.position();
132 const QString value = KDReports::variableValue(pageNumber, d->m_report, variableType);
133 const int startPos = c.position();
134 c.setPosition(c.position() + oldLength, QTextCursor::KeepAnchor);
135 // qDebug() << "inserting text" << value << "with format" << c.charFormat().font();
136 c.insertText(value); // update variable value
137 // update marker
138 setVariableMarker(d->m_textDocument.contentDocument(), startPos, variableType, value.length());
139 }
140 } while (!c.atEnd());
141}
142
143void KDReports::Header::setDefaultFont(const QFont &font)
144{
145 QFont f(font);
146 f.setStyleStrategy(QFont::ForceOutline); // bitmap fonts look awful in printed documents
147 d->m_textDocument.contentDocument().setDefaultFont(f);
148 d->m_builder.setDefaultFont(f);
149}
150
151QFont KDReports::Header::defaultFont() const
152{
153 return d->m_textDocument.contentDocument().defaultFont();
154}
155
156void KDReports::Header::setTabPositions(const QList<QTextOption::Tab> &tabs)
157{
158 d->m_builder.setTabPositions(tabs);
159}
160
161int KDReports::Header::currentPosition() const
162{
163 return d->m_builder.currentPosition();
164}
static const int VariableLengthProperty
static const int VariableTypeProperty
QString variableValue(int pageNumber, KDReports::Report *report, VariableType type)
@ ISODate
names but unlocalized order of names and numbers
@ TextTime
Current time in text format like "13:42:59".
@ PageNumber
Page number.
@ SystemLocaleShortDate
Current date formatted according to the system locale, short format, see Qt::SystemLocaleShortDate.
@ LocaleDate
Current date in locale-dependent format, deprecated in favour of SystemLocaleShortDate or SystemLocal...
@ LocaleTime
Current time in locale-dependent format.
@ PageCount
Page count.
@ DefaultLocaleShortDate
Current date formatted according to the application locale, short format, see Qt::DefaultLocaleShortD...
@ ISOTime
Current time in ISO 8601 format like "13:42:59".
@ DefaultLocaleLongDate
Current date formatted according to the application locale, long format, see Qt::DefaultLocaleLongDat...
@ SystemLocaleLongDate
Current date formatted according to the system locale, long format, see Qt::SystemLocaleLongDate.
void setVariableMarker(QTextDocument &textDoc, int pos, KDReports::VariableType variableType, int valueLength)
void cleanupVariableProperties(QTextCharFormat &charFormat)
QDate currentDate()
QString toString(Qt::DateFormat format) const const
QLocale system()
QString toString(qlonglong i) const const
int length() const const
QString number(int n, int base)
AlignmentFlag
TextDate
QTextCharFormat charFormat() const const
bool movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode, int n)
void setCharFormat(const QTextCharFormat &format)
void setPosition(int pos, QTextCursor::MoveMode m)
void clearProperty(int propertyId)
bool hasProperty(int propertyId) const const
QVariant property(int propertyId) const const
void setProperty(int propertyId, const QVariant &value)
QTime currentTime()
QString toString(Qt::DateFormat format) const const
int toInt(bool *ok) const 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 on Wed Apr 24 2024 04:08:14 for KD Reports API Documentation by doxygen 1.9.8