]> Git trees. - libqmvoc.git/commitdiff
painting the bands
authorMartin Pfeiffer <hubipete@gmx.net>
Sun, 17 Jul 2005 21:03:53 +0000 (21:03 +0000)
committerMartin Pfeiffer <hubipete@gmx.net>
Sun, 17 Jul 2005 21:03:53 +0000 (21:03 +0000)
svn path=/trunk/KDE/kdeedu/kalzium/src/spectrum.h; revision=435690

kalzium/src/spectrum.cpp
kalzium/src/spectrum.h

index 4340c550490f680a2bfc13f89719522452efcbf5..4818d67d1bf176d8d15bbc4ca51292f40573a7d8 100644 (file)
@@ -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<band>::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<double>::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<double>::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 );
index 8211cc84eae1f2ab6c92b157e56858d193868468..49750cb8c27938e2f8438648eea88d97df8c5da5 100644 (file)
@@ -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<band> 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:
                /**