Test change - can I push OK?
[kdeedu-porting.git] / kalzium / src / somwidget_impl.cpp
blobf725b55b94bdee9b3a1ded3f421c331ad5ef36fc
1 /***************************************************************************
2 copyright : (C) 2005, 2006 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 "somwidget_impl.h"
16 #include <QLabel>
17 #include <QPair>
18 #include <QSlider>
19 #include <QTextEdit>
20 #include <QList>
21 #include <QVariant>
23 #include <kdebug.h>
24 #include <knuminput.h>
25 #include <klocale.h>
27 #include <math.h>
29 #include <element.h>
30 #include <tempunit.h>
32 #include "kalziumdataobject.h"
33 #include "prefs.h"
35 SOMWidgetIMPL::SOMWidgetIMPL( QWidget *parent )
36 : QWidget( parent )
38 setupUi( this );
40 m_list = KalziumDataObject::instance()->ElementList;
42 m_htmlBegin = "";
43 m_htmlEnd = "";
44 m_prevUnit = Prefs::temperature();
46 connect( Number1, SIGNAL( valueChanged( int ) ),
47 this, SLOT( sliderValueChanged( int ) ) );
48 connect( temp_slider, SIGNAL( valueChanged( int ) ),
49 this, SLOT( sliderValueChanged( int ) ) );
51 reloadUnits();
54 int SOMWidgetIMPL::temperature() const
56 return temp_slider->value();
59 void SOMWidgetIMPL::reloadUnits()
61 Number1->blockSignals( true );
62 temp_slider->blockSignals( true );
63 lblUnit->setText( TempUnit::unitListSymbol( Prefs::temperature() ) );
64 QPair<double, double> range = TempUnit::rangeForUnit( Prefs::temperature() );
66 int newvalue = TempUnit::convert( Number1->value(), m_prevUnit, Prefs::temperature() );
67 Number1->setRange( range.first, range.second );
68 Number1->setSingleStep(1);
69 Number1->setValue( newvalue );
70 setNewTemp( newvalue );
71 m_prevUnit = Prefs::temperature();
72 Number1->blockSignals( false );
73 temp_slider->blockSignals( false );
76 void SOMWidgetIMPL::sliderValueChanged( int temp )
78 Number1->blockSignals( true );
79 temp_slider->blockSignals( true );
80 int newvalue = TempUnit::convert( (double)temp, (int)TempUnit::Kelvin, Prefs::temperature() );
81 Number1->setValue( newvalue );
82 temp_slider->setValue( newvalue );
83 setNewTemp( newvalue );
84 Number1->blockSignals( false );
85 temp_slider->blockSignals( false );
88 void SOMWidgetIMPL::setNewTemp( int newtemp )
90 static const int threshold = 25;
92 double temp = TempUnit::convert( newtemp, Prefs::temperature(), (int)TempUnit::Kelvin );
94 QStringList listMeltingPoint;
95 QStringList listBoilingPoint;
96 QStringList listBoilingPointValue;
97 QStringList listMeltingPointValue;
99 const QString unitSymbol = TempUnit::unitListSymbol( Prefs::temperature() );
101 foreach (Element * element, m_list)
103 double melting = element->dataAsVariant( ChemicalDataObject::meltingpoint ).toDouble();
104 if ( ( melting > 0.0 ) && fabs( melting - temp ) <= threshold )
106 listMeltingPoint << element->dataAsString( ChemicalDataObject::name );
107 listMeltingPointValue << QString::number(TempUnit::convert(melting,(int)TempUnit::Kelvin,Prefs::temperature()));
109 double boiling = element->dataAsVariant( ChemicalDataObject::boilingpoint ).toDouble();
110 if ( ( boiling > 0.0 ) && fabs( boiling - temp ) <= threshold )
112 listBoilingPoint << element->dataAsString( ChemicalDataObject::name );
113 listBoilingPointValue << QString::number(TempUnit::convert(boiling,(int)TempUnit::Kelvin,Prefs::temperature()));
116 QString htmlcode;
117 if ( listMeltingPoint.count() > 0 )
119 htmlcode += i18n( "Elements with melting point around this temperature:" ) + '\n';
120 for ( int i = 0; i < listMeltingPoint.count(); i++ )
122 htmlcode += " - " + i18nc( "For example: Carbon (300K)", "%1 (%2%3)",
123 listMeltingPoint.at( i ), listMeltingPointValue.at( i ), unitSymbol ) + '\n';
125 htmlcode += '\n';
127 else
129 htmlcode += i18n( "No elements with a melting point around this temperature" );
130 htmlcode += "\n\n";
132 if ( listBoilingPoint.count() > 0 )
134 htmlcode += i18n( "Elements with boiling point around this temperature:" ) + '\n';
135 for ( int i = 0; i < listBoilingPoint.count(); i++ )
137 htmlcode += " - " + i18nc( "For example: Carbon (300K)", "%1 (%2%3)",
138 listBoilingPoint.at( i ), listBoilingPointValue.at( i ), unitSymbol ) + '\n';
140 htmlcode += '\n';
142 else
144 htmlcode += i18n( "No elements with a boiling point around this temperature" );
145 htmlcode += '\n';
148 text->setText( m_htmlBegin + htmlcode + m_htmlEnd );
150 emit temperatureChanged( temperature() );
153 #include "somwidget_impl.moc"