Changeset View
Changeset View
Standalone View
Standalone View
src/common/KReportDesign_p.cpp
Show All 14 Lines | |||||
15 | * | 15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | 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/>. | 17 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "KReportDesign_p.h" | 20 | #include "KReportDesign_p.h" | ||
21 | #include "KReportElement.h" | 21 | #include "KReportElement.h" | ||
22 | #include "KReportUtils.h" | 22 | #include "KReportUtils.h" | ||
23 | #include "KReportUtils_p.h" | ||||
23 | #include "KReportPluginManager.h" | 24 | #include "KReportPluginManager.h" | ||
24 | #include "KReportPluginInterface.h" | 25 | #include "KReportPluginInterface.h" | ||
25 | 26 | | |||
26 | #include <QDebug> | 27 | #include <QDebug> | ||
27 | #include <QDomDocument> | 28 | #include <QDomDocument> | ||
28 | #include <QDomElement> | 29 | #include <QDomElement> | ||
29 | #include <QSizeF> | 30 | #include <QSizeF> | ||
30 | 31 | | |||
31 | const bool DEFAULT_SHOW_GRID = true; | | |||
32 | const bool DEFAULT_SNAP_TO_GRID = true; | | |||
33 | const int DEFAULT_GRID_DIVISIONS = 4; | | |||
34 | const KReportUnit::Type DEFAULT_UNIT_TYPE = KReportUnit::Type::Centimeter; | | |||
35 | const KReportUnit DEFAULT_UNIT(DEFAULT_UNIT_TYPE); | | |||
36 | const int DEFAULT_PAGE_MARGIN = CM_TO_POINT(1.0); | | |||
37 | const QPageSize::PageSizeId DEFAULT_PAGE_SIZE = QPageSize::A4; | | |||
38 | const QPageLayout::Orientation DEFAULT_PAGE_ORIENTATION = QPageLayout::Landscape; | | |||
39 | | ||||
40 | KReportDesign::Private::Private(KReportDesign *design) | 32 | KReportDesign::Private::Private(KReportDesign *design) | ||
41 | : q(design) | 33 | : q(design) | ||
42 | , showGrid(DEFAULT_SHOW_GRID) | 34 | , showGrid(DEFAULT_SHOW_GRID) | ||
43 | , snapToGrid(DEFAULT_SNAP_TO_GRID) | 35 | , snapToGrid(DEFAULT_SNAP_TO_GRID) | ||
44 | , gridDivisions(DEFAULT_GRID_DIVISIONS) | 36 | , gridDivisions(DEFAULT_GRID_DIVISIONS) | ||
45 | , pageUnit(DEFAULT_UNIT) | 37 | , pageUnit(DEFAULT_UNIT) | ||
46 | , sections(static_cast<int>(KReportSection::Type::Detail)) | 38 | , sections(static_cast<int>(KReportSection::Type::Detail)) | ||
47 | { | 39 | { | ||
48 | memset(static_cast<void*>(sections.data()), 0, sizeof(void*) * sections.length()); | 40 | memset(static_cast<void*>(sections.data()), 0, sizeof(void*) * sections.length()); | ||
49 | pageLayout.setUnits(QPageLayout::Point); // initializate because of https://bugreports.qt.io/browse/QTBUG-47551 | 41 | pageLayout.setUnits(QPageLayout::Point); // initializate because of https://bugreports.qt.io/browse/QTBUG-47551 | ||
50 | } | 42 | } | ||
51 | 43 | | |||
52 | KReportDesign::Private::~Private() | 44 | KReportDesign::Private::~Private() | ||
53 | { | 45 | { | ||
54 | qDeleteAll(sections); | 46 | qDeleteAll(sections); | ||
55 | } | 47 | } | ||
56 | 48 | | |||
57 | KReportDesignGlobal::KReportDesignGlobal() | 49 | KReportDesignGlobal::KReportDesignGlobal() | ||
58 | : defaultSectionHeight(CM_TO_POINT(2.0)) | 50 | : defaultSectionHeight(CM_TO_POINT(2.0)) | ||
59 | , defaultSectionBackgroundColor(Qt::white) | 51 | , defaultSectionBackgroundColor(Qt::white) | ||
60 | { | 52 | { | ||
61 | defaultPageLayout.setUnits(QPageLayout::Point); | 53 | defaultPageLayout.setUnits(QPageLayout::Point); | ||
62 | defaultPageLayout.setMargins(QMarginsF(DEFAULT_PAGE_MARGIN, | 54 | defaultPageLayout.setMargins(QMarginsF(DEFAULT_PAGE_MARGIN_PT, | ||
63 | DEFAULT_PAGE_MARGIN, | 55 | DEFAULT_PAGE_MARGIN_PT, | ||
64 | DEFAULT_PAGE_MARGIN, | 56 | DEFAULT_PAGE_MARGIN_PT, | ||
65 | DEFAULT_PAGE_MARGIN)); | 57 | DEFAULT_PAGE_MARGIN_PT)); | ||
66 | defaultPageLayout.setMode(QPageLayout::StandardMode); | 58 | defaultPageLayout.setMode(QPageLayout::StandardMode); | ||
67 | defaultPageLayout.setOrientation(DEFAULT_PAGE_ORIENTATION); | 59 | defaultPageLayout.setOrientation(DEFAULT_PAGE_ORIENTATION); | ||
68 | } | 60 | } | ||
69 | 61 | | |||
70 | KReportSection::Type KReportDesignGlobal::sectionType(const QString& typeName) | 62 | KReportSection::Type KReportDesignGlobal::sectionType(const QString& typeName) | ||
71 | { | 63 | { | ||
72 | initSectionTypes(); | 64 | initSectionTypes(); | ||
73 | return sectionTypesForName.value(typeName); // returns Invalid type for invalid name | 65 | return sectionTypesForName.value(typeName); // returns Invalid type for invalid name | ||
staniek: typefor -> type for | |||||
74 | } | 66 | } | ||
75 | 67 | | |||
76 | inline uint qHash(KReportSection::Type sectionType, uint seed = 0) | 68 | inline uint qHash(KReportSection::Type sectionType, uint seed = 0) | ||
77 | { | 69 | { | ||
78 | return qHash(static_cast<uint>(sectionType), seed); | 70 | return qHash(static_cast<uint>(sectionType), seed); | ||
79 | } | 71 | } | ||
80 | 72 | | |||
81 | QString KReportDesignGlobal::sectionTypeName(KReportSection::Type sectionType) | 73 | QString KReportDesignGlobal::sectionTypeName(KReportSection::Type sectionType) | ||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Line(s) | 129 | { | |||
138 | if (node.isElement()) { | 130 | if (node.isElement()) { | ||
139 | return true; | 131 | return true; | ||
140 | } | 132 | } | ||
141 | setStatus(status, QString::fromLatin1("Element expected inside of <%1>") | 133 | setStatus(status, QString::fromLatin1("Element expected inside of <%1>") | ||
142 | .arg(node.parentNode().toElement().tagName()), node); | 134 | .arg(node.parentNode().toElement().tagName()), node); | ||
143 | return false; | 135 | return false; | ||
144 | } | 136 | } | ||
145 | 137 | | |||
146 | static void setNoAttributeStatus(const QDomElement &el, const char *attrName, KReportDesignReadingStatus *status) | 138 | static void setNoAttributeStatus(const QDomElement &el, const QString &attrName, KReportDesignReadingStatus *status) | ||
147 | { | 139 | { | ||
148 | setStatus(status, QString::fromLatin1("Attribute \"%1\" expected inside of <%1>") | 140 | setStatus(status, QString::fromLatin1("Attribute \"%1\" expected inside of <%1>") | ||
staniek: Mistake: `<%1>` -> `<%2>` | |||||
149 | .arg(QLatin1String(attrName)).arg(el.tagName()), el); | 141 | .arg(attrName).arg(el.tagName()), el); | ||
150 | } | 142 | } | ||
151 | 143 | | |||
152 | #if 0 // TODO unused for now | 144 | #if 0 // TODO unused for now | ||
153 | static bool checkAttribute(const QDomElement &el, const char *attrName, KReportDesignReadingStatus *status) | 145 | static bool checkAttribute(const QDomElement &el, const char *attrName, KReportDesignReadingStatus *status) | ||
154 | { | 146 | { | ||
155 | if (el.hasAttribute(QLatin1String(attrName))) { | 147 | if (el.hasAttribute(QLatin1String(attrName))) { | ||
156 | return true; | 148 | return true; | ||
157 | } | 149 | } | ||
158 | setNoAttributeStatus(el, attrName, status); | 150 | setNoAttributeStatus(el, attrName, status); | ||
159 | return false; | 151 | return false; | ||
160 | } | 152 | } | ||
161 | #endif | 153 | #endif | ||
162 | 154 | | |||
163 | KReportSection KReportDesign::Private::processSectionElement(const QDomElement &el, | 155 | KReportSection KReportDesign::Private::processSectionElement(const QDomElement &el, | ||
164 | KReportDesignReadingStatus *status) | 156 | KReportDesignReadingStatus *status) | ||
165 | { | 157 | { | ||
166 | const QString sectionTypeName = KReportUtils::attr(el, "report:section-type", QString()); | 158 | qDebug() << el.nodeName(); | ||
167 | KReportSection::Type sectionType = s_global->sectionType(sectionTypeName); | 159 | const KReportSection::Type sectionType | ||
160 | = s_global->sectionType(KReportUtils::readSectionTypeNameAttribute(el)); | ||||
168 | if (sectionType == KReportSection::Type::Invalid) { | 161 | if (sectionType == KReportSection::Type::Invalid) { | ||
169 | setStatus(status, | 162 | setStatus(status, | ||
170 | QString::fromLatin1("Invalid value of report:section-type=\"%1\" in element <%2>") | 163 | QString::fromLatin1( | ||
171 | .arg(sectionTypeName).arg(el.tagName()), el); | 164 | "Invalid value of report:section-type=\"%1\" in element <%2>") | ||
165 | .arg(KReportUtils::readSectionTypeNameAttribute(el)) | ||||
166 | .arg(el.tagName()), | ||||
167 | el); | ||||
172 | return KReportSection(); | 168 | return KReportSection(); | ||
173 | } | 169 | } | ||
174 | KReportSection section; | 170 | KReportSection section; | ||
175 | section.setType(sectionType); | 171 | section.setType(sectionType); | ||
176 | qreal height = KReportUtils::attr(el, "svg:height", -1.0); | 172 | const QSizeF size(KReportUtils::readSizeAttributes(el)); | ||
177 | if (height >= 0.0) { | 173 | if (size.height() >= 0) { | ||
178 | section.setHeight(height); | 174 | section.setHeight(size.height()); | ||
179 | } | 175 | } | ||
180 | section.setBackgroundColor(QColor(KReportUtils::attr(el, "fo:background-color", QString()))); | 176 | section.setBackgroundColor( | ||
177 | KReportUtils::attr(el, QLatin1String("fo:background-color"), QColor())); | ||||
181 | for (QDomNode node = el.firstChild(); !node.isNull(); node = node.nextSibling()) { | 178 | for (QDomNode node = el.firstChild(); !node.isNull(); node = node.nextSibling()) { | ||
182 | if (!checkElement(node, status)) { | 179 | if (!checkElement(node, status)) { | ||
183 | return KReportSection(); | 180 | return KReportSection(); | ||
184 | } | 181 | } | ||
185 | KReportElement element = processSectionElementChild(node.toElement(), status); | 182 | KReportElement element = processSectionElementChild(node.toElement(), status); | ||
186 | if (!element.rect().isValid() || (status && status->isError())) { | 183 | if (!element.rect().isValid() || (status && status->isError())) { | ||
187 | return KReportSection(); | 184 | return KReportSection(); | ||
188 | } | 185 | } | ||
Show All 29 Lines | 205 | { | |||
218 | KReportPluginInterface *plugin = findPlugin(QLatin1String(reportElementName), el, status); | 215 | KReportPluginInterface *plugin = findPlugin(QLatin1String(reportElementName), el, status); | ||
219 | if (!plugin) { | 216 | if (!plugin) { | ||
220 | return KReportElement(); | 217 | return KReportElement(); | ||
221 | } | 218 | } | ||
222 | KReportElement element = plugin->createElement(); | 219 | KReportElement element = plugin->createElement(); | ||
223 | if (!plugin->loadElement(&element, el, status)) { | 220 | if (!plugin->loadElement(&element, el, status)) { | ||
224 | return KReportElement(); | 221 | return KReportElement(); | ||
225 | } | 222 | } | ||
226 | element.setName(KReportUtils::attr(el, "report:name", QString())); | 223 | element.setName(KReportUtils::readNameAttribute(el)); | ||
227 | if (element.name().isEmpty()) { | 224 | if (element.name().isEmpty()) { | ||
228 | setNoAttributeStatus(el, "report:name", status); | 225 | setNoAttributeStatus(el, QLatin1String("report:name"), status); | ||
229 | return KReportElement(); | 226 | return KReportElement(); | ||
230 | } | 227 | } | ||
231 | return element; | 228 | return element; | ||
232 | } | 229 | } | ||
233 | 230 | | |||
234 | bool KReportDesign::Private::processGroupElement(const QDomElement &el, | 231 | bool KReportDesign::Private::processGroupElement(const QDomElement &el, | ||
235 | KReportDesignReadingStatus *status) | 232 | KReportDesignReadingStatus *status) | ||
236 | { | 233 | { | ||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Line(s) | 341 | { | |||
345 | const QByteArray name = el.tagName().toLatin1(); | 342 | const QByteArray name = el.tagName().toLatin1(); | ||
346 | QPageLayout defaultPageLayout = KReportDesign::defaultPageLayout(); | 343 | QPageLayout defaultPageLayout = KReportDesign::defaultPageLayout(); | ||
347 | //kreportDebug() << name; | 344 | //kreportDebug() << name; | ||
348 | if (name == "report:title") { | 345 | if (name == "report:title") { | ||
349 | title = el.text(); | 346 | title = el.text(); | ||
350 | #ifdef KREPORT_SCRIPTING | 347 | #ifdef KREPORT_SCRIPTING | ||
351 | } else if (name == "report:script") { | 348 | } else if (name == "report:script") { | ||
352 | script = el.firstChildElement().text(); | 349 | script = el.firstChildElement().text(); | ||
353 | originalInterpreter = KReportUtils::attr(el, "report:script-interpreter", QString()); | 350 | originalInterpreter = KReportUtils::attr( | ||
351 | el, QLatin1String("report:script-interpreter"), QString()); | ||||
354 | #endif | 352 | #endif | ||
355 | } else if (name == "report:grid") { | 353 | } else if (name == "report:grid") { | ||
356 | showGrid = KReportUtils::attr(el, "report:grid-visible", DEFAULT_SHOW_GRID); | 354 | showGrid = KReportUtils::attr( | ||
357 | snapToGrid = KReportUtils::attr(el, "report:grid-snap", DEFAULT_SNAP_TO_GRID); | 355 | el, QLatin1String("report:grid-visible"), DEFAULT_SHOW_GRID); | ||
358 | gridDivisions = KReportUtils::attr(el, "report:grid-divisions", DEFAULT_GRID_DIVISIONS); | 356 | snapToGrid = KReportUtils::attr( | ||
359 | const QString pageUnitString = KReportUtils::attr(el, "report:page-unit", QString()); | 357 | el, QLatin1String("report:grid-snap"), DEFAULT_SNAP_TO_GRID); | ||
360 | pageUnit = KReportUnit(KReportUnit::symbolToType(pageUnitString)); | 358 | gridDivisions = KReportUtils::attr( | ||
359 | el, QLatin1String("report:grid-divisions"), DEFAULT_GRID_DIVISIONS); | ||||
360 | const QString pageUnitString | ||||
361 | = KReportUtils::attr(el, QLatin1String("report:page-unit"), QString()); | ||||
362 | | ||||
363 | pageUnit = KReportUnit::symbolToType(pageUnitString); | ||||
361 | if (!pageUnit.isValid()) { | 364 | if (!pageUnit.isValid()) { | ||
362 | pageUnit = DEFAULT_UNIT; | 365 | pageUnit = DEFAULT_UNIT; | ||
363 | if (!pageUnitString.isEmpty()) { | 366 | if (!pageUnitString.isEmpty()) { | ||
364 | qWarning() << "Invalid page unit" << pageUnitString << "specified in" << name | 367 | qWarning() << "Invalid page unit" << pageUnitString << "specified in" << name | ||
365 | << "element, defaulting to" << pageUnit.symbol(); | 368 | << "element, defaulting to" << pageUnit.symbol(); | ||
366 | } | 369 | } | ||
367 | } | 370 | } | ||
368 | } | 371 | } | ||
369 | else if (name == "report:page-style") { // see https://git.reviewboard.kde.org/r/115314 | 372 | else if (name == "report:page-style") { // see https://git.reviewboard.kde.org/r/115314 | ||
370 | const QByteArray pagetype = el.text().toLatin1(); | 373 | const QByteArray pagetype = el.text().toLatin1(); | ||
371 | if (pagetype == "predefined") { | 374 | if (pagetype == "predefined") { | ||
372 | pageLayout.setPageSize( | 375 | pageLayout.setPageSize(KReportUtils::pageSize( | ||
373 | KReportUtils::pageSize(KReportUtils::attr(el, "report:page-size", | 376 | KReportUtils::attr(el, QLatin1String("report:page-size"), | ||
374 | QPageSize(DEFAULT_PAGE_SIZE).key()))); | 377 | QPageSize(DEFAULT_PAGE_SIZE).key()))); | ||
375 | } else if (pagetype.isEmpty() || pagetype == "custom") { | 378 | } else if (pagetype.isEmpty() || pagetype == "custom") { | ||
376 | QSizeF size(KReportUtils::attr(el, "fo:page-width", -1.0), | 379 | QSizeF size(KReportUtils::attr(el, QLatin1String("fo:page-width"), -1.0), | ||
377 | KReportUtils::attr(el, "fo:page-height", -1.0)); | 380 | KReportUtils::attr(el, QLatin1String("fo:page-height"), -1.0)); | ||
378 | if (size.isValid()) { | 381 | if (size.isValid()) { | ||
379 | pageLayout.setPageSize(QPageSize(size, QPageSize::Point)); | 382 | pageLayout.setPageSize(QPageSize(size, QPageSize::Point)); | ||
380 | } else { | 383 | } else { | ||
381 | pageLayout.setPageSize(defaultPageLayout.pageSize()); | 384 | pageLayout.setPageSize(defaultPageLayout.pageSize()); | ||
382 | } | 385 | } | ||
383 | } else if (pagetype == "label") { | 386 | } else if (pagetype == "label") { | ||
384 | //! @todo? | 387 | //! @todo? | ||
385 | pageLayout.setPageSize(defaultPageLayout.pageSize()); | 388 | pageLayout.setPageSize(defaultPageLayout.pageSize()); | ||
386 | } | 389 | } | ||
387 | QMarginsF margins(KReportUtils::attr(el, "fo:margin-left", defaultPageLayout.margins().left()), | 390 | QMarginsF margins(KReportUtils::attr(el, QLatin1String("fo:margin-left"), | ||
388 | KReportUtils::attr(el, "fo:margin-top", defaultPageLayout.margins().top()), | 391 | defaultPageLayout.margins().left()), | ||
389 | KReportUtils::attr(el, "fo:margin-right", defaultPageLayout.margins().right()), | 392 | KReportUtils::attr(el, QLatin1String("fo:margin-top"), | ||
390 | KReportUtils::attr(el, "fo:margin-bottom", defaultPageLayout.margins().bottom())); | 393 | defaultPageLayout.margins().top()), | ||
394 | KReportUtils::attr(el, QLatin1String("fo:margin-right"), | ||||
395 | defaultPageLayout.margins().right()), | ||||
396 | KReportUtils::attr(el, QLatin1String("fo:margin-bottom"), | ||||
397 | defaultPageLayout.margins().bottom())); | ||||
391 | bool b = pageLayout.setMargins(margins); | 398 | bool b = pageLayout.setMargins(margins); | ||
392 | if (!b) { | 399 | if (!b) { | ||
393 | qWarning() << "Failed to set page margins to" << margins; | 400 | qWarning() << "Failed to set page margins to" << margins; | ||
394 | } | 401 | } | ||
395 | const QString s = KReportUtils::attr(el, "report:print-orientation", QString()); | 402 | const QString s = KReportUtils::attr( | ||
403 | el, QLatin1String("report:print-orientation"), QString()); | ||||
396 | if (s == QLatin1String("portrait")) { | 404 | if (s == QLatin1String("portrait")) { | ||
397 | pageLayout.setOrientation(QPageLayout::Portrait); | 405 | pageLayout.setOrientation(QPageLayout::Portrait); | ||
398 | } else if (s == QLatin1String("landscape")) { | 406 | } else if (s == QLatin1String("landscape")) { | ||
399 | pageLayout.setOrientation(QPageLayout::Landscape); | 407 | pageLayout.setOrientation(QPageLayout::Landscape); | ||
400 | } | 408 | } | ||
401 | else { | 409 | else { | ||
402 | pageLayout.setOrientation(defaultPageLayout.orientation()); | 410 | pageLayout.setOrientation(defaultPageLayout.orientation()); | ||
403 | } | 411 | } | ||
▲ Show 20 Lines • Show All 71 Lines • Show Last 20 Lines |
typefor -> type for