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