From 025a035402f226148b40f2410e7869aecfaa53bb Mon Sep 17 00:00:00 2001 From: Carsten Niehaus Date: Thu, 14 Jul 2005 10:44:14 +0000 Subject: [PATCH] * add new icon, provided by Lee Olson * make more and more things work in the spectrum svn path=/trunk/KDE/kdeedu/kalzium/src/spectrum.h; revision=434491 --- kalzium/src/spectrum.cpp | 73 +++++++++++++++++++++++++++++++++------- kalzium/src/spectrum.h | 39 +++++++++++++++++++-- 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/kalzium/src/spectrum.cpp b/kalzium/src/spectrum.cpp index d040d3d..deaf56a 100644 --- a/kalzium/src/spectrum.cpp +++ b/kalzium/src/spectrum.cpp @@ -45,6 +45,8 @@ SpectrumWidget::SpectrumWidget( QWidget* parent, const char* name ) m_realHeight = 200; m_stretch = 1; + + setType( EmissionSpectrum ); } SpectrumWidget::~SpectrumWidget(){} @@ -54,20 +56,61 @@ void SpectrumWidget::paintEvent( QPaintEvent * /*e*/ ) QPainter p; p.begin( this ); p.fillRect( 0, 0, width(), m_realHeight, Qt::black ); - drawLines( &p ); + + switch ( m_type ) + { + case EmissionSpectrum: + drawEmmissionSpectrum( &p ); + break; + case AbsorptionSpectrum: + drawAbsorptionSpectrum( &p ); + break; + } drawTickmarks( &p ); } -void SpectrumWidget::drawLines( QPainter *p ) +void SpectrumWidget::drawAbsorptionSpectrum( QPainter *p ) { - //the spectrum goes from about 780nm to about 400nm - //700 is a dark red - //660 yellow - //580 green - //500 light blue - //400 dark blue + kdDebug() << "SpectrumWidget::drawAbsorptionSpectrum()" << endl; int i = 0; + + for ( double va = startValue; va <= endValue ; va += 0.1 ) + { + int x = xPos( va ); + p->setPen(linecolor( va )); + p->drawLine( x,0,x, m_realHeight+10 ); + } + + for ( QValueList::Iterator it = m_spectra.begin(); + it != m_spectra.end(); + ++it ) + { + if ( (*it) < startValue || ( *it ) > endValue ) + continue; + + int x = xPos( *it ); + + int temp = 0; // every second item will have a little offset + if ( i%2 ) + temp = 35; + else + temp = 0; + + p->setPen( Qt::black ); + p->drawLine( x,0,x, m_realHeight+10+temp ); + QString text = QString::number( *it ); + p->drawText(0, 0, text); + + i++; + } +} + +void SpectrumWidget::drawEmmissionSpectrum( QPainter *p ) +{ + kdDebug() << "SpectrumWidget::drawEmmissionSpectrum()" << endl; + int i = 0; + for ( QValueList::Iterator it = m_spectra.begin(); it != m_spectra.end(); ++it ) @@ -129,8 +172,6 @@ void SpectrumWidget::wavelengthToRGB( double wavelength, int& r, int& g, int& b int wavelength_ = floor( wavelength ); -// kdDebug() << wavelength << " :: " << wavelength_ << endl; - if ( wavelength_ > 380 && wavelength_ < 439 ) { red = -( wavelength-440 ) / ( 440-380 ); @@ -192,8 +233,9 @@ int SpectrumWidget::Adjust( double color, double factor ) int SpectrumWidget::xPos( double value ) { - int proportion = width() * ( value - startValue ) / ( endValue - startValue ); - return proportion; + return ( int ) width() * ( value - startValue ) / ( endValue - startValue ); +//X int proportion = ( int ) width() * ( value - startValue ) / ( endValue - startValue ); +//X return proportion; } int SpectrumWidget::Wavelength( double position ) @@ -228,11 +270,18 @@ SpectrumView::SpectrumView( QWidget *parent, const char* name ) connect( m_spinbox_right, SIGNAL( valueChanged( int ) ), m_spectrum, SLOT( setRightBorder( int ) ) ); connect( m_spinbox_left, SIGNAL( valueChanged( int ) ), m_spectrum, SLOT( setLeftBorder( int ) ) ); + + m_spectrumbox = new KComboBox( this, "combobox" ); + m_spectrumbox->insertItem( "EmissionSpectrum" ); + m_spectrumbox->insertItem( "AbsorptionSpectrum" ); + connect( m_spectrumbox, SIGNAL( activated( int ) ), m_spectrum, SLOT( slotActivateSpectrum( int ) ) ); + hbox->addWidget( new QLabel( i18n( "Minimumvalue" ), this ) ); hbox->addWidget( m_spinbox_left ); hbox->addWidget( new QLabel( i18n( "Maximumvalue" ), this ) ); hbox->addWidget( m_spinbox_right ); + hbox->addWidget( m_spectrumbox ); spectrumLayout->addLayout( hbox ); } diff --git a/kalzium/src/spectrum.h b/kalzium/src/spectrum.h index c021b3d..715ca0a 100644 --- a/kalzium/src/spectrum.h +++ b/kalzium/src/spectrum.h @@ -31,6 +31,7 @@ #include #include #include +#include #define MAXCOLOR = 750 #define MINCOLOR = 450 @@ -66,10 +67,29 @@ class SpectrumWidget : public QWidget * of pixel the next band is away */ int findNearestBand( QValueList::iterator it ); + + /** + * there are several possible types. + */ + enum SpectrumType + { + EmissionSpectrum = 0, + AbsorptionSpectrum + }; + + /** + * sets the type of the spectrum to @p t + * @param t the type of the spectrum + */ + void setType( SpectrumType t ){ + m_type = t; + } private: QValueList m_spectra; + SpectrumType m_type; + /** * @return the adjusted value of the @p color. The * correction depends on @p factor which has been @@ -103,11 +123,19 @@ class SpectrumWidget : public QWidget /** * draws the spectra-lines */ - void drawLines( QPainter *p ); + void drawAbsorptionSpectrum( QPainter *p ); + + /** + * draws the spectra-lines + */ + void drawEmmissionSpectrum( QPainter *p ); + /** + * Draw the scale + */ void drawTickmarks( QPainter *p ); - int xPos( double value ); + inline int xPos( double value ); /** * @returns the color of a line @@ -143,6 +171,11 @@ class SpectrumWidget : public QWidget endValue = startValue+1; update(); } + + void slotActivateSpectrum( int spectrumtype ){ + m_type = ( SpectrumType )spectrumtype; + update(); + } protected: virtual void paintEvent( QPaintEvent *e ); @@ -164,6 +197,8 @@ class SpectrumView : public QWidget SpectrumWidget *m_spectrum; QSpinBox *m_spinbox_left, *m_spinbox_right; + + KComboBox *m_spectrumbox; }; #endif // SPECTRUM_H -- 2.47.3