not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / randr / ktimerdialog.cpp
blobf4e7d65d32b7f211eeaf713519a338afc1082880
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 <fixx11h.h>
23 #include <QLabel>
24 #include <QLayout>
25 #include <QPixmap>
26 #include <QTimer>
28 #include <QtGui/QProgressBar>
30 #include <kwindowsystem.h>
31 #include <kiconloader.h>
33 #include <klocale.h>
34 #include <kdebug.h>
35 #include <kvbox.h>
37 #include "ktimerdialog.h"
38 #include "ktimerdialog.moc"
40 KTimerDialog::KTimerDialog( int msec, TimerStyle style, QWidget *parent,
41 const char *name, bool modal,
42 const QString &caption,
43 int buttonMask, ButtonCode defaultButton,
44 bool separator,
45 const KGuiItem &user1,
46 const KGuiItem &user2,
47 const KGuiItem &user3 )
48 : KDialog( parent )
50 setObjectName( name );
51 setModal( modal );
52 setCaption( caption );
53 setButtons( (ButtonCodes)buttonMask );
54 setDefaultButton( defaultButton );
55 showButtonSeparator( separator );
56 setButtonGuiItem( User1, user1 );
57 setButtonGuiItem( User2, user2 );
58 setButtonGuiItem( User3, user3 );
60 totalTimer = new QTimer( this );
61 totalTimer->setSingleShot( true );
62 updateTimer = new QTimer( this );
63 updateTimer->setSingleShot( false );
64 msecTotal = msecRemaining = msec;
65 updateInterval = 1000;
66 tStyle = style;
67 KWindowSystem::setIcons( winId(), DesktopIcon("preferences-desktop-display-randr"), SmallIcon("preferences-desktop-display-randr") );
68 // default to canceling the dialog on timeout
69 if ( buttonMask & Cancel )
70 buttonOnTimeout = Cancel;
72 connect( totalTimer, SIGNAL( timeout() ), SLOT( slotInternalTimeout() ) );
73 connect( updateTimer, SIGNAL( timeout() ), SLOT( slotUpdateTime() ) );
75 // create the widgets
76 mainWidget = new KVBox( this );
77 timerWidget = new KHBox( mainWidget );
78 timerLabel = new QLabel( timerWidget );
79 timerProgress = new QProgressBar( timerWidget );
80 timerProgress->setRange( 0, msecTotal );
81 timerProgress->setTextVisible( false );
83 KDialog::setMainWidget( mainWidget );
85 slotUpdateTime( false );
88 KTimerDialog::~KTimerDialog()
92 void KTimerDialog::setVisible( bool visible )
94 KDialog::setVisible( visible );
96 if ( visible ) {
97 totalTimer->start( msecTotal );
98 updateTimer->start( updateInterval );
102 int KTimerDialog::exec()
104 totalTimer->start( msecTotal );
105 updateTimer->start( updateInterval );
106 return KDialog::exec();
109 void KTimerDialog::setMainWidget( QWidget *widget )
111 // yuck, here goes.
112 KVBox *newWidget = new KVBox( this );
114 if ( widget->parentWidget() != mainWidget ) {
115 widget->setParent( newWidget);
117 timerWidget->setParent( newWidget);
119 delete mainWidget;
120 mainWidget = newWidget;
121 KDialog::setMainWidget( mainWidget );
124 void KTimerDialog::setRefreshInterval( int msec )
126 updateInterval = msec;
127 if ( updateTimer->isActive() )
128 updateTimer->start( updateInterval );
131 int KTimerDialog::timeoutButton() const
133 return buttonOnTimeout;
136 void KTimerDialog::setTimeoutButton( const ButtonCode newButton )
138 buttonOnTimeout = newButton;
141 int KTimerDialog::timerStyle() const
143 return tStyle;
146 void KTimerDialog::setTimerStyle( const TimerStyle newStyle )
148 tStyle = newStyle;
151 void KTimerDialog::slotUpdateTime( bool update )
153 if ( update )
154 switch ( tStyle ) {
155 case CountDown:
156 msecRemaining -= updateInterval;
157 break;
158 case CountUp:
159 msecRemaining += updateInterval;
160 break;
161 case Manual:
162 break;
165 timerProgress->setValue( msecRemaining );
167 timerLabel->setText( i18np("1 second remaining:","%1 seconds remaining:",msecRemaining/1000) );
170 void KTimerDialog::slotInternalTimeout()
172 emit timerTimeout();
173 switch ( buttonOnTimeout ) {
174 case Help:
175 slotButtonClicked(KDialog::Help);
176 break;
177 case Default:
178 slotButtonClicked(KDialog::Default);
179 break;
180 case Ok:
181 slotButtonClicked(KDialog::Ok);
182 break;
183 case Apply:
184 slotButtonClicked(KDialog::Apply);
185 break;
186 case Try:
187 slotButtonClicked(KDialog::Try);
188 break;
189 case Cancel:
190 slotButtonClicked(KDialog::Cancel);
191 break;
192 case Close:
193 slotButtonClicked(KDialog::Close);
194 break;
195 case User1:
196 slotButtonClicked(KDialog::User1);
197 break;
198 case User2:
199 slotButtonClicked(KDialog::User2);
200 break;
201 case User3:
202 slotButtonClicked(KDialog::User3);
203 break;
204 case No:
205 slotButtonClicked(KDialog::No);
206 break;
207 case Yes:
208 slotButtonClicked(KDialog::Cancel);
209 break;
210 case Details:
211 slotButtonClicked(KDialog::Details);
212 break;
213 case None:
214 slotButtonClicked(KDialog::None);
215 break;
216 case NoDefault:
217 slotButtonClicked(KDialog::NoDefault);
218 break;