summaryrefslogtreecommitdiffstats
path: root/python/pyqt/examples3/progressbar.py
blob: 9aba9ee58e1c09e41eb575dfa8bb0d87a54fe694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python

"""***********************************************************************
** $Id$
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
**************************************************************************"""

#import pdb
import sys
from qt import *

FALSE = 0
TRUE  = 1

class ProgressBar( QButtonGroup ):
    # Constructor
    # Creates child widgets of the ProgressBar widget
    def __init__( self, parent=None, name=None ):
        QButtonGroup.__init__( self, 0, Qt.Horizontal, "Progress Bar", parent, name )
        
        self.timer = QTimer()
        
        self.setMargin( 10 )
        toplayout = QGridLayout( self.layout(), 2, 2, 5)
            
        self.setRadioButtonExclusive( TRUE )
        
        # insert three radiobuttons which the user can use
        # to set the speed of the progress and two pushbuttons
        # to start/pause/continue and reset the progress
        self.slow   = QRadioButton( "&Slow",   self )
        self.normal = QRadioButton( "&Normal", self )
        self.fast   = QRadioButton( "&Fast",   self )
        vb1 = QVBoxLayout( )
        toplayout.addLayout( vb1, 0, 0 )
        vb1.addWidget( self.slow )
        vb1.addWidget( self.normal )
        vb1.addWidget( self.fast )
        
        # two push buttons, one for start, for for reset.
        self.start = QPushButton( "&Start", self )
        self.reset = QPushButton( "&Reset", self )
        
        vb2 = QVBoxLayout()
        toplayout.addLayout( vb2, 0, 1 )
        vb2.addWidget( self.start )
        vb2.addWidget( self.reset )
        
        # Create the progressbar
        self.progress = QProgressBar( 100, self )
        #self.progress.setStyle( QMotifStyle() )
        toplayout.addMultiCellWidget( self.progress, 1, 1, 0, 1 )
        
        # connect the clicked() SIGNALs of the pushbuttons to SLOTs
        self.connect( self.start, SIGNAL("clicked()"), self.slotStart )
        self.connect( self.reset, SIGNAL("clicked()"), self.slotReset )
        
        # connect the timeout() SIGNAL of the progress-timer to a SLOT
        self.connect( self.timer, SIGNAL("timeout()"), self.slotTimeout )
        # Let's start with normal speed...
        self.normal.setChecked( TRUE )

        # some contraints
        self.start.setFixedWidth( 80 )
        self.setMinimumWidth( 300 )
        
    # SLOT slotStart
    # This SLOT is called if the user clicks start/pause/continue button
    def slotStart( self ):
        # If the progress bar is at the beginning...
        if self.progress.progress() == -1 :
            # ...set according to the checked speed-radiobutton the
            # the number of steps which are needed to complete the process
            if self.slow.isChecked():
                self.progress.setTotalSteps( 10000 )
            elif self.normal.isChecked() :
                self.progress.setTotalSteps( 1000 )
            else :
                self.progress.setTotalSteps( 50 )
    
            # disable the speed-radiobuttons
            self.slow.setEnabled( FALSE )
            self.normal.setEnabled( FALSE )
            self.fast.setEnabled( FALSE )
            
        # If the progress is not running...
        if not self.timer.isActive() :
            # ...start the timer (and so the progress) with a interval of 1 ms...
            self.timer.start( 1 )
            # ...and rename the start/pause/continue button to Pause
            self.start.setText( "&Pause" )
        else : # if the prgress is running...
            # ...stop the timer (and so the prgress)...
            self.timer.stop()
            # ...and rename the start/pause/continue button to Continue
            self.start.setText( "&Continue" )
    
    # SLOT slotReset
    # This SLOT is called when the user clicks the reset button
    def slotReset( self ):
        # stop the timer and progress
        self.timer.stop()
    
        # rename the start/pause/continue button to Start...
        self.start.setText( "&Start" )
        # ...and enable this button
        self.start.setEnabled( TRUE )
    
        # enable the speed-radiobuttons
        self.slow.setEnabled( TRUE )
        self.normal.setEnabled( TRUE )
        self.fast.setEnabled( TRUE )
    
        # reset the progressbar
        self.progress.reset()
    
    # SLOT slotTimeout
    # This SLOT is called each ms when the timer is
    # active (== progress is running)
    def slotTimeout( self ):
        p = self.progress.progress()

        # If the progress is complete...
        if p == self.progress.totalSteps() :
            # ...rename the start/pause/continue button to Start...
            self.start.setText( "&Start" )
            # ...and disable it...
            self.start.setEnabled( FALSE )
            # ...and return
            return

        # If the process is not complete increase it
        p += 1
        self.progress.setProgress( p )
    

    #QPushButton *start, *pause, *reset;

def main( args ):
    a = QApplication( args )
    
    progressbar = ProgressBar()
    progressbar.setCaption("Qt Example - ProgressBar")
    a.setMainWidget(progressbar)
    progressbar.show()
    
    a.exec_loop()
    
if __name__=="__main__":
    main(sys.argv)