File indexing completed on 2024-04-28 17:05:53

0001 /*
0002     SPDX-FileCopyrightText: 2000 Shie Erlich <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000 Rafi Yanai <krusader@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "krpleasewait.h"
0010 
0011 // QtCore
0012 #include <QDateTime>
0013 #include <QTimer>
0014 // QtGui
0015 #include <QCloseEvent>
0016 // QtWidgets
0017 #include <QApplication>
0018 #include <QLabel>
0019 #include <QProgressBar>
0020 #include <QPushButton>
0021 
0022 #include <KI18n/KLocalizedString>
0023 #include <KWidgetsAddons/KCursor>
0024 
0025 #include "../krglobal.h"
0026 
0027 KrPleaseWait::KrPleaseWait(const QString &msg, QWidget *parent, int count, bool cancel)
0028     : QProgressDialog(cancel ? nullptr : parent)
0029     , inc(true)
0030 {
0031     setModal(!cancel);
0032 
0033     timer = new QTimer(this);
0034     setWindowTitle(i18n("Krusader::Wait"));
0035 
0036     setMinimumDuration(500);
0037     setAutoClose(false);
0038     setAutoReset(false);
0039 
0040     connect(timer, &QTimer::timeout, this, &KrPleaseWait::cycleProgress);
0041 
0042     auto *progress = new QProgressBar(this);
0043     progress->setMaximum(count);
0044     progress->setMinimum(0);
0045     setBar(progress);
0046 
0047     QLabel *label = new QLabel(this);
0048     setLabel(label);
0049 
0050     QPushButton *btn = new QPushButton(i18n("&Cancel"), this);
0051     setCancelButton(btn);
0052 
0053     btn->setEnabled(canClose = cancel);
0054     setLabelText(msg);
0055 
0056     show();
0057 }
0058 
0059 void KrPleaseWait::closeEvent(QCloseEvent *e)
0060 {
0061     if (canClose) {
0062         emit canceled();
0063         e->accept();
0064     } else /* if cancel is not allowed, we disable */
0065         e->ignore(); /* the window closing [x] also */
0066 }
0067 
0068 void KrPleaseWait::incProgress(int howMuch)
0069 {
0070     setValue(value() + howMuch);
0071 }
0072 
0073 void KrPleaseWait::cycleProgress()
0074 {
0075     if (inc)
0076         setValue(value() + 1);
0077     else
0078         setValue(value() - 1);
0079     if (value() >= 9)
0080         inc = false;
0081     if (value() <= 0)
0082         inc = true;
0083 }
0084 
0085 KrPleaseWaitHandler::KrPleaseWaitHandler(QWidget *parentWindow)
0086     : QObject(parentWindow)
0087     , _parentWindow(parentWindow)
0088     , dlg(nullptr)
0089 {
0090 }
0091 
0092 void KrPleaseWaitHandler::stopWait()
0093 {
0094     if (dlg != nullptr)
0095         delete dlg;
0096     dlg = nullptr;
0097     cycleMutex = incMutex = false;
0098     // return cursor to normal arrow
0099     _parentWindow->setCursor(Qt::ArrowCursor);
0100 }
0101 
0102 void KrPleaseWaitHandler::startWaiting(const QString &msg, int count, bool cancel)
0103 {
0104     if (dlg == nullptr) {
0105         dlg = new KrPleaseWait(msg, _parentWindow, count, cancel);
0106         connect(dlg, &KrPleaseWait::canceled, this, &KrPleaseWaitHandler::killJob);
0107     }
0108     incMutex = cycleMutex = _wasCancelled = false;
0109     dlg->setValue(0);
0110 
0111     dlg->setLabelText(msg);
0112     if (count == 0) {
0113         dlg->setMaximum(10);
0114         cycle = true;
0115         cycleProgress();
0116     } else {
0117         dlg->setMaximum(count);
0118         cycle = false;
0119     }
0120 }
0121 
0122 void KrPleaseWaitHandler::cycleProgress()
0123 {
0124     if (cycleMutex)
0125         return;
0126     cycleMutex = true;
0127     if (dlg)
0128         dlg->cycleProgress();
0129     if (cycle)
0130         QTimer::singleShot(2000, this, &KrPleaseWaitHandler::cycleProgress);
0131     cycleMutex = false;
0132 }
0133 
0134 void KrPleaseWaitHandler::killJob()
0135 {
0136     if (!job.isNull())
0137         job->kill(KJob::EmitResult);
0138     stopWait();
0139     _wasCancelled = true;
0140 }
0141 
0142 void KrPleaseWaitHandler::setJob(KIO::Job *j)
0143 {
0144     job = j;
0145 }
0146 
0147 void KrPleaseWaitHandler::incProgress(int i)
0148 {
0149     if (incMutex)
0150         return;
0151     incMutex = true;
0152     if (dlg)
0153         dlg->incProgress(i);
0154     incMutex = false;
0155 }