File indexing completed on 2024-05-19 04:29:55

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 #ifndef KO_UPDATER_H
0009 #define KO_UPDATER_H
0010 
0011 #include "KoProgressProxy.h"
0012 #include <QObject>
0013 #include <QPointer>
0014 #include <QAtomicInt>
0015 
0016 class KoProgressUpdater;
0017 class KoUpdaterPrivate;
0018 
0019 
0020 /**
0021  * An KoUpdater is a helper for keeping the progress of each subtask up to speed.
0022  * This class is not thread safe, and it should only be used from one thread.
0023  * The thread it is used in can be different from any other subtask or the
0024  * KoProgressUpdater, though.
0025  *
0026  * It is possible to create a KoProgressUpdater on a KoUpdater for when you
0027  * need to recursively split up progress reporting. (For instance, when your
0028  * progress reporting routine can be called by other progress reporting
0029  * routines.)
0030  *
0031  * KoUpdater implements KoProgressProxy because it is possible to recursively
0032  * create another KoProgressUpdater with an updater as progress proxy.
0033  *
0034  * @see KoProgressUpdater::startSubtask()
0035  */
0036 class KRITAWIDGETUTILS_EXPORT KoUpdater : public QObject, public KoProgressProxy {
0037 
0038     Q_OBJECT
0039 
0040 public:
0041     virtual ~KoUpdater();
0042 
0043     /**
0044      * Call this when this subtask wants to abort all the actions.
0045      */
0046     void cancel();
0047 
0048 public Q_SLOTS:
0049     /**
0050      * Update your progress. Progress is always from 0 to 100.
0051      * The global progress shown to the user is determined by the total
0052      * amount of subtasks there are. This means that each subtasks can just
0053      * report its own private progress in the range from 0 to 100.
0054      */
0055     void setProgress(int percent);
0056 
0057 public:
0058     /**
0059      * return true when this task should stop processing immediately.
0060      * When the task has been cancelled all the subtasks will get interrupted
0061      * and should stop working.  It is therefore important to have repeated calls to
0062      * this method at regular (time) intervals and as soon as the method returns true
0063      * cancel the subtask.
0064      * @return true when this task should stop processing immediately.
0065      */
0066     bool interrupted() const;
0067 
0068     /**
0069      * return the progress this subtask has made.
0070      */
0071     int progress() const;
0072 
0073 public: // KoProgressProxy implementation
0074 
0075     int maximum() const override;
0076     void setValue( int value ) override;
0077     void setRange( int minimum, int maximum ) override;
0078     void setFormat( const QString & format ) override;
0079     void setAutoNestedName(const QString &name) override;
0080 
0081 Q_SIGNALS:
0082 
0083     /// emitted whenever the subtask has called cancel on us
0084     void sigCancel();
0085 
0086     /// emitted whenever the subtask has called setProgress on us
0087     void sigProgress( int percent );
0088 
0089     void sigNestedNameChanged(const QString &value);
0090     void sigHasValidRangeChanged(bool value);
0091 
0092 protected:
0093 
0094     friend class KoUpdaterPrivate;
0095     KoUpdater(KoUpdaterPrivate *_d);
0096 
0097 public:
0098 
0099     QPointer<KoUpdaterPrivate> d;
0100     int range;
0101     int min;
0102     int max;
0103 
0104 private Q_SLOTS:
0105 
0106     void setInterrupted(bool value);
0107 
0108 private:
0109 
0110     QAtomicInt m_interrupted;
0111     int  m_progressPercent;
0112 };
0113 
0114 /// A holder for an updater that does nothing
0115 class KRITAWIDGETUTILS_EXPORT KoDummyUpdaterHolder
0116 {
0117 public:
0118     KoDummyUpdaterHolder();
0119     ~KoDummyUpdaterHolder();
0120 
0121     KoUpdater *updater();
0122 
0123 private:
0124     Q_DISABLE_COPY_MOVE(KoDummyUpdaterHolder)
0125     KoUpdaterPrivate *d;
0126 };
0127 
0128 #endif
0129