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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • jobs
kstatusbarjobtracker.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  Copyright (C) 2000 Matej Koss <koss@miesto.sk>
3  Copyright (C) 2007 Kevin Ottens <ervin@kde.org>
4  Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 
20 */
21 
22 #include "kstatusbarjobtracker.h"
23 #include "kstatusbarjobtracker_p.h"
24 
25 #include <QWidget>
26 #include <QProgressBar>
27 #include <QLabel>
28 #include <QBoxLayout>
29 #include <QStackedWidget>
30 #include <QMouseEvent>
31 
32 #include <kpushbutton.h>
33 #include <klocale.h>
34 #include <kglobal.h>
35 
36 KStatusBarJobTracker::KStatusBarJobTracker(QWidget *parent, bool button)
37  : KAbstractWidgetJobTracker(parent), d(new Private(parent, button))
38 {
39 }
40 
41 KStatusBarJobTracker::~KStatusBarJobTracker()
42 {
43  delete d;
44 }
45 
46 void KStatusBarJobTracker::registerJob(KJob *job)
47 {
48  KAbstractWidgetJobTracker::registerJob(job);
49 
50  if (d->progressWidget.contains(job)) {
51  return;
52  }
53 
54  Private::ProgressWidget *vi = new Private::ProgressWidget(job, this, d->parent);
55  d->currentProgressWidget = vi;
56 
57  d->progressWidget.insert(job, vi);
58 }
59 
60 void KStatusBarJobTracker::unregisterJob(KJob *job)
61 {
62  KAbstractWidgetJobTracker::unregisterJob(job);
63 
64  if (!d->progressWidget.contains(job))
65  return;
66 
67  if (d->currentProgressWidget == d->progressWidget[job])
68  d->currentProgressWidget = 0;
69 
70  if (!d->progressWidget[job]->beingDeleted)
71  delete d->progressWidget[job];
72 
73  d->progressWidget.remove(job);
74 }
75 
76 QWidget *KStatusBarJobTracker::widget(KJob *job)
77 {
78  if (!d->progressWidget.contains(job)) {
79  return 0;
80  }
81 
82  return d->progressWidget[job];
83 }
84 
85 void KStatusBarJobTracker::setStatusBarMode(StatusBarModes statusBarMode)
86 {
87  if (!d->currentProgressWidget) {
88  return;
89  }
90 
91  d->currentProgressWidget->setMode(statusBarMode);
92 }
93 
94 void KStatusBarJobTracker::description(KJob *job, const QString &title,
95  const QPair<QString, QString> &field1,
96  const QPair<QString, QString> &field2)
97 {
98  if (!d->progressWidget.contains(job)) {
99  return;
100  }
101 
102  d->progressWidget[job]->description(title, field1, field2);
103 }
104 
105 void KStatusBarJobTracker::totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
106 {
107  if (!d->progressWidget.contains(job)) {
108  return;
109  }
110 
111  d->progressWidget[job]->totalAmount(unit, amount);
112 }
113 
114 void KStatusBarJobTracker::percent(KJob *job, unsigned long percent)
115 {
116  if (!d->progressWidget.contains(job)) {
117  return;
118  }
119 
120  d->progressWidget[job]->percent(percent);
121 }
122 
123 void KStatusBarJobTracker::speed(KJob *job, unsigned long value)
124 {
125  if (!d->progressWidget.contains(job)) {
126  return;
127  }
128 
129  d->progressWidget[job]->speed(value);
130 }
131 
132 void KStatusBarJobTracker::slotClean(KJob *job)
133 {
134  if (!d->progressWidget.contains(job)) {
135  return;
136  }
137 
138  d->progressWidget[job]->slotClean();
139 }
140 
141 void KStatusBarJobTracker::Private::ProgressWidget::killJob()
142 {
143  job->kill(KJob::EmitResult); // notify that the job has been killed
144 }
145 
146 void KStatusBarJobTracker::Private::ProgressWidget::init(KJob *job, QWidget *parent)
147 {
148  widget = new QWidget(parent);
149 
150  int w = fontMetrics().width( " 999.9 kB/s 00:00:01 " ) + 8;
151  box = new QHBoxLayout(widget);
152  box->setMargin(0);
153  box->setSpacing(0);
154  widget->setLayout(box);
155 
156  stack = new QStackedWidget(widget);
157  box->addWidget(stack);
158 
159  if (q->d->showStopButton) {
160  button = new KPushButton(i18n("Stop"), widget);
161  box->addWidget(button);
162  connect(button, SIGNAL(clicked(bool)),
163  this, SLOT(killJob()));
164  } else {
165  button = 0;
166  }
167 
168  progressBar = new QProgressBar(widget);
169  progressBar->installEventFilter(this);
170  progressBar->setMinimumWidth(w);
171  stack->insertWidget(1, progressBar);
172 
173  label = new QLabel(widget);
174  label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
175  label->installEventFilter(this);
176  label->setMinimumWidth(w);
177  stack->insertWidget(2, label);
178  setMinimumSize(sizeHint());
179 
180  setMode(KStatusBarJobTracker::LabelOnly);
181 
182  q->setAutoDelete(job, true);
183 
184  QVBoxLayout *layout = new QVBoxLayout;
185  layout->addWidget(widget);
186  setLayout(layout);
187 }
188 
189 void KStatusBarJobTracker::Private::ProgressWidget::setMode(StatusBarModes newMode)
190 {
191  mode = newMode;
192 
193  if (newMode == KStatusBarJobTracker::NoInformation)
194  {
195  stack->hide();
196 
197  return;
198  }
199 
200  if (newMode & KStatusBarJobTracker::LabelOnly)
201  {
202  stack->show();
203  stack->setCurrentWidget(label);
204 
205  return; // TODO: we should make possible to show an informative label and the progress bar
206  }
207 
208  if (newMode & KStatusBarJobTracker::ProgressOnly)
209  {
210  stack->show();
211  stack->setCurrentWidget(progressBar);
212  }
213 }
214 
215 void KStatusBarJobTracker::Private::ProgressWidget::description(const QString &title,
216  const QPair<QString, QString> &field1,
217  const QPair<QString, QString> &field2)
218 {
219  Q_UNUSED(field1);
220  Q_UNUSED(field2);
221 
222  label->setText(title);
223 }
224 
225 void KStatusBarJobTracker::Private::ProgressWidget::totalAmount(KJob::Unit unit, qulonglong amount)
226 {
227  Q_UNUSED(unit);
228  Q_UNUSED(amount);
229 #if 0 // currently unused
230  if (unit==KJob::Bytes) {
231  totalSize = amount;
232  }
233 #endif
234 }
235 
236 void KStatusBarJobTracker::Private::ProgressWidget::percent(unsigned long percent)
237 {
238  progressBar->setValue(percent);
239 }
240 
241 void KStatusBarJobTracker::Private::ProgressWidget::speed(unsigned long value)
242 {
243  if (value == 0 ) { // speed is measured in bytes-per-second
244  label->setText(i18n(" Stalled "));
245  } else {
246  label->setText(i18n(" %1/s ", KGlobal::locale()->formatByteSize(value)));
247  }
248 }
249 
250 void KStatusBarJobTracker::Private::ProgressWidget::slotClean()
251 {
252  // we don't want to delete this widget, only clean
253  progressBar->setValue(0);
254  label->clear();
255 
256  setMode(KStatusBarJobTracker::NoInformation);
257 }
258 
259 bool KStatusBarJobTracker::Private::ProgressWidget::eventFilter(QObject *obj, QEvent *event)
260 {
261  if (obj==progressBar || obj==label) {
262 
263  if (event->type() == QEvent::MouseButtonPress) {
264  QMouseEvent *e = static_cast<QMouseEvent*>(event);
265 
266  // TODO: we should make possible to show an informative label and the progress bar
267  if (e->button() == Qt::LeftButton) { // toggle view on left mouse button
268  if (mode == KStatusBarJobTracker::LabelOnly) {
269  setMode(KStatusBarJobTracker::ProgressOnly);
270  } else if (mode == KStatusBarJobTracker::ProgressOnly) {
271  setMode(KStatusBarJobTracker::LabelOnly);
272  }
273  return true;
274  }
275  }
276 
277  return false;
278  }
279 
280  return QWidget::eventFilter(obj, event);
281 }
282 
283 #include "kstatusbarjobtracker.moc"
284 #include "kstatusbarjobtracker_p.moc"
i18n
QString i18n(const char *text)
KPushButton
A QPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:46
KStatusBarJobTracker::slotClean
virtual void slotClean(KJob *job)
Definition: kstatusbarjobtracker.cpp:132
KStandardShortcut::label
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
Definition: kstandardshortcut.cpp:267
KStatusBarJobTracker::registerJob
virtual void registerJob(KJob *job)
Register a new job in this tracker.
Definition: kstatusbarjobtracker.cpp:46
QWidget
KAbstractWidgetJobTracker
The base class for widget based job trackers.
Definition: kabstractwidgetjobtracker.h:34
KStatusBarJobTracker::unregisterJob
virtual void unregisterJob(KJob *job)
Unregister a job from this tracker.
Definition: kstatusbarjobtracker.cpp:60
KStatusBarJobTracker::speed
virtual void speed(KJob *job, unsigned long value)
Definition: kstatusbarjobtracker.cpp:123
QString
KStatusBarJobTracker::ProgressOnly
Shows a progress bar with the job completion.
Definition: kstatusbarjobtracker.h:39
QObject
klocale.h
kglobal.h
KStatusBarJobTracker::description
virtual void description(KJob *job, const QString &title, const QPair< QString, QString > &field1, const QPair< QString, QString > &field2)
The following slots are inherited from KJobTrackerInterface.
Definition: kstatusbarjobtracker.cpp:94
KStatusBarJobTracker::setStatusBarMode
void setStatusBarMode(StatusBarModes statusBarMode)
Sets the mode of the status bar.
Definition: kstatusbarjobtracker.cpp:85
KStatusBarJobTracker::LabelOnly
Shows an informative label for job progress.
Definition: kstatusbarjobtracker.h:38
KStatusBarJobTracker::widget
virtual QWidget * widget(KJob *job)
The widget associated to this tracker.
Definition: kstatusbarjobtracker.cpp:76
KAbstractWidgetJobTracker::registerJob
virtual void registerJob(KJob *job)
Register a new job in this tracker.
Definition: kabstractwidgetjobtracker.cpp:43
KStatusBarJobTracker::NoInformation
Does not show any information.
Definition: kstatusbarjobtracker.h:37
kpushbutton.h
KGlobal::locale
KLocale * locale()
KStatusBarJobTracker::~KStatusBarJobTracker
virtual ~KStatusBarJobTracker()
Destroys a KStatusBarJobTracker.
Definition: kstatusbarjobtracker.cpp:41
KStatusBarJobTracker::totalAmount
virtual void totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
Definition: kstatusbarjobtracker.cpp:105
QLabel
KStatusBarJobTracker::percent
virtual void percent(KJob *job, unsigned long percent)
Definition: kstatusbarjobtracker.cpp:114
QPair
kstatusbarjobtracker.h
KAbstractWidgetJobTracker::unregisterJob
virtual void unregisterJob(KJob *job)
Unregister a job from this tracker.
Definition: kabstractwidgetjobtracker.cpp:48
KJob
KStatusBarJobTracker::KStatusBarJobTracker
KStatusBarJobTracker(QWidget *parent=0, bool button=true)
Creates a new KStatusBarJobTracker.
Definition: kstatusbarjobtracker.cpp:36
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal