]> Git trees. - libqmvoc.git/commitdiff
first bunch of updates from 3.5 branch and compiling fixes
authorPino Toscano <pino@kde.org>
Thu, 1 Sep 2005 16:50:47 +0000 (16:50 +0000)
committerPino Toscano <pino@kde.org>
Thu, 1 Sep 2005 16:50:47 +0000 (16:50 +0000)
svn path=/trunk/KDE/kdeedu/kalzium/src/spectrum.h; revision=455913

kalzium/src/element.cpp
kalzium/src/element.h
kalzium/src/isotope.cpp
kalzium/src/isotope.h
kalzium/src/spectrum.cpp
kalzium/src/spectrum.h
kalzium/src/tempunit.cpp [new file with mode: 0644]
kalzium/src/tempunit.h [new file with mode: 0644]

index d920c000ee3795e2a7a0a486e923de0d2a49354a..26f49118ae5a2718ee55b411ccdc72da9684f398 100644 (file)
 #include "spectrum.h"
 #include "isotope.h"
 #include "kalziumdataobject.h"
+#include "kalziumutils.h"
 
 #include <qdom.h>
 #include <qfile.h>
 #include <qpainter.h>
 #include <qregexp.h>
+#include <qfontmetrics.h>
 
 #include <kdebug.h>
 #include <klocale.h>
 #include <kurl.h>
 #include <kstandarddirs.h>
 
-#include <math.h>
-
 Element::Element()
 {
        m_radioactive = false;
        m_artificial = false;
+       m_abundance = 0;
+}
+
+Isotope* Element::isotopeByNucleons( int numberOfNucleons )
+{
+       QList<Isotope*>::ConstIterator it = m_isotopeList.begin();
+       const QList<Isotope*>::ConstIterator itEnd = m_isotopeList.end();
+
+       for ( ; it != itEnd; ++it )
+       {
+               if ( ( ( *it )->neutrons() + ( *it )->protones() ) == numberOfNucleons )
+                       return *it;
+       }
+       return 0;
 }
 
 QString Element::parsedOrbits( bool canBeEmpty )
@@ -58,30 +72,10 @@ QString Element::parsedOrbits( bool canBeEmpty )
        return orbits;
 }
 
-
-double Element::strippedValue( double num )
-{
-       if ( !finite( num ) )
-               return num;
-
-       double power;
-       power = 1e-6;
-       while ( power < num )
-               power *= 10;
-
-       num = num / power * 10000;
-       num = round( num );
-
-       return num * power / 10000;
-}
-
-
 Element::~Element()
 {
 }
 
-
-
 double Element::meanmass()
 {
        return m_mass/m_number;
@@ -158,11 +152,11 @@ const QString Element::adjustUnits( const int type )
                                        break;
                                case 1://Kelvin to Celsius
                                        val-=273.15;
-                                       v = i18n( "%1 is the temperature in Celsius", "%1 C" ).arg( QString::number( val ) );
+                                       v = i18n( "%1 is the temperature in Celsius", "%1 %2C" ).arg( QString::number( val ) ).arg( "\xB0" );
                                        break;
                                case 2: // Kelvin to Fahrenheit
                                        val = val * 1.8 - 459.67;
-                                       v = i18n( "%1 is the temperature in Fahrenheit", "%1 F" ).arg( QString::number( val ) );
+                                       v = i18n( "%1 is the temperature in Fahrenheit", "%1 %2F" ).arg( QString::number( val ) ).arg("\xB0");
                                        break;
                        }
                }
@@ -253,7 +247,8 @@ void Element::drawStateOfMatter( QPainter* p, double temp )
        
        QString text;
        QFont symbol_font = p->font();
-       symbol_font.setPointSize( 18 );
+
+       symbol_font.setPointSize( 10 );
        QFont f = p->font();
        f.setPointSize( 9 );
                
@@ -261,7 +256,7 @@ void Element::drawStateOfMatter( QPainter* p, double temp )
 
        //top left
        p->setPen( Qt::black );
-       text = QString::number( strippedValue( mass( ) ) );
+       text = QString::number( KalziumUtils::strippedValue( mass( ) ) );
        p->drawText( X,Y ,ELEMENTSIZE,h_small,Qt::AlignCenter, text );
 
        text = QString::number( number() );
