1 | import wx |
---|
2 | from wx import StatusBar as wxStatusB |
---|
3 | import wx.lib |
---|
4 | from wx.lib import newevent |
---|
5 | #numner of fields of the status bar |
---|
6 | NB_FIELDS = 3 |
---|
7 | #position of the status bar's fields |
---|
8 | ICON_POSITION = 0 |
---|
9 | MSG_POSITION = 1 |
---|
10 | GAUGE_POSITION = 2 |
---|
11 | BUTTON_SIZE = 40 |
---|
12 | class StatusBar(wxStatusB): |
---|
13 | def __init__(self, parent, *args, **kargs): |
---|
14 | wxStatusB.__init__(self, parent, *args, **kargs) |
---|
15 | """ |
---|
16 | Implement statusbar functionalities |
---|
17 | """ |
---|
18 | self.parent= parent |
---|
19 | self.parent.SetStatusBarPane(MSG_POSITION) |
---|
20 | |
---|
21 | #Layout of status bar |
---|
22 | self.SetFieldsCount(NB_FIELDS) |
---|
23 | self.SetStatusWidths([BUTTON_SIZE, -2, -1]) |
---|
24 | |
---|
25 | #display default message |
---|
26 | self.msg_position = MSG_POSITION |
---|
27 | |
---|
28 | #save the position of the gauge |
---|
29 | width, height = self.GetSize() |
---|
30 | self.gauge = wx.Gauge(self, size=(width/10,height-3), |
---|
31 | style= wx.GA_HORIZONTAL) |
---|
32 | rect = self.GetFieldRect(GAUGE_POSITION) |
---|
33 | self.gauge.SetPosition((rect.x+5, rect.y-2)) |
---|
34 | self.gauge.Hide() |
---|
35 | |
---|
36 | #status bar icon |
---|
37 | self.bitmap_bt_warning = wx.BitmapButton(self, -1, size=(BUTTON_SIZE,-1), |
---|
38 | style=wx.NO_BORDER) |
---|
39 | rect = self.GetFieldRect(ICON_POSITION) |
---|
40 | self.bitmap_bt_warning.SetPosition((rect.x+5, rect.y-2)) |
---|
41 | |
---|
42 | ## Current progress value of the bar |
---|
43 | self.progress = 0 |
---|
44 | self.timer = wx.Timer(self, -1) |
---|
45 | self.timer_stop = wx.Timer(self, -1) |
---|
46 | self.thread = None |
---|
47 | self.Bind(wx.EVT_TIMER,self.OnTimer, self.timer) |
---|
48 | self.Bind(wx.EVT_TIMER,self.OnTimer_stop, self.timer_stop) |
---|
49 | |
---|
50 | def get_msg_position(self): |
---|
51 | """ |
---|
52 | """ |
---|
53 | return self.msg_position |
---|
54 | |
---|
55 | def SetStatusText(self, text="", number=MSG_POSITION): |
---|
56 | """ |
---|
57 | """ |
---|
58 | wxStatusB.SetStatusText(self, text, MSG_POSITION) |
---|
59 | |
---|
60 | def PopStatusText(self, *args, **kwds): |
---|
61 | wxStatusB.PopStatusText(self, field=MSG_POSITION) |
---|
62 | |
---|
63 | def PushStatusText(self, *args, **kwds): |
---|
64 | wxStatusB.PushStatusText(self, field=MSG_POSITION,string=string) |
---|
65 | |
---|
66 | def OnTimer_stop(self, evt): |
---|
67 | """Clear the progress bar |
---|
68 | @param evt: wx.EVT_TIMER |
---|
69 | |
---|
70 | """ |
---|
71 | count = 0 |
---|
72 | while(count <= 100): |
---|
73 | count += 1 |
---|
74 | self.timer_stop.Stop() |
---|
75 | self.clear_gauge(msg="") |
---|
76 | |
---|
77 | def OnTimer(self, evt): |
---|
78 | """Update the progress bar while the timer is running |
---|
79 | @param evt: wx.EVT_TIMER |
---|
80 | |
---|
81 | """ |
---|
82 | # Check stop flag that can be set from non main thread |
---|
83 | if self.timer.IsRunning(): |
---|
84 | self.gauge.Pulse() |
---|
85 | """ |
---|
86 | def set_progress(self): |
---|
87 | #Set the gauge value given the status of a thread |
---|
88 | self.gauge.Show(True) |
---|
89 | if self.timer.IsRunning(): |
---|
90 | while(self.thread.isrunning()): |
---|
91 | self.progress += 1 |
---|
92 | # Update the progress value if it is less than the range |
---|
93 | if self.progress < self.gauge.GetRange()-20: |
---|
94 | self.gauge.SetValue(int(self.progress)) |
---|
95 | else: |
---|
96 | self.gauge.SetValue(70) |
---|
97 | self.progress = 0 |
---|
98 | self.timer.Stop() |
---|
99 | self.timer.Stop() |
---|
100 | self.gauge.SetValue(90) |
---|
101 | self.progress =0 |
---|
102 | """ |
---|
103 | def clear_gauge(self, msg=""): |
---|
104 | """ |
---|
105 | Hide the gauge |
---|
106 | """ |
---|
107 | #self.SetStatusText(str(msg), MSG_POSITION) |
---|
108 | self.progress = 0 |
---|
109 | self.gauge.SetValue(0) |
---|
110 | self.gauge.Hide() |
---|
111 | |
---|
112 | def set_icon(self, event): |
---|
113 | """ |
---|
114 | display icons related to the type of message sent to the statusbar |
---|
115 | when available |
---|
116 | """ |
---|
117 | if not hasattr(event, "info"): |
---|
118 | return |
---|
119 | msg = event.info.lower() |
---|
120 | if msg == "warning": |
---|
121 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_TOOLBAR) |
---|
122 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
123 | if msg == "error": |
---|
124 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_TOOLBAR) |
---|
125 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
126 | if msg == "info": |
---|
127 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
128 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
129 | |
---|
130 | def set_message(self, event): |
---|
131 | """ |
---|
132 | display received message on the statusbar |
---|
133 | """ |
---|
134 | if hasattr(event, "status"): |
---|
135 | self.SetStatusText(str(event.status)) |
---|
136 | |
---|
137 | def set_gauge(self, event): |
---|
138 | """ |
---|
139 | change the state of the gauge according the state of the current job |
---|
140 | """ |
---|
141 | if not hasattr(event, "type"): |
---|
142 | return |
---|
143 | type = event.type |
---|
144 | self.gauge.Show(True) |
---|
145 | if type.lower()=="start": |
---|
146 | self.timer.Stop() |
---|
147 | self.progress += 10 |
---|
148 | self.gauge.SetValue(int(self.progress)) |
---|
149 | self.progress += 10 |
---|
150 | if self.progress < self.gauge.GetRange()-20: |
---|
151 | self.gauge.SetValue(int(self.progress)) |
---|
152 | if type.lower()=="progress": |
---|
153 | self.timer.Start(100) |
---|
154 | self.gauge.Pulse() |
---|
155 | if type.lower()=="update": |
---|
156 | self.timer.Stop() |
---|
157 | self.progress += 10 |
---|
158 | if self.progress < self.gauge.GetRange()-20: |
---|
159 | self.gauge.SetValue(int(self.progress)) |
---|
160 | if type.lower()=="stop": |
---|
161 | self.gauge.Show(True) |
---|
162 | self.timer.Stop() |
---|
163 | self.progress = 0 |
---|
164 | self.gauge.SetValue(90) |
---|
165 | self.timer_stop.Start(3) |
---|
166 | |
---|
167 | def set_status(self, event): |
---|
168 | """ |
---|
169 | Update the status bar . |
---|
170 | @param type: type of message send. |
---|
171 | type must be in ["start","progress","update","stop"] |
---|
172 | @param msg: the message itself as string |
---|
173 | @param thread: if updatting using a thread status |
---|
174 | """ |
---|
175 | self.set_icon(event=event) |
---|
176 | self.set_message(event=event) |
---|
177 | self.set_gauge(event=event) |
---|
178 | |
---|
179 | if __name__ == "__main__": |
---|
180 | app = wx.PySimpleApp() |
---|
181 | frame= wx.Frame(None,wx.ID_ANY,'test frame') |
---|
182 | statusBar = StatusBar(frame, wx.ID_ANY) |
---|
183 | frame.SetStatusBar(statusBar) |
---|
184 | frame.Show(True) |
---|
185 | (NewPlotEvent, EVT_NEW_PLOT) = wx.lib.newevent.NewEvent() |
---|
186 | event = NewPlotEvent() |
---|
187 | event.type = "progress" |
---|
188 | event.status = "statusbar...." |
---|
189 | event.info = "info" |
---|
190 | statusBar.set_status(event=event) |
---|
191 | app.MainLoop() |
---|
192 | |
---|