• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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))
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     button = new KPushButton("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; // TODO: we should make possible to show an informative label and the progress bar
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 #if 0 // currently unused
00223     if (unit==KJob::Bytes) {
00224         totalSize = amount;
00225     }
00226 #endif
00227 }
00228 
00229 void KStatusBarJobTracker::Private::ProgressWidget::percent(unsigned long percent)
00230 {
00231     progressBar->setValue(percent);
00232 }
00233 
00234 void KStatusBarJobTracker::Private::ProgressWidget::speed(unsigned long value)
00235 {
00236     if (value == 0 ) { // speed is measured in bytes-per-second
00237         label->setText(i18n(" Stalled "));
00238     } else {
00239         label->setText(i18n(" %1/s ", KGlobal::locale()->formatByteSize(value)));
00240     }
00241 }
00242 
00243 void KStatusBarJobTracker::Private::ProgressWidget::slotClean()
00244 {
00245     // we don't want to delete this widget, only clean
00246     progressBar->setValue(0);
00247     label->clear();
00248 
00249     setMode(KStatusBarJobTracker::NoInformation);
00250 }
00251 
00252 bool KStatusBarJobTracker::Private::ProgressWidget::eventFilter(QObject *obj, QEvent *event)
00253 {
00254     if (obj==progressBar || obj==label) {
00255 
00256         if (event->type() == QEvent::MouseButtonPress) {
00257             QMouseEvent *e = static_cast<QMouseEvent*>(event);
00258 
00259             // TODO: we should make possible to show an informative label and the progress bar
00260             if (e->button() == Qt::LeftButton) {    // toggle view on left mouse button
00261                 if (mode == KStatusBarJobTracker::LabelOnly) {
00262                     setMode(KStatusBarJobTracker::ProgressOnly);
00263                 } else if (mode == KStatusBarJobTracker::ProgressOnly) {
00264                     setMode(KStatusBarJobTracker::LabelOnly);
00265                 }
00266                 return true;
00267             }
00268         }
00269 
00270         return false;
00271     }
00272 
00273     return QWidget::eventFilter(obj, event);
00274 }
00275 
00276 #include "kstatusbarjobtracker.moc"
00277 #include "kstatusbarjobtracker_p.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal