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"
25 #include <QtGui/QX11Info>
26 #include <QApplication>
34 //-----------------------------------------------------------------------------
36 KScreenSaver::KScreenSaver( WId id
) : QWidget()
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);
57 void KScreenSaver::embed( QWidget
*w
)
59 QApplication::sendPostedEvents();
60 #ifdef Q_WS_X11 //FIXME
61 XReparentWindow(QX11Info::display(), w
->winId(), winId(), 0, 0);
63 w
->setGeometry( 0, 0, width(), height() );
64 QApplication::sendPostedEvents();
67 KScreenSaverInterface::~KScreenSaverInterface()
71 QDialog
* KScreenSaverInterface::setup()
76 //============================================================================
78 class KBlankEffectPrivate
81 KBlankEffect::BlankEffect currentEffect
;
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()
109 void KBlankEffect::finished()
112 d
->effectProgress
= 0;
117 void KBlankEffect::blank( QWidget
*w
, Effect effect
)
124 if ( effect
== Random
)
125 effect
= (Effect
)(KRandom::random() % MaximumEffects
);
127 d
->effectProgress
= 0;
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
);
146 void KBlankEffect::blankSweepRight()
148 QPainter
p( d
->widget
);
149 p
.fillRect( d
->effectProgress
, 0, 50, d
->widget
->height(), Qt::black
);
151 d
->effectProgress
+= 50;
152 if ( d
->effectProgress
>= d
->widget
->width() )
157 void KBlankEffect::blankSweepDown()
159 QPainter
p( d
->widget
);
160 p
.fillRect( 0, d
->effectProgress
, d
->widget
->width(), 50, Qt::black
);
162 d
->effectProgress
+= 50;
163 if ( d
->effectProgress
>= d
->widget
->height() )
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
++ )
180 for ( int i
= 0; i
< bx
*by
; i
++ ) {
181 int swap
= KRandom::random()%(bx
*by
);
183 block
[i
] = block
[swap
];
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
);
200 if ( d
->effectProgress
>= bx
*by
) {
206 #include "kscreensaver.moc"