Skip to content

Commit 8182ec2

Browse files
committedFeb 12, 2017
QgsFeedback can handle report progress reports
1 parent 4b3d401 commit 8182ec2

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
 

‎python/core/qgsfeedback.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ class QgsFeedback : QObject
3636
//! Tells whether the operation has been canceled already
3737
bool isCanceled() const;
3838

39+
void setProgress( double progress );
40+
41+
double progress() const;
42+
3943
signals:
4044
//! Internal routines can connect to this signal if they use event loop
4145
void canceled();
4246

47+
void progressChanged( double progress );
48+
49+
4350
};

‎src/core/qgsfeedback.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,44 @@ class CORE_EXPORT QgsFeedback : public QObject
6161
//! Tells whether the operation has been canceled already
6262
bool isCanceled() const { return mCanceled; }
6363

64+
/**
65+
* Sets the current progress for the feedback object. The \a progress
66+
* argument is in percentage and valid values range from 0-100.
67+
* @see progress()
68+
* @see progressChanged()
69+
* @note added in QGIS 3.0
70+
*/
71+
void setProgress( double progress ) { mProgress = progress; emit progressChanged( mProgress ); }
72+
73+
/**
74+
* Returns the current progress reported by the feedback object. Depending on how the
75+
* feedback object is used progress reporting may not be supported. The returned value
76+
* is in percentage and ranges from 0-100.
77+
* @see setProgress()
78+
* @see progressChanged()
79+
* @note added in QGIS 3.0
80+
*/
81+
double progress() const { return mProgress; }
82+
6483
signals:
6584
//! Internal routines can connect to this signal if they use event loop
6685
void canceled();
6786

87+
/**
88+
* Emitted when the feedback object reports a progress change. Depending on how the
89+
* feedback object is used progress reporting may not be supported. The \a progress
90+
* argument is in percentage and ranges from 0-100.
91+
* @note added in QGIS 3.0
92+
* @see setProgress()
93+
* @see progress()
94+
*/
95+
void progressChanged( double progress );
96+
6897
private:
6998
//! Whether the operation has been canceled already. False by default.
7099
bool mCanceled;
100+
101+
double mProgress = 0.0;
71102
};
72103

73104
#endif // QGSFEEDBACK_H

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ADD_PYTHON_TEST(PyQgsFieldFormattersTest test_qgsfieldformatters.py)
5050
ADD_PYTHON_TEST(PyQgsFillSymbolLayers test_qgsfillsymbollayers.py)
5151
ADD_PYTHON_TEST(PyQgsProject test_qgsproject.py)
5252
ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py)
53+
ADD_PYTHON_TEST(PyQgsFeedback test_qgsfeedback.py)
5354
ADD_PYTHON_TEST(PyQgsField test_qgsfield.py)
5455
ADD_PYTHON_TEST(PyQgsFieldModel test_qgsfieldmodel.py)
5556
ADD_PYTHON_TEST(PyQgsFilterLineEdit test_qgsfilterlineedit.py)

‎tests/src/python/test_qgsfeedback.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsFeedback.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '12/02/2017'
11+
__copyright__ = 'Copyright 2017, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
import os
17+
18+
from qgis.core import (QgsFeedback)
19+
from qgis.PyQt.QtTest import QSignalSpy
20+
from qgis.testing import unittest
21+
22+
23+
class TestQgsFeedback(unittest.TestCase):
24+
25+
def testCancel(self):
26+
f = QgsFeedback()
27+
self.assertFalse(f.isCanceled())
28+
29+
cancel_spy = QSignalSpy(f.canceled)
30+
31+
f.cancel()
32+
self.assertTrue(f.isCanceled())
33+
self.assertEqual(len(cancel_spy), 1)
34+
35+
def testProgress(self):
36+
f = QgsFeedback()
37+
self.assertEqual(f.progress(), 0.0)
38+
39+
progress_spy = QSignalSpy(f.progressChanged)
40+
41+
f.setProgress(25)
42+
self.assertEqual(f.progress(), 25.0)
43+
self.assertEqual(len(progress_spy), 1)
44+
self.assertEqual(progress_spy[0][0], 25.0)
45+
46+
47+
if __name__ == '__main__':
48+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.