File indexing completed on 2024-05-12 16:02:31

0001 /* This file is part of the KDE project
0002  *
0003  * SPDX-FileCopyrightText: 2006-2007 Thomas Zander <zander@kde.org>
0004  * SPDX-FileCopyrightText: 2009 Boudewijn Rempt <boud@valdyas.org>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #include "KoUpdater.h"
0010 
0011 #include "KoUpdaterPrivate_p.h"
0012 
0013 KoUpdater::KoUpdater(KoUpdaterPrivate *_d)
0014     : m_progressPercent(0)
0015 {
0016     d = _d;
0017     Q_ASSERT(!d.isNull());
0018 
0019     connect(this, SIGNAL(sigCancel()), d, SLOT(cancel()));
0020     connect(this, SIGNAL(sigProgress(int)), d, SLOT(setProgress(int)));
0021     connect(this, SIGNAL(sigNestedNameChanged(QString)), d, SLOT(setAutoNestedName(QString)));
0022     connect(this, SIGNAL(sigHasValidRangeChanged(bool)), d, SLOT(setHasValidRange(bool)));
0023     connect(d, SIGNAL(sigInterrupted(bool)), this, SLOT(setInterrupted(bool)));
0024 
0025 
0026     setRange(0, 100);
0027     m_interrupted = false;
0028 }
0029 
0030 KoUpdater::~KoUpdater()
0031 {
0032 }
0033 
0034 void KoUpdater::cancel()
0035 {
0036     emit sigCancel();
0037 }
0038 
0039 void KoUpdater::setProgress(int percent)
0040 {
0041     const bool percentChanged = m_progressPercent != percent;
0042 
0043     if (percentChanged || percent == 0 || percent == 100) {
0044         m_progressPercent = percent;
0045         emit sigProgress( percent );
0046     }
0047 }
0048 
0049 int KoUpdater::progress() const
0050 {
0051 
0052     return m_progressPercent;
0053 }
0054 
0055 bool KoUpdater::interrupted() const
0056 {
0057     return m_interrupted;
0058 }
0059 
0060 int KoUpdater::maximum() const
0061 {
0062     return max;
0063 }
0064 
0065 void KoUpdater::setValue( int value )
0066 {
0067     value = qBound(min, value, max);
0068 
0069     // Go from range to percent
0070     const int range = max - min;
0071 
0072     if (range == 0) {
0073         m_progressPercent = max;
0074         emit sigProgress(max);
0075     } else {
0076         setProgress((100 * (value - min)) / range);
0077     }
0078 }
0079 
0080 void KoUpdater::setRange( int minimum, int maximum )
0081 {
0082     min = minimum;
0083     max = maximum;
0084     range = max - min;
0085     emit sigHasValidRangeChanged(range != 0);
0086 }
0087 
0088 void KoUpdater::setFormat( const QString & format )
0089 {
0090     emit sigNestedNameChanged(format);
0091 }
0092 
0093 void KoUpdater::setAutoNestedName(const QString &name)
0094 {
0095     emit sigNestedNameChanged(name);
0096 }
0097 
0098 void KoUpdater::setInterrupted(bool value)
0099 {
0100     m_interrupted = value;
0101 }
0102 
0103 KoDummyUpdater::KoDummyUpdater()
0104     : KoUpdater(new KoUpdaterPrivate(0, 0, "dummy"))
0105 {
0106 }