From 8182ec2d3d85298b1d2aff28810347b9224cf393 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sun, 12 Feb 2017 11:00:16 +1000 Subject: [PATCH] QgsFeedback can handle report progress reports --- python/core/qgsfeedback.sip | 7 ++++ src/core/qgsfeedback.h | 31 ++++++++++++++++++ tests/src/python/CMakeLists.txt | 1 + tests/src/python/test_qgsfeedback.py | 48 ++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 tests/src/python/test_qgsfeedback.py diff --git a/python/core/qgsfeedback.sip b/python/core/qgsfeedback.sip index 118a3acdbb1..8a835b022c7 100644 --- a/python/core/qgsfeedback.sip +++ b/python/core/qgsfeedback.sip @@ -36,8 +36,15 @@ class QgsFeedback : QObject //! Tells whether the operation has been canceled already bool isCanceled() const; + void setProgress( double progress ); + + double progress() const; + signals: //! Internal routines can connect to this signal if they use event loop void canceled(); + void progressChanged( double progress ); + + }; diff --git a/src/core/qgsfeedback.h b/src/core/qgsfeedback.h index 331ec106a7b..a053e382695 100644 --- a/src/core/qgsfeedback.h +++ b/src/core/qgsfeedback.h @@ -61,13 +61,44 @@ class CORE_EXPORT QgsFeedback : public QObject //! Tells whether the operation has been canceled already bool isCanceled() const { return mCanceled; } + /** + * Sets the current progress for the feedback object. The \a progress + * argument is in percentage and valid values range from 0-100. + * @see progress() + * @see progressChanged() + * @note added in QGIS 3.0 + */ + void setProgress( double progress ) { mProgress = progress; emit progressChanged( mProgress ); } + + /** + * Returns the current progress reported by the feedback object. Depending on how the + * feedback object is used progress reporting may not be supported. The returned value + * is in percentage and ranges from 0-100. + * @see setProgress() + * @see progressChanged() + * @note added in QGIS 3.0 + */ + double progress() const { return mProgress; } + signals: //! Internal routines can connect to this signal if they use event loop void canceled(); + /** + * Emitted when the feedback object reports a progress change. Depending on how the + * feedback object is used progress reporting may not be supported. The \a progress + * argument is in percentage and ranges from 0-100. + * @note added in QGIS 3.0 + * @see setProgress() + * @see progress() + */ + void progressChanged( double progress ); + private: //! Whether the operation has been canceled already. False by default. bool mCanceled; + + double mProgress = 0.0; }; #endif // QGSFEEDBACK_H diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index b4b6c158653..0a40483733a 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -50,6 +50,7 @@ ADD_PYTHON_TEST(PyQgsFieldFormattersTest test_qgsfieldformatters.py) ADD_PYTHON_TEST(PyQgsFillSymbolLayers test_qgsfillsymbollayers.py) ADD_PYTHON_TEST(PyQgsProject test_qgsproject.py) ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py) +ADD_PYTHON_TEST(PyQgsFeedback test_qgsfeedback.py) ADD_PYTHON_TEST(PyQgsField test_qgsfield.py) ADD_PYTHON_TEST(PyQgsFieldModel test_qgsfieldmodel.py) ADD_PYTHON_TEST(PyQgsFilterLineEdit test_qgsfilterlineedit.py) diff --git a/tests/src/python/test_qgsfeedback.py b/tests/src/python/test_qgsfeedback.py new file mode 100644 index 00000000000..a234667028c --- /dev/null +++ b/tests/src/python/test_qgsfeedback.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +"""QGIS Unit tests for QgsFeedback. + +.. note:: This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. +""" +__author__ = 'Nyall Dawson' +__date__ = '12/02/2017' +__copyright__ = 'Copyright 2017, The QGIS Project' +# This will get replaced with a git SHA1 when you do a git archive +__revision__ = '$Format:%H$' + +import qgis # NOQA +import os + +from qgis.core import (QgsFeedback) +from qgis.PyQt.QtTest import QSignalSpy +from qgis.testing import unittest + + +class TestQgsFeedback(unittest.TestCase): + + def testCancel(self): + f = QgsFeedback() + self.assertFalse(f.isCanceled()) + + cancel_spy = QSignalSpy(f.canceled) + + f.cancel() + self.assertTrue(f.isCanceled()) + self.assertEqual(len(cancel_spy), 1) + + def testProgress(self): + f = QgsFeedback() + self.assertEqual(f.progress(), 0.0) + + progress_spy = QSignalSpy(f.progressChanged) + + f.setProgress(25) + self.assertEqual(f.progress(), 25.0) + self.assertEqual(len(progress_spy), 1) + self.assertEqual(progress_spy[0][0], 25.0) + + +if __name__ == '__main__': + unittest.main()