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 | val.setHeight(attr(el, QLatin1String("svg:height"), defaultValue.height())); | ||||
175 | return val; | ||||
176 | } | ||||
177 | | ||||
165 | QRectF KReportUtils::readRectAttributes(const QDomElement &el, const QRectF &defaultValue) | 178 | QRectF KReportUtils::readRectAttributes(const QDomElement &el, const QRectF &defaultValue) | ||
166 | { | 179 | { | ||
167 | QRectF val; | 180 | QRectF val; | ||
168 | val.setX(attr(el, "svg:x", defaultValue.x())); | 181 | val.setX(attr(el, QLatin1String("svg:x"), defaultValue.x())); | ||
169 | val.setY(attr(el, "svg:y", defaultValue.y())); | 182 | val.setY(attr(el, QLatin1String("svg:y"), defaultValue.y())); | ||
170 | val.setWidth(attr(el, "svg:width", defaultValue.width())); | 183 | val.setWidth(attr(el, QLatin1String("svg:width"), defaultValue.width())); | ||
171 | val.setHeight(attr(el, "svg:height", defaultValue.height())); | 184 | val.setHeight(attr(el, QLatin1String("svg:height"), defaultValue.height())); | ||
172 | return val; | 185 | return val; | ||
173 | } | 186 | } | ||
174 | 187 | | |||
175 | int KReportUtils::readPercent(const QDomElement& el, const char* name, int defaultPercentValue, bool *ok) | 188 | qreal KReportUtils::readZAttribute(const QDomElement &el, qreal defaultValue) | ||
176 | { | 189 | { | ||
177 | Q_ASSERT(name); | 190 | return KReportUtils::attr(el, QLatin1String("report:z-index"), defaultValue); | ||
178 | QString percent(el.attribute(QLatin1String(name))); | 191 | } | ||
192 | | ||||
193 | int KReportUtils::readPercent(const QDomElement& el, const QString &attrName, int defaultPercentValue, bool *ok) | ||||
194 | { | ||||
195 | QString percent(el.attribute(attrName)); | ||||
179 | if (percent.isEmpty()) { | 196 | if (percent.isEmpty()) { | ||
180 | if (ok) | 197 | if (ok) | ||
181 | *ok = true; | 198 | *ok = true; | ||
182 | return defaultPercentValue; | 199 | return defaultPercentValue; | ||
183 | } | 200 | } | ||
184 | if (!percent.endsWith(QLatin1Char('%'))) { | 201 | if (!percent.endsWith(QLatin1Char('%'))) { | ||
185 | if (ok) | 202 | if (ok) | ||
186 | *ok = false; | 203 | *ok = false; | ||
187 | return 0; | 204 | return 0; | ||
188 | } | 205 | } | ||
189 | percent.chop(1); | 206 | percent.chop(1); | ||
190 | if (ok) | 207 | if (ok) | ||
191 | *ok = true; | 208 | *ok = true; | ||
192 | return percent.toInt(ok); | 209 | return percent.toInt(ok); | ||
193 | } | 210 | } | ||
194 | 211 | | |||
212 | QString KReportUtils::readSectionTypeNameAttribute(const QDomElement &el, const QString &defaultValue) | ||||
213 | { | ||||
214 | return attr(el, QLatin1String("report:section-type"), defaultValue); | ||||
215 | } | ||||
216 | | ||||
195 | //! @return string representation of @a value, cuts of zeros; precision is set to 2 | 217 | //! @return string representation of @a value, cuts of zeros; precision is set to 2 | ||
196 | static QString roundValueToString(qreal value) | 218 | static QString roundValueToString(qreal value) | ||
197 | { | 219 | { | ||
198 | QString s(QString::number(value, 'g', 2)); | 220 | QString s(QString::number(value, 'g', 2)); | ||
199 | if (s.endsWith(QLatin1String(".00"))) | 221 | if (s.endsWith(QLatin1String(".00"))) | ||
200 | return QString::number(qRound(value)); | 222 | return QString::number(qRound(value)); | ||
201 | return s; | 223 | return s; | ||
202 | } | 224 | } | ||
Show All 12 Lines | 228 | { | |||
215 | // default, "normal" | 237 | // default, "normal" | ||
216 | return QFont::MixedCase; | 238 | return QFont::MixedCase; | ||
217 | } | 239 | } | ||
218 | 240 | | |||
219 | void KReportUtils::readFontAttributes(const QDomElement& el, QFont *font) | 241 | void KReportUtils::readFontAttributes(const QDomElement& el, QFont *font) | ||
220 | { | 242 | { | ||
221 | Q_ASSERT(font); | 243 | Q_ASSERT(font); | ||
222 | const QFont::Capitalization cap = readFontCapitalization( | 244 | const QFont::Capitalization cap = readFontCapitalization( | ||
223 | attr(el, "fo:font-variant", QByteArray()), attr(el, "fo:text-transform", QByteArray())); | 245 | attr(el, QLatin1String("fo:font-variant"), QByteArray()), | ||
246 | attr(el, QLatin1String("fo:text-transform"), QByteArray())); | ||||
224 | font->setCapitalization(cap); | 247 | font->setCapitalization(cap); | ||
225 | 248 | | |||
226 | // weight | 249 | // weight | ||
227 | const QByteArray fontWeight(attr(el, "fo:font-weight", QByteArray())); | 250 | const QByteArray fontWeight(attr(el, QLatin1String("fo:font-weight"), QByteArray())); | ||
228 | int weight = -1; | 251 | int weight = -1; | ||
229 | if (fontWeight == "bold") { | 252 | if (fontWeight == "bold") { | ||
230 | weight = QFont::Bold; | 253 | weight = QFont::Bold; | ||
231 | } | 254 | } | ||
232 | if (fontWeight == "normal") { | 255 | if (fontWeight == "normal") { | ||
233 | weight = QFont::Normal; | 256 | weight = QFont::Normal; | ||
234 | } | 257 | } | ||
235 | else if (!fontWeight.isEmpty()) { | 258 | 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 | 259 | // 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 | 260 | // 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 | 261 | // and http://www.w3.org/TR/CSS2/fonts.html#font-boldness | ||
239 | bool ok; | 262 | bool ok; | ||
240 | qreal boldness = fontWeight.toUInt(&ok); | 263 | qreal boldness = fontWeight.toUInt(&ok); | ||
241 | if (ok) { | 264 | if (ok) { | ||
242 | boldness = qMin(boldness, 900.0); | 265 | boldness = qMin(boldness, 900.0); | ||
243 | boldness = qMax(boldness, 100.0); | 266 | boldness = qMax(boldness, 100.0); | ||
244 | weight = (boldness - 100.0) * 0.12375 /*== 99/800*/; // 0..99 | 267 | weight = (boldness - 100.0) * 0.12375 /*== 99/800*/; // 0..99 | ||
245 | } | 268 | } | ||
246 | } | 269 | } | ||
247 | if (weight >= 0) { | 270 | if (weight >= 0) { | ||
248 | font->setWeight(weight); | 271 | font->setWeight(weight); | ||
249 | } | 272 | } | ||
250 | 273 | | |||
251 | font->setItalic(attr(el, "fo:font-style", QByteArray()) == "italic"); | 274 | font->setItalic(attr(el, QLatin1String("fo:font-style"), QByteArray()) == "italic"); | ||
252 | font->setFixedPitch(attr(el, "style:font-pitch", QByteArray()) == "fixed"); | 275 | font->setFixedPitch(attr(el, QLatin1String("style:font-pitch"), QByteArray()) == "fixed"); | ||
253 | font->setFamily(attr(el, "fo:font-family", font->family())); | 276 | font->setFamily(attr(el, QLatin1String("fo:font-family"), font->family())); | ||
254 | font->setKerning(attr(el, "style:letter-kerning", font->kerning())); | 277 | font->setKerning(attr(el, QLatin1String("style:letter-kerning"), font->kerning())); | ||
255 | 278 | | |||
256 | // underline | 279 | // underline | ||
257 | const QByteArray underlineType(attr(el, "style:text-underline-type", QByteArray())); | 280 | const QByteArray underlineType( | ||
258 | font->setUnderline(!underlineType.isEmpty() && underlineType != "none"); // double or single (we don't recognize them) | 281 | attr(el, QLatin1String("style:text-underline-type"), QByteArray())); | ||
282 | font->setUnderline(!underlineType.isEmpty() | ||||
283 | && underlineType | ||||
284 | != "none"); // double or single (we don't recognize them) | ||||
259 | 285 | | |||
260 | // stricken-out | 286 | // stricken-out | ||
261 | const QByteArray strikeOutType(attr(el, "style:text-line-through-type", QByteArray())); | 287 | 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) | 288 | font->setStrikeOut(!strikeOutType.isEmpty() && strikeOutType != "none"); // double or single (we don't recognize them) | ||
263 | 289 | | |||
264 | //! @todo support fo:font-size-rel? | 290 | //! @todo support fo:font-size-rel? | ||
265 | //! @todo support fo:font-size in px | 291 | //! @todo support fo:font-size in px | ||
266 | font->setPointSizeF(KReportUtils::attr(el, "fo:font-size", font->pointSizeF())); | 292 | font->setPointSizeF(KReportUtils::attr(el, QLatin1String("fo:font-size"), font->pointSizeF())); | ||
267 | 293 | | |||
268 | // letter spacing | 294 | // letter spacing | ||
269 | // §7.16.2 of [XSL] http://www.w3.org/TR/xsl11/#letter-spacing | 295 | // §7.16.2 of [XSL] http://www.w3.org/TR/xsl11/#letter-spacing | ||
270 | font->setLetterSpacing(QFont::PercentageSpacing, | 296 | font->setLetterSpacing(QFont::PercentageSpacing, | ||
271 | 100.0 * KReportUtils::attrPercent(el, "fo:letter-spacing", font->letterSpacing())); | 297 | 100.0 * KReportUtils::attrPercent( | ||
298 | el, QLatin1String("fo:letter-spacing"), font->letterSpacing())); | ||||
272 | } | 299 | } | ||
273 | 300 | | |||
274 | void KReportUtils::writeFontAttributes(QDomElement *el, const QFont &font) | 301 | void KReportUtils::writeFontAttributes(QDomElement *el, const QFont &font) | ||
275 | { | 302 | { | ||
276 | Q_ASSERT(el); | 303 | Q_ASSERT(el); | ||
277 | switch (font.capitalization()) { | 304 | switch (font.capitalization()) { | ||
278 | case QFont::SmallCaps: | 305 | case QFont::SmallCaps: | ||
279 | el->setAttribute(QLatin1String("fo:font-variant"), QLatin1String("small-caps")); | 306 | 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" | 348 | // pitch, default is "variable" | ||
322 | if (font.fixedPitch()) { | 349 | if (font.fixedPitch()) { | ||
323 | el->setAttribute(QLatin1String("style:font-pitch"), QLatin1String("fixed")); | 350 | el->setAttribute(QLatin1String("style:font-pitch"), QLatin1String("fixed")); | ||
324 | } | 351 | } | ||
325 | if (!font.family().isEmpty()) { | 352 | if (!font.family().isEmpty()) { | ||
326 | el->setAttribute(QLatin1String("fo:font-family"), font.family()); | 353 | el->setAttribute(QLatin1String("fo:font-family"), font.family()); | ||
327 | } | 354 | } | ||
328 | // kerning, default is "true" | 355 | // kerning, default is "true" | ||
329 | el->setAttribute(QLatin1String("style:letter-kerning"), QLatin1String(font.kerning() ? "true" : "false")); | 356 | el->setAttribute(QLatin1String("style:letter-kerning"), | ||
357 | font.kerning() ? QLatin1String("true") : QLatin1String("false")); | ||||
330 | // underline, default is "none" | 358 | // underline, default is "none" | ||
331 | if (font.underline()) { | 359 | if (font.underline()) { | ||
332 | el->setAttribute(QLatin1String("style:text-underline-type"), QLatin1String("single")); | 360 | el->setAttribute(QLatin1String("style:text-underline-type"), QLatin1String("single")); | ||
333 | } | 361 | } | ||
334 | // stricken-out, default is "none" | 362 | // stricken-out, default is "none" | ||
335 | if (font.strikeOut()) { | 363 | if (font.strikeOut()) { | ||
336 | el->setAttribute(QLatin1String("style:text-line-through-type"), QLatin1String("single")); | 364 | el->setAttribute(QLatin1String("style:text-line-through-type"), QLatin1String("single")); | ||
337 | } | 365 | } | ||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Line(s) | 408 | { | |||
412 | 440 | | |||
413 | entity->appendChild(element); | 441 | entity->appendChild(element); | ||
414 | } | 442 | } | ||
415 | 443 | | |||
416 | void KReportUtils::addPropertyAsAttribute(QDomElement* e, KProperty* p) | 444 | void KReportUtils::addPropertyAsAttribute(QDomElement* e, KProperty* p) | ||
417 | { | 445 | { | ||
418 | Q_ASSERT(e); | 446 | Q_ASSERT(e); | ||
419 | Q_ASSERT(p); | 447 | Q_ASSERT(p); | ||
420 | const QString name = QLatin1String("report:") + QLatin1String(p->name().toLower()); | 448 | const QString name = QLatin1String("report:") + QString::fromLatin1(p->name().toLower()); | ||
421 | 449 | | |||
422 | switch (p->value().type()) { | 450 | switch (p->value().type()) { | ||
423 | case QVariant::Int : | 451 | case QVariant::Int : | ||
424 | e->setAttribute(name, p->value().toInt()); | 452 | e->setAttribute(name, p->value().toInt()); | ||
425 | break; | 453 | break; | ||
426 | case QVariant::Double: | 454 | case QVariant::Double: | ||
427 | e->setAttribute(name, p->value().toDouble()); | 455 | e->setAttribute(name, p->value().toDouble()); | ||
428 | break; | 456 | break; | ||
Show All 28 Lines | 482 | { | |||
457 | KReportUtils::setAttribute(e, QLatin1String("svg:height"), value.height()); | 485 | KReportUtils::setAttribute(e, QLatin1String("svg:height"), value.height()); | ||
458 | } | 486 | } | ||
459 | 487 | | |||
460 | bool KReportUtils::parseReportTextStyleData(const QDomElement & elemSource, KReportTextStyleData *ts) | 488 | bool KReportUtils::parseReportTextStyleData(const QDomElement & elemSource, KReportTextStyleData *ts) | ||
461 | { | 489 | { | ||
462 | Q_ASSERT(ts); | 490 | Q_ASSERT(ts); | ||
463 | if (elemSource.tagName() != QLatin1String("report:text-style")) | 491 | if (elemSource.tagName() != QLatin1String("report:text-style")) | ||
464 | return false; | 492 | return false; | ||
465 | ts->backgroundColor = QColor(elemSource.attribute(QLatin1String("fo:background-color"), QLatin1String("#ffffff"))); | 493 | ts->backgroundColor = QColor(elemSource.attribute( | ||
466 | ts->foregroundColor = QColor(elemSource.attribute(QLatin1String("fo:foreground-color"), QLatin1String("#000000"))); | 494 | QLatin1String("fo:background-color"), QLatin1String("#ffffff"))); | ||
495 | ts->foregroundColor = QColor(elemSource.attribute( | ||||
496 | QLatin1String("fo:foreground-color"), QLatin1String("#000000"))); | ||||
467 | 497 | | |||
468 | bool ok; | 498 | bool ok; | ||
469 | ts->backgroundOpacity = KReportUtils::readPercent(elemSource, "fo:background-opacity", 100, &ok); | 499 | ts->backgroundOpacity = KReportUtils::readPercent( | ||
500 | elemSource, QLatin1String("fo:background-opacity"), 100, &ok); | ||||
470 | if (!ok) { | 501 | if (!ok) { | ||
471 | return false; | 502 | return false; | ||
472 | } | 503 | } | ||
473 | KReportUtils::readFontAttributes(elemSource, &ts->font); | 504 | KReportUtils::readFontAttributes(elemSource, &ts->font); | ||
474 | return true; | 505 | return true; | ||
475 | } | 506 | } | ||
476 | 507 | | |||
477 | bool KReportUtils::parseReportLineStyleData(const QDomElement & elemSource, KReportLineStyle *ls) | 508 | bool KReportUtils::parseReportLineStyleData(const QDomElement & elemSource, KReportLineStyle *ls) | ||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |