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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2012-01-13
* Description : progress manager
*
* SPDX-FileCopyrightText: 2007-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
* SPDX-FileCopyrightText: 2004 by Till Adam <adam at kde dot org>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "progressview.h"
// Qt includes
#include <QApplication>
#include <QCloseEvent>
#include <QEvent>
#include <QFrame>
#include <QLabel>
#include <QLayout>
#include <QObject>
#include <QProgressBar>
#include <QPushButton>
#include <QScrollBar>
#include <QTimer>
#include <QToolButton>
#include <QMap>
#include <QPixmap>
#include <QIcon>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "digikam_debug.h"
#include "progressmanager.h"
namespace Digikam
{
class TransactionItem;
TransactionItemView::TransactionItemView(QWidget* const parent, const QString& name)
: QScrollArea(parent)
{
setObjectName(name);
setFrameStyle(NoFrame);
m_bigBox = new DVBox(this);
setWidget(m_bigBox);
setWidgetResizable(true);
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}
TransactionItem* TransactionItemView::addTransactionItem(ProgressItem* const item, bool first)
{
TransactionItem* const ti = new TransactionItem(m_bigBox, item, first);
m_bigBox->layout()->addWidget(ti);
resize(m_bigBox->width(), m_bigBox->height());
return ti;
}
void TransactionItemView::resizeEvent(QResizeEvent* event)
{
// Tell the layout in the parent (progressview) that our size changed
updateGeometry();
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
{
int f = 2 * frameWidth();
// Make room for a vertical scrollbar in all cases, to avoid a horizontal one
int vsbExt = verticalScrollBar()->sizeHint().width();
int minw = topLevelWidget()->width() / 3;
int maxh = topLevelWidget()->height() / 2;
QSize sz( m_bigBox->minimumSizeHint() );
sz.setWidth( qMax( sz.width(), minw ) + f + vsbExt );
sz.setHeight( qMin( sz.height(), maxh ) + f );
return sz;
}
void TransactionItemView::slotLayoutFirstItem()
{
// 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.
*/
TransactionItem* const ti = m_bigBox->findChild<TransactionItem*>(QLatin1String("TransactionItem"));
if (ti)
{
ti->hideHLine();
}
else
{
Q_EMIT signalTransactionViewIsEmpty();
}
}
// ----------------------------------------------------------------------------
class Q_DECL_HIDDEN TransactionItem::Private
{
public:
Private() = default;
const int maxLabelWidth = 650;
QProgressBar* progress = nullptr;
QPushButton* cancelButton = nullptr;
QLabel* itemLabel = nullptr;
QLabel* itemStatus = nullptr;
QLabel* itemThumb = nullptr;
QFrame* frame = nullptr;
ProgressItem* item = nullptr;
};
TransactionItem::TransactionItem(QWidget* const parent, ProgressItem* const item, bool first)
: DVBox(parent),
d (new Private)
{
d->item = item;
setSpacing(2);
setContentsMargins(2, 2, 2, 2);
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
d->frame = new QFrame(this);
d->frame->setFrameShape(QFrame::HLine);
d->frame->setFrameShadow(QFrame::Raised);
d->frame->show();
setStretchFactor(d->frame, 3);
layout()->addWidget(d->frame);
DHBox* h = new DHBox(this);
h->setSpacing(5);
layout()->addWidget(h);
if (item->hasThumbnail())
{
d->itemThumb = new QLabel(h);
d->itemThumb->setFixedSize(QSize(48, 48));
d->itemThumb->setAlignment(Qt::AlignCenter);
h->layout()->addWidget(d->itemThumb);
h->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
}
d->itemLabel = new QLabel(fontMetrics().elidedText(item->label(), Qt::ElideRight, d->maxLabelWidth), h);
h->layout()->addWidget(d->itemLabel);
h->setStretchFactor(d->itemLabel, 100);
h->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
d->progress = new QProgressBar(h);
d->progress->setFormat(i18nc("%p is the percent value, % is the percent sign", "%p%"));
d->progress->setMaximum(100);
d->progress->setValue(item->progress());
h->layout()->addWidget(d->progress);
if (item->canBeCanceled())
{
d->cancelButton = new QPushButton(QIcon::fromTheme(QLatin1String("dialog-cancel")), QString(), h);
d->cancelButton->setToolTip( i18n("Cancel this operation."));
connect(d->cancelButton, SIGNAL(clicked()),
this, SLOT(slotItemCanceled()));
h->layout()->addWidget(d->cancelButton);
}
h = new DHBox(this);
h->setSpacing(5);
h->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
layout()->addWidget(h);
d->itemStatus = new QLabel(h);
d->itemStatus->setTextFormat(Qt::RichText);
d->itemStatus->setText(fontMetrics().elidedText(item->status(), Qt::ElideRight, d->maxLabelWidth));
h->layout()->addWidget(d->itemStatus);
if (first)
{
hideHLine();
}
}
TransactionItem::~TransactionItem()
{
delete d;
}
ProgressItem* TransactionItem::item() const
{
return d->item;
}
void TransactionItem::hideHLine()
{
d->frame->hide();
}
void TransactionItem::setProgress(int progress)
{
d->progress->setValue(progress);
}
void TransactionItem::setItemComplete()
{
d->item = nullptr;
}
void TransactionItem::setLabel(const QString& label)
{
QStringList textList = label.split(QLatin1Char('\n'),
Qt::KeepEmptyParts);
QString text;
for (int i = 0 ; i < textList.size() ; ++i)
{
text += fontMetrics().elidedText(textList.at(i),
Qt::ElideRight,
d->maxLabelWidth);
if ((i + 1) < textList.size())
{
text += QLatin1Char('\n');
}
}
d->itemLabel->setText(text);
}
void TransactionItem::setThumbnail(const QPixmap& thumb)
{
d->itemThumb->setPixmap(thumb);
}
void TransactionItem::setStatus(const QString& status)
{
d->itemStatus->setText(fontMetrics().elidedText(status,
Qt::ElideRight,
d->maxLabelWidth));
}
void TransactionItem::setTotalSteps(int totalSteps)
{
d->progress->setMaximum(totalSteps);
}
void TransactionItem::slotItemCanceled()
{
if (d->item)
{
d->item->cancel();
}
}
void TransactionItem::addSubTransaction(ProgressItem* const item)<--- Shadow argument
{
Q_UNUSED(item);
}
// ---------------------------------------------------------------------------
class Q_DECL_HIDDEN ProgressView::Private
{
public:
Private() = default;
bool wasLastShown = false;
TransactionItemView* scrollView = nullptr;
TransactionItem* previousItem = nullptr;
QMap<const ProgressItem*, TransactionItem*> transactionsToListviewItems;
};
ProgressView::ProgressView(QWidget* const alignWidget, QWidget* const parent, const QString& name)
: OverlayWidget(alignWidget, parent, name),
d (new Private)
{
setFrameStyle(QFrame::Panel | QFrame::Sunken);
setAutoFillBackground(true);
d->scrollView = new TransactionItemView(this, QLatin1String("ProgressScrollView"));
layout()->addWidget( d->scrollView );
// No more close button for now, since there is no more autoshow
/*
QVBox* const rightBox = new QVBox( this );
QToolButton* const pbClose = new QToolButton( rightBox );
pbClose->setAutoRaise(true);
pbClose->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
pbClose->setFixedSize( 16, 16 );
pbClose->setIcon( KIconLoader::global()->loadIconSet( "window-close", KIconLoader::Small, 14 ) );
pbClose->setToolTip( i18n( "Hide detailed progress window" ) );
connect(pbClose, SIGNAL(clicked()), this, SLOT(slotClose()));
QWidget* const spacer = new QWidget( rightBox ); // don't let the close button take up all the height
rightBox->setStretchFactor( spacer, 100 );
*/
/*
* Get the singleton ProgressManager item which will inform us of
* appearing and vanishing items.
*/
ProgressManager* const pm = ProgressManager::instance();
connect(pm, SIGNAL(progressItemAdded(ProgressItem*)),
this, SLOT(slotTransactionAdded(ProgressItem*)));
connect(pm, SIGNAL(progressItemCompleted(ProgressItem*)),
this, SLOT(slotTransactionCompleted(ProgressItem*)));
connect(pm, SIGNAL(progressItemProgress(ProgressItem*,uint)),
this, SLOT(slotTransactionProgress(ProgressItem*,uint)));
connect(pm, SIGNAL(progressItemStatus(ProgressItem*,QString)),
this, SLOT(slotTransactionStatus(ProgressItem*,QString)));
connect(pm, SIGNAL(progressItemLabel(ProgressItem*,QString)),
this, SLOT(slotTransactionLabel(ProgressItem*,QString)));
connect(pm, SIGNAL(progressItemUsesBusyIndicator(ProgressItem*,bool)),
this, SLOT(slotTransactionUsesBusyIndicator(ProgressItem*,bool)));
connect(pm, SIGNAL(progressItemThumbnail(ProgressItem*,QPixmap)),
this, SLOT(slotTransactionThumbnail(ProgressItem*,QPixmap)));
connect(pm, SIGNAL(showProgressView()),
this, SLOT(slotShow()));
connect(d->scrollView, SIGNAL(signalTransactionViewIsEmpty()),
pm, SLOT(slotTransactionViewIsEmpty()));
}
ProgressView::~ProgressView()
{
// NOTE: no need to delete child widgets.
delete d;
}
void ProgressView::closeEvent(QCloseEvent* e)
{
e->accept();
hide();
}
void ProgressView::slotTransactionAdded(ProgressItem* item)
{
TransactionItem* parent = nullptr;
if (item->parent())
{
if (d->transactionsToListviewItems.contains(item->parent()))
{
parent = d->transactionsToListviewItems[item->parent()];
parent->addSubTransaction(item);
}
}
else
{
const bool first = d->transactionsToListviewItems.isEmpty();
TransactionItem* const ti = d->scrollView->addTransactionItem( item, first );
if (ti)
{
d->transactionsToListviewItems.insert( item, ti );
}
if (item->showAtStart())
{
// Force to show progress view for 5 seconds to inform user about new process add in queue.
QTimer::singleShot(1000, this, SLOT(slotShow()));
QTimer::singleShot(6000, this, SLOT(slotClose()));
return;
}
if (first && d->wasLastShown)
{
QTimer::singleShot(1000, this, SLOT(slotShow()));
}
}
}
void ProgressView::slotTransactionCompleted(ProgressItem* item)
{
if (d->transactionsToListviewItems.contains(item))
{
TransactionItem* const ti = d->transactionsToListviewItems[item];
d->transactionsToListviewItems.remove( item );
ti->setItemComplete();
QTimer::singleShot(3000, ti, SLOT(deleteLater()));
// see the slot for comments as to why that works
connect(ti, SIGNAL(destroyed()),
d->scrollView, SLOT(slotLayoutFirstItem()));
}
// This was the last item, hide.
if (d->transactionsToListviewItems.isEmpty())
{
QTimer::singleShot(3000, this, SLOT(slotHide()));
}
}
void ProgressView::slotTransactionCanceled(ProgressItem*)
{
}
void ProgressView::slotTransactionProgress(ProgressItem* item, unsigned int progress)
{
if (d->transactionsToListviewItems.contains(item))
{
TransactionItem* const ti = d->transactionsToListviewItems[item];
ti->setProgress(progress);
}
}
void ProgressView::slotTransactionStatus(ProgressItem* item, const QString& status)
{
if (d->transactionsToListviewItems.contains(item))
{
TransactionItem* const ti = d->transactionsToListviewItems[item];
ti->setStatus(status);
}
}
void ProgressView::slotTransactionLabel(ProgressItem* item, const QString& label )
{
if ( d->transactionsToListviewItems.contains(item))
{
TransactionItem* const ti = d->transactionsToListviewItems[item];
ti->setLabel(label);
}
}
void ProgressView::slotTransactionUsesBusyIndicator(ProgressItem* item, bool value)
{
if (d->transactionsToListviewItems.contains(item))
{
TransactionItem* const ti = d->transactionsToListviewItems[item];
if (value)
{
ti->setTotalSteps(0);
}
else
{
ti->setTotalSteps(100);
}
}
}
void ProgressView::slotTransactionThumbnail(ProgressItem* item, const QPixmap& thumb)
{
if (d->transactionsToListviewItems.contains(item))
{
TransactionItem* const ti = d->transactionsToListviewItems[item];
ti->setThumbnail(thumb);
}
}
void ProgressView::slotShow()
{
this->setVisible(true);
}
void ProgressView::slotHide()
{
// check if a new item showed up since we started the timer. If not, hide
if (d->transactionsToListviewItems.isEmpty())
{
setVisible(false);
}
}
void ProgressView::slotClose()
{
d->wasLastShown = false;
setVisible(false);
}
void ProgressView::setVisible(bool b)
{
OverlayWidget::setVisible(b);
Q_EMIT visibilityChanged(b);
}
void ProgressView::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.
*/
d->wasLastShown = isHidden();
if (!isHidden() || !d->transactionsToListviewItems.isEmpty())
{
setVisible(isHidden());
}
}
} // namespace Digikam
#include "moc_progressview.cpp"
|