File: progressdialog.cpp

package info (click to toggle)
libkdepim 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,716 kB
  • sloc: cpp: 2,747; xml: 35; makefile: 18; ansic: 9; sh: 1
file content (498 lines) | stat: -rw-r--r-- 14,976 bytes parent folder | download
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/** -*- c++ -*-
 * progressdialog.cpp
 *
 *  SPDX-FileCopyrightText: 2004 Till Adam <adam@kde.org>
 *  SPDX-FileCopyrightText: 2004 David Faure <faure@kde.org>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "progressdialog.h"
#include "ssllabel.h"

#include <KLocalizedString>
#include <QHBoxLayout>

#include <QCloseEvent>
#include <QFrame>
#include <QLabel>
#include <QLayout>
#include <QObject>
#include <QProgressBar>
#include <QPushButton>
#include <QScrollBar>
#include <QTimer>
#include <QVBoxLayout>
#include <chrono>

using namespace std::chrono_literals;

using namespace KPIM;
static const int MAX_LABEL_WIDTH = 650;

class KPIM::OverlayWidgetPrivate
{
public:
    OverlayWidgetPrivate() = default;

    QWidget *mAlignWidget = nullptr;
};

OverlayWidget::OverlayWidget(QWidget *alignWidget, QWidget *parent)
    : QFrame(parent)
    , d(new KPIM::OverlayWidgetPrivate)
{
    setAlignWidget(alignWidget);
    setLayout(new QHBoxLayout(this));
}

OverlayWidget::~OverlayWidget() = default;

QWidget *OverlayWidget::alignWidget() const
{
    return d->mAlignWidget;
}

void OverlayWidget::reposition()
{
    if (!d->mAlignWidget) {
        return;
    }
    // p is in the alignWidget's coordinates
    QPoint p;
    // We are always above the alignWidget, right-aligned with it for
    // LTR locales, and left-aligned for RTL locales (default value=0).
    if (layoutDirection() == Qt::LeftToRight) {
        p.setX(d->mAlignWidget->width() - width());
    }
    p.setY(-height());
    // Position in the toplevelwidget's coordinates
    QPoint pTopLevel = d->mAlignWidget->mapTo(topLevelWidget(), p);
    // Position in the widget's parentWidget coordinates
    QPoint pParent = parentWidget()->mapFrom(topLevelWidget(), pTopLevel);
    // Move 'this' to that position.
    move(pParent);
}

void OverlayWidget::setAlignWidget(QWidget *w)
{
    if (w == d->mAlignWidget) {
        return;
    }

    if (d->mAlignWidget) {
        d->mAlignWidget->removeEventFilter(this);
    }

    d->mAlignWidget = w;

    if (d->mAlignWidget) {
        d->mAlignWidget->installEventFilter(this);
    }

    reposition();
}

bool OverlayWidget::eventFilter(QObject *o, QEvent *e)
{
    if (o == d->mAlignWidget && (e->type() == QEvent::Move || e->type() == QEvent::Resize)) {
        reposition();
    }
    return QFrame::eventFilter(o, e);
}

void OverlayWidget::resizeEvent(QResizeEvent *ev)
{
    reposition();
    QFrame::resizeEvent(ev);
}

TransactionItemView::TransactionItemView(QWidget *parent, const QString &name)
    : QScrollArea(parent)
    , mBigBox(new QWidget(this))
{
    setObjectName(name);
    setFrameStyle(NoFrame);
    auto mBigBoxVBoxLayout = new QVBoxLayout(mBigBox);
    mBigBoxVBoxLayout->setContentsMargins(0, 0, 0, 0);
    setWidget(mBigBox);
    setWidgetResizable(true);
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}

TransactionItemView::~TransactionItemView()
{
    mBigBox = nullptr;
}

TransactionItem *TransactionItemView::addTransactionItem(ProgressItem *item, bool first)
{
    auto ti = new TransactionItem(mBigBox, item, first);
    mBigBox->layout()->addWidget(ti);

    resize(mBigBox->width(), mBigBox->height());

    return ti;
}

void TransactionItemView::resizeEvent(QResizeEvent *event)
{
    // Tell the layout in the parent (progressdialog) that our size changed
    updateGeometry();

    const QSize sz = parentWidget()->sizeHint();
    int currentWidth = parentWidget()->width();

    // Don't resize to sz.width() every time when it only reduces a little bit
    if (currentWidth < sz.width() || currentWidth > sz.width() + 100) {
        currentWidth = sz.width();
    }
    parentWidget()->resize(currentWidth, sz.height());

    QScrollArea::resizeEvent(event);
}

QSize TransactionItemView::sizeHint() const
{
    return minimumSizeHint();
}

QSize TransactionItemView::minimumSizeHint() const
{
    const int f = 2 * frameWidth();
    // Make room for a vertical scrollbar in all cases, to avoid a horizontal one
    const int vsbExt = verticalScrollBar()->sizeHint().width();
    const int minw = topLevelWidget()->width() / 3;
    const int maxh = topLevelWidget()->height() / 2;
    QSize sz(mBigBox->minimumSizeHint());
    sz.setWidth(qMax(sz.width(), minw) + f + vsbExt);
    sz.setHeight(qMin(sz.height(), maxh) + f);
    return sz;
}

void TransactionItemView::slotLayoutFirstItem()
{
    if (!mBigBox)
        return;
    // This slot is called whenever a TransactionItem is deleted, so this is a
    // good place to call updateGeometry(), so our parent takes the new size
    // into account and resizes.
    updateGeometry();

    /*
     The below relies on some details in Qt's behaviour regarding deleting
     objects. This slot is called from the destroyed signal of an item just
     going away. That item is at that point still in the  list of children, but
     since the vtable is already gone, it will have type QObject. The first
     one with both the right name and the right class therefore is what will
     be the first item very shortly. That's the one we want to remove the
     hline for.
    */
    auto ti = mBigBox->findChild<KPIM::TransactionItem *>(QStringLiteral("TransactionItem"));
    if (ti) {
        ti->hideHLine();
    }
}

