Changeset View
Changeset View
Standalone View
Standalone View
src/part/KgvUnit.cpp
Show All 20 Lines | 19 | /* This file was part of the KDE project | |||
---|---|---|---|---|---|
21 | 21 | | |||
22 | This program is free software; you can redistribute it and/or | 22 | This program is free software; you can redistribute it and/or | ||
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 | //#include <KoGlobal.h> | 28 | //#include <KoGlobal.h> | ||
29 | #include "kgraphviewerlib_debug.h" | | |||
30 | #include "KgvUnit.h" | 29 | #include "KgvUnit.h" | ||
30 | #include "kgraphviewerlib_debug.h" | ||||
31 | 31 | | |||
32 | #include <QDebug> | ||||
32 | #include <QLocale> | 33 | #include <QLocale> | ||
33 | #include <klocalizedstring.h> | 34 | #include <klocalizedstring.h> | ||
34 | #include <QDebug> | | |||
35 | #include <qregexp.h> | 35 | #include <qregexp.h> | ||
36 | 36 | | |||
37 | QStringList KgvUnit::listOfUnitName() | 37 | QStringList KgvUnit::listOfUnitName() | ||
38 | { | 38 | { | ||
39 | QStringList lst; | 39 | QStringList lst; | ||
40 | for ( uint i = 0 ; i <= KgvUnit::U_LASTUNIT ; ++i ) | 40 | for (uint i = 0; i <= KgvUnit::U_LASTUNIT; ++i) { | ||
41 | { | | |||
42 | KgvUnit::Unit unit = static_cast<KgvUnit::Unit>( i ); | 41 | KgvUnit::Unit unit = static_cast<KgvUnit::Unit>(i); | ||
43 | lst.append( KgvUnit::unitDescription( unit ) ); | 42 | lst.append(KgvUnit::unitDescription(unit)); | ||
44 | } | 43 | } | ||
45 | return lst; | 44 | return lst; | ||
46 | } | 45 | } | ||
47 | 46 | | |||
48 | QString KgvUnit::unitDescription( Unit _unit ) | 47 | QString KgvUnit::unitDescription(Unit _unit) | ||
49 | { | 48 | { | ||
50 | switch ( _unit ) | 49 | switch (_unit) { | ||
51 | { | | |||
52 | case KgvUnit::U_MM: | 50 | case KgvUnit::U_MM: | ||
53 | return i18n("Millimeters (mm)"); | 51 | return i18n("Millimeters (mm)"); | ||
54 | case KgvUnit::U_CM: | 52 | case KgvUnit::U_CM: | ||
55 | return i18n("Centimeters (cm)"); | 53 | return i18n("Centimeters (cm)"); | ||
56 | case KgvUnit::U_DM: | 54 | case KgvUnit::U_DM: | ||
57 | return i18n("Decimeters (dm)"); | 55 | return i18n("Decimeters (dm)"); | ||
58 | case KgvUnit::U_INCH: | 56 | case KgvUnit::U_INCH: | ||
59 | return i18n("Inches (in)"); | 57 | return i18n("Inches (in)"); | ||
60 | case KgvUnit::U_PI: | 58 | case KgvUnit::U_PI: | ||
61 | return i18n("Pica (pi)"); | 59 | return i18n("Pica (pi)"); | ||
62 | case KgvUnit::U_DD: | 60 | case KgvUnit::U_DD: | ||
63 | return i18n("Didot (dd)"); | 61 | return i18n("Didot (dd)"); | ||
64 | case KgvUnit::U_CC: | 62 | case KgvUnit::U_CC: | ||
65 | return i18n("Cicero (cc)"); | 63 | return i18n("Cicero (cc)"); | ||
66 | case KgvUnit::U_PT: | 64 | case KgvUnit::U_PT: | ||
67 | return i18n("Points (pt)" ); | 65 | return i18n("Points (pt)"); | ||
68 | default: | 66 | default: | ||
69 | return i18n("Error."); | 67 | return i18n("Error."); | ||
70 | } | 68 | } | ||
71 | } | 69 | } | ||
72 | 70 | | |||
73 | double KgvUnit::toUserValue( double ptValue, Unit unit ) | 71 | double KgvUnit::toUserValue(double ptValue, Unit unit) | ||
74 | { | 72 | { | ||
75 | switch ( unit ) { | 73 | switch (unit) { | ||
76 | case U_MM: | 74 | case U_MM: | ||
77 | return toMM( ptValue ); | 75 | return toMM(ptValue); | ||
78 | case U_CM: | 76 | case U_CM: | ||
79 | return toCM( ptValue ); | 77 | return toCM(ptValue); | ||
80 | case U_DM: | 78 | case U_DM: | ||
81 | return toDM( ptValue ); | 79 | return toDM(ptValue); | ||
82 | case U_INCH: | 80 | case U_INCH: | ||
83 | return toInch( ptValue ); | 81 | return toInch(ptValue); | ||
84 | case U_PI: | 82 | case U_PI: | ||
85 | return toPI( ptValue ); | 83 | return toPI(ptValue); | ||
86 | case U_DD: | 84 | case U_DD: | ||
87 | return toDD( ptValue ); | 85 | return toDD(ptValue); | ||
88 | case U_CC: | 86 | case U_CC: | ||
89 | return toCC( ptValue ); | 87 | return toCC(ptValue); | ||
90 | case U_PT: | 88 | case U_PT: | ||
91 | default: | 89 | default: | ||
92 | return toPoint( ptValue ); | 90 | return toPoint(ptValue); | ||
93 | } | 91 | } | ||
94 | } | 92 | } | ||
95 | 93 | | |||
96 | double KgvUnit::ptToUnit( const double ptValue, const Unit unit ) | 94 | double KgvUnit::ptToUnit(const double ptValue, const Unit unit) | ||
97 | { | 95 | { | ||
98 | switch ( unit ) | 96 | switch (unit) { | ||
99 | { | | |||
100 | case U_MM: | 97 | case U_MM: | ||
101 | return POINT_TO_MM( ptValue ); | 98 | return POINT_TO_MM(ptValue); | ||
102 | case U_CM: | 99 | case U_CM: | ||
103 | return POINT_TO_CM( ptValue ); | 100 | return POINT_TO_CM(ptValue); | ||
104 | case U_DM: | 101 | case U_DM: | ||
105 | return POINT_TO_DM( ptValue ); | 102 | return POINT_TO_DM(ptValue); | ||
106 | case U_INCH: | 103 | case U_INCH: | ||
107 | return POINT_TO_INCH( ptValue ); | 104 | return POINT_TO_INCH(ptValue); | ||
108 | case U_PI: | 105 | case U_PI: | ||
109 | return POINT_TO_PI( ptValue ); | 106 | return POINT_TO_PI(ptValue); | ||
110 | case U_DD: | 107 | case U_DD: | ||
111 | return POINT_TO_DD( ptValue ); | 108 | return POINT_TO_DD(ptValue); | ||
112 | case U_CC: | 109 | case U_CC: | ||
113 | return POINT_TO_CC( ptValue ); | 110 | return POINT_TO_CC(ptValue); | ||
114 | case U_PT: | 111 | case U_PT: | ||
115 | default: | 112 | default: | ||
116 | return ptValue; | 113 | return ptValue; | ||
117 | } | 114 | } | ||
118 | } | 115 | } | ||
119 | 116 | | |||
120 | QString KgvUnit::toUserStringValue( double ptValue, Unit unit ) | 117 | QString KgvUnit::toUserStringValue(double ptValue, Unit unit) | ||
121 | { | 118 | { | ||
122 | return QLocale().toString(toUserValue( ptValue, unit ) ); | 119 | return QLocale().toString(toUserValue(ptValue, unit)); | ||
123 | } | 120 | } | ||
124 | 121 | | |||
125 | double KgvUnit::fromUserValue( double value, Unit unit ) | 122 | double KgvUnit::fromUserValue(double value, Unit unit) | ||
126 | { | 123 | { | ||
127 | switch ( unit ) { | 124 | switch (unit) { | ||
128 | case U_MM: | 125 | case U_MM: | ||
129 | return MM_TO_POINT( value ); | 126 | return MM_TO_POINT(value); | ||
130 | case U_CM: | 127 | case U_CM: | ||
131 | return CM_TO_POINT( value ); | 128 | return CM_TO_POINT(value); | ||
132 | case U_DM: | 129 | case U_DM: | ||
133 | return DM_TO_POINT( value ); | 130 | return DM_TO_POINT(value); | ||
134 | case U_INCH: | 131 | case U_INCH: | ||
135 | return INCH_TO_POINT( value ); | 132 | return INCH_TO_POINT(value); | ||
136 | case U_PI: | 133 | case U_PI: | ||
137 | return PI_TO_POINT( value ); | 134 | return PI_TO_POINT(value); | ||
138 | case U_DD: | 135 | case U_DD: | ||
139 | return DD_TO_POINT( value ); | 136 | return DD_TO_POINT(value); | ||
140 | case U_CC: | 137 | case U_CC: | ||
141 | return CC_TO_POINT( value ); | 138 | return CC_TO_POINT(value); | ||
142 | case U_PT: | 139 | case U_PT: | ||
143 | default: | 140 | default: | ||
144 | return value; | 141 | return value; | ||
145 | } | 142 | } | ||
146 | } | 143 | } | ||
147 | 144 | | |||
148 | double KgvUnit::fromUserValue( const QString& value, Unit unit, bool* ok ) | 145 | double KgvUnit::fromUserValue(const QString &value, Unit unit, bool *ok) | ||
149 | { | 146 | { | ||
150 | return fromUserValue( QLocale().toDouble(value, ok ), unit ); | 147 | return fromUserValue(QLocale().toDouble(value, ok), unit); | ||
151 | } | 148 | } | ||
152 | 149 | | |||
153 | double KgvUnit::parseValue( const QString& sval, double defaultVal ) | 150 | double KgvUnit::parseValue(const QString &sval, double defaultVal) | ||
154 | { | 151 | { | ||
155 | QString value = sval; | 152 | QString value = sval; | ||
156 | value = value.simplified(); | 153 | value = value.simplified(); | ||
157 | value.remove( ' ' ); | 154 | value.remove(' '); | ||
158 | 155 | | |||
159 | if( value.isEmpty() ) | 156 | if (value.isEmpty()) | ||
160 | return defaultVal; | 157 | return defaultVal; | ||
161 | 158 | | |||
162 | int index = value.indexOf(QRegExp("[a-z]+$")); | 159 | int index = value.indexOf(QRegExp("[a-z]+$")); | ||
163 | if ( index == -1 ) | 160 | if (index == -1) | ||
164 | return value.toDouble(); | 161 | return value.toDouble(); | ||
165 | 162 | | |||
166 | QString unit = value.mid( index ); | 163 | QString unit = value.mid(index); | ||
167 | value.truncate ( index ); | 164 | value.truncate(index); | ||
168 | double val = value.toDouble(); | 165 | double val = value.toDouble(); | ||
169 | 166 | | |||
170 | if ( unit == "pt" ) | 167 | if (unit == "pt") | ||
171 | return val; | 168 | return val; | ||
172 | 169 | | |||
173 | bool ok; | 170 | bool ok; | ||
174 | Unit u = KgvUnit::unit( unit, &ok ); | 171 | Unit u = KgvUnit::unit(unit, &ok); | ||
175 | if( ok ) | 172 | if (ok) | ||
176 | return fromUserValue( val, u ); | 173 | return fromUserValue(val, u); | ||
177 | 174 | | |||
178 | if( unit == "m" ) | 175 | if (unit == "m") | ||
179 | return fromUserValue( val * 10.0, U_DM ); | 176 | return fromUserValue(val * 10.0, U_DM); | ||
180 | else if( unit == "km" ) | 177 | else if (unit == "km") | ||
181 | return fromUserValue( val * 10000.0, U_DM ); | 178 | return fromUserValue(val * 10000.0, U_DM); | ||
182 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Unit" << unit << "is not supported, please report."; | 179 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Unit" << unit << "is not supported, please report."; | ||
183 | 180 | | |||
184 | // TODO : add support for mi/ft ? | 181 | // TODO : add support for mi/ft ? | ||
185 | return defaultVal; | 182 | return defaultVal; | ||
186 | } | 183 | } | ||
187 | 184 | | |||
188 | KgvUnit::Unit KgvUnit::unit( const QString &_unitName, bool* ok ) | 185 | KgvUnit::Unit KgvUnit::unit(const QString &_unitName, bool *ok) | ||
189 | { | 186 | { | ||
190 | if ( ok ) | 187 | if (ok) | ||
191 | *ok = true; | 188 | *ok = true; | ||
192 | if ( _unitName == QString::fromLatin1( "mm" ) ) return U_MM; | 189 | if (_unitName == QString::fromLatin1("mm")) | ||
193 | if ( _unitName == QString::fromLatin1( "cm" ) ) return U_CM; | 190 | return U_MM; | ||
194 | if ( _unitName == QString::fromLatin1( "dm" ) ) return U_DM; | 191 | if (_unitName == QString::fromLatin1("cm")) | ||
195 | if ( _unitName == QString::fromLatin1( "in" ) | 192 | return U_CM; | ||
196 | || _unitName == QString::fromLatin1("inch") /*compat*/ ) return U_INCH; | 193 | if (_unitName == QString::fromLatin1("dm")) | ||
197 | if ( _unitName == QString::fromLatin1( "pi" ) ) return U_PI; | 194 | return U_DM; | ||
198 | if ( _unitName == QString::fromLatin1( "dd" ) ) return U_DD; | 195 | if (_unitName == QString::fromLatin1("in") || _unitName == QString::fromLatin1("inch") /*compat*/) | ||
199 | if ( _unitName == QString::fromLatin1( "cc" ) ) return U_CC; | 196 | return U_INCH; | ||
200 | if ( _unitName == QString::fromLatin1( "pt" ) ) return U_PT; | 197 | if (_unitName == QString::fromLatin1("pi")) | ||
198 | return U_PI; | ||||
199 | if (_unitName == QString::fromLatin1("dd")) | ||||
200 | return U_DD; | ||||
201 | if (_unitName == QString::fromLatin1("cc")) | ||||
202 | return U_CC; | ||||
203 | if (_unitName == QString::fromLatin1("pt")) | ||||
204 | return U_PT; | ||||
201 | if ( ok ) | 205 | if (ok) | ||
202 | *ok = false; | 206 | *ok = false; | ||
203 | return U_PT; | 207 | return U_PT; | ||
204 | } | 208 | } | ||
205 | 209 | | |||
206 | QString KgvUnit::unitName( Unit _unit ) | 210 | QString KgvUnit::unitName(Unit _unit) | ||
207 | { | 211 | { | ||
208 | if ( _unit == U_MM ) return QString::fromLatin1( "mm" ); | 212 | if (_unit == U_MM) | ||
209 | if ( _unit == U_CM ) return QString::fromLatin1( "cm" ); | 213 | return QString::fromLatin1("mm"); | ||
210 | if ( _unit == U_DM ) return QString::fromLatin1( "dm" ); | 214 | if (_unit == U_CM) | ||
211 | if ( _unit == U_INCH ) return QString::fromLatin1( "in" ); | 215 | return QString::fromLatin1("cm"); | ||
212 | if ( _unit == U_PI ) return QString::fromLatin1( "pi" ); | 216 | if (_unit == U_DM) | ||
213 | if ( _unit == U_DD ) return QString::fromLatin1( "dd" ); | 217 | return QString::fromLatin1("dm"); | ||
214 | if ( _unit == U_CC ) return QString::fromLatin1( "cc" ); | 218 | if (_unit == U_INCH) | ||
219 | return QString::fromLatin1("in"); | ||||
220 | if (_unit == U_PI) | ||||
221 | return QString::fromLatin1("pi"); | ||||
222 | if (_unit == U_DD) | ||||
223 | return QString::fromLatin1("dd"); | ||||
224 | if (_unit == U_CC) | ||||
225 | return QString::fromLatin1("cc"); | ||||
215 | return QString::fromLatin1( "pt" ); | 226 | return QString::fromLatin1("pt"); | ||
216 | } | 227 | } | ||
217 | |