Changeset View
Changeset View
Standalone View
Standalone View
src/common/KReportSectionData.cpp
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com) | 2 | * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com) | ||
3 | * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk) | 3 | * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk) | ||
4 | * Copyright (C) 2010 Jarosław Staniek <staniek@kde.org> | 4 | * Copyright (C) 2010-2018 Jarosław Staniek <staniek@kde.org> | ||
5 | * | 5 | * | ||
6 | * This library is free software; you can redistribute it and/or | 6 | * This library is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | 7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | 8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | 9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | 10 | * | ||
11 | * This library is distributed in the hope that it will be useful, | 11 | * This library is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
Show All 15 Lines | |||||
28 | #include "KReportUtils.h" | 28 | #include "KReportUtils.h" | ||
29 | #include "KReportUtils_p.h" | 29 | #include "KReportUtils_p.h" | ||
30 | #include "kreport_debug.h" | 30 | #include "kreport_debug.h" | ||
31 | 31 | | |||
32 | #include <KPropertySet> | 32 | #include <KPropertySet> | ||
33 | 33 | | |||
34 | #include <QDomElement> | 34 | #include <QDomElement> | ||
35 | 35 | | |||
36 | KReportSectionData::KReportSectionData(QObject *parent) : KReportSectionData(QDomElement(), parent) | 36 | class Q_DECL_HIDDEN KReportSectionData::Private | ||
37 | { | 37 | { | ||
38 | public: | ||||
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(); | ||||
38 | } | 50 | } | ||
39 | 51 | | |||
40 | KReportSectionData::KReportSectionData(const QDomElement & elemSource, QObject *parent) | 52 | ~Private() | ||
41 | : QObject(parent) | | |||
42 | , m_unit(DEFAULT_UNIT_TYPE) | | |||
43 | { | 53 | { | ||
44 | if (elemSource.isNull()) { | 54 | qDeleteAll(objects); | ||
45 | m_type = Type::None; | | |||
46 | } else { | | |||
47 | setObjectName(elemSource.tagName()); | | |||
48 | m_type = sectionTypeFromString(KReportUtils::readSectionTypeNameAttribute(elemSource)); | | |||
49 | } | 55 | } | ||
50 | 56 | | |||
51 | createProperties(elemSource); | 57 | void setHeight(qreal ptHeight, KProperty::ValueOptions options); | ||
52 | 58 | | |||
53 | if (elemSource.isNull()) { | 59 | static bool zLessThan(KReportItemBase* s1, KReportItemBase* s2); | ||
54 | m_valid = true; | 60 | static bool xLessThan(KReportItemBase* s1, KReportItemBase* s2); | ||
55 | } else { | 61 | | ||
56 | loadXml(elemSource); | 62 | KReportSectionData * const q; | ||
63 | KPropertySet set; | ||||
64 | KProperty *backgroundColor; | ||||
65 | KProperty *height; | ||||
66 | QList<KReportItemBase *> objects; | ||||
67 | KReportUnit unit = KReportUnit(DEFAULT_UNIT_TYPE); | ||||
68 | Type type = Type::None; | ||||
69 | bool valid = true; | ||||
70 | | ||||
71 | private: | ||||
72 | void createProperties(const QDomElement &elemSource); | ||||
73 | void loadXml(const QDomElement &elemSource); | ||||
74 | }; | ||||
75 | | ||||
76 | KReportSectionData::KReportSectionData(QObject *parent) | ||||
77 | : KReportSectionData(QDomElement(), parent) | ||||
78 | { | ||||
57 | } | 79 | } | ||
58 | m_set->clearModifiedFlags(); | 80 | | ||
81 | KReportSectionData::KReportSectionData(const QDomElement & elemSource, QObject *parent) | ||||
82 | : QObject(parent), d(new Private(this, elemSource)) | ||||
83 | { | ||||
59 | } | 84 | } | ||
60 | 85 | | |||
61 | void KReportSectionData::loadXml(const QDomElement &elemSource) | 86 | void KReportSectionData::Private::loadXml(const QDomElement &elemSource) | ||
62 | { | 87 | { | ||
63 | if (objectName() != QLatin1String("report:section") || m_type == KReportSectionData::Type::None) { | 88 | if (q->objectName() != QLatin1String("report:section") || type == KReportSectionData::Type::None) { | ||
64 | m_valid = false; | 89 | valid = false; | ||
65 | return; | 90 | return; | ||
66 | } | 91 | } | ||
67 | 92 | | |||
68 | KReportPluginManager* manager = KReportPluginManager::self(); | 93 | KReportPluginManager* manager = KReportPluginManager::self(); | ||
69 | 94 | | |||
70 | QDomNodeList section = elemSource.childNodes(); | 95 | QDomNodeList section = elemSource.childNodes(); | ||
71 | for (int nodeCounter = 0; nodeCounter < section.count(); nodeCounter++) { | 96 | for (int nodeCounter = 0; nodeCounter < section.count(); nodeCounter++) { | ||
72 | QDomElement elemThis = section.item(nodeCounter).toElement(); | 97 | QDomElement elemThis = section.item(nodeCounter).toElement(); | ||
Show All 10 Lines | 107 | if (plugin) { | |||
83 | QObject *obj = plugin->createRendererInstance(elemThis); | 108 | QObject *obj = plugin->createRendererInstance(elemThis); | ||
84 | if (obj) { | 109 | if (obj) { | ||
85 | krobj = dynamic_cast<KReportItemBase*>(obj); | 110 | krobj = dynamic_cast<KReportItemBase*>(obj); | ||
86 | } | 111 | } | ||
87 | } | 112 | } | ||
88 | } | 113 | } | ||
89 | if (krobj) { | 114 | if (krobj) { | ||
90 | krobj->propertySet()->clearModifiedFlags(); | 115 | krobj->propertySet()->clearModifiedFlags(); | ||
91 | m_objects.append(krobj); | 116 | objects.append(krobj); | ||
92 | } else { | 117 | } else { | ||
93 | kreportWarning() << "Could not create element of type" << reportItemName; | 118 | kreportWarning() << "Could not create element of type" << reportItemName; | ||
94 | } | 119 | } | ||
95 | } else { | 120 | } else { | ||
96 | kreportWarning() << "While parsing section encountered an unknown element:" << n; | 121 | kreportWarning() << "While parsing section encountered an unknown element:" << n; | ||
97 | } | 122 | } | ||
98 | } | 123 | } | ||
99 | qSort(m_objects.begin(), m_objects.end(), zLessThan); | 124 | qSort(objects.begin(), objects.end(), zLessThan); | ||
100 | m_valid = true; | 125 | valid = true; | ||
101 | } | 126 | } | ||
102 | 127 | | |||
103 | KReportSectionData::~KReportSectionData() | 128 | KReportSectionData::~KReportSectionData() | ||
104 | { | 129 | { | ||
105 | delete m_set; | 130 | delete d; | ||
106 | qDeleteAll(m_objects); | | |||
107 | } | 131 | } | ||
108 | 132 | | |||
109 | KReportUnit KReportSectionData::unit() const | 133 | KReportUnit KReportSectionData::unit() const | ||
110 | { | 134 | { | ||
111 | return m_unit; | 135 | return d->unit; | ||
112 | } | 136 | } | ||
113 | 137 | | |||
114 | void KReportSectionData::setUnit(const KReportUnit &u) | 138 | void KReportSectionData::setUnit(const KReportUnit &u) | ||
115 | { | 139 | { | ||
116 | if (m_unit == u) { | 140 | if (d->unit == u) { | ||
117 | return; | 141 | return; | ||
118 | } | 142 | } | ||
119 | // convert values | 143 | // convert values | ||
120 | KReportUnit oldunit = m_unit; | 144 | KReportUnit oldunit = d->unit; | ||
121 | m_unit = u; | 145 | d->unit = u; | ||
122 | 146 | | |||
123 | m_height->setValue(KReportUnit::convertFromUnitToUnit(m_height->value().toReal(), oldunit, u), | 147 | d->height->setValue(KReportUnit::convertFromUnitToUnit(d->height->value().toReal(), oldunit, u), | ||
124 | KProperty::ValueOption::IgnoreOld); | 148 | KProperty::ValueOption::IgnoreOld); | ||
125 | m_height->setOption("suffix", u.symbol()); | 149 | d->height->setOption("suffix", u.symbol()); | ||
126 | } | 150 | } | ||
127 | 151 | | |||
128 | bool KReportSectionData::zLessThan(KReportItemBase* s1, KReportItemBase* s2) | 152 | bool KReportSectionData::Private::zLessThan(KReportItemBase* s1, KReportItemBase* s2) | ||
129 | { | 153 | { | ||
130 | return s1->z() < s2->z(); | 154 | return s1->z() < s2->z(); | ||
131 | } | 155 | } | ||
132 | 156 | | |||
133 | bool KReportSectionData::xLessThan(KReportItemBase* s1, KReportItemBase* s2) | 157 | bool KReportSectionData::Private::xLessThan(KReportItemBase* s1, KReportItemBase* s2) | ||
134 | { | 158 | { | ||
135 | return s1->position().toPoint().x() < s2->position().toPoint().x(); | 159 | return s1->position().toPoint().x() < s2->position().toPoint().x(); | ||
136 | } | 160 | } | ||
137 | 161 | | |||
138 | void KReportSectionData::createProperties(const QDomElement & elemSource) | 162 | void KReportSectionData::Private::createProperties(const QDomElement & elemSource) | ||
139 | { | 163 | { | ||
140 | m_set = new KPropertySet(this); | 164 | KReportDesigner::addMetaProperties(&set, KReportSectionData::tr("Section", "Report section"), | ||
141 | KReportDesigner::addMetaProperties(m_set, | 165 | QLatin1String("kreport-section-element")); | ||
142 | tr("Section", "Report section"), QLatin1String("kreport-section-element")); | 166 | height = new KProperty("height", 0.0, tr("Height")); | ||
143 | 167 | backgroundColor = new KProperty( | |||
144 | m_height = new KProperty("height", 0.0, tr("Height")); | | |||
145 | m_backgroundColor = new KProperty( | | |||
146 | "background-color", | 168 | "background-color", | ||
147 | KReportUtils::attr(elemSource, QLatin1String("fo:background-color"), QColor(Qt::white)), | 169 | KReportUtils::attr(elemSource, QLatin1String("fo:background-color"), QColor(Qt::white)), | ||
148 | tr("Background Color")); | 170 | tr("Background Color")); | ||
149 | m_height->setOption("unit", QLatin1String("cm")); | 171 | height->setOption("unit", QLatin1String("cm")); | ||
150 | if (!elemSource.isNull()) { | 172 | if (!elemSource.isNull()) { | ||
151 | m_height->setValue(m_unit.convertFromPoint( | 173 | height->setValue(unit.convertFromPoint( | ||
152 | KReportUtils::readSizeAttributes( | 174 | KReportUtils::readSizeAttributes( | ||
153 | elemSource, QSizeF(DEFAULT_SECTION_SIZE_PT, DEFAULT_SECTION_SIZE_PT)) | 175 | elemSource, QSizeF(DEFAULT_SECTION_SIZE_PT, DEFAULT_SECTION_SIZE_PT)) | ||
154 | .height())); | 176 | .height())); | ||
155 | } | 177 | } | ||
156 | 178 | | |||
157 | m_set->addProperty(m_height); | 179 | set.addProperty(height); | ||
158 | m_set->addProperty(m_backgroundColor); | 180 | set.addProperty(backgroundColor); | ||
159 | m_set->clearModifiedFlags(); | 181 | set.clearModifiedFlags(); | ||
160 | } | 182 | } | ||
161 | 183 | | |||
162 | QString KReportSectionData::name() const | 184 | QString KReportSectionData::name() const | ||
163 | { | 185 | { | ||
164 | return (objectName() + QLatin1Char('-') + sectionTypeString(m_type)); | 186 | return (objectName() + QLatin1Char('-') + sectionTypeString(d->type)); | ||
165 | } | 187 | } | ||
166 | 188 | | |||
167 | QString KReportSectionData::sectionTypeString(KReportSectionData::Type type) | 189 | QString KReportSectionData::sectionTypeString(KReportSectionData::Type type) | ||
168 | { | 190 | { | ||
169 | //! @todo use QMap | 191 | //! @todo use QMap | ||
170 | QString sectiontype; | 192 | QString sectiontype; | ||
171 | switch (type) { | 193 | switch (type) { | ||
172 | case KReportSectionData::Type::PageHeaderAny: | 194 | case KReportSectionData::Type::PageHeaderAny: | ||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Line(s) | 248 | //! @todo use QMap | |||
259 | else | 281 | else | ||
260 | type = KReportSectionData::Type::None; | 282 | type = KReportSectionData::Type::None; | ||
261 | 283 | | |||
262 | return type; | 284 | return type; | ||
263 | } | 285 | } | ||
264 | 286 | | |||
265 | qreal KReportSectionData::height() const | 287 | qreal KReportSectionData::height() const | ||
266 | { | 288 | { | ||
267 | return m_unit.convertToPoint(m_height->value().toReal()); | 289 | return d->unit.convertToPoint(d->height->value().toReal()); | ||
268 | } | 290 | } | ||
269 | 291 | | |||
270 | void KReportSectionData::setHeight(qreal ptHeight, KProperty::ValueOptions options) | 292 | void KReportSectionData::setHeight(qreal ptHeight, KProperty::ValueOptions options) | ||
271 | { | 293 | { | ||
272 | m_height->setValue(m_unit.convertFromPoint(ptHeight), options); | 294 | d->height->setValue(d->unit.convertFromPoint(ptHeight), options); | ||
273 | } | 295 | } | ||
274 | 296 | | |||
275 | void KReportSectionData::setHeight(qreal ptHeight) | 297 | void KReportSectionData::setHeight(qreal ptHeight) | ||
276 | { | 298 | { | ||
277 | setHeight(ptHeight, KProperty::ValueOptions()); | 299 | setHeight(ptHeight, KProperty::ValueOptions()); | ||
278 | } | 300 | } | ||
301 | | ||||
302 | KPropertySet* KReportSectionData::propertySet() | ||||
303 | { | ||||
304 | return &d->set; | ||||
305 | } | ||||
306 | | ||||
307 | const KPropertySet* KReportSectionData::propertySet() const | ||||
308 | { | ||||
309 | return &d->set; | ||||
310 | } | ||||
311 | | ||||
312 | bool KReportSectionData::isValid() const | ||||
313 | { | ||||
314 | return d->valid; | ||||
315 | } | ||||
316 | | ||||
317 | QList<KReportItemBase*> KReportSectionData::objects() const | ||||
318 | { | ||||
319 | return d->objects; | ||||
320 | } | ||||
321 | | ||||
322 | QColor KReportSectionData::backgroundColor() const | ||||
323 | { | ||||
324 | return d->backgroundColor->value().value<QColor>(); | ||||
325 | } | ||||
326 | | ||||
327 | void KReportSectionData::setBackgroundColor(const QColor &color) | ||||
328 | { | ||||
329 | d->backgroundColor->setValue(color); | ||||
330 | } | ||||
331 | | ||||
332 | KReportSectionData::Type KReportSectionData::type() const | ||||
333 | { | ||||
334 | return d->type; | ||||
335 | } | ||||
336 | | ||||
337 | KReportItemBase* KReportSectionData::object(int index) | ||||
338 | { | ||||
339 | return d->objects.value(index); | ||||
340 | } |