lib Library API Documentation

koUnit.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>, Torben Weis <weis@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #ifndef kounit_h
00021 #define kounit_h
00022 #include <qstring.h>
00023 #include <qstringlist.h>
00024 
00025 // 1 inch ^= 72 pt
00026 // 1 inch ^= 25.399956 mm (-pedantic ;p)
00027 // 1 pt = 1/12 pi
00028 // 1 pt ^= 0.0077880997 cc
00029 // 1 cc = 12 dd
00030 // Note: I don't use division but multiplication with the inverse value
00031 // because it's faster ;p (Werner)
00032 #define POINT_TO_MM(px) ((px)*0.352777167)
00033 #define MM_TO_POINT(mm) ((mm)*2.83465058)
00034 #define POINT_TO_CM(px) ((px)*0.0352777167)
00035 #define CM_TO_POINT(cm) ((cm)*28.3465058)
00036 #define POINT_TO_DM(px) ((px)*0.00352777167)
00037 #define DM_TO_POINT(dm) ((dm)*283.465058)
00038 #define POINT_TO_INCH(px) ((px)*0.01388888888889)
00039 #define INCH_TO_POINT(inch) ((inch)*72.0)
00040 #define MM_TO_INCH(mm) ((mm)*0.039370147)
00041 #define INCH_TO_MM(inch) ((inch)*25.399956)
00042 #define POINT_TO_PI(px)((px)*0.083333333)
00043 #define POINT_TO_DD(px)((px)*0.006490083)
00044 #define POINT_TO_CC(px)((px)*0.077880997)
00045 #define PI_TO_POINT(pi)((pi)*12)
00046 #define DD_TO_POINT(dd)((dd)*154.08124)
00047 #define CC_TO_POINT(cc)((cc)*12.840103)
00048 
00053 class KoUnit
00054 {
00055 public:
00056     enum Unit {
00057         U_MM = 0,
00058         U_PT = 1,
00059         U_INCH = 2,
00060         U_CM = 3,
00061         U_DM = 4,
00062         U_PI = 5, // pica
00063         U_DD = 6, // didot
00064         U_CC = 7, // cicero
00065         U_LASTUNIT = U_CC // update when adding a new unit
00066         // when adding a new unit, make sure to implement support for it in koRuler too
00067     };
00068 
00070     static double toPoint( double ptValue ) {
00071         // No conversion, only rounding (to 0.001 precision)
00072         return qRound( ptValue * 1000.0 ) / 1000.0;
00073     }
00074 
00076     static double toMM( double ptValue ) {
00077         // "mm" values are rounded to 0.0001 millimeters
00078         return qRound( POINT_TO_MM( ptValue ) * 10000.0 ) / 10000.0;
00079     }
00080 
00082     static double toCM( double ptValue ) {
00083         return qRound( POINT_TO_CM( ptValue ) * 10000.0 ) / 10000.0;
00084     }
00085 
00087     static double toDM( double ptValue ) {
00088         return qRound( POINT_TO_DM( ptValue ) * 10000.0 ) / 10000.0;
00089     }
00090 
00092     static double toInch( double ptValue ) {
00093         // "in" values are rounded to 0.00001 inches
00094         return qRound( POINT_TO_INCH( ptValue ) * 100000.0 ) / 100000.0;
00095     }
00096 
00098     static double toPI( double ptValue ) {
00099         // "pi" values are rounded to 0.00001 inches
00100         return qRound( POINT_TO_PI( ptValue ) * 100000.0 ) / 100000.0;
00101     }
00102 
00104     static double toDD( double ptValue ) {
00105         // "dd" values are rounded to 0.00001 inches
00106         return qRound( POINT_TO_DD( ptValue ) * 100000.0 ) / 100000.0;
00107     }
00108 
00110     static double toCC( double ptValue ) {
00111         // "cc" values are rounded to 0.00001 inches
00112         return qRound( POINT_TO_CC( ptValue ) * 100000.0 ) / 100000.0;
00113     }
00114 
00117     static double ptToUnit( double ptValue, Unit unit );
00118 
00121     static QString userValue( double ptValue, Unit unit );
00122 
00125     static double ptFromUnit( double value, Unit unit );
00126 
00129     static double fromUserValue( const QString& value, Unit unit );
00130 
00132     static Unit unit( const QString &_unitName ) {
00133         if ( _unitName == QString::fromLatin1( "mm" ) ) return U_MM;
00134         if ( _unitName == QString::fromLatin1( "cm" ) ) return U_CM;
00135         if ( _unitName == QString::fromLatin1( "dm" ) ) return U_DM;
00136         if ( _unitName == QString::fromLatin1( "in" )
00137             || _unitName == QString::fromLatin1("inch") /*compat*/ ) return U_INCH;
00138         if ( _unitName == QString::fromLatin1( "pi" ) ) return U_PI;
00139         if ( _unitName == QString::fromLatin1( "dd" ) ) return U_DD;
00140         if ( _unitName == QString::fromLatin1( "cc" ) ) return U_CC;
00141         return U_PT;
00142     }
00144     static QString unitName( Unit _unit ) {
00145         if ( _unit == U_MM ) return QString::fromLatin1( "mm" );
00146         if ( _unit == U_CM ) return QString::fromLatin1( "cm" );
00147         if ( _unit == U_DM ) return QString::fromLatin1( "dm" );
00148         if ( _unit == U_INCH ) return QString::fromLatin1( "in" );
00149         if ( _unit == U_PI ) return QString::fromLatin1( "pi" );
00150         if ( _unit == U_DD ) return QString::fromLatin1( "dd" );
00151         if ( _unit == U_CC ) return QString::fromLatin1( "cc" );
00152         return QString::fromLatin1( "pt" );
00153     }
00155     static QString unitDescription( Unit _unit );
00156     static QStringList listOfUnitName();
00157 
00159     static double parseValue( QString value, double defaultVal = 0.0 );
00160     // Note: the above method doesn't take a const ref, since it modifies the arg.
00161 };
00162 
00163 
00164 #endif
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 13:19:27 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2003