@@ -274,7 +269,7 @@ void Element::drawStateOfMatter( QPainter* p, double temp )
        p->setPen( Qt::black );
        p->drawRect( X, Y,ELEMENTSIZE+1,ELEMENTSIZE+1);
 }
-       
+
 QColor Element::currentColor( const double temp )
 {
        QColor color;
@@ -315,7 +310,7 @@ void Element::drawGradient( QPainter* p, const QString& value, const QColor& c)
        setElementColor( c );
        
        //the height of a "line" inside an element
-       int h_small = 15; //the size for the small units like elementnumber
+       int h_small = 10; //the size for the small units like elementnumber
 
        //The X-coordiante
        int X = xPos();
@@ -328,16 +323,18 @@ void Element::drawGradient( QPainter* p, const QString& value, const QColor& c)
        
        p->setPen( Qt::black );
        QFont symbol_font = p->font();
-       symbol_font.setPointSize( 18 );
        QFont f = p->font();
-       f.setPointSize( 9 );
-               
+
+       f.setPointSize( KalziumUtils::maxSize(value, QRect( X,Y+ELEMENTSIZE-h_small, ELEMENTSIZE, h_small ),f, p ) );
        p->setFont( f );
 
        p->drawText( X,Y+ELEMENTSIZE-h_small , ELEMENTSIZE, h_small,Qt::AlignCenter, value );
 
+       const QRect rect = QRect( X,Y,ELEMENTSIZE-2,ELEMENTSIZE-10 );
+       int goodsize = KalziumUtils::maxSize( symbol(), rect, symbol_font, p );
+       symbol_font.setPointSize( goodsize );
        p->setFont( symbol_font );
-       p->drawText( X,Y, ELEMENTSIZE,ELEMENTSIZE,Qt::AlignCenter, symbol() );
+       p->drawText( X+1,Y+5, ELEMENTSIZE-2,ELEMENTSIZE-10,Qt::AlignCenter, symbol() );
        
        //border
        p->setPen( Qt::black );
@@ -347,7 +344,7 @@ void Element::drawGradient( QPainter* p, const QString& value, const QColor& c)
 void Element::drawSelf( QPainter* p, bool simple, bool isCrystal )
 {
        //the height of a "line" inside an element
-       int h_small = 15; //the size for the small units like elementnumber
+       int h_small = 12; //the size for the small units like elementnumber
 
        //The X-coordiante
        int X = xPos();
@@ -357,15 +354,30 @@ void Element::drawSelf( QPainter* p, bool simple, bool isCrystal )
 
        p->setPen( elementColor() );
        p->fillRect( X, Y,ELEMENTSIZE,ELEMENTSIZE, elementColor() );
+       p->setPen( Qt::black );
        
        QString text;
        QFont symbol_font = p->font();
-       symbol_font.setPointSize( 18 );
+       
+       const int max = ELEMENTSIZE-10;
+       
+       const QRect rect = QRect( X,Y,ELEMENTSIZE-2,max );
+
+       int goodsize = KalziumUtils::maxSize( symbol(), rect, symbol_font, p );
+       symbol_font.setPointSize( goodsize );
+       p->setFont( symbol_font );
+       
+       if ( !simple )
+               p->drawText( X+1,Y+5, ELEMENTSIZE-2,max,Qt::AlignCenter, symbol() );
+       else
+               p->drawText( X+1,Y+5, ELEMENTSIZE-2,max,Qt::AlignHCenter, symbol() );
+       
        QFont f = p->font();
-       f.setPointSize( 9 );
-               
+
+       QRect smallRect( X,Y ,ELEMENTSIZE-4,h_small );
+       f.setPointSize( KalziumUtils::maxSize( QString::number( number() ), smallRect, f, p ) );
+       
        p->setFont( f );
-       p->setPen( Qt::black );
 
        if ( !simple )
        {//the user only wants a simple periodic table, don't weight the cell
@@ -389,24 +401,15 @@ void Element::drawSelf( QPainter* p, bool simple, bool isCrystal )
                                text = i18n( "Crystalsystem hexagonal dense packed", "hdp" );
                        else if ( structure == "ccp" )
                                text = i18n( "Crystalsystem cubic close packed", "ccp" );
-//                     else
-//                             text = QString::null;
                }
                else
-                       text = QString::number( strippedValue( mass( ) ) );
-               p->drawText( X,Y ,ELEMENTSIZE,h_small,Qt::AlignCenter, text );
+                       text = QString::number( KalziumUtils::strippedValue( mass( ) ) );
+               p->drawText( X+2,Y ,ELEMENTSIZE-4 ,h_small,Qt::AlignCenter, text );
        }
-
+       
        text = QString::number( number() );
-       p->drawText( X,Y+ELEMENTSIZE-h_small , ELEMENTSIZE, h_small,Qt::AlignCenter, text );
-
-       p->setFont( symbol_font );
-       if ( !simple )
-               p->drawText( X,Y, ELEMENTSIZE,ELEMENTSIZE,Qt::AlignCenter, symbol() );
-       else
-               p->drawText( X,Y, ELEMENTSIZE,ELEMENTSIZE,Qt::AlignHCenter, symbol() );
+       p->drawText( X+2,Y+ELEMENTSIZE-h_small , ELEMENTSIZE-4, h_small,Qt::AlignCenter, text );
        
-       p->setPen( Qt::black );
        p->drawRect( X, Y,ELEMENTSIZE+1,ELEMENTSIZE+1);
 }
 
