1 //-----------------------------------------------------------------------------
3 // kpolygon - Basic screen saver for KDE
5 // Copyright (c) Martin R. Jones 1996
7 // layout management added 1998/04/19 by Mario Weilguni <mweilguni@kde.org>
8 // 2001/03/04 Converted to libkscreensaver by Martin R. Jones
18 #include <Q3PointArray>
19 #include <QVBoxLayout>
20 #include <QHBoxLayout>
24 #include <kmessagebox.h>
29 #include "polygon.moc"
33 #define MAXVERTICES 19
35 // libkscreensaver interface
36 class KPolygonSaverInterface
: public KScreenSaverInterface
41 virtual KAboutData
* aboutData() {
42 return new KAboutData( "kpolygon.kss", I18N_NOOP( "KPolygon" ), "2.2.0", I18N_NOOP( "KPolygon" ) );
46 virtual KScreenSaver
* create( WId id
)
48 return new kPolygonSaver( id
);
51 virtual QDialog
* setup()
53 return new kPolygonSetup();
57 int main( int argc
, char *argv
[] )
59 KPolygonSaverInterface kss
;
60 return kScreenSaverMain( argc
, argv
, kss
);
63 //-----------------------------------------------------------------------------
64 // dialog to setup screen saver parameters
66 kPolygonSetup::kPolygonSetup( QWidget
*parent
, const char *name
)
68 , saver( 0 ), length( 10 ), vertices( 3 ),
71 setCaption(i18n( "Setup Polygon Screen Saver" ));
72 setButtons(Ok
|Cancel
|Help
);
77 QWidget
*main
= new QWidget(this);
79 setButtonText( Help
, i18n( "A&bout" ) );
81 QHBoxLayout
*tl
= new QHBoxLayout(main
, 0, spacingHint());
82 QVBoxLayout
*tl1
= new QVBoxLayout
;
85 QLabel
*label
= new QLabel( i18n("Length:"), main
);
86 tl1
->addWidget(label
);
88 QSlider
*sb
= new QSlider(1, MAXLENGTH
, 10, length
, Qt::Horizontal
,
90 sb
->setMinimumSize( 90, 20 );
91 sb
->setTickPosition(QSlider::TicksBelow
);
92 sb
->setTickInterval(10);
93 connect( sb
, SIGNAL( valueChanged( int ) ), SLOT( slotLength( int ) ) );
96 label
= new QLabel( i18n("Vertices:"), main
);
97 tl1
->addWidget(label
);
99 sb
= new QSlider(3, MAXVERTICES
, 2, vertices
, Qt::Horizontal
, main
);
100 sb
->setMinimumSize( 90, 20 );
101 sb
->setTickPosition(QSlider::TicksBelow
);
102 sb
->setTickInterval(2);
103 connect( sb
, SIGNAL( valueChanged( int ) ), SLOT( slotVertices( int ) ) );
106 label
= new QLabel( i18n("Speed:"), main
);
107 tl1
->addWidget(label
);
109 sb
= new QSlider(0, 100, 10, speed
, Qt::Horizontal
, main
);
110 sb
->setMinimumSize( 90, 20 );
111 sb
->setTickPosition(QSlider::TicksBelow
);
112 sb
->setTickInterval(10);
113 connect( sb
, SIGNAL( valueChanged( int ) ), SLOT( slotSpeed( int ) ) );
117 preview
= new QWidget( main
);
118 preview
->setFixedSize( 220, 170 );
119 preview
->setBackgroundColor( Qt::black
);
120 preview
->show(); // otherwise saver does not get correct size
121 saver
= new kPolygonSaver( preview
->winId() );
122 tl
->addWidget(preview
);
124 setMinimumSize( sizeHint() );
127 kPolygonSetup::~kPolygonSetup()
132 // read settings from config file
133 void kPolygonSetup::readSettings()
135 KConfig
*config
= KGlobal::config();
136 config
->setGroup( "Settings" );
138 length
= config
->readEntry( "Length", length
);
139 if ( length
> MAXLENGTH
)
141 else if ( length
< 1 )
144 vertices
= config
->readEntry( "Vertices", vertices
);
145 if ( vertices
> MAXVERTICES
)
146 vertices
= MAXVERTICES
;
147 else if ( vertices
< 3 )
150 speed
= config
->readEntry( "Speed", speed
);
153 else if ( speed
< 50 )
157 void kPolygonSetup::slotLength( int len
)
161 saver
->setPolygon( length
, vertices
);
164 void kPolygonSetup::slotVertices( int num
)
168 saver
->setPolygon( length
, vertices
);
171 void kPolygonSetup::slotSpeed( int num
)
175 saver
->setSpeed( speed
);
178 // Ok pressed - save settings and exit
179 void kPolygonSetup::slotOk()
181 KConfig
*config
= KGlobal::config();
182 config
->setGroup( "Settings" );
185 slength
.setNum( length
);
186 config
->writeEntry( "Length", slength
);
189 svertices
.setNum( vertices
);
190 config
->writeEntry( "Vertices", svertices
);
193 sspeed
.setNum( speed
);
194 config
->writeEntry( "Speed", sspeed
);
201 void kPolygonSetup::slotHelp()
203 KMessageBox::information(this,
204 i18n("Polygon Version 2.2.0\n\n"\
205 "Written by Martin R. Jones 1996\n"\
209 //-----------------------------------------------------------------------------
212 kPolygonSaver::kPolygonSaver( WId id
) : KScreenSaver( id
)
214 polygons
.setAutoDelete( TRUE
);
218 directions
.resize( numVertices
);
223 initialisePolygons();
225 timer
.start( speed
);
226 connect( &timer
, SIGNAL( timeout() ), SLOT( slotTimeout() ) );
229 kPolygonSaver::~kPolygonSaver()
234 // set polygon properties
235 void kPolygonSaver::setPolygon( int len
, int ver
)
241 directions
.resize( numVertices
);
243 initialisePolygons();
246 timer
.start( speed
);
250 void kPolygonSaver::setSpeed( int spd
)
254 timer
.start( speed
);
257 // read configuration settings from config file
258 void kPolygonSaver::readSettings()
260 KConfig
*config
= KGlobal::config();
261 config
->setGroup( "Settings" );
263 numLines
= config
->readEntry( "Length", 10 );
266 else if ( numLines
< 1 )
269 numVertices
= config
->readEntry( "Vertices", 3 );
270 if ( numVertices
> 20 )
272 else if ( numVertices
< 3 )
275 speed
= 100 - config
->readEntry( "Speed", 50 );
278 // draw next polygon and erase tail
279 void kPolygonSaver::slotTimeout()
282 if ( polygons
.count() > numLines
)
284 p
.setPen( Qt::black
);
285 p
.drawPolyline( *polygons
.first() );
289 p
.setPen( colors
[currentColor
] );
290 p
.drawPolyline( *polygons
.last() );
292 if ( polygons
.count() > numLines
)
293 polygons
.removeFirst();
295 polygons
.append( new Q3PointArray( polygons
.last()->copy() ) );
299 void kPolygonSaver::blank()
301 setBackgroundColor( Qt::black
);
305 // initialise the polygon
306 void kPolygonSaver::initialisePolygons()
310 polygons
.append( new Q3PointArray( numVertices
+ 1 ) );
312 Q3PointArray
&poly
= *polygons
.last();
314 for ( i
= 0; i
< numVertices
; i
++ )
316 poly
.setPoint( i
, rnd
.getLong(width()), rnd
.getLong(height()) );
317 directions
[i
].setX( 16 - rnd
.getLong(8) * 4 );
318 if ( directions
[i
].x() == 0 )
319 directions
[i
].setX( 1 );
320 directions
[i
].setY( 16 - rnd
.getLong(8) * 4 );
321 if ( directions
[i
].y() == 0 )
322 directions
[i
].setY( 1 );
325 poly
.setPoint( i
, poly
.point(0) );
328 // move polygon in current direction and change direction if a border is hit
329 void kPolygonSaver::moveVertices()
332 Q3PointArray
&poly
= *polygons
.last();
334 for ( i
= 0; i
< numVertices
; i
++ )
336 poly
.setPoint( i
, poly
.point(i
) + directions
[i
] );
337 if ( poly
[i
].x() >= (int)width() )
339 directions
[i
].setX( -(rnd
.getLong(4) + 1) * 4 );
340 poly
[i
].setX( (int)width() );
342 else if ( poly
[i
].x() < 0 )
344 directions
[i
].setX( (rnd
.getLong(4) + 1) * 4 );
348 if ( poly
[i
].y() >= (int)height() )
350 directions
[i
].setY( -(rnd
.getLong(4) + 1) * 4 );
351 poly
[i
].setY( height() );
353 else if ( poly
[i
].y() < 0 )
355 directions
[i
].setY( (rnd
.getLong(4) + 1) * 4 );
360 poly
.setPoint( i
, poly
.point(0) );
363 // create a color table of 64 colors
364 void kPolygonSaver::initialiseColor()
366 for ( int i
= 0; i
< 64; i
++ )
368 colors
[i
].setHsv( i
* 360 / 64, 255, 255 );
374 // set foreground color to next in the table
375 void kPolygonSaver::nextColor()
379 if ( currentColor
> 63 )