1 //-----------------------------------------------------------------------------
3 // klines 0.1.1 - Basic screen saver for KDE
4 // by Dirk Staneker 1997
5 // based on kpolygon from Martin R. Jones 1996
6 // mailto:dirk.staneker@student.uni-tuebingen.de
8 // layout management added 1998/04/19 by Mario Weilguni <mweilguni@kde.org>
9 // 2001/03/04 Converted to libkscreensaver by Martin R. Jones
18 #include <QVBoxLayout>
19 #include <QHBoxLayout>
21 #include <kapplication.h>
22 #include <kmessagebox.h>
23 #include <kcolorbutton.h>
25 #include "kcolordialog.h"
36 // libkscreensaver interface
37 class kLinesSaverInterface
: public KScreenSaverInterface
42 virtual KAboutData
* aboutData() {
43 return new KAboutData( "klines.kss", I18N_NOOP( "KLines" ), "2.2.0", I18N_NOOP( "KLines" ) );
47 virtual KScreenSaver
* create( WId id
)
49 return new kLinesSaver( id
);
52 virtual QDialog
* setup()
54 return new kLinesSetup();
58 int main( int argc
, char *argv
[] )
60 kLinesSaverInterface kss
;
61 return kScreenSaverMain( argc
, argv
, kss
);
64 // Methods of the Lines-class
74 for(i
=1; i
<numLn
; i
++){
84 for(i
=0; i
<numLn
; i
++){
91 inline void Lines::reset(){ akt
=start
; }
93 inline void Lines::getKoord(int& a
, int& b
, int& c
, int& d
){
99 inline void Lines::setKoord(const int& a
, const int& b
, const int& c
, const int& d
){
100 akt
->x1
=a
; akt
->y1
=b
;
101 akt
->x2
=c
; akt
->y2
=d
;
104 inline void Lines::next(void){ akt
=akt
->next
; }
106 void Lines::turn(const int& w
, const int& h
){
107 start
->x1
=end
->x1
+offx1
;
108 start
->y1
=end
->y1
+offy1
;
109 start
->x2
=end
->x2
+offx2
;
110 start
->y2
=end
->y2
+offy2
;
111 if(start
->x1
>=w
) offx1
=-8;
112 if(start
->x1
<=0) offx1
=7;
113 if(start
->y1
>=h
) offy1
=-11;
114 if(start
->y1
<=0) offy1
=13;
115 if(start
->x2
>=w
) offx2
=-17;
116 if(start
->x2
<=0) offx2
=15;
117 if(start
->y2
>=h
) offy2
=-10;
118 if(start
->y2
<=0) offy2
=13;
125 //-----------------------------------------------------------------------------
126 // dialog to setup screen saver parameters
128 kLinesSetup::kLinesSetup(QWidget
*parent
, const char *name
)
130 , saver( 0 ), length( 10 ), speed( 50 )
132 setCaption(i18n( "Setup Lines Screen Saver" ));
134 setButtons(Ok
|Cancel
|Help
);
135 setDefaultButton(Ok
);
139 setButtonText( Help
, i18n( "A&bout" ) );
140 QWidget
*main
= new QWidget(this);
143 QHBoxLayout
*tl
= new QHBoxLayout(main
, 0, spacingHint());
144 QVBoxLayout
*tl1
= new QVBoxLayout
;
147 QLabel
*label
=new QLabel(i18n("Length:"), main
);
148 tl1
->addWidget(label
);
150 QSlider
*sb
= new QSlider(1, MAXLENGTH
+1, 16, length
, Qt::Horizontal
,
152 sb
->setMinimumSize(120, 20);
153 sb
->setTickPosition(QSlider::TicksBelow
);
154 sb
->setTickInterval(32);
155 connect(sb
, SIGNAL(valueChanged(int)), SLOT(slotLength(int)));
158 label
=new QLabel(i18n("Speed:"), main
);
159 tl1
->addWidget(label
);
161 sb
= new QSlider(0, 100, 10, speed
, Qt::Horizontal
, main
);
162 sb
->setMinimumSize(120, 20);
163 sb
->setTickPosition(QSlider::TicksBelow
);
164 sb
->setTickInterval(10);
165 connect( sb
, SIGNAL( valueChanged( int ) ), SLOT( slotSpeed( int ) ) );
168 label
=new QLabel(i18n("Beginning:"), main
);
169 tl1
->addWidget(label
);
171 colorPush0
=new KColorButton(colstart
, main
);
172 connect(colorPush0
, SIGNAL(changed(const QColor
&)),
173 SLOT(slotColstart(const QColor
&)));
174 tl1
->addWidget(colorPush0
);
176 label
=new QLabel(i18n("Middle:"), main
);
177 tl1
->addWidget(label
);
179 colorPush1
=new KColorButton(colmid
, main
);
180 connect(colorPush1
, SIGNAL(changed(const QColor
&)),
181 SLOT(slotColmid(const QColor
&)));
182 tl1
->addWidget(colorPush1
);
184 label
=new QLabel(i18n("End:"), main
);
185 tl1
->addWidget(label
);
187 colorPush2
=new KColorButton(colend
, main
);
188 connect(colorPush2
, SIGNAL(changed(const QColor
&)),
189 SLOT(slotColend(const QColor
&)));
190 tl1
->addWidget(colorPush2
);
193 preview
= new QWidget( main
);
194 preview
->setFixedSize( 220, 170 );
195 preview
->setBackgroundColor( Qt::black
);
196 preview
->show(); // otherwise saver does not get correct size
197 saver
=new kLinesSaver(preview
->winId());
198 tl
->addWidget(preview
);
201 kLinesSetup::~kLinesSetup()
206 // read settings from config file
207 void kLinesSetup::readSettings(){
208 KConfig
*config
= KGlobal::config();
209 config
->setGroup( "Settings" );
213 length
= config
->readEntry("Length", length
);
214 if(length
>MAXLENGTH
) length
=MAXLENGTH
;
215 else if(length
<1) length
=1;
217 speed
= config
->readEntry("Speed", speed
);
218 if(speed
>100) speed
=100;
219 else if(speed
<50) speed
=50;
221 str
=config
->readEntry("StartColor");
222 if(!str
.isNull()) colstart
.setNamedColor(str
);
223 else colstart
=Qt::white
;
224 str
=config
->readEntry("MidColor");
225 if(!str
.isNull()) colmid
.setNamedColor(str
);
226 else colmid
=Qt::blue
;
227 str
=config
->readEntry("EndColor");
228 if(!str
.isNull()) colend
.setNamedColor(str
);
229 else colend
=Qt::black
;
232 void kLinesSetup::slotLength(int len
){
234 if(saver
) saver
->setLines(length
);
237 void kLinesSetup::slotSpeed(int num
){
239 if(saver
) saver
->setSpeed(speed
);
242 void kLinesSetup::slotColstart(const QColor
&col
){
244 if(saver
) saver
->setColor(colstart
, colmid
, colend
);
247 void kLinesSetup::slotColmid(const QColor
&col
){
249 if(saver
) saver
->setColor(colstart
, colmid
, colend
);
252 void kLinesSetup::slotColend(const QColor
&col
){
254 if(saver
) saver
->setColor(colstart
, colmid
, colend
);
257 void kLinesSetup::slotHelp(){
258 KMessageBox::about(this,
259 i18n("Lines Version 2.2.0\n\n"
260 "Written by Dirk Staneker 1997\n"
261 "dirk.stanerker@student.uni-tuebingen.de"));
264 // Ok pressed - save settings and exit
265 void kLinesSetup::slotOk(){
266 KConfig
*config
= KGlobal::config();
267 config
->setGroup("Settings");
270 slength
.setNum(length
);
271 config
->writeEntry("Length", slength
);
274 sspeed
.setNum( speed
);
275 config
->writeEntry( "Speed", sspeed
);
277 QString colName0
, colName1
, colName2
;
278 colName0
.sprintf("#%02x%02x%02x", colstart
.red(),
279 colstart
.green(), colstart
.blue() );
280 config
->writeEntry( "StartColor", colName0
);
282 colName1
.sprintf("#%02x%02x%02x", colmid
.red(),
283 colmid
.green(), colmid
.blue() );
284 config
->writeEntry( "MidColor", colName1
);
286 colName2
.sprintf("#%02x%02x%02x", colend
.red(),
287 colend
.green(), colend
.blue() );
288 config
->writeEntry( "EndColor", colName2
);
294 //-----------------------------------------------------------------------------
297 kLinesSaver::kLinesSaver( WId id
) : KScreenSaver( id
){
299 lines
=new Lines(numLines
);
304 connect(&timer
, SIGNAL(timeout()), SLOT(slotTimeout()));
307 kLinesSaver::~kLinesSaver(){
312 // set lines properties
313 void kLinesSaver::setLines(int len
){
323 void kLinesSaver::setSpeed(int spd
){
329 void kLinesSaver::setColor(const QColor
& cs
, const QColor
& cm
, const QColor
& ce
){
336 // read configuration settings from config file
337 void kLinesSaver::readSettings(){
338 KConfig
*config
=KGlobal::config();
339 config
->setGroup("Settings");
341 numLines
=config
->readEntry("Length", 10);
342 speed
= 100- config
->readEntry("Speed", 50);
343 if(numLines
>MAXLENGTH
) numLines
=MAXLENGTH
;
344 else if(numLines
<1) numLines
= 1;
346 colstart
=config
->readEntry("StartColor", Qt::white
);
347 colmid
=config
->readEntry("MidColor", Qt::blue
);
348 colend
=config
->readEntry("EndColor", Qt::black
);
351 // draw next lines and erase tail
352 void kLinesSaver::slotTimeout(){
359 p
.setPen( Qt::black
);
361 for(i
=0; i
<numLines
; i
++){
362 lines
->getKoord(x1
,y1
,x2
,y2
);
363 p
.drawLine( x1
, y1
, x2
, y2
);
364 p
.setPen( colors
[col
] );
365 col
=(int)(i
*colscale
);
368 lines
->turn(width(), height());
371 void kLinesSaver::blank(){
372 setBackgroundColor( Qt::black
);
376 // initialise the lines
377 void kLinesSaver::initialiseLines(){
381 lines
=new Lines(numLines
);
383 x1
=rnd
.getLong(width());
384 y1
=rnd
.getLong(height());
385 x2
=rnd
.getLong(width());
386 y2
=rnd
.getLong(height());
387 for(i
=0; i
<numLines
; i
++){
388 lines
->setKoord(x1
,y1
,x2
,y2
);
393 // create a color table of 64 colors
394 void kLinesSaver::initialiseColor(){
398 mr
=(double)(colmid
.red()-colstart
.red())/32;
399 mg
=(double)(colmid
.green()-colstart
.green())/32;
400 mb
=(double)(colmid
.blue()-colstart
.blue())/32;
405 colors
[63-i
].setRgb((int)(mr
*i
+cr
), (int)(mg
*i
+cg
), (int)(mb
*i
+cb
));
407 mr
=(double)(colend
.red()-colmid
.red())/32;
408 mg
=(double)(colend
.green()-colmid
.green())/32;
409 mb
=(double)(colend
.blue()-colmid
.blue())/32;
414 colors
[31-1].setRgb((int)(mr
*i
+cr
), (int)(mg
*i
+cg
), (int)(mb
*i
+cb
));
416 colscale
=64.0/(double)numLines
;