KoUnit.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3 Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21//#include <KoGlobal.h>
22#include "KoUnit.h"
23#include <KoXmlWriter.h>
24
25#include <tdelocale.h>
26#include <tdeglobal.h>
27#include <kdebug.h>
28
29#include <tqregexp.h>
30#include <tqdom.h>
31
32TQStringList KoUnit::listOfUnitName()
33{
34 TQStringList lst;
35 for ( uint i = 0 ; i <= KoUnit::U_LASTUNIT ; ++i )
36 {
37 KoUnit::Unit unit = static_cast<KoUnit::Unit>( i );
38 lst.append( KoUnit::unitDescription( unit ) );
39 }
40 return lst;
41}
42
44{
45 switch ( _unit )
46 {
47 case KoUnit::U_MM:
48 return i18n("Millimeters (mm)");
49 case KoUnit::U_CM:
50 return i18n("Centimeters (cm)");
51 case KoUnit::U_DM:
52 return i18n("Decimeters (dm)");
53 case KoUnit::U_INCH:
54 return i18n("Inches (in)");
55 case KoUnit::U_PI:
56 return i18n("Pica (pi)");
57 case KoUnit::U_DD:
58 return i18n("Didot (dd)");
59 case KoUnit::U_CC:
60 return i18n("Cicero (cc)");
61 case KoUnit::U_PT:
62 return i18n("Points (pt)" );
63 default:
64 return i18n("Error!");
65 }
66}
67
68double KoUnit::toUserValue( double ptValue, Unit unit )
69{
70 switch ( unit ) {
71 case U_MM:
72 return toMM( ptValue );
73 case U_CM:
74 return toCM( ptValue );
75 case U_DM:
76 return toDM( ptValue );
77 case U_INCH:
78 return toInch( ptValue );
79 case U_PI:
80 return toPI( ptValue );
81 case U_DD:
82 return toDD( ptValue );
83 case U_CC:
84 return toCC( ptValue );
85 case U_PT:
86 default:
87 return toPoint( ptValue );
88 }
89}
90
91double KoUnit::ptToUnit( const double ptValue, const Unit unit )
92{
93 switch ( unit )
94 {
95 case U_MM:
96 return POINT_TO_MM( ptValue );
97 case U_CM:
98 return POINT_TO_CM( ptValue );
99 case U_DM:
100 return POINT_TO_DM( ptValue );
101 case U_INCH:
102 return POINT_TO_INCH( ptValue );
103 case U_PI:
104 return POINT_TO_PI( ptValue );
105 case U_DD:
106 return POINT_TO_DD( ptValue );
107 case U_CC:
108 return POINT_TO_CC( ptValue );
109 case U_PT:
110 default:
111 return ptValue;
112 }
113}
114
115TQString KoUnit::toUserStringValue( double ptValue, Unit unit )
116{
117 return TDEGlobal::locale()->formatNumber( toUserValue( ptValue, unit ) );
118}
119
120double KoUnit::fromUserValue( double value, Unit unit )
121{
122 switch ( unit ) {
123 case U_MM:
124 return MM_TO_POINT( value );
125 case U_CM:
126 return CM_TO_POINT( value );
127 case U_DM:
128 return DM_TO_POINT( value );
129 case U_INCH:
130 return INCH_TO_POINT( value );
131 case U_PI:
132 return PI_TO_POINT( value );
133 case U_DD:
134 return DD_TO_POINT( value );
135 case U_CC:
136 return CC_TO_POINT( value );
137 case U_PT:
138 default:
139 return value;
140 }
141}
142
143double KoUnit::fromUserValue( const TQString& value, Unit unit, bool* ok )
144{
145 return fromUserValue( TDEGlobal::locale()->readNumber( value, ok ), unit );
146}
147
148double KoUnit::parseValue( TQString value, double defaultVal )
149{
150 value.simplifyWhiteSpace();
151 value.remove( ' ' );
152
153 if( value.isEmpty() )
154 return defaultVal;
155
156 int index = value.find( TQRegExp( "[a-z]+$" ) );
157 if ( index == -1 )
158 return value.toDouble();
159
160 TQString unit = value.mid( index );
161 value.truncate ( index );
162 double val = value.toDouble();
163
164 if ( unit == "pt" )
165 return val;
166
167 bool ok;
168 Unit u = KoUnit::unit( unit, &ok );
169 if( ok )
170 return fromUserValue( val, u );
171
172 if( unit == "m" )
173 return fromUserValue( val * 10.0, U_DM );
174 else if( unit == "km" )
175 return fromUserValue( val * 10000.0, U_DM );
176 kdWarning() << "KoUnit::parseValue: Unit " << unit << " is not supported, please report." << endl;
177
178 // TODO : add support for mi/ft ?
179 return defaultVal;
180}
181
182KoUnit::Unit KoUnit::unit( const TQString &_unitName, bool* ok )
183{
184 if ( ok )
185 *ok = true;
186 if ( _unitName == TQString::fromLatin1( "mm" ) ) return U_MM;
187 if ( _unitName == TQString::fromLatin1( "cm" ) ) return U_CM;
188 if ( _unitName == TQString::fromLatin1( "dm" ) ) return U_DM;
189 if ( _unitName == TQString::fromLatin1( "in" )
190 || _unitName == TQString::fromLatin1("inch") /*compat*/ ) return U_INCH;
191 if ( _unitName == TQString::fromLatin1( "pi" ) ) return U_PI;
192 if ( _unitName == TQString::fromLatin1( "dd" ) ) return U_DD;
193 if ( _unitName == TQString::fromLatin1( "cc" ) ) return U_CC;
194 if ( _unitName == TQString::fromLatin1( "pt" ) ) return U_PT;
195 if ( ok )
196 *ok = false;
197 return U_PT;
198}
199
200TQString KoUnit::unitName( Unit _unit )
201{
202 if ( _unit == U_MM ) return TQString::fromLatin1( "mm" );
203 if ( _unit == U_CM ) return TQString::fromLatin1( "cm" );
204 if ( _unit == U_DM ) return TQString::fromLatin1( "dm" );
205 if ( _unit == U_INCH ) return TQString::fromLatin1( "in" );
206 if ( _unit == U_PI ) return TQString::fromLatin1( "pi" );
207 if ( _unit == U_DD ) return TQString::fromLatin1( "dd" );
208 if ( _unit == U_CC ) return TQString::fromLatin1( "cc" );
209 return TQString::fromLatin1( "pt" );
210}
211
212void KoUnit::saveOasis(KoXmlWriter* settingsWriter, Unit _unit)
213{
214 settingsWriter->addConfigItem( "unit", unitName(_unit) );
215}
static double toInch(double ptValue)
Prepare ptValue to be displayed in inch.
Definition KoUnit.h:99
static double toUserValue(double ptValue, Unit unit)
This method is the one to use to display a value in a dialog.
Definition KoUnit.cpp:68
static double toDM(double ptValue)
Prepare ptValue to be displayed in dm.
Definition KoUnit.h:94
static double toCM(double ptValue)
Prepare ptValue to be displayed in cm.
Definition KoUnit.h:89
static double toMM(double ptValue)
Prepare ptValue to be displayed in mm.
Definition KoUnit.h:83
static double fromUserValue(double value, Unit unit)
This method is the one to use to read a value from a dialog.
Definition KoUnit.cpp:120
static double toPI(double ptValue)
Prepare ptValue to be displayed in pica.
Definition KoUnit.h:105
static TQString unitDescription(Unit _unit)
Get the full (translated) description of a unit.
Definition KoUnit.cpp:43
static double toCC(double ptValue)
Prepare ptValue to be displayed in cicero.
Definition KoUnit.h:117
static TQString toUserStringValue(double ptValue, Unit unit)
This method is the one to use to display a value in a dialog.
Definition KoUnit.cpp:115
static double toDD(double ptValue)
Prepare ptValue to be displayed in didot.
Definition KoUnit.h:111
static double ptToUnit(const double ptValue, const Unit unit)
Convert the value ptValue to a given unit unit Unlike KoUnit::ptToUnit the return value remains unrou...
Definition KoUnit.cpp:91
static double parseValue(TQString value, double defaultVal=0.0)
parse common KOffice and OO values, like "10cm", "5mm" to pt
Definition KoUnit.cpp:148
Unit
Length units supported by KOffice.
Definition KoUnit.h:63
static void saveOasis(KoXmlWriter *settingsWriter, Unit _unit)
Save a unit in OASIS format.
Definition KoUnit.cpp:212
static double toPoint(double ptValue)
Prepare ptValue to be displayed in pt.
Definition KoUnit.h:77
static Unit unit(const TQString &_unitName, bool *ok=0)
Convert a unit name into a Unit enum.
Definition KoUnit.cpp:182
static TQString unitName(Unit _unit)
Get the name of a unit.
Definition KoUnit.cpp:200
A class for writing out XML (to any TQIODevice), with a special attention on performance.
Definition KoXmlWriter.h:37
void addConfigItem(const TQString &configName, const TQString &value)
Special helper for writing config item into settings.xml.