// ----------------------------------------------------------------------------

TransactionItem::TransactionItem(QWidget *parent, ProgressItem *item, bool first)
    : QWidget(parent)
    , mItem(item)
{
    auto vboxLayout = new QVBoxLayout(this);
    vboxLayout->setSpacing(2);
    vboxLayout->setContentsMargins(2, 2, 2, 2);
    setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));

    mFrame = new QFrame(this);
    mFrame->setFrameShape(QFrame::HLine);
    mFrame->setFrameShadow(QFrame::Raised);
    mFrame->show();
    layout()->addWidget(mFrame);

    auto h = new QWidget(this);
    auto hHBoxLayout = new QHBoxLayout(h);
    hHBoxLayout->setContentsMargins(0, 0, 0, 0);
    hHBoxLayout->setSpacing(5);
    layout()->addWidget(h);

    mItemLabel = new QLabel(fontMetrics().elidedText(item->label(), Qt::ElideRight, MAX_LABEL_WIDTH), h);
    h->layout()->addWidget(mItemLabel);
    h->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));

    mProgress = new QProgressBar(h);
    hHBoxLayout->addWidget(mProgress);
    mProgress->setFormat(i18nc("Percent value; %p is the value, % is the percent sign", "%p%"));
    mProgress->setMaximum(100);
    mProgress->setValue(item->progress());
    h->layout()->addWidget(mProgress);

    if (item->canBeCanceled()) {
        mCancelButton = new QPushButton(QIcon::fromTheme(QStringLiteral("dialog-cancel")), QString(), h);
        hHBoxLayout->addWidget(mCancelButton);
        mCancelButton->setToolTip(i18nc("@info:tooltip", "Cancel this operation."));
        connect(mCancelButton, &QAbstractButton::clicked, this, &TransactionItem::slotItemCanceled);
        h->layout()->addWidget(mCancelButton);
    }

    h = new QWidget(this);
    hHBoxLayout = new QHBoxLayout(h);
    hHBoxLayout->setContentsMargins(0, 0, 0, 0);
    hHBoxLayout->setSpacing(5);
    h->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
    layout()->addWidget(h);
    mSSLLabel = new SSLLabel(h);
    hHBoxLayout->addWidget(mSSLLabel);
    mSSLLabel->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    h->layout()->addWidget(mSSLLabel);
    mItemStatus = new QLabel(h);
    hHBoxLayout->addWidget(mItemStatus);
    mItemStatus->setTextFormat(Qt::RichText);
    mItemStatus->setText(fontMetrics().elidedText(item->status(), Qt::ElideRight, MAX_LABEL_WIDTH));
    h->layout()->addWidget(mItemStatus);
    setCryptoStatus(item->cryptoStatus());
    if (first) {
        hideHLine();
    }
}

