KDEUI
kstatusbarjobtracker.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kstatusbarjobtracker.h"
00023 #include "kstatusbarjobtracker_p.h"
00024
00025 #include <QWidget>
00026 #include <QProgressBar>
00027 #include <QLabel>
00028 #include <QBoxLayout>
00029 #include <QStackedWidget>
00030 #include <QMouseEvent>
00031
00032 #include <kpushbutton.h>
00033 #include <klocale.h>
00034 #include <kglobal.h>
00035
00036 KStatusBarJobTracker::KStatusBarJobTracker(QWidget *parent, bool button)
00037 : KAbstractWidgetJobTracker(parent), d(new Private(parent))
00038 {
00039 }
00040
00041 KStatusBarJobTracker::~KStatusBarJobTracker()
00042 {
00043 delete d;
00044 }
00045
00046 void KStatusBarJobTracker::registerJob(KJob *job)
00047 {
00048 KAbstractWidgetJobTracker::registerJob(job);
00049
00050 if (d->progressWidget.contains(job)) {
00051 return;
00052 }
00053
00054 Private::ProgressWidget *vi = new Private::ProgressWidget(job, this, d->parent);
00055 d->currentProgressWidget = vi;
00056
00057 d->progressWidget.insert(job, vi);
00058 }
00059
00060 void KStatusBarJobTracker::unregisterJob(KJob *job)
00061 {
00062 KAbstractWidgetJobTracker::unregisterJob(job);
00063
00064 if (!d->progressWidget.contains(job))
00065 return;
00066
00067 if (d->currentProgressWidget == d->progressWidget[job])
00068 d->currentProgressWidget = 0;
00069
00070 if (!d->progressWidget[job]->beingDeleted)
00071 delete d->progressWidget[job];
00072
00073 d->progressWidget.remove(job);
00074 }
00075
00076 QWidget *KStatusBarJobTracker::widget(KJob *job)
00077 {
00078 if (!d->progressWidget.contains(job)) {
00079 return 0;
00080 }
00081
00082 return d->progressWidget[job];
00083 }
00084
00085 void KStatusBarJobTracker::setStatusBarMode(StatusBarModes statusBarMode)
00086 {
00087 if (!d->currentProgressWidget) {
00088 return;
00089 }
00090
00091 d->currentProgressWidget->setMode(statusBarMode);
00092 }
00093
00094 void KStatusBarJobTracker::description(KJob *job, const QString &title,
00095 const QPair<QString, QString> &field1,
00096 const QPair<QString, QString> &field2)
00097 {
00098 if (!d->progressWidget.contains(job)) {
00099 return;
00100 }
00101
00102 d->progressWidget[job]->description(title, field1, field2);
00103 }
00104
00105 void KStatusBarJobTracker::totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
00106 {
00107 if (!d->progressWidget.contains(job)) {
00108 return;
00109 }
00110
00111 d->progressWidget[job]->totalAmount(unit, amount);
00112 }
00113
00114 void KStatusBarJobTracker::percent(KJob *job, unsigned long percent)
00115 {
00116 if (!d->progressWidget.contains(job)) {
00117 return;
00118 }
00119
00120 d->progressWidget[job]->percent(percent);
00121 }
00122
00123 void KStatusBarJobTracker::speed(KJob *job, unsigned long value)
00124 {
00125 if (!d->progressWidget.contains(job)) {
00126 return;
00127 }
00128
00129 d->progressWidget[job]->speed(value);
00130 }
00131
00132 void KStatusBarJobTracker::slotClean(KJob *job)
00133 {
00134 if (!d->progressWidget.contains(job)) {
00135 return;
00136 }
00137
00138 d->progressWidget[job]->slotClean();
00139 }
00140
00141 void KStatusBarJobTracker::Private::ProgressWidget::killJob()
00142 {
00143 job->kill(KJob::EmitResult);
00144 }
00145
00146 void KStatusBarJobTracker::Private::ProgressWidget::init(KJob *job, QWidget *parent)
00147 {
00148 widget = new QWidget(parent);
00149
00150 int w = fontMetrics().width( " 999.9 kB/s 00:00:01 " ) + 8;
00151 box = new QHBoxLayout(widget);
00152 box->setMargin(0);
00153 box->setSpacing(0);
00154 widget->setLayout(box);
00155
00156 button = new KPushButton(i18n("Stop"), widget);
00157 box->addWidget(button);
00158 stack = new QStackedWidget(widget);
00159 box->addWidget(stack);
00160 connect(button, SIGNAL(clicked(bool)),
00161 this, SLOT(killJob()));
00162
00163 progressBar = new QProgressBar(widget);
00164 progressBar->installEventFilter(this);
00165 progressBar->setMinimumWidth(w);
00166 stack->insertWidget(1, progressBar);
00167
00168 label = new QLabel("", widget);
00169 label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
00170 label->installEventFilter(this);
00171 label->setMinimumWidth(w);
00172 stack->insertWidget(2, label);
00173 setMinimumSize(sizeHint());
00174
00175 setMode(KStatusBarJobTracker::LabelOnly);
00176
00177 q->setAutoDelete(job, true);
00178
00179 QVBoxLayout *layout = new QVBoxLayout;
00180 layout->addWidget(widget);
00181 setLayout(layout);
00182 }
00183
00184 void KStatusBarJobTracker::Private::ProgressWidget::setMode(StatusBarModes newMode)
00185 {
00186 mode = newMode;
00187
00188 if (newMode == KStatusBarJobTracker::NoInformation)
00189 {
00190 stack->hide();
00191
00192 return;
00193 }
00194
00195 if (newMode & KStatusBarJobTracker::LabelOnly)
00196 {
00197 stack->show();
00198 stack->setCurrentWidget(label);
00199
00200 return;
00201 }
00202
00203 if (newMode & KStatusBarJobTracker::ProgressOnly)
00204 {
00205 stack->show();
00206 stack->setCurrentWidget(progressBar);
00207 }
00208 }
00209
00210 void KStatusBarJobTracker::Private::ProgressWidget::description(const QString &title,
00211 const QPair<QString, QString> &field1,
00212 const QPair<QString, QString> &field2)
00213 {
00214 Q_UNUSED(field1);
00215 Q_UNUSED(field2);
00216
00217 label->setText(title);
00218 }
00219
00220 void KStatusBarJobTracker::Private::ProgressWidget::totalAmount(KJob::Unit unit, qulonglong amount)
00221 {
00222 Q_UNUSED(unit);
00223 Q_UNUSED(amount);
00224 #if 0 // currently unused
00225 if (unit==KJob::Bytes) {
00226 totalSize = amount;
00227 }
00228 #endif
00229 }
00230
00231 void KStatusBarJobTracker::Private::ProgressWidget::percent(unsigned long percent)
00232 {
00233 progressBar->setValue(percent);
00234 }
00235
00236 void KStatusBarJobTracker::Private::ProgressWidget::speed(unsigned long value)
00237 {
00238 if (value == 0 ) {
00239 label->setText(i18n(" Stalled "));
00240 } else {
00241 label->setText(i18n(" %1/s ", KGlobal::locale()->formatByteSize(value)));
00242 }
00243 }
00244
00245 void KStatusBarJobTracker::Private::ProgressWidget::slotClean()
00246 {
00247
00248 progressBar->setValue(0);
00249 label->clear();
00250
00251 setMode(KStatusBarJobTracker::NoInformation);
00252 }
00253
00254 bool KStatusBarJobTracker::Private::ProgressWidget::eventFilter(QObject *obj, QEvent *event)
00255 {
00256 if (obj==progressBar || obj==label) {
00257
00258 if (event->type() == QEvent::MouseButtonPress) {
00259 QMouseEvent *e = static_cast<QMouseEvent*>(event);
00260
00261
00262 if (e->button() == Qt::LeftButton) {
00263 if (mode == KStatusBarJobTracker::LabelOnly) {
00264 setMode(KStatusBarJobTracker::ProgressOnly);
00265 } else if (mode == KStatusBarJobTracker::ProgressOnly) {
00266 setMode(KStatusBarJobTracker::LabelOnly);
00267 }
00268 return true;
00269 }
00270 }
00271
00272 return false;
00273 }
00274
00275 return QWidget::eventFilter(obj, event);
00276 }
00277
00278 #include "kstatusbarjobtracker.moc"
00279 #include "kstatusbarjobtracker_p.moc"