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 <kwin.h>
00039 
00040 KProgress::KProgress(QWidget *parent, const char *name, WFlags f)
00041   : QProgressBar(parent, name, f),
00042     mFormat("%p%")
00043 {
00044     setProgress(0);
00045 }
00046 
00047 KProgress::KProgress(int totalSteps, QWidget *parent, const char *name, WFlags f)
00048   : QProgressBar(totalSteps, parent, name, f),
00049     mFormat("%p%")
00050 {
00051     setProgress(0);
00052 }
00053 
00054 KProgress::~KProgress()
00055 {
00056 }
00057 
00058 void KProgress::advance(int offset)
00059 {
00060     setProgress(progress() + offset);
00061 }
00062 
00063 void KProgress::setTotalSteps(int totalSteps)
00064 {
00065     QProgressBar::setTotalSteps(totalSteps);
00066 
00067     if (totalSteps)
00068     {
00069         emit percentageChanged((progress() * 100) / totalSteps);
00070     }
00071 }
00072 
00073 void KProgress::setProgress(int progress)
00074 {
00075     QProgressBar::setProgress(progress);
00076 
00077     if (totalSteps())
00078     {
00079         emit percentageChanged((progress * 100) / totalSteps());
00080     }
00081 }
00082 
00083 void KProgress::setValue(int progress)
00084 {
00085     setProgress(progress); 
00086 }
00087 
00088 void KProgress::setRange(int /*min*/, int max)
00089 {
00090     setTotalSteps(max);
00091 }
00092 
00093 int KProgress::maxValue()
00094 {
00095     return totalSteps();
00096 }
00097 
00098 void KProgress::setTextEnabled(bool enable)
00099 {
00100     setPercentageVisible(enable);
00101 }
00102 
00103 bool KProgress::textEnabled() const
00104 {
00105     return percentageVisible();
00106 }
00107 
00108 void KProgress::setFormat(const QString & format)
00109 {
00110     mFormat = format;
00111 }
00112 
00113 QString KProgress::format() const
00114 {
00115     return mFormat;
00116 }
00117 
00118 int KProgress::value() const
00119 {
00120     return progress();
00121 }
00122 
00123 bool KProgress::setIndicator(QString &indicator, int progress, int totalSteps)
00124 {
00125     if (!totalSteps)
00126         return false;
00127     QString newString(mFormat);
00128     newString.replace(QRegExp(QString::fromLatin1("%v")),
00129                       QString::number(progress));
00130     newString.replace(QRegExp(QString::fromLatin1("%m")),
00131                       QString::number(totalSteps));
00132 
00133     if (totalSteps > INT_MAX / 1000) {
00134         progress /= 1000;
00135         totalSteps /= 1000;
00136     }
00137 
00138     newString.replace(QRegExp(QString::fromLatin1("%p")),
00139                       QString::number((progress * 100) / totalSteps));
00140 
00141     if (newString != indicator)
00142     {
00143         indicator = newString;
00144         return true;
00145     }
00146 
00147     return false;
00148 }
00149 
00150 
00151 /*
00152  * KProgressDialog implementation
00153  */
00154 KProgressDialog::KProgressDialog(QWidget* parent, const char* name,
00155                                  const QString& caption, const QString& text,
00156                                  bool modal)
00157     : KDialogBase(KDialogBase::Plain, caption, KDialogBase::Cancel,
00158                   KDialogBase::Cancel, parent, name, modal),
00159       mAutoClose(true),
00160       mAutoReset(false),
00161       mCancelled(false),
00162       mAllowCancel(true),
00163       mShown(false),
00164       mMinDuration(2000)
00165 {
00166     KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon());
00167     mShowTimer = new QTimer(this);
00168     
00169     showButton(KDialogBase::Close, false);
00170     mCancelText = actionButton(KDialogBase::Cancel)->text();
00171 
00172     QFrame* mainWidget = plainPage();
00173     QVBoxLayout* layout = new QVBoxLayout(mainWidget, 10);
00174 
00175     mLabel = new QLabel(text, mainWidget);
00176     layout->addWidget(mLabel);
00177 
00178     mProgressBar = new KProgress(mainWidget);
00179     layout->addWidget(mProgressBar);
00180 
00181     connect(mProgressBar, SIGNAL(percentageChanged(int)),
00182             this, SLOT(slotAutoActions(int)));
00183     connect(mShowTimer, SIGNAL(timeout()), this, SLOT(slotAutoShow()));
00184     mShowTimer->start(mMinDuration, true);
00185 }
00186 
00187 KProgressDialog::~KProgressDialog()
00188 {
00189 }
00190 
00191 void KProgressDialog::slotAutoShow()
00192 {
00193     if (mShown || mCancelled)
00194     {
00195         return;
00196     }
00197 
00198     show();
00199     kapp->processEvents();
00200     mShown = true;
00201 }
00202 
00203 void KProgressDialog::slotCancel()
00204 {
00205     mCancelled = true;
00206 
00207     if (mAllowCancel)
00208     {
00209         KDialogBase::slotCancel();
00210     }
00211 }
00212 
00213 bool KProgressDialog::wasCancelled()
00214 {
00215     return mCancelled;
00216 }
00217 
00218 void KProgressDialog::setMinimumDuration(int ms)
00219 {
00220     mMinDuration = ms;
00221     if (!mShown)
00222     {
00223         mShowTimer->stop();
00224         mShowTimer->start(mMinDuration, true);
00225     }
00226 }
00227 
00228 int KProgressDialog::minimumDuration()
00229 {
00230     return mMinDuration;
00231 }
00232 
00233 void KProgressDialog::setAllowCancel(bool allowCancel)
00234 {
00235     mAllowCancel = allowCancel;
00236     showCancelButton(allowCancel);
00237 }
00238 
00239 bool KProgressDialog::allowCancel()
00240 {
00241     return mAllowCancel;
00242 }
00243 
00244 KProgress* KProgressDialog::progressBar()
00245 {
00246     return mProgressBar;
00247 }
00248 
00249 void KProgressDialog::setLabel(const QString& text)
00250 {
00251     mLabel->setText(text);
00252 }
00253 
00254 QString KProgressDialog::labelText()
00255 {
00256     return mLabel->text();
00257 }
00258 
00259 void KProgressDialog::showCancelButton(bool show)
00260 {
00261     showButtonCancel(show);
00262 }
00263 
00264 bool KProgressDialog::autoClose()
00265 {
00266     return mAutoClose;
00267 }
00268 
00269 void KProgressDialog::setAutoClose(bool autoClose)
00270 {
00271     mAutoClose = autoClose;
00272 }
00273 
00274 bool KProgressDialog::autoReset()
00275 {
00276     return mAutoReset;
00277 }
00278 
00279 void KProgressDialog::setAutoReset(bool autoReset)
00280 {
00281     mAutoReset = autoReset;
00282 }
00283 
00284 void KProgressDialog::setButtonText(const QString& text)
00285 {
00286     mCancelText = text;
00287     setButtonCancelText(mCancelText);
00288 }
00289 
00290 QString KProgressDialog::buttonText()
00291 {
00292     return mCancelText;
00293 }
00294 
00295 void KProgressDialog::slotAutoActions(int percentage)
00296 {
00297     if (percentage < 100)
00298     {
00299         setButtonCancelText(mCancelText);
00300     }
00301     else
00302     {
00303         if (mAutoReset)
00304         {
00305             mProgressBar->setProgress(0);
00306         }
00307         else
00308         {
00309             setAllowCancel(true);
00310             setButtonCancelText("&Close");
00311         }
00312 
00313         if (mAutoClose)
00314         {
00315             hide();
00316         }
00317     }
00318 }
00319 
00320 void KProgress::virtual_hook( int, void* )
00321 { /*BASE::virtual_hook( id, data );*/ }
00322 
00323 void KProgressDialog::virtual_hook( int id, void* data )
00324 { KDialogBase::virtual_hook( id, data ); }
00325 
00326 #include "kprogress.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:04 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001