Changeset View
Changeset View
Standalone View
Standalone View
src/common/KReportUnit.cpp
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
---|---|---|---|---|---|
2 | Copyright (C) 2001 David Faure <faure@kde.org> | 2 | Copyright (C) 2001 David Faure <faure@kde.org> | ||
3 | Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org> | 3 | Copyright (C) 2004 Nicolas GOUTTE <goutte@kde.org> | ||
4 | Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org> | 4 | Copyright (C) 2012 Friedrich W. H. Kossebau <kossebau@kde.org> | ||
5 | Copyright (C) 2017 Jarosław Staniek <staniek@kde.org> | 5 | Copyright (C) 2017 Jarosław Staniek <staniek@kde.org> | ||
6 | 6 | | |||
7 | This library is free software; you can redistribute it and/or | 7 | This library is free software; you can redistribute it and/or | ||
8 | modify it under the terms of the GNU Library General Public | 8 | modify it under the terms of the GNU Library General Public | ||
9 | License as published by the Free Software Foundation; either | 9 | License as published by the Free Software Foundation; either | ||
10 | version 2 of the License, or (at your option) any later version. | 10 | version 2 of the License, or (at your option) any later version. | ||
11 | 11 | | |||
12 | This library is distributed in the hope that it will be useful, | 12 | This library is distributed in the hope that it will be useful, | ||
▲ Show 20 Lines • Show All 207 Lines • ▼ Show 20 Line(s) | 199 | { | |||
220 | } | 220 | } | ||
221 | } | 221 | } | ||
222 | 222 | | |||
223 | QString KReportUnit::toUserStringValue(qreal ptValue) const | 223 | QString KReportUnit::toUserStringValue(qreal ptValue) const | ||
224 | { | 224 | { | ||
225 | return QLocale::system().toString(toUserValue(ptValue)); | 225 | return QLocale::system().toString(toUserValue(ptValue)); | ||
226 | } | 226 | } | ||
227 | 227 | | |||
228 | qreal KReportUnit::fromUserValue(qreal value) const | 228 | qreal KReportUnit::convertFromPoint(qreal ptValue) const | ||
229 | { | ||||
230 | switch (d->type) { | ||||
231 | case Type::Millimeter: | ||||
232 | return POINT_TO_MM(ptValue); | ||||
233 | case Type::Centimeter: | ||||
234 | return POINT_TO_CM(ptValue); | ||||
235 | case Type::Decimeter: | ||||
236 | return POINT_TO_DM(ptValue); | ||||
237 | case Type::Inch: | ||||
238 | return POINT_TO_INCH(ptValue); | ||||
239 | case Type::Pica: | ||||
240 | return POINT_TO_PI(ptValue); | ||||
241 | case Type::Cicero: | ||||
242 | return POINT_TO_CC(ptValue); | ||||
243 | case Type::Pixel: | ||||
244 | return ptValue * d->pixelConversion; | ||||
245 | case Type::Point: | ||||
246 | default: | ||||
247 | return ptValue; | ||||
248 | } | ||||
249 | } | ||||
250 | | ||||
251 | QPointF KReportUnit::convertFromPoint(const QPointF &ptValue) const | ||||
252 | { | ||||
253 | return QPointF(convertFromPoint(ptValue.x()), convertFromPoint(ptValue.y())); | ||||
254 | } | ||||
255 | | ||||
256 | QSizeF KReportUnit::convertFromPoint(const QSizeF &ptValue) const | ||||
257 | { | ||||
258 | return QSizeF(convertFromPoint(ptValue.width()), convertFromPoint(ptValue.height())); | ||||
259 | } | ||||
260 | | ||||
261 | qreal KReportUnit::convertToPoint(qreal value) const | ||||
229 | { | 262 | { | ||
230 | switch (d->type) { | 263 | switch (d->type) { | ||
231 | case Type::Invalid: | 264 | case Type::Invalid: | ||
232 | return -1.0; | 265 | return -1.0; | ||
233 | case Type::Millimeter: | 266 | case Type::Millimeter: | ||
234 | return MM_TO_POINT(value); | 267 | return MM_TO_POINT(value); | ||
235 | case Type::Centimeter: | 268 | case Type::Centimeter: | ||
236 | return CM_TO_POINT(value); | 269 | return CM_TO_POINT(value); | ||
237 | case Type::Decimeter: | 270 | case Type::Decimeter: | ||
238 | return DM_TO_POINT(value); | 271 | return DM_TO_POINT(value); | ||
239 | case Type::Inch: | 272 | case Type::Inch: | ||
240 | return INCH_TO_POINT(value); | 273 | return INCH_TO_POINT(value); | ||
241 | case Type::Pica: | 274 | case Type::Pica: | ||
242 | return PI_TO_POINT(value); | 275 | return PI_TO_POINT(value); | ||
243 | case Type::Cicero: | 276 | case Type::Cicero: | ||
244 | return CC_TO_POINT(value); | 277 | return CC_TO_POINT(value); | ||
245 | case Type::Pixel: | 278 | case Type::Pixel: | ||
246 | return value / d->pixelConversion; | 279 | return value / d->pixelConversion; | ||
247 | case Type::Point: | 280 | case Type::Point: | ||
248 | default: | 281 | default: | ||
249 | return value; | 282 | return value; | ||
250 | } | 283 | } | ||
251 | } | 284 | } | ||
252 | 285 | | |||
253 | qreal KReportUnit::fromUserValue(const QString &value, bool *ok) const | 286 | qreal KReportUnit::convertToPoint(const QString &value, bool *ok) const | ||
254 | { | 287 | { | ||
255 | if (d->type == Type::Invalid) { | 288 | return convertToPoint(QLocale::system().toDouble(value, ok)); | ||
256 | kreportWarning() << "Conversion from Invalid type not supported"; | | |||
257 | if (ok) { | | |||
258 | *ok = false; | | |||
259 | } | 289 | } | ||
260 | return -1.0; | 290 | | ||
291 | QPointF KReportUnit::convertToPoint(const QPointF &value) const | ||||
292 | { | ||||
293 | return QPointF(KReportUnit::convertToPoint(value.x()), | ||||
294 | KReportUnit::convertToPoint(value.y())); | ||||
261 | } | 295 | } | ||
262 | return fromUserValue(QLocale::system().toDouble(value, ok)); | 296 | | ||
297 | QSizeF KReportUnit::convertToPoint(const QSizeF &value) const | ||||
298 | { | ||||
299 | return QSizeF(KReportUnit::convertToPoint(value.width()), | ||||
300 | KReportUnit::convertToPoint(value.height())); | ||||
263 | } | 301 | } | ||
264 | 302 | | |||
265 | qreal KReportUnit::parseValue(const QString& _value, qreal defaultVal) | 303 | qreal KReportUnit::parseValue(const QString& _value, qreal defaultVal) | ||
266 | { | 304 | { | ||
267 | if (_value.isEmpty()) | 305 | if (_value.isEmpty()) | ||
268 | return defaultVal; | 306 | return defaultVal; | ||
269 | 307 | | |||
270 | QString value(_value.simplified()); | 308 | QString value(_value.simplified()); | ||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Line(s) | 423 | case Type::Inch: | |||
386 | return POINT_TO_INCH(pt); | 424 | return POINT_TO_INCH(pt); | ||
387 | case Type::Pica: | 425 | case Type::Pica: | ||
388 | return POINT_TO_PI(pt); | 426 | return POINT_TO_PI(pt); | ||
389 | case Type::Cicero: | 427 | case Type::Cicero: | ||
390 | return POINT_TO_CC(pt); | 428 | return POINT_TO_CC(pt); | ||
391 | case Type::Pixel: | 429 | case Type::Pixel: | ||
392 | return pt * factor; | 430 | return pt * factor; | ||
393 | case Type::Invalid: | 431 | case Type::Invalid: | ||
394 | case Type::Point: | | |||
395 | default: | 432 | default: | ||
396 | return pt; | 433 | return pt; | ||
397 | } | 434 | } | ||
398 | } | 435 | } | ||
399 | 436 | | |||
437 | QPointF KReportUnit::convertFromUnitToUnit(const QPointF &value, | ||||
438 | const KReportUnit &fromUnit, | ||||
439 | const KReportUnit &toUnit) | ||||
440 | { | ||||
441 | return QPointF( | ||||
442 | KReportUnit::convertFromUnitToUnit(value.x(), fromUnit, toUnit), | ||||
443 | KReportUnit::convertFromUnitToUnit(value.y(), fromUnit, toUnit)); | ||||
444 | } | ||||
445 | | ||||
446 | QSizeF KReportUnit::convertFromUnitToUnit(const QSizeF &value, | ||||
447 | const KReportUnit &fromUnit, | ||||
448 | const KReportUnit &toUnit) | ||||
449 | { | ||||
450 | return QSizeF( | ||||
451 | KReportUnit::convertFromUnitToUnit(value.width(), fromUnit, toUnit), | ||||
452 | KReportUnit::convertFromUnitToUnit(value.height(), fromUnit, toUnit)); | ||||
453 | } | ||||
454 | | ||||
400 | QString KReportUnit::symbol() const | 455 | QString KReportUnit::symbol() const | ||
401 | { | 456 | { | ||
402 | return QLatin1String(s_unitSymbols[static_cast<int>(d->type)]); | 457 | return QLatin1String(s_unitSymbols[static_cast<int>(d->type)]); | ||
403 | } | 458 | } | ||
404 | 459 | | |||
405 | qreal KReportUnit::parseAngle(const QString& _value, qreal defaultVal) | 460 | qreal KReportUnit::parseAngle(const QString& _value, qreal defaultVal) | ||
406 | { | 461 | { | ||
407 | if (_value.isEmpty()) | 462 | if (_value.isEmpty()) | ||
▲ Show 20 Lines • Show All 68 Lines • Show Last 20 Lines |