Skip to content

Commit

Permalink
QgsFeedback can handle report progress reports
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 12, 2017
1 parent 4b3d401 commit 8182ec2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/qgsfeedback.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );


};
31 changes: 31 additions & 0 deletions src/core/qgsfeedback.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions tests/src/python/test_qgsfeedback.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 8182ec2

Please sign in to comment.