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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657 | /* ============================================================
*
* 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 "progressmanager.h"
// Qt includes
#include <QApplication>
#include <QAtomicInt>
#include <QMutex>
#include <QMutexLocker>
#include <QThread>
#include <QEventLoop>
#include <QMessageBox>
#include <QIcon>
#include <QStyle>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "digikam_debug.h"
namespace Digikam
{
class Q_DECL_HIDDEN ProgressItem::Private
{
public:
typedef QMap<ProgressItem*, bool> ProgressItemMap;
public:
Private() = default;
public:
volatile bool waitingForKids = false;
volatile bool canceled = false;
bool usesBusyIndicator = false;
bool canBeCanceled = false;
bool hasThumb = false;
bool showAtStart = false; // Force to show progress item when it's add in progress manager
QAtomicInt progress = 0;
QAtomicInt total = 0;
QAtomicInt completed = 0;
QString id;
QString label;
QString status;
ProgressItem* parent = nullptr;
ProgressItemMap children;
};
ProgressItem::ProgressItem(ProgressItem* const parent, const QString& id,
const QString& label, const QString& status,
bool canBeCanceled, bool hasThumb)
: d(new Private)
{
d->canBeCanceled = canBeCanceled;
d->hasThumb = hasThumb;
d->id = id;
d->label = label;
d->status = status;
d->parent = parent;
}
ProgressItem::~ProgressItem()
{
delete d;
}
void ProgressItem::setShowAtStart(bool showAtStart)<--- Shadow argument
{
d->showAtStart = showAtStart;
}
bool ProgressItem::showAtStart() const
{
return d->showAtStart;
}
void ProgressItem::setComplete()
{
if (d->children.isEmpty())
{
if (!d->canceled)
{
setProgress(100);
}
Q_EMIT progressItemCompleted(this);
}
else
{
d->waitingForKids = true;
}
}
void ProgressItem::addChild(ProgressItem* const kiddo)
{
d->children.insert(kiddo, true);
}
void ProgressItem::removeChild(ProgressItem* const kiddo)
{
d->children.remove(kiddo);
// in case we were waiting for the last kid to go away, now is the time
if ((d->children.count() == 0) && d->waitingForKids)
{
Q_EMIT progressItemCompleted(this);
}
}
void ProgressItem::cancel()
{
if (d->canceled || !d->canBeCanceled)
{
return;
}
d->canceled = true;
// Cancel all children.
QList<ProgressItem*> kids = d->children.keys();
QList<ProgressItem*>::Iterator it(kids.begin());
QList<ProgressItem*>::Iterator end(kids.end());
for ( ; it != end ; ++it)
{
ProgressItem* const kid = *it;
if (kid->canBeCanceled())
{
kid->cancel();
}
}
setStatus(i18n("Aborting..."));
Q_EMIT progressItemCanceled(this);
Q_EMIT progressItemCanceledById(this->id());
}
void ProgressItem::setLabel(const QString& v)
{
d->label = v;
Q_EMIT progressItemLabel(this, d->label);
}
void ProgressItem::setStatus(const QString& v)
{
d->status = v;
Q_EMIT progressItemStatus(this, d->status);
}
void ProgressItem::setUsesBusyIndicator(bool useBusyIndicator)
{
d->usesBusyIndicator = useBusyIndicator;
Q_EMIT progressItemUsesBusyIndicator(this, useBusyIndicator);
}
void ProgressItem::setThumbnail(const QIcon& icon)
{
if (!hasThumbnail())
{
return;
}
int iconSize = 48;
if (icon.isNull())
{
Q_EMIT progressItemThumbnail(this, QIcon::fromTheme(QLatin1String("image-missing")).pixmap(iconSize));
return;
}
QPixmap pix = icon.pixmap(iconSize);
Q_EMIT progressItemThumbnail(this, pix);
}
void ProgressItem::reset()
{
setProgress(0);
setStatus(QString());
d->completed = 0;
}
void ProgressItem::setProgress(unsigned int v)
{
d->progress.fetchAndStoreOrdered(v);
Q_EMIT progressItemProgress(this, v);
}
void ProgressItem::updateProgress()
{
int total = d->total;
setProgress(total? d->completed * 100 / total : 0);
}
bool ProgressItem::advance(unsigned int v)
{
bool complete = incCompletedItems(v);
updateProgress();
return complete;
}
void ProgressItem::setTotalItems(unsigned int v)
{
d->total.fetchAndStoreOrdered(v);
}
void ProgressItem::incTotalItems(unsigned int v)
{
d->total.fetchAndAddOrdered(v);
}
unsigned int ProgressItem::totalItems() const
{
return d->total;
}
bool ProgressItem::setCompletedItems(unsigned int v)
{
d->completed.fetchAndStoreOrdered(v);
return (v == (unsigned int)d->total);
}
unsigned int ProgressItem::completedItems() const
{
return d->completed;
}
bool ProgressItem::incCompletedItems(unsigned int v)
{
unsigned int previous = d->completed.fetchAndAddOrdered(v);
return ((previous+v) == (unsigned int) d->total);
}
bool ProgressItem::totalCompleted() const
{
return (d->completed == d->total);
}
bool ProgressItem::canceled() const
{
return d->canceled;
}
const QString& ProgressItem::id() const
{
return d->id;
}
ProgressItem* ProgressItem::parent() const
{
return d->parent;
}
const QString& ProgressItem::label() const
{
return d->label;
}
const QString& ProgressItem::status() const
{
return d->status;
}
bool ProgressItem::canBeCanceled() const
{
return d->canBeCanceled;
}
bool ProgressItem::usesBusyIndicator() const
{
return d->usesBusyIndicator;
}
bool ProgressItem::hasThumbnail() const
{
return d->hasThumb;
}
unsigned int ProgressItem::progress() const
{
return d->progress;
}
// --------------------------------------------------------------------------
class Q_DECL_HIDDEN ProgressManagerCreator
{
public:
ProgressManager object;
};
class Q_DECL_HIDDEN ProgressManager::Private
{
public:
Private() = default;
void addItem(ProgressItem* const t, ProgressItem* const parent);
void removeItem(ProgressItem* const t);
public:
QMutex mutex;
QHash<QString, ProgressItem*> transactions;
QAtomicInt uID = 1000;
QEventLoop* waitingLoop = nullptr;
};
Q_GLOBAL_STATIC(ProgressManagerCreator, creator)
void ProgressManager::Private::addItem(ProgressItem* const t, ProgressItem* const parent)
{
if (!t)
{
return;
}
QMutexLocker lock(&mutex);
transactions.insert(t->id(), t);
if (parent)
{
parent->addChild(t);
}
}
void ProgressManager::Private::removeItem(ProgressItem* const t)
{
if (!t)
{
return;
}
QMutexLocker lock(&mutex);
transactions.remove(t->id());
if (t->parent())
{
t->parent()->removeChild(t);
}
}
// --------------------------------------------------------------------------
ProgressManager::ProgressManager()
: QObject(),
d (new Private)
{
if (thread() != QApplication::instance()->thread())
{
qCWarning(DIGIKAM_GENERAL_LOG) << "Attention: ProgressManager was created from a thread. Create it in the main thread!";
moveToThread(QApplication::instance()->thread());
}
d->waitingLoop = new QEventLoop(this);
connect(this, SIGNAL(completeTransactionDeferred(ProgressItem*)),
this, SLOT(slotTransactionCompletedDeferred(ProgressItem*)));
}
ProgressManager::~ProgressManager()
{
delete d;
}
ProgressManager* ProgressManager::instance()
{
return (creator.isDestroyed() ? nullptr : &creator->object);
}
bool ProgressManager::isEmpty() const
{
return d->transactions.isEmpty();
}
ProgressItem* ProgressManager::findItembyId(const QString& id) const
{
if (id.isEmpty())
{
return nullptr;
}
QMutexLocker lock(&d->mutex);
return d->transactions.value(id);
}
QString ProgressManager::getUniqueID()
{
return QString::number(d->uID.fetchAndAddOrdered(1));
}
ProgressItem* ProgressManager::createProgressItemImpl(ProgressItem* const parent,
const QString& id,
const QString& label,
const QString& status,
bool cancellable,
bool hasThumb)
{
ProgressItem* t = findItembyId(id);
if (!t)
{
t = new ProgressItem(parent, id, label, status, cancellable, hasThumb);
addProgressItemImpl(t, parent);
}
return t;
}
ProgressItem* ProgressManager::createProgressItemImpl(const QString& parent,
const QString& id,
const QString& label,
const QString& status,
bool canBeCanceled,
bool hasThumb)
{
ProgressItem* const p = findItembyId(parent);
return createProgressItemImpl(p, id, label, status, canBeCanceled, hasThumb);
}
bool ProgressManager::addProgressItem(ProgressItem* const t, ProgressItem* const parent)
{
if (!instance()->findItembyId(t->id()))
{
instance()->addProgressItemImpl(t, parent);
return true;
}
else
{
qCWarning(DIGIKAM_GENERAL_LOG) << "A tool identified as " << t->id() << " is already running.";
t->setComplete();
return false;
}
}
void ProgressManager::addProgressItemImpl(ProgressItem* const t, ProgressItem* const parent)
{
if (t->thread() != thread())
{
if (t->thread() != QThread::currentThread())
{
// we cannot moveToThread this item living in a third thread. Refusing to add.
qCDebug(DIGIKAM_GENERAL_LOG) << "Refusing to add in thread 1 a ProgressItem created in thread 2 to ProgressManager, living in thread 3";
return;
}
// Move to ProgressManager's thread
t->moveToThread(thread());
}
connect(t, SIGNAL(progressItemCompleted(ProgressItem*)),
this, SLOT(slotTransactionCompleted(ProgressItem*)), Qt::DirectConnection);
connect(t, SIGNAL(progressItemProgress(ProgressItem*,uint)),
this, SIGNAL(progressItemProgress(ProgressItem*,uint)));
connect(t, SIGNAL(progressItemAdded(ProgressItem*)),
this, SIGNAL(progressItemAdded(ProgressItem*)));
connect(t, SIGNAL(progressItemCanceled(ProgressItem*)),
this, SIGNAL(progressItemCanceled(ProgressItem*)));
connect(t, SIGNAL(progressItemStatus(ProgressItem*,QString)),
this, SIGNAL(progressItemStatus(ProgressItem*,QString)));
connect(t, SIGNAL(progressItemLabel(ProgressItem*,QString)),
this, SIGNAL(progressItemLabel(ProgressItem*,QString)));
connect(t, SIGNAL(progressItemUsesBusyIndicator(ProgressItem*,bool)),
this, SIGNAL(progressItemUsesBusyIndicator(ProgressItem*,bool)));
connect(t, SIGNAL(progressItemThumbnail(ProgressItem*,QPixmap)),
this, SIGNAL(progressItemThumbnail(ProgressItem*,QPixmap)));
d->addItem(t, parent);
Q_EMIT progressItemAdded(t);
}
void ProgressManager::emitShowProgressViewImpl()
{
Q_EMIT showProgressView();
}
void ProgressManager::slotTransactionCompleted(ProgressItem* item)
{
if (!item)
{
return;
}
d->removeItem(item);
// move to UI thread
Q_EMIT completeTransactionDeferred(item);
}
void ProgressManager::slotTransactionCompletedDeferred(ProgressItem* item)
{
Q_EMIT progressItemCompleted(item);
item->deleteLater();
}
void ProgressManager::slotStandardCancelHandler(ProgressItem* item)
{
item->setComplete();
}
ProgressItem* ProgressManager::singleItem() const
{
QHash<QString, ProgressItem*> hash;
{
QMutexLocker lock(&d->mutex);
hash = d->transactions;
}
ProgressItem* item = nullptr;
QHash<QString, ProgressItem*>::const_iterator it = hash.constBegin();
while (it != hash.constEnd())
{
// No single item for progress possible, as one of them is a busy indicator one.
if ((*it)->usesBusyIndicator())
{
return nullptr;
}
// If it's a top level one, only those count.
if (!(*it)->parent())
{
if (item)
{
return nullptr; // we found more than one
}
else
{
item = (*it);
}
}
++it;
}
return item;
}
void ProgressManager::slotAbortAll()
{
QHash<QString, ProgressItem*> hash;
{
QMutexLocker lock(&d->mutex);
if (d->transactions.isEmpty())
{
return;
}
hash = d->transactions;
}
QHashIterator<QString, ProgressItem*> it(hash);
while (it.hasNext())
{
it.next();
it.value()->cancel();
}
d->waitingLoop->exec(QEventLoop::ExcludeUserInputEvents);
}
void ProgressManager::slotTransactionViewIsEmpty()
{
d->waitingLoop->quit();
}
void ProgressManager::emitShowProgressView()
{
instance()->emitShowProgressViewImpl();
}
ProgressItem* ProgressManager::createProgressItem(const QString& label, const QString& status, bool canBeCanceled, bool hasThumb)
{
return instance()->createProgressItemImpl(nullptr, instance()->getUniqueID(), label, status, canBeCanceled, hasThumb);
}
ProgressItem* ProgressManager::createProgressItem(ProgressItem* parent, const QString& id, const QString& label,
const QString& status, bool canBeCanceled, bool hasThumb)
{
return instance()->createProgressItemImpl(parent, id, label, status, canBeCanceled, hasThumb);
}
ProgressItem* ProgressManager::createProgressItem(const QString& parent, const QString& id, const QString& label,
const QString& status, bool canBeCanceled, bool hasThumb)
{
return instance()->createProgressItemImpl(parent, id, label, status, canBeCanceled, hasThumb);
}
ProgressItem* ProgressManager::createProgressItem(const QString& id, const QString& label, const QString& status,
bool canBeCanceled, bool hasThumb)
{
return instance()->createProgressItemImpl(nullptr, id, label, status, canBeCanceled, hasThumb);
}
} // namespace Digikam
#include "moc_progressmanager.cpp"
|