Fix connect signal/slot
[kdeartwork.git] / kscreensaver / kdesavers / lines.cpp
blob0ae8a44c01fbc96ff82bc20ad273c52246efa1b5
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);
199 connect(this,SIGNAL(okClicked()),this,SLOT(slotOk()));
200 connect(this,SIGNAL(helpClicked()),this,SLOT(slotHelp()));
203 kLinesSetup::~kLinesSetup()
205 delete saver;
208 // read settings from config file
209 void kLinesSetup::readSettings(){
210 KConfig *config = KGlobal::config();
211 config->setGroup( "Settings" );
213 QString str;
215 length = config->readEntry("Length", length);
216 if(length>MAXLENGTH) length=MAXLENGTH;
217 else if(length<1) length=1;
219 speed = config->readEntry("Speed", speed);
220 if(speed>100) speed=100;
221 else if(speed<50) speed=50;
223 str=config->readEntry("StartColor");
224 if(!str.isNull()) colstart.setNamedColor(str);
225 else colstart=Qt::white;
226 str=config->readEntry("MidColor");
227 if(!str.isNull()) colmid.setNamedColor(str);
228 else colmid=Qt::blue;
229 str=config->readEntry("EndColor");
230 if(!str.isNull()) colend.setNamedColor(str);
231 else colend=Qt::black;
234 void kLinesSetup::slotLength(int len){
235 length=len;
236 if(saver) saver->setLines(length);
239 void kLinesSetup::slotSpeed(int num){
240 speed=num;
241 if(saver) saver->setSpeed(speed);
244 void kLinesSetup::slotColstart(const QColor &col){
245 colstart = col;
246 if(saver) saver->setColor(colstart, colmid, colend);
249 void kLinesSetup::slotColmid(const QColor &col){
250 colmid = col;
251 if(saver) saver->setColor(colstart, colmid, colend);
254 void kLinesSetup::slotColend(const QColor &col){
255 colend = col;
256 if(saver) saver->setColor(colstart, colmid, colend);
259 void kLinesSetup::slotHelp(){
260 KMessageBox::about(this,
261 i18n("Lines Version 2.2.0\n\n"
262 "Written by Dirk Staneker 1997\n"
263 "dirk.stanerker@student.uni-tuebingen.de"));
266 // Ok pressed - save settings and exit
267 void kLinesSetup::slotOk(){
268 KConfig *config = KGlobal::config();
269 config->setGroup("Settings");
271 QString slength;
272 slength.setNum(length);
273 config->writeEntry("Length", slength);
275 QString sspeed;
276 sspeed.setNum( speed );
277 config->writeEntry( "Speed", sspeed );
279 QString colName0, colName1, colName2;
280 colName0.sprintf("#%02x%02x%02x", colstart.red(),
281 colstart.green(), colstart.blue() );
282 config->writeEntry( "StartColor", colName0 );
284 colName1.sprintf("#%02x%02x%02x", colmid.red(),
285 colmid.green(), colmid.blue() );
286 config->writeEntry( "MidColor", colName1 );
288 colName2.sprintf("#%02x%02x%02x", colend.red(),
289 colend.green(), colend.blue() );
290 config->writeEntry( "EndColor", colName2 );
292 config->sync();
293 accept();
296 //-----------------------------------------------------------------------------
299 kLinesSaver::kLinesSaver( WId id ) : KScreenSaver( id ){
300 readSettings();
301 lines=new Lines(numLines);
302 blank();
303 initialiseColor();
304 initialiseLines();
305 timer.start(speed);
306 connect(&timer, SIGNAL(timeout()), SLOT(slotTimeout()));
309 kLinesSaver::~kLinesSaver(){
310 timer.stop();
311 delete lines;
314 // set lines properties
315 void kLinesSaver::setLines(int len){
316 timer.stop();
317 numLines=len;
318 initialiseLines();
319 initialiseColor();
320 blank();
321 timer.start(speed);
324 // set the speed
325 void kLinesSaver::setSpeed(int spd){
326 timer.stop();
327 speed=100-spd;
328 timer.start(speed);
331 void kLinesSaver::setColor(const QColor& cs, const QColor& cm, const QColor& ce){
332 colstart=cs;
333 colmid=cm;
334 colend=ce;
335 initialiseColor();
338 // read configuration settings from config file
339 void kLinesSaver::readSettings(){
340 KConfig *config=KGlobal::config();
341 config->setGroup("Settings");
343 numLines=config->readEntry("Length", 10);
344 speed = 100- config->readEntry("Speed", 50);
345 if(numLines>MAXLENGTH) numLines=MAXLENGTH;
346 else if(numLines<1) numLines = 1;
348 colstart=config->readEntry("StartColor", Qt::white);
349 colmid=config->readEntry("MidColor", Qt::blue);
350 colend=config->readEntry("EndColor", Qt::black);
353 // draw next lines and erase tail
354 void kLinesSaver::slotTimeout(){
355 uint i;
356 int x1,y1,x2,y2;
357 int col=0;
359 lines->reset();
360 QPainter p( this );
361 p.setPen( Qt::black );
363 for(i=0; i<numLines; i++){
364 lines->getKoord(x1,y1,x2,y2);
365 p.drawLine( x1, y1, x2, y2 );
366 p.setPen( colors[col] );
367 col=(int)(i*colscale);
368 if(col>63) col=0;
370 lines->turn(width(), height());
373 void kLinesSaver::blank(){
374 setBackgroundColor( Qt::black );
375 update();
378 // initialise the lines
379 void kLinesSaver::initialiseLines(){
380 uint i;
381 int x1,y1,x2,y2;
382 delete lines;
383 lines=new Lines(numLines);
384 lines->reset();
385 x1=rnd.getLong(width());
386 y1=rnd.getLong(height());
387 x2=rnd.getLong(width());
388 y2=rnd.getLong(height());
389 for(i=0; i<numLines; i++){
390 lines->setKoord(x1,y1,x2,y2);
391 lines->next();
395 // create a color table of 64 colors
396 void kLinesSaver::initialiseColor(){
397 int i;
398 double mr, mg, mb;
399 double cr, cg, cb;
400 mr=(double)(colmid.red()-colstart.red())/32;
401 mg=(double)(colmid.green()-colstart.green())/32;
402 mb=(double)(colmid.blue()-colstart.blue())/32;
403 cr=colstart.red();
404 cg=colstart.green();
405 cb=colstart.blue();
406 for(i=0; i<32; i++){
407 colors[63-i].setRgb((int)(mr*i+cr), (int)(mg*i+cg), (int)(mb*i+cb));
409 mr=(double)(colend.red()-colmid.red())/32;
410 mg=(double)(colend.green()-colmid.green())/32;
411 mb=(double)(colend.blue()-colmid.blue())/32;
412 cr=colmid.red();
413 cg=colmid.green();
414 cb=colmid.blue();
415 for(i=0; i<32; i++){
416 colors[31-1].setRgb((int)(mr*i+cr), (int)(mg*i+cg), (int)(mb*i+cb));
418 colscale=64.0/(double)numLines;