1# -*- coding: utf-8 -*-
2"""
3Using ProgressDialog to show progress updates in a nested process.
4
5"""
6import initExample ## Add path to library (just for examples; you do not need this)
7
8import time
9import pyqtgraph as pg
10from pyqtgraph.Qt import QtCore, QtGui
11
12app = pg.mkQApp("Progress Dialog Example")
13
14
15def runStage(i):
16    """Waste time for 2 seconds while incrementing a progress bar.
17    """
18    with pg.ProgressDialog("Running stage %s.." % i, maximum=100, nested=True) as dlg:
19        for j in range(100):
20            time.sleep(0.02)
21            dlg += 1
22            if dlg.wasCanceled():
23                print("Canceled stage %s" % i)
24                break
25
26
27def runManyStages(i):
28    """Iterate over runStage() 3 times while incrementing a progress bar.
29    """
30    with pg.ProgressDialog("Running stage %s.." % i, maximum=3, nested=True, wait=0) as dlg:
31        for j in range(1,4):
32            runStage('%d.%d' % (i, j))
33            dlg += 1
34            if dlg.wasCanceled():
35                print("Canceled stage %s" % i)
36                break
37
38
39with pg.ProgressDialog("Doing a multi-stage process..", maximum=5, nested=True, wait=0) as dlg1:
40    for i in range(1,6):
41        if i == 3:
42            # this stage will have 3 nested progress bars
43            runManyStages(i)
44        else:
45            # this stage will have 2 nested progress bars
46            runStage(i)
47
48        dlg1 += 1
49        if dlg1.wasCanceled():
50            print("Canceled process")
51            break
52
53
54