1 /*
2     This file is part of Kig, a KDE program for Interactive Geometry...
3     SPDX-FileCopyrightText: 2006 Pino Toscano <toscano.pino@tiscali.it>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #ifndef KIG_MISC_UNIT_H
9 #define KIG_MISC_UNIT_H
10 
11 #include <QStringList>
12 
13 /**
14  * This small class server as helper to perform conversions between
15  * metrical units.
16  */
17 class Unit
18 {
19 public:
20   /**
21    * The kinds of metrical units we support.
22    */
23   enum MetricalUnit { pixel = 0, cm, in };
24   explicit Unit( double value = 0.0, Unit::MetricalUnit unit = cm, int dpi = 1 );
25   ~Unit();
26   void setValue( double value );
27   double value() const;
28   /**
29    * Set the unit of the current object to \p unit, but it doesn't
30    * convert the value to the new unit.
31    *
32    * \see convertTo()
33    */
34   void setUnit( Unit::MetricalUnit unit );
35   /**
36    * Set the unit of the current object to \p unit and convert the
37    * value to the new unit using \ref convert().
38    *
39    * \see setUnit()
40    */
41   void convertTo( Unit::MetricalUnit unit );
42   Unit::MetricalUnit unit() const;
43   double getValue( Unit::MetricalUnit unit ) const;
44   void setDpi( int dpi );
45   int dpi() const;
46   /**
47    * The most useful method of this class: convert the specified
48    * \p value from the unit \p from to the unit \p to, using the
49    * specified \p dpi in case of conversions from/to pixels.
50    */
51   static double convert( double value, Unit::MetricalUnit from, Unit::MetricalUnit to, int dpi = 1 );
52   /**
53    * Get a list of the supported metrical units.
54    */
55   static QStringList unitList();
56   static Unit::MetricalUnit intToUnit( int index );
57 
58   /**
59    * How many decimals the \p unit have.
60    */
61   static int precision( Unit::MetricalUnit unit );
62 
63   Unit& operator=( const Unit& u );
64 
65 private:
66   double mvalue;
67   Unit::MetricalUnit munit;
68   int mdpi;
69 };
70 
71 #endif
72