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