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