From 171de66037484e636a6d0e113eb28534fd00ed9e Mon Sep 17 00:00:00 2001 From: Carsten Niehaus Date: Thu, 13 Oct 2005 12:23:09 +0000 Subject: [PATCH] * Make everything compile (before it only linked ;-) * Add tempunit.h for temperature-conversion svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=470240 --- libphysics/Makefile.am | 14 +-- libphysics/element.cpp | 213 ++++++++++++++++++++-------------------- libphysics/tempunit.cpp | 134 +++++++++++++++++++++++++ libphysics/tempunit.h | 92 +++++++++++++++++ 4 files changed, 337 insertions(+), 116 deletions(-) create mode 100644 libphysics/tempunit.cpp create mode 100644 libphysics/tempunit.h diff --git a/libphysics/Makefile.am b/libphysics/Makefile.am index 74f4635..ac1b9cc 100644 --- a/libphysics/Makefile.am +++ b/libphysics/Makefile.am @@ -4,17 +4,13 @@ INCLUDES= $(all_includes) lib_LTLIBRARIES = libphysics.la -libextdate_la_SOURCES = element.cpp \ +libphysics_la_SOURCES = element.cpp \ spectrum.cpp \ isotope.cpp \ - spectrumparser.cpp + spectrumparser.cpp \ + tempunit.cpp -EXTRA_DIST = element.cpp element.h \ - spectrum.cpp spectrum.h \ - isotope.cpp isotope.h \ - spectrumparser.cpp spectrumparser.h - -libextdate_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 4:0:0 -libextdate_la_LIBADD = $(LIB_KDEUI) +libphysics_la_LDFLAGS = $(all_libraries) -no-undefined -version-info 4:0:0 +libphysics_la_LIBADD = $(LIB_KDEUI) METASOURCES = AUTO diff --git a/libphysics/element.cpp b/libphysics/element.cpp index 179d2bd..f6a919c 100644 --- a/libphysics/element.cpp +++ b/libphysics/element.cpp @@ -19,7 +19,6 @@ ***************************************************************************/ #include "element.h" -#include "prefs.h" #include "spectrum.h" #include "isotope.h" #include "tempunit.h" @@ -107,17 +106,17 @@ const QString Element::adjustUnits( const int type, double value ) QString v; if ( type == IE ) //an ionization energy { - if ( Prefs::energies() == 0 ) - { - value*=96.6; - v = QString::number( value ); - v.append( " kJ/mol" ); - } - else // use electronvolt - { - v = QString::number( value ); - v.append( " eV" ); - } +//X if ( Prefs::energies() == 0 ) +//X { +//X value*=96.6; +//X v = QString::number( value ); +//X v.append( " kJ/mol" ); +//X } +//X else // use electronvolt +//X { +//X v = QString::number( value ); +//X v.append( " eV" ); +//X } } return v; } @@ -128,101 +127,101 @@ const QString Element::adjustUnits( const int type ) double val = 0.0; //the value to convert - if ( type == BOILINGPOINT || type == MELTINGPOINT ) // convert a temperature - { - if ( type == BOILINGPOINT ) - val = boiling(); - else - val = melting(); - - if ( val <= 0 ) - v = i18n( "Value unknown" ); - else - { - double newvalue = TempUnit::convert( val, (int)TempUnit::Kelvin, Prefs::temperature() ); - switch (Prefs::temperature()) { - case 0: //Kelvin - v = i18n( "%1 is the temperature in Kelvin", "%1 K" ).arg( newvalue ); - break; - case 1://Kelvin to Celsius - v = i18n( "%1 is the temperature in Celsius", "%1 %2C" ).arg( newvalue ).arg( QChar(0xB0) ); - break; - case 2: // Kelvin to Fahrenheit - v = i18n( "%1 is the temperature in Fahrenheit", "%1 %2F" ).arg( newvalue ).arg( QChar(0xB0) ); - break; - case 3: // Kelvin to Rankine - v = i18n( "%1 is the temperature in Rankine", "%1 %2Ra" ).arg( newvalue ).arg( QChar(0xB0) ); - break; - case 4: // Kelvin to Reamur - v = i18n( "%1 is the temperature in Reamur", "%1 %2R" ).arg( newvalue ).arg( QChar(0xB0) ); - break; - } - } - } - else if ( type == EN ) //Electronegativity - { - val = electroneg(); - if ( val <= 0 ) - v = i18n( "Value not defined" ); - else - v = QString::number( val ); - } - else if ( type == EA ) //Electron affinity - { - val = electroaf(); - if ( val == 0.0 ) - v = i18n( "Value not defined" ); - else - { - if ( Prefs::energies() == 0 ) - { - v = i18n( "%1 kJ/mol" ).arg( QString::number( val ) ); - } - else // use electronvolt - { - val/=96.6; - v = i18n( "%1 eV" ).arg( QString::number( val ) ); - } - } - } - else if ( type == MASS ) // its a mass - { - val = mass(); - if ( val <= 0 ) - v = i18n( "Value unknown" ); - else - v = i18n( "%1 u" ).arg( QString::number( val ) ); - } - else if ( type == DENSITY ) // its a density - { - val = density(); - - if ( val <= 0 ) - v = i18n( "Value unknown" ); - else - { - if ( boiling() < 295.0 )//gasoline - { - v = i18n( "%1 g/L" ).arg( QString::number( val ) ); - } - else//liquid or solid - { - v = i18n( "%1 g/cm3" ).arg( QString::number( val ) ); - } - } - } - else if ( type == DATE ) //its a date - { - val = date(); - if ( val < 1600 ) - { - v = i18n( "This element was known to ancient cultures" ); - } - else - { - v = i18n( "This element was discovered in the year %1" ).arg( QString::number( val ) ); - } - } +//X if ( type == BOILINGPOINT || type == MELTINGPOINT ) // convert a temperature +//X { +//X if ( type == BOILINGPOINT ) +//X val = boiling(); +//X else +//X val = melting(); +//X +//X if ( val <= 0 ) +//X v = i18n( "Value unknown" ); +//X else +//X { +//X double newvalue = TempUnit::convert( val, (int)TempUnit::Kelvin, Prefs::temperature() ); +//X switch (Prefs::temperature()) { +//X case 0: //Kelvin +//X v = i18n( "%1 is the temperature in Kelvin", "%1 K" ).arg( newvalue ); +//X break; +//X case 1://Kelvin to Celsius +//X v = i18n( "%1 is the temperature in Celsius", "%1 %2C" ).arg( newvalue ).arg( QChar(0xB0) ); +//X break; +//X case 2: // Kelvin to Fahrenheit +//X v = i18n( "%1 is the temperature in Fahrenheit", "%1 %2F" ).arg( newvalue ).arg( QChar(0xB0) ); +//X break; +//X case 3: // Kelvin to Rankine +//X v = i18n( "%1 is the temperature in Rankine", "%1 %2Ra" ).arg( newvalue ).arg( QChar(0xB0) ); +//X break; +//X case 4: // Kelvin to Reamur +//X v = i18n( "%1 is the temperature in Reamur", "%1 %2R" ).arg( newvalue ).arg( QChar(0xB0) ); +//X break; +//X } +//X } +//X } +//X else if ( type == EN ) //Electronegativity +//X { +//X val = electroneg(); +//X if ( val <= 0 ) +//X v = i18n( "Value not defined" ); +//X else +//X v = QString::number( val ); +//X } +//X else if ( type == EA ) //Electron affinity +//X { +//X val = electroaf(); +//X if ( val == 0.0 ) +//X v = i18n( "Value not defined" ); +//X else +//X { +//X if ( Prefs::energies() == 0 ) +//X { +//X v = i18n( "%1 kJ/mol" ).arg( QString::number( val ) ); +//X } +//X else // use electronvolt +//X { +//X val/=96.6; +//X v = i18n( "%1 eV" ).arg( QString::number( val ) ); +//X } +//X } +//X } +//X else if ( type == MASS ) // its a mass +//X { +//X val = mass(); +//X if ( val <= 0 ) +//X v = i18n( "Value unknown" ); +//X else +//X v = i18n( "%1 u" ).arg( QString::number( val ) ); +//X } +//X else if ( type == DENSITY ) // its a density +//X { +//X val = density(); +//X +//X if ( val <= 0 ) +//X v = i18n( "Value unknown" ); +//X else +//X { +//X if ( boiling() < 295.0 )//gasoline +//X { +//X v = i18n( "%1 g/L" ).arg( QString::number( val ) ); +//X } +//X else//liquid or solid +//X { +//X v = i18n( "%1 g/cm3" ).arg( QString::number( val ) ); +//X } +//X } +//X } +//X else if ( type == DATE ) //its a date +//X { +//X val = date(); +//X if ( val < 1600 ) +//X { +//X v = i18n( "This element was known to ancient cultures" ); +//X } +//X else +//X { +//X v = i18n( "This element was discovered in the year %1" ).arg( QString::number( val ) ); +//X } +//X } return v; } diff --git a/libphysics/tempunit.cpp b/libphysics/tempunit.cpp new file mode 100644 index 0000000..55d3630 --- /dev/null +++ b/libphysics/tempunit.cpp @@ -0,0 +1,134 @@ +/*************************************************************************** + * Copyright (C) 2005 by Pino Toscano * + * toscano.pino@tiscali.it * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "tempunit.h" + +double TempUnit::convert( double value, TempUnit::Unit from, TempUnit::Unit to ) +{ + /** + * These are the four formula needed to convert from + * Kelvin the the other units: + + Kelvin Celsius °C = K - 273,15 + Kelvin Fahrenheit °F = K × 1,8 - 459,67 + Kelvin Rankine °Ra = K × 1,8 + Kelvin Reaumur °R = ( K - 273,15 ) × 0,8 + */ + + if ( from != Kelvin ) + value = convertToKelvin( value, from ); + + //now 'value' is in Kelvin + switch( to ) + { + case Celsius: + return value - 273.16; + case Fahrenheit: + return value * 1.8 - 459.67; + case Rankine: + return value * 1.8; + case Reaumur: + return ( value - 273.15 )* 0.8; + case Kelvin: + return value; + } + return value; +} + +double TempUnit::convertToKelvin( const double value, TempUnit::Unit from ) +{ + switch( from ) + { + case Kelvin: + return value; + case Celsius: + return value + 273.16; + case Fahrenheit: + return ( value + 459.67 )/1.8; + case Rankine: + return value / 1.8; + case Reaumur: + return ( value * 1.25 ) + 273.15; + } + return value; +} + +double TempUnit::convert( const double value, int from, int to ) +{ + return convert( value, (TempUnit::Unit)from, (TempUnit::Unit)to ); +} + +QStringList TempUnit::unitListSymbols() +{ + QStringList list; + list << "K"; + list << QString::fromUtf8("°C"); + list << QString::fromUtf8("°F"); + list << QString::fromUtf8("°Ra"); + list << QString::fromUtf8("°R"); + return list; +} + +QString TempUnit::unitListSymbol( int i ) +{ + QStringList l = unitListSymbols(); + if ( ( i < 0 ) || ( i >= (int)l.count() ) ) + return l[0]; + return l[i]; +} + +QPair TempUnit::rangeForUnit( TempUnit::Unit u ) +{ + switch( u ) + { + case Kelvin: + { + return QPair( 0.0, 5000.0 ); + break; + } + case Celsius: + { + return QPair( -273.16, 4726.84 ); + break; + } + case Fahrenheit: + { + return QPair( -459.67, 8540.33 ); + break; + } + case Rankine: + { + return QPair( 0.0, 9000.0 ); + break; + } + case Reaumur: + { + return QPair( -218.52, 3781.48 ); + break; + } + } + return QPair( 0.0, 5000.0 ); +} + +QPair TempUnit::rangeForUnit( int u ) +{ + return rangeForUnit( (TempUnit::Unit)u ); +} + diff --git a/libphysics/tempunit.h b/libphysics/tempunit.h new file mode 100644 index 0000000..db4c366 --- /dev/null +++ b/libphysics/tempunit.h @@ -0,0 +1,92 @@ +#ifndef TEMPUNIT_H +#define TEMPUNIT_H +/*************************************************************************** + * Copyright (C) 2005 by Pino Toscano * + * toscano.pino@tiscali.it * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include +#include + +/** + * This class obtains all the info needed to work with the units of measure + * of temperature. + * + * It has static methods you can call to get the various infos. The most + * important methods is convert(), that performs conversions. + * + * @author Pino Toscano + */ +class TempUnit +{ + public: + + enum Unit { Kelvin = 0, + Celsius, + Fahrenheit, + Rankine, + Reaumur + }; + + /** + * Convert the temperature unit @p value given in the unit + * @p from to the unit @p to. + * + * @return the value converted + */ + static double convert( double value, + TempUnit::Unit from, + TempUnit::Unit to ); + + + /** + * @param value the temperature to convert + * @param from the unit from which to convert + * + * @return the temperature to Kelvin + */ + static double convertToKelvin( const double value, + TempUnit::Unit from ); + + /** + * @overload + */ + static double convert( const double value, + int from, + int to ); + + /** + * @return a list with the symbols of the temperature units + * we support + */ + static QStringList unitListSymbols(); + + /** + * @return the symbol of the @p i -th unit + */ + static QString unitListSymbol( int i ); + + static QPair rangeForUnit( TempUnit::Unit u ); + + /** + * @overload + */ + static QPair rangeForUnit( int u ); + +}; +#endif // TEMPUNIT_H -- 2.47.3