KDEUI
kstatusbarjobtracker.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (C) 2000 Matej Koss <koss@miesto.sk> 00003 Copyright (C) 2007 Kevin Ottens <ervin@kde.org> 00004 Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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, button)) 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); // notify that the job has been killed 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 stack = new QStackedWidget(widget); 00157 box->addWidget(stack); 00158 00159 if (q->d->showStopButton) { 00160 button = new KPushButton(i18n("Stop"), widget); 00161 box->addWidget(button); 00162 connect(button, SIGNAL(clicked(bool)), 00163 this, SLOT(killJob())); 00164 } else { 00165 button = 0; 00166 } 00167 00168 progressBar = new QProgressBar(widget); 00169 progressBar->installEventFilter(this); 00170 progressBar->setMinimumWidth(w); 00171 stack->insertWidget(1, progressBar); 00172 00173 label = new QLabel(widget); 00174 label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); 00175 label->installEventFilter(this); 00176 label->setMinimumWidth(w); 00177 stack->insertWidget(2, label); 00178 setMinimumSize(sizeHint()); 00179 00180 setMode(KStatusBarJobTracker::LabelOnly); 00181 00182 q->setAutoDelete(job, true); 00183 00184 QVBoxLayout *layout = new QVBoxLayout; 00185 layout->addWidget(widget); 00186 setLayout(layout); 00187 } 00188 00189 void KStatusBarJobTracker::Private::ProgressWidget::setMode(StatusBarModes newMode) 00190 { 00191 mode = newMode; 00192 00193 if (newMode == KStatusBarJobTracker::NoInformation) 00194 { 00195 stack->hide(); 00196 00197 return; 00198 } 00199 00200 if (newMode & KStatusBarJobTracker::LabelOnly) 00201 { 00202 stack->show(); 00203 stack->setCurrentWidget(label); 00204 00205 return; // TODO: we should make possible to show an informative label and the progress bar 00206 } 00207 00208 if (newMode & KStatusBarJobTracker::ProgressOnly) 00209 { 00210 stack->show(); 00211 stack->setCurrentWidget(progressBar); 00212 } 00213 } 00214 00215 void KStatusBarJobTracker::Private::ProgressWidget::description(const QString &title, 00216 const QPair<QString, QString> &field1, 00217 const QPair<QString, QString> &field2) 00218 { 00219 Q_UNUSED(field1); 00220 Q_UNUSED(field2); 00221 00222 label->setText(title); 00223 } 00224 00225 void KStatusBarJobTracker::Private::ProgressWidget::totalAmount(KJob::Unit unit, qulonglong amount) 00226 { 00227 Q_UNUSED(unit); 00228 Q_UNUSED(amount); 00229 #if 0 // currently unused 00230 if (unit==KJob::Bytes) { 00231 totalSize = amount; 00232 } 00233 #endif 00234 } 00235 00236 void KStatusBarJobTracker::Private::ProgressWidget::percent(unsigned long percent) 00237 { 00238 progressBar->setValue(percent); 00239 } 00240 00241 void KStatusBarJobTracker::Private::ProgressWidget::speed(unsigned long value) 00242 { 00243 if (value == 0 ) { // speed is measured in bytes-per-second 00244 label->setText(i18n(" Stalled ")); 00245 } else { 00246 label->setText(i18n(" %1/s ", KGlobal::locale()->formatByteSize(value))); 00247 } 00248 } 00249 00250 void KStatusBarJobTracker::Private::ProgressWidget::slotClean() 00251 { 00252 // we don't want to delete this widget, only clean 00253 progressBar->setValue(0); 00254 label->clear(); 00255 00256 setMode(KStatusBarJobTracker::NoInformation); 00257 } 00258 00259 bool KStatusBarJobTracker::Private::ProgressWidget::eventFilter(QObject *obj, QEvent *event) 00260 { 00261 if (obj==progressBar || obj==label) { 00262 00263 if (event->type() == QEvent::MouseButtonPress) { 00264 QMouseEvent *e = static_cast<QMouseEvent*>(event); 00265 00266 // TODO: we should make possible to show an informative label and the progress bar 00267 if (e->button() == Qt::LeftButton) { // toggle view on left mouse button 00268 if (mode == KStatusBarJobTracker::LabelOnly) { 00269 setMode(KStatusBarJobTracker::ProgressOnly); 00270 } else if (mode == KStatusBarJobTracker::ProgressOnly) { 00271 setMode(KStatusBarJobTracker::LabelOnly); 00272 } 00273 return true; 00274 } 00275 } 00276 00277 return false; 00278 } 00279 00280 return QWidget::eventFilter(obj, event); 00281 } 00282 00283 #include "kstatusbarjobtracker.moc" 00284 #include "kstatusbarjobtracker_p.moc"
KDE 4.6 API Reference