From 1d8dd9a02c9715561b365007f1520e41fe64e703 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 9 Nov 2020 14:19:02 +0100 Subject: [PATCH] Track progress range in QFutureInterface::setProgressValue Previously QFutureInterface::setProgressValue was silently ignoring the progress range and allowed to set any progress value. Also no checks were performed in QFutureInterface::setProgressRange, which allowed the user to set minimum > maximum. Add checking of the current progress range, when settings the progress value. Add checks for minimum and maximum values while setting the progress range. The implementation of the checks is mostly based on the logic that is used in QProgressBar. - If maximum is smaller than minimum, minimum becomes the only legal value. - If the current progress value falls outside the new range, the progress value is set to be minimum. - If both progressMinimum() and progressMaximum() return 0, the current progress range is considered to be unused, and any progress value can be set. - When setting the value using setProgressValue(), if the value falls out of the progress range, the method has no effect. Task-number: QTBUG-84729 Change-Id: I29cf4f94b8e98e1af30dd46fbdba39c421cf66bf Reviewed-by: Qt CI Bot Reviewed-by: Sona Kurazyan --- src/corelib/thread/qfutureinterface.cpp | 32 +++++++++++- src/corelib/thread/qpromise.qdoc | 16 +++++- .../corelib/thread/qfuture/tst_qfuture.cpp | 51 +++++++++++++++++++ .../corelib/thread/qpromise/tst_qpromise.cpp | 1 - 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/corelib/thread/qfutureinterface.cpp b/src/corelib/thread/qfutureinterface.cpp index 600dd9bd3f1..57ddc7407e4 100644 --- a/src/corelib/thread/qfutureinterface.cpp +++ b/src/corelib/thread/qfutureinterface.cpp @@ -484,12 +484,29 @@ void QFutureInterfaceBase::setFilterMode(bool enable) resultStoreBase().setFilterMode(enable); } +/*! + \internal + Sets the progress range's minimum and maximum values to \a minimum and + \a maximum respectively. + + If \a maximum is smaller than \a minimum, \a minimum becomes the only + legal value. + + The progress value is reset to be \a minimum. + + The progress range usage can be disabled by using setProgressRange(0, 0). + In this case progress value is also reset to 0. + + The behavior of this method is mostly inspired by + \l QProgressBar::setRange. +*/ void QFutureInterfaceBase::setProgressRange(int minimum, int maximum) { QMutexLocker locker(&d->m_mutex); d->m_progressMinimum = minimum; - d->m_progressMaximum = maximum; + d->m_progressMaximum = qMax(minimum, maximum); d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::ProgressRange, minimum, maximum)); + d->m_progressValue = minimum; } void QFutureInterfaceBase::setProgressValue(int progressValue) @@ -497,12 +514,25 @@ void QFutureInterfaceBase::setProgressValue(int progressValue) setProgressValueAndText(progressValue, QString()); } +/*! + \internal + In case of the \a progressValue falling out of the progress range, + this method has no effect. + Such behavior is inspired by \l QProgressBar::setValue. +*/ void QFutureInterfaceBase::setProgressValueAndText(int progressValue, const QString &progressText) { QMutexLocker locker(&d->m_mutex); if (d->manualProgress == false) d->manualProgress = true; + + const bool useProgressRange = (d->m_progressMaximum != 0) || (d->m_progressMinimum != 0); + if (useProgressRange + && ((progressValue < d->m_progressMinimum) || (progressValue > d->m_progressMaximum))) { + return; + } + if (d->m_progressValue >= progressValue) return; diff --git a/src/corelib/thread/qpromise.qdoc b/src/corelib/thread/qpromise.qdoc index ae0d924668d..78182323bee 100644 --- a/src/corelib/thread/qpromise.qdoc +++ b/src/corelib/thread/qpromise.qdoc @@ -217,7 +217,16 @@ Sets the progress range of the computation to be between \a minimum and \a maximum. - \sa QFuture::progressMinimum(), QFuture::progressMaximum() + If \a maximum is smaller than \a minimum, \a minimum becomes the only + legal value. + + The progress value is reset to be \a minimum. + + The progress range usage can be disabled by using setProgressRange(0, 0). + In this case progress value is also reset to 0. + + \sa QFuture::progressMinimum(), QFuture::progressMaximum(), + QFuture::progressValue() */ /*! \fn template void QPromise::setProgressValue(int progressValue) @@ -226,7 +235,10 @@ possible to only increment the progress value. This is a convenience method for calling setProgressValueAndText(progressValue, QString()). - \sa QFuture::progressValue() + In case of the \a progressValue falling out of the progress range, + this method has no effect. + + \sa QFuture::progressValue(), setProgressRange() */ /*! \fn template void QPromise::setProgressValueAndText(int progressValue, const QString &progressText) diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index e2c81840cb2..0d1097ce1cf 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -107,6 +107,8 @@ private slots: void multipleResults(); void indexedResults(); void progress(); + void setProgressRange(); + void progressWithRange(); void progressText(); void resultsAfterFinished(); void resultsAsList(); @@ -1000,6 +1002,55 @@ void tst_QFuture::progress() QCOMPARE (f.progressValue(), 50); } +void tst_QFuture::setProgressRange() +{ + QFutureInterface i; + + QCOMPARE(i.progressMinimum(), 0); + QCOMPARE(i.progressMaximum(), 0); + + i.setProgressRange(10, 5); + + QCOMPARE(i.progressMinimum(), 10); + QCOMPARE(i.progressMaximum(), 10); + + i.setProgressRange(5, 10); + + QCOMPARE(i.progressMinimum(), 5); + QCOMPARE(i.progressMaximum(), 10); +} + +void tst_QFuture::progressWithRange() +{ + QFutureInterface i; + QFuture f; + + i.reportStarted(); + f = i.future(); + + QCOMPARE(i.progressValue(), 0); + + i.setProgressRange(5, 10); + + QCOMPARE(i.progressValue(), 5); + + i.setProgressValue(20); + + QCOMPARE(i.progressValue(), 5); + + i.setProgressValue(9); + + QCOMPARE(i.progressValue(), 9); + + i.setProgressRange(5, 7); + + QCOMPARE(i.progressValue(), 5); + + i.reportFinished(); + + QCOMPARE(f.progressValue(), 5); +} + void tst_QFuture::progressText() { QFutureInterface i; diff --git a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp index d8f321215bf..82a359451d8 100644 --- a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp +++ b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp @@ -310,7 +310,6 @@ void tst_QPromise::progress() promise.setProgressValue(0); // decrement QCOMPARE(f.progressValue(), 1); promise.setProgressValue(10); // out of range - QEXPECT_FAIL("", "Out of range value is set - QTBUG-84729", Continue); QCOMPARE(f.progressValue(), 1); promise.setProgressRange(0, 100);