File: unit.h

package info (click to toggle)
kig 4%3A25.08.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,716 kB
  • sloc: cpp: 41,465; xml: 851; python: 486; perl: 23; sh: 17; makefile: 3
file content (68 lines) | stat: -rw-r--r-- 1,852 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
    This file is part of Kig, a KDE program for Interactive Geometry.
    SPDX-FileCopyrightText: 2006 Pino Toscano <toscano.pino@tiscali.it>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#pragma once

#include <QStringList>

/**
 * This small class server as helper to perform conversions between
 * metrical units.
 */
class Unit
{
public:
    /**
     * The kinds of metrical units we support.
     */
    enum MetricalUnit { pixel = 0, cm, in };
    explicit Unit(double value = 0.0, Unit::MetricalUnit unit = cm, int dpi = 1);
    ~Unit();
    void setValue(double value);
    double value() const;
    /**
     * Set the unit of the current object to \p unit, but it doesn't
     * convert the value to the new unit.
     *
     * \see convertTo()
     */
    void setUnit(Unit::MetricalUnit unit);
    /**
     * Set the unit of the current object to \p unit and convert the
     * value to the new unit using \ref convert().
     *
     * \see setUnit()
     */
    void convertTo(Unit::MetricalUnit unit);
    Unit::MetricalUnit unit() const;
    double getValue(Unit::MetricalUnit unit) const;
    void setDpi(int dpi);
    int dpi() const;
    /**
     * The most useful method of this class: convert the specified
     * \p value from the unit \p from to the unit \p to, using the
     * specified \p dpi in case of conversions from/to pixels.
     */
    static double convert(double value, Unit::MetricalUnit from, Unit::MetricalUnit to, int dpi = 1);
    /**
     * Get a list of the supported metrical units.
     */
    static QStringList unitList();
    static Unit::MetricalUnit intToUnit(int index);

    /**
     * How many decimals the \p unit have.
     */
    static int precision(Unit::MetricalUnit unit);

    Unit &operator=(const Unit &u);

private:
    double mvalue;
    Unit::MetricalUnit munit;
    int mdpi;
};