1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2012-01-26
7  * Description : a progress bar with information dispatched to progress manager
8  *
9  * Copyright (C) 2012-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "dprogresswdg.h"
25 
26 // Qt includes
27 
28 #include <QString>
29 #include <QIcon>
30 
31 // Local includes
32 
33 #include "progressmanager.h"
34 
35 namespace Digikam
36 {
37 
38 class Q_DECL_HIDDEN DProgressWdg::Private
39 {
40 public:
41 
Private()42     explicit Private()
43     {
44     }
45 
findProgressItem() const46     ProgressItem* findProgressItem() const
47     {
48         return ProgressManager::instance()->findItembyId(progressId);
49     }
50 
51 public:
52 
53     QString progressId;
54 };
55 
DProgressWdg(QWidget * const parent)56 DProgressWdg::DProgressWdg(QWidget* const parent)
57     : QProgressBar(parent),
58       d(new Private)
59 {
60     connect(this, &DProgressWdg::valueChanged,
61             this, &DProgressWdg::slotValueChanged);
62 }
63 
~DProgressWdg()64 DProgressWdg::~DProgressWdg()
65 {
66     delete d;
67 }
68 
slotValueChanged(int)69 void DProgressWdg::slotValueChanged(int)
70 {
71     float percents           = ((float)value() / (float)maximum()) * 100.0;
72     ProgressItem* const item = d->findProgressItem();
73 
74     if (item)
75     {
76         item->setProgress(percents);
77     }
78 }
79 
progressCompleted()80 void DProgressWdg::progressCompleted()
81 {
82     ProgressItem* const item = d->findProgressItem();
83 
84     if (item)
85     {
86         item->setComplete();
87     }
88 }
89 
progressThumbnailChanged(const QPixmap & thumb)90 void DProgressWdg::progressThumbnailChanged(const QPixmap& thumb)
91 {
92     ProgressItem* const item = d->findProgressItem();
93 
94     if (item)
95     {
96         item->setThumbnail(thumb);
97     }
98 }
99 
progressStatusChanged(const QString & status)100 void DProgressWdg::progressStatusChanged(const QString& status)
101 {
102     ProgressItem* const item = d->findProgressItem();
103 
104     if (item)
105     {
106         item->setStatus(status);
107     }
108 }
109 
progressScheduled(const QString & title,bool canBeCanceled,bool hasThumb)110 void DProgressWdg::progressScheduled(const QString& title, bool canBeCanceled, bool hasThumb)
111 {
112     ProgressItem* const item = ProgressManager::createProgressItem(title,
113                                                                    QString(),
114                                                                    canBeCanceled,
115                                                                    hasThumb);
116 
117     if (canBeCanceled)
118     {
119         connect(item, SIGNAL(progressItemCanceledById(QString)),
120                 this, SLOT(slotProgressCanceled(QString)));
121     }
122 
123     d->progressId = item->id();
124 }
125 
slotProgressCanceled(const QString & id)126 void DProgressWdg::slotProgressCanceled(const QString& id)
127 {
128     if (d->progressId == id)
129     {
130         emit signalProgressCanceled();
131     }
132 }
133 
134 } // namespace Digikam
135