KD Reports API Documentation  2.2
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"
17 #include "KDReportsTextElement.h"
18 
19 #include <QDate>
20 #include <QDebug>
21 #include <QLocale>
22 #include <QTextBlock>
23 #include <QTime>
24 
25 KDReports::Header::Header(KDReports::Report *report)
26  : d(new HeaderPrivate(report))
27 {
28  d->m_textDocument.contentDocument().setDefaultFont(report->d->m_layout->defaultFont());
29 }
30 
31 KDReports::Header::~Header()
32 {
33 }
34 
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 
41 void 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 
47 KDReports::TextDocument &KDReports::Header::doc()
48 {
49  return d->m_textDocument;
50 }
51 
52 static const int VariableTypeProperty = QTextFormat::UserProperty + 246;
53 static const int VariableLengthProperty = QTextFormat::UserProperty + 247;
54 
55 namespace KDReports {
56 QString variableValue(int pageNumber, KDReports::Report *report, VariableType type)
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:
64  return QDate::currentDate().toString(Qt::TextDate);
65  case ISODate:
66  return QDate::currentDate().toString(Qt::ISODate);
68  return QLocale::system().toString(QDate::currentDate(), QLocale::ShortFormat);
70  return QLocale::system().toString(QDate::currentDate(), QLocale::LongFormat);
71  case LocaleDate:
73  return QLocale().toString(QDate::currentDate(), QLocale::ShortFormat);
75  return QLocale().toString(QDate::currentDate(), QLocale::LongFormat);
76  case TextTime:
77  return QTime::currentTime().toString(Qt::TextDate);
78  case ISOTime:
79  return QTime::currentTime().toString(Qt::ISODate);
80  case LocaleTime:
81  return QLocale().toString(QTime::currentTime(), QLocale::ShortFormat);
82  default:
83  qWarning() << "Program error, variable" << type << "not implemented";
84  }
85  return QString();
86 }
87 }
88 
89 void KDReports::setVariableMarker(QTextDocument &textDoc, int pos, KDReports::VariableType variableType, int valueLength)
90 {
91  QTextCursor c(&textDoc);
92  c.setPosition(pos);
93  c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
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 
101 void KDReports::cleanupVariableProperties(QTextCharFormat &charFormat)
102 {
103  charFormat.clearProperty(ResizableImageProperty);
104  charFormat.clearProperty(VariableTypeProperty);
105  charFormat.clearProperty(VariableLengthProperty);
106 }
107 
109 {
110  d->m_builder.addVariablePublic(variable);
111 }
112 
114 {
115  d->m_builder.addVerticalSpacingPublic(space);
116 }
117 
118 void 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 
143 void 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 
152 {
153  return d->m_textDocument.contentDocument().defaultFont();
154 }
155 
157 {
158  d->m_builder.setTabPositions(tabs);
159 }
160 
162 {
163  return d->m_builder.currentPosition();
164 }
static const int VariableLengthProperty
static const int VariableTypeProperty
void addVerticalSpacing(qreal space)
void setDefaultFont(const QFont &font)
void setTabPositions(const QList< QTextOption::Tab > &tabs)
void addInlineElement(const Element &element)
int currentPosition() const
void addVariable(VariableType variable)
void addElement(const Element &element, Qt::AlignmentFlag horizontalAlignment=Qt::AlignLeft)
QFont defaultFont() const
int numberOfPages() const
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.
static const int ResizableImageProperty
void setVariableMarker(QTextDocument &textDoc, int pos, KDReports::VariableType variableType, int valueLength)
void cleanupVariableProperties(QTextCharFormat &charFormat)

© 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