Changeset View
Changeset View
Standalone View
Standalone View
src/common/KReportUnit.cpp
Show All 21 Lines | |||||
22 | #include "KReportUnit.h" | 22 | #include "KReportUnit.h" | ||
23 | #include "kreport_debug.h" | 23 | #include "kreport_debug.h" | ||
24 | 24 | | |||
25 | #include <cmath> | 25 | #include <cmath> | ||
26 | 26 | | |||
27 | #include <QTransform> | 27 | #include <QTransform> | ||
28 | #include <QCoreApplication> | 28 | #include <QCoreApplication> | ||
29 | 29 | | |||
30 | class Q_DECL_HIDDEN KReportUnit::Private | ||||
31 | { | ||||
32 | public: | ||||
33 | Type type; | ||||
34 | qreal pixelConversion; | ||||
35 | }; | ||||
36 | | ||||
30 | // ensure the same order as in KReportUnit::Unit | 37 | // ensure the same order as in KReportUnit::Unit | ||
31 | static const char* const unitNameList[KReportUnit::TypeCount] = | 38 | static const char* const unitNameList[KReportUnit::TypeCount] = | ||
32 | { | 39 | { | ||
33 | "mm", | 40 | "mm", | ||
34 | "pt", | 41 | "pt", | ||
35 | "in", | 42 | "in", | ||
36 | "cm", | 43 | "cm", | ||
37 | "dm", | 44 | "dm", | ||
38 | "pi", | 45 | "pi", | ||
39 | "cc", | 46 | "cc", | ||
40 | "px" | 47 | "px" | ||
41 | }; | 48 | }; | ||
42 | 49 | | |||
50 | KReportUnit::KReportUnit(Type type, qreal factor) : d(new Private) | ||||
51 | { | ||||
52 | d->type = type; | ||||
53 | d->pixelConversion = factor; | ||||
54 | } | ||||
55 | | ||||
56 | KReportUnit::KReportUnit(const KReportUnit& other) : d(new Private) | ||||
57 | { | ||||
58 | d->type = other.type(); | ||||
59 | d->pixelConversion = other.factor(); | ||||
60 | } | ||||
61 | | ||||
62 | KReportUnit::~KReportUnit() | ||||
63 | { | ||||
64 | delete d; | ||||
65 | } | ||||
66 | | ||||
67 | bool KReportUnit::operator==(const KReportUnit& other) const | ||||
68 | { | ||||
69 | return d->type == other.d->type && | ||||
70 | (d->type != Pixel || | ||||
71 | qFuzzyCompare(d->pixelConversion, other.d->pixelConversion)); | ||||
72 | } | ||||
73 | | ||||
74 | bool KReportUnit::operator!=(const KReportUnit& other) const | ||||
75 | { | ||||
76 | return !operator==(other); | ||||
77 | } | ||||
78 | | ||||
79 | | ||||
80 | KReportUnit& KReportUnit::operator=(Type type) | ||||
81 | { | ||||
82 | d->type = type; | ||||
83 | d->pixelConversion = 1.0; | ||||
84 | return *this; | ||||
85 | } | ||||
86 | | ||||
87 | KReportUnit & KReportUnit::operator=(const KReportUnit& other) | ||||
88 | { | ||||
89 | d->type = other.type(); | ||||
90 | d->pixelConversion = other.factor(); | ||||
91 | return *this; | ||||
92 | } | ||||
93 | | ||||
43 | QString KReportUnit::unitDescription(KReportUnit::Type type) | 94 | QString KReportUnit::unitDescription(KReportUnit::Type type) | ||
44 | { | 95 | { | ||
45 | switch (type) { | 96 | switch (type) { | ||
46 | case KReportUnit::Millimeter: | 97 | case KReportUnit::Millimeter: | ||
47 | return QCoreApplication::translate("KReportUnit", "Millimeters (mm)"); | 98 | return QCoreApplication::translate("KReportUnit", "Millimeters (mm)"); | ||
48 | case KReportUnit::Centimeter: | 99 | case KReportUnit::Centimeter: | ||
49 | return QCoreApplication::translate("KReportUnit", "Centimeters (cm)"); | 100 | return QCoreApplication::translate("KReportUnit", "Centimeters (cm)"); | ||
50 | case KReportUnit::Decimeter: | 101 | case KReportUnit::Decimeter: | ||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | 146 | if ((0 <= index) && (index < KReportUnit::TypeCount)) { | |||
106 | } | 157 | } | ||
107 | } | 158 | } | ||
108 | 159 | | |||
109 | return KReportUnit(type, factor); | 160 | return KReportUnit(type, factor); | ||
110 | } | 161 | } | ||
111 | 162 | | |||
112 | int KReportUnit::indexInListForUi(ListOptions listOptions) const | 163 | int KReportUnit::indexInListForUi(ListOptions listOptions) const | ||
113 | { | 164 | { | ||
114 | if ((listOptions&HidePixel) && (m_type == Pixel)) { | 165 | if ((listOptions&HidePixel) && (d->type == Pixel)) { | ||
115 | return -1; | 166 | return -1; | ||
116 | } | 167 | } | ||
117 | 168 | | |||
118 | int result = -1; | 169 | int result = -1; | ||
119 | 170 | | |||
120 | int skipped = 0; | 171 | int skipped = 0; | ||
121 | for (int i = 0; i < KReportUnit::TypeCount; ++i) { | 172 | for (int i = 0; i < KReportUnit::TypeCount; ++i) { | ||
122 | if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) { | 173 | if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) { | ||
123 | ++skipped; | 174 | ++skipped; | ||
124 | continue; | 175 | continue; | ||
125 | } | 176 | } | ||
126 | if (typesInUi[i] == m_type) { | 177 | if (typesInUi[i] == d->type) { | ||
127 | result = i - skipped; | 178 | result = i - skipped; | ||
128 | break; | 179 | break; | ||
129 | } | 180 | } | ||
130 | } | 181 | } | ||
131 | 182 | | |||
132 | return result; | 183 | return result; | ||
133 | } | 184 | } | ||
134 | 185 | | |||
135 | qreal KReportUnit::toUserValue(qreal ptValue) const | 186 | qreal KReportUnit::toUserValue(qreal ptValue) const | ||
136 | { | 187 | { | ||
137 | switch (m_type) { | 188 | switch (d->type) { | ||
138 | case Millimeter: | 189 | case Millimeter: | ||
139 | return toMillimeter(ptValue); | 190 | return toMillimeter(ptValue); | ||
140 | case Centimeter: | 191 | case Centimeter: | ||
141 | return toCentimeter(ptValue); | 192 | return toCentimeter(ptValue); | ||
142 | case Decimeter: | 193 | case Decimeter: | ||
143 | return toDecimeter(ptValue); | 194 | return toDecimeter(ptValue); | ||
144 | case Inch: | 195 | case Inch: | ||
145 | return toInch(ptValue); | 196 | return toInch(ptValue); | ||
146 | case Pica: | 197 | case Pica: | ||
147 | return toPica(ptValue); | 198 | return toPica(ptValue); | ||
148 | case Cicero: | 199 | case Cicero: | ||
149 | return toCicero(ptValue); | 200 | return toCicero(ptValue); | ||
150 | case Pixel: | 201 | case Pixel: | ||
151 | return ptValue * m_pixelConversion; | 202 | return ptValue * d->pixelConversion; | ||
152 | case Point: | 203 | case Point: | ||
153 | default: | 204 | default: | ||
154 | return toPoint(ptValue); | 205 | return toPoint(ptValue); | ||
155 | } | 206 | } | ||
156 | } | 207 | } | ||
157 | 208 | | |||
158 | qreal KReportUnit::ptToUnit(qreal ptValue, const KReportUnit &unit) | 209 | qreal KReportUnit::ptToUnit(qreal ptValue, const KReportUnit &unit) | ||
159 | { | 210 | { | ||
160 | switch (unit.m_type) { | 211 | switch (unit.d->type) { | ||
161 | case Millimeter: | 212 | case Millimeter: | ||
162 | return POINT_TO_MM(ptValue); | 213 | return POINT_TO_MM(ptValue); | ||
163 | case Centimeter: | 214 | case Centimeter: | ||
164 | return POINT_TO_CM(ptValue); | 215 | return POINT_TO_CM(ptValue); | ||
165 | case Decimeter: | 216 | case Decimeter: | ||
166 | return POINT_TO_DM(ptValue); | 217 | return POINT_TO_DM(ptValue); | ||
167 | case Inch: | 218 | case Inch: | ||
168 | return POINT_TO_INCH(ptValue); | 219 | return POINT_TO_INCH(ptValue); | ||
169 | case Pica: | 220 | case Pica: | ||
170 | return POINT_TO_PI(ptValue); | 221 | return POINT_TO_PI(ptValue); | ||
171 | case Cicero: | 222 | case Cicero: | ||
172 | return POINT_TO_CC(ptValue); | 223 | return POINT_TO_CC(ptValue); | ||
173 | case Pixel: | 224 | case Pixel: | ||
174 | return ptValue * unit.m_pixelConversion; | 225 | return ptValue * unit.d->pixelConversion; | ||
175 | case Point: | 226 | case Point: | ||
176 | default: | 227 | default: | ||
177 | return ptValue; | 228 | return ptValue; | ||
178 | } | 229 | } | ||
179 | } | 230 | } | ||
180 | 231 | | |||
181 | QString KReportUnit::toUserStringValue(qreal ptValue) const | 232 | QString KReportUnit::toUserStringValue(qreal ptValue) const | ||
182 | { | 233 | { | ||
183 | return QLocale::system().toString(toUserValue(ptValue)); | 234 | return QLocale::system().toString(toUserValue(ptValue)); | ||
184 | } | 235 | } | ||
185 | 236 | | |||
186 | qreal KReportUnit::fromUserValue(qreal value) const | 237 | qreal KReportUnit::fromUserValue(qreal value) const | ||
187 | { | 238 | { | ||
188 | switch (m_type) { | 239 | switch (d->type) { | ||
189 | case Millimeter: | 240 | case Millimeter: | ||
190 | return MM_TO_POINT(value); | 241 | return MM_TO_POINT(value); | ||
191 | case Centimeter: | 242 | case Centimeter: | ||
192 | return CM_TO_POINT(value); | 243 | return CM_TO_POINT(value); | ||
193 | case Decimeter: | 244 | case Decimeter: | ||
194 | return DM_TO_POINT(value); | 245 | return DM_TO_POINT(value); | ||
195 | case Inch: | 246 | case Inch: | ||
196 | return INCH_TO_POINT(value); | 247 | return INCH_TO_POINT(value); | ||
197 | case Pica: | 248 | case Pica: | ||
198 | return PI_TO_POINT(value); | 249 | return PI_TO_POINT(value); | ||
199 | case Cicero: | 250 | case Cicero: | ||
200 | return CC_TO_POINT(value); | 251 | return CC_TO_POINT(value); | ||
201 | case Pixel: | 252 | case Pixel: | ||
202 | return value / m_pixelConversion; | 253 | return value / d->pixelConversion; | ||
203 | case Point: | 254 | case Point: | ||
204 | default: | 255 | default: | ||
205 | return value; | 256 | return value; | ||
206 | } | 257 | } | ||
207 | } | 258 | } | ||
208 | 259 | | |||
209 | qreal KReportUnit::fromUserValue(const QString &value, bool *ok) const | 260 | qreal KReportUnit::fromUserValue(const QString &value, bool *ok) const | ||
210 | { | 261 | { | ||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Line(s) | 335 | { | |||
329 | default: | 380 | default: | ||
330 | return pt; | 381 | return pt; | ||
331 | } | 382 | } | ||
332 | 383 | | |||
333 | } | 384 | } | ||
334 | 385 | | |||
335 | QString KReportUnit::symbol() const | 386 | QString KReportUnit::symbol() const | ||
336 | { | 387 | { | ||
337 | return QLatin1String(unitNameList[m_type]); | 388 | return QLatin1String(unitNameList[d->type]); | ||
338 | } | 389 | } | ||
339 | 390 | | |||
340 | qreal KReportUnit::parseAngle(const QString& _value, qreal defaultVal) | 391 | qreal KReportUnit::parseAngle(const QString& _value, qreal defaultVal) | ||
341 | { | 392 | { | ||
342 | if (_value.isEmpty()) | 393 | if (_value.isEmpty()) | ||
343 | return defaultVal; | 394 | return defaultVal; | ||
344 | 395 | | |||
345 | QString value(_value.simplified()); | 396 | QString value(_value.simplified()); | ||
Show All 32 Lines | |||||
378 | #ifndef NDEBUG | 429 | #ifndef NDEBUG | ||
379 | debug.nospace() << unit.symbol(); | 430 | debug.nospace() << unit.symbol(); | ||
380 | #else | 431 | #else | ||
381 | Q_UNUSED(unit); | 432 | Q_UNUSED(unit); | ||
382 | #endif | 433 | #endif | ||
383 | return debug.space(); | 434 | return debug.space(); | ||
384 | 435 | | |||
385 | } | 436 | } | ||
437 | | ||||
438 | void KReportUnit::setFactor(qreal factor) | ||||
439 | { | ||||
440 | d->pixelConversion = factor; | ||||
441 | } | ||||
442 | | ||||
443 | qreal KReportUnit::factor() const | ||||
444 | { | ||||
445 | return d->pixelConversion; | ||||
446 | } | ||||
447 | | ||||
448 | KReportUnit::Type KReportUnit::type() const | ||||
449 | { | ||||
450 | return d->type; | ||||
451 | } | ||||
452 | | ||||
386 | #endif | 453 | #endif | ||
staniek: Here and in many other places: `{ -> \n{` | |||||
staniek: Can we have the ctor and dtor on the top at line 50 or so? |
Here and in many other places: { -> \n{