KPIM::TransactionItem::~TransactionItem() = default;

void TransactionItem::hideHLine()
{
    mFrame->hide();
}

void TransactionItem::setProgress(int progress)
{
    mProgress->setValue(progress);
}

void TransactionItem::setLabel(const QString &label)
{
    mItemLabel->setText(fontMetrics().elidedText(label, Qt::ElideRight, MAX_LABEL_WIDTH));
}

void TransactionItem::setStatus(const QString &status)
{
    mItemStatus->setText(fontMetrics().elidedText(status, Qt::ElideRight, MAX_LABEL_WIDTH));
}

void TransactionItem::setCryptoStatus(KPIM::ProgressItem::CryptoStatus status)
{
    switch (status) {
    case KPIM::ProgressItem::Encrypted:
        mSSLLabel->setEncrypted(SSLLabel::Encrypted);
        break;
    case KPIM::ProgressItem::Unencrypted:
        mSSLLabel->setEncrypted(SSLLabel::Unencrypted);
        break;
    case KPIM::ProgressItem::Unknown:
        mSSLLabel->setEncrypted(SSLLabel::Unknown);
        break;
    }
    mSSLLabel->setState(mSSLLabel->lastState());
}

void TransactionItem::setTotalSteps(int totalSteps)
{
    mProgress->setMaximum(totalSteps);
}

void TransactionItem::slotItemCanceled()
{
    if (mItem) {
        mItem->cancel();
    }
}

void TransactionItem::addSubTransaction(ProgressItem *item)
{
    Q_UNUSED(item)
}

// ---------------------------------------------------------------------------

ProgressDialog::ProgressDialog(QWidget *alignWidget, QWidget *parent)
    : OverlayWidget(alignWidget, parent)
{
    // Qt Bug: Sunken is not applied for RTL layouts correctly (is not mirrored).
    // For now let's just use Plain, which is fine for this.
    if (layoutDirection() == Qt::LeftToRight) {
        setFrameStyle(QFrame::Panel | QFrame::Sunken); // QFrame
    } else {
        setFrameStyle(QFrame::Panel | QFrame::Plain); // QFrame
    }

    setAutoFillBackground(true);

    mScrollView = new TransactionItemView(this, QStringLiteral("ProgressScrollView"));
    layout()->addWidget(mScrollView);
    /*
     * Get the singleton ProgressManager item which will inform us of
     * appearing and vanishing items.
     */
    ProgressManager *pm = ProgressManager::instance();
    connect(pm, &ProgressManager::progressItemAdded, this, &ProgressDialog::slotTransactionAdded);
    connect(pm, &ProgressManager::progressItemCompleted, this, &ProgressDialog::slotTransactionCompleted);
    connect(pm, &ProgressManager::progressItemProgress, this, &ProgressDialog::slotTransactionProgress);
    connect(pm, &ProgressManager::progressItemStatus, this, &ProgressDialog::slotTransactionStatus);
    connect(pm, &ProgressManager::progressItemLabel, this, &ProgressDialog::slotTransactionLabel);
    connect(pm, &ProgressManager::progressItemCryptoStatus, this, &ProgressDialog::slotTransactionCryptoStatus);
    connect(pm, &ProgressManager::progressItemUsesBusyIndicator, this, &ProgressDialog::slotTransactionUsesBusyIndicator);
    connect(pm, &ProgressManager::showProgressDialog, this, &ProgressDialog::slotShow);
}

void ProgressDialog::closeEvent(QCloseEvent *e)
{
    e->accept();
    hide();
}

bool ProgressDialog::wasLastShown() const
{
    return mWasLastShown;
}

/*
 *  Destructor
 */
