KJobWidgets

kstatusbarjobtracker.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2000 Matej Koss <koss@miesto.sk>
4 SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
5 SPDX-FileCopyrightText: 2007 Rafael Fernández López <ereslibre@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-only
8*/
9
10#include "kstatusbarjobtracker.h"
11#include "kjobtrackerformatters_p.h"
12#include "kstatusbarjobtracker_p.h"
13
14#include <QCoreApplication>
15#include <QLabel>
16#include <QMouseEvent>
17#include <QObject>
18#include <QProgressBar>
19#include <QPushButton>
20#include <QStackedWidget>
21#include <QWidget>
22
24 : KAbstractWidgetJobTracker(*new KStatusBarJobTrackerPrivate(this, parent, button), parent)
25{
26}
27
29
31{
33
35
36 if (d->progressWidget.contains(job)) {
37 return;
38 }
39
40 auto *vi = new KStatusBarJobTrackerPrivate::ProgressWidget(job, this, d->parent);
41 d->currentProgressWidget = vi;
42
43 d->progressWidget.insert(job, vi);
44}
45
47{
49
51
52 if (!d->progressWidget.contains(job)) {
53 return;
54 }
55
56 if (d->currentProgressWidget == d->progressWidget[job]) {
57 d->currentProgressWidget = nullptr;
58 }
59
60 if (!d->progressWidget[job]->beingDeleted) {
61 delete d->progressWidget[job];
62 }
63
64 d->progressWidget.remove(job);
65}
66
68{
70
71 if (!d->progressWidget.contains(job)) {
72 return nullptr;
73 }
74
75 return d->progressWidget[job];
76}
77
79{
81
82 if (!d->currentProgressWidget) {
83 return;
84 }
85
86 d->currentProgressWidget->setMode(statusBarMode);
87}
88
89void KStatusBarJobTracker::description(KJob *job, const QString &title, const QPair<QString, QString> &field1, const QPair<QString, QString> &field2)
90{
92
93 if (!d->progressWidget.contains(job)) {
94 return;
95 }
96
97 d->progressWidget[job]->description(title, field1, field2);
98}
99
100void KStatusBarJobTracker::totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
101{
103
104 if (!d->progressWidget.contains(job)) {
105 return;
106 }
107
108 d->progressWidget[job]->totalAmount(unit, amount);
109}
110
111void KStatusBarJobTracker::percent(KJob *job, unsigned long percent)
112{
114
115 if (!d->progressWidget.contains(job)) {
116 return;
117 }
118
119 d->progressWidget[job]->percent(percent);
120}
121
122void KStatusBarJobTracker::speed(KJob *job, unsigned long value)
123{
125
126 if (!d->progressWidget.contains(job)) {
127 return;
128 }
129
130 d->progressWidget[job]->speed(value);
131}
132
133void KStatusBarJobTracker::slotClean(KJob *job)
134{
136
137 if (!d->progressWidget.contains(job)) {
138 return;
139 }
140
141 d->progressWidget[job]->slotClean();
142}
143
144void KStatusBarJobTrackerPrivate::ProgressWidget::killJob()
145{
146 job->kill(KJob::EmitResult); // notify that the job has been killed
147}
148
149void KStatusBarJobTrackerPrivate::ProgressWidget::init(KJob *job, QWidget *parent)
150{
151 widget = new QWidget(parent);
152 int w = fontMetrics().horizontalAdvance(QStringLiteral(" 999.9 kB/s 00:00:01 ")) + 8;
153 box = new QHBoxLayout(widget);
154 box->setContentsMargins(0, 0, 0, 0);
155 box->setSpacing(0);
156
157 stack = new QStackedWidget(widget);
158 box->addWidget(stack);
159
160 if (q->d_func()->showStopButton) {
161 button = new QPushButton(QCoreApplication::translate("KStatusBarJobTracker", "Stop"), widget);
162 box->addWidget(button);
163 connect(button, &QPushButton::clicked, this, &KStatusBarJobTrackerPrivate::ProgressWidget::killJob);
164 } else {
165 button = nullptr;
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
181
182 q->setAutoDelete(job, true);
183
184 QVBoxLayout *layout = new QVBoxLayout(this);
185 layout->addWidget(widget);
186}
187
188void KStatusBarJobTrackerPrivate::ProgressWidget::setMode(KStatusBarJobTracker::StatusBarModes newMode)
189{
190 mode = newMode;
191
193 stack->hide();
194
195 return;
196 }
197
198 if (newMode & KStatusBarJobTracker::LabelOnly) {
199 stack->show();
200 stack->setCurrentWidget(label);
201
202 return; // TODO: we should make possible to show an informative label and the progress bar
203 }
204
206 stack->show();
207 stack->setCurrentWidget(progressBar);
208 }
209}
210
211void KStatusBarJobTrackerPrivate::ProgressWidget::description(const QString &title,
212 const QPair<QString, QString> &field1,
213 const QPair<QString, QString> &field2)
214{
215 Q_UNUSED(field1);
216 Q_UNUSED(field2);
217
218 label->setText(title);
219}
220
221void KStatusBarJobTrackerPrivate::ProgressWidget::totalAmount(KJob::Unit unit, qulonglong amount)
222{
223 Q_UNUSED(unit);
224 Q_UNUSED(amount);
225#if 0 // currently unused
226 if (unit == KJob::Bytes) {
227 totalSize = amount;
228 }
229#endif
230}
231
232void KStatusBarJobTrackerPrivate::ProgressWidget::percent(unsigned long percent)
233{
234 progressBar->setValue(percent);
235}
236
237void KStatusBarJobTrackerPrivate::ProgressWidget::speed(unsigned long value)
238{
239 if (value == 0) { // speed is measured in bytes-per-second
240 label->setText(QCoreApplication::translate("KStatusBarJobTracker", " Stalled "));
241 } else {
242 label->setText(QCoreApplication::translate("KStatusBarJobTracker", " %1/s ").arg(KJobTrackerFormatters::byteSize(value)));
243 }
244}
245
246void KStatusBarJobTrackerPrivate::ProgressWidget::slotClean()
247{
248 // we don't want to delete this widget, only clean
249 progressBar->setValue(0);
250 label->clear();
251
253}
254
255bool KStatusBarJobTrackerPrivate::ProgressWidget::eventFilter(QObject *obj, QEvent *event)
256{
257 if (obj == progressBar || obj == label) {
258 if (event->type() == QEvent::MouseButtonPress) {
259 QMouseEvent *e = static_cast<QMouseEvent *>(event);
260
261 // TODO: we should make possible to show an informative label and the progress bar
262 if (e->button() == Qt::LeftButton) { // toggle view on left mouse button
265 } else if (mode == KStatusBarJobTracker::ProgressOnly) {
267 }
268 return true;
269 }
270 }
271
272 return false;
273 }
274
275 return QWidget::eventFilter(obj, event);
276}
277
278#include "moc_kstatusbarjobtracker.cpp"
279#include "moc_kstatusbarjobtracker_p.cpp"
The base class for widget based job trackers.
void unregisterJob(KJob *job) override
Unregister a job from this tracker.
void registerJob(KJob *job) override
Register a new job in this tracker.
bool kill(KJob::KillVerbosity verbosity=KJob::Quietly)
This class implements a job tracker with a widget suited for embedding in a status bar.
KStatusBarJobTracker(QWidget *parent=nullptr, bool button=true)
Creates a new KStatusBarJobTracker.
~KStatusBarJobTracker() override
Destroys a KStatusBarJobTracker.
@ ProgressOnly
Shows a progress bar with the job completion.
@ NoInformation
Does not show any information.
@ LabelOnly
Shows an informative label for job progress.
QWidget * widget(KJob *job) override
The widget associated to this tracker.
virtual void description(KJob *job, const QString &title, const QPair< QString, QString > &field1, const QPair< QString, QString > &field2) override
The following slots are inherited from KJobTrackerInterface.
void setStatusBarMode(StatusBarModes statusBarMode)
Sets the mode of the status bar.
void registerJob(KJob *job) override
Register a new job in this tracker.
void unregisterJob(KJob *job) override
Unregister a job from this tracker.
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QString label(StandardShortcut id)
void clicked(bool checked)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
MouseButtonPress
virtual bool eventFilter(QObject *watched, QEvent *event)
QObject * parent() const const
Qt::MouseButton button() const const
void clear()
AlignHCenter
LeftButton
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:04 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.