| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: display.cpp 160 2006-12-11 20:15:57Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Copyright (C) 1992-2001 Trolltech AS. All rights reserved.
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of an example program for Qt. This example
|
|---|
| 7 | ** program may be used, distributed and modified without limitation.
|
|---|
| 8 | **
|
|---|
| 9 | *****************************************************************************/
|
|---|
| 10 |
|
|---|
| 11 | #include "display.h"
|
|---|
| 12 |
|
|---|
| 13 | #include <qpainter.h>
|
|---|
| 14 | #include <qlayout.h>
|
|---|
| 15 | #include <qtimer.h>
|
|---|
| 16 | #include <qpushbutton.h>
|
|---|
| 17 | #include <qframe.h>
|
|---|
| 18 | #include <qdial.h>
|
|---|
| 19 | #include <qlcdnumber.h>
|
|---|
| 20 | #include <qprogressbar.h>
|
|---|
| 21 | #include <qspinbox.h>
|
|---|
| 22 |
|
|---|
| 23 | #include <math.h>
|
|---|
| 24 |
|
|---|
| 25 | Screen::Screen( QWidget *parent, const char *name )
|
|---|
| 26 | : QFrame( parent, name )
|
|---|
| 27 | {
|
|---|
| 28 | setLineWidth( FrameWidth );
|
|---|
| 29 | setFrameStyle( Panel | Sunken );
|
|---|
| 30 | setBackgroundMode( PaletteBase );
|
|---|
| 31 | setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
|
|---|
| 32 | setPaletteBackgroundColor( black );
|
|---|
| 33 | setPaletteForegroundColor( blue );
|
|---|
| 34 |
|
|---|
| 35 | yval = new int[width()];
|
|---|
| 36 | memset( yval, 0, sizeof(int)*width() );
|
|---|
| 37 | pos0 = 0;
|
|---|
| 38 | t0 = 0;
|
|---|
| 39 | step = 0;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | Screen::~Screen()
|
|---|
| 43 | {
|
|---|
| 44 | delete yval;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | void Screen::resizeEvent( QResizeEvent *e )
|
|---|
| 48 | {
|
|---|
| 49 | delete yval;
|
|---|
| 50 | int w = e->size().width();
|
|---|
| 51 | yval = new int[w];
|
|---|
| 52 | memset( yval, 0, sizeof(int)*w);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | void Screen::animate()
|
|---|
| 56 | {
|
|---|
| 57 | if ( step == 0 )
|
|---|
| 58 | return;
|
|---|
| 59 |
|
|---|
| 60 | int t = t0;
|
|---|
| 61 | int p = pos0;
|
|---|
| 62 | if ( step < 0 ) {
|
|---|
| 63 | t += width() + step;
|
|---|
| 64 | } else {
|
|---|
| 65 | t -= step;
|
|---|
| 66 | p -= step;
|
|---|
| 67 | if ( p < 0 )
|
|---|
| 68 | p += width();
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | for ( int i = 0; i < QABS( step ); i++ ) {
|
|---|
| 72 | int y = (int)((height()-FrameWidth)/2 * sin( 3.1415*(double)t/180.0 ));
|
|---|
| 73 | yval[ p ] = y;
|
|---|
| 74 | ++t;
|
|---|
| 75 | t %= 360;
|
|---|
| 76 | ++p;
|
|---|
| 77 | p %= width();
|
|---|
| 78 | }
|
|---|
| 79 | t0 -= step;
|
|---|
| 80 | if ( t0 < 0 )
|
|---|
| 81 | t0 += 360;
|
|---|
| 82 | pos0 = (pos0 - step) % width();
|
|---|
| 83 | if ( pos0 < 0 )
|
|---|
| 84 | pos0 += width();
|
|---|
| 85 |
|
|---|
| 86 | scroll( step, 0, QRect( FrameWidth, FrameWidth, width()-2*FrameWidth, height()-2*FrameWidth ));
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | void Screen::setStep( int s )
|
|---|
| 90 | {
|
|---|
| 91 | step = s;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | void Screen::drawContents( QPainter *p )
|
|---|
| 95 | {
|
|---|
| 96 | QRect r = p->hasClipping() ?
|
|---|
| 97 | p->clipRegion().boundingRect() : contentsRect();
|
|---|
| 98 |
|
|---|
| 99 | int vp = ( r.left() - FrameWidth + pos0 ) % width();
|
|---|
| 100 | int y0 = FrameWidth + height()/2;
|
|---|
| 101 |
|
|---|
| 102 | for ( int x = r.left(); x <= r.right(); x++ ) {
|
|---|
| 103 | p->drawLine( x, y0 + yval[ vp ], x, r.bottom());
|
|---|
| 104 | ++vp;
|
|---|
| 105 | vp %= width();
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /***********************************************************************/
|
|---|
| 110 |
|
|---|
| 111 | Curve::Curve( QWidget *parent, const char *name )
|
|---|
| 112 | : QFrame( parent, name )
|
|---|
| 113 | {
|
|---|
| 114 | setLineWidth( FrameWidth );
|
|---|
| 115 | setFrameStyle( Panel | Sunken );
|
|---|
| 116 | setBackgroundMode( PaletteBase );
|
|---|
| 117 | setPaletteBackgroundColor(black);
|
|---|
| 118 | setPaletteForegroundColor(red);
|
|---|
| 119 | setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
|
|---|
| 120 |
|
|---|
| 121 | shift = 0;
|
|---|
| 122 | n = 1;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | void Curve::drawContents( QPainter *p )
|
|---|
| 126 | {
|
|---|
| 127 | p->moveTo( width()/2, height()/2 + (int)(90.0*sin( double(shift)*3.1415/180.0)));
|
|---|
| 128 |
|
|---|
| 129 | for ( double a = 0.0; a < 360.0; a += 1.0 ) {
|
|---|
| 130 | double rad = 3.1415 / 180.0 * a;
|
|---|
| 131 | double x = width()/2 + 90.0 * sin(rad);
|
|---|
| 132 | double y = height()/2 + 90.0 * sin(n * rad + double(shift)*3.1415/180.0);
|
|---|
| 133 | p->lineTo( int(x), int(y) );
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | void Curve::animate()
|
|---|
| 138 | {
|
|---|
| 139 | shift = (shift + 1) % 360;
|
|---|
| 140 | update( FrameWidth, FrameWidth, width() - 2*FrameWidth, height() - 2*FrameWidth );
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | void Curve::setFactor( int f )
|
|---|
| 144 | {
|
|---|
| 145 | n = f;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /***********************************************************************/
|
|---|
| 149 |
|
|---|
| 150 | DisplayWidget::DisplayWidget( QWidget *parent, const char *name )
|
|---|
| 151 | : QWidget( parent, name )
|
|---|
| 152 | {
|
|---|
| 153 | timer = 0;
|
|---|
| 154 |
|
|---|
| 155 | QVBoxLayout *vbox = new QVBoxLayout( this, 10 );
|
|---|
| 156 |
|
|---|
| 157 | QHBoxLayout *hbox = new QHBoxLayout( vbox );
|
|---|
| 158 | screen = new Screen( this );
|
|---|
| 159 | dial = new QDial( this );
|
|---|
| 160 | dial->setNotchesVisible( TRUE );
|
|---|
| 161 | dial->setRange( -10, 10 );
|
|---|
| 162 | dial->setValue( 1 );
|
|---|
| 163 | screen->setStep( dial->value() );
|
|---|
| 164 | connect( dial, SIGNAL( valueChanged( int )),
|
|---|
| 165 | screen, SLOT( setStep( int )));
|
|---|
| 166 | lcd = new QLCDNumber( 2, this );
|
|---|
| 167 | lcd->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
|
|---|
| 168 | lcdval = 0;
|
|---|
| 169 |
|
|---|
| 170 | hbox->addWidget( screen );
|
|---|
| 171 |
|
|---|
| 172 | QVBoxLayout *vb2 = new QVBoxLayout( hbox );
|
|---|
| 173 |
|
|---|
| 174 | curve = new Curve( this );
|
|---|
| 175 | spin = new QSpinBox( 1, 10, 1, this );
|
|---|
| 176 | connect( spin, SIGNAL( valueChanged( int )), curve, SLOT( setFactor( int )));
|
|---|
| 177 | spin->setValue( 2 );
|
|---|
| 178 | vb2->addWidget( curve );
|
|---|
| 179 | vb2->addWidget( spin );
|
|---|
| 180 |
|
|---|
| 181 | QHBoxLayout *hbox2 = new QHBoxLayout( vb2 );
|
|---|
| 182 |
|
|---|
| 183 | hbox2->addWidget( dial );
|
|---|
| 184 | hbox2->addWidget( lcd );
|
|---|
| 185 |
|
|---|
| 186 | bar = new QProgressBar( 10, this );
|
|---|
| 187 | tbar = 0;
|
|---|
| 188 |
|
|---|
| 189 | vbox->addWidget( bar );
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | void DisplayWidget::run()
|
|---|
| 193 | {
|
|---|
| 194 | if ( !timer ) {
|
|---|
| 195 | timer = new QTimer( this );
|
|---|
| 196 | connect( timer, SIGNAL( timeout() ), SLOT( tick() ) );
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | timer->start( 5 );
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | void DisplayWidget::stop()
|
|---|
| 203 | {
|
|---|
| 204 | timer->stop();
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | void DisplayWidget::tick()
|
|---|
| 208 | {
|
|---|
| 209 | // sine
|
|---|
| 210 | screen->animate();
|
|---|
| 211 | // Lissajous
|
|---|
| 212 | curve->animate();
|
|---|
| 213 | // lcd display
|
|---|
| 214 | lcd->display( ++lcdval % 100 );
|
|---|
| 215 | // progress bar
|
|---|
| 216 | bar->setProgress( 5 + (int)(5*sin( 3.1415 * (double)tbar / 180.0 )));
|
|---|
| 217 | ++tbar;
|
|---|
| 218 | tbar %= 360;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | void DisplayWidget::showEvent( QShowEvent * )
|
|---|
| 222 | {
|
|---|
| 223 | run();
|
|---|
| 224 | screen->repaint();
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | void DisplayWidget::hideEvent( QHideEvent * )
|
|---|
| 228 | {
|
|---|
| 229 | stop();
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|