Changeset View
Changeset View
Standalone View
Standalone View
libs/odf/KoUnit.h
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | |||||
51 | constexpr qreal INCH_TO_POINT(qreal inch) {return (inch)*72.0;} | 51 | constexpr qreal INCH_TO_POINT(qreal inch) {return (inch)*72.0;} | ||
52 | constexpr qreal MM_TO_INCH(qreal mm) {return (mm)*0.039370147;} | 52 | constexpr qreal MM_TO_INCH(qreal mm) {return (mm)*0.039370147;} | ||
53 | constexpr qreal INCH_TO_MM(qreal inch) {return (inch)*25.399956;} | 53 | constexpr qreal INCH_TO_MM(qreal inch) {return (inch)*25.399956;} | ||
54 | constexpr qreal POINT_TO_PI(qreal px) {return (px)*0.083333333;} | 54 | constexpr qreal POINT_TO_PI(qreal px) {return (px)*0.083333333;} | ||
55 | constexpr qreal POINT_TO_CC(qreal px) {return (px)*0.077880997;} | 55 | constexpr qreal POINT_TO_CC(qreal px) {return (px)*0.077880997;} | ||
56 | constexpr qreal PI_TO_POINT(qreal pi) {return (pi)*12;} | 56 | constexpr qreal PI_TO_POINT(qreal pi) {return (pi)*12;} | ||
57 | constexpr qreal CC_TO_POINT(qreal cc) {return (cc)*12.840103;} | 57 | constexpr qreal CC_TO_POINT(qreal cc) {return (cc)*12.840103;} | ||
58 | 58 | | |||
59 | | ||||
60 | static const qreal PT_ROUNDING {1000.0}; | ||||
61 | //static const qreal PX_ROUNDING {1000.0}; // pixel value is not to be rounded | ||||
62 | | ||||
63 | static const qreal CM_ROUNDING {10000.0}; | ||||
64 | static const qreal DM_ROUNDING {10000.0}; | ||||
65 | static const qreal MM_ROUNDING {10000.0}; | ||||
66 | | ||||
67 | static const qreal IN_ROUNDING {100000.0}; | ||||
68 | | ||||
69 | static const qreal PI_ROUNDING {100000.0}; // pico | ||||
70 | static const qreal CC_ROUNDING {100000.0}; // cicero | ||||
71 | | ||||
72 | | ||||
59 | /** | 73 | /** | ||
60 | * %Calligra stores everything in pt (using "qreal") internally. | 74 | * %Calligra stores everything in pt (using "qreal") internally. | ||
61 | * When displaying a value to the user, the value is converted to the user's unit | 75 | * When displaying a value to the user, the value is converted to the user's unit | ||
62 | * of choice, and rounded to a reasonable precision to avoid 0.999999 | 76 | * of choice, and rounded to a reasonable precision to avoid 0.999999 | ||
63 | * | 77 | * | ||
64 | * For implementing the selection of a unit type in the UI use the *ForUi() methods. | 78 | * For implementing the selection of a unit type in the UI use the *ForUi() methods. | ||
65 | * They ensure the same order of the unit types in all places, with the order not | 79 | * They ensure the same order of the unit types in all places, with the order not | ||
66 | * bound to the order in the enum (so ABI-compatible extension is possible) and | 80 | * bound to the order in the enum (so ABI-compatible extension is possible) and | ||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | 85 | public: | |||
120 | 134 | | |||
121 | KoUnit::Type type() const { | 135 | KoUnit::Type type() const { | ||
122 | return m_type; | 136 | return m_type; | ||
123 | } | 137 | } | ||
124 | 138 | | |||
125 | void setFactor(qreal factor) { | 139 | void setFactor(qreal factor) { | ||
126 | m_pixelConversion = factor; | 140 | m_pixelConversion = factor; | ||
127 | } | 141 | } | ||
128 | /** | | |||
129 | * Prepare ptValue to be displayed in pt | | |||
130 | * This method will round to 0.001 precision | | |||
131 | */ | | |||
132 | static qreal toPoint(qreal ptValue) { | | |||
133 | // No conversion, only rounding (to 0.001 precision) | | |||
134 | return floor(ptValue * 1000.0) / 1000.0; | | |||
135 | } | | |||
136 | | ||||
137 | /** | | |||
138 | * Prepare ptValue to be displayed in mm | | |||
139 | * This method will round to 0.0001 precision, use POINT_TO_MM() for lossless conversion. | | |||
140 | */ | | |||
141 | static qreal toMillimeter(qreal ptValue) { | | |||
142 | // "mm" values are rounded to 0.0001 millimeters | | |||
143 | return floor(POINT_TO_MM(ptValue) * 10000.0) / 10000.0; | | |||
144 | } | | |||
145 | | ||||
146 | /** | | |||
147 | * Prepare ptValue to be displayed in cm | | |||
148 | * This method will round to 0.0001 precision, use POINT_TO_CM() for lossless conversion. | | |||
149 | */ | | |||
150 | static qreal toCentimeter(qreal ptValue) { | | |||
151 | return floor(POINT_TO_CM(ptValue) * 10000.0) / 10000.0; | | |||
152 | } | | |||
153 | 142 | | |||
154 | /** | | |||
155 | * Prepare ptValue to be displayed in dm | | |||
156 | * This method will round to 0.0001 precision, use POINT_TO_DM() for lossless conversion. | | |||
157 | */ | | |||
158 | static qreal toDecimeter(qreal ptValue) { | | |||
159 | return floor(POINT_TO_DM(ptValue) * 10000.0) / 10000.0; | | |||
160 | } | | |||
161 | 143 | | |||
162 | /** | 144 | /** | ||
163 | * Prepare ptValue to be displayed in inch | 145 | * convert the given value directly from one unit to another | ||
164 | * This method will round to 0.00001 precision, use POINT_TO_INCH() for lossless conversion. | | |||
165 | */ | 146 | */ | ||
166 | static qreal toInch(qreal ptValue) { | 147 | static qreal convertFromUnitToUnit(const qreal value, const KoUnit &fromUnit, const KoUnit &toUnit, qreal factor = 1.0); | ||
167 | // "in" values are rounded to 0.00001 inches | | |||
168 | return floor(POINT_TO_INCH(ptValue) * 100000.0) / 100000.0; | | |||
169 | } | | |||
170 | 148 | | |||
171 | /** | 149 | /** | ||
172 | * Prepare ptValue to be displayed in pica | 150 | * Convert the value @p ptValue to the unit and round it. | ||
173 | * This method will round to 0.00001 precision, use POINT_TO_PI() for lossless conversion. | 151 | * This method is meant to be used to display a value in a dialog. | ||
152 | * \return the converted and rounded value | ||||
174 | */ | 153 | */ | ||
175 | static qreal toPica(qreal ptValue) { | | |||
176 | // "pi" values are rounded to 0.00001 inches | | |||
177 | return floor(POINT_TO_PI(ptValue) * 100000.0) / 100000.0; | | |||
178 | } | | |||
179 | 154 | | |||
155 | qreal toUserValueRounded(const qreal value) const; | ||||
180 | /** | 156 | /** | ||
181 | * Prepare ptValue to be displayed in cicero | 157 | * Convert the value @p ptValue to the unit (without rounding) | ||
182 | * This method will round to 0.00001 precision, use POINT_TO_CC() for lossless conversion. | 158 | * This method is meant to be used in complex calculations. | ||
159 | * \return the converted value | ||||
183 | */ | 160 | */ | ||
184 | static qreal toCicero(qreal ptValue) { | 161 | qreal toUserValuePrecise(const qreal ptValue) const; | ||
185 | // "cc" values are rounded to 0.00001 inches | | |||
186 | return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; | | |||
187 | } | | |||
188 | 162 | | |||
189 | /** | 163 | /** | ||
190 | * convert the given value directly from one unit to another | 164 | * Convert the value @p ptValue with or with rounding as indicated by @p rounding. | ||
165 | * This method is a proxy to toUserValuePrecise and toUserValueRounded. | ||||
166 | * \return the value @p ptValue converted to unit | ||||
191 | */ | 167 | */ | ||
192 | static qreal convertFromUnitToUnit(const qreal value, const KoUnit &fromUnit, const KoUnit &toUnit, qreal factor = 1.0); | 168 | qreal toUserValue(qreal ptValue, bool rounding = true) const; | ||
193 | 169 | | |||
194 | 170 | | |||
195 | /** | | |||
196 | * This method is the one to use to display a value in a dialog | | |||
197 | * \return the value @p ptValue converted to unit and rounded, ready to be displayed | | |||
198 | */ | | |||
199 | qreal toUserValue(qreal ptValue) const; | | |||
200 | | ||||
201 | /** | | |||
202 | * Convert the value @p ptValue to a given unit @p unit | | |||
203 | * Unlike KoUnit::ptToUnit the return value remains unrounded, so that it can be used in complex calculation | | |||
204 | * \return the converted value | | |||
205 | */ | | |||
206 | static qreal ptToUnit(const qreal ptValue, const KoUnit &unit); | | |||
207 | 171 | | |||
208 | /// This method is the one to use to display a value in a dialog | 172 | /// This method is the one to use to display a value in a dialog | ||
209 | /// @return the value @p ptValue converted the unit and rounded, ready to be displayed | 173 | /// @return the value @p ptValue converted the unit and rounded, ready to be displayed | ||
210 | QString toUserStringValue(qreal ptValue) const; | 174 | QString toUserStringValue(qreal ptValue) const; | ||
211 | 175 | | |||
212 | /// This method is the one to use to read a value from a dialog | 176 | /// This method is the one to use to read a value from a dialog | ||
213 | /// @return the value converted to points for internal use | 177 | /// @return the value converted to points for internal use | ||
214 | qreal fromUserValue(qreal value) const; | 178 | qreal fromUserValue(qreal value) const; | ||
▲ Show 20 Lines • Show All 62 Lines • Show Last 20 Lines |