Qt3to4
[kdeartwork.git] / kscreensaver / kdesavers / banner.cpp
blob87136a735208c3904c9eeb08e3ea7c960c037dbc
1 //-----------------------------------------------------------------------------
2 //
3 // kbanner - Basic screen saver for KDE
4 //
5 // Copyright (c) Martin R. Jones 1996
6 //
7 // layout management added 1998/04/19 by Mario Weilguni <mweilguni@kde.org>
8 // clock function and color cycling added 2000/01/09 by Alexander Neundorf <alexander.neundorf@rz.tu-ilmenau.de>
9 // 2001/03/04 Converted to use libkscreensaver by Martin R. Jones
10 // 2002/04/07 Added random vertical position of text,
11 // changed horizontal step size to reduce jerkyness,
12 // text will return to right margin immediately (and not be drawn half a screen width off-screen)
13 // Harald H.-J. Bongartz <harald@bongartz.org>
14 // 2003/09/06 Converted to use KDialogBase - Nadeem Hasan <nhasan@kde.org>
15 #include <stdlib.h>
17 #include <qlabel.h>
18 #include <qlineedit.h>
19 #include <qcombobox.h>
20 #include <qcheckbox.h>
21 #include <q3groupbox.h>
22 #include <qslider.h>
23 #include <qlayout.h>
24 #include <qdatetime.h>
25 #include <qfontdatabase.h>
26 #include <qpainter.h>
27 //Added by qt3to4:
28 #include <QPixmap>
29 #include <QGridLayout>
30 #include <QHBoxLayout>
31 #include <QVBoxLayout>
33 #include <kapplication.h>
34 #include <krandomsequence.h>
35 #include <kglobal.h>
36 #include <klocale.h>
37 #include <kconfig.h>
38 #include <kmessagebox.h>
39 #include <kcolorbutton.h>
40 #include <kfontcombo.h>
42 #include "banner.h"
43 #include "banner.moc"
44 #include <QDesktopWidget>
45 // libkscreensaver interface
46 extern "C"
48 KDE_EXPORT const char *kss_applicationName = "kbanner.kss";
49 KDE_EXPORT const char *kss_description = I18N_NOOP( "KBanner" );
50 KDE_EXPORT const char *kss_version = "2.2.0";
52 KDE_EXPORT KScreenSaver *kss_create( WId id )
54 return new KBannerSaver( id );
57 KDE_EXPORT QDialog *kss_setup()
59 return new KBannerSetup();
63 //-----------------------------------------------------------------------------
65 KBannerSetup::KBannerSetup( QWidget *parent, const char *name )
66 : KDialogBase( parent, name, true, i18n( "Setup Banner Screen Saver" ),
67 Ok|Cancel|Help, Ok, true ), saver( 0 ), ed(0), speed( 50 )
69 setButtonText( Help, i18n( "A&bout" ) );
70 readSettings();
72 QWidget *main = makeMainWidget();
74 QLabel *label;
76 QVBoxLayout *tl = new QVBoxLayout(main, 0, spacingHint());
77 QHBoxLayout *tl1 = new QHBoxLayout( 0, 0, spacingHint() );
78 tl->addLayout(tl1);
79 QVBoxLayout *tl11 = new QVBoxLayout( 0, 0, spacingHint() );
80 tl1->addLayout(tl11);
82 Q3GroupBox *group = new Q3GroupBox( 0, Vertical, i18n("Font"), main );
83 QGridLayout *gl = new QGridLayout(group->layout(), 6, 2, spacingHint() );
85 label = new QLabel( i18n("Family:"), group );
86 gl->addWidget(label, 1, 0);
88 KFontCombo* comboFonts = new KFontCombo( QFontDatabase().families(), group );
89 comboFonts->setCurrentFont( fontFamily );
90 gl->addWidget(comboFonts, 1, 1);
91 connect( comboFonts, SIGNAL( activated( const QString& ) ),
92 SLOT( slotFamily( const QString& ) ) );
94 label = new QLabel( i18n("Size:"), group );
95 gl->addWidget(label, 2, 0);
97 comboSizes = new QComboBox( TRUE, group );
98 fillFontSizes();
99 gl->addWidget(comboSizes, 2, 1);
100 connect( comboSizes, SIGNAL( activated( int ) ), SLOT( slotSize( int ) ) );
101 connect( comboSizes, SIGNAL( textChanged( const QString & ) ),
102 SLOT( slotSizeEdit( const QString & ) ) );
104 QCheckBox *cb = new QCheckBox( i18n("Bold"),
105 group );
106 cb->setChecked( bold );
107 connect( cb, SIGNAL( toggled( bool ) ), SLOT( slotBold( bool ) ) );
108 gl->addWidget(cb, 3, 0);
110 cb = new QCheckBox( i18n("Italic"), group );
111 cb->setChecked( italic );
112 gl->addWidget(cb, 3, 1);
113 connect( cb, SIGNAL( toggled( bool ) ), SLOT( slotItalic( bool ) ) );
115 label = new QLabel( i18n("Color:"), group );
116 gl->addWidget(label, 4, 0);
118 colorPush = new KColorButton( fontColor, group );
119 gl->addWidget(colorPush, 4, 1);
120 connect( colorPush, SIGNAL( changed(const QColor &) ),
121 SLOT( slotColor(const QColor &) ) );
123 QCheckBox *cyclingColorCb=new QCheckBox(i18n("Cycling color"),group);
124 cyclingColorCb->setMinimumSize(cyclingColorCb->sizeHint());
125 gl->addMultiCellWidget(cyclingColorCb,5,5,0,1);
126 connect(cyclingColorCb,SIGNAL(toggled(bool)),this,SLOT(slotCyclingColor(bool)));
127 cyclingColorCb->setChecked(cyclingColor);
129 preview = new QWidget( main );
130 preview->setFixedSize( 220, 170 );
131 preview->setBackgroundColor( Qt::black );
132 preview->show(); // otherwise saver does not get correct size
133 saver = new KBannerSaver( preview->winId() );
134 tl1->addWidget(preview);
136 tl11->addWidget(group);
138 label = new QLabel( i18n("Speed:"), main );
139 tl11->addStretch(1);
140 tl11->addWidget(label);
142 QSlider *sb = new QSlider(0, 100, 10, speed, QSlider::Horizontal, main );
143 sb->setMinimumWidth( 180);
144 sb->setFixedHeight(20);
145 sb->setTickmarks(QSlider::Below);
146 sb->setTickInterval(10);
147 tl11->addWidget(sb);
148 connect( sb, SIGNAL( valueChanged( int ) ), SLOT( slotSpeed( int ) ) );
150 QHBoxLayout *tl2 = new QHBoxLayout;
151 tl->addLayout(tl2);
153 label = new QLabel( i18n("Message:"), main );
154 tl2->addWidget(label);
156 ed = new QLineEdit( main );
157 tl2->addWidget(ed);
158 ed->setText( message );
159 connect( ed, SIGNAL( textChanged( const QString & ) ),
160 SLOT( slotMessage( const QString & ) ) );
162 QCheckBox *timeCb=new QCheckBox( i18n("Show current time"), main);
163 timeCb->setFixedSize(timeCb->sizeHint());
164 tl->addWidget(timeCb,0,Qt::AlignLeft);
165 connect(timeCb,SIGNAL(toggled(bool)),this,SLOT(slotTimeToggled(bool)));
166 timeCb->setChecked(showTime);
168 tl->addStretch();
171 // read settings from config file
172 void KBannerSetup::readSettings()
174 KConfig *config = KGlobal::config();
175 config->setGroup( "Settings" );
177 speed=config->readNumEntry("Speed",50);
178 /* if ( speed > 100 )
179 speed = 100;
180 else if ( speed < 50 )
181 speed = 50;*/
183 message=config->readEntry("Message","KDE");
184 showTime=config->readBoolEntry("ShowTime",FALSE);
185 fontFamily=config->readEntry("FontFamily",(QApplication::font()).family());
186 fontSize=config->readNumEntry("FontSize",48);
187 fontColor.setNamedColor(config->readEntry("FontColor","red"));
188 cyclingColor=config->readBoolEntry("CyclingColor",FALSE);
189 bold=config->readBoolEntry("FontBold",FALSE);
190 italic=config->readBoolEntry("FontItalic",FALSE);
193 void KBannerSetup::fillFontSizes()
195 bool block = comboSizes->signalsBlocked();
196 comboSizes->blockSignals( true );
197 comboSizes->clear();
198 int i = 0;
199 sizes = QFontDatabase().pointSizes( fontFamily );
200 sizes << 96 << 128 << 156 << 0;
201 int current = 0;
202 while ( sizes[i] )
204 QString num;
205 num.setNum( sizes[i] );
206 comboSizes->insertItem( num, i );
207 if ( fontSize == sizes[i] ) // fontsize equals one of the defined ones
209 current = i;
210 comboSizes->setCurrentItem( current );
211 slotSize( current );
213 i++;
215 if ( current == 0 ) // fontsize seems to be entered by hand
217 QString fsize;
218 fsize.setNum( fontSize );
219 comboSizes->setEditText(fsize);
220 slotSizeEdit( fsize );
222 comboSizes->blockSignals( block );
225 void KBannerSetup::slotFamily( const QString& fam )
227 fontFamily = fam;
228 fillFontSizes(); // different font, different sizes
229 if ( saver )
230 saver->setFont( fontFamily, fontSize, fontColor, bold, italic );
233 void KBannerSetup::slotSize( int indx )
235 fontSize = sizes[indx];
236 if ( saver )
237 saver->setFont( fontFamily, fontSize, fontColor, bold, italic );
240 void KBannerSetup::slotSizeEdit( const QString& fs )
242 bool ok;
243 fontSize = fs.toInt( &ok, 10 );
244 if ( ok )
245 if ( saver )
246 saver->setFont( fontFamily, fontSize, fontColor, bold, italic );
249 void KBannerSetup::slotColor( const QColor &col )
251 fontColor = col;
252 if ( saver )
253 saver->setColor(fontColor);
256 void KBannerSetup::slotCyclingColor(bool on)
258 colorPush->setEnabled(!on);
259 cyclingColor=on;
261 if ( saver )
263 saver->setCyclingColor( on );
264 if ( !on )
265 saver->setColor( fontColor );
269 void KBannerSetup::slotBold( bool state )
271 bold = state;
272 if ( saver )
273 saver->setFont( fontFamily, fontSize, fontColor, bold, italic );
276 void KBannerSetup::slotItalic( bool state )
278 italic = state;
279 if ( saver )
280 saver->setFont( fontFamily, fontSize, fontColor, bold, italic );
283 void KBannerSetup::slotSpeed( int num )
285 speed = num;
286 if ( saver )
287 saver->setSpeed( speed );
290 void KBannerSetup::slotMessage( const QString &msg )
292 message = msg;
293 if ( saver )
294 saver->setMessage( message );
297 void KBannerSetup::slotTimeToggled( bool on )
299 ed->setEnabled(!on);
300 showTime=on;
301 if (saver)
303 if (showTime)
304 saver->setTimeDisplay();
305 else
307 message=ed->text();
308 saver->setMessage(message);
313 // Ok pressed - save settings and exit
314 void KBannerSetup::slotOk()
316 KConfig *config = KGlobal::config();
317 config->setGroup( "Settings" );
319 config->writeEntry( "Speed", speed );
320 config->writeEntry( "Message", message );
321 config->writeEntry( "ShowTime", showTime );
322 config->writeEntry( "FontFamily", fontFamily );
324 QString fsize;
325 if (fontSize == 0) // an non-number was entered in the font size combo
327 fontSize = 48;
329 fsize.setNum( fontSize );
330 config->writeEntry( "FontSize", fsize );
332 QString colName;
333 colName.sprintf( "#%02x%02x%02x", fontColor.red(), fontColor.green(),
334 fontColor.Qt::blue() );
335 config->writeEntry( "FontColor", colName );
336 config->writeEntry( "CyclingColor", cyclingColor );
337 config->writeEntry( "FontBold", bold );
338 config->writeEntry( "FontItalic", italic );
340 config->sync();
342 accept();
345 void KBannerSetup::slotHelp()
347 KMessageBox::about(this,
348 i18n("Banner Version 2.2.1\n\nWritten by Martin R. Jones 1996\nmjones@kde.org\nExtended by Alexander Neundorf 2000\nalexander.neundorf@rz.tu-ilmenau.de\n"));
351 //-----------------------------------------------------------------------------
353 KBannerSaver::KBannerSaver( WId id ) : KScreenSaver( id )
355 krnd = new KRandomSequence();
356 readSettings();
357 initialize();
358 colorContext = QColor::enterAllocContext();
359 needBlank = TRUE;
360 timer.start( speed );
361 connect( &timer, SIGNAL( timeout() ), SLOT( slotTimeout() ) );
364 KBannerSaver::~KBannerSaver()
366 timer.stop();
367 QColor::leaveAllocContext();
368 QColor::destroyAllocContext( colorContext );
369 delete krnd;
372 void KBannerSaver::setSpeed( int spd )
374 timer.stop();
375 int inv = 100 - spd;
376 speed = 1 + ((inv * inv) / 100);
377 timer.start( speed );
380 void KBannerSaver::setFont( const QString& family, int size, const QColor &color,
381 bool b, bool i )
383 fontFamily = family;
384 fontSize = size;
385 fontColor = color;
386 bold = b;
387 italic = i;
389 initialize();
392 void KBannerSaver::setColor(QColor &color)
394 fontColor = color;
395 cyclingColor = FALSE;
396 needUpdate = TRUE;
399 void KBannerSaver::setCyclingColor( bool on )
401 cyclingColor = on;
402 needUpdate = TRUE;
405 void KBannerSaver::setMessage( const QString &msg )
407 showTime = FALSE;
408 message = msg;
409 pixmapSize = QSize();
410 needBlank = TRUE;
413 void KBannerSaver::setTimeDisplay()
415 showTime = TRUE;
416 pixmapSize = QSize();
417 needBlank = TRUE;
420 // read settings from config file
421 void KBannerSaver::readSettings()
423 KConfig *config = KGlobal::config();
424 config->setGroup( "Settings" );
426 setSpeed( config->readNumEntry("Speed",50) );
428 message=config->readEntry("Message","KDE");
430 showTime=config->readBoolEntry("ShowTime",FALSE);
432 fontFamily=config->readEntry("FontFamily",(QApplication::font()).family());
434 fontSize=config->readNumEntry("FontSize",48);
436 fontColor.setNamedColor(config->readEntry("FontColor","red"));
438 cyclingColor=config->readBoolEntry("CyclingColor",FALSE);
440 bold=config->readBoolEntry("FontBold",FALSE);
441 italic=config->readBoolEntry("FontItalic",FALSE);
443 if ( cyclingColor )
445 currentHue=0;
446 fontColor.setHsv(0,SATURATION,VALUE);
450 // initialize font
451 void KBannerSaver::initialize()
453 fsize = fontSize * height() / QApplication::desktop()->height();
455 font = QFont( fontFamily, fsize, bold ? QFont::Bold : QFont::Normal, italic );
457 pixmapSize = QSize();
458 needBlank = TRUE;
460 xpos = width();
461 ypos = fsize + (int) ((double)(height()-fsize)*krnd->getDouble());
462 step = 2 * width() / QApplication::desktop()->width(); // 6 -> 2 -hhjb-
463 if ( step == 0 )
464 step = 1;
467 // erase old text and draw in new position
468 void KBannerSaver::slotTimeout()
470 if (cyclingColor)
472 int hueStep = speed/10;
473 currentHue=(currentHue+hueStep)%360;
474 fontColor.setHsv(currentHue,SATURATION,VALUE);
476 if (showTime)
478 QString new_message = KGlobal::locale()->formatTime(QTime::currentTime(), true);
479 if( new_message != message )
480 needUpdate = TRUE;
481 message = new_message;
483 if ( !pixmapSize.isValid() || cyclingColor || needUpdate || needBlank )
485 QRect rect = QFontMetrics( font ).boundingRect( message );
486 rect.setWidth( rect.width() + step );
487 if ( rect.width() > pixmapSize.width() )
488 pixmapSize.setWidth( rect.width() );
489 if ( rect.height() > pixmapSize.height() )
490 pixmapSize.setHeight( rect.height() );
491 pixmap = QPixmap( pixmapSize );
492 pixmap.fill( Qt::black );
493 QPainter p( &pixmap );
494 p.setFont( font );
495 p.setPen( fontColor );
496 p.drawText( -rect.x(), -rect.y(), message );
497 needUpdate = FALSE;
499 xpos -= step;
500 if ( xpos < -pixmapSize.width() ) {
501 QPainter p( this );
502 p.fillRect( xpos + step, ypos, pixmapSize.width(), pixmapSize.height(), Qt::black );
503 xpos = width();
504 ypos = fsize + (int) ((double)(height()-2.0*fsize)*krnd->getDouble());
507 if ( needBlank )
509 setBackgroundColor( Qt::black );
510 erase();
511 needBlank = FALSE;
513 bitBlt( this, xpos, ypos, &pixmap, 0, 0, pixmapSize.width(), pixmapSize.height());