Changeset View
Changeset View
Standalone View
Standalone View
src/common/KReportUnit.h
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
---|---|---|---|---|---|
2 | Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org> | 2 | Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org> | ||
3 | Copyright (C) 1998, 1999 Torben Weis <weis@kde.org> | 3 | Copyright (C) 1998, 1999 Torben Weis <weis@kde.org> | ||
4 | Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org> | 4 | Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org> | ||
5 | Copyright (C) 2010 Thomas Zander <zander@kde.org> | 5 | Copyright (C) 2010 Thomas Zander <zander@kde.org> | ||
6 | Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org> | 6 | Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org> | ||
7 | Copyright (C) 2017 Jarosław Staniek <staniek@kde.org> | ||||
7 | 8 | | |||
8 | This library is free software; you can redistribute it and/or | 9 | This library is free software; you can redistribute it and/or | ||
9 | modify it under the terms of the GNU Library General Public | 10 | modify it under the terms of the GNU Library General Public | ||
10 | License as published by the Free Software Foundation; either | 11 | License as published by the Free Software Foundation; either | ||
11 | version 2 of the License, or (at your option) any later version. | 12 | version 2 of the License, or (at your option) any later version. | ||
12 | 13 | | |||
13 | This library is distributed in the hope that it will be useful, | 14 | This library is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | Library General Public License for more details. | 17 | Library General Public License for more details. | ||
17 | 18 | | |||
18 | You should have received a copy of the GNU Library General Public License | 19 | You should have received a copy of the GNU Library General Public License | ||
19 | along with this library; see the file COPYING.LIB. If not, write to | 20 | along with this library; see the file COPYING.LIB. If not, write to | ||
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 21 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
21 | * Boston, MA 02110-1301, USA. | 22 | * Boston, MA 02110-1301, USA. | ||
22 | */ | 23 | */ | ||
23 | 24 | | |||
24 | #ifndef KREPORTUNIT_H | 25 | #ifndef KREPORTUNIT_H | ||
25 | #define KREPORTUNIT_H | 26 | #define KREPORTUNIT_H | ||
26 | 27 | | |||
27 | #include <math.h> // for floor | 28 | #include <math.h> // for floor | ||
28 | 29 | | |||
30 | #include <QCoreApplication> | ||||
29 | #include <QString> | 31 | #include <QString> | ||
30 | #include <QDebug> | 32 | #include <QDebug> | ||
31 | #include <QMetaType> | 33 | #include <QMetaType> | ||
32 | 34 | | |||
33 | #include "kreport_export.h" | 35 | #include "kreport_export.h" | ||
34 | 36 | | |||
35 | class QStringList; | | |||
36 | | ||||
37 | // 1 inch ^= 72 pt | 37 | // 1 inch ^= 72 pt | ||
38 | // 1 inch ^= 25.399956 mm (-pedantic ;p) | 38 | // 1 inch ^= 25.399956 mm (-pedantic ;p) | ||
39 | // 1 pt = 1/12 pi | 39 | // 1 pt = 1/12 pi | ||
40 | // 1 pt ^= 0.0077880997 cc | 40 | // 1 pt ^= 0.0077880997 cc | ||
41 | // 1 cc = 12 dd | 41 | // 1 cc = 12 dd | ||
42 | // Note: I don't use division but multiplication with the inverse value | 42 | // Note: I don't use division but multiplication with the inverse value | ||
43 | // because it's faster ;p (Werner) | 43 | // because it's faster ;p (Werner) | ||
44 | #define POINT_TO_MM(px) qreal((px)*0.352777167) | 44 | #define POINT_TO_MM(px) qreal((px)*0.352777167) | ||
Show All 10 Lines | |||||
55 | #define POINT_TO_CC(px) qreal((px)*0.077880997) | 55 | #define POINT_TO_CC(px) qreal((px)*0.077880997) | ||
56 | #define PI_TO_POINT(pi) qreal((pi)*12) | 56 | #define PI_TO_POINT(pi) qreal((pi)*12) | ||
57 | #define CC_TO_POINT(cc) qreal((cc)*12.840103) | 57 | #define CC_TO_POINT(cc) qreal((cc)*12.840103) | ||
58 | /** | 58 | /** | ||
59 | * %KReport stores everything in pt (using "qreal") internally. | 59 | * %KReport stores everything in pt (using "qreal") 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 | * For implementing the selection of a unit type in the UI use the *ForUi() methods. | 63 | * For implementing the selection of a unit type in the UI use the allTypes() method. | ||
64 | * They ensure the same order of the unit types in all places, with the order not | 64 | * it ensure the same order of the unit types in all places, with the order not | ||
65 | * bound to the order in the enum (so ABI-compatible extension is possible) and | 65 | * bound to the order in the enum so ABI-compatible extension is possible. | ||
66 | * with the order and scope of listed types controlled by the @c ListOptions parameter. | | |||
67 | */ | 66 | */ | ||
68 | class KREPORT_EXPORT KReportUnit | 67 | class KREPORT_EXPORT KReportUnit | ||
69 | { | 68 | { | ||
69 | Q_DECLARE_TR_FUNCTIONS(KReportUnit) | ||||
70 | public: | 70 | public: | ||
71 | /** Length units supported by %KReport. */ | 71 | /** Length units supported by %KReport. */ | ||
72 | enum Type { | 72 | enum class Type { | ||
73 | Millimeter = 0, | 73 | Invalid, | ||
74 | Point, ///< Postscript point, 1/72th of an Inco | 74 | Millimeter, | ||
75 | Inch, | | |||
76 | Centimeter, | 75 | Centimeter, | ||
77 | Decimeter, | 76 | Decimeter, | ||
77 | Inch, | ||||
78 | Pica, | 78 | Pica, | ||
79 | Cicero, | 79 | Cicero, | ||
80 | Point, ///< Postscript point, 1/72th of an Inco | ||||
80 | Pixel, | 81 | Pixel, | ||
81 | TypeCount ///< @internal | 82 | Last = Pixel ///< internal | ||
82 | }; | 83 | }; | ||
83 | 84 | | |||
84 | /// Used to control the scope of the unit types listed in the UI | 85 | /** | ||
85 | enum ListOption { | 86 | * @brief Constructs invalid unit | ||
86 | ListAll = 0, | 87 | * | ||
87 | HidePixel = 1, | 88 | * @since 3.1 | ||
88 | HideMask = HidePixel | 89 | */ | ||
89 | }; | 90 | KReportUnit(); | ||
90 | Q_DECLARE_FLAGS(ListOptions, ListOption) | | |||
91 | 91 | | |||
92 | /** Construction requires initialization. The factor is for variable factor units like pixel */ | 92 | /** Construct unit with given type and factor. */ | ||
93 | explicit KReportUnit(Type type = Point, qreal factor = 1.0); | 93 | explicit KReportUnit(Type type, qreal factor = 1.0); | ||
94 | 94 | | |||
95 | KReportUnit(const KReportUnit &other); | 95 | KReportUnit(const KReportUnit &other); | ||
96 | 96 | | |||
97 | ~KReportUnit(); | 97 | ~KReportUnit(); | ||
98 | 98 | | |||
99 | /// Assigns specified type and factor 1.0 to the object | 99 | /// Assigns specified type and factor 1.0 to the object | ||
100 | /// @param unit Type of unit | 100 | /// @param unit Type of unit | ||
101 | KReportUnit& operator=(Type type); | 101 | KReportUnit& operator=(Type type); | ||
102 | 102 | | |||
103 | KReportUnit& operator=(const KReportUnit& other); | 103 | KReportUnit& operator=(const KReportUnit& other); | ||
104 | 104 | | |||
105 | bool operator!=(const KReportUnit &other) const; | 105 | bool operator!=(const KReportUnit &other) const; | ||
106 | 106 | | |||
107 | bool operator==(const KReportUnit &other) const; | 107 | bool operator==(const KReportUnit &other) const; | ||
108 | 108 | | |||
109 | /** | ||||
110 | * @brief Returns true if type of this unit is valid | ||||
111 | * | ||||
112 | * @since 3.1 | ||||
113 | */ | ||||
114 | bool isValid() const; | ||||
115 | | ||||
116 | /** | ||||
117 | * @brief Returns list of all supported types (without Invalid) | ||||
118 | * | ||||
119 | * @since 3.1 | ||||
120 | */ | ||||
121 | static QList<Type> allTypes(); | ||||
122 | | ||||
123 | /** Returns the type of this unit */ | ||||
109 | KReportUnit::Type type() const; | 124 | KReportUnit::Type type() const; | ||
110 | 125 | | |||
111 | void setFactor(qreal factor); | 126 | /** | ||
127 | * @brief Returns (translated) description string for type of this unit | ||||
128 | * | ||||
129 | * @since 3.1 | ||||
130 | */ | ||||
131 | QString description() const; | ||||
112 | 132 | | |||
113 | qreal factor() const; | 133 | /** | ||
134 | * @brief Returns (translated) description string for given unit type | ||||
135 | * | ||||
136 | * @since 3.1 | ||||
137 | */ | ||||
138 | static QString description(KReportUnit::Type type); | ||||
114 | 139 | | |||
115 | /** Returns a KReportUnit instance with the type at the @p index of the UI list with the given @p listOptions. */ | 140 | /** | ||
116 | static KReportUnit fromListForUi(int index, ListOptions listOptions = ListAll, qreal factor = 1.0); | 141 | * @brief Returns the list of (translated) description strings for given list of types | ||
142 | * | ||||
143 | * @since 3.1 | ||||
144 | */ | ||||
145 | static QStringList descriptions(const QList<Type> &types); | ||||
117 | 146 | | |||
118 | /// Convert a unit symbol string into a KReportUnit | 147 | void setFactor(qreal factor); | ||
119 | /// @param symbol symbol to convert | 148 | | ||
120 | /// @param ok if set, it will be true if the unit was known, false if unknown | 149 | qreal factor() const; | ||
121 | static KReportUnit fromSymbol(const QString &symbol, bool *ok = nullptr); | | |||
122 | 150 | | |||
123 | /** | 151 | /** | ||
124 | * Prepare ptValue to be displayed in pt | 152 | * Prepare ptValue to be displayed in pt | ||
125 | * This method will round to 0.001 precision | 153 | * This method will round to 0.001 precision | ||
126 | */ | 154 | */ | ||
127 | static inline qreal toPoint(qreal ptValue) | 155 | static inline qreal toPoint(qreal ptValue) | ||
128 | { | 156 | { | ||
129 | // No conversion, only rounding (to 0.001 precision) | 157 | // No conversion, only rounding (to 0.001 precision) | ||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Line(s) | 214 | { | |||
188 | return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; | 216 | return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; | ||
189 | } | 217 | } | ||
190 | 218 | | |||
191 | /** | 219 | /** | ||
192 | * convert the given value directly from one unit to another | 220 | * convert the given value directly from one unit to another | ||
193 | */ | 221 | */ | ||
194 | static qreal convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor = 1.0); | 222 | static qreal convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor = 1.0); | ||
195 | 223 | | |||
196 | | ||||
197 | /** | 224 | /** | ||
198 | * This method is the one to use to display a value in a dialog | 225 | * This method is the one to use to display a value in a dialog | ||
199 | * \return the value @p ptValue converted to unit and rounded, ready to be displayed | 226 | * \return the value @p ptValue converted to unit and rounded, ready to be displayed | ||
200 | */ | 227 | */ | ||
201 | qreal toUserValue(qreal ptValue) const; | 228 | qreal toUserValue(qreal ptValue) const; | ||
202 | 229 | | |||
203 | /** | 230 | /** | ||
204 | * Convert the value @p ptValue to a given unit @p unit | 231 | * Convert the value @p ptValue to a given unit @p unit | ||
Show All 12 Lines | |||||
217 | 244 | | |||
218 | /// This method is the one to use to read a value from a dialog | 245 | /// This method is the one to use to read a value from a dialog | ||
219 | /// @param value value entered by the user | 246 | /// @param value value entered by the user | ||
220 | /// @param ok if set, the pointed bool is set to true if the value could be | 247 | /// @param ok if set, the pointed bool is set to true if the value could be | ||
221 | /// converted to a qreal, and to false otherwise. | 248 | /// converted to a qreal, and to false otherwise. | ||
222 | /// @return the value converted to points for internal use | 249 | /// @return the value converted to points for internal use | ||
223 | qreal fromUserValue(const QString &value, bool *ok = nullptr) const; | 250 | qreal fromUserValue(const QString &value, bool *ok = nullptr) const; | ||
224 | 251 | | |||
225 | /// Get the description string of the given unit | 252 | //! Returns the symbol string of given unit type | ||
226 | static QString unitDescription(KReportUnit::Type type); | 253 | //! Symbol for Invalid type is empty string. | ||
254 | static QString symbol(KReportUnit::Type type); | ||||
227 | 255 | | |||
228 | /// Get the symbol string of the unit | 256 | //! Returns the symbol string of the unit | ||
257 | //! Symbol for Invalid type is empty string. | ||||
229 | QString symbol() const; | 258 | QString symbol() const; | ||
230 | 259 | | |||
231 | /// Returns the list of unit types for the UI, controlled with the given @p listOptions. | 260 | /** | ||
232 | static QStringList listOfUnitNameForUi(ListOptions listOptions = ListAll); | 261 | * Equal to symbol(): returns the symbol string of the unit. | ||
262 | */ | ||||
263 | inline QString toString() const | ||||
264 | { | ||||
265 | return symbol(); | ||||
266 | } | ||||
233 | 267 | | |||
234 | /// Get the index of this unit in the list of unit types for the UI, | 268 | /** | ||
235 | /// if it is controlled with the given @p listOptions. | 269 | * @brief Returns a unit symbol string to type | ||
236 | int indexInListForUi(ListOptions listOptions = ListAll) const; | 270 | * | ||
271 | * @param symbol symbol to convert, must be lowercase | ||||
272 | * @return Invalid type for unsupported symbol | ||||
273 | * | ||||
274 | * @since 3.1 | ||||
275 | */ | ||||
276 | static KReportUnit::Type symbolToType(const QString &symbol); | ||||
277 | | ||||
278 | /** | ||||
279 | * @brief Returns the list of unit symbols for the given types | ||||
280 | * | ||||
281 | * @since 3.1 | ||||
282 | */ | ||||
283 | static QStringList symbols(const QList<Type> &types); | ||||
237 | 284 | | |||
238 | /// Parses common %KReport and ODF values, like "10cm", "5mm" to pt. | 285 | /// Parses common %KReport and ODF values, like "10cm", "5mm" to pt. | ||
239 | /// If no unit is specified, pt is assumed. | 286 | /// If no unit is specified, pt is assumed. | ||
240 | static qreal parseValue(const QString &value, qreal defaultVal = 0.0); | 287 | static qreal parseValue(const QString &value, qreal defaultVal = 0.0); | ||
241 | 288 | | |||
242 | /// parse an angle to its value in degrees | 289 | /// parse an angle to its value in degrees | ||
243 | static qreal parseAngle(const QString &value, qreal defaultVal = 0.0); | 290 | static qreal parseAngle(const QString &value, qreal defaultVal = 0.0); | ||
244 | 291 | | |||
245 | /** | | |||
246 | * Equal to symbol(): returns the symbol string of the unit. | | |||
247 | */ | | |||
248 | inline QString toString() const | | |||
249 | { | | |||
250 | return symbol(); | | |||
251 | } | | |||
252 | | ||||
253 | private: | 292 | private: | ||
254 | class Private; | 293 | class Private; | ||
255 | Private * const d; | 294 | Private * const d; | ||
256 | }; | 295 | }; | ||
257 | 296 | | |||
258 | #ifndef QT_NO_DEBUG_STREAM | 297 | #ifndef QT_NO_DEBUG_STREAM | ||
259 | KREPORT_EXPORT QDebug operator<<(QDebug, const KReportUnit &); | 298 | KREPORT_EXPORT QDebug operator<<(QDebug, const KReportUnit &); | ||
260 | #endif | 299 | #endif | ||
261 | 300 | | |||
262 | Q_DECLARE_METATYPE(KReportUnit) | 301 | Q_DECLARE_METATYPE(KReportUnit) | ||
263 | Q_DECLARE_OPERATORS_FOR_FLAGS(KReportUnit::ListOptions) | | |||
264 | 302 | | |||
265 | #endif | 303 | #endif |