kdeui Library API Documentation

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., 59 Temple Place - Suite 330, 00016 Boston, MA 02111-1307, USA. 00017 */ 00022 #include <stdlib.h> 00023 #include <limits.h> 00024 00025 #include <qpainter.h> 00026 #include <qpixmap.h> 00027 #include <qlabel.h> 00028 #include <qlayout.h> 00029 #include <qpushbutton.h> 00030 #include <qstring.h> 00031 #include <qregexp.h> 00032 #include <qstyle.h> 00033 #include <qtimer.h> 00034 00035 #include "kprogress.h" 00036 00037 #include <kapplication.h> 00038 #include <klocale.h> 00039 #include <kwin.h> 00040 00041 KProgress::KProgress(QWidget *parent, const char *name, WFlags f) 00042 : QProgressBar(parent, name, f), 00043 mFormat("%p%") 00044 { 00045 setProgress(0); 00046 } 00047 00048 KProgress::KProgress(int totalSteps, QWidget *parent, const char *name, WFlags f) 00049 : QProgressBar(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 QProgressBar::setTotalSteps(totalSteps); 00067 00068 if (totalSteps) 00069 { 00070 emit percentageChanged((progress() * 100) / totalSteps); 00071 } 00072 } 00073 00074 void KProgress::setProgress(int progress) 00075 { 00076 QProgressBar::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 QString & format) 00113 { 00114 mFormat = format; 00115 if (mFormat != "%p%") 00116 setCenterIndicator(true); 00117 } 00118 00119 QString 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(QString &indicator, int progress, int totalSteps) 00131 { 00132 if (!totalSteps) 00133 return false; 00134 QString newString(mFormat); 00135 newString.replace(QString::fromLatin1("%v"), 00136 QString::number(progress)); 00137 newString.replace(QString::fromLatin1("%m"), 00138 QString::number(totalSteps)); 00139 00140 if (totalSteps > INT_MAX / 1000) { 00141 progress /= 1000; 00142 totalSteps /= 1000; 00143 } 00144 00145 newString.replace(QString::fromLatin1("%p"), 00146 QString::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(QWidget* parent, const char* name, 00170 const QString& caption, const QString& 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 QTimer(this); 00186 00187 showButton(KDialogBase::Close, false); 00188 mCancelText = actionButton(KDialogBase::Cancel)->text(); 00189 00190 QFrame* mainWidget = plainPage(); 00191 QVBoxLayout* layout = new QVBoxLayout(mainWidget, 10); 00192 00193 mLabel = new QLabel(text, mainWidget); 00194 layout->addWidget(mLabel); 00195 00196 mProgressBar = new KProgress(mainWidget); 00197 layout->addWidget(mProgressBar); 00198 00199 connect(mProgressBar, SIGNAL(percentageChanged(int)), 00200 this, SLOT(slotAutoActions(int))); 00201 connect(mShowTimer, SIGNAL(timeout()), this, 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 mShown = true; 00220 } 00221 00222 void KProgressDialog::slotCancel() 00223 { 00224 mCancelled = true; 00225 00226 if (mAllowCancel) 00227 { 00228 KDialogBase::slotCancel(); 00229 } 00230 } 00231 00232 bool KProgressDialog::wasCancelled() 00233 { 00234 return mCancelled; 00235 } 00236 00237 bool KProgressDialog::wasCancelled() const 00238 { 00239 return mCancelled; 00240 } 00241 00242 void KProgressDialog::setMinimumDuration(int ms) 00243 { 00244 mMinDuration = ms; 00245 if (!mShown) 00246 { 00247 mShowTimer->stop(); 00248 mShowTimer->start(mMinDuration, true); 00249 } 00250 } 00251 00252 int KProgressDialog::minimumDuration() 00253 { 00254 return mMinDuration; 00255 } 00256 00257 int KProgressDialog::minimumDuration() const 00258 { 00259 return mMinDuration; 00260 } 00261 00262 void KProgressDialog::setAllowCancel(bool allowCancel) 00263 { 00264 mAllowCancel = allowCancel; 00265 showCancelButton(allowCancel); 00266 } 00267 00268 // ### KDE 4 remove 00269 bool KProgressDialog::allowCancel() 00270 { 00271 return mAllowCancel; 00272 } 00273 00274 bool KProgressDialog::allowCancel() const 00275 { 00276 return mAllowCancel; 00277 } 00278 00279 KProgress* KProgressDialog::progressBar() 00280 { 00281 return mProgressBar; 00282 } 00283 00284 const KProgress* KProgressDialog::progressBar() const 00285 { 00286 return mProgressBar; 00287 } 00288 00289 void KProgressDialog::setLabel(const QString& text) 00290 { 00291 mLabel->setText(text); 00292 } 00293 00294 // ### KDE 4 remove 00295 QString KProgressDialog::labelText() 00296 { 00297 return mLabel->text(); 00298 } 00299 00300 QString KProgressDialog::labelText() const 00301 { 00302 return mLabel->text(); 00303 } 00304 00305 void KProgressDialog::showCancelButton(bool show) 00306 { 00307 showButtonCancel(show); 00308 } 00309 00310 // ### KDE 4 remove 00311 bool KProgressDialog::autoClose() 00312 { 00313 return mAutoClose; 00314 } 00315 00316 bool KProgressDialog::autoClose() const 00317 { 00318 return mAutoClose; 00319 } 00320 00321 void KProgressDialog::setAutoClose(bool autoClose) 00322 { 00323 mAutoClose = autoClose; 00324 } 00325 00326 // ### KDE 4 remove 00327 bool KProgressDialog::autoReset() 00328 { 00329 return mAutoReset; 00330 } 00331 00332 bool KProgressDialog::autoReset() const 00333 { 00334 return mAutoReset; 00335 } 00336 00337 void KProgressDialog::setAutoReset(bool autoReset) 00338 { 00339 mAutoReset = autoReset; 00340 } 00341 00342 void KProgressDialog::setButtonText(const QString& text) 00343 { 00344 mCancelText = text; 00345 setButtonCancel(text); 00346 } 00347 00348 // ### KDE 4 remove 00349 QString KProgressDialog::buttonText() 00350 { 00351 return mCancelText; 00352 } 00353 00354 QString KProgressDialog::buttonText() const 00355 { 00356 return mCancelText; 00357 } 00358 00359 void KProgressDialog::slotAutoActions(int percentage) 00360 { 00361 if (percentage < 100) 00362 { 00363 if (!d->cancelButtonShown) 00364 { 00365 setButtonCancel(mCancelText); 00366 d->cancelButtonShown = true; 00367 } 00368 return; 00369 } 00370 00371 mShowTimer->stop(); 00372 00373 if (mAutoReset) 00374 { 00375 mProgressBar->setProgress(0); 00376 } 00377 else 00378 { 00379 setAllowCancel(true); 00380 setButtonCancel(KStdGuiItem::close()); 00381 d->cancelButtonShown = false; 00382 } 00383 00384 if (mAutoClose) 00385 { 00386 if (mShown) 00387 { 00388 hide(); 00389 } 00390 else 00391 { 00392 emit finished(); 00393 } 00394 } 00395 } 00396 00397 void KProgress::virtual_hook( int, void* ) 00398 { /*BASE::virtual_hook( id, data );*/ } 00399 00400 void KProgressDialog::virtual_hook( int id, void* data ) 00401 { KDialogBase::virtual_hook( id, data ); } 00402 00403 #include "kprogress.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Apr 12 22:56:30 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003