kprogress.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1996 Martynas Kunigelis 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00022 #include <stdlib.h> 00023 #include <limits.h> 00024 00025 #include <tqpainter.h> 00026 #include <tqpixmap.h> 00027 #include <tqlabel.h> 00028 #include <tqlayout.h> 00029 #include <tqpushbutton.h> 00030 #include <tqstring.h> 00031 #include <tqregexp.h> 00032 #include <tqstyle.h> 00033 #include <tqtimer.h> 00034 00035 #include "kprogress.h" 00036 00037 #include <kapplication.h> 00038 #include <klocale.h> 00039 #include <kwin.h> 00040 00041 KProgress::KProgress(TQWidget *parent, const char *name, WFlags f) 00042 : TQProgressBar(parent, name, f), 00043 mFormat("%p%") 00044 { 00045 setProgress(0); 00046 } 00047 00048 KProgress::KProgress(int totalSteps, TQWidget *parent, const char *name, WFlags f) 00049 : TQProgressBar(totalSteps, parent, name, f), 00050 mFormat("%p%") 00051 { 00052 setProgress(0); 00053 } 00054 00055 KProgress::~KProgress() 00056 { 00057 } 00058 00059 void KProgress::advance(int offset) 00060 { 00061 setProgress(progress() + offset); 00062 } 00063 00064 void KProgress::setTotalSteps(int totalSteps) 00065 { 00066 TQProgressBar::setTotalSteps(totalSteps); 00067 00068 if (totalSteps) 00069 { 00070 emit percentageChanged((progress() * 100) / totalSteps); 00071 } 00072 } 00073 00074 void KProgress::setProgress(int progress) 00075 { 00076 TQProgressBar::setProgress(progress); 00077 00078 if (totalSteps()) 00079 { 00080 emit percentageChanged((progress * 100) / totalSteps()); 00081 } 00082 } 00083 00084 // ### KDE 4 remove 00085 void KProgress::setValue(int progress) 00086 { 00087 setProgress(progress); 00088 } 00089 00090 // ### KDE 4 remove 00091 void KProgress::setRange(int /*min*/, int max) 00092 { 00093 setTotalSteps(max); 00094 } 00095 00096 // ### KDE 4 remove 00097 int KProgress::maxValue() 00098 { 00099 return totalSteps(); 00100 } 00101 00102 void KProgress::setTextEnabled(bool enable) 00103 { 00104 setPercentageVisible(enable); 00105 } 00106 00107 bool KProgress::textEnabled() const 00108 { 00109 return percentageVisible(); 00110 } 00111 00112 void KProgress::setFormat(const TQString & format) 00113 { 00114 mFormat = format; 00115 if (mFormat != "%p%") 00116 setCenterIndicator(true); 00117 } 00118 00119 TQString KProgress::format() const 00120 { 00121 return mFormat; 00122 } 00123 00124 // ### KDE 4 remove 00125 int KProgress::value() const 00126 { 00127 return progress(); 00128 } 00129 00130 bool KProgress::setIndicator(TQString &indicator, int progress, int totalSteps) 00131 { 00132 if (!totalSteps) 00133 return false; 00134 TQString newString(mFormat); 00135 newString.replace(TQString::fromLatin1("%v"), 00136 TQString::number(progress)); 00137 newString.replace(TQString::fromLatin1("%m"), 00138 TQString::number(totalSteps)); 00139 00140 if (totalSteps > INT_MAX / 1000) { 00141 progress /= 1000; 00142 totalSteps /= 1000; 00143 } 00144 00145 newString.replace(TQString::fromLatin1("%p"), 00146 TQString::number((progress * 100) / totalSteps)); 00147 00148 if (newString != indicator) 00149 { 00150 indicator = newString; 00151 return true; 00152 } 00153 00154 return false; 00155 } 00156 00157 struct KProgressDialog::KProgressDialogPrivate 00158 { 00159 KProgressDialogPrivate() : cancelButtonShown(true) 00160 { 00161 } 00162 00163 bool cancelButtonShown; 00164 }; 00165 00166 /* 00167 * KProgressDialog implementation 00168 */ 00169 KProgressDialog::KProgressDialog(TQWidget* parent, const char* name, 00170 const TQString& caption, const TQString& text, 00171 bool modal) 00172 : KDialogBase(KDialogBase::Plain, caption, KDialogBase::Cancel, 00173 KDialogBase::Cancel, parent, name, modal), 00174 mAutoClose(true), 00175 mAutoReset(false), 00176 mCancelled(false), 00177 mAllowCancel(true), 00178 mShown(false), 00179 mMinDuration(2000), 00180 d(new KProgressDialogPrivate) 00181 { 00182 #ifdef Q_WS_X11 00183 KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon()); 00184 #endif 00185 mShowTimer = new TQTimer(this); 00186 00187 showButton(KDialogBase::Close, false); 00188 mCancelText = actionButton(KDialogBase::Cancel)->text(); 00189 00190 TQFrame* mainWidget = plainPage(); 00191 TQVBoxLayout* layout = new TQVBoxLayout(mainWidget, 10); 00192 00193 mLabel = new TQLabel(text, mainWidget); 00194 layout->addWidget(mLabel); 00195 00196 mProgressBar = new KProgress(mainWidget); 00197 layout->addWidget(mProgressBar); 00198 00199 connect(mProgressBar, TQT_SIGNAL(percentageChanged(int)), 00200 this, TQT_SLOT(slotAutoActions(int))); 00201 connect(mShowTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotAutoShow())); 00202 mShowTimer->start(mMinDuration, true); 00203 } 00204 00205 KProgressDialog::~KProgressDialog() 00206 { 00207 delete d; 00208 } 00209 00210 void KProgressDialog::slotAutoShow() 00211 { 00212 if (mShown || mCancelled) 00213 { 00214 return; 00215 } 00216 00217 show(); 00218 kapp->processEvents(); 00219 } 00220 00221 void KProgressDialog::slotCancel() 00222 { 00223 mCancelled = true; 00224 00225 if (mAllowCancel) 00226 { 00227 KDialogBase::slotCancel(); 00228 } 00229 } 00230 00231 bool KProgressDialog::wasCancelled() 00232 { 00233 return mCancelled; 00234 } 00235 00236 void KProgressDialog::ignoreCancel() 00237 { 00238 mCancelled = false; 00239 } 00240 00241 bool KProgressDialog::wasCancelled() const 00242 { 00243 return mCancelled; 00244 } 00245 00246 void KProgressDialog::setMinimumDuration(int ms) 00247 { 00248 mMinDuration = ms; 00249 if (!mShown) 00250 { 00251 mShowTimer->stop(); 00252 mShowTimer->start(mMinDuration, true); 00253 } 00254 } 00255 00256 int KProgressDialog::minimumDuration() 00257 { 00258 return mMinDuration; 00259 } 00260 00261 int KProgressDialog::minimumDuration() const 00262 { 00263 return mMinDuration; 00264 } 00265 00266 void KProgressDialog::setAllowCancel(bool allowCancel) 00267 { 00268 mAllowCancel = allowCancel; 00269 showCancelButton(allowCancel); 00270 } 00271 00272 // ### KDE 4 remove 00273 bool KProgressDialog::allowCancel() 00274 { 00275 return mAllowCancel; 00276 } 00277 00278 bool KProgressDialog::allowCancel() const 00279 { 00280 return mAllowCancel; 00281 } 00282 00283 KProgress* KProgressDialog::progressBar() 00284 { 00285 return mProgressBar; 00286 } 00287 00288 const KProgress* KProgressDialog::progressBar() const 00289 { 00290 return mProgressBar; 00291 } 00292 00293 void KProgressDialog::setLabel(const TQString& text) 00294 { 00295 mLabel->setText(text); 00296 } 00297 00298 // ### KDE 4 remove 00299 TQString KProgressDialog::labelText() 00300 { 00301 return mLabel->text(); 00302 } 00303 00304 TQString KProgressDialog::labelText() const 00305 { 00306 return mLabel->text(); 00307 } 00308 00309 void KProgressDialog::showCancelButton(bool show) 00310 { 00311 showButtonCancel(show); 00312 } 00313 00314 // ### KDE 4 remove 00315 bool KProgressDialog::autoClose() 00316 { 00317 return mAutoClose; 00318 } 00319 00320 bool KProgressDialog::autoClose() const 00321 { 00322 return mAutoClose; 00323 } 00324 00325 void KProgressDialog::setAutoClose(bool autoClose) 00326 { 00327 mAutoClose = autoClose; 00328 } 00329 00330 // ### KDE 4 remove 00331 bool KProgressDialog::autoReset() 00332 { 00333 return mAutoReset; 00334 } 00335 00336 bool KProgressDialog::autoReset() const 00337 { 00338 return mAutoReset; 00339 } 00340 00341 void KProgressDialog::setAutoReset(bool autoReset) 00342 { 00343 mAutoReset = autoReset; 00344 } 00345 00346 void KProgressDialog::setButtonText(const TQString& text) 00347 { 00348 mCancelText = text; 00349 setButtonCancel(text); 00350 } 00351 00352 // ### KDE 4 remove 00353 TQString KProgressDialog::buttonText() 00354 { 00355 return mCancelText; 00356 } 00357 00358 TQString KProgressDialog::buttonText() const 00359 { 00360 return mCancelText; 00361 } 00362 00363 void KProgressDialog::slotAutoActions(int percentage) 00364 { 00365 if (percentage < 100) 00366 { 00367 if (!d->cancelButtonShown) 00368 { 00369 setButtonCancel(mCancelText); 00370 d->cancelButtonShown = true; 00371 } 00372 return; 00373 } 00374 00375 mShowTimer->stop(); 00376 00377 if (mAutoReset) 00378 { 00379 mProgressBar->setProgress(0); 00380 } 00381 else 00382 { 00383 setAllowCancel(true); 00384 setButtonCancel(KStdGuiItem::close()); 00385 d->cancelButtonShown = false; 00386 } 00387 00388 if (mAutoClose) 00389 { 00390 if (mShown) 00391 { 00392 hide(); 00393 } 00394 else 00395 { 00396 emit finished(); 00397 } 00398 } 00399 } 00400 00401 void KProgressDialog::show() 00402 { 00403 KDialogBase::show(); 00404 mShown = true; 00405 } 00406 00407 00408 void KProgress::virtual_hook( int, void* ) 00409 { /*BASE::virtual_hook( id, data );*/ } 00410 00411 void KProgressDialog::virtual_hook( int id, void* data ) 00412 { KDialogBase::virtual_hook( id, data ); } 00413 00414 #include "kprogress.moc"