/*************************************************************************** copyright : (C) 2005 by Carsten Niehaus email : cniehaus@kde.org ***************************************************************************/ /*************************************************************************** * * * 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. * * * ***************************************************************************/ #include "somwidget_impl.h" #include #include #include #include #include #include #include #include #include #include "element.h" #include "kalziumdataobject.h" #include "prefs.h" #include "tempunit.h" SOMWidgetIMPL::SOMWidgetIMPL( TQWidget *parent, const char* name ) : SOMWidget( parent,name ) { m_list = KalziumDataObject::instance()->ElementList; text->tqsetAlignment( text->tqalignment() | Qt::WordBreak ); text->setTextFormat( Qt::RichText ); text->setReadOnly( true ); text->setPaletteBackgroundColor( paletteBackgroundColor() ); text->setFrameStyle( TQFrame::NoFrame ); m_htmlBegin = ""; m_htmlEnd = ""; m_prevUnit = Prefs::temperature(); connect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( spinValueChanged( double ) ) ); connect( temp_slider, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( sliderValueChanged( int ) ) ); connect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( setNewTemp( double ) ) ); reloadUnits(); } void SOMWidgetIMPL::reloadUnits() { lblUnit->setText( TempUnit::unitListSymbol( Prefs::temperature() ) ); QPair range = TempUnit::rangeForUnit( Prefs::temperature() ); double newvalue = TempUnit::convert( Number1->value(), m_prevUnit, Prefs::temperature() ); Number1->setRange( range.first, range.second, 0.1, 1 ); Number1->setValue( newvalue ); setNewTemp( newvalue ); kdDebug() << "min: " << Number1->minValue() << " - max: " << Number1->maxValue() << endl; m_prevUnit = Prefs::temperature(); } void SOMWidgetIMPL::sliderValueChanged( int temp ) { // TODO check if in Qt4 the RangeControl emits the signal again disconnect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( spinValueChanged( double ) ) ); disconnect( temp_slider, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( sliderValueChanged( int ) ) ); disconnect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( setNewTemp( double ) ) ); double newvalue = TempUnit::convert( (double)temp, (int)TempUnit::Kelvin, Prefs::temperature() ); Number1->setValue( newvalue ); setNewTemp( newvalue ); connect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( spinValueChanged( double ) ) ); connect( temp_slider, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( sliderValueChanged( int ) ) ); connect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( setNewTemp( double ) ) ); } void SOMWidgetIMPL::spinValueChanged( double temp ) { disconnect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( spinValueChanged( double ) ) ); disconnect( temp_slider, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( sliderValueChanged( int ) ) ); disconnect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( setNewTemp( double ) ) ); int newvalue = (int)TempUnit::convert( temp, Prefs::temperature(), (int)TempUnit::Kelvin ); temp_slider->setValue( newvalue ); setNewTemp( temp ); connect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( spinValueChanged( double ) ) ); connect( temp_slider, TQT_SIGNAL( valueChanged( int ) ), this, TQT_SLOT( sliderValueChanged( int ) ) ); connect( Number1, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( setNewTemp( double ) ) ); } void SOMWidgetIMPL::setNewTemp( double newtemp ) { static const int threshold = 25; double temp = TempUnit::convert( newtemp, Prefs::temperature(), (int)TempUnit::Kelvin ); TQValueList::ConstIterator it = m_list.begin(); const TQValueList::ConstIterator itEnd = m_list.end(); TQStringList listMeltingPoint; TQStringList listBoilingPoint; TQStringList listBoilingPointValue; TQStringList listMeltingPointValue; for ( ; it != itEnd; ++it ) { if ( ( ( *it )->melting() > 0.0 ) && fabs( ( *it )->melting() - temp ) <= threshold ) { listMeltingPoint << ( *it )->elname(); listMeltingPointValue << ( *it )->adjustUnits( Element::MELTINGPOINT ); } if ( ( ( *it )->boiling() > 0.0 ) && fabs( ( *it )->boiling() - temp ) <= threshold ) { listBoilingPoint << ( *it )->elname(); listBoilingPointValue << ( *it )->adjustUnits( Element::BOILINGPOINT ); } } TQString htmlcode; if ( listMeltingPoint.count() > 0 ) { htmlcode += i18n( "Elements with melting point around this temperature:" ) + "
"; for ( uint i = 0; i < listMeltingPoint.count(); i++ ) { htmlcode += " · " + i18n( "For example: Carbon (300K)", "%1 (%2)" ).arg( listMeltingPoint[i] ).arg( listMeltingPointValue[i] ) + "
"; } htmlcode += "
"; } else { htmlcode += i18n( "No elements with a melting point around this temperature" ); htmlcode += "

"; } if ( listBoilingPoint.count() > 0 ) { htmlcode += i18n( "Elements with boiling point around this temperature:" ) + "
"; for ( uint i = 0; i < listBoilingPoint.count(); i++ ) { htmlcode += " · " + i18n( "For example: Carbon (300K)", "%1 (%2)" ).arg( listBoilingPoint[i] ).arg( listBoilingPointValue[i] ) + "
"; } htmlcode += "
"; } else { htmlcode += i18n( "No elements with a boiling point around this temperature" ); htmlcode += "
"; } // kdDebug() << m_htmlBegin + htmlcode + m_htmlEnd << endl; text->setText( m_htmlBegin + htmlcode + m_htmlEnd ); } #include "somwidget_impl.moc"