Back to index

kdeartwork  4.3.2
polygon.cpp
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 // kpolygon - Basic screen saver for KDE
00004 //
00005 // Copyright (c)  Martin R. Jones 1996
00006 //
00007 // layout management added 1998/04/19 by Mario Weilguni <mweilguni@kde.org>
00008 // 2001/03/04 Converted to libkscreensaver by Martin R. Jones
00009 
00010 #include <stdlib.h>
00011 #include <time.h>
00012 #include <qcolor.h>
00013 #include <qlabel.h>
00014 #include <qslider.h>
00015 #include <qlayout.h>
00016 #include <QPolygon>
00017 #include <QVBoxLayout>
00018 #include <QHBoxLayout>
00019 #include <klocale.h>
00020 #include <kconfig.h>
00021 #include <kglobal.h>
00022 #include <kmessagebox.h>
00023 
00024 #include "polygon.h"
00025 #include <qpainter.h>
00026 
00027 #include "polygon.moc"
00028 
00029 
00030 #define MAXLENGTH    65
00031 #define MAXVERTICES  19
00032 
00033 // libkscreensaver interface
00034 class KPolygonSaverInterface : public KScreenSaverInterface
00035 {
00036 
00037 
00038 public:
00039     virtual KAboutData* aboutData() {
00040         return new KAboutData( "kpolygon.kss", "klock", ki18n( "KPolygon" ), "2.2.0", ki18n( "KPolygon" ) );
00041     }
00042 
00043 
00044     virtual KScreenSaver* create( WId id )
00045     {
00046         return new kPolygonSaver( id );
00047     }
00048 
00049     virtual QDialog* setup()
00050     {
00051         return new kPolygonSetup();
00052     }
00053 };
00054 
00055 int main( int argc, char *argv[] )
00056 {
00057     KPolygonSaverInterface kss;
00058     return kScreenSaverMain( argc, argv, kss );
00059 }
00060 
00061 //-----------------------------------------------------------------------------
00062 // dialog to setup screen saver parameters
00063 //
00064 kPolygonSetup::kPolygonSetup( QWidget *parent )
00065        : KDialog( parent)
00066          , saver( 0 ), length( 10 ), vertices( 3 ),
00067          speed( 50 )
00068 {
00069        setCaption(i18n( "Setup Polygon Screen Saver" ));
00070        setButtons(Ok|Cancel|Help);
00071        setDefaultButton(Ok);
00072        setModal(true);
00073        readSettings();
00074 
00075        QWidget *main = new QWidget(this);
00076        setMainWidget(main);
00077        setButtonText( Help, i18n( "A&bout" ) );
00078 
00079        QHBoxLayout *tl = new QHBoxLayout(main);
00080         tl->setSpacing(spacingHint());
00081        QVBoxLayout *tl1 = new QVBoxLayout;
00082        tl->addLayout(tl1);
00083 
00084        QLabel *label = new QLabel( i18n("Length:"), main );
00085        tl1->addWidget(label);
00086 
00087         QSlider *sb = new QSlider(Qt::Horizontal, main);
00088         sb->setMinimum(1);
00089         sb->setMaximum(MAXLENGTH);
00090         sb->setPageStep(10);
00091         sb->setValue(length);
00092         sb->setMinimumSize( 90, 20 );
00093         sb->setTickPosition(QSlider::TicksBelow);
00094         sb->setTickInterval(10);
00095         connect( sb, SIGNAL( valueChanged( int ) ), SLOT( slotLength( int ) ) );
00096         tl1->addWidget(sb);
00097 
00098        label = new QLabel( i18n("Vertices:"), main );
00099        tl1->addWidget(label);
00100 
00101         sb = new QSlider(Qt::Horizontal, main);
00102         sb->setMinimum(3);
00103         sb->setMaximum(MAXVERTICES);
00104         sb->setPageStep(2);
00105         sb->setValue(vertices);
00106         sb->setMinimumSize( 90, 20 );
00107         sb->setTickPosition(QSlider::TicksBelow);
00108         sb->setTickInterval(2);
00109         connect( sb, SIGNAL( valueChanged( int ) ), SLOT( slotVertices( int ) ) );
00110         tl1->addWidget(sb);
00111 
00112        label = new QLabel( i18n("Speed:"), main );
00113        tl1->addWidget(label);
00114 
00115         sb = new QSlider(Qt::Horizontal, main);
00116         sb->setMinimum(0);
00117         sb->setMaximum(100);
00118         sb->setPageStep(10);
00119         sb->setValue(speed);
00120         sb->setMinimumSize( 90, 20 );
00121         sb->setTickPosition(QSlider::TicksBelow);
00122         sb->setTickInterval(10);
00123         connect( sb, SIGNAL( valueChanged( int ) ), SLOT( slotSpeed( int ) ) );
00124         tl1->addWidget(sb);
00125         tl1->addStretch();
00126 
00127        preview = new QWidget( main );
00128        preview->setFixedSize( 220, 170 );
00129         {
00130             QPalette palette;
00131             palette.setColor( preview->backgroundRole(), Qt::black );
00132             preview->setPalette( palette );
00133            preview->setAutoFillBackground(true);
00134         }
00135        preview->show();    // otherwise saver does not get correct size
00136        saver = new kPolygonSaver( preview->winId() );
00137        tl->addWidget(preview);
00138         connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
00139        connect(this,SIGNAL(helpClicked()),this,SLOT(slotHelp()));
00140        setMinimumSize( sizeHint() );
00141 }
00142 
00143 kPolygonSetup::~kPolygonSetup()
00144 {
00145     delete saver;
00146 }
00147 
00148 // read settings from config file
00149 void kPolygonSetup::readSettings()
00150 {
00151     KConfigGroup config(KGlobal::config(), "Settings");
00152 
00153     length = config.readEntry( "Length", length );
00154     if ( length > MAXLENGTH )
00155         length = MAXLENGTH;
00156     else if ( length < 1 )
00157         length = 1;
00158 
00159     vertices = config.readEntry( "Vertices", vertices );
00160     if ( vertices > MAXVERTICES )
00161         vertices = MAXVERTICES;
00162     else if ( vertices < 3 )
00163         vertices = 3;
00164 
00165     speed = config.readEntry( "Speed", speed );
00166     if ( speed > 100 )
00167         speed = 100;
00168     else if ( speed < 50 )
00169         speed = 50;
00170 }
00171 
00172 void kPolygonSetup::slotLength( int len )
00173 {
00174        length = len;
00175        if ( saver )
00176               saver->setPolygon( length, vertices );
00177 }
00178 
00179 void kPolygonSetup::slotVertices( int num )
00180 {
00181        vertices = num;
00182        if ( saver )
00183               saver->setPolygon( length, vertices );
00184 }
00185 
00186 void kPolygonSetup::slotSpeed( int num )
00187 {
00188        speed = num;
00189        if ( saver )
00190               saver->setSpeed( speed );
00191 }
00192 
00193 // Ok pressed - save settings and exit
00194 void kPolygonSetup::slotOk()
00195 {
00196     KConfigGroup config(KGlobal::config(), "Settings");
00197 
00198     QString slength;
00199     slength.setNum( length );
00200     config.writeEntry( "Length", slength );
00201 
00202     QString svertices;
00203     svertices.setNum( vertices );
00204     config.writeEntry( "Vertices", svertices );
00205 
00206     QString sspeed;
00207     sspeed.setNum( speed );
00208     config.writeEntry( "Speed", sspeed );
00209 
00210     config.sync();
00211 
00212     accept();
00213 }
00214 
00215 void kPolygonSetup::slotHelp()
00216 {
00217        KMessageBox::information(this,
00218                           i18n("Polygon Version 2.2.0\n\n"\
00219                                           "Written by Martin R. Jones 1996\n"\
00220                                           "mjones@kde.org"));
00221 }
00222 
00223 //-----------------------------------------------------------------------------
00224 
00225 
00226 kPolygonSaver::kPolygonSaver( WId id ) : KScreenSaver( id )
00227 {
00228        readSettings();
00229 
00230        directions.resize( numVertices );
00231 
00232         {
00233             QPalette palette;
00234             palette.setColor( backgroundRole(), Qt::black );
00235             setPalette( palette );
00236            setAutoFillBackground(true);
00237         }
00238 
00239        initialiseColor();
00240        initialisePolygons();
00241 
00242        timer.start( speed );
00243        connect( &timer, SIGNAL( timeout() ), SLOT( update() ) );
00244         show();
00245 }
00246 
00247 kPolygonSaver::~kPolygonSaver()
00248 {
00249        timer.stop();
00250 }
00251 
00252 // set polygon properties
00253 void kPolygonSaver::setPolygon( int len, int ver )
00254 {
00255        timer.stop();
00256        numLines = len;
00257        numVertices = ver;
00258 
00259        directions.resize( numVertices );
00260        polygons.clear();
00261        initialisePolygons();
00262 
00263        timer.start( speed );
00264 }
00265 
00266 // set the speed
00267 void kPolygonSaver::setSpeed( int spd )
00268 {
00269        timer.stop();
00270        speed = 100-spd;
00271        timer.start( speed );
00272 }
00273 
00274 // read configuration settings from config file
00275 void kPolygonSaver::readSettings()
00276 {
00277     KConfigGroup config(KGlobal::config(), "Settings");
00278 
00279     numLines = config.readEntry( "Length", 10 );
00280     if ( numLines > 50 )
00281            numLines = 50;
00282     else if ( numLines < 1 )
00283            numLines = 1;
00284 
00285     numVertices = config.readEntry( "Vertices", 3 );
00286     if ( numVertices > 20 )
00287            numVertices = 20;
00288     else if ( numVertices < 3 )
00289            numVertices = 3;
00290 
00291     speed = 100 - config.readEntry( "Speed", 50 );
00292 }
00293 
00294 // draw next polygon and erase tail
00295 void kPolygonSaver::paintEvent(QPaintEvent *)
00296 {
00297     setAttribute( Qt::WA_NoSystemBackground ); // Only after initial clear
00298     QPainter p( this );
00299        if ( polygons.count() > numLines )
00300        {
00301               p.setPen( Qt::black );
00302                 p.drawPolyline( polygons.first() );
00303        }
00304 
00305        nextColor();
00306     p.setPen( colors[currentColor] );
00307     p.drawPolyline( polygons.last() );
00308 
00309        if ( polygons.count() > numLines )
00310               polygons.removeFirst();
00311 
00312        polygons.append( QPolygon( polygons.last() ) );
00313        moveVertices();
00314 }
00315 
00316 // initialise the polygon
00317 void kPolygonSaver::initialisePolygons()
00318 {
00319        int i;
00320 
00321        polygons.append( QPolygon( numVertices + 1 ) );
00322 
00323        QPolygon &poly = polygons.last();
00324 
00325        for ( i = 0; i < numVertices; i++ )
00326        {
00327               poly.setPoint( i, rnd.getLong(width()), rnd.getLong(height()) );
00328               directions[i].setX( 16 - rnd.getLong(8) * 4 );
00329               if ( directions[i].x() == 0 )
00330                      directions[i].setX( 1 );
00331               directions[i].setY( 16 - rnd.getLong(8) * 4 );
00332               if ( directions[i].y() == 0 )
00333                      directions[i].setY( 1 );
00334        }
00335 
00336        poly.setPoint( i, poly.point(0) );
00337 }
00338 
00339 // move polygon in current direction and change direction if a border is hit
00340 void kPolygonSaver::moveVertices()
00341 {
00342        int i;
00343        QPolygon &poly = polygons.last();
00344 
00345        for ( i = 0; i < numVertices; i++ )
00346        {
00347               poly.setPoint( i, poly.point(i) + directions[i] );
00348               if ( poly[i].x() >= (int)width() )
00349               {
00350                      directions[i].setX( -(rnd.getLong(4) + 1) * 4 );
00351                      poly[i].setX( (int)width() );
00352               }
00353               else if ( poly[i].x() < 0 )
00354               {
00355                      directions[i].setX( (rnd.getLong(4) + 1) * 4 );
00356                      poly[i].setX( 0 );
00357               }
00358 
00359               if ( poly[i].y() >= (int)height() )
00360               {
00361                      directions[i].setY( -(rnd.getLong(4) + 1) * 4 );
00362                      poly[i].setY( height() );
00363               }
00364               else if ( poly[i].y() < 0 )
00365               {
00366                      directions[i].setY( (rnd.getLong(4) + 1) * 4 );
00367                      poly[i].setY( 0 );
00368               }
00369        }
00370 
00371        poly.setPoint( i, poly.point(0) );
00372 }
00373 
00374 // create a color table of 64 colors
00375 void kPolygonSaver::initialiseColor()
00376 {
00377        for ( int i = 0; i < 64; i++ )
00378        {
00379               colors[i].setHsv( i * 360 / 64, 255, 255 );
00380        }
00381 
00382     currentColor = 0;
00383 }
00384 
00385 // set foreground color to next in the table
00386 void kPolygonSaver::nextColor()
00387 {
00388        currentColor++;
00389 
00390        if ( currentColor > 63 )
00391               currentColor = 0;
00392 }
00393