index 008a643a26dfd73f37c037bc3272b7311d7c62b7..e0212dd1c8d3be656187b03b82c9afee2e245f87 100644 (file)
 #define ELEMENTSIZE 40
 
 #include <qcolor.h>
-#include <qvaluelist.h>
+#include <qlist.h>
 
 class Element;
 class QDomDocument;
 class QPainter;
 class QPoint;
+class QFont;
+class QRect;
 class Spectrum;
 class Isotope;
 
 struct coordinate;
 
-typedef QValueList<Element*> EList;
-typedef QValueList<coordinate> CList;
-typedef QValueList<double> doubleList;
+typedef QList<Element*> EList;
+typedef QList<coordinate> CList;
+typedef QList<double> doubleList;
 
 struct coordinate{
        int x;
@@ -55,6 +57,12 @@ class Element{
                Element();
 
                virtual ~Element();
+
+               /**
+                * @returns a pointer to the istope with @p numberOfNucleons
+                * nucleons
+                */
+               Isotope* isotopeByNucleons( int numberOfNucleons );
                
                enum RADIUSTYPE
                {
@@ -71,14 +79,23 @@ class Element{
                        return m_number;
                }
 
+               /**
+                * @return if the Element is radioactive
+                */
                bool radioactive() const{
                        return m_radioactive;
                }
                
+               /**
+                * @return if the Element is artificial
+                */
                bool artificial() const{
                        return m_artificial;
                }
 
+               /**
+                * @return the information where the name of the Element comes from
+                */
                QString nameOrigin() const{
                        return m_origin;
                }
@@ -92,6 +109,11 @@ class Element{
                void setEA( double value ) { m_EA = value; }
                void setMeltingpoint( double value ) { m_MP = value; }
                void setBoilingpoint( double value ) { m_BP = value; }
+
+               /**
+                * sets the density of the Element
+                * @param value the density of the Element
+                */
                void setDensity( double value ) { m_Density = value; }
 
                /**
@@ -106,6 +128,12 @@ class Element{
                void setBiologicalMeaning( int value ) { m_biological = value; }
                void setNumber( int num ){ m_number = num; }
 
+               /**
+                * set the abundance in crustal rocks [pm]
+                * @param abundance the abundace in crustal rocks
+                */
+               void setAbundance( int abundance ){ m_abundance = abundance; }
+
                void setScientist( const QString& value ) { m_scientist = value; }
                void setCrysatalstructure( const QString& value ) { m_crystalstructure = value; }
                void setName( const QString& value ) { m_name = value; }
@@ -118,26 +146,28 @@ class Element{
                void setOxydation( const QString& value ) { m_oxstage = value; }
                void setAcidicbehaviour( const QString& value ) { m_acidbeh = value; }
                void setIsotopes( const QString& value ) { m_isotopes = value; }
-
+               
                void setArtificial(){ m_artificial = true; }
                void setRadioactive(){ m_radioactive = true; }
+               
+               void setIonisationList( doubleList l ){ m_ionenergies = l; }
 
-               void setIonisationList( doubleList l ){
-                       m_ionenergies = l;
-               }
-
-               QValueList<Isotope*> isotopes() const{
+               QList<Isotope*> isotopes() const{
                        return m_isotopeList;
         }
 
-               QValueList<double> spectrumList() const{
+               QList<double> spectrumList() const{
                        return m_spectrumList;
                }
 
-               void setIsotopeList( QValueList<Isotope*> list ){
+               void setIsotopeList( QList<Isotope*> list ){
                        m_isotopeList = list;
                }
 
+               /**
+                * sets the Spectrum of the Element
+                * @param spec the Spectrum of the Element
+                */
                void setSpectrum( Spectrum *spec ){
                        m_spectrum = spec;
                }
@@ -149,10 +179,17 @@ class Element{
                        return m_hasSpectrum;
                }
 
+               /**
+                * define if the element has a known Spectrum
+                * @param value if true, the Element has a Spectrum
+                */
                void setHasSepctrum(bool value){
                        m_hasSpectrum = value;
                }
 
+               /**
+                * @return the Spectrum of the element
+                */
                Spectrum* spectrum() const{
                        return m_spectrum;
                }
@@ -193,6 +230,13 @@ class Element{
                int biological() const {
                        return m_biological;
                }
+
+               /**
+                * @return the abundance in crustal rocks in parts per million
+                */
+               int abundance() const {
+                       return 12;
+               }
                
                /**
                 * @return the symbol of the element
@@ -373,12 +417,6 @@ class Element{
                        IONICRADIUS
                };
 
-               /**
-                * calculate the 4-digit value of the value @p w
-                * @return the 4-digit value
-                */
-               static double strippedValue( double w );
-
                QPoint pos() const;
                QPoint coords() const;
 
@@ -401,9 +439,9 @@ class Element{
 
                bool m_hasSpectrum;
 
-               QValueList<Isotope*> m_isotopeList;
+               QList<Isotope*> m_isotopeList;
 
-               QValueList<double> m_spectrumList;
+               QList<double> m_spectrumList;
        
                QColor m_Color;
 
@@ -425,7 +463,8 @@ class Element{
                int     m_number,
                        m_date,
                        m_biological,
-                       m_period;
+                       m_period,
+                       m_abundance;
 
                QString m_symbol,
                        m_name,
index 2395f04064b4ffefe192d90c999fcfe09d2e9b5c..a8908bdd5814715cc2f143d1a1f4be475388cb67 100644 (file)
@@ -22,7 +22,7 @@
 #include <kdebug.h>
 #include <klocale.h>
 
-Isotope::Isotope( int neutrons, int protones,  double percentage, double weight, double halflife, QString format, bool alphadecay, bool betaplusdecay, bool betaminusdecay, bool ecdecay, double alphapercentage, double betapluspercentage, double betaminuspercentage, double ecpercentage, double decayenergy, QString spin, QString magmoment)
+Isotope::Isotope( int neutrons, int protones,  double percentage, double weight, double halflife, QString format, double alphadecay, double betaplusdecay, double betaminusdecay, double ecdecay, double alphapercentage, double betapluspercentage, double betaminuspercentage, double ecpercentage, QString spin, QString magmoment)
 {
        m_neutrons = neutrons;
        m_protones = protones;
@@ -35,7 +35,6 @@ Isotope::Isotope( int neutrons, int protones,  double percentage, double weight,
        m_betaminusdecay = betaminusdecay;
        m_ecdecay = ecdecay;
        
-       m_decayenergy = decayenergy;
        m_spin = spin;
        m_magmoment = magmoment;
 
index 063c29ae439f887a1b4a43e1872e2ba4a0c33ca0..681bc67ba6022f0a0d2b30048efc675244b3a4c2 100644 (file)
@@ -37,15 +37,14 @@ class Isotope
                                double weight, 
                                double halflife, 
                                QString format, 
-                               bool alphadecay, 
-                               bool betaplusdecay, 
-                               bool betaminusdecay, 
-                               bool ecdecay, 
+                               double alphadecay, 
+                               double betaplusdecay, 
+                               double betaminusdecay, 
+                               double ecdecay, 
                                double alphapercentage, 
                                double betapluspercentage, 
                                double betaminuspercentage, 
                                double ecpercentage, 
-                               double decayenergy, 
                                QString spin, 
                                QString magmoment);
 
@@ -82,6 +81,13 @@ class Isotope
                        return m_protones;
                }
 
+               /**
+                * @return the number of nucleons of ths isotope
+                */
+               int nucleons() const{
+                       return m_protones+m_neutrons;
+               }
+
                /**
                 * the weight of the isotope
                 */
@@ -90,34 +96,33 @@ class Isotope
                }
                
                /**
-                * @return true if the isotopes decays in alpha rays
+                * @return the energy of isotope's alpha decay 
                 */
-               bool alphadecay() const{
+               double alphadecay() const{
                        return m_alphadecay;
                }
                
                /**
-                * @return true if the isotopes decays in beta plus rays
+                * @return the energy of isotope's beta plus decay
                 */
-               bool betaplusdecay() const{
+               double betaplusdecay() const{
                        return m_betaplusdecay;
                }
                
                /**
-                * @return true if the isotopes decays in beta-minus rays
+                * @return the energy of isotope's beta-minus decay
                 */
-               bool betaminusdecay() const{
+               double betaminusdecay() const{
                        return m_betaminusdecay;
                }
                
-               bool ecdecay() const{
+               /**
+                * @return the energy of isotope's EC- decay
+                */
+               double ecdecay() const{
                        return m_ecdecay;
                }
-               
-               double decayenergy() const{
-                       return m_decayenergy;
-               }
-               
+       
                QString spin() const{
                        return m_spin;
                }
@@ -196,16 +201,15 @@ class Isotope
                 */
                int m_protones;
        
-               ///Specify if the decay is of this kind
-               bool  m_alphadecay;
-               ///Specify if the decay is of this kind
-               bool  m_betaplusdecay;
-               ///Specify if the decay is of this kind
-               bool  m_betaminusdecay;
-               ///Specify if the decay is of this kind
-               bool  m_ecdecay;
+               ///Specify if the decay is the energy
+               double  m_alphadecay;
+               ///Specify if the decay is the energy
+               double  m_betaplusdecay;
+               ///Specify if the decay is the energy
+               double  m_betaminusdecay;
+               ///Specify if the decay is the energy
+               double  m_ecdecay;
                
-               double m_decayenergy;
                
                /**
                *spin and parity
index 474b48edf95a9bdefb8096f90e49bf49444c67b4..4482c40b108374cf1683ecfd953a803d7a941320 100644 (file)
@@ -28,8 +28,8 @@
 double Spectrum::minBand()
 {
        double value = ( *m_bandlist.begin() ).wavelength;
-       QValueList<band>::const_iterator it = m_bandlist.begin();
-       const QValueList<band>::const_iterator itEnd = m_bandlist.end();
+       QList<band>::const_iterator it = m_bandlist.begin();
+       const QList<band>::const_iterator itEnd = m_bandlist.end();
        for (;it!=itEnd;++it)
        {
                if ( value > ( *it ).wavelength )
@@ -41,8 +41,8 @@ double Spectrum::minBand()
 double Spectrum::maxBand()
 {
        double value = ( *m_bandlist.begin() ).wavelength;
-       QValueList<band>::const_iterator it = m_bandlist.begin();
-       const QValueList<band>::const_iterator itEnd = m_bandlist.end();
+       QList<band>::const_iterator it = m_bandlist.begin();
+       const QList<band>::const_iterator itEnd = m_bandlist.end();
        for (;it!=itEnd;++it)
        {
                if ( value < ( *it ).wavelength )
@@ -56,8 +56,8 @@ Spectrum* Spectrum::adjustToWavelength( double min, double max )
 {
        Spectrum *spec = new Spectrum();
 
-       QValueList<band>::const_iterator it = m_bandlist.begin();
-       const QValueList<band>::const_iterator itEnd = m_bandlist.end();
+       QList<band>::const_iterator it = m_bandlist.begin();
+       const QList<band>::const_iterator itEnd = m_bandlist.end();
 
        for ( ; it != itEnd; ++it )
        {
@@ -75,8 +75,8 @@ Spectrum* Spectrum::adjustToWavelength( double min, double max )
 void Spectrum::adjustIntensities()
 {
        int maxInt = 0;
-       QValueList<band>::Iterator it = m_bandlist.begin();
-       const QValueList<band>::Iterator itEnd = m_bandlist.end();
+       QList<band>::Iterator it = m_bandlist.begin();
+       const QList<band>::Iterator itEnd = m_bandlist.end();
 
        //find the highest intensity
        for ( ; it != itEnd; ++it )
@@ -101,12 +101,12 @@ void Spectrum::adjustIntensities()
        }
 }
 
-QValueList<double> Spectrum::wavelengths( double min, double max )
+QList<double> Spectrum::wavelengths( double min, double max )
 {
-       QValueList<double> list;
+       QList<double> list;
        
-       QValueList<band>::const_iterator it = m_bandlist.begin();
-       const QValueList<band>::const_iterator itEnd = m_bandlist.end();
+       QList<band>::const_iterator it = m_bandlist.begin();
+       const QList<band>::const_iterator itEnd = m_bandlist.end();
 
        for ( ; it != itEnd; ++it )
        {
@@ -125,8 +125,8 @@ QString Spectrum::bandsAsHtml()
 
        html += "<table>";
        
-       QValueList<band>::const_iterator it = m_bandlist.begin();
-       const QValueList<band>::const_iterator itEnd = m_bandlist.end();
+       QList<band>::const_iterator it = m_bandlist.begin();
+       const QList<band>::const_iterator itEnd = m_bandlist.end();
        for (;it!=itEnd;++it)
        {
                html += QString( "<tr>" )
index 32c58907f75282f0fd535df0969cc680d6a67090..ca31023ef0b285f0f17ec852e644ab563ecd2cf5 100644 (file)
@@ -22,7 +22,7 @@
  ***************************************************************************/
 
 #include <qstring.h>
-#include <qvaluelist.h>
+#include <qlist.h>
 
 class QPixmap;
 class Element;
@@ -103,9 +103,9 @@ class Spectrum
                 * @param min the lowest allowed wavalength in nanometer
                 * @param max the highest allowed wavalength in nanometer
                 * 
-                * @return the wavelength in a QValueList<double>
+                * @return the wavelength in a QList<double>
                 */
-               QValueList<double> wavelengths( double min, double max );
+               QList<double> wavelengths( double min, double max );
 
                /**
                 * @return the smallest wavelength
@@ -124,7 +124,7 @@ class Spectrum
                /**
                 * @return the list of bands of the spectrum
                 */
-               QValueList<band>* bandlist(){
+               QList<band>* bandlist(){
                        return &m_bandlist;
                }
        
@@ -162,7 +162,7 @@ class Spectrum
                /**
                 * the internal dataset
                 */
-               QValueList<band> m_bandlist;
+               QList<band> m_bandlist;
 
                /**
                 * the cached values of the highest and lowest wavelength
diff --git a/kalzium/src/tempunit.cpp b/kalzium/src/tempunit.cpp
new file mode 100644 (file)
index 0000000..e044f79
--- /dev/null
@@ -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 Steet, 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<double, double> TempUnit::rangeForUnit( TempUnit::Unit u )
+{
+       switch( u )
+       {
+               case Kelvin:
+               {
+                       return QPair<double, double>( 0.0, 5000.0 );
+                       break;
+               }
+               case Celsius:
+               {
+                       return QPair<double, double>( -273.16, 4726.84 );
+                       break;
+               }
+               case Fahrenheit:
+               {
+                       return QPair<double, double>( -459.67, 8540.33 );
+                       break;
+               }
+               case Rankine:
+               {
+                       return QPair<double, double>( 0.0, 9000.0 );
+                       break;
+               }
+               case Reaumur:
+               {
+                       return QPair<double, double>( -218.52, 3781.48 );
+                       break;
+               }
+       }
+       return QPair<double, double>( 0.0, 5000.0 );
+}
+
+QPair<double, double> TempUnit::rangeForUnit( int u )
+{
+       return rangeForUnit( (TempUnit::Unit)u );
+}
+
diff --git a/kalzium/src/tempunit.h b/kalzium/src/tempunit.h
new file mode 100644 (file)
index 0000000..f7de2c9
--- /dev/null
@@ -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 Steet, Fifth Floor, Boston, MA  02110-1301, USA.          *
+ ***************************************************************************/
+
+#include <qpair.h>
+#include <qstringlist.h>
+
+/**
+ * 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<double, double> rangeForUnit( TempUnit::Unit u );
+
+               /**
+                * @overload
+                */
+               static QPair<double, double> rangeForUnit( int u );
+
+};
+#endif // TEMPUNIT_H