~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kscreensaver/libkscreensaver/kscreensaver.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE libraries
 
2
 
 
3
    Copyright (c) 2001  Martin R. Jones <mjones@kde.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#include "kscreensaver.h"
 
22
 
 
23
#include <QPainter>
 
24
#include <QTimer>
 
25
#include <QtGui/QX11Info>
 
26
#include <QApplication>
 
27
#include <QDebug>
 
28
#include <krandom.h>
 
29
 
 
30
#ifdef Q_WS_X11
 
31
#include <X11/Xlib.h>
 
32
#endif
 
33
 
 
34
//-----------------------------------------------------------------------------
 
35
 
 
36
KScreenSaver::KScreenSaver( WId id ) : QWidget(), embeddedWidget(0)
 
37
{
 
38
    if ( id )
 
39
    {
 
40
        create( id, false, true );
 
41
    }
 
42
}
 
43
 
 
44
KScreenSaver::~KScreenSaver()
 
45
{
 
46
    destroy( false, false );
 
47
}
 
48
 
 
49
bool KScreenSaver::event(QEvent* e)
 
50
{
 
51
    bool r = QWidget::event(e);
 
52
    if (e->type() == QEvent::Polish)
 
53
        setAttribute(Qt::WA_StyledBackground, false);
 
54
    if ((e->type() == QEvent::Resize) && embeddedWidget)
 
55
        embeddedWidget->resize( size() );
 
56
    return r;
 
57
}
 
58
 
 
59
void KScreenSaver::embed( QWidget *w )
 
60
{
 
61
    w->resize( size() );
 
62
    QApplication::sendPostedEvents();
 
63
#ifdef Q_WS_X11 //FIXME
 
64
    XReparentWindow(QX11Info::display(), w->winId(), winId(), 0, 0);
 
65
#endif
 
66
    w->setGeometry( 0, 0, width(), height() );
 
67
    embeddedWidget = w;
 
68
    QApplication::sendPostedEvents();
 
69
}
 
70
 
 
71
KScreenSaverInterface::~KScreenSaverInterface()
 
72
{
 
73
}
 
74
 
 
75
QDialog* KScreenSaverInterface::setup()
 
76
{
 
77
    return 0;
 
78
}
 
79
 
 
80
//============================================================================
 
81
 
 
82
class KBlankEffectPrivate
 
83
{
 
84
public:
 
85
    KBlankEffect::BlankEffect currentEffect;
 
86
    int effectProgress;
 
87
    QTimer *timer;
 
88
    QWidget *widget;
 
89
};
 
90
 
 
91
KBlankEffect::BlankEffect KBlankEffect::effects[] = {
 
92
    &KBlankEffect::blankNormal,
 
93
    &KBlankEffect::blankSweepRight,
 
94
    &KBlankEffect::blankSweepDown,
 
95
    &KBlankEffect::blankBlocks
 
96
};
 
97
 
 
98
KBlankEffect::KBlankEffect( QObject *parent ) : QObject( parent )
 
99
{
 
100
    d = new KBlankEffectPrivate;
 
101
    d->currentEffect = &KBlankEffect::blankNormal;
 
102
    d->effectProgress = 0;
 
103
    d->timer = new QTimer( this );
 
104
    connect( d->timer, SIGNAL(timeout()), this, SLOT(timeout()) );
 
105
}
 
106
 
 
107
 
 
108
KBlankEffect::~KBlankEffect()
 
109
{
 
110
    delete d;
 
111
}
 
112
 
 
113
void KBlankEffect::finished()
 
114
{
 
115
    d->timer->stop();
 
116
    d->effectProgress = 0;
 
117
    emit doneBlank();
 
118
}
 
119
 
 
120
 
 
121
void KBlankEffect::blank( QWidget *w, Effect effect )
 
122
{
 
123
    if ( !w ) {
 
124
        emit doneBlank();
 
125
        return;
 
126
    }
 
127
 
 
128
    if ( effect == Random )
 
129
        effect = (Effect)(KRandom::random() % MaximumEffects);
 
130
 
 
131
    d->effectProgress = 0;
 
132
    d->widget = w;
 
133
    d->currentEffect = effects[ (int)effect ];
 
134
    d->timer->start( 10 );
 
135
}
 
136
 
 
137
void KBlankEffect::timeout()
 
138
{
 
139
    (this->*d->currentEffect)();
 
140
}
 
141
 
 
142
void KBlankEffect::blankNormal()
 
143
{
 
144
    QPainter p( d->widget );
 
145
    p.fillRect( 0, 0, d->widget->width(), d->widget->height(), Qt::black );
 
146
    finished();
 
147
}
 
148
 
 
149
 
 
150
void KBlankEffect::blankSweepRight()
 
151
{
 
152
    QPainter p( d->widget );
 
153
    p.fillRect( d->effectProgress, 0, 50, d->widget->height(), Qt::black );
 
154
    qApp->flush();
 
155
    d->effectProgress += 50;
 
156
    if ( d->effectProgress >= d->widget->width() )
 
157
        finished();
 
158
}
 
159
 
 
160
 
 
161
void KBlankEffect::blankSweepDown()
 
162
{
 
163
    QPainter p( d->widget );
 
164
    p.fillRect( 0, d->effectProgress, d->widget->width(), 50, Qt::black );
 
165
    qApp->flush();
 
166
    d->effectProgress += 50;
 
167
    if ( d->effectProgress >= d->widget->height() )
 
168
        finished();
 
169
}
 
170
 
 
171
 
 
172
void KBlankEffect::blankBlocks()
 
173
{
 
174
    static int *block = 0;
 
175
 
 
176
    int bx = (d->widget->width()+63)/64;
 
177
    int by = (d->widget->height()+63)/64;
 
178
 
 
179
    if ( !d->effectProgress ) {
 
180
        block = new int [ bx*by ];
 
181
 
 
182
        for ( int i = 0; i < bx*by; i++ )
 
183
            block[i] = i;
 
184
        for ( int i = 0; i < bx*by; i++ ) {
 
185
            int swap = KRandom::random()%(bx*by);
 
186
            int tmp = block[i];
 
187
            block[i] = block[swap];
 
188
            block[swap] = tmp;
 
189
        }
 
190
    }
 
191
 
 
192
    QPainter p( d->widget );
 
193
 
 
194
    // erase a couple of blocks at a time, otherwise it looks too slow
 
195
    for ( int i = 0; i < 2 && d->effectProgress < bx*by; i++ ) {
 
196
        int x = block[d->effectProgress]%bx;
 
197
        int y = block[d->effectProgress]/bx;
 
198
        p.fillRect( x*64, y*64, 64, 64, Qt::black );
 
199
        d->effectProgress++;
 
200
    }
 
201
 
 
202
    qApp->flush();
 
203
 
 
204
    if ( d->effectProgress >= bx*by ) {
 
205
        delete[] block;
 
206
        finished();
 
207
    }
 
208
}
 
209
 
 
210
#include "kscreensaver.moc"