lib

KoUnit.cpp

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.h>
00030 #include <qdom.h>
00031 
00032 QStringList KoUnit::listOfUnitName()
00033 {
00034     QStringList lst;
00035     for ( uint i = 0 ; i <= KoUnit::U_LASTUNIT ; ++i )
00036     {
00037         KoUnit::Unit unit = static_cast<KoUnit::Unit>( i );
00038         lst.append( KoUnit::unitDescription( unit ) );
00039     }
00040     return lst;
00041 }
00042 
00043 QString KoUnit::unitDescription( Unit _unit )
00044 {
00045     switch ( _unit )
00046     {
00047     case KoUnit::U_MM:
00048         return i18n("Millimeters (mm)");
00049     case KoUnit::U_CM:
00050         return i18n("Centimeters (cm)");
00051     case KoUnit::U_DM:
00052         return i18n("Decimeters (dm)");
00053     case KoUnit::U_INCH:
00054         return i18n("Inches (in)");
00055     case KoUnit::U_PI:
00056         return i18n("Pica (pi)");
00057     case KoUnit::U_DD:
00058         return i18n("Didot (dd)");
00059     case KoUnit::U_CC:
00060         return i18n("Cicero (cc)");
00061     case KoUnit::U_PT:
00062         return i18n("Points (pt)" );
00063     default:
00064         return i18n("Error!");
00065     }
00066 }
00067 
00068 double KoUnit::toUserValue( double ptValue, Unit unit )
00069 {
00070     switch ( unit ) {
00071     case U_MM:
00072         return toMM( ptValue );
00073     case U_CM:
00074         return toCM( ptValue );
00075     case U_DM:
00076         return toDM( ptValue );
00077     case U_INCH:
00078         return toInch( ptValue );
00079     case U_PI:
00080         return toPI( ptValue );
00081     case U_DD:
00082         return toDD( ptValue );
00083     case U_CC:
00084         return toCC( ptValue );
00085     case U_PT:
00086     default:
00087         return toPoint( ptValue );
00088     }
00089 }
00090 
00091 double KoUnit::ptToUnit( const double ptValue, const Unit unit )
00092 {
00093     switch ( unit )
00094     {
00095     case U_MM:
00096         return POINT_TO_MM( ptValue );
00097     case U_CM:
00098         return POINT_TO_CM( ptValue );
00099     case U_DM:
00100         return POINT_TO_DM( ptValue );
00101     case U_INCH:
00102         return POINT_TO_INCH( ptValue );
00103     case U_PI:
00104         return POINT_TO_PI( ptValue );
00105     case U_DD:
00106         return POINT_TO_DD( ptValue );
00107     case U_CC:
00108         return POINT_TO_CC( ptValue );
00109     case U_PT:
00110     default:
00111         return ptValue;
00112     }
00113 }
00114 
00115 QString KoUnit::toUserStringValue( double ptValue, Unit unit )
00116 {
00117     return KGlobal::locale()->formatNumber( toUserValue( ptValue, unit ) );
00118 }
00119 
00120 double KoUnit::fromUserValue( double value, Unit unit )
00121 {
00122     switch ( unit ) {
00123     case U_MM:
00124         return MM_TO_POINT( value );
00125     case U_CM:
00126         return CM_TO_POINT( value );
00127     case U_DM:
00128         return DM_TO_POINT( value );
00129     case U_INCH:
00130         return INCH_TO_POINT( value );
00131     case U_PI:
00132         return PI_TO_POINT( value );
00133     case U_DD:
00134         return DD_TO_POINT( value );
00135     case U_CC:
00136         return CC_TO_POINT( value );
00137     case U_PT:
00138     default:
00139         return value;
00140     }
00141 }
00142 
00143 double KoUnit::fromUserValue( const QString& value, Unit unit, bool* ok )
00144 {
00145     return fromUserValue( KGlobal::locale()->readNumber( value, ok ), unit );
00146 }
00147 
00148 double KoUnit::parseValue( QString value, double defaultVal )
00149 {
00150     value.simplifyWhiteSpace();
00151     value.remove( ' ' );
00152 
00153     if( value.isEmpty() )
00154         return defaultVal;
00155 
00156     int index = value.find( QRegExp( "[a-z]+$" ) );
00157     if ( index == -1 )
00158         return value.toDouble();
00159 
00160     QString unit = value.mid( index );
00161     value.truncate ( index );
00162     double val = value.toDouble();
00163 
00164     if ( unit == "pt" )
00165         return val;
00166 
00167     bool ok;
00168     Unit u = KoUnit::unit( unit, &ok );
00169     if( ok )
00170         return fromUserValue( val, u );
00171 
00172     if( unit == "m" )
00173         return fromUserValue( val * 10.0, U_DM );
00174     else if( unit == "km" )
00175         return fromUserValue( val * 10000.0, U_DM );
00176     kdWarning() << "KoUnit::parseValue: Unit " << unit << " is not supported, please report." << endl;
00177 
00178     // TODO : add support for mi/ft ?
00179     return defaultVal;
00180 }
00181 
00182 KoUnit::Unit KoUnit::unit( const QString &_unitName, bool* ok )
00183 {
00184     if ( ok )
00185         *ok = true;
00186     if ( _unitName == QString::fromLatin1( "mm" ) ) return U_MM;
00187     if ( _unitName == QString::fromLatin1( "cm" ) ) return U_CM;
00188     if ( _unitName == QString::fromLatin1( "dm" ) ) return U_DM;
00189     if ( _unitName == QString::fromLatin1( "in" )
00190          || _unitName == QString::fromLatin1("inch") /*compat*/ ) return U_INCH;
00191     if ( _unitName == QString::fromLatin1( "pi" ) ) return U_PI;
00192     if ( _unitName == QString::fromLatin1( "dd" ) ) return U_DD;
00193     if ( _unitName == QString::fromLatin1( "cc" ) ) return U_CC;
00194     if ( _unitName == QString::fromLatin1( "pt" ) ) return U_PT;
00195     if ( ok )
00196         *ok = false;
00197     return U_PT;
00198 }
00199 
00200 QString KoUnit::unitName( Unit _unit )
00201 {
00202     if ( _unit == U_MM ) return QString::fromLatin1( "mm" );
00203     if ( _unit == U_CM ) return QString::fromLatin1( "cm" );
00204     if ( _unit == U_DM ) return QString::fromLatin1( "dm" );
00205     if ( _unit == U_INCH ) return QString::fromLatin1( "in" );
00206     if ( _unit == U_PI ) return QString::fromLatin1( "pi" );
00207     if ( _unit == U_DD ) return QString::fromLatin1( "dd" );
00208     if ( _unit == U_CC ) return QString::fromLatin1( "cc" );
00209     return QString::fromLatin1( "pt" );
00210 }
00211 
00212 void KoUnit::saveOasis(KoXmlWriter* settingsWriter, Unit _unit)
00213 {
00214     settingsWriter->addConfigItem( "unit", unitName(_unit) );
00215 }
KDE Home | KDE Accessibility Home | Description of Access Keys