Changeset View
Changeset View
Standalone View
Standalone View
src/common/KReportUtils.cpp
Show All 23 Lines | |||||
24 | 24 | | |||
25 | #include <KProperty> | 25 | #include <KProperty> | ||
26 | 26 | | |||
27 | #include <QDomDocument> | 27 | #include <QDomDocument> | ||
28 | #include <QDomElement> | 28 | #include <QDomElement> | ||
29 | 29 | | |||
30 | #include <float.h> | 30 | #include <float.h> | ||
31 | 31 | | |||
32 | QString KReportUtils::attr(const QDomElement &el, const char *attrName, | 32 | QString KReportUtils::attr(const QDomElement &el, const QString &attrName, | ||
33 | const QString &defaultValue) | 33 | const QString &defaultValue) | ||
34 | { | 34 | { | ||
35 | const QString val = el.attribute(QLatin1String(attrName)); | 35 | const QString val = el.attribute(attrName); | ||
36 | return val.isEmpty() ? defaultValue : val; | 36 | return val.isEmpty() ? defaultValue : val; | ||
37 | } | 37 | } | ||
38 | 38 | | |||
39 | QByteArray KReportUtils::attr(const QDomElement &el, const char *attrName, | 39 | QByteArray KReportUtils::attr(const QDomElement &el, const QString &attrName, | ||
40 | const QByteArray &defaultValue) | 40 | const QByteArray &defaultValue) | ||
41 | { | 41 | { | ||
42 | const QByteArray val = el.attribute(QLatin1String(attrName)).toLatin1(); | 42 | const QByteArray val = el.attribute(attrName).toLatin1(); | ||
43 | return val.isEmpty() ? defaultValue : val; | 43 | return val.isEmpty() ? defaultValue : val; | ||
44 | } | 44 | } | ||
45 | 45 | | |||
46 | bool KReportUtils::attr(const QDomElement &el, const char *attrName, bool defaultValue) | 46 | bool KReportUtils::attr(const QDomElement &el, const QString &attrName, bool defaultValue) | ||
47 | { | 47 | { | ||
48 | const QString val = el.attribute(QLatin1String(attrName)); | 48 | const QString val = el.attribute(attrName); | ||
49 | return val.isEmpty() ? defaultValue : QVariant(val).toBool(); | 49 | return val.isEmpty() ? defaultValue : QVariant(val).toBool(); | ||
50 | } | 50 | } | ||
51 | 51 | | |||
52 | int KReportUtils::attr(const QDomElement &el, const char *attrName, int defaultValue) | 52 | int KReportUtils::attr(const QDomElement &el, const QString &attrName, int defaultValue) | ||
53 | { | 53 | { | ||
54 | const QString val = el.attribute(QLatin1String(attrName)); | 54 | const QString val = el.attribute(attrName); | ||
55 | if (val.isEmpty()) { | 55 | if (val.isEmpty()) { | ||
56 | return defaultValue; | 56 | return defaultValue; | ||
57 | } | 57 | } | ||
58 | bool ok; | 58 | bool ok; | ||
59 | const int result = QVariant(val).toInt(&ok); | 59 | const int result = QVariant(val).toInt(&ok); | ||
60 | return ok ? result : defaultValue; | 60 | return ok ? result : defaultValue; | ||
61 | } | 61 | } | ||
62 | 62 | | |||
63 | qreal KReportUtils::attr(const QDomElement &el, const char *attrName, qreal defaultValue) | 63 | qreal KReportUtils::attr(const QDomElement &el, const QString &attrName, qreal defaultValue) | ||
64 | { | 64 | { | ||
65 | const QString val = el.attribute(QLatin1String(attrName)); | 65 | const QString val = el.attribute(attrName); | ||
66 | return KReportUnit::parseValue(val, defaultValue); | 66 | return KReportUnit::parseValue(val, defaultValue); | ||
67 | } | 67 | } | ||
68 | 68 | | |||
69 | QColor KReportUtils::attr(const QDomElement &el, const char *attrName, const QColor &defaultValue) | 69 | QColor KReportUtils::attr(const QDomElement &el, const QString &attrName, const QColor &defaultValue) | ||
70 | { | 70 | { | ||
71 | const QString val = el.attribute(QLatin1String(attrName)); | 71 | const QString val = el.attribute(attrName); | ||
72 | if (val.isEmpty()) { | 72 | if (val.isEmpty()) { | ||
73 | return defaultValue; | 73 | return defaultValue; | ||
74 | } | 74 | } | ||
75 | return QColor(val); | 75 | return QColor(val); | ||
76 | } | 76 | } | ||
77 | 77 | | |||
78 | qreal KReportUtils::attrPercent(const QDomElement& el, const char* attrName, qreal defaultValue) | 78 | qreal KReportUtils::attrPercent(const QDomElement& el, const QString &attrName, qreal defaultValue) | ||
79 | { | 79 | { | ||
80 | QString str(el.attribute(QLatin1String(attrName))); | 80 | QString str(el.attribute(attrName)); | ||
81 | if (str.isEmpty() || !str.endsWith(QLatin1Char('%'))) { | 81 | if (str.isEmpty() || !str.endsWith(QLatin1Char('%'))) { | ||
82 | return defaultValue; | 82 | return defaultValue; | ||
83 | } | 83 | } | ||
84 | str.chop(1); | 84 | str.chop(1); | ||
85 | bool ok; | 85 | bool ok; | ||
86 | const qreal result = QVariant(str).toReal(&ok) / 100.0; | 86 | const qreal result = QVariant(str).toReal(&ok) / 100.0; | ||
87 | if (!ok) { | 87 | if (!ok) { | ||
88 | return defaultValue; | 88 | return defaultValue; | ||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Line(s) | 154 | { | |||
157 | } else if (alignment.testFlag(Qt::AlignLeft)) { | 157 | } else if (alignment.testFlag(Qt::AlignLeft)) { | ||
158 | return QLatin1String("left"); | 158 | return QLatin1String("left"); | ||
159 | } else if (alignment.testFlag(Qt::AlignRight)) { | 159 | } else if (alignment.testFlag(Qt::AlignRight)) { | ||
160 | return QLatin1String("right"); | 160 | return QLatin1String("right"); | ||
161 | } | 161 | } | ||
162 | return QString(); | 162 | return QString(); | ||
163 | } | 163 | } | ||
164 | 164 | | |||
165 | QString KReportUtils::readNameAttribute(const QDomElement &el, const QString &defaultValue) | ||||
166 | { | ||||
167 | return attr(el, QLatin1String("report:name"), defaultValue); | ||||
168 | } | ||||
169 | | ||||
170 | QSizeF KReportUtils::readSizeAttributes(const QDomElement &el, const QSizeF &defaultValue) | ||||
171 | { | ||||
172 | QSizeF val; | ||||
173 | val.setWidth(attr(el, QLatin1String("svg:width"), defaultValue.width())); | ||||
174 | if (val.width() < 0.0) { | ||||
175 | val.setWidth(defaultValue.width()); | ||||
176 | } | ||||
177 | val.setHeight(attr(el, QLatin1String("svg:height"), defaultValue.height())); | ||||
178 | if (val.height() < 0.0) { | ||||
179 | val.setHeight(defaultValue.height()); | ||||
180 | } | ||||
181 | return val; | ||||
182 | } | ||||
183 | | ||||
165 | QRectF KReportUtils::readRectAttributes(const QDomElement &el, const QRectF &defaultValue) | 184 | QRectF KReportUtils::readRectAttributes(const QDomElement &el, const QRectF &defaultValue) | ||
166 | { | 185 | { | ||
167 | QRectF val; | 186 | QRectF val; | ||
168 | val.setX(attr(el, "svg:x", defaultValue.x())); | 187 | val.setX(attr(el, QLatin1String("svg:x"), defaultValue.x())); | ||
169 | val.setY(attr(el, "svg:y", defaultValue.y())); | 188 | val.setY(attr(el, QLatin1String("svg:y"), defaultValue.y())); | ||
170 | val.setWidth(attr(el, "svg:width", defaultValue.width())); | 189 | val.setSize(readSizeAttributes(el, defaultValue.size())); | ||
171 | val.setHeight(attr(el, "svg:height", defaultValue.height())); | | |||
172 | return val; | 190 | return val; | ||
173 | } | 191 | } | ||
174 | 192 | | |||
175 | int KReportUtils::readPercent(const QDomElement& el, const char* name, int defaultPercentValue, bool *ok) | 193 | qreal KReportUtils::readZAttribute(const QDomElement &el, qreal defaultValue) | ||
194 | { | ||||
195 | return KReportUtils::attr(el, QLatin1String("report:z-index"), defaultValue); | ||||
196 | } | ||||
197 | | ||||
198 | int KReportUtils::readPercent(const QDomElement& el, const QString &attrName, int defaultPercentValue, bool *ok) | ||||
176 | { | 199 | { | ||
177 | Q_ASSERT(name); | 200 | QString percent(el.attribute(attrName)); | ||
178 | QString percent(el.attribute(QLatin1String(name))); | | |||
179 | if (percent.isEmpty()) { | 201 | if (percent.isEmpty()) { | ||
180 | if (ok) | 202 | if (ok) | ||
181 | *ok = true; | 203 | *ok = true; | ||
182 | return defaultPercentValue; | 204 | return defaultPercentValue; | ||
183 | } | 205 | } | ||
184 | if (!percent.endsWith(QLatin1Char('%'))) { | 206 | if (!percent.endsWith(QLatin1Char('%'))) { | ||
185 | if (ok) | 207 | if (ok) | ||
186 | *ok = false; | 208 | *ok = false; | ||
187 | return 0; | 209 | return 0; | ||
188 | } | 210 | } | ||
189 | percent.chop(1); | 211 | percent.chop(1); | ||
190 | if (ok) | 212 | if (ok) | ||
191 | *ok = true; | 213 | *ok = true; | ||
192 | return percent.toInt(ok); | 214 | return percent.toInt(ok); | ||
193 | } | 215 | } | ||
194 | 216 | | |||
217 | QString KReportUtils::readSectionTypeNameAttribute(const QDomElement &el, const QString &defaultValue) | ||||
218 | { | ||||
219 | return attr(el, QLatin1String("report:section-type"), defaultValue); | ||||
220 | } | ||||
221 | | ||||
195 | //! @return string representation of @a value, cuts of zeros; precision is set to 2 | 222 | //! @return string representation of @a value, cuts of zeros; precision is set to 2 | ||
196 | static QString roundValueToString(qreal value) | 223 | static QString roundValueToString(qreal value) | ||
197 | { | 224 | { | ||
198 | QString s(QString::number(value, 'g', 2)); | 225 | QString s(QString::number(value, 'g', 2)); | ||
199 | if (s.endsWith(QLatin1String(".00"))) | 226 | if (s.endsWith(QLatin1String(".00"))) | ||
200 | return QString::number(qRound(value)); | 227 | return QString::number(qRound(value)); | ||
201 | return s; | 228 | return s; | ||
202 | } | 229 | } | ||
Show All 12 Lines | 233 | { | |||
215 | // default, "normal" | 242 | // default, "normal" | ||
216 | return QFont::MixedCase; | 243 | return QFont::MixedCase; | ||
217 | } | 244 | } | ||
218 | 245 | | |||
219 | void KReportUtils::readFontAttributes(const QDomElement& el, QFont *font) | 246 | void KReportUtils::readFontAttributes(const QDomElement& el, QFont *font) | ||
220 | { | 247 | { | ||
221 | Q_ASSERT(font); | 248 | Q_ASSERT(font); | ||
222 | const QFont::Capitalization cap = readFontCapitalization( | 249 | const QFont::Capitalization cap = readFontCapitalization( | ||
223 | attr(el, "fo:font-variant", QByteArray()), attr(el, "fo:text-transform", QByteArray())); | 250 | attr(el, QLatin1String("fo:font-variant"), QByteArray()), | ||
251 | attr(el, QLatin1String("fo:text-transform"), QByteArray())); | ||||
224 | font->setCapitalization(cap); | 252 | font->setCapitalization(cap); | ||
225 | 253 | | |||
226 | // weight | 254 | // weight | ||
227 | const QByteArray fontWeight(attr(el, "fo:font-weight", QByteArray())); | 255 | const QByteArray fontWeight(attr(el, QLatin1String("fo:font-weight"), QByteArray())); | ||
228 | int weight = -1; | 256 | int weight = -1; | ||
229 | if (fontWeight == "bold") { | 257 | if (fontWeight == "bold") { | ||
230 | weight = QFont::Bold; | 258 | weight = QFont::Bold; | ||
231 | } | 259 | } | ||
232 | if (fontWeight == "normal") { | 260 | if (fontWeight == "normal") { | ||
233 | weight = QFont::Normal; | 261 | weight = QFont::Normal; | ||
234 | } | 262 | } | ||
235 | else if (!fontWeight.isEmpty()) { | 263 | else if (!fontWeight.isEmpty()) { | ||
236 | // Remember : Qt and CSS/XSL doesn't have the same scale. It's 100-900 instead of Qt's 0-100 | 264 | // Remember : Qt and CSS/XSL doesn't have the same scale. It's 100-900 instead of Qt's 0-100 | ||
237 | // See http://www.w3.org/TR/2001/REC-xsl-20011015/slice7.html#font-weight | 265 | // See http://www.w3.org/TR/2001/REC-xsl-20011015/slice7.html#font-weight | ||
238 | // and http://www.w3.org/TR/CSS2/fonts.html#font-boldness | 266 | // and http://www.w3.org/TR/CSS2/fonts.html#font-boldness | ||
239 | bool ok; | 267 | bool ok; | ||
240 | qreal boldness = fontWeight.toUInt(&ok); | 268 | qreal boldness = fontWeight.toUInt(&ok); | ||
241 | if (ok) { | 269 | if (ok) { | ||
242 | boldness = qMin(boldness, 900.0); | 270 | boldness = qMin(boldness, 900.0); | ||
243 | boldness = qMax(boldness, 100.0); | 271 | boldness = qMax(boldness, 100.0); | ||
244 | weight = (boldness - 100.0) * 0.12375 /*== 99/800*/; // 0..99 | 272 | weight = (boldness - 100.0) * 0.12375 /*== 99/800*/; // 0..99 | ||
245 | } | 273 | } | ||
246 | } | 274 | } | ||
247 | if (weight >= 0) { | 275 | if (weight >= 0) { | ||
248 | font->setWeight(weight); | 276 | font->setWeight(weight); | ||
249 | } | 277 | } | ||
250 | 278 | | |||
251 | font->setItalic(attr(el, "fo:font-style", QByteArray()) == "italic"); | 279 | font->setItalic(attr(el, QLatin1String("fo:font-style"), QByteArray()) == "italic"); | ||
252 | font->setFixedPitch(attr(el, "style:font-pitch", QByteArray()) == "fixed"); | 280 | font->setFixedPitch(attr(el, QLatin1String("style:font-pitch"), QByteArray()) == "fixed"); | ||
253 | font->setFamily(attr(el, "fo:font-family", font->family())); | 281 | font->setFamily(attr(el, QLatin1String("fo:font-family"), font->family())); | ||
254 | font->setKerning(attr(el, "style:letter-kerning", font->kerning())); | 282 | font->setKerning(attr(el, QLatin1String("style:letter-kerning"), font->kerning())); | ||
255 | 283 | | |||
256 | // underline | 284 | // underline | ||
257 | const QByteArray underlineType(attr(el, "style:text-underline-type", QByteArray())); | 285 | const QByteArray underlineType( | ||
258 | font->setUnderline(!underlineType.isEmpty() && underlineType != "none"); // double or single (we don't recognize them) | 286 | attr(el, QLatin1String("style:text-underline-type"), QByteArray())); | ||
287 | font->setUnderline(!underlineType.isEmpty() | ||||
288 | && underlineType | ||||
289 | != "none"); // double or single (we don't recognize them) | ||||
259 | 290 | | |||
260 | // stricken-out | 291 | // stricken-out | ||
261 | const QByteArray strikeOutType(attr(el, "style:text-line-through-type", QByteArray())); | 292 | const QByteArray strikeOutType(attr(el, QLatin1String("style:text-line-through-type"), QByteArray())); | ||
262 | font->setStrikeOut(!strikeOutType.isEmpty() && strikeOutType != "none"); // double or single (we don't recognize them) | 293 | font->setStrikeOut(!strikeOutType.isEmpty() && strikeOutType != "none"); // double or single (we don't recognize them) | ||
263 | 294 | | |||
264 | //! @todo support fo:font-size-rel? | 295 | //! @todo support fo:font-size-rel? | ||
265 | //! @todo support fo:font-size in px | 296 | //! @todo support fo:font-size in px | ||
266 | font->setPointSizeF(KReportUtils::attr(el, "fo:font-size", font->pointSizeF())); | 297 | font->setPointSizeF(KReportUtils::attr(el, QLatin1String("fo:font-size"), font->pointSizeF())); | ||
267 | 298 | | |||
268 | // letter spacing | 299 | // letter spacing | ||
269 | // §7.16.2 of [XSL] http://www.w3.org/TR/xsl11/#letter-spacing | 300 | // §7.16.2 of [XSL] http://www.w3.org/TR/xsl11/#letter-spacing | ||
270 | font->setLetterSpacing(QFont::PercentageSpacing, | 301 | font->setLetterSpacing(QFont::PercentageSpacing, | ||
271 | 100.0 * KReportUtils::attrPercent(el, "fo:letter-spacing", font->letterSpacing())); | 302 | 100.0 * KReportUtils::attrPercent( | ||
303 | el, QLatin1String("fo:letter-spacing"), font->letterSpacing())); | ||||
272 | } | 304 | } | ||
273 | 305 | | |||
274 | void KReportUtils::writeFontAttributes(QDomElement *el, const QFont &font) | 306 | void KReportUtils::writeFontAttributes(QDomElement *el, const QFont &font) | ||
275 | { | 307 | { | ||
276 | Q_ASSERT(el); | 308 | Q_ASSERT(el); | ||
277 | switch (font.capitalization()) { | 309 | switch (font.capitalization()) { | ||
278 | case QFont::SmallCaps: | 310 | case QFont::SmallCaps: | ||
279 | el->setAttribute(QLatin1String("fo:font-variant"), QLatin1String("small-caps")); | 311 | el->setAttribute(QLatin1String("fo:font-variant"), QLatin1String("small-caps")); | ||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | |||||
321 | // pitch, default is "variable" | 353 | // pitch, default is "variable" | ||
322 | if (font.fixedPitch()) { | 354 | if (font.fixedPitch()) { | ||
323 | el->setAttribute(QLatin1String("style:font-pitch"), QLatin1String("fixed")); | 355 | el->setAttribute(QLatin1String("style:font-pitch"), QLatin1String("fixed")); | ||
324 | } | 356 | } | ||
325 | if (!font.family().isEmpty()) { | 357 | if (!font.family().isEmpty()) { | ||
326 | el->setAttribute(QLatin1String("fo:font-family"), font.family()); | 358 | el->setAttribute(QLatin1String("fo:font-family"), font.family()); | ||
327 | } | 359 | } | ||
328 | // kerning, default is "true" | 360 | // kerning, default is "true" | ||
329 | el->setAttribute(QLatin1String("style:letter-kerning"), QLatin1String(font.kerning() ? "true" : "false")); | 361 | el->setAttribute(QLatin1String("style:letter-kerning"), | ||
362 | font.kerning() ? QLatin1String("true") : QLatin1String("false")); | ||||
330 | // underline, default is "none" | 363 | // underline, default is "none" | ||
331 | if (font.underline()) { | 364 | if (font.underline()) { | ||
332 | el->setAttribute(QLatin1String("style:text-underline-type"), QLatin1String("single")); | 365 | el->setAttribute(QLatin1String("style:text-underline-type"), QLatin1String("single")); | ||
333 | } | 366 | } | ||
334 | // stricken-out, default is "none" | 367 | // stricken-out, default is "none" | ||
335 | if (font.strikeOut()) { | 368 | if (font.strikeOut()) { | ||
336 | el->setAttribute(QLatin1String("style:text-line-through-type"), QLatin1String("single")); | 369 | el->setAttribute(QLatin1String("style:text-line-through-type"), QLatin1String("single")); | ||
337 | } | 370 | } | ||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Line(s) | 413 | { | |||
412 | 445 | | |||
413 | entity->appendChild(element); | 446 | entity->appendChild(element); | ||
414 | } | 447 | } | ||
415 | 448 | | |||
416 | void KReportUtils::addPropertyAsAttribute(QDomElement* e, KProperty* p) | 449 | void KReportUtils::addPropertyAsAttribute(QDomElement* e, KProperty* p) | ||
417 | { | 450 | { | ||
418 | Q_ASSERT(e); | 451 | Q_ASSERT(e); | ||
419 | Q_ASSERT(p); | 452 | Q_ASSERT(p); | ||
420 | const QString name = QLatin1String("report:") + QLatin1String(p->name().toLower()); | 453 | const QString name = QLatin1String("report:") + QString::fromLatin1(p->name().toLower()); | ||
421 | 454 | | |||
422 | switch (p->value().type()) { | 455 | switch (p->value().type()) { | ||
423 | case QVariant::Int : | 456 | case QVariant::Int : | ||
424 | e->setAttribute(name, p->value().toInt()); | 457 | e->setAttribute(name, p->value().toInt()); | ||
425 | break; | 458 | break; | ||
426 | case QVariant::Double: | 459 | case QVariant::Double: | ||
427 | e->setAttribute(name, p->value().toDouble()); | 460 | e->setAttribute(name, p->value().toDouble()); | ||
428 | break; | 461 | break; | ||
Show All 28 Lines | 487 | { | |||
457 | KReportUtils::setAttribute(e, QLatin1String("svg:height"), value.height()); | 490 | KReportUtils::setAttribute(e, QLatin1String("svg:height"), value.height()); | ||
458 | } | 491 | } | ||
459 | 492 | | |||
460 | bool KReportUtils::parseReportTextStyleData(const QDomElement & elemSource, KReportTextStyleData *ts) | 493 | bool KReportUtils::parseReportTextStyleData(const QDomElement & elemSource, KReportTextStyleData *ts) | ||
461 | { | 494 | { | ||
462 | Q_ASSERT(ts); | 495 | Q_ASSERT(ts); | ||
463 | if (elemSource.tagName() != QLatin1String("report:text-style")) | 496 | if (elemSource.tagName() != QLatin1String("report:text-style")) | ||
464 | return false; | 497 | return false; | ||
465 | ts->backgroundColor = QColor(elemSource.attribute(QLatin1String("fo:background-color"), QLatin1String("#ffffff"))); | 498 | ts->backgroundColor = QColor(elemSource.attribute( | ||
466 | ts->foregroundColor = QColor(elemSource.attribute(QLatin1String("fo:foreground-color"), QLatin1String("#000000"))); | 499 | QLatin1String("fo:background-color"), QLatin1String("#ffffff"))); | ||
500 | ts->foregroundColor = QColor(elemSource.attribute( | ||||
501 | QLatin1String("fo:foreground-color"), QLatin1String("#000000"))); | ||||
467 | 502 | | |||
468 | bool ok; | 503 | bool ok; | ||
469 | ts->backgroundOpacity = KReportUtils::readPercent(elemSource, "fo:background-opacity", 100, &ok); | 504 | ts->backgroundOpacity = KReportUtils::readPercent( | ||
505 | elemSource, QLatin1String("fo:background-opacity"), 100, &ok); | ||||
470 | if (!ok) { | 506 | if (!ok) { | ||
471 | return false; | 507 | return false; | ||
472 | } | 508 | } | ||
473 | KReportUtils::readFontAttributes(elemSource, &ts->font); | 509 | KReportUtils::readFontAttributes(elemSource, &ts->font); | ||
474 | return true; | 510 | return true; | ||
475 | } | 511 | } | ||
476 | 512 | | |||
477 | bool KReportUtils::parseReportLineStyleData(const QDomElement & elemSource, KReportLineStyle *ls) | 513 | bool KReportUtils::parseReportLineStyleData(const QDomElement & elemSource, KReportLineStyle *ls) | ||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |