not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kscreensaver / libkscreensaver / kscreensaver.cpp
blob1a112b83c0c70c0fc62ede53db3e5b28400807ff
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 <QDebug>
28 #include <krandom.h>
30 #ifdef Q_WS_X11
31 #include <X11/Xlib.h>
32 #endif
34 //-----------------------------------------------------------------------------
36 KScreenSaver::KScreenSaver( WId id ) : QWidget()
38 if ( id )
40 create( id, false, true );
44 KScreenSaver::~KScreenSaver()
46 destroy( false, false );
49 bool KScreenSaver::event(QEvent* e)
51 bool r = QWidget::event(e);
52 if (e->type() == QEvent::Polish)
53 setAttribute(Qt::WA_StyledBackground, false);
54 return r;
57 void KScreenSaver::embed( QWidget *w )
59 QApplication::sendPostedEvents();
60 #ifdef Q_WS_X11 //FIXME
61 XReparentWindow(QX11Info::display(), w->winId(), winId(), 0, 0);
62 #endif
63 w->setGeometry( 0, 0, width(), height() );
64 QApplication::sendPostedEvents();
67 KScreenSaverInterface::~KScreenSaverInterface()
71 QDialog* KScreenSaverInterface::setup()
73 return 0;
76 //============================================================================
78 class KBlankEffectPrivate
80 public:
81 KBlankEffect::BlankEffect currentEffect;
82 int effectProgress;
83 QTimer *timer;
84 QWidget *widget;
87 KBlankEffect::BlankEffect KBlankEffect::effects[] = {
88 &KBlankEffect::blankNormal,
89 &KBlankEffect::blankSweepRight,
90 &KBlankEffect::blankSweepDown,
91 &KBlankEffect::blankBlocks
94 KBlankEffect::KBlankEffect( QObject *parent ) : QObject( parent )
96 d = new KBlankEffectPrivate;
97 d->currentEffect = &KBlankEffect::blankNormal;
98 d->effectProgress = 0;
99 d->timer = new QTimer( this );
100 connect( d->timer, SIGNAL(timeout()), this, SLOT(timeout()) );
104 KBlankEffect::~KBlankEffect()
106 delete d;
109 void KBlankEffect::finished()
111 d->timer->stop();
112 d->effectProgress = 0;
113 emit doneBlank();
117 void KBlankEffect::blank( QWidget *w, Effect effect )
119 if ( !w ) {
120 emit doneBlank();
121 return;
124 if ( effect == Random )
125 effect = (Effect)(KRandom::random() % MaximumEffects);
127 d->effectProgress = 0;
128 d->widget = w;
129 d->currentEffect = effects[ (int)effect ];
130 d->timer->start( 10 );
133 void KBlankEffect::timeout()
135 (this->*d->currentEffect)();
138 void KBlankEffect::blankNormal()
140 QPainter p( d->widget );
141 p.fillRect( 0, 0, d->widget->width(), d->widget->height(), Qt::black );
142 finished();
146 void KBlankEffect::blankSweepRight()
148 QPainter p( d->widget );
149 p.fillRect( d->effectProgress, 0, 50, d->widget->height(), Qt::black );
150 qApp->flush();
151 d->effectProgress += 50;
152 if ( d->effectProgress >= d->widget->width() )
153 finished();
157 void KBlankEffect::blankSweepDown()
159 QPainter p( d->widget );
160 p.fillRect( 0, d->effectProgress, d->widget->width(), 50, Qt::black );
161 qApp->flush();
162 d->effectProgress += 50;
163 if ( d->effectProgress >= d->widget->height() )
164 finished();
168 void KBlankEffect::blankBlocks()
170 static int *block = 0;
172 int bx = (d->widget->width()+63)/64;
173 int by = (d->widget->height()+63)/64;
175 if ( !d->effectProgress ) {
176 block = new int [ bx*by ];
178 for ( int i = 0; i < bx*by; i++ )
179 block[i] = i;
180 for ( int i = 0; i < bx*by; i++ ) {
181 int swap = KRandom::random()%(bx*by);
182 int tmp = block[i];
183 block[i] = block[swap];
184 block[swap] = tmp;
188 QPainter p( d->widget );
190 // erase a couple of blocks at a time, otherwise it looks too slow
191 for ( int i = 0; i < 2 && d->effectProgress < bx*by; i++ ) {
192 int x = block[d->effectProgress]%bx;
193 int y = block[d->effectProgress]/bx;
194 p.fillRect( x*64, y*64, 64, 64, Qt::black );
195 d->effectProgress++;
198 qApp->flush();
200 if ( d->effectProgress >= bx*by ) {
201 delete[] block;
202 finished();
206 #include "kscreensaver.moc"