Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

graphwindow.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2002-2004 Michael 'Mickey' Lauer.  All rights reserved.
00003 **
00004 ** This file is part of Wellenreiter II.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 **********************************************************************/
00015 
00016 #include "graphwindow.h"
00017 
00018 #include <qpainter.h>
00019 #include <qpixmap.h>
00020 #include <qtimer.h>
00021 
00022 /* XPM */
00023 static char * background[] = {
00024 "16 16 6 1",
00025 "       c None",
00026 ".      c #52676E",
00027 "+      c #3F545B",
00028 "@      c #394E56",
00029 "#      c #2F454C",
00030 "$      c #364B52",
00031 ".+++++++++++++++",
00032 "@###############",
00033 "+$$$$$$$$$$$$$$$",
00034 "@###############",
00035 "+$$$$$$$$$$$$$$$",
00036 "@###############",
00037 "+$$$$$$$$$$$$$$$",
00038 "@###############",
00039 "+$$$$$$$$$$$$$$$",
00040 "@###############",
00041 "+$$$$$$$$$$$$$$$",
00042 "@###############",
00043 "+$$$$$$$$$$$$$$$",
00044 "@###############",
00045 "+$$$$$$$$$$$$$$$",
00046 "@###############"};
00047 
00048 
00049 MFrequencySpectrum::MFrequencySpectrum( int channels, QWidget* parent, const char* name, WFlags f)
00050                    :QWidget( parent, name,f ), _channels( channels )
00051 {
00052     _values = new int[_channels];
00053     _dirty = new bool[_channels];
00054     for ( int i = 0; i < channels; ++i )
00055     {   _values[i] = 0;
00056         _dirty[i] = true;
00057     }
00058 
00059     // we draw everything on our own
00060     setBackgroundMode( QWidget::NoBackground );
00061 }
00062 
00063 
00064 void MFrequencySpectrum::drawTopLine( QPainter* p, int x, int y, int width, const QColor& c )
00065 {
00066     p->setPen( c.light() );
00067     p->drawLine( x, y, x+width-1, y );
00068 }
00069 
00070 
00071 void MFrequencySpectrum::drawBottomLine( QPainter* p, int x, int y, int width, const QColor& c )
00072 {
00073     p->setPen( c.dark() );
00074     p->drawLine( x, y, x+width-1, y );
00075 }
00076 
00077 
00078 void MFrequencySpectrum::drawLine( QPainter* p, int x, int y, int width, const QColor& c )
00079 {
00080     p->setPen( c.light() );
00081     p->drawPoint( x++, y );
00082     p->setPen( c );
00083     p->drawLine( x, y, x+width-2, y );
00084     p->setPen( c.dark() );
00085     p->drawPoint( x+width-1, y );
00086 }
00087 
00088 
00089 void MFrequencySpectrum::drawBar( QPainter* p, int x, int y, int width, int height, int maxheight )
00090 {
00091     int h1 = 133; int h2 = 0;
00092     int s1 = 200; int s2 = 255;
00093     int v1 = 140; int v2 = 255;
00094 
00095     /*int h1 = 196; int h2 = 194;
00096     int s1 = 85; int s2 = 15;
00097     int v1 = 95; int v2 = 237;*/
00098 
00099     QColor c( 120, 60, 200 );
00100     for ( int i = 0; i < height; ++i )
00101     {
00102         int h = (h2-h1)*i/maxheight + h1;
00103         int s = (s2-s1)*i/maxheight + s1;
00104         int v = (v2-v1)*i/maxheight + v1;
00105         if ( i == 0 )
00106             drawBottomLine( p, x, y-i, width, QColor( h,s,v, QColor::Hsv ) );
00107         else if ( i == height-1 )
00108             drawTopLine( p, x, y-i, width, QColor( h,s,v, QColor::Hsv ) );
00109         else
00110             drawLine( p, x, y-i, width, QColor( h,s,v, QColor::Hsv ) );
00111     }
00112 }
00113 
00114 
00115 void MFrequencySpectrum::paintEvent( QPaintEvent*  )
00116 {
00117     QPixmap pm( size() );
00118     QPainter p;
00119     p.begin( &pm );
00120     p.drawTiledPixmap( 0, 0, size().width(), size().height(), QPixmap( (const char**) &background ) );
00121 
00122     int xmargin = 5;
00123     int ymargin = 2;
00124     int y = size().height() - 2 * ymargin;
00125     int x = 0;
00126     int width = ( size().width() - 2 * xmargin ) / _channels;
00127 
00128     for ( int i = 0; i < _channels; ++i )
00129     {
00130         if ( _dirty[i] )
00131         {
00132             drawBar( &p, xmargin + x, y - ymargin, width-3, _values[i]*y/100, y );
00133             _dirty[i] = false;
00134         }
00135         x+= width;
00136     }
00137 
00138     p.end();
00139     bitBlt( this, 0, 0, &pm );
00140 }
00141 
00142 
00143 void MFrequencySpectrum::mousePressEvent( QMouseEvent* e )
00144 {
00145     int xmargin = 5;
00146     int ymargin = 2;
00147     int y = size().height() - 2 * ymargin;
00148     int width = ( size().width() - 2 * xmargin ) / _channels;
00149 
00150     QPoint pos = e->pos();
00151     int channel = ( pos.x()-xmargin ) / width;
00152     int height = 100 - ( pos.y()-ymargin )*100/y;
00153     if ( channel < 15 ) _values[channel] = height;
00154 
00155 }
00156 
00157 
00158 Legende::Legende( int channels, QWidget* parent, const char* name, WFlags f )
00159         :QFrame( parent, name, f ), _channels( channels )
00160 {
00161     setLineWidth( 2 );
00162     setFrameStyle( Panel + Sunken );
00163     setFixedHeight( 16 );
00164 
00165 }
00166 
00167 
00168 void Legende::drawContents( QPainter* p )
00169 {
00170     int xmargin = 5;
00171     int width = ( contentsRect().width() - 2 * xmargin ) / _channels;
00172 
00173     for ( int i = 0; i < _channels; ++i )
00174         p->drawText( xmargin + (width*i), 12, QString().sprintf( "%02d", i+1 ) );
00175 }
00176 
00177 
00178 MGraphWindow::MGraphWindow( QWidget* parent, const char* name, WFlags f )
00179              :QVBox( parent, name, f )
00180 {
00181     spectrum = new MFrequencySpectrum( 14, this );
00182     legende = new Legende( 14, this );
00183     startTimer( 50 );
00184 };
00185 
00186 
00187 void MGraphWindow::testGraph()
00188 {
00189     static int i = 0;
00190     spectrum->setValue( i++, 100 );
00191     if ( i == 14 ) i = 0;
00192     QTimer::singleShot( 2000, this, SLOT( testGraph() ) );
00193 
00194 }
00195 
00196 
00197 void MGraphWindow::timerEvent( QTimerEvent*  )
00198 {
00199     for ( int i = 0; i < 14; i++ )
00200     {
00201         spectrum->decrease( i, 1 );
00202     }
00203     spectrum->repaint();
00204 }
00205 
00206 
00207 void MGraphWindow::traffic( int channel, int signal )
00208 {
00209     spectrum->setValue( channel-1, signal );
00210 }
00211 

Generated on Sat Nov 5 16:17:45 2005 for OPIE by  doxygen 1.4.2