File indexing completed on 2024-05-26 16:16:34

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 #ifndef KOPROGRESSUPDATER_H
0020 #define KOPROGRESSUPDATER_H
0021 
0022 #include "kowidgetutils_export.h"
0023 
0024 #include <QString>
0025 #include <QObject>
0026 #include <QPointer>
0027 
0028 class KoUpdater;
0029 class KoProgressProxy;
0030 class QTextStream;
0031 class QTime;
0032 
0033 /**
0034  * Allow multiple subtasks to safely update and report progress.
0035  * This class is able to update a progress bar with the total progress
0036  * of a project that may be separated into different subtasks.
0037  * Each subtask will use one KoUpdater which that subtask can then report
0038  * progress to.  Each KoUpdater.setProgress() call will automatically calculate
0039  * the total progress over the whole tasks made and update the progress bar
0040  * with the total progress so far.
0041  *
0042  * This class is created specifically with threading in mind so that subtasks
0043  * can report their progress from their personal subthread and the progress bar
0044  * will be updated correctly and not more often than repaints can occur.
0045  *
0046  * Typical usage can be:
0047  * @code
0048   KoProgressUpdater *pu = new KoProgressUpdater(myProgressBar);
0049   pu->start(100);
0050   // create the subtasks
0051   KoUpdater smooth = pu->startSubtask(5);
0052   KoUpdater scale = pu->startSubtask(5);
0053   KoUpdater cleanup = pu->startSubtask(1);
0054   @endcode
0055  * Doing a smooth.setProgress(50) will move the progress bar to 50% of the share
0056  * of task 'smooth' which is 5 / 11 of the total and thus to 22.
0057  *
0058  * KoProgressUpdater should be created in the main thread;
0059  * KoProgressProxy must be, if it is gui subclass in the QApplication
0060  * main thread. The other objects can be created in whatever thread
0061  * one wants.
0062  *
0063  * Also to prevent jumps in the progress-calculation and -display it is recommed
0064  * to first create all the subtasks and then start to use setProgress on them.
0065  */
0066 class KOWIDGETUTILS_EXPORT KoProgressUpdater : public QObject
0067 {
0068     Q_OBJECT
0069 public:
0070 
0071     enum Mode {
0072         Threaded,
0073         Unthreaded
0074     };
0075 
0076     /**
0077      * Constructor.
0078      * @param progressBar the progress bar to update.
0079      */
0080     explicit KoProgressUpdater(KoProgressProxy *progressBar, Mode mode = Threaded,
0081                                QTextStream *output = 0);
0082 
0083     /// destructor
0084     ~KoProgressUpdater() override;
0085 
0086     /**
0087      * Start a new task.
0088      *
0089      * This will invalidate any previously created subtasks and set
0090      * the range of the progressBar as well as the text in the
0091      * progressbar.
0092      *
0093      * @param range the total range of progress bar makes.
0094      * @param text The text to show in the progressBar.
0095      * @see KoProgressProxy::setRange()
0096      * @see KoProgressProxy::setFormat()
0097      */
0098     void start(int range = 100, const QString &text = QLatin1String("%p%"));
0099 
0100     /**
0101      * After calling start() you can create any number of Updaters,
0102      * one for each subtask. @param weight use a weight to specify the
0103      * weight this subtask has compared to the rest of the subtasks.
0104      *
0105      * KoProgressUpdater will delete the KoUpdater instances when a
0106      * start() is called or when it is deleted. The KoUpdater pointers
0107      * are packed in a QPointer so you can check whether they have
0108      * been deleted before dereferencing.
0109      */
0110     QPointer<KoUpdater> startSubtask(int weight=1,
0111                                      const QString &name = QString());
0112 
0113     /**
0114      * Cancelling the action will make each subtask be marked as 'interrupted' and
0115      * set the total progress to 100%.
0116      */
0117     void cancel();
0118 
0119     /**
0120      * Set the time with respect to which the progress events are logged.
0121      */
0122     void setReferenceTime(const QTime &referenceTime);
0123 
0124     /**
0125      * Get the time with respect to which the progress events are logged.
0126      */
0127     QTime referenceTime() const;
0128 
0129     /**
0130      * @return true when the processing is interrupted
0131      */
0132     bool interrupted() const;
0133 
0134     /**
0135      * @return true when the output has been set
0136      */
0137     bool hasOutput() const;
0138 
0139 private Q_SLOTS:
0140 
0141     void update();
0142     void updateUi();
0143 
0144 private:
0145 
0146     class Private;
0147     Private *const d;
0148 
0149 };
0150 
0151 
0152 
0153 
0154 #endif
0155