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     KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon());
00183     mShowTimer = new QTimer(this);
00184     
00185     showButton(KDialogBase::Close, false);
00186     mCancelText = actionButton(KDialogBase::Cancel)->text();
00187 
00188     QFrame* mainWidget = plainPage();
00189     QVBoxLayout* layout = new QVBoxLayout(mainWidget, 10);
00190 
00191     mLabel = new QLabel(text, mainWidget);
00192     layout->addWidget(mLabel);
00193 
00194     mProgressBar = new KProgress(mainWidget);
00195     layout->addWidget(mProgressBar);
00196 
00197     connect(mProgressBar, SIGNAL(percentageChanged(int)),
00198             this, SLOT(slotAutoActions(int)));
00199     connect(mShowTimer, SIGNAL(timeout()), this, SLOT(slotAutoShow()));
00200     mShowTimer->start(mMinDuration, true);
00201 }
00202 
00203 KProgressDialog::~KProgressDialog()
00204 {
00205     delete d;
00206 }
00207 
00208 void KProgressDialog::slotAutoShow()
00209 {
00210     if (mShown || mCancelled)
00211     {
00212         return;
00213     }
00214 
00215     show();
00216     kapp->processEvents();
00217     mShown = true;
00218 }
00219 
00220 void KProgressDialog::slotCancel()
00221 {
00222     mCancelled = true;
00223 
00224     if (mAllowCancel)
00225     {
00226         KDialogBase::slotCancel();
00227     }
00228 }
00229 
00230 bool KProgressDialog::wasCancelled()
00231 {
00232     return mCancelled;
00233 }
00234 
00235 bool KProgressDialog::wasCancelled() const
00236 {
00237     return mCancelled;
00238 }
00239 
00240 void KProgressDialog::setMinimumDuration(int ms)
00241 {
00242     mMinDuration = ms;
00243     if (!mShown)
00244     {
00245         mShowTimer->stop();
00246         mShowTimer->start(mMinDuration, true);
00247     }
00248 }
00249 
00250 int KProgressDialog::minimumDuration()
00251 {
00252     return mMinDuration;
00253 }
00254 
00255 int KProgressDialog::minimumDuration() const
00256 {
00257     return mMinDuration;
00258 }
00259 
00260 void KProgressDialog::setAllowCancel(bool allowCancel)
00261 {
00262     mAllowCancel = allowCancel;
00263     showCancelButton(allowCancel);
00264 }
00265 
00266 // ### KDE 4 remove
00267 bool KProgressDialog::allowCancel()
00268 {
00269     return mAllowCancel;
00270 }
00271 
00272 bool KProgressDialog::allowCancel() const
00273 {
00274     return mAllowCancel;
00275 }
00276 
00277 KProgress* KProgressDialog::progressBar()
00278 {
00279     return mProgressBar;
00280 }
00281 
00282 const KProgress* KProgressDialog::progressBar() const
00283 {
00284     return mProgressBar;
00285 }
00286 
00287 void KProgressDialog::setLabel(const QString& text)
00288 {
00289     mLabel->setText(text);
00290 }
00291 
00292 // ### KDE 4 remove
00293 QString KProgressDialog::labelText()
00294 {
00295     return mLabel->text();
00296 }
00297 
00298 QString KProgressDialog::labelText() const
00299 {
00300     return mLabel->text();
00301 }
00302 
00303 void KProgressDialog::showCancelButton(bool show)
00304 {
00305     showButtonCancel(show);
00306 }
00307 
00308 // ### KDE 4 remove
00309 bool KProgressDialog::autoClose()
00310 {
00311     return mAutoClose;
00312 }
00313 
00314 bool KProgressDialog::autoClose() const
00315 {
00316     return mAutoClose;
00317 }
00318 
00319 void KProgressDialog::setAutoClose(bool autoClose)
00320 {
00321     mAutoClose = autoClose;
00322 }
00323 
00324 // ### KDE 4 remove
00325 bool KProgressDialog::autoReset()
00326 {
00327     return mAutoReset;
00328 }
00329 
00330 bool KProgressDialog::autoReset() const
00331 {
00332     return mAutoReset;
00333 }
00334 
00335 void KProgressDialog::setAutoReset(bool autoReset)
00336 {
00337     mAutoReset = autoReset;
00338 }
00339 
00340 void KProgressDialog::setButtonText(const QString& text)
00341 {
00342     mCancelText = text;
00343     setButtonCancel(text);
00344 }
00345 
00346 // ### KDE 4 remove
00347 QString KProgressDialog::buttonText()
00348 {
00349     return mCancelText;
00350 }
00351 
00352 QString KProgressDialog::buttonText() const
00353 {
00354     return mCancelText;
00355 }
00356 
00357 void KProgressDialog::slotAutoActions(int percentage)
00358 {
00359     if (percentage < 100)
00360     {
00361         if (!d->cancelButtonShown)
00362         {
00363             setButtonCancel(mCancelText);
00364             d->cancelButtonShown = true;
00365         }
00366         return;
00367     }
00368 
00369     mShowTimer->stop();
00370 
00371     if (mAutoReset)
00372     {
00373         mProgressBar->setProgress(0);
00374     }
00375     else
00376     {
00377         setAllowCancel(true);
00378         setButtonCancel(KStdGuiItem::close());
00379         d->cancelButtonShown = false;
00380     }
00381 
00382     if (mAutoClose)
00383     {
00384         if (mShown)
00385         {
00386             hide();
00387         }
00388         else
00389         {
00390             emit finished();
00391         }
00392     }
00393 }
00394 
00395 void KProgress::virtual_hook( int, void* )
00396 { /*BASE::virtual_hook( id, data );*/ }
00397 
00398 void KProgressDialog::virtual_hook( int id, void* data )
00399 { KDialogBase::virtual_hook( id, data ); }
00400 
00401 #include "kprogress.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Feb 18 15:10:19 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003