Changeset View
Changeset View
Standalone View
Standalone View
src/common/KReportUnit.h
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Line(s) | 72 | enum Type { | |||
---|---|---|---|---|---|
77 | Decimeter, | 77 | Decimeter, | ||
78 | Pica, | 78 | Pica, | ||
79 | Cicero, | 79 | Cicero, | ||
80 | Pixel, | 80 | Pixel, | ||
81 | TypeCount ///< @internal | 81 | TypeCount ///< @internal | ||
82 | }; | 82 | }; | ||
83 | 83 | | |||
84 | /// Used to control the scope of the unit types listed in the UI | 84 | /// Used to control the scope of the unit types listed in the UI | ||
85 | enum ListOption { | 85 | enum ListOption { | ||
staniek: Note for later: I plan something for ListOption: T4967 | |||||
86 | ListAll = 0, | 86 | ListAll = 0, | ||
87 | HidePixel = 1, | 87 | HidePixel = 1, | ||
88 | HideMask = HidePixel | 88 | HideMask = HidePixel | ||
89 | }; | 89 | }; | ||
90 | Q_DECLARE_FLAGS(ListOptions, ListOption) | 90 | Q_DECLARE_FLAGS(ListOptions, ListOption) | ||
91 | 91 | | |||
92 | /** Returns a KReportUnit instance with the type at the @p index of the UI list with the given @p listOptions. */ | 92 | /** Returns a KReportUnit instance with the type at the @p index of the UI list with the given @p listOptions. */ | ||
Sorry for asking for more but when we're at the topic of ctors, can we move these two statics to line 123 or so where the statics are? staniek: Sorry for asking for more but when we're at the topic of ctors, can we move these two statics… | |||||
93 | static KReportUnit fromListForUi(int index, ListOptions listOptions = ListAll, qreal factor = 1.0); | 93 | static KReportUnit fromListForUi(int index, ListOptions listOptions = ListAll, qreal factor = 1.0); | ||
94 | 94 | | |||
95 | /// Convert a unit symbol string into a KReportUnit | 95 | /// Convert a unit symbol string into a KReportUnit | ||
96 | /// @param symbol symbol to convert | 96 | /// @param symbol symbol to convert | ||
97 | /// @param ok if set, it will be true if the unit was known, false if unknown | 97 | /// @param ok if set, it will be true if the unit was known, false if unknown | ||
98 | static KReportUnit fromSymbol(const QString &symbol, bool *ok = 0); | 98 | static KReportUnit fromSymbol(const QString &symbol, bool *ok = 0); | ||
99 | 99 | | |||
100 | /** Construction requires initialization. The factor is for variable factor units like pixel */ | 100 | /** Construction requires initialization. The factor is for variable factor units like pixel */ | ||
101 | explicit KReportUnit(Type unit = Point, qreal factor = 1.0) { | 101 | explicit KReportUnit(Type type = Point, qreal factor = 1.0); | ||
102 | m_type = unit; | | |||
103 | m_pixelConversion = factor; | | |||
104 | } | | |||
105 | 102 | | |||
106 | KReportUnit& operator=(Type unit) { | 103 | KReportUnit(const KReportUnit &other); | ||
107 | m_type = unit; m_pixelConversion = 1.0; return *this; | | |||
108 | } | | |||
109 | 104 | | |||
110 | bool operator!=(const KReportUnit &other) const { | 105 | ~KReportUnit(); | ||
staniek: Needs docs like this:
`Assigns specified type and factor 1.0 to the object` | |||||
staniek: unit -> type? | |||||
111 | return !operator==(other); | | |||
112 | } | | |||
113 | 106 | | |||
114 | bool operator==(const KReportUnit &other) const { | 107 | /// Assigns specified type and factor 1.0 to the object | ||
If we have operator= then also the copy ctor needs to be implemented explicitely. staniek: If we have operator= then also the copy ctor needs to be implemented explicitely.
| |||||
115 | return m_type == other.m_type && | 108 | /// @param unit Type of unit | ||
116 | (m_type != Pixel || | 109 | KReportUnit& operator=(Type type); | ||
117 | qFuzzyCompare(m_pixelConversion, other.m_pixelConversion)); | | |||
118 | } | | |||
119 | 110 | | |||
120 | KReportUnit::Type type() const { | 111 | KReportUnit& operator=(const KReportUnit& other); | ||
121 | return m_type; | 112 | | ||
122 | } | 113 | bool operator!=(const KReportUnit &other) const; | ||
114 | | ||||
115 | bool operator==(const KReportUnit &other) const; | ||||
116 | | ||||
117 | KReportUnit::Type type() const; | ||||
118 | | ||||
119 | void setFactor(qreal factor); | ||||
120 | | ||||
121 | qreal factor() const; | ||||
123 | 122 | | |||
staniek: unit -> type? | |||||
im not sure...the whole class is about Units ... what about Type > Unit? or leave? piggz: im not sure...the whole class is about Units ... what about Type > Unit? or leave? | |||||
I understand Type as a type of unit. KReportUnit::Unit sounds problematic, KReportUnit::Type sounds ok. Since the type name is Type then I proposed name of the argument to be called type to also follow that term. staniek: I understand `Type` as a type of unit. KReportUnit::Unit sounds problematic, KReportUnit::Type… | |||||
124 | void setFactor(qreal factor) { | | |||
125 | m_pixelConversion = factor; | | |||
126 | } | | |||
127 | /** | 123 | /** | ||
128 | * Prepare ptValue to be displayed in pt | 124 | * Prepare ptValue to be displayed in pt | ||
129 | * This method will round to 0.001 precision | 125 | * This method will round to 0.001 precision | ||
130 | */ | 126 | */ | ||
131 | static qreal toPoint(qreal ptValue) { | 127 | static inline qreal toPoint(qreal ptValue) | ||
128 | { | ||||
132 | // No conversion, only rounding (to 0.001 precision) | 129 | // No conversion, only rounding (to 0.001 precision) | ||
133 | return floor(ptValue * 1000.0) / 1000.0; | 130 | return floor(ptValue * 1000.0) / 1000.0; | ||
134 | } | 131 | } | ||
While we can't keep the code that uses d-pointer inline I propose to keep all these short static methods inline. And even prepend inline keyword to them. staniek: While we can't keep the code that uses d-pointer inline I propose to keep all these short… | |||||
Does it make sense to have static-inline? me research seems inconclusive :) piggz: Does it make sense to have static-inline? me research seems inconclusive :) | |||||
staniek: http://stackoverflow.com/a/10877104 | |||||
staniek: Remove all `};` from the header | |||||
135 | 132 | | |||
136 | /** | 133 | /** | ||
137 | * Prepare ptValue to be displayed in mm | 134 | * Prepare ptValue to be displayed in mm | ||
138 | * This method will round to 0.0001 precision, use POINT_TO_MM() for lossless conversion. | 135 | * This method will round to 0.0001 precision, use POINT_TO_MM() for lossless conversion. | ||
139 | */ | 136 | */ | ||
140 | static qreal toMillimeter(qreal ptValue) { | 137 | static inline qreal toMillimeter(qreal ptValue) | ||
138 | { | ||||
141 | // "mm" values are rounded to 0.0001 millimeters | 139 | // "mm" values are rounded to 0.0001 millimeters | ||
142 | return floor(POINT_TO_MM(ptValue) * 10000.0) / 10000.0; | 140 | return floor(POINT_TO_MM(ptValue) * 10000.0) / 10000.0; | ||
143 | } | 141 | } | ||
144 | 142 | | |||
145 | /** | 143 | /** | ||
146 | * Prepare ptValue to be displayed in cm | 144 | * Prepare ptValue to be displayed in cm | ||
147 | * This method will round to 0.0001 precision, use POINT_TO_CM() for lossless conversion. | 145 | * This method will round to 0.0001 precision, use POINT_TO_CM() for lossless conversion. | ||
148 | */ | 146 | */ | ||
149 | static qreal toCentimeter(qreal ptValue) { | 147 | static inline qreal toCentimeter(qreal ptValue) | ||
148 | { | ||||
150 | return floor(POINT_TO_CM(ptValue) * 10000.0) / 10000.0; | 149 | return floor(POINT_TO_CM(ptValue) * 10000.0) / 10000.0; | ||
151 | } | 150 | } | ||
152 | 151 | | |||
153 | /** | 152 | /** | ||
154 | * Prepare ptValue to be displayed in dm | 153 | * Prepare ptValue to be displayed in dm | ||
155 | * This method will round to 0.0001 precision, use POINT_TO_DM() for lossless conversion. | 154 | * This method will round to 0.0001 precision, use POINT_TO_DM() for lossless conversion. | ||
156 | */ | 155 | */ | ||
157 | static qreal toDecimeter(qreal ptValue) { | 156 | static inline qreal toDecimeter(qreal ptValue) | ||
157 | { | ||||
158 | return floor(POINT_TO_DM(ptValue) * 10000.0) / 10000.0; | 158 | return floor(POINT_TO_DM(ptValue) * 10000.0) / 10000.0; | ||
159 | } | 159 | } | ||
160 | 160 | | |||
161 | /** | 161 | /** | ||
162 | * Prepare ptValue to be displayed in inch | 162 | * Prepare ptValue to be displayed in inch | ||
163 | * This method will round to 0.00001 precision, use POINT_TO_INCH() for lossless conversion. | 163 | * This method will round to 0.00001 precision, use POINT_TO_INCH() for lossless conversion. | ||
164 | */ | 164 | */ | ||
165 | static qreal toInch(qreal ptValue) { | 165 | static inline qreal toInch(qreal ptValue) | ||
166 | { | ||||
166 | // "in" values are rounded to 0.00001 inches | 167 | // "in" values are rounded to 0.00001 inches | ||
167 | return floor(POINT_TO_INCH(ptValue) * 100000.0) / 100000.0; | 168 | return floor(POINT_TO_INCH(ptValue) * 100000.0) / 100000.0; | ||
168 | } | 169 | } | ||
169 | 170 | | |||
170 | /** | 171 | /** | ||
171 | * Prepare ptValue to be displayed in pica | 172 | * Prepare ptValue to be displayed in pica | ||
172 | * This method will round to 0.00001 precision, use POINT_TO_PI() for lossless conversion. | 173 | * This method will round to 0.00001 precision, use POINT_TO_PI() for lossless conversion. | ||
173 | */ | 174 | */ | ||
174 | static qreal toPica(qreal ptValue) { | 175 | static inline qreal toPica(qreal ptValue) | ||
176 | { | ||||
175 | // "pi" values are rounded to 0.00001 inches | 177 | // "pi" values are rounded to 0.00001 inches | ||
176 | return floor(POINT_TO_PI(ptValue) * 100000.0) / 100000.0; | 178 | return floor(POINT_TO_PI(ptValue) * 100000.0) / 100000.0; | ||
177 | } | 179 | } | ||
178 | 180 | | |||
179 | /** | 181 | /** | ||
180 | * Prepare ptValue to be displayed in cicero | 182 | * Prepare ptValue to be displayed in cicero | ||
181 | * This method will round to 0.00001 precision, use POINT_TO_CC() for lossless conversion. | 183 | * This method will round to 0.00001 precision, use POINT_TO_CC() for lossless conversion. | ||
182 | */ | 184 | */ | ||
183 | static qreal toCicero(qreal ptValue) { | 185 | static inline qreal toCicero(qreal ptValue) | ||
186 | { | ||||
184 | // "cc" values are rounded to 0.00001 inches | 187 | // "cc" values are rounded to 0.00001 inches | ||
185 | return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; | 188 | return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; | ||
186 | } | 189 | } | ||
187 | 190 | | |||
188 | /** | 191 | /** | ||
189 | * convert the given value directly from one unit to another | 192 | * convert the given value directly from one unit to another | ||
190 | */ | 193 | */ | ||
191 | static qreal convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor = 1.0); | 194 | static qreal convertFromUnitToUnit(qreal value, const KReportUnit &fromUnit, const KReportUnit &toUnit, qreal factor = 1.0); | ||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Line(s) | |||||
237 | static qreal parseValue(const QString &value, qreal defaultVal = 0.0); | 240 | static qreal parseValue(const QString &value, qreal defaultVal = 0.0); | ||
238 | 241 | | |||
239 | /// parse an angle to its value in degrees | 242 | /// parse an angle to its value in degrees | ||
240 | static qreal parseAngle(const QString &value, qreal defaultVal = 0.0); | 243 | static qreal parseAngle(const QString &value, qreal defaultVal = 0.0); | ||
241 | 244 | | |||
242 | /** | 245 | /** | ||
243 | * Equal to symbol(): returns the symbol string of the unit. | 246 | * Equal to symbol(): returns the symbol string of the unit. | ||
244 | */ | 247 | */ | ||
245 | inline QString toString() const { | 248 | inline QString toString() const | ||
249 | { | ||||
246 | return symbol(); | 250 | return symbol(); | ||
247 | } | 251 | }; | ||
248 | 252 | | |||
staniek: If it's inline let's keep the code in the header. | |||||
249 | private: | 253 | private: | ||
250 | Type m_type; | 254 | class Private; | ||
251 | qreal m_pixelConversion; | 255 | Private * const d; | ||
252 | }; | 256 | }; | ||
253 | 257 | | |||
254 | #ifndef QT_NO_DEBUG_STREAM | 258 | #ifndef QT_NO_DEBUG_STREAM | ||
255 | KREPORT_EXPORT QDebug operator<<(QDebug, const KReportUnit &); | 259 | KREPORT_EXPORT QDebug operator<<(QDebug, const KReportUnit &); | ||
256 | #endif | 260 | #endif | ||
257 | 261 | | |||
258 | Q_DECLARE_METATYPE(KReportUnit) | 262 | Q_DECLARE_METATYPE(KReportUnit) | ||
259 | Q_DECLARE_OPERATORS_FOR_FLAGS(KReportUnit::ListOptions) | 263 | Q_DECLARE_OPERATORS_FOR_FLAGS(KReportUnit::ListOptions) | ||
260 | 264 | | |||
261 | #endif | 265 | #endif |
Note for later: I plan something for ListOption: T4967