summaryrefslogtreecommitdiffstats
path: root/kalzium/src/somwidget_impl.cpp
blob: c0b8ef6a3e91e1b68b7554bc01513ec0db503b39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/***************************************************************************
    copyright            : (C) 2005 by Carsten Niehaus
    email                : [email protected]
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   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 <tqlabel.h>
#include <tqpair.h>
#include <tqslider.h>
#include <tqtextedit.h>
#include <tqvaluelist.h>

#include <kdebug.h>
#include <knuminput.h>
#include <klocale.h>

#include <math.h>

#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->setAlignment( text->alignment() | TQt::WordBreak );
	text->setTextFormat( TQt::RichText );
	text->setReadOnly( true );
	text->setPaletteBackgroundColor( paletteBackgroundColor() );
	text->setFrameStyle( TQFrame::NoFrame );

	m_htmlBegin = "<qt>";
	m_htmlEnd = "</qt>";
	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() ) );
	TQPair<double, double> 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 TQt4 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<Element*>::ConstIterator it = m_list.begin();
	const TQValueList<Element*>::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:" ) + "<br>";
		for ( uint i = 0; i < listMeltingPoint.count(); i++ )
		{
			htmlcode += "&nbsp;<b>&middot;</b>&nbsp;" + i18n( "For example: Carbon (300K)", "%1 (%2)" ).arg( listMeltingPoint[i] ).arg( listMeltingPointValue[i] ) + "<br>";
		}
		htmlcode += "<br>";
	}
	else
	{
		htmlcode += i18n( "No elements with a melting point around this temperature" );
		htmlcode += "<br><br>";
	}
	if ( listBoilingPoint.count() > 0 )
	{
		htmlcode += i18n( "Elements with boiling point around this temperature:" ) + "<br>";
		for ( uint i = 0; i < listBoilingPoint.count(); i++ )
		{
			htmlcode += "&nbsp;<b>&middot;</b>&nbsp;" + i18n( "For example: Carbon (300K)", "%1 (%2)" ).arg( listBoilingPoint[i] ).arg( listBoilingPointValue[i] ) + "<br>";
		}
		htmlcode += "<br>";
	}
	else
	{
		htmlcode += i18n( "No elements with a boiling point around this temperature" );
		htmlcode += "<br>";
	}

//	kdDebug() << m_htmlBegin + htmlcode + m_htmlEnd << endl;

	text->setText( m_htmlBegin + htmlcode + m_htmlEnd );

}

#include "somwidget_impl.moc"