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

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2006-2007 Thomas Zander <zander@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #ifndef KOPROGRESSUPDATER_H
0007 #define KOPROGRESSUPDATER_H
0008 
0009 #include "kritawidgetutils_export.h"
0010 
0011 #include <QString>
0012 #include <QObject>
0013 #include <QPointer>
0014 
0015 class KoUpdater;
0016 class KoProgressProxy;
0017 class QTextStream;
0018 class QTime;
0019 
0020 /**
0021  * Allow multiple subtasks to safely update and report progress.
0022  * This class is able to update a progress bar with the total progress
0023  * of a project that may be separated into different subtasks.
0024  * Each subtask will use one KoUpdater which that subtask can then report
0025  * progress to.  Each KoUpdater.setProgress() call will automatically calculate
0026  * the total progress over the whole tasks made and update the progress bar
0027  * with the total progress so far.
0028  *
0029  * This class is created specifically with threading in mind so that subtasks
0030  * can report their progress from their personal subthread and the progress bar
0031  * will be updated correctly and not more often than repaints can occur.
0032  *
0033  * Typical usage can be:
0034  * @code
0035   KoProgressUpdater *pu = new KoProgressUpdater(myProgressBar);
0036   pu->start(100);
0037   // create the subtasks
0038   KoUpdater smooth = pu->startSubtask(5);
0039   KoUpdater scale = pu->startSubtask(5);
0040   KoUpdater cleanup = pu->startSubtask(1);
0041   @endcode
0042  * Doing a smooth.setProgress(50) will move the progress bar to 50% of the share
0043  * of task 'smooth' which is 5 / 11 of the total and thus to 22.
0044  *
0045  * KoProgressUpdater should be created in the main thread;
0046  * KoProgressProxy must be, if it is gui subclass in the QApplication
0047  * main thread. The other objects can be created in whatever thread
0048  * one wants.
0049  *
0050  * Also to prevent jumps in the progress-calculation and -display it is recommend
0051  * to first create all the subtasks and then start to use setProgress on them.
0052  */
0053 class KRITAWIDGETUTILS_EXPORT KoProgressUpdater : public QObject
0054 {
0055     Q_OBJECT
0056 public:
0057 
0058     enum Mode {
0059         Threaded,
0060         Unthreaded
0061     };
0062 
0063     /**
0064      * Constructor.
0065      * @param progressBar the progress bar to update.
0066      */
0067     explicit KoProgressUpdater(KoProgressProxy *progressProxy, Mode mode = Threaded);
0068 
0069     /**
0070      * @brief a special constructor for connecting the progress updater to a self-destructable
0071      * KoUpdater object.
0072      *
0073      * HACK ALERT: KoUpdater inherits KoProgressProxy, so be careful when constructing
0074      *             the updater and check which override is actually used.
0075      */
0076     explicit KoProgressUpdater(QPointer<KoUpdater> updater);
0077 
0078     /// destructor
0079     ~KoProgressUpdater() override;
0080 
0081     /**
0082      * Start a new task.
0083      *
0084      * This will invalidate any previously created subtasks and set
0085      * the range of the progressBar as well as the text in the
0086      * progressbar.
0087      *
0088      * @param range the total range of progress bar makes.
0089      * @param text The text to show in the progressBar.
0090      * @see KoProgressProxy::setRange()
0091      * @see KoProgressProxy::setFormat()
0092      */
0093     void start(int range = 100, const QString &text = "");
0094 
0095     /**
0096      * After calling start() you can create any number of Updaters,
0097      * one for each subtask. @param weight use a weight to specify the
0098      * weight this subtask has compared to the rest of the subtasks.
0099      *
0100      * KoProgressUpdater will delete the KoUpdater instances when a
0101      * start() is called or when it is deleted. The KoUpdater pointers
0102      * are packed in a QPointer so you can check whether they have
0103      * been deleted before dereferencing.
0104      */
0105     QPointer<KoUpdater> startSubtask(int weight=1,
0106                                      const QString &name = QString(), bool isPersistent = false);
0107 
0108     void removePersistentSubtask(QPointer<KoUpdater> updater);
0109 
0110     /**
0111      * Cancelling the action will make each subtask be marked as 'interrupted' and
0112      * set the total progress to 100%.
0113      */
0114     void cancel();
0115 
0116     /**
0117      * @return true when the processing is interrupted
0118      */
0119     bool interrupted() const;
0120 
0121     void setUpdateInterval(int ms);
0122     int updateInterval() const;
0123 
0124     void setAutoNestNames(bool value);
0125     bool autoNestNames() const;
0126 
0127 private Q_SLOTS:
0128 
0129     void update();
0130     void updateUi();
0131 
0132 Q_SIGNALS:
0133     void triggerUpdateAsynchronously();
0134 
0135 private:
0136 
0137     class Private;
0138     Private *const d;
0139 
0140 };
0141 
0142 
0143 
0144 
0145 #endif
0146