summaryrefslogtreecommitdiffstats
path: root/kalzium/src/elementdataviewer.cpp
blob: 6fe8a3e3e9a587df3155ddee524d094f1d90a8da (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/***************************************************************************
    copyright            : (C) 2004, 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 "elementdataviewer.h"
#include "element.h"
#include "plotsetupwidget.h"
#include "plotwidget.h"
#include "kalziumdataobject.h"

#include <klocale.h>
#include <knuminput.h>
#include <kcombobox.h>
#include <kdebug.h>
#include <kactioncollection.h>
#include <kapplication.h>

//QT-Includes
#include <qlayout.h>
#include <qcheckbox.h>
#include <qlabel.h>

ElementDataViewer::ElementDataViewer( QWidget *parent, const char* name )
	: KDialogBase( KDialogBase::Plain, 
			i18n( "Plot Data") , 
			Help | User1 | Close,
			User1,
			parent, name )
{
	d = KalziumDataObject::instance();

	yData = new AxisData();
	
	QHBoxLayout *layout = new QHBoxLayout(plainPage(), 0, KDialog::spacingHint() );

	m_pPlotSetupWidget = new PlotSetupWidget( plainPage(), "plotsetup" );	
	m_pPlotSetupWidget->from->setMaxValue( d->numberOfElements() - 1 );
	m_pPlotSetupWidget->to->setMaxValue( d->numberOfElements() );
	m_pPlotWidget = new PlotWidget( 0.0, 12.0 ,0.0 ,22.0, plainPage(), "plotwidget" );
	m_pPlotWidget->setYAxisLabel(" ");
	m_pPlotWidget->setMinimumWidth( 200 );
	m_pPlotWidget->resize( 400, m_pPlotWidget->height() );

	layout->addWidget( m_pPlotSetupWidget );
	layout->addWidget( m_pPlotWidget );
	layout->setStretchFactor( m_pPlotSetupWidget, 0 );
	layout->setStretchFactor( m_pPlotWidget, 1 );

	// setup the list of names
	EList::iterator it = d->ElementList.begin();
	const EList::iterator itEnd = d->ElementList.end();
	for( ; it != itEnd ; ++it )
	{
		names.append( (*it)->elname() );
	}

	resize(500, 500);

	setButtonText( User1, i18n("&Plot") );

	m_actionCollection = new KActionCollection(this);
	KStdAction::quit(this, SLOT(slotClose()), m_actionCollection);	

	connect ( m_pPlotSetupWidget->KCB_y,         SIGNAL( activated(int) ),
			  this,                              SLOT( drawPlot()) );

	connect ( m_pPlotSetupWidget->connectPoints, SIGNAL( toggled(bool) ),
			  this,                              SLOT( drawPlot()) );
	connect ( m_pPlotSetupWidget->showNames,     SIGNAL( toggled(bool) ),
			  this,                              SLOT( drawPlot()) );

	// Draw the plot so that the user doesn't have to press the "Plot"
	// button to seee anything.
	drawPlot();
}

void ElementDataViewer::slotHelp()
{
	emit helpClicked();
	if ( kapp )
		kapp->invokeHelp ( "plot_data", "kalzium" );
}

// Reimplement slotUser1 from KDialogBase

void ElementDataViewer::slotUser1()
{
	kdDebug() << "slotUser1" << endl;

	drawPlot();
}


void ElementDataViewer::setLimits(int f, int t)
{
	kdDebug() << "setLimits()" << endl;
	
	double minY = yData->value(f);
	double maxY = yData->value(f);

	for ( int _currentVal = f; _currentVal <= t; _currentVal++ )
	{
		double v = yData->value( _currentVal );
		
		if( minY > v )
			minY = v;
		if( maxY < v)
			maxY = v;
	}
	
	// try to put a small padding to make the points on the axis visible
	double dx = ( t - f + 1 ) / 25 + 1.0;
	double dy = ( maxY - minY ) / 10.0;
	// in case that dy is quite small (for example, when plotting a single
	// point)
	if ( dy < 1e-7 )
		dy = maxY / 10.0;
	m_pPlotWidget->setLimits( f - dx, t + dx, minY - dy, maxY + dy );
}

void ElementDataViewer::paintEvent(QPaintEvent*)
{
	m_pPlotWidget->update();
}

void ElementDataViewer::keyPressEvent(QKeyEvent *e)
{
	switch ( e->key() )
	{
		case Key_Plus:
		case Key_Equal:
			slotZoomIn();
			break;
		case Key_Minus:
		case Key_Underscore:
			slotZoomOut();
			break;
		case Key_Escape:
			close();
			break;
	}
}

void ElementDataViewer::slotZoomIn(){}
void ElementDataViewer::slotZoomOut(){}

void ElementDataViewer::setupAxisData()
{
	DoubleList l;

	const int selectedData = m_pPlotSetupWidget->KCB_y->currentItem();

	//this should be somewhere else, eg in its own method
	yData->m_currentDataType = selectedData;

	EList::iterator it = d->ElementList.begin();
	const EList::iterator itEnd = d->ElementList.end();
	switch(selectedData)
	{
		case AxisData::MASS:
			for( ; it != itEnd ; ++it ) {
				double value = (*it)->mass();
				if( value > 0.0 )
				  l.append( value );
				else
				  l.append( 0.0 );
			}
			m_pPlotWidget->setYAxisLabel(i18n("Atomic Mass [u]"));
			break;
		case AxisData::MEANWEIGHT:
			for( ; it != itEnd ; ++it ) {
				double value =(*it)->meanmass();
				if( value > 0.0 )
				  l.append( value );
				else
				  l.append( 0.0 );
			}
			m_pPlotWidget->setYAxisLabel(i18n("Mean Mass [u]"));
			break;
		case AxisData::DENSITY:
			for( ; it != itEnd ; ++it ) {
				double value =(*it)->density();
				if( value > 0.0 )
				  l.append( value );
				else
				  l.append( 0.0 );
			}
			m_pPlotWidget->setYAxisLabel(i18n("Density"));
			break;
		case AxisData::EN:
			for( ; it != itEnd ; ++it ) {
				double value = (*it)->electroneg();
				if( value > 0.0 )
				  l.append( value );
				else
				  l.append( 0.0 );
			}
			m_pPlotWidget->setYAxisLabel(i18n("Electronegativity"));
			break;
		case AxisData::MELTINGPOINT:
			for( ; it != itEnd ; ++it ) {
				double value = (*it)->melting();
				if( value > 0.0 )
				  l.append( value );
				else
				  l.append( 0.0 );
			}
			m_pPlotWidget->setYAxisLabel(i18n("Melting Point [K]"));
			break;
		case AxisData::BOILINGPOINT:
			for( ; it != itEnd ; ++it ) {
				double value = (*it)->boiling();
				if( value > 0.0 )
				  l.append( value );
				else
				  l.append( 0.0 );
			}
			m_pPlotWidget->setYAxisLabel(i18n("Boiling Point [K]"));
			break;
		case AxisData::ATOMICRADIUS:
			for( ; it != itEnd ; ++it ) {
				double value = (*it)->radius( Element::ATOMIC );
				if( value > 0.0 )
				  l.append( value );
				else
				  l.append( 0.0 );
			}
			m_pPlotWidget->setYAxisLabel(i18n("Atomic Radius [pm]"));
			break;
		case AxisData::COVALENTRADIUS:
			for( ; it != itEnd ; ++it ) {
				double value = (*it)->radius( Element::COVALENT );
				if( value > 0.0 )
				  l.append( value );
				else
				  l.append( 0.0 );
			}
			m_pPlotWidget->setYAxisLabel(i18n("Covalent Radius [pm]"));
			break;
	}

	yData->setDataList( l );
}

void ElementDataViewer::drawPlot()
{
	kdDebug() << "drawPlot()" << endl;

	/*
	 * to be 100% safe delete the old list
	 */
	m_pPlotWidget->clearObjectList();

	/*
	 * spare the next step in case everything is already set and done
	 */
	if( yData->currentDataType() != m_pPlotSetupWidget->KCB_y->currentItem() )
		initData();

	/*
	 * if the user selected the elements 20 to 30 the list-values are 19 to 29!!!
	 */
	const int from = m_pPlotSetupWidget->from->value();
	const int to = m_pPlotSetupWidget->to->value();
	
	/*
	 * The number of elements. #20 to 30 are 30-20+1=11 Elements
	 */
	const int num = to-from+1;
	
	setLimits(from,to);

	/*
	 * check if the users wants to see the elementnames or not
	 */
	bool showNames = m_pPlotSetupWidget->showNames->isChecked();
	bool connectPoints = m_pPlotSetupWidget->connectPoints->isChecked();

	if ( connectPoints )
		m_pPlotWidget->setConnection( true );
	else
		m_pPlotWidget->setConnection( false );
		
	/*
	 * reserve the memory for the KPlotObjects
	 */
	//TODO QT4 replace QMemArray with QVector
	QMemArray<KPlotObject*> dataPoint(num);
	QMemArray<KPlotObject*> dataPointLabel(num);

	int number = 0;

	double max = 0.0, av = 0.0;
	double min = yData->value( 1 );

	/*
	 * iterate for example from element 20 to 30 and contruct
	 * the KPlotObjects
	 */
	for( int i = from; i < to+1 ; i++ )
	{
		double v = yData->value( i );

		if ( v >= 0.0 )
		{
			if ( v < min )
				min = v;
			if ( v > max )
				max = v;
			av += v;

			dataPoint[number] = new KPlotObject( "whocares", "Blue", KPlotObject::POINTS, 4, KPlotObject::CIRCLE );
			dataPoint[number]->addPoint( new DPoint( (double)i , v ) );
			m_pPlotWidget->addObject( dataPoint[ number ] );

			if (showNames)
			{
				dataPointLabel[number] = new KPlotObject( *(names.at(i-1)), "Red", KPlotObject::LABEL );
				dataPointLabel[number]->addPoint( new DPoint( (double)i , yData->value( i ) ) );
				m_pPlotWidget->addObject( dataPointLabel[number] );
			}
		}
		number++;
	}

	//now set the values for the min, max and avarage value
	m_pPlotSetupWidget->aValue->setText( QString::number( av/number ) );
	m_pPlotSetupWidget->minValue->setText( QString::number( min ) );
	m_pPlotSetupWidget->maxValue->setText( QString::number( max ) );
}

void ElementDataViewer::initData()
{
	setupAxisData();
}

///////////////////////////////////////////////

AxisData::AxisData()
{}

#include "elementdataviewer.moc"