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
16 #include <QVBoxLayout>
17 #include <QHBoxLayout>
19 #include <kapplication.h>
20 #include <kmessagebox.h>
21 #include <kcolorbutton.h>
23 #include "kcolordialog.h"
34 // libkscreensaver interface
35 class kLinesSaverInterface
: public KScreenSaverInterface
40 virtual KAboutData
* aboutData() {
41 return new KAboutData( "klines.kss", 0, ki18n( "KLines" ), "2.2.0", ki18n( "KLines" ) );
45 virtual KScreenSaver
* create( WId id
)
47 return new kLinesSaver( id
);
50 virtual QDialog
* setup()
52 return new kLinesSetup();
56 int main( int argc
, char *argv
[] )
58 kLinesSaverInterface kss
;
59 return kScreenSaverMain( argc
, argv
, kss
);
62 // Methods of the Lines-class
72 for(i
=1; i
<numLn
; i
++){
82 for(i
=0; i
<numLn
; i
++){
89 inline void Lines::reset(){ akt
=start
; }
91 inline void Lines::getKoord(int& a
, int& b
, int& c
, int& d
){
97 inline void Lines::setKoord(const int& a
, const int& b
, const int& c
, const int& d
){
102 inline void Lines::next(void){ akt
=akt
->next
; }
104 void Lines::turn(const int& w
, const int& h
){
105 start
->x1
=end
->x1
+offx1
;
106 start
->y1
=end
->y1
+offy1
;
107 start
->x2
=end
->x2
+offx2
;
108 start
->y2
=end
->y2
+offy2
;
109 if(start
->x1
>=w
) offx1
=-8;
110 if(start
->x1
<=0) offx1
=7;
111 if(start
->y1
>=h
) offy1
=-11;
112 if(start
->y1
<=0) offy1
=13;
113 if(start
->x2
>=w
) offx2
=-17;
114 if(start
->x2
<=0) offx2
=15;
115 if(start
->y2
>=h
) offy2
=-10;
116 if(start
->y2
<=0) offy2
=13;
123 //-----------------------------------------------------------------------------
124 // dialog to setup screen saver parameters
126 kLinesSetup::kLinesSetup(QWidget
*parent
)
128 , saver( 0 ), length( 10 ), speed( 50 )
130 setCaption(i18n( "Setup Lines Screen Saver" ));
132 setButtons(Ok
|Cancel
|Help
);
133 setDefaultButton(Ok
);
137 setButtonText( Help
, i18n( "A&bout" ) );
138 QWidget
*main
= new QWidget(this);
141 QHBoxLayout
*tl
= new QHBoxLayout(main
);
142 tl
->setSpacing( spacingHint() );
143 QVBoxLayout
*tl1
= new QVBoxLayout
;
146 QLabel
*label
=new QLabel(i18n("Length:"), main
);
147 tl1
->addWidget(label
);
149 QSlider
*sb
= new QSlider(Qt::Horizontal
, main
);
151 sb
->setMaximum(MAXLENGTH
+ 1);
153 sb
->setValue(length
);
154 sb
->setMinimumSize(120, 20);
155 sb
->setTickPosition(QSlider::TicksBelow
);
156 sb
->setTickInterval(32);
157 connect(sb
, SIGNAL(valueChanged(int)), SLOT(slotLength(int)));
160 label
=new QLabel(i18n("Speed:"), main
);
161 tl1
->addWidget(label
);
163 sb
= new QSlider(Qt::Horizontal
, main
);
168 sb
->setMinimumSize(120, 20);
169 sb
->setTickPosition(QSlider::TicksBelow
);
170 sb
->setTickInterval(10);
171 connect( sb
, SIGNAL( valueChanged( int ) ), SLOT( slotSpeed( int ) ) );
174 label
=new QLabel(i18n("Beginning:"), main
);
175 tl1
->addWidget(label
);
177 colorPush0
=new KColorButton(colstart
, main
);
178 connect(colorPush0
, SIGNAL(changed(const QColor
&)),
179 SLOT(slotColstart(const QColor
&)));
180 tl1
->addWidget(colorPush0
);
182 label
=new QLabel(i18n("Middle:"), main
);
183 tl1
->addWidget(label
);
185 colorPush1
=new KColorButton(colmid
, main
);
186 connect(colorPush1
, SIGNAL(changed(const QColor
&)),
187 SLOT(slotColmid(const QColor
&)));
188 tl1
->addWidget(colorPush1
);
190 label
=new QLabel(i18n("End:"), main
);
191 tl1
->addWidget(label
);
193 colorPush2
=new KColorButton(colend
, main
);
194 connect(colorPush2
, SIGNAL(changed(const QColor
&)),
195 SLOT(slotColend(const QColor
&)));
196 tl1
->addWidget(colorPush2
);
199 preview
= new QWidget( main
);
200 preview
->setFixedSize( 220, 170 );
203 palette
.setColor( preview
->backgroundRole(), Qt::black
);
204 preview
->setPalette( palette
);
206 preview
->show(); // otherwise saver does not get correct size
207 saver
=new kLinesSaver(preview
->winId());
208 tl
->addWidget(preview
);
209 connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
210 connect(this,SIGNAL(helpClicked()),this,SLOT(slotHelp()));
213 kLinesSetup::~kLinesSetup()
218 // read settings from config file
219 void kLinesSetup::readSettings(){
220 KConfigGroup
config(KGlobal::config(), "Settings");
224 length
= config
.readEntry("Length", length
);
225 if(length
>MAXLENGTH
) length
=MAXLENGTH
;
226 else if(length
<1) length
=1;
228 speed
= config
.readEntry("Speed", speed
);
229 if(speed
>100) speed
=100;
230 else if(speed
<50) speed
=50;
232 str
=config
.readEntry("StartColor");
233 if(!str
.isNull()) colstart
.setNamedColor(str
);
234 else colstart
=Qt::white
;
235 str
=config
.readEntry("MidColor");
236 if(!str
.isNull()) colmid
.setNamedColor(str
);
237 else colmid
=Qt::blue
;
238 str
=config
.readEntry("EndColor");
239 if(!str
.isNull()) colend
.setNamedColor(str
);
240 else colend
=Qt::black
;
243 void kLinesSetup::slotLength(int len
){
245 if(saver
) saver
->setLines(length
);
248 void kLinesSetup::slotSpeed(int num
){
250 if(saver
) saver
->setSpeed(speed
);
253 void kLinesSetup::slotColstart(const QColor
&col
){
255 if(saver
) saver
->setColor(colstart
, colmid
, colend
);
258 void kLinesSetup::slotColmid(const QColor
&col
){
260 if(saver
) saver
->setColor(colstart
, colmid
, colend
);
263 void kLinesSetup::slotColend(const QColor
&col
){
265 if(saver
) saver
->setColor(colstart
, colmid
, colend
);
268 void kLinesSetup::slotHelp(){
269 KMessageBox::about(this,
270 i18n("Lines Version 2.2.0\n\n"
271 "Written by Dirk Staneker 1997\n"
272 "dirk.stanerker@student.uni-tuebingen.de"));
275 // Ok pressed - save settings and exit
276 void kLinesSetup::slotOk(){
277 KConfigGroup
config(KGlobal::config(), "Settings");
280 slength
.setNum(length
);
281 config
.writeEntry("Length", slength
);
284 sspeed
.setNum( speed
);
285 config
.writeEntry( "Speed", sspeed
);
287 QString colName0
, colName1
, colName2
;
288 colName0
.sprintf("#%02x%02x%02x", colstart
.red(),
289 colstart
.green(), colstart
.blue() );
290 config
.writeEntry( "StartColor", colName0
);
292 colName1
.sprintf("#%02x%02x%02x", colmid
.red(),
293 colmid
.green(), colmid
.blue() );
294 config
.writeEntry( "MidColor", colName1
);
296 colName2
.sprintf("#%02x%02x%02x", colend
.red(),
297 colend
.green(), colend
.blue() );
298 config
.writeEntry( "EndColor", colName2
);
304 //-----------------------------------------------------------------------------
307 kLinesSaver::kLinesSaver( WId id
) : KScreenSaver( id
){
309 lines
=new Lines(numLines
);
313 connect(&timer
, SIGNAL(timeout()), SLOT(slotTimeout()));
316 palette
.setColor( backgroundRole(), Qt::black
);
317 setPalette( palette
);
322 kLinesSaver::~kLinesSaver(){
327 // set lines properties
328 void kLinesSaver::setLines(int len
){
337 void kLinesSaver::setSpeed(int spd
){
343 void kLinesSaver::setColor(const QColor
& cs
, const QColor
& cm
, const QColor
& ce
){
350 // read configuration settings from config file
351 void kLinesSaver::readSettings(){
352 KConfigGroup
config(KGlobal::config(), "Settings");
354 numLines
=config
.readEntry("Length", 10);
355 speed
= 100- config
.readEntry("Speed", 50);
356 if(numLines
>MAXLENGTH
) numLines
=MAXLENGTH
;
357 else if(numLines
<1) numLines
= 1;
359 colstart
=config
.readEntry("StartColor", QColor(Qt::white
));
360 colmid
=config
.readEntry("MidColor", QColor(Qt::blue
));
361 colend
=config
.readEntry("EndColor", QColor(Qt::black
));
364 // draw next lines and erase tail
365 void kLinesSaver::slotTimeout(){
374 p.setPen( Qt::black );
376 for(i=0; i<numLines; i++){
377 lines->getKoord(x1,y1,x2,y2);
378 p.drawLine( x1, y1, x2, y2 );
379 p.setPen( colors[col] );
380 col=(int)(i*colscale);
383 lines->turn(width(), height());
387 void kLinesSaver::paintEvent(QPaintEvent
*)
389 setAttribute( Qt::WA_NoSystemBackground
); // Only after initial clear
398 p
.setPen( Qt::black
);
400 for(i
=0; i
<numLines
; i
++){
401 lines
->getKoord(x1
,y1
,x2
,y2
);
402 p
.drawLine( x1
, y1
, x2
, y2
);
403 p
.setPen( colors
[col
] );
404 col
=(int)(i
*colscale
);
407 lines
->turn(width(), height());
411 // initialise the lines
412 void kLinesSaver::initialiseLines(){
416 lines
=new Lines(numLines
);
418 x1
=rnd
.getLong(width());
419 y1
=rnd
.getLong(height());
420 x2
=rnd
.getLong(width());
421 y2
=rnd
.getLong(height());
422 for(i
=0; i
<numLines
; i
++){
423 lines
->setKoord(x1
,y1
,x2
,y2
);
428 // create a color table of 64 colors
429 void kLinesSaver::initialiseColor(){
433 mr
=(double)(colmid
.red()-colstart
.red())/32;
434 mg
=(double)(colmid
.green()-colstart
.green())/32;
435 mb
=(double)(colmid
.blue()-colstart
.blue())/32;
440 colors
[63-i
].setRgb((int)(mr
*i
+cr
), (int)(mg
*i
+cg
), (int)(mb
*i
+cb
));
442 mr
=(double)(colend
.red()-colmid
.red())/32;
443 mg
=(double)(colend
.green()-colmid
.green())/32;
444 mb
=(double)(colend
.blue()-colmid
.blue())/32;
449 colors
[31-1].setRgb((int)(mr
*i
+cr
), (int)(mg
*i
+cg
), (int)(mb
*i
+cb
));
451 colscale
=64.0/(double)numLines
;