Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kscreensaver / libkscreensaver / kscreensaver.cpp
blob2dfc521e620538c197baff7e676b31b4ae2055d5
1 /* This file is part of the KDE libraries
3 Copyright (c) 2001 Martin R. Jones <mjones@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "kscreensaver.h"
23 #include <QPainter>
24 #include <QTimer>
25 #include <QtGui/QX11Info>
26 #include <QApplication>
27 #include <krandom.h>
29 #ifdef Q_WS_X11
30 #include <X11/Xlib.h>
31 #endif
33 //-----------------------------------------------------------------------------
35 KScreenSaver::KScreenSaver( WId id ) : QWidget()
37 if ( id )
39 create( id, false, true );
43 KScreenSaver::~KScreenSaver()
45 destroy( false, false );
48 void KScreenSaver::embed( QWidget *w )
50 QApplication::sendPostedEvents();
51 #ifdef Q_WS_X11 //FIXME
52 XReparentWindow(QX11Info::display(), w->winId(), winId(), 0, 0);
53 #endif
54 w->setGeometry( 0, 0, width(), height() );
55 QApplication::sendPostedEvents();
58 KScreenSaverInterface::~KScreenSaverInterface()
62 QDialog* KScreenSaverInterface::setup()
64 return 0;
67 //============================================================================
69 class KBlankEffectPrivate
71 public:
72 KBlankEffect::BlankEffect currentEffect;
73 int effectProgress;
74 QTimer *timer;
75 QWidget *widget;
78 KBlankEffect::BlankEffect KBlankEffect::effects[] = {
79 &KBlankEffect::blankNormal,
80 &KBlankEffect::blankSweepRight,
81 &KBlankEffect::blankSweepDown,
82 &KBlankEffect::blankBlocks
85 KBlankEffect::KBlankEffect( QObject *parent ) : QObject( parent )
87 d = new KBlankEffectPrivate;
88 d->currentEffect = &KBlankEffect::blankNormal;
89 d->effectProgress = 0;
90 d->timer = new QTimer( this );
91 connect( d->timer, SIGNAL(timeout()), this, SLOT(timeout()) );
95 KBlankEffect::~KBlankEffect()
97 delete d;
100 void KBlankEffect::finished()
102 d->timer->stop();
103 d->effectProgress = 0;
104 emit doneBlank();
108 void KBlankEffect::blank( QWidget *w, Effect effect )
110 if ( !w ) {
111 emit doneBlank();
112 return;
115 if ( effect == Random )
116 effect = (Effect)(KRandom::random() % MaximumEffects);
118 d->effectProgress = 0;
119 d->widget = w;
120 d->currentEffect = effects[ (int)effect ];
121 d->timer->start( 10 );
124 void KBlankEffect::timeout()
126 (this->*d->currentEffect)();
129 void KBlankEffect::blankNormal()
131 QPainter p( d->widget );
132 p.fillRect( 0, 0, d->widget->width(), d->widget->height(), Qt::black );
133 finished();
137 void KBlankEffect::blankSweepRight()
139 QPainter p( d->widget );
140 p.fillRect( d->effectProgress, 0, 50, d->widget->height(), Qt::black );
141 qApp->flush();
142 d->effectProgress += 50;
143 if ( d->effectProgress >= d->widget->width() )
144 finished();
148 void KBlankEffect::blankSweepDown()
150 QPainter p( d->widget );
151 p.fillRect( 0, d->effectProgress, d->widget->width(), 50, Qt::black );
152 qApp->flush();
153 d->effectProgress += 50;
154 if ( d->effectProgress >= d->widget->height() )
155 finished();
159 void KBlankEffect::blankBlocks()
161 static int *block = 0;
163 int bx = (d->widget->width()+63)/64;
164 int by = (d->widget->height()+63)/64;
166 if ( !d->effectProgress ) {
167 block = new int [ bx*by ];
169 for ( int i = 0; i < bx*by; i++ )
170 block[i] = i;
171 for ( int i = 0; i < bx*by; i++ ) {
172 int swap = KRandom::random()%(bx*by);
173 int tmp = block[i];
174 block[i] = block[swap];
175 block[swap] = tmp;
179 QPainter p( d->widget );
181 // erase a couple of blocks at a time, otherwise it looks too slow
182 for ( int i = 0; i < 2 && d->effectProgress < bx*by; i++ ) {
183 int x = block[d->effectProgress]%bx;
184 int y = block[d->effectProgress]/bx;
185 p.fillRect( x*64, y*64, 64, 64, Qt::black );
186 d->effectProgress++;
189 qApp->flush();
191 if ( d->effectProgress >= bx*by ) {
192 delete[] block;
193 finished();
197 #include "kscreensaver.moc"