Back to index

kdeartwork  4.3.2
lines.cpp
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 // klines 0.1.1 - Basic screen saver for KDE
00004 // by Dirk Staneker 1997
00005 // based on kpolygon from Martin R. Jones 1996
00006 // mailto:dirk.staneker@student.uni-tuebingen.de
00007 //
00008 // layout management added 1998/04/19 by Mario Weilguni <mweilguni@kde.org>
00009 // 2001/03/04 Converted to libkscreensaver by Martin R. Jones
00010 
00011 #include <stdlib.h>
00012 #include <time.h>
00013 #include <qcolor.h>
00014 #include <qlabel.h>
00015 #include <qslider.h>
00016 #include <QVBoxLayout>
00017 #include <QHBoxLayout>
00018 #include <kconfig.h>
00019 #include <kapplication.h>
00020 #include <kmessagebox.h>
00021 #include <kcolorbutton.h>
00022 
00023 #include "kcolordialog.h"
00024 #include "lines.h"
00025 #include "lines.moc"
00026 
00027 #include <qlayout.h>
00028 #include <klocale.h>
00029 #include <kglobal.h>
00030 #include <qpainter.h>
00031 
00032 #define MAXLENGTH    256
00033 
00034 // libkscreensaver interface
00035 class kLinesSaverInterface : public KScreenSaverInterface
00036 {
00037 
00038 
00039 public:
00040     virtual KAboutData* aboutData() {
00041         return new KAboutData( "klines.kss", "klock", ki18n( "KLines" ), "2.2.0", ki18n( "KLines" ) );
00042     }
00043 
00044 
00045     virtual KScreenSaver* create( WId id )
00046     {
00047         return new kLinesSaver( id );
00048     }
00049 
00050     virtual QDialog* setup()
00051     {
00052         return new kLinesSetup();
00053     }
00054 };
00055 
00056 int main( int argc, char *argv[] )
00057 {
00058     kLinesSaverInterface kss;
00059     return kScreenSaverMain( argc, argv, kss );
00060 }
00061 
00062 // Methods of the Lines-class
00063 Lines::Lines(int x){
00064        uint i;
00065        numLn=x;
00066        offx1=12;
00067        offy1=16;
00068        offx2=9;
00069        offy2=10;
00070        start=new Ln;
00071        end=start;
00072        for(i=1; i<numLn; i++){
00073               end->next=new Ln;
00074               end=end->next;
00075        }
00076        end->next=start;
00077        akt=start;
00078 }
00079 
00080 Lines::~Lines(){
00081        uint i;
00082        for(i=0; i<numLn; i++){
00083               end=start->next;
00084               delete start;
00085               start=end;
00086        }
00087 }
00088 
00089 inline void Lines::reset(){ akt=start;    }
00090 
00091 inline void Lines::getKoord(int& a, int& b, int& c, int& d){
00092        a=akt->x1; b=akt->y1;
00093        c=akt->x2; d=akt->y2;
00094        akt=akt->next;
00095 }
00096 
00097 inline void Lines::setKoord(const int& a, const int& b, const int& c, const int& d){
00098        akt->x1=a; akt->y1=b;
00099        akt->x2=c; akt->y2=d;
00100 }
00101 
00102 inline void Lines::next(void){ akt=akt->next; }
00103 
00104 void Lines::turn(const int& w, const int& h){
00105        start->x1=end->x1+offx1;
00106        start->y1=end->y1+offy1;
00107        start->x2=end->x2+offx2;
00108        start->y2=end->y2+offy2;
00109        if(start->x1>=w) offx1=-8;
00110        if(start->x1<=0) offx1=7;
00111        if(start->y1>=h) offy1=-11;
00112        if(start->y1<=0) offy1=13;
00113        if(start->x2>=w) offx2=-17;
00114        if(start->x2<=0) offx2=15;
00115        if(start->y2>=h) offy2=-10;
00116        if(start->y2<=0) offy2=13;
00117        end->next=start;
00118        start=start->next;
00119        end=end->next;
00120 }
00121 
00122 
00123 //-----------------------------------------------------------------------------
00124 // dialog to setup screen saver parameters
00125 //
00126 kLinesSetup::kLinesSetup(QWidget *parent)
00127        : KDialog(parent)
00128          , saver( 0 ), length( 10 ), speed( 50 )
00129 {
00130        setCaption(i18n( "Setup Lines Screen Saver" ));
00131        setModal(true);
00132        setButtons(Ok|Cancel|Help);
00133        setDefaultButton(Ok);
00134 
00135        readSettings();
00136 
00137        setButtonText( Help, i18n( "A&bout" ) );
00138        QWidget *main = new QWidget(this);
00139        setMainWidget(main);
00140 
00141        QHBoxLayout *tl = new QHBoxLayout(main);
00142         tl->setSpacing( spacingHint() );
00143        QVBoxLayout *tl1 = new QVBoxLayout;
00144        tl->addLayout(tl1);
00145 
00146        QLabel *label=new QLabel(i18n("Length:"), main);
00147        tl1->addWidget(label);
00148 
00149        QSlider *sb= new QSlider(Qt::Horizontal, main);
00150         sb->setMinimum(1);
00151         sb->setMaximum(MAXLENGTH + 1);
00152         sb->setPageStep(16);
00153         sb->setValue(length);
00154        sb->setMinimumSize(120, 20);
00155        sb->setTickPosition(QSlider::TicksBelow);
00156        sb->setTickInterval(32);
00157        connect(sb, SIGNAL(valueChanged(int)), SLOT(slotLength(int)));
00158        tl1->addWidget(sb);
00159 
00160        label=new QLabel(i18n("Speed:"), main);
00161        tl1->addWidget(label);
00162 
00163        sb = new QSlider(Qt::Horizontal, main);
00164         sb->setMinimum(0);
00165         sb->setMaximum(100);
00166         sb->setPageStep(10);
00167         sb->setValue(speed);
00168        sb->setMinimumSize(120, 20);
00169        sb->setTickPosition(QSlider::TicksBelow);
00170        sb->setTickInterval(10);
00171        connect( sb, SIGNAL( valueChanged( int ) ), SLOT( slotSpeed( int ) ) );
00172        tl1->addWidget(sb);
00173 
00174        label=new QLabel(i18n("Beginning:"), main);
00175        tl1->addWidget(label);
00176 
00177        colorPush0=new KColorButton(colstart, main);
00178        connect(colorPush0, SIGNAL(changed(const QColor &)),
00179               SLOT(slotColstart(const QColor &)));
00180        tl1->addWidget(colorPush0);
00181 
00182        label=new QLabel(i18n("Middle:"), main);
00183        tl1->addWidget(label);
00184 
00185        colorPush1=new KColorButton(colmid, main);
00186        connect(colorPush1, SIGNAL(changed(const QColor &)),
00187               SLOT(slotColmid(const QColor &)));
00188        tl1->addWidget(colorPush1);
00189 
00190        label=new QLabel(i18n("End:"), main);
00191        tl1->addWidget(label);
00192 
00193        colorPush2=new KColorButton(colend, main);
00194        connect(colorPush2, SIGNAL(changed(const QColor &)),
00195               SLOT(slotColend(const QColor &)));
00196        tl1->addWidget(colorPush2);
00197        tl1->addStretch();
00198 
00199        preview = new QWidget( main );
00200        preview->setFixedSize( 220, 170 );
00201         {
00202             QPalette palette;
00203             palette.setColor( preview->backgroundRole(), Qt::black );
00204             preview->setPalette( palette );
00205            preview->setAutoFillBackground(true);
00206         }
00207        preview->show();    // otherwise saver does not get correct size
00208        saver=new kLinesSaver(preview->winId());
00209        tl->addWidget(preview);
00210        connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
00211        connect(this,SIGNAL(helpClicked()),this,SLOT(slotHelp()));
00212 }
00213 
00214 kLinesSetup::~kLinesSetup()
00215 {
00216     delete saver;
00217 }
00218 
00219 // read settings from config file
00220 void kLinesSetup::readSettings(){
00221     KConfigGroup config(KGlobal::config(), "Settings");
00222 
00223     QString str;
00224 
00225     length = config.readEntry("Length", length);
00226     if(length>MAXLENGTH) length=MAXLENGTH;
00227     else if(length<1) length=1;
00228 
00229     speed = config.readEntry("Speed", speed);
00230     if(speed>100) speed=100;
00231     else if(speed<50) speed=50;
00232 
00233     str=config.readEntry("StartColor");
00234     if(!str.isNull()) colstart.setNamedColor(str);
00235     else colstart=Qt::white;
00236     str=config.readEntry("MidColor");
00237     if(!str.isNull()) colmid.setNamedColor(str);
00238     else colmid=Qt::blue;
00239     str=config.readEntry("EndColor");
00240     if(!str.isNull()) colend.setNamedColor(str);
00241     else colend=Qt::black;
00242 }
00243 
00244 void kLinesSetup::slotLength(int len){
00245        length=len;
00246        if(saver) saver->setLines(length);
00247 }
00248 
00249 void kLinesSetup::slotSpeed(int num){
00250        speed=num;
00251        if(saver) saver->setSpeed(speed);
00252 }
00253 
00254 void kLinesSetup::slotColstart(const QColor &col){
00255     colstart = col;
00256     if(saver) saver->setColor(colstart, colmid, colend);
00257 }
00258 
00259 void kLinesSetup::slotColmid(const QColor &col){
00260     colmid = col;
00261     if(saver) saver->setColor(colstart, colmid, colend);
00262 }
00263 
00264 void kLinesSetup::slotColend(const QColor &col){
00265     colend = col;
00266     if(saver) saver->setColor(colstart, colmid, colend);
00267 }
00268 
00269 void kLinesSetup::slotHelp(){
00270        KMessageBox::about(this,
00271               i18n("Lines Version 2.2.0\n\n"
00272                                "Written by Dirk Staneker 1997\n"
00273                                "dirk.stanerker@student.uni-tuebingen.de"));
00274 }
00275 
00276 // Ok pressed - save settings and exit
00277 void kLinesSetup::slotOk(){
00278     KConfigGroup config(KGlobal::config(), "Settings");
00279 
00280     QString slength;
00281     slength.setNum(length);
00282     config.writeEntry("Length", slength);
00283 
00284     QString sspeed;
00285     sspeed.setNum( speed );
00286     config.writeEntry( "Speed", sspeed );
00287 
00288     QString colName0, colName1, colName2;
00289     colName0.sprintf("#%02x%02x%02x", colstart.red(),
00290                    colstart.green(), colstart.blue() );
00291     config.writeEntry( "StartColor", colName0 );
00292 
00293     colName1.sprintf("#%02x%02x%02x", colmid.red(),
00294                    colmid.green(), colmid.blue() );
00295     config.writeEntry( "MidColor", colName1 );
00296 
00297     colName2.sprintf("#%02x%02x%02x", colend.red(),
00298                    colend.green(), colend.blue() );
00299     config.writeEntry( "EndColor", colName2 );
00300 
00301     config.sync();
00302     accept();
00303 }
00304 
00305 //-----------------------------------------------------------------------------
00306 
00307 
00308 kLinesSaver::kLinesSaver( WId id ) : KScreenSaver( id ){
00309        readSettings();
00310        lines=new Lines(numLines);
00311        initialiseColor();
00312        initialiseLines();
00313        timer.start(speed);
00314        connect(&timer, SIGNAL(timeout()), SLOT(slotTimeout()));
00315         {
00316             QPalette palette;
00317             palette.setColor( backgroundRole(), Qt::black );
00318             setPalette( palette );
00319            setAutoFillBackground(true);
00320         }
00321        show();
00322 }
00323 
00324 kLinesSaver::~kLinesSaver(){
00325        timer.stop();
00326        delete lines;
00327 }
00328 
00329 // set lines properties
00330 void kLinesSaver::setLines(int len){
00331        timer.stop();
00332        numLines=len;
00333        initialiseLines();
00334        initialiseColor();
00335        timer.start(speed);
00336 }
00337 
00338 // set the speed
00339 void kLinesSaver::setSpeed(int spd){
00340        timer.stop();
00341        speed=100-spd;
00342        timer.start(speed);
00343 }
00344 
00345 void kLinesSaver::setColor(const QColor& cs, const QColor& cm, const QColor& ce){
00346        colstart=cs;
00347        colmid=cm;
00348        colend=ce;
00349         initialiseColor();
00350 }
00351 
00352 // read configuration settings from config file
00353 void kLinesSaver::readSettings(){
00354     KConfigGroup config(KGlobal::config(), "Settings");
00355 
00356     numLines=config.readEntry("Length", 10);
00357     speed = 100- config.readEntry("Speed", 50);
00358     if(numLines>MAXLENGTH) numLines=MAXLENGTH;
00359     else if(numLines<1) numLines = 1;
00360 
00361     colstart=config.readEntry("StartColor", QColor(Qt::white));
00362     colmid=config.readEntry("MidColor", QColor(Qt::blue));
00363     colend=config.readEntry("EndColor", QColor(Qt::black));
00364 }
00365 
00366 // draw next lines and erase tail
00367 void kLinesSaver::slotTimeout(){
00368    update();
00369 /*
00370        uint i;
00371        int x1,y1,x2,y2;
00372        int col=0;
00373 
00374        lines->reset();
00375     QPainter p( this );
00376     p.setPen( Qt::black );
00377 
00378        for(i=0; i<numLines; i++){
00379               lines->getKoord(x1,y1,x2,y2);
00380         p.drawLine( x1, y1, x2, y2 );
00381               p.setPen( colors[col] );
00382               col=(int)(i*colscale);
00383               if(col>63) col=0;
00384        }
00385        lines->turn(width(), height());
00386 */
00387 }
00388 
00389 void kLinesSaver::paintEvent(QPaintEvent *)
00390 {
00391         setAttribute( Qt::WA_NoSystemBackground ); // Only after initial clear
00392 
00393         uint i;
00394         int x1,y1,x2,y2;
00395         int col=0;
00396 
00397         lines->reset();
00398 
00399      QPainter p( this );
00400     p.setPen( Qt::black );
00401 
00402         for(i=0; i<numLines; i++){
00403                 lines->getKoord(x1,y1,x2,y2);
00404         p.drawLine( x1, y1, x2, y2 );
00405                 p.setPen( colors[col] );
00406                 col=(int)(i*colscale);
00407                 if(col>63) col=0;
00408         }
00409         lines->turn(width(), height());
00410 
00411 }
00412 
00413 // initialise the lines
00414 void kLinesSaver::initialiseLines(){
00415        uint i;
00416        int x1,y1,x2,y2;
00417        delete lines;
00418        lines=new Lines(numLines);
00419        lines->reset();
00420        x1=rnd.getLong(width());
00421        y1=rnd.getLong(height());
00422        x2=rnd.getLong(width());
00423        y2=rnd.getLong(height());
00424        for(i=0; i<numLines; i++){
00425               lines->setKoord(x1,y1,x2,y2);
00426               lines->next();
00427        }
00428 }
00429 
00430 // create a color table of 64 colors
00431 void kLinesSaver::initialiseColor(){
00432        int i;
00433        double mr, mg, mb;
00434        double cr, cg, cb;
00435     mr=(double)(colmid.red()-colstart.red())/32;
00436     mg=(double)(colmid.green()-colstart.green())/32;
00437     mb=(double)(colmid.blue()-colstart.blue())/32;
00438     cr=colstart.red();
00439     cg=colstart.green();
00440     cb=colstart.blue();
00441        for(i=0; i<32; i++){
00442               colors[63-i].setRgb((int)(mr*i+cr), (int)(mg*i+cg), (int)(mb*i+cb));
00443        }
00444        mr=(double)(colend.red()-colmid.red())/32;
00445        mg=(double)(colend.green()-colmid.green())/32;
00446        mb=(double)(colend.blue()-colmid.blue())/32;
00447        cr=colmid.red();
00448        cg=colmid.green();
00449        cb=colmid.blue();
00450        for(i=0; i<32; i++){
00451               colors[31-1].setRgb((int)(mr*i+cr), (int)(mg*i+cg), (int)(mb*i+cb));
00452        }
00453        colscale=64.0/(double)numLines;
00454 }