Test change - can I push OK?
[kdeedu-porting.git] / kalzium / src / kalziumutils.cpp
blobd55eb18eccaed7cab58bb21d06980236485ca30c
1 /***************************************************************************
2 copyright : (C) 2005, 2006, 2007 by Carsten Niehaus
3 email : cniehaus@kde.org
4 ***************************************************************************/
5 /***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
14 #include "kalziumutils.h"
16 #include <QFont>
17 #include <QRect>
18 #include <QPainter>
20 #include <kdebug.h>
21 #include <klocale.h>
23 #include <tempunit.h>
24 #include <element.h>
26 #include "prefs.h"
28 #include <math.h>
29 #if defined(Q_OS_SOLARIS)
30 #include <ieeefp.h>
31 #endif
33 int KalziumUtils::maxSize( const QString& string, const QRect& rect, QFont font, QPainter* p, int minFontSize, int maxFontSize )
35 bool goodSizeFound = false;
36 int size = maxFontSize;
37 QRect r;
41 font.setPointSize( size );
42 p->setFont( font );
43 r = p->boundingRect( QRect(), Qt::AlignTop | Qt::AlignLeft, string );
44 r.translate( rect.left(), rect.top() );
46 if ( rect.contains( r ) )
47 goodSizeFound = true;
48 else
49 size--;
51 while ( !goodSizeFound && ( size > minFontSize ) );
53 return size;
56 int KalziumUtils::StringHeight( const QString& string, const QFont& font, QPainter* p )
58 Q_UNUSED( font );
59 return p->boundingRect( QRect(), Qt::AlignTop | Qt::AlignLeft, string ).height();
62 int KalziumUtils::StringWidth( const QString& string, const QFont& font, QPainter* p )
64 Q_UNUSED( font );
65 return p->boundingRect( QRect(), Qt::AlignTop | Qt::AlignLeft, string ).width();
68 double KalziumUtils::strippedValue( double num )
70 if ( !finite( num ) )
71 return num;
73 double power;
74 power = 1e-6;
75 while ( power < num )
76 power *= 10;
78 num = num / power * 10000;
79 num = qRound( num );
81 return num * power / 10000;
84 QString KalziumUtils::prettyUnit( const Element* el, ChemicalDataObject::BlueObelisk kind )
86 if (!el) return "error";
88 QString result;
89 double val = 0.0; //the value to convert
91 switch ( kind )
93 //FIXME I just copied the code from "boilingpoint", no clue
94 //if that really works
95 case ChemicalDataObject::meltingpoint: // a temperature
97 val = el->dataAsVariant( kind ).toDouble();
98 if ( val <= 0.0 )
99 result = i18n( "Unknown Value" );
100 else
102 val = TempUnit::convert( val, (int)TempUnit::Kelvin, Prefs::temperature() );
103 result = i18nc( "%1 is the temperature, %2 is the unit, like \"300 K\"", "%1 %2", val, TempUnit::unitListSymbol( Prefs::temperature() ) );
105 break;
107 case ChemicalDataObject::boilingpoint:
109 val = el->dataAsVariant( kind ).toDouble();
110 if ( val <= 0.0 )
111 result = i18n( "Unknown Value" );
112 else
114 val = TempUnit::convert( val, (int)TempUnit::Kelvin, Prefs::temperature() );
115 result = i18nc( "%1 is the temperature, %2 is the unit, like \"300 K\"", "%1 %2", val, TempUnit::unitListSymbol( Prefs::temperature() ) );
117 break;
119 case ChemicalDataObject::electronegativityPauling: // electronegativity
121 val = el->dataAsVariant( kind ).toDouble();
122 if ( val <= 0.0 )
123 result = i18n( "Value not defined" );
124 else
125 result = i18nc("Just a number", "%1", val );
126 break;
128 case ChemicalDataObject::electronAffinity: // an energy
130 val = el->dataAsVariant( kind ).toDouble();
131 result = i18nc( "electron volt", "%1 eV", val );
132 break;
134 case ChemicalDataObject::ionization:
136 val = el->dataAsVariant( kind ).toDouble();
137 if ( val <= 0.0 )
138 result = i18n( "Value not defined" );
139 else
141 switch ( Prefs::energies() )
143 case 0:
145 result = i18nc( "kilo joule per mol", "%1 kJ/mol", val );
146 break;
148 case 1: // use electronvolt
150 val /= 96.6;
151 result = i18nc( "electron volt", "%1 eV", val );
152 break;
156 break;
158 case ChemicalDataObject::mass: // a mass
160 val = el->dataAsVariant( kind ).toDouble();
161 if ( val <= 0.0 )
162 result = i18n( "Unknown Value" );
163 else
164 result = i18nc( "x u (units). The atomic mass.", "%1 u", val );
165 break;
167 case ChemicalDataObject::date: // a date
169 val = el->dataAsVariant( kind ).toInt();
170 if ( val > 1600 ) {
171 result = i18n( "This element was discovered in the year <numid>%1</numid>.", val );
172 }else if( val == -1 ){
173 result = i18n( "The element has not yet been offically recoqnized by the IUPAC." );
174 } //this should now really be 0. If not there is a bug in the database
175 else {
176 result = i18n( "This element was known to ancient cultures." );
178 break;
180 case ChemicalDataObject::radiusCovalent: // a length
182 val = el->dataAsVariant( kind ).toDouble() * 100;
183 if ( val <= 0.0 )
184 result = i18n( "Unknown Value" );
185 else
186 result = i18nc( "%1 is a length, eg: 12.3 pm", "%1 pm", val );
187 break;
189 case ChemicalDataObject::radiusVDW:
191 val = el->dataAsVariant( kind ).toDouble() * 100;
192 if ( val <= 0.0 )
193 result = i18n( "Unknown Value" );
194 else
195 result = i18nc( "%1 is a length, eg: 12.3 pm", "%1 pm", val );
196 break;
198 default:
200 result = el->dataAsVariant( kind ).toString();
204 return result;