Changeset View
Changeset View
Standalone View
Standalone View
src/part/KgvUnit.h
Show All 22 Lines | 19 | /* This file was part of the KDE project | |||
---|---|---|---|---|---|
23 | modify it under the terms of the GNU Library General Public | 23 | modify it under the terms of the GNU Library General Public | ||
24 | License as published by the Free Software Foundation; either | 24 | License as published by the Free Software Foundation; either | ||
25 | version 2 of the License, or (at your option) any later version. | 25 | version 2 of the License, or (at your option) any later version. | ||
26 | */ | 26 | */ | ||
27 | 27 | | |||
28 | #ifndef KGVUNIT_H | 28 | #ifndef KGVUNIT_H | ||
29 | #define KGVUNIT_H | 29 | #define KGVUNIT_H | ||
30 | 30 | | |||
31 | #include <math.h> // for floor | ||||
31 | #include <qstring.h> | 32 | #include <qstring.h> | ||
32 | #include <qstringlist.h> | 33 | #include <qstringlist.h> | ||
33 | #include <math.h> // for floor | | |||
34 | 34 | | |||
35 | // 1 inch ^= 72 pt | 35 | // 1 inch ^= 72 pt | ||
36 | // 1 inch ^= 25.399956 mm (-pedantic ;p) | 36 | // 1 inch ^= 25.399956 mm (-pedantic ;p) | ||
37 | // 1 pt = 1/12 pi | 37 | // 1 pt = 1/12 pi | ||
38 | // 1 pt ^= 0.0077880997 cc | 38 | // 1 pt ^= 0.0077880997 cc | ||
39 | // 1 cc = 12 dd | 39 | // 1 cc = 12 dd | ||
40 | // Note: I don't use division but multiplication with the inverse value | 40 | // Note: I don't use division but multiplication with the inverse value | ||
41 | // because it's faster ;p (Werner) | 41 | // because it's faster ;p (Werner) | ||
42 | #define POINT_TO_MM(px) ((px)*0.352777167) | 42 | #define POINT_TO_MM(px) ((px)*0.352777167) | ||
43 | #define MM_TO_POINT(mm) ((mm)*2.83465058) | 43 | #define MM_TO_POINT(mm) ((mm)*2.83465058) | ||
44 | #define POINT_TO_CM(px) ((px)*0.0352777167) | 44 | #define POINT_TO_CM(px) ((px)*0.0352777167) | ||
45 | #define CM_TO_POINT(cm) ((cm)*28.3465058) | 45 | #define CM_TO_POINT(cm) ((cm)*28.3465058) | ||
46 | #define POINT_TO_DM(px) ((px)*0.00352777167) | 46 | #define POINT_TO_DM(px) ((px)*0.00352777167) | ||
47 | #define DM_TO_POINT(dm) ((dm)*283.465058) | 47 | #define DM_TO_POINT(dm) ((dm)*283.465058) | ||
48 | #define POINT_TO_INCH(px) ((px)*0.01388888888889) | 48 | #define POINT_TO_INCH(px) ((px)*0.01388888888889) | ||
49 | #define INCH_TO_POINT(inch) ((inch)*72.0) | 49 | #define INCH_TO_POINT(inch) ((inch)*72.0) | ||
50 | #define MM_TO_INCH(mm) ((mm)*0.039370147) | 50 | #define MM_TO_INCH(mm) ((mm)*0.039370147) | ||
51 | #define INCH_TO_MM(inch) ((inch)*25.399956) | 51 | #define INCH_TO_MM(inch) ((inch)*25.399956) | ||
52 | #define POINT_TO_PI(px)((px)*0.083333333) | 52 | #define POINT_TO_PI(px) ((px)*0.083333333) | ||
53 | #define POINT_TO_DD(px)((px)*0.006490083) | 53 | #define POINT_TO_DD(px) ((px)*0.006490083) | ||
54 | #define POINT_TO_CC(px)((px)*0.077880997) | 54 | #define POINT_TO_CC(px) ((px)*0.077880997) | ||
55 | #define PI_TO_POINT(pi)((pi)*12) | 55 | #define PI_TO_POINT(pi) ((pi)*12) | ||
56 | #define DD_TO_POINT(dd)((dd)*154.08124) | 56 | #define DD_TO_POINT(dd) ((dd)*154.08124) | ||
57 | #define CC_TO_POINT(cc)((cc)*12.840103) | 57 | #define CC_TO_POINT(cc) ((cc)*12.840103) | ||
58 | /** | 58 | /** | ||
59 | * %KOffice stores everything in pt (using "double") internally. | 59 | * %KOffice stores everything in pt (using "double") internally. | ||
60 | * When displaying a value to the user, the value is converted to the user's unit | 60 | * When displaying a value to the user, the value is converted to the user's unit | ||
61 | * of choice, and rounded to a reasonable precision to avoid 0.999999 | 61 | * of choice, and rounded to a reasonable precision to avoid 0.999999 | ||
62 | */ | 62 | */ | ||
63 | class KgvUnit | 63 | class KgvUnit | ||
64 | { | 64 | { | ||
65 | public: | 65 | public: | ||
66 | /** Length units supported by KOffice. */ | 66 | /** Length units supported by KOffice. */ | ||
67 | enum Unit { | 67 | enum Unit { | ||
68 | U_MM = 0, | 68 | U_MM = 0, | ||
69 | U_PT = 1, | 69 | U_PT = 1, | ||
70 | U_INCH = 2, | 70 | U_INCH = 2, | ||
71 | U_CM = 3, | 71 | U_CM = 3, | ||
72 | U_DM = 4, | 72 | U_DM = 4, | ||
73 | U_PI = 5, // pica | 73 | U_PI = 5, // pica | ||
74 | U_DD = 6, // didot | 74 | U_DD = 6, // didot | ||
75 | U_CC = 7, // cicero | 75 | U_CC = 7, // cicero | ||
76 | U_LASTUNIT = U_CC // update when adding a new unit | 76 | U_LASTUNIT = U_CC // update when adding a new unit | ||
77 | // when adding a new unit, make sure to implement support for it in koRuler too | 77 | // when adding a new unit, make sure to implement support for it in koRuler too | ||
78 | }; | 78 | }; | ||
79 | 79 | | |||
80 | /// Prepare ptValue to be displayed in pt | 80 | /// Prepare ptValue to be displayed in pt | ||
81 | static double toPoint( double ptValue ) { | 81 | static double toPoint(double ptValue) | ||
82 | { | ||||
82 | // No conversion, only rounding (to 0.001 precision) | 83 | // No conversion, only rounding (to 0.001 precision) | ||
83 | return floor( ptValue * 1000.0 ) / 1000.0; | 84 | return floor(ptValue * 1000.0) / 1000.0; | ||
84 | } | 85 | } | ||
85 | 86 | | |||
86 | /// Prepare ptValue to be displayed in mm | 87 | /// Prepare ptValue to be displayed in mm | ||
87 | static double toMM( double ptValue ) { | 88 | static double toMM(double ptValue) | ||
89 | { | ||||
88 | // "mm" values are rounded to 0.0001 millimeters | 90 | // "mm" values are rounded to 0.0001 millimeters | ||
89 | return floor( POINT_TO_MM( ptValue ) * 10000.0 ) / 10000.0; | 91 | return floor(POINT_TO_MM(ptValue) * 10000.0) / 10000.0; | ||
90 | } | 92 | } | ||
91 | 93 | | |||
92 | /// Prepare ptValue to be displayed in cm | 94 | /// Prepare ptValue to be displayed in cm | ||
93 | static double toCM( double ptValue ) { | 95 | static double toCM(double ptValue) | ||
96 | { | ||||
94 | return floor( POINT_TO_CM( ptValue ) * 10000.0 ) / 10000.0; | 97 | return floor(POINT_TO_CM(ptValue) * 10000.0) / 10000.0; | ||
95 | } | 98 | } | ||
96 | 99 | | |||
97 | /// Prepare ptValue to be displayed in dm | 100 | /// Prepare ptValue to be displayed in dm | ||
98 | static double toDM( double ptValue ) { | 101 | static double toDM(double ptValue) | ||
102 | { | ||||
99 | return floor( POINT_TO_DM( ptValue ) * 10000.0 ) / 10000.0; | 103 | return floor(POINT_TO_DM(ptValue) * 10000.0) / 10000.0; | ||
100 | } | 104 | } | ||
101 | 105 | | |||
102 | /// Prepare ptValue to be displayed in inch | 106 | /// Prepare ptValue to be displayed in inch | ||
103 | static double toInch( double ptValue ) { | 107 | static double toInch(double ptValue) | ||
108 | { | ||||
104 | // "in" values are rounded to 0.00001 inches | 109 | // "in" values are rounded to 0.00001 inches | ||
105 | return floor( POINT_TO_INCH( ptValue ) * 100000.0 ) / 100000.0; | 110 | return floor(POINT_TO_INCH(ptValue) * 100000.0) / 100000.0; | ||
106 | } | 111 | } | ||
107 | 112 | | |||
108 | /// Prepare ptValue to be displayed in pica | 113 | /// Prepare ptValue to be displayed in pica | ||
109 | static double toPI( double ptValue ) { | 114 | static double toPI(double ptValue) | ||
115 | { | ||||
110 | // "pi" values are rounded to 0.00001 inches | 116 | // "pi" values are rounded to 0.00001 inches | ||
111 | return floor( POINT_TO_PI( ptValue ) * 100000.0 ) / 100000.0; | 117 | return floor(POINT_TO_PI(ptValue) * 100000.0) / 100000.0; | ||
112 | } | 118 | } | ||
113 | 119 | | |||
114 | /// Prepare ptValue to be displayed in didot | 120 | /// Prepare ptValue to be displayed in didot | ||
115 | static double toDD( double ptValue ) { | 121 | static double toDD(double ptValue) | ||
122 | { | ||||
116 | // "dd" values are rounded to 0.00001 inches | 123 | // "dd" values are rounded to 0.00001 inches | ||
117 | return floor( POINT_TO_DD( ptValue ) * 100000.0 ) / 100000.0; | 124 | return floor(POINT_TO_DD(ptValue) * 100000.0) / 100000.0; | ||
118 | } | 125 | } | ||
119 | 126 | | |||
120 | /// Prepare ptValue to be displayed in cicero | 127 | /// Prepare ptValue to be displayed in cicero | ||
121 | static double toCC( double ptValue ) { | 128 | static double toCC(double ptValue) | ||
129 | { | ||||
122 | // "cc" values are rounded to 0.00001 inches | 130 | // "cc" values are rounded to 0.00001 inches | ||
123 | return floor( POINT_TO_CC( ptValue ) * 100000.0 ) / 100000.0; | 131 | return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; | ||
124 | } | 132 | } | ||
125 | 133 | | |||
126 | /** | 134 | /** | ||
127 | * This method is the one to use to display a value in a dialog | 135 | * This method is the one to use to display a value in a dialog | ||
128 | * \return the value @p ptValue converted to @p unit and rounded, ready to be displayed | 136 | * \return the value @p ptValue converted to @p unit and rounded, ready to be displayed | ||
129 | * Old name: ptToUnit | 137 | * Old name: ptToUnit | ||
130 | */ | 138 | */ | ||
131 | static double toUserValue( double ptValue, Unit unit ); | 139 | static double toUserValue(double ptValue, Unit unit); | ||
132 | 140 | | |||
133 | /** | 141 | /** | ||
134 | * Convert the value @p ptValue to a given unit @p unit | 142 | * Convert the value @p ptValue to a given unit @p unit | ||
135 | * Unlike KoUnit::ptToUnit the return value remains unrounded, so that it can be used in complex calculation | 143 | * Unlike KoUnit::ptToUnit the return value remains unrounded, so that it can be used in complex calculation | ||
136 | * \return the converted value | 144 | * \return the converted value | ||
137 | * Old name: ptToUnitUnrounded | 145 | * Old name: ptToUnitUnrounded | ||
138 | */ | 146 | */ | ||
139 | static double ptToUnit( const double ptValue, const Unit unit ); | 147 | static double ptToUnit(const double ptValue, const Unit unit); | ||
140 | 148 | | |||
141 | /// This method is the one to use to display a value in a dialog | 149 | /// This method is the one to use to display a value in a dialog | ||
142 | /// @return the value @p ptValue converted to @p unit and rounded, ready to be displayed | 150 | /// @return the value @p ptValue converted to @p unit and rounded, ready to be displayed | ||
143 | /// Old name: userValue | 151 | /// Old name: userValue | ||
144 | static QString toUserStringValue( double ptValue, Unit unit ); | 152 | static QString toUserStringValue(double ptValue, Unit unit); | ||
145 | 153 | | |||
146 | /// This method is the one to use to read a value from a dialog | 154 | /// This method is the one to use to read a value from a dialog | ||
147 | /// @return the value in @p unit, converted to points for internal use | 155 | /// @return the value in @p unit, converted to points for internal use | ||
148 | /// Old name: ptFromUnit | 156 | /// Old name: ptFromUnit | ||
149 | static double fromUserValue( double value, Unit unit ); | 157 | static double fromUserValue(double value, Unit unit); | ||
150 | 158 | | |||
151 | /// This method is the one to use to read a value from a dialog | 159 | /// This method is the one to use to read a value from a dialog | ||
152 | /// @param value value entered by the user | 160 | /// @param value value entered by the user | ||
153 | /// @param unit unit type selected by the user | 161 | /// @param unit unit type selected by the user | ||
154 | /// @param ok if set, the pointed bool is set to true if the value could be | 162 | /// @param ok if set, the pointed bool is set to true if the value could be | ||
155 | /// converted to a double, and to false otherwise. | 163 | /// converted to a double, and to false otherwise. | ||
156 | /// @return the value in @p unit, converted to points for internal use | 164 | /// @return the value in @p unit, converted to points for internal use | ||
157 | static double fromUserValue(const QString& value, Unit unit, bool* ok = nullptr); | 165 | static double fromUserValue(const QString &value, Unit unit, bool *ok = nullptr); | ||
158 | 166 | | |||
159 | /// Convert a unit name into a Unit enum | 167 | /// Convert a unit name into a Unit enum | ||
160 | /// @param _unitName name to convert | 168 | /// @param _unitName name to convert | ||
161 | /// @param ok if set, it will be true if the unit was known, false if unknown | 169 | /// @param ok if set, it will be true if the unit was known, false if unknown | ||
162 | static Unit unit(const QString &_unitName, bool* ok = nullptr); | 170 | static Unit unit(const QString &_unitName, bool *ok = nullptr); | ||
163 | /// Get the name of a unit | 171 | /// Get the name of a unit | ||
164 | static QString unitName( Unit _unit ); | 172 | static QString unitName(Unit _unit); | ||
165 | /// Get the full (translated) description of a unit | 173 | /// Get the full (translated) description of a unit | ||
166 | static QString unitDescription( Unit _unit ); | 174 | static QString unitDescription(Unit _unit); | ||
167 | static QStringList listOfUnitName(); | 175 | static QStringList listOfUnitName(); | ||
168 | 176 | | |||
169 | /// parse common %KOffice and OO values, like "10cm", "5mm" to pt | 177 | /// parse common %KOffice and OO values, like "10cm", "5mm" to pt | ||
170 | static double parseValue( const QString& value, double defaultVal = 0.0 ); | 178 | static double parseValue(const QString &value, double defaultVal = 0.0); | ||
171 | // Note: the above method doesn't take a const ref, since it modifies the arg. | 179 | // Note: the above method doesn't take a const ref, since it modifies the arg. | ||
172 | | ||||
173 | }; | 180 | }; | ||
174 | 181 | | |||
175 | | ||||
176 | #endif // KGVUNIT_H | 182 | #endif // KGVUNIT_H |