Deprecated--
[kdeartwork.git] / kscreensaver / kdesavers / lines.cpp
blobe7162294270c672fcee93249383161061b3d4544
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 <config.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <qcolor.h>
15 #include <qlabel.h>
16 #include <qslider.h>
17 //Added by qt3to4:
18 #include <QVBoxLayout>
19 #include <QHBoxLayout>
20 #include <kconfig.h>
21 #include <kapplication.h>
22 #include <kmessagebox.h>
23 #include <kcolorbutton.h>
25 #include "kcolordialog.h"
26 #include "lines.h"
27 #include "lines.moc"
29 #include <qlayout.h>
30 #include <klocale.h>
31 #include <kglobal.h>
32 #include <qpainter.h>
34 #define MAXLENGTH 256
36 // libkscreensaver interface
37 class kLinesSaverInterface : public KScreenSaverInterface
41 public:
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
65 Lines::Lines(int x){
66 uint i;
67 numLn=x;
68 offx1=12;
69 offy1=16;
70 offx2=9;
71 offy2=10;
72 start=new Ln;
73 end=start;
74 for(i=1; i<numLn; i++){
75 end->next=new Ln;
76 end=end->next;
78 end->next=start;
79 akt=start;
82 Lines::~Lines(){
83 uint i;
84 for(i=0; i<numLn; i++){
85 end=start->next;
86 delete start;
87 start=end;
91 inline void Lines::reset(){ akt=start; }
93 inline void Lines::getKoord(int& a, int& b, int& c, int& d){
94 a=akt->x1; b=akt->y1;
95 c=akt->x2; d=akt->y2;
96 akt=akt->next;
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;
119 end->next=start;
120 start=start->next;
121 end=end->next;
125 //-----------------------------------------------------------------------------
126 // dialog to setup screen saver parameters
128 kLinesSetup::kLinesSetup(QWidget *parent, const char *name)
129 : KDialog(parent)
130 , saver( 0 ), length( 10 ), speed( 50 )
132 setCaption(i18n( "Setup Lines Screen Saver" ));
133 setModal(true);
134 setButtons(Ok|Cancel|Help);
135 setDefaultButton(Ok);
137 readSettings();
139 setButtonText( Help, i18n( "A&bout" ) );
140 QWidget *main = new QWidget(this);
141 setMainWidget(main);
143 QHBoxLayout *tl = new QHBoxLayout(main, 0, spacingHint());
144 QVBoxLayout *tl1 = new QVBoxLayout;
145 tl->addLayout(tl1);
147 QLabel *label=new QLabel(i18n("Length:"), main);
148 tl1->addWidget(label);
150 QSlider *sb= new QSlider(1, MAXLENGTH+1, 16, length, Qt::Horizontal,
151 main);
152 sb->setMinimumSize(120, 20);
153 sb->setTickPosition(QSlider::TicksBelow);
154 sb->setTickInterval(32);
155 connect(sb, SIGNAL(valueChanged(int)), SLOT(slotLength(int)));
156 tl1->addWidget(sb);
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 ) ) );
166 tl1->addWidget(sb);
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);
191 tl1->addStretch();
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()
203 delete saver;
206 // read settings from config file
207 void kLinesSetup::readSettings(){
208 KConfig *config = KGlobal::config();
209 config->setGroup( "Settings" );
211 QString str;
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){
233 length=len;
234 if(saver) saver->setLines(length);
237 void kLinesSetup::slotSpeed(int num){
238 speed=num;
239 if(saver) saver->setSpeed(speed);
242 void kLinesSetup::slotColstart(const QColor &col){
243 colstart = col;
244 if(saver) saver->setColor(colstart, colmid, colend);
247 void kLinesSetup::slotColmid(const QColor &col){
248 colmid = col;
249 if(saver) saver->setColor(colstart, colmid, colend);
252 void kLinesSetup::slotColend(const QColor &col){
253 colend = 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");
269 QString slength;
270 slength.setNum(length);
271 config->writeEntry("Length", slength);
273 QString sspeed;
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 );
290 config->sync();
291 accept();
294 //-----------------------------------------------------------------------------
297 kLinesSaver::kLinesSaver( WId id ) : KScreenSaver( id ){
298 readSettings();
299 lines=new Lines(numLines);
300 blank();
301 initialiseColor();
302 initialiseLines();
303 timer.start(speed);
304 connect(&timer, SIGNAL(timeout()), SLOT(slotTimeout()));
307 kLinesSaver::~kLinesSaver(){
308 timer.stop();
309 delete lines;
312 // set lines properties
313 void kLinesSaver::setLines(int len){
314 timer.stop();
315 numLines=len;
316 initialiseLines();
317 initialiseColor();
318 blank();
319 timer.start(speed);
322 // set the speed
323 void kLinesSaver::setSpeed(int spd){
324 timer.stop();
325 speed=100-spd;
326 timer.start(speed);
329 void kLinesSaver::setColor(const QColor& cs, const QColor& cm, const QColor& ce){
330 colstart=cs;
331 colmid=cm;
332 colend=ce;
333 initialiseColor();
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(){
353 uint i;
354 int x1,y1,x2,y2;
355 int col=0;
357 lines->reset();
358 QPainter p( this );
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);
366 if(col>63) col=0;
368 lines->turn(width(), height());
371 void kLinesSaver::blank(){
372 setBackgroundColor( Qt::black );
373 update();
376 // initialise the lines
377 void kLinesSaver::initialiseLines(){
378 uint i;
379 int x1,y1,x2,y2;
380 delete lines;
381 lines=new Lines(numLines);
382 lines->reset();
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);
389 lines->next();
393 // create a color table of 64 colors
394 void kLinesSaver::initialiseColor(){
395 int i;
396 double mr, mg, mb;
397 double cr, cg, cb;
398 mr=(double)(colmid.red()-colstart.red())/32;
399 mg=(double)(colmid.green()-colstart.green())/32;
400 mb=(double)(colmid.blue()-colstart.blue())/32;
401 cr=colstart.red();
402 cg=colstart.green();
403 cb=colstart.blue();
404 for(i=0; i<32; i++){
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;
410 cr=colmid.red();
411 cg=colmid.green();
412 cb=colmid.blue();
413 for(i=0; i<32; i++){
414 colors[31-1].setRgb((int)(mr*i+cr), (int)(mg*i+cg), (int)(mb*i+cb));
416 colscale=64.0/(double)numLines;