kprogress.cpp
/* This file is part of the KDE libraries
Copyright (C) 1996 Martynas Kunigelis
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <limits.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qstring.h>
#include <qregexp.h>
#include <qstyle.h>
#include <qtimer.h>
#include "kprogress.h"
#include <kapplication.h>
#include <klocale.h>
#include <kwin.h>
KProgress::KProgress(QWidget *parent, const char *name, WFlags f)
: QProgressBar(parent, name, f),
mFormat("%p%")
{
setProgress(0);
}
KProgress::KProgress(int totalSteps, QWidget *parent, const char *name, WFlags f)
: QProgressBar(totalSteps, parent, name, f),
mFormat("%p%")
{
setProgress(0);
}
KProgress::~KProgress()
{
}
void KProgress::advance(int offset)
{
setProgress(progress() + offset);
}
void KProgress::setTotalSteps(int totalSteps)
{
QProgressBar::setTotalSteps(totalSteps);
if (totalSteps)
{
emit percentageChanged((progress() * 100) / totalSteps);
}
}
void KProgress::setProgress(int progress)
{
QProgressBar::setProgress(progress);
if (totalSteps())
{
emit percentageChanged((progress * 100) / totalSteps());
}
}
// ### KDE 4 remove
void KProgress::setValue(int progress)
{
setProgress(progress);
}
// ### KDE 4 remove
void KProgress::setRange(int /*min*/, int max)
{
setTotalSteps(max);
}
// ### KDE 4 remove
int KProgress::maxValue()
{
return totalSteps();
}
void KProgress::setTextEnabled(bool enable)
{
setPercentageVisible(enable);
}
bool KProgress::textEnabled() const
{
return percentageVisible();
}
void KProgress::setFormat(const QString & format)
{
mFormat = format;
if (mFormat != "%p%")
setCenterIndicator(true);
}
QString KProgress::format() const
{
return mFormat;
}
// ### KDE 4 remove
int KProgress::value() const
{
return progress();
}
bool KProgress::setIndicator(QString &indicator, int progress, int totalSteps)
{
if (!totalSteps)
return false;
QString newString(mFormat);
newString.replace(QString::fromLatin1("%v"),
QString::number(progress));
newString.replace(QString::fromLatin1("%m"),
QString::number(totalSteps));
if (totalSteps > INT_MAX / 1000) {
progress /= 1000;
totalSteps /= 1000;
}
newString.replace(QString::fromLatin1("%p"),
QString::number((progress * 100) / totalSteps));
if (newString != indicator)
{
indicator = newString;
return true;
}
return false;
}
struct KProgressDialog::KProgressDialogPrivate
{
KProgressDialogPrivate() : cancelButtonShown(true)
{
}
bool cancelButtonShown;
};
/*
* KProgressDialog implementation
*/
KProgressDialog::KProgressDialog(QWidget* parent, const char* name,
const QString& caption, const QString& text,
bool modal)
: KDialogBase(KDialogBase::Plain, caption, KDialogBase::Cancel,
KDialogBase::Cancel, parent, name, modal),
mAutoClose(true),
mAutoReset(false),
mCancelled(false),
mAllowCancel(true),
mShown(false),
mMinDuration(2000),
d(new KProgressDialogPrivate)
{
#ifdef Q_WS_X11
KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon());
#endif
mShowTimer = new QTimer(this);
showButton(KDialogBase::Close, false);
mCancelText = actionButton(KDialogBase::Cancel)->text();
QFrame* mainWidget = plainPage();
QVBoxLayout* layout = new QVBoxLayout(mainWidget, 10);
mLabel = new QLabel(text, mainWidget);
layout->addWidget(mLabel);
mProgressBar = new KProgress(mainWidget);
layout->addWidget(mProgressBar);
connect(mProgressBar, SIGNAL(percentageChanged(int)),
this, SLOT(slotAutoActions(int)));
connect(mShowTimer, SIGNAL(timeout()), this, SLOT(slotAutoShow()));
mShowTimer->start(mMinDuration, true);
}
KProgressDialog::~KProgressDialog()
{
delete d;
}
void KProgressDialog::slotAutoShow()
{
if (mShown || mCancelled)
{
return;
}
show();
kapp->processEvents();
}
void KProgressDialog::slotCancel()
{
mCancelled = true;
if (mAllowCancel)
{
KDialogBase::slotCancel();
}
}
bool KProgressDialog::wasCancelled()
{
return mCancelled;
}
void KProgressDialog::ignoreCancel()
{
mCancelled = false;
}
bool KProgressDialog::wasCancelled() const
{
return mCancelled;
}
void KProgressDialog::setMinimumDuration(int ms)
{
mMinDuration = ms;
if (!mShown)
{
mShowTimer->stop();
mShowTimer->start(mMinDuration, true);
}
}
int KProgressDialog::minimumDuration()
{
return mMinDuration;
}
int KProgressDialog::minimumDuration() const
{
return mMinDuration;
}
void KProgressDialog::setAllowCancel(bool allowCancel)
{
mAllowCancel = allowCancel;
showCancelButton(allowCancel);
}
// ### KDE 4 remove
bool KProgressDialog::allowCancel()
{
return mAllowCancel;
}
bool KProgressDialog::allowCancel() const
{
return mAllowCancel;
}
KProgress* KProgressDialog::progressBar()
{
return mProgressBar;
}
const KProgress* KProgressDialog::progressBar() const
{
return mProgressBar;
}
void KProgressDialog::setLabel(const QString& text)
{
mLabel->setText(text);
}
// ### KDE 4 remove
QString KProgressDialog::labelText()
{
return mLabel->text();
}
QString KProgressDialog::labelText() const
{
return mLabel->text();
}
void KProgressDialog::showCancelButton(bool show)
{
showButtonCancel(show);
}
// ### KDE 4 remove
bool KProgressDialog::autoClose()
{
return mAutoClose;
}
bool KProgressDialog::autoClose() const
{
return mAutoClose;
}
void KProgressDialog::setAutoClose(bool autoClose)
{
mAutoClose = autoClose;
}
// ### KDE 4 remove
bool KProgressDialog::autoReset()
{
return mAutoReset;
}
bool KProgressDialog::autoReset() const
{
return mAutoReset;
}
void KProgressDialog::setAutoReset(bool autoReset)
{
mAutoReset = autoReset;
}
void KProgressDialog::setButtonText(const QString& text)
{
mCancelText = text;
setButtonCancel(text);
}
// ### KDE 4 remove
QString KProgressDialog::buttonText()
{
return mCancelText;
}
QString KProgressDialog::buttonText() const
{
return mCancelText;
}
void KProgressDialog::slotAutoActions(int percentage)
{
if (percentage < 100)
{
if (!d->cancelButtonShown)
{
setButtonCancel(mCancelText);
d->cancelButtonShown = true;
}
return;
}
mShowTimer->stop();
if (mAutoReset)
{
mProgressBar->setProgress(0);
}
else
{
setAllowCancel(true);
setButtonCancel(KStdGuiItem::close());
d->cancelButtonShown = false;
}
if (mAutoClose)
{
if (mShown)
{
hide();
}
else
{
emit finished();
}
}
}
void KProgressDialog::show()
{
KDialogBase::show();
mShown = true;
}
void KProgress::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }
void KProgressDialog::virtual_hook( int id, void* data )
{ KDialogBase::virtual_hook( id, data ); }
#include "kprogress.moc"
KProgressDialog::setMinimumDuration
void setMinimumDuration(int ms)
Set the minimum number of milliseconds to wait before actually showing the dialog.
Definition: kprogress.cpp:246
KProgress::setProgress
virtual void setProgress(int progress)
Set the current value of the progress bar to progress.
Definition: kprogress.cpp:74
KDialogBase::Close
Show Close-button.
Definition: kdialogbase.h:205
KProgress::format
QString format() const
Retrieve the current format for printing status text.
Definition: kprogress.cpp:119
KProgressDialog::~KProgressDialog
~KProgressDialog()
Destructor.
Definition: kprogress.cpp:205
KProgressDialog::ignoreCancel
void ignoreCancel()
Ignores the last cancel action if the cancel button was pressed.
Definition: kprogress.cpp:236
KProgressDialog::setLabel
void setLabel(const QString &text)
Sets the text in the dialog.
Definition: kprogress.cpp:293
KProgress::setTextEnabled
void setTextEnabled(bool)
If this is set to true, the progress text will be displayed.
Definition: kprogress.cpp:102
KDialogBase::setButtonCancel
void setButtonCancel(const KGuiItem &item=KStdGuiItem::cancel())
Sets the appearance of the Cancel button.
Definition: kdialogbase.cpp:970
KProgress
A progress indicator widget.
Definition: kprogress.h:46
KProgress::setRange
void setRange(int min, int max) KDE_DEPRECATED
Definition: kprogress.cpp:91
KProgress::setFormat
void setFormat(const QString &format)
Set the format of the text to use to display status.
Definition: kprogress.cpp:112
KProgress::~KProgress
~KProgress()
Destruct the progress bar.
Definition: kprogress.cpp:55
KDialogBase::Cancel
Show Cancel-button.
Definition: kdialogbase.h:204
klocale.h
KProgressDialog::progressBar
KProgress * progressBar()
Returns the KProgress used in this dialog.
Definition: kprogress.cpp:283
KDialogBase
A dialog base class with standard buttons and predefined layouts.
Definition: kdialogbase.h:191
KWin::setIcons
static void setIcons(WId win, const QPixmap &icon, const QPixmap &miniIcon)
KProgressDialog::autoClose
bool autoClose()
Returns true if the dialog will close upon completion, or false otherwise.
Definition: kprogress.cpp:315
KProgressDialog::setButtonText
void setButtonText(const QString &)
Sets the text to appear on the cancel button.
Definition: kprogress.cpp:346
KProgressDialog::buttonText
QString buttonText() KDE_DEPRECATED
Returns the text on the cancel button.
Definition: kprogress.cpp:353
KProgress::percentageChanged
void percentageChanged(int)
Emitted when the state of the progress bar changes.
KProgressDialog::showCancelButton
void showCancelButton(bool show)
Sets whether the cancel button is visible.
Definition: kprogress.cpp:309
KProgressDialog::setAutoReset
void setAutoReset(bool autoReset)
Sets whether the dialog should reset the KProgress dialog back to 0 steps compelete when all steps ha...
Definition: kprogress.cpp:341
KProgressDialog::wasCancelled
bool wasCancelled()
Returns true if the dialog was closed or canceled before completion.
Definition: kprogress.cpp:231
KDialogBase::actionButton
QPushButton * actionButton(ButtonCode id)
Returns the action button that corresponds to the id.
Definition: kdialogbase.cpp:832
KProgress::KProgress
KProgress(QWidget *parent=0, const char *name=0, WFlags f=0)
Construct a progress bar.
Definition: kprogress.cpp:41
KProgress::maxValue
int maxValue() KDE_DEPRECATED
Definition: kprogress.cpp:97
KProgressDialog::autoReset
bool autoReset()
Returns true if the KProgress widget will be reset upon completion, or false otherwise.
Definition: kprogress.cpp:331
KDialogBase::slotCancel
virtual void slotCancel()
Activated when the Cancel button has been clicked.
Definition: kdialogbase.cpp:1215
KDialogBase::showButton
void showButton(ButtonCode id, bool state)
Hide or display a general action button.
Definition: kdialogbase.cpp:866
KDialogBase::showButtonCancel
void showButtonCancel(bool state)
Hide or display the Cancel button.
Definition: kdialogbase.cpp:888
KProgressDialog::setAllowCancel
void setAllowCancel(bool allowCancel)
Sets whether or not the user can cancel the process.
Definition: kprogress.cpp:266
KProgress::setTotalSteps
void setTotalSteps(int totalSteps)
Set the current total number of steps in the action that the progress bar is representing.
Definition: kprogress.cpp:64
KProgressDialog::allowCancel
bool allowCancel() KDE_DEPRECATED
Returns true if the dialog can be canceled, false otherwise.
Definition: kprogress.cpp:273
KDialogBase::mainWidget
QWidget * mainWidget()
Returns the main widget if any.
Definition: kdialogbase.cpp:1464
KProgressDialog::minimumDuration
int minimumDuration() KDE_DEPRECATED
Returns the wait duration in milliseconds.
Definition: kprogress.cpp:256
KProgressDialog::KProgressDialog
KProgressDialog(QWidget *parent=0, const char *name=0, const QString &caption=QString::null, const QString &text=QString::null, bool modal=false)
Constructs a KProgressDialog.
Definition: kprogress.cpp:169
KProgressDialog::setAutoClose
void setAutoClose(bool close)
Sets whether the dialog should close automagically when all the steps in the KProgress have been comp...
Definition: kprogress.cpp:325
KProgressDialog::labelText
QString labelText() KDE_DEPRECATED
Returns the current dialog text.
Definition: kprogress.cpp:299
KProgress::advance
virtual void advance(int offset)
Advance the progress bar by offset.
Definition: kprogress.cpp:59
KProgressDialog::show
virtual void show()
Reimplemented for internal reasons, the API is not affected.
Definition: kprogress.cpp:401
KProgress::value
int value() const KDE_DEPRECATED
Definition: kprogress.cpp:125
KProgress::textEnabled
bool textEnabled() const
Returns true if progress text will be displayed, false otherwise.
Definition: kprogress.cpp:107
KDialogBase::plainPage
QFrame * plainPage()
Retrieve the empty page when the predefined layout is used in Plain mode.
Definition: kdialogbase.cpp:420
KDialogBase::finished
void finished()
The dialog has finished.
KProgress::setValue
void setValue(int progress)
Definition: kprogress.cpp:85

KDE Home | KDE Accessibility Home | Description of Access Keys