ProgressDialog::~ProgressDialog()
{
    for (auto &conn : std::as_const(mConnections))
        QObject::disconnect(conn);
}

void ProgressDialog::setShowTypeProgressItem(unsigned int type)
{
    mShowTypeProgressItem = type;
}

void ProgressDialog::slotTransactionAdded(ProgressItem *item)
{
    if (item->typeProgressItem() == mShowTypeProgressItem) {
        if (item->parent()) {
            if (TransactionItem *parent = mTransactionsToListviewItems.value(item->parent())) {
                parent->addSubTransaction(item);
            }
        } else {
            const bool first = mTransactionsToListviewItems.empty();
            TransactionItem *ti = mScrollView->addTransactionItem(item, first);
            if (ti) {
                mTransactionsToListviewItems.insert(item, ti);
            }
            if (first && mWasLastShown) {
                QTimer::singleShot(1s, this, &ProgressDialog::slotShow);
            }
        }
    }
}

void ProgressDialog::slotTransactionCompleted(ProgressItem *item)
{
    if (TransactionItem *ti = mTransactionsToListviewItems.value(item)) {
        mTransactionsToListviewItems.remove(item);
        ti->setItemComplete();
        QTimer::singleShot(3s, ti, &QObject::deleteLater);
        // see the slot for comments as to why that works
        mConnections << connect(ti, &QObject::destroyed, mScrollView, &TransactionItemView::slotLayoutFirstItem);
    }
    // This was the last item, hide.
    if (mTransactionsToListviewItems.empty()) {
        QTimer::singleShot(3s, this, &ProgressDialog::slotHide);
    }
}

void ProgressDialog::slotTransactionCanceled(ProgressItem *)
{
}

void ProgressDialog::slotTransactionProgress(ProgressItem *item, unsigned int progress)
{
    if (TransactionItem *ti = mTransactionsToListviewItems.value(item)) {
        ti->setProgress(progress);
    }
}

void ProgressDialog::slotTransactionStatus(ProgressItem *item, const QString &status)
{
    if (TransactionItem *ti = mTransactionsToListviewItems.value(item)) {
        ti->setStatus(status);
    }
}

void ProgressDialog::slotTransactionLabel(ProgressItem *item, const QString &label)
{
    if (TransactionItem *ti = mTransactionsToListviewItems.value(item)) {
        ti->setLabel(label);
    }
}

void ProgressDialog::slotTransactionCryptoStatus(ProgressItem *item, KPIM::ProgressItem::CryptoStatus value)
{
    if (TransactionItem *ti = mTransactionsToListviewItems.value(item)) {
        ti->setCryptoStatus(value);
    }
}

void ProgressDialog::slotTransactionUsesBusyIndicator(KPIM::ProgressItem *item, bool value)
{
    if (TransactionItem *ti = mTransactionsToListviewItems.value(item)) {
        if (value) {
            ti->setTotalSteps(0);
        } else {
            ti->setTotalSteps(100);
        }
    }
}

void ProgressDialog::slotShow()
{
    setVisible(true);
}

void ProgressDialog::slotHide()
{
    // check if a new item showed up since we started the timer. If not, hide
    if (mTransactionsToListviewItems.isEmpty()) {
        setVisible(false);
    }

    // while we're here, clean up the mConnections list
    for (auto it = mConnections.begin(); it != mConnections.end();) {
        if (!*it) { // operator bool tell us if it's still valid
            it = mConnections.erase(it);
        } else {
            ++it;
        }
    }
}

void ProgressDialog::slotClose()
{
    mWasLastShown = false;
    setVisible(false);
}

void ProgressDialog::setVisible(bool b)
{
    OverlayWidget::setVisible(b);
    Q_EMIT visibilityChanged(b);
}

void ProgressDialog::slotToggleVisibility()
{
    /* Since we are only hiding with a timeout, there is a short period of
     * time where the last item is still visible, but clicking on it in
     * the statusbarwidget should not display the dialog, because there
     * are no items to be shown anymore. Guard against that.
     */
    if (!isHidden() || !mTransactionsToListviewItems.isEmpty()) {
        const bool showNow = isHidden();
        setVisible(showNow);
        mWasLastShown = showNow;
    }
}

#include "moc_progressdialog.cpp"