KoUnit.h
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>, Torben Weis <weis@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#ifndef kounit_h
22#define kounit_h
23#include <tqstring.h>
24#include <tqstringlist.h>
25#include <math.h> // for floor
26#include <koffice_export.h>
27
28class KoXmlWriter;
29class TQDomElement;
30
31// 1 inch ^= 72 pt
32// 1 inch ^= 25.399956 mm (-pedantic ;p)
33// 1 pt = 1/12 pi
34// 1 pt ^= 0.0077880997 cc
35// 1 cc = 12 dd
36// Note: I don't use division but multiplication with the inverse value
37// because it's faster ;p (Werner)
38#define POINT_TO_MM(px) ((px)*0.352777167)
39#define MM_TO_POINT(mm) ((mm)*2.83465058)
40#define POINT_TO_CM(px) ((px)*0.0352777167)
41#define CM_TO_POINT(cm) ((cm)*28.3465058)
42#define POINT_TO_DM(px) ((px)*0.00352777167)
43#define DM_TO_POINT(dm) ((dm)*283.465058)
44#define POINT_TO_INCH(px) ((px)*0.01388888888889)
45#define INCH_TO_POINT(inch) ((inch)*72.0)
46#define MM_TO_INCH(mm) ((mm)*0.039370147)
47#define INCH_TO_MM(inch) ((inch)*25.399956)
48#define POINT_TO_PI(px)((px)*0.083333333)
49#define POINT_TO_DD(px)((px)*0.006490083)
50#define POINT_TO_CC(px)((px)*0.077880997)
51#define PI_TO_POINT(pi)((pi)*12)
52#define DD_TO_POINT(dd)((dd)*154.08124)
53#define CC_TO_POINT(cc)((cc)*12.840103)
59class KOFFICECORE_EXPORT KoUnit
60{
61public:
63 enum Unit {
64 U_MM = 0,
65 U_PT = 1,
66 U_INCH = 2,
67 U_CM = 3,
68 U_DM = 4,
69 U_PI = 5, // pica
70 U_DD = 6, // didot
71 U_CC = 7, // cicero
72 U_LASTUNIT = U_CC // update when adding a new unit
73 // when adding a new unit, make sure to implement support for it in koRuler too
74 };
75
77 static double toPoint( double ptValue ) {
78 // No conversion, only rounding (to 0.001 precision)
79 return floor( ptValue * 1000.0 ) / 1000.0;
80 }
81
83 static double toMM( double ptValue ) {
84 // "mm" values are rounded to 0.0001 millimeters
85 return floor( POINT_TO_MM( ptValue ) * 10000.0 ) / 10000.0;
86 }
87
89 static double toCM( double ptValue ) {
90 return floor( POINT_TO_CM( ptValue ) * 10000.0 ) / 10000.0;
91 }
92
94 static double toDM( double ptValue ) {
95 return floor( POINT_TO_DM( ptValue ) * 10000.0 ) / 10000.0;
96 }
97
99 static double toInch( double ptValue ) {
100 // "in" values are rounded to 0.00001 inches
101 return floor( POINT_TO_INCH( ptValue ) * 100000.0 ) / 100000.0;
102 }
103
105 static double toPI( double ptValue ) {
106 // "pi" values are rounded to 0.00001 inches
107 return floor( POINT_TO_PI( ptValue ) * 100000.0 ) / 100000.0;
108 }
109
111 static double toDD( double ptValue ) {
112 // "dd" values are rounded to 0.00001 inches
113 return floor( POINT_TO_DD( ptValue ) * 100000.0 ) / 100000.0;
114 }
115
117 static double toCC( double ptValue ) {
118 // "cc" values are rounded to 0.00001 inches
119 return floor( POINT_TO_CC( ptValue ) * 100000.0 ) / 100000.0;
120 }
121
127 static double toUserValue( double ptValue, Unit unit );
128
135 static double ptToUnit( const double ptValue, const Unit unit );
136
140 static TQString toUserStringValue( double ptValue, Unit unit );
141
145 static double fromUserValue( double value, Unit unit );
146
153 static double fromUserValue( const TQString& value, Unit unit, bool* ok = 0 );
154
158 static Unit unit( const TQString &_unitName, bool* ok = 0 );
160 static TQString unitName( Unit _unit );
162 static TQString unitDescription( Unit _unit );
163 static TQStringList listOfUnitName();
164
166 static double parseValue( TQString value, double defaultVal = 0.0 );
167 // Note: the above method doesn't take a const ref, since it modifies the arg.
168
170 static void saveOasis(KoXmlWriter* settingsWriter, Unit _unit);
171
172};
173
174
175#endif
KOffice stores everything in pt (using "double") internally.
Definition KoUnit.h:60
static double toInch(double ptValue)
Prepare ptValue to be displayed in inch.
Definition KoUnit.h:99
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 toPI(double ptValue)
Prepare ptValue to be displayed in pica.
Definition KoUnit.h:105
static double toCC(double ptValue)
Prepare ptValue to be displayed in cicero.
Definition KoUnit.h:117
static double toDD(double ptValue)
Prepare ptValue to be displayed in didot.
Definition KoUnit.h:111
Unit
Length units supported by KOffice.
Definition KoUnit.h:63
static double toPoint(double ptValue)
Prepare ptValue to be displayed in pt.
Definition KoUnit.h:77
A class for writing out XML (to any TQIODevice), with a special attention on performance.
Definition KoXmlWriter.h:37