KD Reports API Documentation  2.2
KDReportsTextElement.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 "KDReportsTextElement.h"
12 #include "KDReportsHeader_p.h" // for cleanupVariableProperties
14 #include <QDebug>
15 
16 class KDReports::TextElementPrivate
17 {
18 public:
19  QString m_string;
20  QString m_id;
21 
22  QString m_fontFamily;
23  bool m_bold = false;
24  bool m_boldIsSet = false;
25  bool m_italic = false;
26  bool m_italicIsSet = false;
27  bool m_underline = false;
28  bool m_underlineIsSet = false;
29  bool m_strikeout = false;
30  bool m_strikeoutIsSet = false;
31  bool m_fontSet = false;
32  qreal m_pointSize = 0;
33  QColor m_foreground;
34  QFont m_font;
35 };
36 
38  : Element()
39  , d(new TextElementPrivate)
40 {
41  d->m_string = string;
42 }
43 
45  : Element(other)
46  , d(new TextElementPrivate(*other.d))
47 {
48 }
49 
51 {
52  if (&other == this)
53  return *this;
54  Element::operator=(other);
55  *d = *other.d;
56  return *this;
57 }
58 
60 {
61 }
62 
64 {
65  QTextCursor &cursor = builder.cursor();
66  const int charPosition = cursor.position();
67  QTextCharFormat charFormat = cursor.charFormat();
69  if (d->m_fontSet) {
70 #if QT_VERSION >= QT_VERSION_CHECK(5, 3, 0)
71  charFormat.setFont(d->m_font, QTextCharFormat::FontPropertiesSpecifiedOnly);
72 #else
73  charFormat.setFont(d->m_font);
74 #endif
75  }
76  if (d->m_boldIsSet)
77  charFormat.setFontWeight(d->m_bold ? QFont::Bold : QFont::Normal);
78  if (d->m_italicIsSet)
79  charFormat.setFontItalic(d->m_italic);
80  if (d->m_underlineIsSet)
81  charFormat.setFontUnderline(d->m_underline);
82  if (d->m_strikeoutIsSet)
83  charFormat.setFontStrikeOut(d->m_strikeout);
84  if (d->m_pointSize > 0)
85  charFormat.setFontPointSize(d->m_pointSize);
86  if (!d->m_fontFamily.isEmpty())
87 #if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0)
88  charFormat.setFontFamilies({d->m_fontFamily});
89 #else
90  charFormat.setFontFamily(d->m_fontFamily);
91 #endif
92  if (d->m_foreground.isValid())
93  charFormat.setForeground(d->m_foreground);
94  else
95  charFormat.clearForeground();
96  if (background().style() != Qt::NoBrush)
97  charFormat.setBackground(background());
98  else
99  charFormat.clearBackground();
100  cursor.setCharFormat(charFormat);
101  cursor.insertText(d->m_string);
102  if (!d->m_id.isEmpty())
103  builder.currentDocumentData().setTextValueMarker(charPosition, d->m_id, d->m_string.length(), false);
104 }
105 
107 {
108  d->m_string.append(str);
109  return *this;
110 }
111 
112 void KDReports::TextElement::setText(const QString &text)
113 {
114  d->m_string = text;
115 }
116 
117 void KDReports::TextElement::setFontFamily(const QString &family)
118 {
119  d->m_fontFamily = family;
120 }
121 
123 {
124  d->m_bold = bold;
125  d->m_boldIsSet = true;
126 }
127 
129 {
130  d->m_italic = italic;
131  d->m_italicIsSet = true;
132 }
133 
135 {
136  d->m_underline = underline;
137  d->m_underlineIsSet = true;
138 }
139 
141 {
142  d->m_strikeout = strikeout;
143  d->m_strikeoutIsSet = true;
144 }
145 
147 {
148  d->m_pointSize = size;
149 }
150 
151 void KDReports::TextElement::setFont(const QFont &font)
152 {
153  d->m_font = font;
154  d->m_fontSet = true;
155 }
156 
157 void KDReports::TextElement::setTextColor(const QColor &color)
158 {
159  d->m_foreground = color;
160 }
161 
163 {
164  return d->m_foreground;
165 }
166 
168 {
169  return new TextElement(*this);
170 }
171 
172 void KDReports::TextElement::setId(const QString &id)
173 {
174  d->m_id = id;
175 }
176 
178 {
179  return d->m_id;
180 }
181 
183 {
184  return d->m_string;
185 }
186 
188 {
189  return d->m_font;
190 }
Element & operator=(const Element &other)
TextDocumentData & currentDocumentData()
void setTextValueMarker(int pos, const QString &id, int valueLength, bool html)
TextElement & operator=(const TextElement &other)
void setTextColor(const QColor &color)
void setFontFamily(const QString &family)
Set font attribute: family.
void setUnderline(bool underline)
Set font attribute: underline.
void setStrikeOut(bool strikeout)
Set font attribute: strike out.
void setItalic(bool italic)
Set font attribute: italic.
void setText(const QString &text)
TextElement(const QString &string=QString())
void setPointSize(qreal size)
Set font attribute: size in points. Can be integer or decimal.
void setBold(bool bold)
Set font attribute: bold.
void build(ReportBuilder &builder) const override
TextElement & operator<<(const QString &)
Element * clone() const override
void setId(const QString &id)
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