File indexing completed on 2025-01-05 04:55:50

0001 /*
0002     progressbar.cpp
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <config-libkleo.h>
0011 
0012 #include "progressbar.h"
0013 
0014 #include <kleo_ui_debug.h>
0015 
0016 #include <QTimer>
0017 
0018 static const int busyTimerTickInterval = 100;
0019 static const int busyTimerTickIncrement = 5;
0020 
0021 Kleo::ProgressBar::ProgressBar(QWidget *parent)
0022     : QProgressBar(parent)
0023     , mRealProgress(-1)
0024 {
0025     mBusyTimer = new QTimer(this);
0026     connect(mBusyTimer, &QTimer::timeout, this, &ProgressBar::slotBusyTimerTick);
0027     fixup(true);
0028 }
0029 
0030 void Kleo::ProgressBar::slotProgress(const QString &, int cur, int tot)
0031 {
0032     setRange(cur, tot);
0033 }
0034 
0035 void Kleo::ProgressBar::slotProgress(const QString &, int, int cur, int tot)
0036 {
0037     setRange(cur, tot);
0038 }
0039 
0040 void Kleo::ProgressBar::setMaximum(int total)
0041 {
0042     qCDebug(KLEO_UI_LOG) << "Kleo::ProgressBar::setMaximum(" << total << " )";
0043     if (total == maximum()) {
0044         return;
0045     }
0046     QProgressBar::setMaximum(0);
0047     fixup(false);
0048 }
0049 
0050 void Kleo::ProgressBar::setValue(int p)
0051 {
0052     qCDebug(KLEO_UI_LOG) << "Kleo::ProgressBar::setValue(" << p << " )";
0053     mRealProgress = p;
0054     fixup(true);
0055 }
0056 
0057 void Kleo::ProgressBar::reset()
0058 {
0059     mRealProgress = -1;
0060     fixup(true);
0061 }
0062 
0063 void Kleo::ProgressBar::slotBusyTimerTick()
0064 {
0065     fixup(false);
0066     if (mBusyTimer->isActive()) {
0067         QProgressBar::setValue(QProgressBar::value() + busyTimerTickIncrement);
0068     }
0069 }
0070 
0071 void Kleo::ProgressBar::fixup(bool newValue)
0072 {
0073     const int cur = QProgressBar::value();
0074     const int tot = QProgressBar::maximum();
0075 
0076     qCDebug(KLEO_UI_LOG) << "Kleo::ProgressBar::startStopBusyTimer() cur =" << cur << "; tot =" << tot << "; real =" << mRealProgress;
0077 
0078     if ((newValue && mRealProgress < 0) || (!newValue && cur < 0)) {
0079         qCDebug(KLEO_UI_LOG) << "(new value) switch to reset";
0080         mBusyTimer->stop();
0081         if (newValue) {
0082             QProgressBar::reset();
0083         }
0084         mRealProgress = -1;
0085     } else if (tot == 0) {
0086         qCDebug(KLEO_UI_LOG) << "(new value) switch or stay in busy";
0087         if (!mBusyTimer->isActive()) {
0088             mBusyTimer->start(busyTimerTickInterval);
0089             if (newValue) {
0090                 QProgressBar::setValue(mRealProgress);
0091             }
0092         }
0093     } else {
0094         qCDebug(KLEO_UI_LOG) << "(new value) normal progress";
0095         mBusyTimer->stop();
0096         if (QProgressBar::value() != mRealProgress) {
0097             QProgressBar::setValue(mRealProgress);
0098         }
0099     }
0100 }
0101 
0102 #include "moc_progressbar.cpp"