Skip to content

Progression

Import
from pyqgis_wrapper.utils import Progression

Progression(feedback=QgsProcessingFeedback(), mode=FeedbackType.SINGLE, steps=0)

Handle the progression bar in QGIS Provided some utilities to make easier the incrementation of the progression bar and the algorithm cancellation

Parameters:

Name Type Description Default
feedback qgis.core.QgsProcessingFeedback

Feedback object to use for the progression bar, defaults to QgsProcessingFeedback()

qgis.core.QgsProcessingFeedback()
mode typing.Union[pyqgis_wrapper.utils.progression.FeedbackType, str]

Either a single step or multi step feedback style, defaults to FeedbackType.SINGLE Accepted : - FeedbackType.SINGLE or "SINGLE" (case insensitive) - FeedbackType.MULTI or "MULTI" (case insensitive)

pyqgis_wrapper.utils.progression.FeedbackType.SINGLE
steps int

Number of steps for the MULTi mode, defaults to 0

0
Source code in pyqgis_wrapper/utils/progression.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def __init__(
    self,
    feedback: QgsProcessingFeedback = QgsProcessingFeedback(),
    mode: Union[FeedbackType, str] = FeedbackType.SINGLE,
    steps: int = 0,
):
    """
    :param feedback: Feedback object to use for
    the progression bar, defaults to QgsProcessingFeedback()
    :type feedback: QgsProcessingFeedback, optional
    :param mode: Either a single step
    or multi step feedback style, defaults to FeedbackType.SINGLE
    Accepted :
        - FeedbackType.SINGLE or "SINGLE" (case insensitive)
        - FeedbackType.MULTI or "MULTI" (case insensitive)
    :type mode: Union[FeedbackType, str], optional
    :param steps: Number of steps for the MULTi mode, defaults to 0
    :type steps: int, optional
    """
    self.steps = steps
    self.current_step = 0
    self.total = None

    mode = self._resolve_feedback_type(mode)
    if mode == "MULTI":
        if not steps:
            raise ValueError(
                "In MultiStepFeedback a number of steps should be provided."
            )
        self.feedback = QgsProcessingMultiStepFeedback(steps, feedback)
        self.feedback.setCurrentStep(0)
        self.feedback.setProgress(1)

    elif mode == "SINGLE":
        self.feedback = feedback
        self.feedback.setProgress(1)

is_canceled property

Cancel an aglorithm when the user emit a cancel signal. When catching the exception you should return an empty dict to avoid QGIS throwing an error

next_step()

Increment the step for MultiStepAlgorithm Incrementation will be ignored if we already are at the maximum steps

Source code in pyqgis_wrapper/utils/progression.py
129
130
131
132
133
134
135
136
137
138
139
140
def next_step(self):
    """
    Increment the step for MultiStepAlgorithm
    Incrementation will be ignored if we already are at the maximum steps
    """
    if self.feedback:
        if self.current_step < self.steps:
            self.current_step += 1
            self.feedback.setCurrentStep(self.current_step)
            self.feedback.setProgress(1)
            self.total = None
        self.is_canceled

set_total(total)

Allows to set a total count to scale the progress bar.

Parameters:

Name Type Description Default
total typing.Union[int, float]

Number of 'feature' to process tor each 100%

required
Source code in pyqgis_wrapper/utils/progression.py
142
143
144
145
146
147
148
149
def set_total(self, total: Union[int, float]):
    """
    Allows to set a total count to scale the progress bar.

    :param total: Number of 'feature' to process tor each 100%
    :type total: Union[int, float]
    """
    self.total = total

set_progress(progress, add=False, text=None)

Incrementation of the progress bar

Parameters:

Name Type Description Default
progress typing.Union[int, float]

Value to set the progress bar at

required
add bool

Add given progress to current progress if True. It'll not scale it to total if set., defaults to False

False
text typing.Optional[str]

Additionnal information for the user, defaults to None

None
Source code in pyqgis_wrapper/utils/progression.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def set_progress(
    self,
    progress: Union[int, float],
    add: bool = False,
    text: Optional[str] = None,
):
    """
    Incrementation of the progress bar

    :param progress: Value to set the progress bar at
    :type progress: Union[int, float]
    :param add: Add given progress to current progress if True.
    It'll not scale it to total if set., defaults to False
    :type add: bool, optional
    :param text: Additionnal information for the user, defaults to None
    :type text: Optional[str], optional
    """
    if self.feedback:
        # Set progress
        if add:
            self.feedback.setProgress(self.feedback.progress() + progress)
        else:
            self.feedback.setProgress(self._scale(progress))
        if text:
            self.feedback.setProgressText(text)
        # Check for cancellation
        self.is_canceled

FeedbackType

Bases: str, enum.Enum

Class to store feedback mode

CancelException

Bases: Exception

Raised when the cancel button is trigger. It is emant to be catched by the main loop to safely exit the algorithm