lib Library API Documentation

koUnit.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 David Faure <david@mandrakesoft.com>
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 //#include <koGlobal.h>
00021 #include "koUnit.h"
00022 #include <klocale.h>
00023 #include <kglobal.h>
00024 #include <qregexp.h>
00025 #include <kdebug.h>
00026 
00027 QStringList KoUnit::listOfUnitName()
00028 {
00029     QStringList lst;
00030     for ( uint i = 0 ; i <= KoUnit::U_LASTUNIT ; ++i )
00031     {
00032         KoUnit::Unit unit = static_cast<KoUnit::Unit>( i );
00033         lst.append( KoUnit::unitDescription( unit ) );
00034     }
00035     return lst;
00036 }
00037 
00038 QString KoUnit::unitDescription( Unit _unit )
00039 {
00040     switch ( _unit )
00041     {
00042     case KoUnit::U_MM:
00043         return i18n("Millimeters (mm)");
00044     case KoUnit::U_CM:
00045         return i18n("Centimeters (cm)");
00046     case KoUnit::U_DM:
00047         return i18n("Decimeters (dm)");
00048     case KoUnit::U_INCH:
00049         return i18n("Inches (in)");
00050     case KoUnit::U_PI:
00051         return i18n("Pica (pi)");
00052     case KoUnit::U_DD:
00053         return i18n("Didot (dd)");
00054     case KoUnit::U_CC:
00055         return i18n("Cicero (cc)");
00056     case KoUnit::U_PT:
00057         return i18n("Points (pt)" );
00058     default:
00059         return i18n("Error!");
00060     }
00061 }
00062 
00063 double KoUnit::ptToUnit( double ptValue, Unit unit )
00064 {
00065     switch ( unit ) {
00066     case U_MM:
00067         return toMM( ptValue );
00068     case U_CM:
00069         return toCM( ptValue );
00070     case U_DM:
00071         return toDM( ptValue );
00072     case U_INCH:
00073         return toInch( ptValue );
00074     case U_PI:
00075         return toPI( ptValue );
00076     case U_DD:
00077         return toDD( ptValue );
00078     case U_CC:
00079         return toCC( ptValue );
00080     case U_PT:
00081     default:
00082         return toPoint( ptValue );
00083     }
00084 }
00085 
00086 QString KoUnit::userValue( double ptValue, Unit unit )
00087 {
00088     return KGlobal::locale()->formatNumber( ptToUnit( ptValue, unit ) );
00089 }
00090 
00091 double KoUnit::ptFromUnit( double value, Unit unit )
00092 {
00093     switch ( unit ) {
00094     case U_MM:
00095         return MM_TO_POINT( value );
00096     case U_CM:
00097         return CM_TO_POINT( value );
00098     case U_DM:
00099         return DM_TO_POINT( value );
00100     case U_INCH:
00101         return INCH_TO_POINT( value );
00102     case U_PI:
00103         return PI_TO_POINT( value );
00104     case U_DD:
00105         return DD_TO_POINT( value );
00106     case U_CC:
00107         return CC_TO_POINT( value );
00108     case U_PT:
00109     default:
00110         return value;
00111     }
00112 }
00113 
00114 double KoUnit::fromUserValue( const QString& value, Unit unit )
00115 {
00116     bool ok; // TODO pass as parameter
00117     return ptFromUnit( KGlobal::locale()->readNumber( value, &ok ), unit );
00118 }
00119 
00120 double KoUnit::parseValue( QString value, double defaultVal )
00121 {
00122     value.simplifyWhiteSpace();
00123     value.remove( ' ' );
00124 
00125     if( value.isEmpty() )
00126         return defaultVal;
00127 
00128     int index = value.find( QRegExp( "[a-z]+$" ) );
00129     if ( index == -1 )
00130         return value.toDouble();
00131 
00132     QString unit = value.mid( index );
00133     value.truncate ( index );
00134     double val = value.toDouble();
00135 
00136     if ( unit == "pt" )
00137         return val;
00138 
00139     Unit u = KoUnit::unit( unit );
00140     if( u != U_PT )
00141         return ptFromUnit( val, u );
00142     if( unit == "m" )
00143         return ptFromUnit( val * 10.0, U_DM );
00144     else if( unit == "km" )
00145         return ptFromUnit( val * 10000.0, U_DM );
00146     kdWarning() << "KoUnit::parseValue: Unit " << unit << "is not supported, please report." << endl;
00147 
00148     // TODO : add support for mi/ft ?
00149     return defaultVal;
00150 }
00151 
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