summaryrefslogtreecommitdiffstats
path: root/src/widgets/dialogs/qprogressdialog.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/dialogs/qprogressdialog.cpp')
-rw-r--r--src/widgets/dialogs/qprogressdialog.cpp93
1 files changed, 31 insertions, 62 deletions
diff --git a/src/widgets/dialogs/qprogressdialog.cpp b/src/widgets/dialogs/qprogressdialog.cpp
index 448acfc7f9..9761bcc828 100644
--- a/src/widgets/dialogs/qprogressdialog.cpp
+++ b/src/widgets/dialogs/qprogressdialog.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtWidgets module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qprogressdialog.h"
@@ -53,15 +17,20 @@
#include "qelapsedtimer.h"
#include "qscopedvaluerollback.h"
#include <private/qdialog_p.h>
+
+#include <QtCore/qpointer.h>
+
#include <limits.h>
+using namespace std::chrono_literals;
+
QT_BEGIN_NAMESPACE
// If the operation is expected to take this long (as predicted by
// progress time), show the progress dialog.
-static const int defaultShowTime = 4000;
+static constexpr auto defaultShowTime = 4000ms;
// Wait at least this long before attempting to make a prediction.
-static const int minWaitTime = 50;
+static constexpr auto minWaitTime = 50ms;
class QProgressDialogPrivate : public QDialogPrivate
{
@@ -88,7 +57,7 @@ public:
QPointer<QObject> receiverToDisconnectOnClose;
QElapsedTimer starttime;
QByteArray memberToDisconnectOnClose;
- int showTime = defaultShowTime;
+ std::chrono::milliseconds showTime = defaultShowTime;
bool processingEvents = false;
bool shownOnce = false;
bool autoClose = true;
@@ -228,10 +197,11 @@ void QProgressDialogPrivate::_q_disconnectOnClose()
A modeless progress dialog is suitable for operations that take
place in the background, where the user is able to interact with the
- application. Such operations are typically based on QTimer (or
- QObject::timerEvent()) or QSocketNotifier; or performed
- in a separate thread. A QProgressBar in the status bar of your main window
- is often an alternative to a modeless progress dialog.
+ application. Such operations are typically based on a timer class,
+ such as QChronoTimer (or the more low-level QObject::timerEvent()) or
+ QSocketNotifier; or performed in a separate thread. A QProgressBar in
+ the status bar of your main window is often an alternative to a modeless
+ progress dialog.
You need to have an event loop to be running, connect the
canceled() signal to a slot that stops the operation, and call \l
@@ -251,8 +221,7 @@ void QProgressDialogPrivate::_q_disconnectOnClose()
\image fusion-progressdialog.png A progress dialog shown in the Fusion widget style.
- \sa QDialog, QProgressBar, {fowler}{GUI Design Handbook: Progress Indicator},
- {Find Files Example}, {Pixelator Example}
+ \sa QDialog, QProgressBar
*/
@@ -662,23 +631,22 @@ void QProgressDialog::setValue(int progress)
return;
} else {
d->setValueCalled = true;
- bool need_show;
- int elapsed = d->starttime.elapsed();
+ bool need_show = false;
+ using namespace std::chrono;
+ nanoseconds elapsed = d->starttime.durationElapsed();
if (elapsed >= d->showTime) {
need_show = true;
} else {
if (elapsed > minWaitTime) {
- int estimate;
- int totalSteps = maximum() - minimum();
- int myprogress = progress - minimum();
- if (myprogress == 0) myprogress = 1;
- if ((totalSteps - myprogress) >= INT_MAX / elapsed)
- estimate = (totalSteps - myprogress) / myprogress * elapsed;
+ const int totalSteps = maximum() - minimum();
+ const int myprogress = std::max(progress - minimum(), 1);
+ const int remainingSteps = totalSteps - myprogress;
+ nanoseconds estimate;
+ if (remainingSteps >= INT_MAX / elapsed.count())
+ estimate = (remainingSteps / myprogress) * elapsed;
else
- estimate = elapsed * (totalSteps - myprogress) / myprogress;
+ estimate = (elapsed * remainingSteps) / myprogress;
need_show = estimate >= d->showTime;
- } else {
- need_show = false;
}
}
if (need_show) {
@@ -754,17 +722,18 @@ void QProgressDialog::changeEvent(QEvent *ev)
void QProgressDialog::setMinimumDuration(int ms)
{
Q_D(QProgressDialog);
- d->showTime = ms;
+ std::chrono::milliseconds msecs{ms};
+ d->showTime = msecs;
if (d->bar->value() == d->bar->minimum()) {
d->forceTimer->stop();
- d->forceTimer->start(ms);
+ d->forceTimer->start(msecs);
}
}
int QProgressDialog::minimumDuration() const
{
Q_D(const QProgressDialog);
- return d->showTime;
+ return int(d->showTime.count());
}
@@ -780,7 +749,7 @@ void QProgressDialog::closeEvent(QCloseEvent *e)
/*!
\property QProgressDialog::autoReset
- \brief whether the progress dialog calls reset() as soon as value() equals maximum()
+ \brief whether the progress dialog calls reset() as soon as value() equals maximum().
The default is true.