progressbar.cpp
00001 /* 00002 progressbar.cpp 00003 00004 This file is part of libkleopatra, the KDE keymanagement library 00005 Copyright (c) 2004 Klarälvdalens Datakonsult AB 00006 00007 Libkleopatra is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU General Public License as 00009 published by the Free Software Foundation; either version 2 of the 00010 License, or (at your option) any later version. 00011 00012 Libkleopatra is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 00021 In addition, as a special exception, the copyright holders give 00022 permission to link the code of this program with any edition of 00023 the TQt library by Trolltech AS, Norway (or with modified versions 00024 of TQt that use the same license as TQt), and distribute linked 00025 combinations including the two. You must obey the GNU General 00026 Public License in all respects for all of the code used other than 00027 TQt. If you modify this file, you may extend this exception to 00028 your version of the file, but you are not obligated to do so. If 00029 you do not wish to do so, delete this exception statement from 00030 your version. 00031 */ 00032 00033 #include "config.h" 00034 #include "progressbar.h" 00035 00036 #include <tqtimer.h> 00037 #include <kdebug.h> 00038 00039 static const int busyTimerTickInterval = 100; 00040 static const int busyTimerTickIncrement = 5; 00041 00042 Kleo::ProgressBar::ProgressBar( TQWidget * parent, const char * name, WFlags f ) 00043 : TQProgressBar( 0, parent, name, f ), 00044 mRealProgress( -1 ) 00045 { 00046 mBusyTimer = new TQTimer( this ); 00047 connect( mBusyTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotBusyTimerTick()) ); 00048 fixup( true ); 00049 } 00050 00051 void Kleo::ProgressBar::slotProgress( const TQString &, int cur, int tot ) { 00052 setProgress( cur, tot ); 00053 } 00054 00055 void Kleo::ProgressBar::slotProgress( const TQString &, int, int cur, int tot ) { 00056 setProgress( cur, tot ); 00057 } 00058 00059 void Kleo::ProgressBar::setTotalSteps( int total ) { 00060 kdDebug() << "Kleo::ProgressBar::setTotalSteps( " << total << " )" << endl; 00061 if ( total == totalSteps() ) 00062 return; 00063 TQProgressBar::setTotalSteps( 0 ); 00064 fixup( false ); 00065 } 00066 00067 void Kleo::ProgressBar::setProgress( int p ) { 00068 kdDebug() << "Kleo::ProgressBar::setProgress( " << p << " )" << endl; 00069 mRealProgress = p; 00070 fixup( true ); 00071 } 00072 00073 void Kleo::ProgressBar::reset() { 00074 mRealProgress = -1; 00075 fixup( true ); 00076 } 00077 00078 void Kleo::ProgressBar::slotBusyTimerTick() { 00079 fixup( false ); 00080 if ( mBusyTimer->isActive() ) 00081 TQProgressBar::setProgress( TQProgressBar::progress() + busyTimerTickIncrement ); 00082 } 00083 00084 void Kleo::ProgressBar::fixup( bool newValue ) { 00085 const int cur = TQProgressBar::progress(); 00086 const int tot = TQProgressBar::totalSteps(); 00087 00088 kdDebug() << "Kleo::ProgressBar::startStopBusyTimer() cur = " << cur << "; tot = " << tot << "; real = " << mRealProgress << endl; 00089 00090 if ( ( newValue && mRealProgress < 0 ) || ( !newValue && cur < 0 ) ) { 00091 kdDebug() << "(new value) switch to reset" << endl; 00092 mBusyTimer->stop(); 00093 if ( newValue ) 00094 TQProgressBar::reset(); 00095 mRealProgress = -1; 00096 } else if ( tot == 0 ) { 00097 kdDebug() << "(new value) switch or stay in busy" << endl; 00098 if ( !mBusyTimer->isActive() ) { 00099 mBusyTimer->start( busyTimerTickInterval ); 00100 if ( newValue ) 00101 TQProgressBar::setProgress( mRealProgress ); 00102 } 00103 } else { 00104 kdDebug() << "(new value) normal progress" << endl; 00105 mBusyTimer->stop(); 00106 if ( TQProgressBar::progress() != mRealProgress ) 00107 TQProgressBar::setProgress( mRealProgress ); 00108 } 00109 } 00110 00111 #include "progressbar.moc"