ktimerdialog.cpp
00001 /* 00002 * This file is part of the KDE Libraries 00003 * Copyright (C) 2002 Hamish Rodda <rodda@kde.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 * 00020 */ 00021 00022 #include <tqhbox.h> 00023 #include <tqlayout.h> 00024 #include <tqvbox.h> 00025 #include <tqtimer.h> 00026 #include <tqprogressbar.h> 00027 #include <tqlabel.h> 00028 00029 #include <twin.h> 00030 #include <kiconloader.h> 00031 00032 #include <tdelocale.h> 00033 #include <kdebug.h> 00034 00035 #include "ktimerdialog.h" 00036 #include "ktimerdialog.moc" 00037 00038 KTimerDialog::KTimerDialog( int msec, TimerStyle style, TQWidget *parent, 00039 const char *name, bool modal, 00040 const TQString &caption, 00041 int buttonMask, ButtonCode defaultButton, 00042 bool separator, 00043 const KGuiItem &user1, 00044 const KGuiItem &user2, 00045 const KGuiItem &user3 ) 00046 : KDialogBase(parent, name, modal, caption, buttonMask, defaultButton, 00047 separator, user1, user2, user3 ) 00048 { 00049 totalTimer = new TQTimer( this ); 00050 updateTimer = new TQTimer( this ); 00051 msecTotal = msecRemaining = msec; 00052 updateInterval = 1000; 00053 tStyle = style; 00054 KWin::setIcons( winId(), DesktopIcon("randr"), SmallIcon("randr") ); 00055 // default to cancelling the dialog on timeout 00056 if ( buttonMask & Cancel ) 00057 buttonOnTimeout = Cancel; 00058 00059 connect( totalTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotInternalTimeout() ) ); 00060 connect( updateTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotUpdateTime() ) ); 00061 00062 // create the widgets 00063 mainWidget = new TQVBox( this, "mainWidget" ); 00064 timerWidget = new TQHBox( mainWidget, "timerWidget" ); 00065 timerLabel = new TQLabel( timerWidget ); 00066 timerProgress = new TQProgressBar( timerWidget ); 00067 timerProgress->setTotalSteps( msecTotal ); 00068 timerProgress->setPercentageVisible( false ); 00069 00070 KDialogBase::setMainWidget( mainWidget ); 00071 00072 slotUpdateTime( false ); 00073 } 00074 00075 KTimerDialog::~KTimerDialog() 00076 { 00077 } 00078 00079 void KTimerDialog::show() 00080 { 00081 KDialogBase::show(); 00082 totalTimer->start( msecTotal, true ); 00083 updateTimer->start( updateInterval, false ); 00084 } 00085 00086 int KTimerDialog::exec() 00087 { 00088 totalTimer->start( msecTotal, true ); 00089 updateTimer->start( updateInterval, false ); 00090 return KDialogBase::exec(); 00091 } 00092 00093 void KTimerDialog::setMainWidget( TQWidget *widget ) 00094 { 00095 // yuck, here goes. 00096 TQVBox *newWidget = new TQVBox( this ); 00097 00098 if ( widget->parentWidget() != mainWidget ) { 00099 widget->reparent( newWidget, 0, TQPoint(0,0) ); 00100 } else { 00101 newWidget->insertChild( TQT_TQOBJECT(widget) ); 00102 } 00103 00104 timerWidget->reparent( newWidget, 0, TQPoint(0, 0) ); 00105 00106 delete mainWidget; 00107 mainWidget = newWidget; 00108 KDialogBase::setMainWidget( mainWidget ); 00109 } 00110 00111 void KTimerDialog::setRefreshInterval( int msec ) 00112 { 00113 updateInterval = msec; 00114 if ( updateTimer->isActive() ) 00115 updateTimer->changeInterval( updateInterval ); 00116 } 00117 00118 int KTimerDialog::timeoutButton() const 00119 { 00120 return buttonOnTimeout; 00121 } 00122 00123 void KTimerDialog::setTimeoutButton( const ButtonCode newButton ) 00124 { 00125 buttonOnTimeout = newButton; 00126 } 00127 00128 int KTimerDialog::timerStyle() const 00129 { 00130 return tStyle; 00131 } 00132 00133 void KTimerDialog::setTimerStyle( const TimerStyle newStyle ) 00134 { 00135 tStyle = newStyle; 00136 } 00137 00138 void KTimerDialog::slotUpdateTime( bool update ) 00139 { 00140 if ( update ) 00141 switch ( tStyle ) { 00142 case CountDown: 00143 msecRemaining -= updateInterval; 00144 break; 00145 case CountUp: 00146 msecRemaining += updateInterval; 00147 break; 00148 case Manual: 00149 break; 00150 } 00151 00152 timerProgress->setProgress( msecRemaining ); 00153 00154 timerLabel->setText( i18n("1 second remaining:","%n seconds remaining:",msecRemaining/1000) ); 00155 } 00156 00157 void KTimerDialog::slotInternalTimeout() 00158 { 00159 emit timerTimeout(); 00160 switch ( buttonOnTimeout ) { 00161 case Help: 00162 slotHelp(); 00163 break; 00164 case Default: 00165 slotDefault(); 00166 break; 00167 case Ok: 00168 slotOk(); 00169 break; 00170 case Apply: 00171 applyPressed(); 00172 break; 00173 case Try: 00174 slotTry(); 00175 break; 00176 case Cancel: 00177 slotCancel(); 00178 break; 00179 case Close: 00180 slotClose(); 00181 break; 00182 /*case User1: 00183 slotUser1(); 00184 break; 00185 case User2: 00186 slotUser2(); 00187 break;*/ 00188 case User3: 00189 slotUser3(); 00190 break; 00191 case No: 00192 slotNo(); 00193 break; 00194 case Yes: 00195 slotCancel(); 00196 break; 00197 case Details: 00198 slotDetails(); 00199 break; 00200 case Filler: 00201 case Stretch: 00202 kdDebug() << "Cannot execute button code " << buttonOnTimeout << endl; 00203 break; 00204 } 00205 }