File: kstatusbarjobtracker.cpp

package info (click to toggle)
kf6-kjobwidgets 6.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,456 kB
  • sloc: cpp: 3,288; xml: 238; sh: 14; ansic: 8; makefile: 7
file content (279 lines) | stat: -rw-r--r-- 7,500 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2000 Matej Koss <koss@miesto.sk>
    SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
    SPDX-FileCopyrightText: 2007 Rafael Fernández López <ereslibre@kde.org>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "kstatusbarjobtracker.h"
#include "kjobtrackerformatters_p.h"
#include "kstatusbarjobtracker_p.h"

#include <QCoreApplication>
#include <QLabel>
#include <QMouseEvent>
#include <QObject>
#include <QProgressBar>
#include <QPushButton>
#include <QStackedWidget>
#include <QWidget>

KStatusBarJobTracker::KStatusBarJobTracker(QWidget *parent, bool button)
    : KAbstractWidgetJobTracker(*new KStatusBarJobTrackerPrivate(this, parent, button), parent)
{
}

KStatusBarJobTracker::~KStatusBarJobTracker() = default;

void KStatusBarJobTracker::registerJob(KJob *job)
{
    Q_D(KStatusBarJobTracker);

    KAbstractWidgetJobTracker::registerJob(job);

    if (d->progressWidget.contains(job)) {
        return;
    }

    auto *vi = new KStatusBarJobTrackerPrivate::ProgressWidget(job, this, d->parent);
    d->currentProgressWidget = vi;

    d->progressWidget.insert(job, vi);
}

void KStatusBarJobTracker::unregisterJob(KJob *job)
{
    Q_D(KStatusBarJobTracker);

    KAbstractWidgetJobTracker::unregisterJob(job);

    if (!d->progressWidget.contains(job)) {
        return;
    }

    if (d->currentProgressWidget == d->progressWidget[job]) {
        d->currentProgressWidget = nullptr;
    }

    if (!d->progressWidget[job]->beingDeleted) {
        delete d->progressWidget[job];
    }

    d->progressWidget.remove(job);
}

QWidget *KStatusBarJobTracker::widget(KJob *job)
{
    Q_D(KStatusBarJobTracker);

    if (!d->progressWidget.contains(job)) {
        return nullptr;
    }

    return d->progressWidget[job];
}

void KStatusBarJobTracker::setStatusBarMode(StatusBarModes statusBarMode)
{
    Q_D(KStatusBarJobTracker);

    if (!d->currentProgressWidget) {
        return;
    }

    d->currentProgressWidget->setMode(statusBarMode);
}

void KStatusBarJobTracker::description(KJob *job, const QString &title, const QPair<QString, QString> &field1, const QPair<QString, QString> &field2)
{
    Q_D(KStatusBarJobTracker);

    if (!d->progressWidget.contains(job)) {
        return;
    }

    d->progressWidget[job]->description(title, field1, field2);
}

void KStatusBarJobTracker::totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
{
    Q_D(KStatusBarJobTracker);

    if (!d->progressWidget.contains(job)) {
        return;
    }

    d->progressWidget[job]->totalAmount(unit, amount);
}

void KStatusBarJobTracker::percent(KJob *job, unsigned long percent)
{
    Q_D(KStatusBarJobTracker);

    if (!d->progressWidget.contains(job)) {
        return;
    }

    d->progressWidget[job]->percent(percent);
}

void KStatusBarJobTracker::speed(KJob *job, unsigned long value)
{
    Q_D(KStatusBarJobTracker);

    if (!d->progressWidget.contains(job)) {
        return;
    }

    d->progressWidget[job]->speed(value);
}

void KStatusBarJobTracker::slotClean(KJob *job)
{
    Q_D(KStatusBarJobTracker);

    if (!d->progressWidget.contains(job)) {
        return;
    }

    d->progressWidget[job]->slotClean();
}

void KStatusBarJobTrackerPrivate::ProgressWidget::killJob()
{
    job->kill(KJob::EmitResult); // notify that the job has been killed
}

