From: Martin Pfeiffer Date: Sun, 17 Jul 2005 21:03:53 +0000 (+0000) Subject: painting the bands X-Git-Tag: v3.80.2~300^2~56 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=6d2aaabd863e8f9d06b56391daefd33948a17e01;p=libqmvoc.git painting the bands svn path=/trunk/KDE/kdeedu/kalzium/src/spectrum.h; revision=435690 --- diff --git a/kalzium/src/spectrum.cpp b/kalzium/src/spectrum.cpp index 4340c55..4818d67 100644 --- a/kalzium/src/spectrum.cpp +++ b/kalzium/src/spectrum.cpp @@ -54,15 +54,68 @@ double Spectrum::maxBand() return value; } +void Spectrum::paintBands( QPainter* p, double startValue, double endValue, bool emissionSpectrum ) +{ + kdDebug() << "Spectrum::paintBands" << endl; + + if ( !emissionSpectrum ) + { + for ( double va = startValue; va <= endValue ; va += 0.1 ) + { + int x = xPos( va, startValue, endValue ); + p->setPen( linecolor( va ) ); + p->drawLine( x,0,x, m_realHeight+10 ); + } + + p->setPen( Qt::black ); + } + + int i = 0; + + for ( QValueList::Iterator it = m_bandlist.begin(); + it != m_bandlist.end(); + ++it ) + { + if ( ( *it ).wavelength < startValue || ( *it ).wavelength > endValue ) + continue; + + int x = xPos( ( *it ).wavelength, startValue, endValue ); + + int temp = 0; // every second item will have a little offset + + if ( i%2 ) + temp = 35; + else + temp = 0; + + if ( emissionSpectrum ) + { + p->setPen( linecolor( ( *it ).wavelength ) ); + + p->drawLine( x,0,x, m_realHeight ); + + p->setPen( Qt::black ); + p->drawLine( x,m_realHeight,x, m_realHeight+10+temp ); + } + else + { + p->drawLine( x,0,x, m_realHeight+10+temp ); + } + + QString text = QString::number( ( *it ).wavelength ); + p->drawText(0, 0, text); + + i++; + } +} + SpectrumWidget::SpectrumWidget( QWidget* parent, const char* name ) : QWidget( parent,name ) { - //For the colorcalculation - Gamma = 0.8; - IntensityMax = 255, - + kdDebug() << "SpectrumWidget::SpectrumWidget()" << endl; + m_realWidth = 360; - m_realHeight = 200; + m_realHeight = 200; setType( EmissionSpectrum ); } @@ -74,6 +127,8 @@ void SpectrumWidget::paintEvent( QPaintEvent * /*e*/ ) QPainter p; p.begin( this ); p.fillRect( 0, 0, width(), m_realHeight, Qt::black ); + + m_spectrum->setWidth ( width() ); switch ( m_type ) { @@ -91,70 +146,14 @@ void SpectrumWidget::drawAbsorptionSpectrum( QPainter *p ) { 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++; - } + m_spectrum->paintBands ( p, startValue, endValue, false ); } void SpectrumWidget::drawEmmissionSpectrum( QPainter *p ) { kdDebug() << "SpectrumWidget::drawEmmissionSpectrum()" << endl; - int i = 0; - - 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(linecolor( *it )); - p->drawLine( x,0,x, m_realHeight ); - //p->drawRect( x,0,x+width()/(endValue-startValue),m_realHeight ); - p->setPen( Qt::black ); - p->drawLine( x,m_realHeight,x, m_realHeight+10+temp ); - p->setPen( Qt::black ); - QString text = QString::number( *it ); - p->drawText(0, 0, text); - - i++; - } + m_spectrum->paintBands ( p, startValue, endValue, true ); //To test the widget uncomment this code. //X for ( double va = startValue; va <= endValue ; va += 0.7 ) @@ -165,6 +164,15 @@ void SpectrumWidget::drawEmmissionSpectrum( QPainter *p ) //X } } +QColor Spectrum::linecolor( double spectrum ) +{ + int r,g,b; + wavelengthToRGB( spectrum, r,g,b ); + + QColor c( r,g,b ); + return c; +} + void SpectrumWidget::drawTickmarks( QPainter* p ) { const int space = 13; @@ -208,7 +216,7 @@ void SpectrumWidget::drawTickmarks( QPainter* p ) } } -void SpectrumWidget::wavelengthToRGB( double wavelength, int& r, int& g, int& b ) +void Spectrum::wavelengthToRGB( double wavelength, int& r, int& g, int& b ) { double blue = 0.0, green = 0.0, red = 0.0, factor = 0.0; @@ -265,7 +273,7 @@ void SpectrumWidget::wavelengthToRGB( double wavelength, int& r, int& g, int& b b = Adjust( blue, factor ); } -int SpectrumWidget::Adjust( double color, double factor ) +int Spectrum::Adjust( double color, double factor ) { if ( color == 0.0 ) return 0; @@ -273,18 +281,11 @@ int SpectrumWidget::Adjust( double color, double factor ) return ( int )( round( IntensityMax * pow( color*factor, Gamma ) ) ); } -QColor SpectrumWidget::linecolor( double spectrum ) -{ - int r,g,b; - wavelengthToRGB( spectrum, r,g,b ); - - QColor c( r,g,b ); - return c; -} - SpectrumView::SpectrumView( Spectrum *spec, QWidget *parent, const char* name ) : QWidget( parent, name ) { + kdDebug()<<"SpectrumView::SpectrumView()" << endl; + m_spectrum = spec; QVBoxLayout *spectrumLayout = new QVBoxLayout( this ); diff --git a/kalzium/src/spectrum.h b/kalzium/src/spectrum.h index 8211cc8..49750cb 100644 --- a/kalzium/src/spectrum.h +++ b/kalzium/src/spectrum.h @@ -43,8 +43,17 @@ class Spectrum { public: Spectrum(){ + kdDebug() <<"Spectrum::Spectrum" << endl; + m_min = minBand(); m_max = maxBand(); + + //For the colorcalculation + Gamma = 0.8; + IntensityMax = 255, + + m_realWidth = 360; + m_realHeight = 200; }; ~Spectrum(){}; @@ -78,6 +87,19 @@ class Spectrum return m_max; } + void setWidth( int width ){ + m_width = width; + } + + void paintBands( QPainter* p, double startValue, double endValue, bool emissionSpectrum ); + + /** + * @returns the color of a line + * @param spectrum the value of the spectrum + */ + QColor linecolor( double spectrum ); + + private: /** * @return the smallest wavelength @@ -89,9 +111,44 @@ class Spectrum */ double maxBand(); + /** + * @return the postion in the widget of a band + * with the wavelength @p wavelength + * + * @param wavelength the wavelength for which the position is needed + */ + inline int xPos( double wavelength, double startValue, double endValue ){ + return ( int ) ( m_width * ( wavelength - startValue ) / ( endValue - startValue ) ); + } + + /** + * This method changes the three values @p r @p g and @p b to the + * correct values + * param wavelength the wavelength for which the color is searched + * param r red + * param g green + * param b blue + */ + void wavelengthToRGB( double wavelength, int& r, int& g, int& b ); + + double Gamma; + int IntensityMax; + + int m_realWidth; + int m_realHeight; + + /** + * @return the adjusted value of the @p color. The + * correction depends on @p factor which has been + * figured out emperically + */ + int Adjust( double color, double factor ); + QValueList m_bandlist; double m_max, m_min; + + int m_width; }; /** @@ -158,40 +215,8 @@ class SpectrumWidget : public QWidget SpectrumType m_type; - /** - * @return the adjusted value of the @p color. The - * correction depends on @p factor which has been - * figured out emperically - */ - int Adjust( double color, double factor ); - Spectrum *m_spectrum; - /** - * @param the position on a 0 to 1-scale in the widget. 0.5 mean - * that you want the wavelength in the middle of the widget, - * 0.25 mean at one quarter of the width of the widget - * - * @param position the position on a 0 to 1 scale. - * @return the Wavelength on @p position - */ - inline double Wavelength( double position ){ - return startValue + ( ( endValue-startValue ) * position ); - } - - /** - * This method changes the three values @p r @p g and @p b to the - * correct values - * param wavelength the wavelength for which the color is searched - * param r red - * param g green - * param b blue - */ - void wavelengthToRGB( double wavelength, int& r, int& g, int& b ); - - double Gamma; - int IntensityMax; - /** * draws the spectra-lines */ @@ -207,27 +232,11 @@ class SpectrumWidget : public QWidget */ void drawTickmarks( QPainter *p ); - /** - * @return the postion in the widget of a band - * with the wavelength @p wavelength - * - * @param wavelength the wavelength for which the position is needed - */ - inline int xPos( double wavelength ){ - return ( int ) ( width() * ( wavelength - startValue ) / ( endValue - startValue ) ); - } - - /** - * @returns the color of a line - * @param spectrum the value of the spectrum - */ - QColor linecolor( double spectrum ); - double startValue; double endValue; - int m_realWidth; int m_realHeight; + int m_realWidth; public slots: /**