File indexing completed on 2024-05-19 13:31:51

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de
0006  * SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com
0007  * SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 // clang-format on
0010 
0011 #ifndef PROGRESS_H
0012 #define PROGRESS_H
0013 
0014 #include "ProgressProxy.h"
0015 #include "ui_progressdialog.h"
0016 
0017 #include <list>
0018 
0019 #include <QDialog>
0020 #include <QElapsedTimer>
0021 #include <QPointer>
0022 #include <QThread>
0023 
0024 class KJob;
0025 class QEventLoop;
0026 class QLabel;
0027 class QProgressBar;
0028 class QStatusBar;
0029 
0030 class ProgressDialog: public QDialog
0031 {
0032     Q_OBJECT
0033   public:
0034     ProgressDialog(QWidget* pParent, QStatusBar*);
0035     ~ProgressDialog() override;
0036 
0037     void beginBackgroundTask();
0038     void endBackgroundTask();
0039 
0040     void setStayHidden(bool bStayHidden);
0041     void setInformation(const QString& info, bool bRedrawUpdate = true);
0042     void setInformation(const QString& info, qint32 current, bool bRedrawUpdate = true);
0043     void setCurrent(quint64 current, bool bRedrawUpdate = true);
0044     void step(bool bRedrawUpdate = true);
0045     void clear();
0046     void setMaxNofSteps(const quint64 dMaxNofSteps);
0047     void addNofSteps(const quint64 nofSteps);
0048     void push();
0049     void pop(bool bRedrawUpdate = true);
0050 
0051     // The progressbar goes from 0 to 1 usually.
0052     // By supplying a subrange transformation the subCurrent-values
0053     // 0 to 1 will be transformed to dMin to dMax instead.
0054     // Requirement: 0 < dMin < dMax < 1
0055     void setRangeTransformation(double dMin, double dMax);
0056     void setSubRangeTransformation(double dMin, double dMax);
0057 
0058     void exitEventLoop();
0059     void enterEventLoop(KJob* pJob, const QString& jobInfo);
0060 
0061     bool wasCancelled();
0062     enum e_CancelReason
0063     {
0064         eExit,
0065         eUserAbort,
0066         eResize
0067     };
0068     void cancel(e_CancelReason);
0069     e_CancelReason cancelReason();
0070     void clearCancelState();
0071     void show();
0072     void hide();
0073     void hideStatusBarWidget();
0074     void delayedHideStatusBarWidget();
0075 
0076     void timerEvent(QTimerEvent* event) override;
0077 
0078   protected:
0079     void reject() override;
0080 
0081   private:
0082     void setInformationImp(const QString& info);
0083     void initConnections();
0084 
0085     //Treated as slot by direct call to QMetaObject::invokeMethod
0086   public Q_SLOTS:
0087     void recalc(bool bUpdate);
0088 
0089   private Q_SLOTS:
0090     void delayedHide();
0091     void slotAbort();
0092 
0093   private:
0094     Ui::ProgressDialog dialogUi;
0095 
0096     struct ProgressLevelData {
0097         QAtomicInteger<quint64> m_current = 0;
0098         QAtomicInteger<quint64> m_maxNofSteps = 1; // when step() is used.
0099         double m_dRangeMax = 1;
0100         double m_dRangeMin = 0;
0101         double m_dSubRangeMax = 1;
0102         double m_dSubRangeMin = 0;
0103     };
0104     quint64 backgroundTaskCount = 0;
0105     std::list<ProgressLevelData> m_progressStack;
0106 
0107     qint32 m_progressDelayTimer = 0;
0108     qint32 m_delayedHideTimer = 0;
0109     qint32 m_delayedHideStatusBarWidgetTimer = 0;
0110     QPointer<QEventLoop> m_eventLoop;
0111 
0112     QElapsedTimer m_t1;
0113     QElapsedTimer m_t2;
0114     bool m_bWasCancelled = false;
0115     e_CancelReason m_eCancelReason = eUserAbort;
0116     KJob* m_pJob = nullptr;
0117     QString m_currentJobInfo; // Needed if the job doesn't stop after a reasonable time.
0118     bool m_bStayHidden = false;
0119     QThread* m_pGuiThread = QThread::currentThread();
0120     QStatusBar* m_pStatusBar = nullptr; // status bar of main window (if exists)
0121     QWidget* m_pStatusBarWidget = nullptr;
0122     QProgressBar* m_pStatusProgressBar = nullptr;
0123     QPushButton* m_pStatusAbortButton = nullptr;
0124     /*
0125         This list exists solely to auto disconnect boost signals.
0126     */
0127     std::list<boost::signals2::scoped_connection> connections;
0128 };
0129 
0130 extern QPointer<ProgressDialog> g_pProgressDialog;
0131 
0132 #endif