SVN_SILENT made messages (.desktop file)
[kdeartwork.git] / kscreensaver / kdesavers / lines.cpp
blob52cf44a84d9dc03efdc28fc57d9929397373efce
1 //-----------------------------------------------------------------------------
2 //
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
7 //
8 // layout management added 1998/04/19 by Mario Weilguni <mweilguni@kde.org>
9 // 2001/03/04 Converted to libkscreensaver by Martin R. Jones
11 #include <stdlib.h>
12 #include <time.h>
13 #include <qcolor.h>
14 #include <qlabel.h>
15 #include <qslider.h>
16 #include <QVBoxLayout>
17 #include <QHBoxLayout>
18 #include <kconfig.h>
19 #include <kapplication.h>
20 #include <kmessagebox.h>
21 #include <kcolorbutton.h>
23 #include "kcolordialog.h"
24 #include "lines.h"
25 #include "lines.moc"
27 #include <qlayout.h>
28 #include <klocale.h>
29 #include <kglobal.h>
30 #include <qpainter.h>
32 #define MAXLENGTH 256
34 // libkscreensaver interface
35 class kLinesSaverInterface : public KScreenSaverInterface
39 public:
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
63 Lines::Lines(int x){
64 uint i;
65 numLn=x;
66 offx1=12;
67 offy1=16;
68 offx2=9;
69 offy2=10;
70 start=new Ln;
71 end=start;
72 for(i=1; i<numLn; i++){
73 end->next=new Ln;
74 end=end->next;
76 end->next=start;
77 akt=start;
80 Lines::~Lines(){
81 uint i;
82 for(i=0; i<numLn; i++){
83 end=start->next;
84 delete start;
85 start=end;
89 inline void Lines::reset(){ akt=start; }
91 inline void Lines::getKoord(int& a, int& b, int& c, int& d){
92 a=akt->x1; b=akt->y1;
93 c=akt->x2; d=akt->y2;
94 akt=akt->next;
97 inline void Lines::setKoord(const int& a, const int& b, const int& c, const int& d){
98 akt->x1=a; akt->y1=b;
99 akt->x2=c; akt->y2=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;
117 end->next=start;
118 start=start->next;
119 end=end->next;
123 //-----------------------------------------------------------------------------
124 // dialog to setup screen saver parameters
126 kLinesSetup::kLinesSetup(QWidget *parent)
127 : KDialog(parent)
128 , saver( 0 ), length( 10 ), speed( 50 )
130 setCaption(i18n( "Setup Lines Screen Saver" ));
131 setModal(true);
132 setButtons(Ok|Cancel|Help);
133 setDefaultButton(Ok);
135 readSettings();
137 setButtonText( Help, i18n( "A&bout" ) );
138 QWidget *main = new QWidget(this);
139 setMainWidget(main);
141 QHBoxLayout *tl = new QHBoxLayout(main);
142 tl->setSpacing( spacingHint() );
143 QVBoxLayout *tl1 = new QVBoxLayout;
144 tl->addLayout(tl1);
146 QLabel *label=new QLabel(i18n("Length:"), main);
147 tl1->addWidget(label);
149 QSlider *sb= new QSlider(Qt::Horizontal, main);
150 sb->setMinimum(1);
151 sb->setMaximum(MAXLENGTH + 1);
152 sb->setPageStep(16);
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)));
158 tl1->addWidget(sb);
160 label=new QLabel(i18n("Speed:"), main);
161 tl1->addWidget(label);
163 sb = new QSlider(Qt::Horizontal, main);
164 sb->setMinimum(0);
165 sb->setMaximum(100);
166 sb->setPageStep(10);
167 sb->setValue(speed);
168 sb->setMinimumSize(120, 20);
169 sb->setTickPosition(QSlider::TicksBelow);
170 sb->setTickInterval(10);
171 connect( sb, SIGNAL( valueChanged( int ) ), SLOT( slotSpeed( int ) ) );
172 tl1->addWidget(sb);
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);
197 tl1->addStretch();
199 preview = new QWidget( main );
200 preview->setFixedSize( 220, 170 );
202 QPalette palette;
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()
215 delete saver;
218 // read settings from config file
219 void kLinesSetup::readSettings(){
220 KConfigGroup config(KGlobal::config(), "Settings");
222 QString str;
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){
244 length=len;
245 if(saver) saver->setLines(length);
248 void kLinesSetup::slotSpeed(int num){
249 speed=num;
250 if(saver) saver->setSpeed(speed);
253 void kLinesSetup::slotColstart(const QColor &col){
254 colstart = col;
255 if(saver) saver->setColor(colstart, colmid, colend);
258 void kLinesSetup::slotColmid(const QColor &col){
259 colmid = col;
260 if(saver) saver->setColor(colstart, colmid, colend);
263 void kLinesSetup::slotColend(const QColor &col){
264 colend = 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");
279 QString slength;
280 slength.setNum(length);
281 config.writeEntry("Length", slength);
283 QString sspeed;
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 );
300 config.sync();
301 accept();
304 //-----------------------------------------------------------------------------
307 kLinesSaver::kLinesSaver( WId id ) : KScreenSaver( id ){
308 readSettings();
309 lines=new Lines(numLines);
310 initialiseColor();
311 initialiseLines();
312 timer.start(speed);
313 connect(&timer, SIGNAL(timeout()), SLOT(slotTimeout()));
315 QPalette palette;
316 palette.setColor( backgroundRole(), Qt::black );
317 setPalette( palette );
319 update();
322 kLinesSaver::~kLinesSaver(){
323 timer.stop();
324 delete lines;
327 // set lines properties
328 void kLinesSaver::setLines(int len){
329 timer.stop();
330 numLines=len;
331 initialiseLines();
332 initialiseColor();
333 timer.start(speed);
336 // set the speed
337 void kLinesSaver::setSpeed(int spd){
338 timer.stop();
339 speed=100-spd;
340 timer.start(speed);
343 void kLinesSaver::setColor(const QColor& cs, const QColor& cm, const QColor& ce){
344 colstart=cs;
345 colmid=cm;
346 colend=ce;
347 initialiseColor();
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(){
366 update();
368 uint i;
369 int x1,y1,x2,y2;
370 int col=0;
372 lines->reset();
373 QPainter p( this );
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);
381 if(col>63) col=0;
383 lines->turn(width(), height());
387 void kLinesSaver::paintEvent(QPaintEvent *)
389 setAttribute( Qt::WA_NoSystemBackground ); // Only after initial clear
391 uint i;
392 int x1,y1,x2,y2;
393 int col=0;
395 lines->reset();
397 QPainter p( this );
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);
405 if(col>63) col=0;
407 lines->turn(width(), height());
411 // initialise the lines
412 void kLinesSaver::initialiseLines(){
413 uint i;
414 int x1,y1,x2,y2;
415 delete lines;
416 lines=new Lines(numLines);
417 lines->reset();
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);
424 lines->next();
428 // create a color table of 64 colors
429 void kLinesSaver::initialiseColor(){
430 int i;
431 double mr, mg, mb;
432 double cr, cg, cb;
433 mr=(double)(colmid.red()-colstart.red())/32;
434 mg=(double)(colmid.green()-colstart.green())/32;
435 mb=(double)(colmid.blue()-colstart.blue())/32;
436 cr=colstart.red();
437 cg=colstart.green();
438 cb=colstart.blue();
439 for(i=0; i<32; i++){
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;
445 cr=colmid.red();
446 cg=colmid.green();
447 cb=colmid.blue();
448 for(i=0; i<32; i++){
449 colors[31-1].setRgb((int)(mr*i+cr), (int)(mg*i+cg), (int)(mb*i+cb));
451 colscale=64.0/(double)numLines;