delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kcontrol / componentchooser / ktimerdialog.cpp
blob61ef440b69e0f2a8f310af04e73fbd4513a0b7fc
1 /*
2 * This file is part of the KDE Libraries
3 * Copyright (C) 2002 Hamish Rodda <rodda@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "ktimerdialog.h"
24 #include <QLabel>
25 #include <QLayout>
26 #include <QPixmap>
27 #include <QTimer>
29 #include <QtGui/QProgressBar>
31 #include <kwindowsystem.h>
32 #include <kiconloader.h>
34 #include <klocale.h>
35 #include <kdebug.h>
36 #include <kvbox.h>
38 #include "ktimerdialog.moc"
40 KTimerDialog::KTimerDialog( int msec, TimerStyle style, QWidget *parent,
41 const QString &caption,
42 int buttonMask, ButtonCode defaultButton,
43 bool separator,
44 const KGuiItem &user1,
45 const KGuiItem &user2,
46 const KGuiItem &user3 )
47 : KDialog( parent )
49 setCaption( caption );
50 setButtons( (ButtonCodes)buttonMask );
51 setDefaultButton( defaultButton );
52 setButtonFocus( defaultButton ); // setDefaultButton() doesn't do this
53 showButtonSeparator( separator );
54 setButtonGuiItem( User1, user1 );
55 setButtonGuiItem( User2, user2 );
56 setButtonGuiItem( User3, user3 );
58 totalTimer = new QTimer( this );
59 totalTimer->setSingleShot( true );
60 updateTimer = new QTimer( this );
61 updateTimer->setSingleShot( false );
62 msecTotal = msecRemaining = msec;
63 updateInterval = 1000;
64 tStyle = style;
65 KWindowSystem::setIcons( winId(), DesktopIcon("randr"), SmallIcon("randr") );
66 // default to canceling the dialog on timeout
67 if ( buttonMask & Cancel )
68 buttonOnTimeout = Cancel;
70 connect( totalTimer, SIGNAL( timeout() ), SLOT( slotInternalTimeout() ) );
71 connect( updateTimer, SIGNAL( timeout() ), SLOT( slotUpdateTime() ) );
73 // create the widgets
74 mainWidget = new KVBox( this );
75 timerWidget = new KHBox( mainWidget );
76 timerWidget->setSpacing(KDialog::spacingHint());
77 timerLabel = new QLabel( timerWidget );
78 timerProgress = new QProgressBar( timerWidget );
79 timerProgress->setRange( 0, msecTotal );
80 timerProgress->setTextVisible( false );
82 KDialog::setMainWidget( mainWidget );
84 slotUpdateTime( false );
87 KTimerDialog::~KTimerDialog()
91 void KTimerDialog::setVisible( bool visible )
93 KDialog::setVisible( visible );
95 if ( visible ) {
96 totalTimer->start( msecTotal );
97 updateTimer->start( updateInterval );
101 int KTimerDialog::exec()
103 totalTimer->start( msecTotal );
104 updateTimer->start( updateInterval );
105 return KDialog::exec();
108 void KTimerDialog::setMainWidget( QWidget *widget )
110 // yuck, here goes.
111 KVBox *newWidget = new KVBox( this );
113 if ( widget->parentWidget() != mainWidget ) {
114 widget->setParent( newWidget);
116 timerWidget->setParent( newWidget);
118 delete mainWidget;
119 mainWidget = newWidget;
120 KDialog::setMainWidget( mainWidget );
123 void KTimerDialog::setRefreshInterval( int msec )
125 updateInterval = msec;
126 if ( updateTimer->isActive() )
127 updateTimer->start( updateInterval );
130 int KTimerDialog::timeoutButton() const
132 return buttonOnTimeout;
135 void KTimerDialog::setTimeoutButton( const ButtonCode newButton )
137 buttonOnTimeout = newButton;
140 int KTimerDialog::timerStyle() const
142 return tStyle;
145 void KTimerDialog::setTimerStyle( const TimerStyle newStyle )
147 tStyle = newStyle;
150 void KTimerDialog::slotUpdateTime( bool update )
152 if ( update )
153 switch ( tStyle ) {
154 case CountDown:
155 msecRemaining -= updateInterval;
156 break;
157 case CountUp:
158 msecRemaining += updateInterval;
159 break;
160 case Manual:
161 break;
164 timerProgress->setValue( msecRemaining );
166 timerLabel->setText( i18np("1 second remaining:","%1 seconds remaining:",msecRemaining/1000) );
169 void KTimerDialog::slotInternalTimeout()
171 emit timerTimeout();
172 slotButtonClicked( buttonOnTimeout );