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.
28 #include <QtGui/QProgressBar>
30 #include <kwindowsystem.h>
31 #include <kiconloader.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
,
45 const KGuiItem
&user1
,
46 const KGuiItem
&user2
,
47 const KGuiItem
&user3
)
50 setObjectName( name
);
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;
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() ) );
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
);
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
)
112 KVBox
*newWidget
= new KVBox( this );
114 if ( widget
->parentWidget() != mainWidget
) {
115 widget
->setParent( newWidget
);
117 timerWidget
->setParent( newWidget
);
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
146 void KTimerDialog::setTimerStyle( const TimerStyle newStyle
)
151 void KTimerDialog::slotUpdateTime( bool update
)
156 msecRemaining
-= updateInterval
;
159 msecRemaining
+= updateInterval
;
165 timerProgress
->setValue( msecRemaining
);
167 timerLabel
->setText( i18np("1 second remaining:","%1 seconds remaining:",msecRemaining
/1000) );
170 void KTimerDialog::slotInternalTimeout()
173 switch ( buttonOnTimeout
) {
175 slotButtonClicked(KDialog::Help
);
178 slotButtonClicked(KDialog::Default
);
181 slotButtonClicked(KDialog::Ok
);
184 slotButtonClicked(KDialog::Apply
);
187 slotButtonClicked(KDialog::Try
);
190 slotButtonClicked(KDialog::Cancel
);
193 slotButtonClicked(KDialog::Close
);
196 slotButtonClicked(KDialog::User1
);
199 slotButtonClicked(KDialog::User2
);
202 slotButtonClicked(KDialog::User3
);
205 slotButtonClicked(KDialog::No
);
208 slotButtonClicked(KDialog::Cancel
);
211 slotButtonClicked(KDialog::Details
);
214 slotButtonClicked(KDialog::None
);
217 slotButtonClicked(KDialog::NoDefault
);