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
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2012-01-13
 * Description : progress manager
 *
 * SPDX-FileCopyrightText: 2007-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2004      by Till Adam <adam at kde dot org>
 * SPDX-FileCopyrightText: 2004      by David Faure <faure at kde dot org>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "statusbarprogresswidget.h"

// Qt includes

#include <QEvent>
#include <QHBoxLayout>
#include <QLabel>
#include <QMouseEvent>
#include <QProgressBar>
#include <QPushButton>
#include <QStackedWidget>
#include <QTimer>
#include <QApplication>
#include <QStyle>
#include <QIcon>

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "digikam_debug.h"
#include "progressview.h"
#include "progressmanager.h"

namespace Digikam
{

class Q_DECL_HIDDEN StatusbarProgressWidget::Private
{
public:

    enum Mode
    {
        None,
        Progress
    };

public:

    Private() = default;

    uint            mode            = None;
    bool            bShowButton     = true;

    QProgressBar*   pProgressBar    = nullptr;
    QLabel*         pLabel          = nullptr;
    QPushButton*    pButton         = nullptr;

    QBoxLayout*     box             = nullptr;
    QStackedWidget* stack           = nullptr;
    ProgressItem*   currentItem     = nullptr;
    ProgressView*   progressView    = nullptr;
    QTimer*         delayTimer      = nullptr;
    QTimer*         busyTimer       = nullptr;
    QTimer*         cleanTimer      = nullptr;
};

StatusbarProgressWidget::StatusbarProgressWidget(ProgressView* const progressView, QWidget* const parent, bool button)
    : QFrame(parent),
      d     (new Private)
{
    d->progressView = progressView;
    d->bShowButton  = button;

    int w           = fontMetrics().horizontalAdvance(QLatin1String(" 999.9 kB/s 00:00:01 ")) + 8;

    d->box          = new QHBoxLayout(this);
    d->box->setContentsMargins(QMargins());
    d->box->setSpacing(0);
    d->stack        = new QStackedWidget(this);

    d->pButton      = new QPushButton(this);
    d->pButton->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
    d->pButton->setIcon(QIcon::fromTheme(QLatin1String("go-up")));
    QSize iconSize  = d->pButton->iconSize();

    d->pProgressBar = new QProgressBar(this);
    d->pProgressBar->setFormat(i18nc("%p is the percent value, % is the percent sign", "%p%"));
    d->pProgressBar->installEventFilter(this);
    d->pProgressBar->setMinimumWidth(w);
    d->pProgressBar->setAttribute(Qt::WA_LayoutUsesWidgetRect, true);

    // Determine maximumHeight from the progressbar's height and scale the icon.
    // This operation is style specific and cannot infer the style in use
    // from Q_OS_??? because users can have started us using the -style option
    // (or even be using an unexpected QPA).
    // In most cases, maximumHeight should be set to fontMetrics().height() + 2
    // (Breeze, Oxygen, Fusion, Windows, QtCurve etc.); this corresponds to the actual
    // progressbar height plus a 1 pixel margin above and below.

    int maximumHeight         = d->pProgressBar->fontMetrics().height() + 2;
    const bool macWidgetStyle = QApplication::style()->objectName() == QLatin1String("macintosh");

    if (macWidgetStyle)
    {
        // QProgressBar height is fixed with the macintosh native widget style
        // and alignment with d->pButton is tricky. Sizing the icon to maximumHeight
        // gives a button that is slightly too high and not perfectly
        // aligned. Annoyingly that doesn't improve by calling setMaximumHeight()
        // which even causes the button to change shape. So we use a "flat" button,
        // an invisible outline which is more in line with platform practices anyway.

        maximumHeight = d->pProgressBar->sizeHint().height();
        iconSize.scale(maximumHeight, maximumHeight, Qt::KeepAspectRatio);
        d->pButton->setFlat(true);
        d->pButton->setMaximumWidth(d->pButton->iconSize().width() + 4);
    }
    else
    {
        // The icon is scaled to maximumHeight but with 1 pixel margins on each side
        // because it will be in a visible button.

        iconSize.scale(maximumHeight - 2, maximumHeight - 2, Qt::KeepAspectRatio);

        // additional adjustments:

        d->pButton->setAttribute(Qt::WA_LayoutUsesWidgetRect, true);
        d->stack->setMaximumHeight(maximumHeight);
    }

    d->pButton->setIconSize(iconSize);
    d->box->addWidget(d->pButton);

    d->pButton->setToolTip(i18n("Open detailed progress dialog"));

    d->box->addWidget(d->stack);

    d->stack->insertWidget(1, d->pProgressBar);

    d->pLabel       = new QLabel(i18n("No active process"), this);
    d->pLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    d->pLabel->installEventFilter(this);
    d->pLabel->setMinimumWidth(w);
    d->pLabel->setMaximumHeight(maximumHeight);
    d->stack->insertWidget(2, d->pLabel);
    d->pButton->setMaximumHeight(maximumHeight);
    setMinimumWidth(minimumSizeHint().width());

    setMode();

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

    connect(d->pButton, SIGNAL(clicked()),
            progressView, SLOT(slotToggleVisibility()));

    connect(ProgressManager::instance(), SIGNAL(progressItemAdded(ProgressItem*)),
            this, SLOT(slotProgressItemAdded(ProgressItem*)));

    connect(ProgressManager::instance(), SIGNAL(progressItemCompleted(ProgressItem*)),
            this, SLOT(slotProgressItemCompleted(ProgressItem*)));

    connect(ProgressManager::instance(), SIGNAL(progressItemUsesBusyIndicator(ProgressItem*,bool)),
            this, SLOT(updateBusyMode()));

    connect(progressView, SIGNAL(visibilityChanged(bool)),
            this, SLOT(slotProgressViewVisible(bool)));

    d->delayTimer = new QTimer(this);
    d->delayTimer->setSingleShot(true);

    connect(d->delayTimer, SIGNAL(timeout()),
            this, SLOT(slotShowItemDelayed()));

    d->cleanTimer = new QTimer(this);
    d->cleanTimer->setSingleShot(true);

    connect(d->cleanTimer, SIGNAL(timeout()),
            this, SLOT(slotClean()));
}

StatusbarProgressWidget::~StatusbarProgressWidget()
{
    delete d;
}

// There are three cases: no progressitem, one progressitem (connect to it directly),
// or many progressitems (display busy indicator). Let's call them 0,1,N.
// In slot..Added we can only end up in 1 or N.
// In slot..Removed we can end up in 0, 1, or we can stay in N if we were already.

void StatusbarProgressWidget::updateBusyMode()
{
    connectSingleItem(); // if going to 1 item

    if (d->currentItem)
    {
        // Exactly one item

        delete d->busyTimer;
        d->busyTimer = nullptr;
        d->delayTimer->start(1000);
    }
    else
    {
        // N items

        if (!d->busyTimer)
        {
            d->busyTimer = new QTimer(this);

            connect(d->busyTimer, SIGNAL(timeout()),
                    this, SLOT(slotBusyIndicator()));

            d->delayTimer->start(1000);
        }
    }
}

void StatusbarProgressWidget::slotProgressItemAdded(ProgressItem* item)
{
    if (item->parent())
    {
        return; // we are only interested in top level items
    }

    updateBusyMode();
}

void StatusbarProgressWidget::slotProgressItemCompleted(ProgressItem* item)
{
    if (item && item->parent())
    {
        return; // we are only interested in top level items
    }

    connectSingleItem(); // if going back to 1 item

    if      (ProgressManager::instance()->isEmpty())
    {
        // No item
        // Done. In 5s the progress-widget will close, then we can clean up the statusbar

        d->cleanTimer->start(5000);
    }
    else if (d->currentItem)
    {
        // Exactly one item

        delete d->busyTimer;
        d->busyTimer = nullptr;
        activateSingleItemMode();
    }
}

void StatusbarProgressWidget::connectSingleItem()
{
    if (d->currentItem)
    {
        disconnect(d->currentItem, SIGNAL(progressItemProgress(ProgressItem*,uint)),
                   this, SLOT(slotProgressItemProgress(ProgressItem*,uint)));

        d->currentItem = nullptr;
    }

    d->currentItem = ProgressManager::instance()->singleItem();

    if (d->currentItem)
    {
        connect(d->currentItem, SIGNAL(progressItemProgress(ProgressItem*,uint)),
                this, SLOT(slotProgressItemProgress(ProgressItem*,uint)));
    }
}

void StatusbarProgressWidget::activateSingleItemMode()
{
    d->pProgressBar->setMaximum(100);
    d->pProgressBar->setValue(d->currentItem->progress());
    d->pProgressBar->setTextVisible(true);
}

void StatusbarProgressWidget::slotShowItemDelayed()
{
    bool noItems = ProgressManager::instance()->isEmpty();

    if      (d->currentItem)
    {
        activateSingleItemMode();
    }
    else if (!noItems)
    {
        // N items

        d->pProgressBar->setMaximum(0);
        d->pProgressBar->setTextVisible(false);

        if (d->busyTimer)
        {
            d->busyTimer->start(100);
        }
    }

    if (!noItems && (d->mode == Private::None))
    {
        d->mode = Private::Progress;
        setMode();
    }
}

void StatusbarProgressWidget::slotBusyIndicator()
{
    int p = d->pProgressBar->value();
    d->pProgressBar->setValue(p + 10);
}

void StatusbarProgressWidget::slotProgressItemProgress(ProgressItem* item, unsigned int value)
{
    if (item != d->currentItem) // single item mode; discard others
    {
        return;
    }

    d->pProgressBar->setValue(value);
}

void StatusbarProgressWidget::setMode()
{
    switch (d->mode)
    {
        case Private::None:
        {
            if (d->bShowButton)
            {
                d->pButton->hide();
            }

            d->stack->show();
            d->stack->setCurrentWidget(d->pLabel);

            break;
        }

        case Private::Progress:
        {
            d->stack->show();
            d->stack->setCurrentWidget(d->pProgressBar);

            if (d->bShowButton)
            {
                d->pButton->show();
            }

            break;
        }
    }
}

void StatusbarProgressWidget::slotClean()
{
    // check if a new item showed up since we started the timer. If not, clear

    if (ProgressManager::instance()->isEmpty())
    {
        d->pProgressBar->setValue(0);
/*
        d->pLabel->clear();
*/
        d->mode = Private::None;
        setMode();
    }
}

bool StatusbarProgressWidget::eventFilter(QObject*, QEvent* ev)
{
    if (ev->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent* const e = (QMouseEvent*)ev;

        if ((e->button() == Qt::LeftButton) && (d->mode != Private::None))
        {
            // toggle view on left mouse button
            // Consensus seems to be that we should show/hide the fancy dialog when the user
            // clicks anywhere in the small one.

            d->progressView->slotToggleVisibility();

            return true;
        }
    }

    return false;
}

void StatusbarProgressWidget::slotProgressViewVisible(bool b)
{
    // Update the hide/show button when the detailed one is shown/hidden

    if (b)
    {
        d->pButton->setIcon(QIcon::fromTheme(QLatin1String("go-down")));
        d->pButton->setToolTip(i18n("Hide detailed progress window"));
        setMode();
    }
    else
    {
        d->pButton->setIcon(QIcon::fromTheme(QLatin1String("go-up")));
        d->pButton->setToolTip(i18n("Show detailed progress window"));
    }
}

} // namespace Digikam

#include "moc_statusbarprogresswidget.cpp"