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 | 6 | | |||
6 | This library is free software; you can redistribute it and/or | 7 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | 8 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | 9 | License as published by the Free Software Foundation; either | ||
9 | 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. | ||
10 | 11 | | |||
11 | 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, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
▲ Show 20 Lines • Show All 216 Lines • ▼ Show 20 Line(s) | 211 | { | |||
229 | } | 230 | } | ||
230 | } | 231 | } | ||
231 | 232 | | |||
232 | QString KReportUnit::toUserStringValue(qreal ptValue) const | 233 | QString KReportUnit::toUserStringValue(qreal ptValue) const | ||
233 | { | 234 | { | ||
234 | return QLocale::system().toString(toUserValue(ptValue)); | 235 | return QLocale::system().toString(toUserValue(ptValue)); | ||
235 | } | 236 | } | ||
236 | 237 | | |||
237 | qreal KReportUnit::fromUserValue(qreal value) const | 238 | qreal KReportUnit::convertFromPoint(qreal ptValue) const | ||
239 | { | ||||
240 | switch (d->type) { | ||||
241 | case Millimeter: | ||||
242 | return POINT_TO_MM(ptValue); | ||||
243 | case Centimeter: | ||||
244 | return POINT_TO_CM(ptValue); | ||||
245 | case Decimeter: | ||||
246 | return POINT_TO_DM(ptValue); | ||||
247 | case Inch: | ||||
248 | return POINT_TO_INCH(ptValue); | ||||
249 | case Pica: | ||||
250 | return POINT_TO_PI(ptValue); | ||||
251 | case Cicero: | ||||
252 | return POINT_TO_CC(ptValue); | ||||
253 | case Pixel: | ||||
254 | return ptValue * d->pixelConversion; | ||||
255 | case Point: | ||||
256 | default: | ||||
257 | return ptValue; | ||||
258 | } | ||||
259 | } | ||||
260 | | ||||
261 | QPointF KReportUnit::convertFromPoint(const QPointF &ptValue) const | ||||
262 | { | ||||
263 | return QPointF(convertFromPoint(ptValue.x()), convertFromPoint(ptValue.y())); | ||||
264 | } | ||||
265 | | ||||
266 | QSizeF KReportUnit::convertFromPoint(const QSizeF &ptValue) const | ||||
267 | { | ||||
268 | return QSizeF(convertFromPoint(ptValue.width()), convertFromPoint(ptValue.height())); | ||||
269 | } | ||||
270 | | ||||
271 | qreal KReportUnit::convertToPoint(qreal value) const | ||||
238 | { | 272 | { | ||
239 | switch (d->type) { | 273 | switch (d->type) { | ||
240 | case Millimeter: | 274 | case Millimeter: | ||
241 | return MM_TO_POINT(value); | 275 | return MM_TO_POINT(value); | ||
242 | case Centimeter: | 276 | case Centimeter: | ||
243 | return CM_TO_POINT(value); | 277 | return CM_TO_POINT(value); | ||
244 | case Decimeter: | 278 | case Decimeter: | ||
245 | return DM_TO_POINT(value); | 279 | return DM_TO_POINT(value); | ||
246 | case Inch: | 280 | case Inch: | ||
247 | return INCH_TO_POINT(value); | 281 | return INCH_TO_POINT(value); | ||
248 | case Pica: | 282 | case Pica: | ||
249 | return PI_TO_POINT(value); | 283 | return PI_TO_POINT(value); | ||
250 | case Cicero: | 284 | case Cicero: | ||
251 | return CC_TO_POINT(value); | 285 | return CC_TO_POINT(value); | ||
252 | case Pixel: | 286 | case Pixel: | ||
253 | return value / d->pixelConversion; | 287 | return value / d->pixelConversion; | ||
254 | case Point: | 288 | case Point: | ||
255 | default: | 289 | default: | ||
256 | return value; | 290 | return value; | ||
257 | } | 291 | } | ||
258 | } | 292 | } | ||
259 | 293 | | |||
260 | qreal KReportUnit::fromUserValue(const QString &value, bool *ok) const | 294 | qreal KReportUnit::convertToPoint(const QString &value, bool *ok) const | ||
295 | { | ||||
296 | return convertToPoint(QLocale::system().toDouble(value, ok)); | ||||
297 | } | ||||
298 | | ||||
299 | QPointF KReportUnit::convertToPoint(const QPointF &value) const | ||||
261 | { | 300 | { | ||
262 | return fromUserValue(QLocale::system().toDouble(value, ok)); | 301 | return QPointF(KReportUnit::convertToPoint(value.x()), | ||
302 | KReportUnit::convertToPoint(value.y())); | ||||
303 | } | ||||
304 | | ||||
305 | QSizeF KReportUnit::convertToPoint(const QSizeF &value) const | ||||
306 | { | ||||
307 | return QSizeF(KReportUnit::convertToPoint(value.width()), | ||||
308 | KReportUnit::convertToPoint(value.height())); | ||||
263 | } | 309 | } | ||
264 | 310 | | |||
265 | qreal KReportUnit::parseValue(const QString& _value, qreal defaultVal) | 311 | qreal KReportUnit::parseValue(const QString& _value, qreal defaultVal) | ||
266 | { | 312 | { | ||
267 | if (_value.isEmpty()) | 313 | if (_value.isEmpty()) | ||
268 | return defaultVal; | 314 | return defaultVal; | ||
269 | 315 | | |||
270 | QString value(_value.simplified()); | 316 | QString value(_value.simplified()); | ||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | 371 | if (ok) | |||
326 | *ok = true; | 372 | *ok = true; | ||
327 | } | 373 | } | ||
328 | } | 374 | } | ||
329 | } | 375 | } | ||
330 | 376 | | |||
331 | return KReportUnit(result); | 377 | return KReportUnit(result); | ||
332 | } | 378 | } | ||
333 | 379 | | |||
334 | qreal KReportUnit::convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor) | 380 | qreal KReportUnit::convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, | ||
381 | const KReportUnit &toUnit) | ||||
335 | { | 382 | { | ||
336 | qreal pt; | 383 | const qreal pt = fromUnit.convertToPoint(value); | ||
337 | switch (fromUnit.type()) { | 384 | return toUnit.convertFromPoint(pt); | ||
338 | case Millimeter: | | |||
339 | pt = MM_TO_POINT(value); | | |||
340 | break; | | |||
341 | case Centimeter: | | |||
342 | pt = CM_TO_POINT(value); | | |||
343 | break; | | |||
344 | case Decimeter: | | |||
345 | pt = DM_TO_POINT(value); | | |||
346 | break; | | |||
347 | case Inch: | | |||
348 | pt = INCH_TO_POINT(value); | | |||
349 | break; | | |||
350 | case Pica: | | |||
351 | pt = PI_TO_POINT(value); | | |||
352 | break; | | |||
353 | case Cicero: | | |||
354 | pt = CC_TO_POINT(value); | | |||
355 | break; | | |||
356 | case Pixel: | | |||
357 | pt = value / factor; | | |||
358 | break; | | |||
359 | case Point: | | |||
360 | default: | | |||
361 | pt = value; | | |||
362 | } | 385 | } | ||
363 | 386 | | |||
364 | switch (toUnit.type()) { | 387 | QPointF KReportUnit::convertFromUnitToUnit(const QPointF &value, | ||
365 | case Millimeter: | 388 | const KReportUnit &fromUnit, | ||
366 | return POINT_TO_MM(pt); | 389 | const KReportUnit &toUnit) | ||
367 | case Centimeter: | 390 | { | ||
368 | return POINT_TO_CM(pt); | 391 | return QPointF( | ||
369 | case Decimeter: | 392 | KReportUnit::convertFromUnitToUnit(value.x(), fromUnit, toUnit), | ||
370 | return POINT_TO_DM(pt); | 393 | KReportUnit::convertFromUnitToUnit(value.y(), fromUnit, toUnit)); | ||
371 | case Inch: | | |||
372 | return POINT_TO_INCH(pt); | | |||
373 | case Pica: | | |||
374 | return POINT_TO_PI(pt); | | |||
375 | case Cicero: | | |||
376 | return POINT_TO_CC(pt); | | |||
377 | case Pixel: | | |||
378 | return pt * factor; | | |||
379 | case Point: | | |||
380 | default: | | |||
381 | return pt; | | |||
382 | } | 394 | } | ||
383 | 395 | | |||
396 | QSizeF KReportUnit::convertFromUnitToUnit(const QSizeF &value, | ||||
397 | const KReportUnit &fromUnit, | ||||
398 | const KReportUnit &toUnit) | ||||
399 | { | ||||
400 | return QSizeF( | ||||
401 | KReportUnit::convertFromUnitToUnit(value.width(), fromUnit, toUnit), | ||||
402 | KReportUnit::convertFromUnitToUnit(value.height(), fromUnit, toUnit)); | ||||
384 | } | 403 | } | ||
385 | 404 | | |||
386 | QString KReportUnit::symbol() const | 405 | QString KReportUnit::symbol() const | ||
387 | { | 406 | { | ||
388 | return QLatin1String(unitNameList[d->type]); | 407 | return QLatin1String(unitNameList[d->type]); | ||
389 | } | 408 | } | ||
390 | 409 | | |||
391 | qreal KReportUnit::parseAngle(const QString& _value, qreal defaultVal) | 410 | qreal KReportUnit::parseAngle(const QString& _value, qreal defaultVal) | ||
▲ Show 20 Lines • Show All 62 Lines • Show Last 20 Lines |