F:/KPlato/koffice/libs/kofficecore/KoUnit.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 David Faure <faure@kde.org>
00003    Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 //#include <KoGlobal.h>
00022 #include "KoUnit.h"
00023 #include <KoXmlWriter.h>
00024 
00025 #include <klocale.h>
00026 #include <kglobal.h>
00027 #include <kdebug.h>
00028 
00029 #include <QRegExp>
00030 
00031 QStringList KoUnit::listOfUnitName()
00032 {
00033     QStringList lst;
00034     for ( uint i = 0 ; i <= KoUnit::U_LASTUNIT ; ++i )
00035     {
00036         KoUnit::Unit unit = static_cast<KoUnit::Unit>( i );
00037         lst.append( KoUnit::unitDescription( unit ) );
00038     }
00039     return lst;
00040 }
00041 
00042 QString KoUnit::unitDescription( Unit _unit )
00043 {
00044     switch ( _unit )
00045     {
00046     case KoUnit::U_MM:
00047         return i18n("Millimeters (mm)");
00048     case KoUnit::U_CM:
00049         return i18n("Centimeters (cm)");
00050     case KoUnit::U_DM:
00051         return i18n("Decimeters (dm)");
00052     case KoUnit::U_INCH:
00053         return i18n("Inches (in)");
00054     case KoUnit::U_PI:
00055         return i18n("Pica (pi)");
00056     case KoUnit::U_DD:
00057         return i18n("Didot (dd)");
00058     case KoUnit::U_CC:
00059         return i18n("Cicero (cc)");
00060     case KoUnit::U_PT:
00061         return i18n("Points (pt)" );
00062     default:
00063         return i18n("Error!");
00064     }
00065 }
00066 
00067 double KoUnit::toUserValue( double ptValue, Unit unit )
00068 {
00069     switch ( unit ) {
00070     case U_MM:
00071         return toMM( ptValue );
00072     case U_CM:
00073         return toCM( ptValue );
00074     case U_DM:
00075         return toDM( ptValue );
00076     case U_INCH:
00077         return toInch( ptValue );
00078     case U_PI:
00079         return toPI( ptValue );
00080     case U_DD:
00081         return toDD( ptValue );
00082     case U_CC:
00083         return toCC( ptValue );
00084     case U_PT:
00085     default:
00086         return toPoint( ptValue );
00087     }
00088 }
00089 
00090 double KoUnit::ptToUnit( const double ptValue, const Unit unit )
00091 {
00092     switch ( unit )
00093     {
00094     case U_MM:
00095         return POINT_TO_MM( ptValue );
00096     case U_CM:
00097         return POINT_TO_CM( ptValue );
00098     case U_DM:
00099         return POINT_TO_DM( ptValue );
00100     case U_INCH:
00101         return POINT_TO_INCH( ptValue );
00102     case U_PI:
00103         return POINT_TO_PI( ptValue );
00104     case U_DD:
00105         return POINT_TO_DD( ptValue );
00106     case U_CC:
00107         return POINT_TO_CC( ptValue );
00108     case U_PT:
00109     default:
00110         return ptValue;
00111     }
00112 }
00113 
00114 QString KoUnit::toUserStringValue( double ptValue, Unit unit )
00115 {
00116     return KGlobal::locale()->formatNumber( toUserValue( ptValue, unit ) );
00117 }
00118 
00119 double KoUnit::fromUserValue( double value, Unit unit )
00120 {
00121     switch ( unit ) {
00122     case U_MM:
00123         return MM_TO_POINT( value );
00124     case U_CM:
00125         return CM_TO_POINT( value );
00126     case U_DM:
00127         return DM_TO_POINT( value );
00128     case U_INCH:
00129         return INCH_TO_POINT( value );
00130     case U_PI:
00131         return PI_TO_POINT( value );
00132     case U_DD:
00133         return DD_TO_POINT( value );
00134     case U_CC:
00135         return CC_TO_POINT( value );
00136     case U_PT:
00137     default:
00138         return value;
00139     }
00140 }
00141 
00142 double KoUnit::fromUserValue( const QString& value, Unit unit, bool* ok )
00143 {
00144     return fromUserValue( KGlobal::locale()->readNumber( value, ok ), unit );
00145 }
00146 
00147 double KoUnit::parseValue( QString value, double defaultVal )
00148 {
00149     value.simplified();
00150     value.remove( ' ' );
00151 
00152     if( value.isEmpty() )
00153         return defaultVal;
00154 
00155     int index = value.indexOf( QRegExp( "[a-z]+$" ) );
00156     if ( index == -1 )
00157         return value.toDouble();
00158 
00159     QString unit = value.mid( index );
00160     value.truncate ( index );
00161     double val = value.toDouble();
00162 
00163     if ( unit == "pt" )
00164         return val;
00165 
00166     bool ok;
00167     Unit u = KoUnit::unit( unit, &ok );
00168     if( ok )
00169         return fromUserValue( val, u );
00170 
00171     if( unit == "m" )
00172         return fromUserValue( val * 10.0, U_DM );
00173     else if( unit == "km" )
00174         return fromUserValue( val * 10000.0, U_DM );
00175     kWarning() << "KoUnit::parseValue: Unit " << unit << " is not supported, please report." << endl;
00176 
00177     // TODO : add support for mi/ft ?
00178     return defaultVal;
00179 }
00180 
00181 KoUnit::Unit KoUnit::unit( const QString &_unitName, bool* ok )
00182 {
00183     if ( ok )
00184         *ok = true;
00185     if ( _unitName == QString::fromLatin1( "mm" ) ) return U_MM;
00186     if ( _unitName == QString::fromLatin1( "cm" ) ) return U_CM;
00187     if ( _unitName == QString::fromLatin1( "dm" ) ) return U_DM;
00188     if ( _unitName == QString::fromLatin1( "in" )
00189          || _unitName == QString::fromLatin1("inch") /*compat*/ ) return U_INCH;
00190     if ( _unitName == QString::fromLatin1( "pi" ) ) return U_PI;
00191     if ( _unitName == QString::fromLatin1( "dd" ) ) return U_DD;
00192     if ( _unitName == QString::fromLatin1( "cc" ) ) return U_CC;
00193     if ( _unitName == QString::fromLatin1( "pt" ) ) return U_PT;
00194     if ( ok )
00195         *ok = false;
00196     return U_PT;
00197 }
00198 
00199 QString KoUnit::unitName( Unit _unit )
00200 {
00201     if ( _unit == U_MM ) return QString::fromLatin1( "mm" );
00202     if ( _unit == U_CM ) return QString::fromLatin1( "cm" );
00203     if ( _unit == U_DM ) return QString::fromLatin1( "dm" );
00204     if ( _unit == U_INCH ) return QString::fromLatin1( "in" );
00205     if ( _unit == U_PI ) return QString::fromLatin1( "pi" );
00206     if ( _unit == U_DD ) return QString::fromLatin1( "dd" );
00207     if ( _unit == U_CC ) return QString::fromLatin1( "cc" );
00208     return QString::fromLatin1( "pt" );
00209 }
00210 
00211 void KoUnit::saveOasis(KoXmlWriter* settingsWriter, Unit _unit)
00212 {
00213     settingsWriter->addConfigItem( "unit", unitName(_unit) );
00214 }

Généré le Wed Nov 22 23:41:04 2006 pour KPlato par  doxygen 1.5.1-p1