1 //-----------------------------------------------------------------------------
3 // kbanner - 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 // 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>
18 #include <qlineedit.h>
19 #include <qcombobox.h>
20 #include <qcheckbox.h>
21 #include <q3groupbox.h>
24 #include <qdatetime.h>
25 #include <qfontdatabase.h>
29 #include <QGridLayout>
30 #include <QHBoxLayout>
31 #include <QVBoxLayout>
33 #include <kapplication.h>
34 #include <krandomsequence.h>
38 #include <kmessagebox.h>
39 #include <kcolorbutton.h>
40 #include <kfontcombo.h>
44 #include <QDesktopWidget>
45 // libkscreensaver interface
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" ) );
72 QWidget
*main
= makeMainWidget();
76 QVBoxLayout
*tl
= new QVBoxLayout(main
, 0, spacingHint());
77 QHBoxLayout
*tl1
= new QHBoxLayout( 0, 0, spacingHint() );
79 QVBoxLayout
*tl11
= new QVBoxLayout( 0, 0, spacingHint() );
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
);
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"),
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
);
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);
148 connect( sb
, SIGNAL( valueChanged( int ) ), SLOT( slotSpeed( int ) ) );
150 QHBoxLayout
*tl2
= new QHBoxLayout
;
153 label
= new QLabel( i18n("Message:"), main
);
154 tl2
->addWidget(label
);
156 ed
= new QLineEdit( main
);
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
);
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 )
180 else if ( 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 );
199 sizes
= QFontDatabase().pointSizes( fontFamily
);
200 sizes
<< 96 << 128 << 156 << 0;
205 num
.setNum( sizes
[i
] );
206 comboSizes
->insertItem( num
, i
);
207 if ( fontSize
== sizes
[i
] ) // fontsize equals one of the defined ones
210 comboSizes
->setCurrentItem( current
);
215 if ( current
== 0 ) // fontsize seems to be entered by hand
218 fsize
.setNum( fontSize
);
219 comboSizes
->setEditText(fsize
);
220 slotSizeEdit( fsize
);
222 comboSizes
->blockSignals( block
);
225 void KBannerSetup::slotFamily( const QString
& fam
)
228 fillFontSizes(); // different font, different sizes
230 saver
->setFont( fontFamily
, fontSize
, fontColor
, bold
, italic
);
233 void KBannerSetup::slotSize( int indx
)
235 fontSize
= sizes
[indx
];
237 saver
->setFont( fontFamily
, fontSize
, fontColor
, bold
, italic
);
240 void KBannerSetup::slotSizeEdit( const QString
& fs
)
243 fontSize
= fs
.toInt( &ok
, 10 );
246 saver
->setFont( fontFamily
, fontSize
, fontColor
, bold
, italic
);
249 void KBannerSetup::slotColor( const QColor
&col
)
253 saver
->setColor(fontColor
);
256 void KBannerSetup::slotCyclingColor(bool on
)
258 colorPush
->setEnabled(!on
);
263 saver
->setCyclingColor( on
);
265 saver
->setColor( fontColor
);
269 void KBannerSetup::slotBold( bool state
)
273 saver
->setFont( fontFamily
, fontSize
, fontColor
, bold
, italic
);
276 void KBannerSetup::slotItalic( bool state
)
280 saver
->setFont( fontFamily
, fontSize
, fontColor
, bold
, italic
);
283 void KBannerSetup::slotSpeed( int num
)
287 saver
->setSpeed( speed
);
290 void KBannerSetup::slotMessage( const QString
&msg
)
294 saver
->setMessage( message
);
297 void KBannerSetup::slotTimeToggled( bool on
)
304 saver
->setTimeDisplay();
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
);
325 if (fontSize
== 0) // an non-number was entered in the font size combo
329 fsize
.setNum( fontSize
);
330 config
->writeEntry( "FontSize", fsize
);
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
);
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();
358 colorContext
= QColor::enterAllocContext();
360 timer
.start( speed
);
361 connect( &timer
, SIGNAL( timeout() ), SLOT( slotTimeout() ) );
364 KBannerSaver::~KBannerSaver()
367 QColor::leaveAllocContext();
368 QColor::destroyAllocContext( colorContext
);
372 void KBannerSaver::setSpeed( int spd
)
376 speed
= 1 + ((inv
* inv
) / 100);
377 timer
.start( speed
);
380 void KBannerSaver::setFont( const QString
& family
, int size
, const QColor
&color
,
392 void KBannerSaver::setColor(QColor
&color
)
395 cyclingColor
= FALSE
;
399 void KBannerSaver::setCyclingColor( bool on
)
405 void KBannerSaver::setMessage( const QString
&msg
)
409 pixmapSize
= QSize();
413 void KBannerSaver::setTimeDisplay()
416 pixmapSize
= QSize();
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
);
446 fontColor
.setHsv(0,SATURATION
,VALUE
);
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();
461 ypos
= fsize
+ (int) ((double)(height()-fsize
)*krnd
->getDouble());
462 step
= 2 * width() / QApplication::desktop()->width(); // 6 -> 2 -hhjb-
467 // erase old text and draw in new position
468 void KBannerSaver::slotTimeout()
472 int hueStep
= speed
/10;
473 currentHue
=(currentHue
+hueStep
)%360;
474 fontColor
.setHsv(currentHue
,SATURATION
,VALUE
);
478 QString new_message
= KGlobal::locale()->formatTime(QTime::currentTime(), true);
479 if( new_message
!= message
)
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
);
495 p
.setPen( fontColor
);
496 p
.drawText( -rect
.x(), -rect
.y(), message
);
500 if ( xpos
< -pixmapSize
.width() ) {
502 p
.fillRect( xpos
+ step
, ypos
, pixmapSize
.width(), pixmapSize
.height(), Qt::black
);
504 ypos
= fsize
+ (int) ((double)(height()-2.0*fsize
)*krnd
->getDouble());
509 setBackgroundColor( Qt::black
);
513 bitBlt( this, xpos
, ypos
, &pixmap
, 0, 0, pixmapSize
.width(), pixmapSize
.height());