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 | initSectionTypes(); | 63 | initSectionTypes(); | ||
72 | return sectionTypesForName.value(typeName); // returns Invalid typefor invalid name | 64 | return sectionTypesForName.value(typeName); // returns Invalid typefor invalid name | ||
staniek: typefor -> type for | |||||
73 | } | 65 | } | ||
74 | 66 | | |||
75 | inline uint qHash(KReportSection::Type sectionType, uint seed = 0) | 67 | inline uint qHash(KReportSection::Type sectionType, uint seed = 0) | ||
76 | { | 68 | { | ||
77 | return qHash(static_cast<uint>(sectionType), seed); | 69 | return qHash(static_cast<uint>(sectionType), seed); | ||
78 | } | 70 | } | ||
79 | 71 | | |||
80 | QString KReportDesignGlobal::sectionTypeName(KReportSection::Type sectionType) { | 72 | QString KReportDesignGlobal::sectionTypeName(KReportSection::Type sectionType) { | ||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Line(s) | 126 | { | |||
135 | if (node.isElement()) { | 127 | if (node.isElement()) { | ||
136 | return true; | 128 | return true; | ||
137 | } | 129 | } | ||
138 | setStatus(status, QString::fromLatin1("Element expected inside of <%1>") | 130 | setStatus(status, QString::fromLatin1("Element expected inside of <%1>") | ||
139 | .arg(node.parentNode().toElement().tagName()), node); | 131 | .arg(node.parentNode().toElement().tagName()), node); | ||
140 | return false; | 132 | return false; | ||
141 | } | 133 | } | ||
142 | 134 | | |||
143 | static void setNoAttributeStatus(const QDomElement &el, const char *attrName, KReportDesignReadingStatus *status) | 135 | static void setNoAttributeStatus(const QDomElement &el, const QString &attrName, KReportDesignReadingStatus *status) | ||
144 | { | 136 | { | ||
145 | setStatus(status, QString::fromLatin1("Attribute \"%1\" expected inside of <%1>") | 137 | setStatus(status, QString::fromLatin1("Attribute \"%1\" expected inside of <%1>") | ||
staniek: Mistake: `<%1>` -> `<%2>` | |||||
146 | .arg(QLatin1String(attrName)).arg(el.tagName()), el); | 138 | .arg(attrName).arg(el.tagName()), el); | ||
147 | } | 139 | } | ||
148 | 140 | | |||
149 | #if 0 // TODO unused for now | 141 | #if 0 // TODO unused for now | ||
150 | static bool checkAttribute(const QDomElement &el, const char *attrName, KReportDesignReadingStatus *status) | 142 | static bool checkAttribute(const QDomElement &el, const char *attrName, KReportDesignReadingStatus *status) | ||
151 | { | 143 | { | ||
152 | if (el.hasAttribute(QLatin1String(attrName))) { | 144 | if (el.hasAttribute(QLatin1String(attrName))) { | ||
153 | return true; | 145 | return true; | ||
154 | } | 146 | } | ||
155 | setNoAttributeStatus(el, attrName, status); | 147 | setNoAttributeStatus(el, attrName, status); | ||
156 | return false; | 148 | return false; | ||
157 | } | 149 | } | ||
158 | #endif | 150 | #endif | ||
159 | 151 | | |||
160 | KReportSection KReportDesign::Private::processSectionElement(const QDomElement &el, | 152 | KReportSection KReportDesign::Private::processSectionElement(const QDomElement &el, | ||
161 | KReportDesignReadingStatus *status) | 153 | KReportDesignReadingStatus *status) | ||
162 | { | 154 | { | ||
163 | const QString sectionTypeName = KReportUtils::attr(el, "report:section-type", QString()); | 155 | qDebug() << el.nodeName(); | ||
164 | KReportSection::Type sectionType = s_global->sectionType(sectionTypeName); | 156 | const KReportSection::Type sectionType | ||
157 | = s_global->sectionType(KReportUtils::readSectionTypeNameAttribute(el)); | ||||
165 | if (sectionType == KReportSection::Type::Invalid) { | 158 | if (sectionType == KReportSection::Type::Invalid) { | ||
166 | setStatus(status, | 159 | setStatus(status, | ||
167 | QString::fromLatin1("Invalid value of report:section-type=\"%1\" in element <%2>") | 160 | QString::fromLatin1( | ||
168 | .arg(sectionTypeName).arg(el.tagName()), el); | 161 | "Invalid value of report:section-type=\"%1\" in element <%2>") | ||
162 | .arg(KReportUtils::readSectionTypeNameAttribute(el)) | ||||
163 | .arg(el.tagName()), | ||||
164 | el); | ||||
169 | return KReportSection(); | 165 | return KReportSection(); | ||
170 | } | 166 | } | ||
171 | KReportSection section; | 167 | KReportSection section; | ||
172 | section.setType(sectionType); | 168 | section.setType(sectionType); | ||
173 | qreal height = KReportUtils::attr(el, "svg:height", -1.0); | 169 | const QSizeF size(KReportUtils::readSizeAttributes(el)); | ||
174 | if (height >= 0.0) { | 170 | if (size.height() >= 0) { | ||
175 | section.setHeight(height); | 171 | section.setHeight(size.height()); | ||
176 | } | 172 | } | ||
177 | section.setBackgroundColor(QColor(KReportUtils::attr(el, "fo:background-color", QString()))); | 173 | section.setBackgroundColor( | ||
174 | KReportUtils::attr(el, QLatin1String("fo:background-color"), QColor())); | ||||
178 | for (QDomNode node = el.firstChild(); !node.isNull(); node = node.nextSibling()) { | 175 | for (QDomNode node = el.firstChild(); !node.isNull(); node = node.nextSibling()) { | ||
179 | if (!checkElement(node, status)) { | 176 | if (!checkElement(node, status)) { | ||
180 | return KReportSection(); | 177 | return KReportSection(); | ||
181 | } | 178 | } | ||
182 | KReportElement element = processSectionElementChild(node.toElement(), status); | 179 | KReportElement element = processSectionElementChild(node.toElement(), status); | ||
183 | if (!element.rect().isValid() || (status && status->isError())) { | 180 | if (!element.rect().isValid() || (status && status->isError())) { | ||
184 | return KReportSection(); | 181 | return KReportSection(); | ||
185 | } | 182 | } | ||
Show All 29 Lines | 202 | { | |||
215 | KReportPluginInterface *plugin = findPlugin(QLatin1String(reportElementName), el, status); | 212 | KReportPluginInterface *plugin = findPlugin(QLatin1String(reportElementName), el, status); | ||
216 | if (!plugin) { | 213 | if (!plugin) { | ||
217 | return KReportElement(); | 214 | return KReportElement(); | ||
218 | } | 215 | } | ||
219 | KReportElement element = plugin->createElement(); | 216 | KReportElement element = plugin->createElement(); | ||
220 | if (!plugin->loadElement(&element, el, status)) { | 217 | if (!plugin->loadElement(&element, el, status)) { | ||
221 | return KReportElement(); | 218 | return KReportElement(); | ||
222 | } | 219 | } | ||
223 | element.setName(KReportUtils::attr(el, "report:name", QString())); | 220 | element.setName(KReportUtils::readNameAttribute(el)); | ||
224 | if (element.name().isEmpty()) { | 221 | if (element.name().isEmpty()) { | ||
225 | setNoAttributeStatus(el, "report:name", status); | 222 | setNoAttributeStatus(el, QLatin1String("report:name"), status); | ||
226 | return KReportElement(); | 223 | return KReportElement(); | ||
227 | } | 224 | } | ||
228 | return element; | 225 | return element; | ||
229 | } | 226 | } | ||
230 | 227 | | |||
231 | bool KReportDesign::Private::processGroupElement(const QDomElement &el, | 228 | bool KReportDesign::Private::processGroupElement(const QDomElement &el, | ||
232 | KReportDesignReadingStatus *status) | 229 | KReportDesignReadingStatus *status) | ||
233 | { | 230 | { | ||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Line(s) | 338 | { | |||
342 | const QByteArray name = el.tagName().toLatin1(); | 339 | const QByteArray name = el.tagName().toLatin1(); | ||
343 | QPageLayout defaultPageLayout = KReportDesign::defaultPageLayout(); | 340 | QPageLayout defaultPageLayout = KReportDesign::defaultPageLayout(); | ||
344 | //kreportDebug() << name; | 341 | //kreportDebug() << name; | ||
345 | if (name == "report:title") { | 342 | if (name == "report:title") { | ||
346 | title = el.text(); | 343 | title = el.text(); | ||
347 | #ifdef KREPORT_SCRIPTING | 344 | #ifdef KREPORT_SCRIPTING | ||
348 | } else if (name == "report:script") { | 345 | } else if (name == "report:script") { | ||
349 | script = el.firstChildElement().text(); | 346 | script = el.firstChildElement().text(); | ||
350 | originalInterpreter = KReportUtils::attr(el, "report:script-interpreter", QString()); | 347 | originalInterpreter = KReportUtils::attr( | ||
348 | el, QLatin1String("report:script-interpreter"), QString()); | ||||
351 | #endif | 349 | #endif | ||
352 | } else if (name == "report:grid") { | 350 | } else if (name == "report:grid") { | ||
353 | showGrid = KReportUtils::attr(el, "report:grid-visible", DEFAULT_SHOW_GRID); | 351 | showGrid = KReportUtils::attr( | ||
354 | snapToGrid = KReportUtils::attr(el, "report:grid-snap", DEFAULT_SNAP_TO_GRID); | 352 | el, QLatin1String("report:grid-visible"), DEFAULT_SHOW_GRID); | ||
355 | gridDivisions = KReportUtils::attr(el, "report:grid-divisions", DEFAULT_GRID_DIVISIONS); | 353 | snapToGrid = KReportUtils::attr( | ||
356 | const QString pageUnitString = KReportUtils::attr(el, "report:page-unit", QString()); | 354 | el, QLatin1String("report:grid-snap"), DEFAULT_SNAP_TO_GRID); | ||
357 | pageUnit = KReportUnit(KReportUnit::symbolToType(pageUnitString)); | 355 | gridDivisions = KReportUtils::attr( | ||
356 | el, QLatin1String("report:grid-divisions"), DEFAULT_GRID_DIVISIONS); | ||||
357 | const QString pageUnitString | ||||
358 | = KReportUtils::attr(el, QLatin1String("report:page-unit"), QString()); | ||||
359 | | ||||
360 | pageUnit = KReportUnit::symbolToType(pageUnitString); | ||||
358 | if (!pageUnit.isValid()) { | 361 | if (!pageUnit.isValid()) { | ||
359 | pageUnit = DEFAULT_UNIT; | 362 | pageUnit = DEFAULT_UNIT; | ||
360 | if (!pageUnitString.isEmpty()) { | 363 | if (!pageUnitString.isEmpty()) { | ||
361 | qWarning() << "Invalid page unit" << pageUnitString << "specified in" << name | 364 | qWarning() << "Invalid page unit" << pageUnitString << "specified in" << name | ||
362 | << "element, defaulting to" << pageUnit.symbol(); | 365 | << "element, defaulting to" << pageUnit.symbol(); | ||
363 | } | 366 | } | ||
364 | } | 367 | } | ||
365 | } | 368 | } | ||
366 | else if (name == "report:page-style") { // see https://git.reviewboard.kde.org/r/115314 | 369 | else if (name == "report:page-style") { // see https://git.reviewboard.kde.org/r/115314 | ||
367 | const QByteArray pagetype = el.text().toLatin1(); | 370 | const QByteArray pagetype = el.text().toLatin1(); | ||
368 | if (pagetype == "predefined") { | 371 | if (pagetype == "predefined") { | ||
369 | pageLayout.setPageSize( | 372 | pageLayout.setPageSize(KReportUtils::pageSize( | ||
370 | KReportUtils::pageSize(KReportUtils::attr(el, "report:page-size", | 373 | KReportUtils::attr(el, QLatin1String("report:page-size"), | ||
371 | QPageSize(DEFAULT_PAGE_SIZE).key()))); | 374 | QPageSize(DEFAULT_PAGE_SIZE).key()))); | ||
372 | } else if (pagetype.isEmpty() || pagetype == "custom") { | 375 | } else if (pagetype.isEmpty() || pagetype == "custom") { | ||
373 | QSizeF size(KReportUtils::attr(el, "fo:page-width", -1.0), | 376 | QSizeF size(KReportUtils::attr(el, QLatin1String("fo:page-width"), -1.0), | ||
374 | KReportUtils::attr(el, "fo:page-height", -1.0)); | 377 | KReportUtils::attr(el, QLatin1String("fo:page-height"), -1.0)); | ||
375 | if (size.isValid()) { | 378 | if (size.isValid()) { | ||
376 | pageLayout.setPageSize(QPageSize(size, QPageSize::Point)); | 379 | pageLayout.setPageSize(QPageSize(size, QPageSize::Point)); | ||
377 | } else { | 380 | } else { | ||
378 | pageLayout.setPageSize(defaultPageLayout.pageSize()); | 381 | pageLayout.setPageSize(defaultPageLayout.pageSize()); | ||
379 | } | 382 | } | ||
380 | } else if (pagetype == "label") { | 383 | } else if (pagetype == "label") { | ||
381 | //! @todo? | 384 | //! @todo? | ||
382 | pageLayout.setPageSize(defaultPageLayout.pageSize()); | 385 | pageLayout.setPageSize(defaultPageLayout.pageSize()); | ||
383 | } | 386 | } | ||
384 | QMarginsF margins(KReportUtils::attr(el, "fo:margin-left", defaultPageLayout.margins().left()), | 387 | QMarginsF margins(KReportUtils::attr(el, QLatin1String("fo:margin-left"), | ||
385 | KReportUtils::attr(el, "fo:margin-top", defaultPageLayout.margins().top()), | 388 | defaultPageLayout.margins().left()), | ||
386 | KReportUtils::attr(el, "fo:margin-right", defaultPageLayout.margins().right()), | 389 | KReportUtils::attr(el, QLatin1String("fo:margin-top"), | ||
387 | KReportUtils::attr(el, "fo:margin-bottom", defaultPageLayout.margins().bottom())); | 390 | defaultPageLayout.margins().top()), | ||
391 | KReportUtils::attr(el, QLatin1String("fo:margin-right"), | ||||
392 | defaultPageLayout.margins().right()), | ||||
393 | KReportUtils::attr(el, QLatin1String("fo:margin-bottom"), | ||||
394 | defaultPageLayout.margins().bottom())); | ||||
388 | bool b = pageLayout.setMargins(margins); | 395 | bool b = pageLayout.setMargins(margins); | ||
389 | if (!b) { | 396 | if (!b) { | ||
390 | qWarning() << "Failed to set page margins to" << margins; | 397 | qWarning() << "Failed to set page margins to" << margins; | ||
391 | } | 398 | } | ||
392 | const QString s = KReportUtils::attr(el, "report:print-orientation", QString()); | 399 | const QString s = KReportUtils::attr( | ||
400 | el, QLatin1String("report:print-orientation"), QString()); | ||||
393 | if (s == QLatin1String("portrait")) { | 401 | if (s == QLatin1String("portrait")) { | ||
394 | pageLayout.setOrientation(QPageLayout::Portrait); | 402 | pageLayout.setOrientation(QPageLayout::Portrait); | ||
395 | } else if (s == QLatin1String("landscape")) { | 403 | } else if (s == QLatin1String("landscape")) { | ||
396 | pageLayout.setOrientation(QPageLayout::Landscape); | 404 | pageLayout.setOrientation(QPageLayout::Landscape); | ||
397 | } | 405 | } | ||
398 | else { | 406 | else { | ||
399 | pageLayout.setOrientation(defaultPageLayout.orientation()); | 407 | pageLayout.setOrientation(defaultPageLayout.orientation()); | ||
400 | } | 408 | } | ||
▲ Show 20 Lines • Show All 71 Lines • Show Last 20 Lines |
typefor -> type for