void KStatusBarJobTrackerPrivate::ProgressWidget::init(KJob *job, QWidget *parent)
{
    widget = new QWidget(parent);
    int w = fontMetrics().horizontalAdvance(QStringLiteral(" 999.9 kB/s 00:00:01 ")) + 8;
    box = new QHBoxLayout(widget);
    box->setContentsMargins(0, 0, 0, 0);
    box->setSpacing(0);

    stack = new QStackedWidget(widget);
    box->addWidget(stack);

    if (q->d_func()->showStopButton) {
        button = new QPushButton(QCoreApplication::translate("KStatusBarJobTracker", "Stop"), widget);
        box->addWidget(button);
        connect(button, &QPushButton::clicked, this, &KStatusBarJobTrackerPrivate::ProgressWidget::killJob);
    } else {
        button = nullptr;
    }

    progressBar = new QProgressBar(widget);
    progressBar->installEventFilter(this);
    progressBar->setMinimumWidth(w);
    stack->insertWidget(1, progressBar);

    label = new QLabel(widget);
    label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    label->installEventFilter(this);
    label->setMinimumWidth(w);
    stack->insertWidget(2, label);
    setMinimumSize(sizeHint());

    setMode(KStatusBarJobTracker::LabelOnly);

    q->setAutoDelete(job, true);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(widget);
}

void KStatusBarJobTrackerPrivate::ProgressWidget::setMode(KStatusBarJobTracker::StatusBarModes newMode)
{
    mode = newMode;

    if (newMode == KStatusBarJobTracker::NoInformation) {
        stack->hide();

        return;
    }

    if (newMode & KStatusBarJobTracker::LabelOnly) {
        stack->show();
        stack->setCurrentWidget(label);

        return; // TODO: we should make possible to show an informative label and the progress bar
    }

    if (newMode & KStatusBarJobTracker::ProgressOnly) {
        stack->show();
        stack->setCurrentWidget(progressBar);
    }
}

void KStatusBarJobTrackerPrivate::ProgressWidget::description(const QString &title,
                                                              const QPair<QString, QString> &field1,
                                                              const QPair<QString, QString> &field2)
{
    Q_UNUSED(field1);
    Q_UNUSED(field2);

    label->setText(title);
}

void KStatusBarJobTrackerPrivate::ProgressWidget::totalAmount(KJob::Unit unit, qulonglong amount)
{
    Q_UNUSED(unit);
    Q_UNUSED(amount);
#if 0 // currently unused
    if (unit == KJob::Bytes) {
        totalSize = amount;
    }
#endif
}

void KStatusBarJobTrackerPrivate::ProgressWidget::percent(unsigned long percent)
{
    progressBar->setValue(percent);
}

void KStatusBarJobTrackerPrivate::ProgressWidget::speed(unsigned long value)
{
    if (value == 0) { // speed is measured in bytes-per-second
        label->setText(QCoreApplication::translate("KStatusBarJobTracker", " Stalled "));
    } else {
        label->setText(QCoreApplication::translate("KStatusBarJobTracker", " %1/s ").arg(KJobTrackerFormatters::byteSize(value)));
    }
}

void KStatusBarJobTrackerPrivate::ProgressWidget::slotClean()
{
    // we don't want to delete this widget, only clean
    progressBar->setValue(0);
    label->clear();

    setMode(KStatusBarJobTracker::NoInformation);
}

bool KStatusBarJobTrackerPrivate::ProgressWidget::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == progressBar || obj == label) {
        if (event->type() == QEvent::MouseButtonPress) {
            QMouseEvent *e = static_cast<QMouseEvent *>(event);

            // TODO: we should make possible to show an informative label and the progress bar
            if (e->button() == Qt::LeftButton) { // toggle view on left mouse button
                if (mode == KStatusBarJobTracker::LabelOnly) {
                    setMode(KStatusBarJobTracker::ProgressOnly);
                } else if (mode == KStatusBarJobTracker::ProgressOnly) {
                    setMode(KStatusBarJobTracker::LabelOnly);
                }
                return true;
            }
        }

        return false;
    }

    return QWidget::eventFilter(obj, event);
}

#include "moc_kstatusbarjobtracker.cpp"
#include "moc_kstatusbarjobtracker_p.cpp"