KReport

KReportSectionData.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
3 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
4 * Copyright (C) 2010-2018 Jarosław Staniek <staniek@kde.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "KReportSectionData.h"
21#include "KReportDesigner.h"
22#include "KReportDocument.h"
23#include "KReportItemLine.h"
24#include "KReportPluginInterface.h"
25#include "KReportPluginManager.h"
26#include "KReportItemLine.h"
27#include "KReportDesigner.h"
28#include "KReportUtils.h"
29#include "KReportUtils_p.h"
30#include "kreport_debug.h"
31
32#include <KPropertySet>
33
34#include <QDomElement>
35
36class Q_DECL_HIDDEN KReportSectionData::Private
37{
38public:
39 explicit Private(KReportSectionData *data, const QDomElement &elemSource) : q(data)
40 {
41 if (!elemSource.isNull()) {
42 q->setObjectName(elemSource.tagName());
43 type = sectionTypeFromString(KReportUtils::readSectionTypeNameAttribute(elemSource));
44 }
45 createProperties(elemSource);
46 if (!elemSource.isNull()) {
47 loadXml(elemSource);
48 }
49 set.clearModifiedFlags();
50 }
51
52 ~Private()
53 {
54 qDeleteAll(objects);
55 }
56
57 void setHeight(qreal ptHeight, KProperty::ValueOptions options);
58
59 static bool zLessThan(KReportItemBase* s1, KReportItemBase* s2);
60 static bool xLessThan(KReportItemBase* s1, KReportItemBase* s2);
61
62 KReportSectionData * const q;
63 KPropertySet set;
64 KProperty *backgroundColor;
65 KProperty *height;
67 KReportUnit unit = KReportUnit(DEFAULT_UNIT_TYPE);
68 Type type = Type::None;
69 bool valid = true;
70
71private:
72 void createProperties(const QDomElement &elemSource);
73 void loadXml(const QDomElement &elemSource);
74};
75
76KReportSectionData::KReportSectionData(QObject *parent)
78{
79}
80
81KReportSectionData::KReportSectionData(const QDomElement & elemSource, QObject *parent)
82 : QObject(parent), d(new Private(this, elemSource))
83{
84}
85
86void KReportSectionData::Private::loadXml(const QDomElement &elemSource)
87{
88 if (q->objectName() != QLatin1String("report:section") || type == KReportSectionData::Type::None) {
89 valid = false;
90 return;
91 }
92
93 KReportPluginManager* manager = KReportPluginManager::self();
94
95 QDomNodeList section = elemSource.childNodes();
96 for (int nodeCounter = 0; nodeCounter < section.count(); nodeCounter++) {
97 QDomElement elemThis = section.item(nodeCounter).toElement();
98 QString n = elemThis.tagName();
99 if (n.startsWith(QLatin1String("report:"))) {
100 KReportItemBase *krobj = nullptr;
101 QString reportItemName = n.mid(qstrlen("report:"));
102 if (reportItemName == QLatin1String("line")) {
103 KReportItemLine * line = new KReportItemLine(elemThis);
104 krobj = line;
105 } else {
106 KReportPluginInterface *plugin = manager->plugin(reportItemName);
107 if (plugin) {
108 QObject *obj = plugin->createRendererInstance(elemThis);
109 if (obj) {
110 krobj = dynamic_cast<KReportItemBase*>(obj);
111 }
112 }
113 }
114 if (krobj) {
115 krobj->propertySet()->clearModifiedFlags();
116 objects.append(krobj);
117 } else {
118 kreportWarning() << "Could not create element of type" << reportItemName;
119 }
120 } else {
121 kreportWarning() << "While parsing section encountered an unknown element:" << n;
122 }
123 }
124 std::sort(objects.begin(), objects.end(), zLessThan);
125 valid = true;
126}
127
128KReportSectionData::~KReportSectionData()
129{
130 delete d;
131}
132
133KReportUnit KReportSectionData::unit() const
134{
135 return d->unit;
136}
137
138void KReportSectionData::setUnit(const KReportUnit &u)
139{
140 if (d->unit == u) {
141 return;
142 }
143 // convert values
144 KReportUnit oldunit = d->unit;
145 d->unit = u;
146
147 d->height->setValue(KReportUnit::convertFromUnitToUnit(d->height->value().toReal(), oldunit, u),
148 KProperty::ValueOption::IgnoreOld);
149 d->height->setOption("suffix", u.symbol());
150}
151
152bool KReportSectionData::Private::zLessThan(KReportItemBase* s1, KReportItemBase* s2)
153{
154 return s1->z() < s2->z();
155}
156
157bool KReportSectionData::Private::xLessThan(KReportItemBase* s1, KReportItemBase* s2)
158{
159 return s1->position().toPoint().x() < s2->position().toPoint().x();
160}
161
162void KReportSectionData::Private::createProperties(const QDomElement & elemSource)
163{
164 KReportDesigner::addMetaProperties(&set, KReportSectionData::tr("Section", "Report section"),
165 QLatin1String("kreport-section-element"));
166 height = new KProperty("height", 0.0, tr("Height"));
167 backgroundColor = new KProperty(
168 "background-color",
169 KReportUtils::attr(elemSource, QLatin1String("fo:background-color"), QColor(Qt::white)),
170 tr("Background Color"));
171 height->setOption("unit", QLatin1String("cm"));
172 if (!elemSource.isNull()) {
173 height->setValue(unit.convertFromPoint(
174 KReportUtils::readSizeAttributes(
175 elemSource, QSizeF(DEFAULT_SECTION_SIZE_PT, DEFAULT_SECTION_SIZE_PT))
176 .height()));
177 }
178
179 set.addProperty(height);
180 set.addProperty(backgroundColor);
181 set.clearModifiedFlags();
182}
183
184QString KReportSectionData::name() const
185{
186 return (objectName() + QLatin1Char('-') + sectionTypeString(d->type));
187}
188
189QString KReportSectionData::sectionTypeString(KReportSectionData::Type type)
190{
191//! @todo use QMap
192 QString sectiontype;
193 switch (type) {
194 case KReportSectionData::Type::PageHeaderAny:
195 sectiontype = QLatin1String("header-page-any");
196 break;
197 case KReportSectionData::Type::PageHeaderEven:
198 sectiontype = QLatin1String("header-page-even");
199 break;
200 case KReportSectionData::Type::PageHeaderOdd:
201 sectiontype = QLatin1String("header-page-odd");
202 break;
203 case KReportSectionData::Type::PageHeaderFirst:
204 sectiontype = QLatin1String("header-page-first");
205 break;
206 case KReportSectionData::Type::PageHeaderLast:
207 sectiontype = QLatin1String("header-page-last");
208 break;
209 case KReportSectionData::Type::PageFooterAny:
210 sectiontype = QLatin1String("footer-page-any");
211 break;
212 case KReportSectionData::Type::PageFooterEven:
213 sectiontype = QLatin1String("footer-page-even");
214 break;
215 case KReportSectionData::Type::PageFooterOdd:
216 sectiontype = QLatin1String("footer-page-odd");
217 break;
218 case KReportSectionData::Type::PageFooterFirst:
219 sectiontype = QLatin1String("footer-page-first");
220 break;
221 case KReportSectionData::Type::PageFooterLast:
222 sectiontype = QLatin1String("footer-page-last");
223 break;
224 case KReportSectionData::Type::ReportHeader:
225 sectiontype = QLatin1String("header-report");
226 break;
227 case KReportSectionData::Type::ReportFooter:
228 sectiontype = QLatin1String("footer-report");
229 break;
230 case KReportSectionData::Type::GroupHeader:
231 sectiontype = QLatin1String("group-header");
232 break;
233 case KReportSectionData::Type::GroupFooter:
234 sectiontype = QLatin1String("group-footer");
235 break;
236 case KReportSectionData::Type::Detail:
237 sectiontype = QLatin1String("detail");
238 break;
239 default:
240 break;
241 }
242
243 return sectiontype;
244}
245
246KReportSectionData::Type KReportSectionData::sectionTypeFromString(const QString& s)
247{
248//! @todo use QMap
249 KReportSectionData::Type type;
250 //kreportDebug() << "Determining section type for " << s;
251 if (s == QLatin1String("header-page-any"))
252 type = KReportSectionData::Type::PageHeaderAny;
253 else if (s == QLatin1String("header-page-even"))
254 type = KReportSectionData::Type::PageHeaderEven;
255 else if (s == QLatin1String("header-page-odd"))
256 type = KReportSectionData::Type::PageHeaderOdd;
257 else if (s == QLatin1String("header-page-first"))
258 type = KReportSectionData::Type::PageHeaderFirst;
259 else if (s == QLatin1String("header-page-last"))
260 type = KReportSectionData::Type::PageHeaderLast;
261 else if (s == QLatin1String("header-report"))
262 type = KReportSectionData::Type::ReportHeader;
263 else if (s == QLatin1String("footer-page-any"))
264 type = KReportSectionData::Type::PageFooterAny;
265 else if (s == QLatin1String("footer-page-even"))
266 type = KReportSectionData::Type::PageFooterEven;
267 else if (s == QLatin1String("footer-page-odd"))
268 type = KReportSectionData::Type::PageFooterOdd;
269 else if (s == QLatin1String("footer-page-first"))
270 type = KReportSectionData::Type::PageFooterFirst;
271 else if (s == QLatin1String("footer-page-last"))
272 type = KReportSectionData::Type::PageFooterLast;
273 else if (s == QLatin1String("footer-report"))
274 type = KReportSectionData::Type::ReportFooter;
275 else if (s == QLatin1String("group-header"))
276 type = KReportSectionData::Type::GroupHeader;
277 else if (s == QLatin1String("group-footer"))
278 type = KReportSectionData::Type::GroupFooter;
279 else if (s == QLatin1String("detail"))
280 type = KReportSectionData::Type::Detail;
281 else
282 type = KReportSectionData::Type::None;
283
284 return type;
285}
286
287qreal KReportSectionData::height() const
288{
289 return d->unit.convertToPoint(d->height->value().toReal());
290}
291
292void KReportSectionData::setHeight(qreal ptHeight, KProperty::ValueOptions options)
293{
294 d->height->setValue(d->unit.convertFromPoint(ptHeight), options);
295}
296
297void KReportSectionData::setHeight(qreal ptHeight)
298{
299 setHeight(ptHeight, KProperty::ValueOptions());
300}
301
303{
304 return &d->set;
305}
306
308{
309 return &d->set;
310}
311
312bool KReportSectionData::isValid() const
313{
314 return d->valid;
315}
316
317QList<KReportItemBase*> KReportSectionData::objects() const
318{
319 return d->objects;
320}
321
322QColor KReportSectionData::backgroundColor() const
323{
324 return d->backgroundColor->value().value<QColor>();
325}
326
327void KReportSectionData::setBackgroundColor(const QColor &color)
328{
329 d->backgroundColor->setValue(color);
330}
331
332KReportSectionData::Type KReportSectionData::type() const
333{
334 return d->type;
335}
336
337KReportItemBase* KReportSectionData::object(int index)
338{
339 return d->objects.value(index);
340}
void clearModifiedFlags()
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
void setOption(const char *name, const QVariant &val)
static void addMetaProperties(KPropertySet *set, const QString &classString, const QString &iconName)
Adds meta-properties to the property set set for consumption by property editor.
Base class for items that are drawn syncronously.
QPointF position() const
Return the position in points.
qreal z() const
Return the z-value in points.
An interface for plugins delivering KReport elements.
virtual QObject * createRendererInstance(const QDomNode &element)=0
Manager class for finding and loading available plugins.
KReportSectionData is used to store the information about a specific report section.
KPropertySet * propertySet()
Returns property set for this section.
static KReportSectionData::Type sectionTypeFromString(const QString &s)
static QString sectionTypeString(KReportSectionData::Type type)
Converts between different units.
Definition KReportUnit.h:71
qreal convertFromPoint(qreal ptValue) const
static qreal convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor=1.0)
convert the given value directly from one unit to another with high accuracy
static QString symbol(KReportUnit::Type type)
Returns the symbol string of given unit type Symbol for Invalid type is empty string.
qreal convertToPoint(qreal value) const
Type type(const QSqlDatabase &db)
QString tagName() const const
QDomNodeList childNodes() const const
bool isNull() const const
QDomElement toElement() const const
int count() const const
QDomNode item(int index) const const
void append(QList< T > &&value)
iterator begin()
iterator end()
T value(qsizetype i) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
int x() const const
QPoint toPoint() const const
QString mid(qsizetype position, qsizetype n) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
qreal toReal(bool *ok) const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:21:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.