1 | import wx |
---|
2 | from wx import StatusBar as wxStatusB |
---|
3 | from wx.lib import newevent |
---|
4 | (MessageEvent, EVT_MESSAGE) = newevent.NewEvent() |
---|
5 | #numner of fields of the status bar |
---|
6 | NB_FIELDS = 4 |
---|
7 | #position of the status bar's fields |
---|
8 | ICON_POSITION = 0 |
---|
9 | MSG_POSITION = 1 |
---|
10 | GAUGE_POSITION = 2 |
---|
11 | CONSOLE_POSITION = 3 |
---|
12 | BUTTON_SIZE = 40 |
---|
13 | |
---|
14 | CONSOLE_WIDTH = 340 |
---|
15 | CONSOLE_HEIGHT = 240 |
---|
16 | |
---|
17 | class ConsolePanel(wx.Panel): |
---|
18 | """ |
---|
19 | """ |
---|
20 | def __init__(self, parent, *args, **kwargs): |
---|
21 | """ |
---|
22 | """ |
---|
23 | wx.Panel.__init__(self, parent=parent, *args, **kwargs) |
---|
24 | self.parent = parent |
---|
25 | self.sizer = wx.BoxSizer(wx.VERTICAL) |
---|
26 | self.msg_txt = wx.TextCtrl(self, size=(CONSOLE_WIDTH-40, |
---|
27 | CONSOLE_HEIGHT-60), |
---|
28 | style=wx.TE_MULTILINE) |
---|
29 | self.msg_txt.SetEditable(False) |
---|
30 | self.msg_txt.SetValue('No message available') |
---|
31 | self.sizer.Add(self.msg_txt, 1, wx.EXPAND|wx.ALL, 10) |
---|
32 | self.SetSizer(self.sizer) |
---|
33 | |
---|
34 | def set_message(self, status=""): |
---|
35 | """ |
---|
36 | """ |
---|
37 | msg = status + "\n" |
---|
38 | self.msg_txt.AppendText(str(msg)) |
---|
39 | |
---|
40 | class Console(wx.Frame): |
---|
41 | """ |
---|
42 | """ |
---|
43 | def __init__(self, parent=None, status="", *args, **kwds): |
---|
44 | kwds["size"] = (CONSOLE_WIDTH, CONSOLE_HEIGHT) |
---|
45 | kwds["title"] = "Console" |
---|
46 | wx.Frame.__init__(self, parent=parent, *args, **kwds) |
---|
47 | self.panel = ConsolePanel(self) |
---|
48 | self.panel.set_message(status=status) |
---|
49 | self.Bind(EVT_MESSAGE, self.set_message) |
---|
50 | self.Show(True) |
---|
51 | |
---|
52 | def set_multiple_messages(self, messages=[]): |
---|
53 | """ |
---|
54 | """ |
---|
55 | if messages: |
---|
56 | for status in messages: |
---|
57 | self.panel.set_message(status) |
---|
58 | |
---|
59 | def set_message(self, event): |
---|
60 | """ |
---|
61 | """ |
---|
62 | self.panel.set_message(event.status) |
---|
63 | |
---|
64 | class StatusBar(wxStatusB): |
---|
65 | """ |
---|
66 | """ |
---|
67 | def __init__(self, parent, *args, **kargs): |
---|
68 | wxStatusB.__init__(self, parent, *args, **kargs) |
---|
69 | """ |
---|
70 | Implement statusbar functionalities |
---|
71 | """ |
---|
72 | self.parent = parent |
---|
73 | self.parent.SetStatusBarPane(MSG_POSITION) |
---|
74 | #Layout of status bar |
---|
75 | self.SetFieldsCount(NB_FIELDS) |
---|
76 | self.SetStatusWidths([BUTTON_SIZE, -2, -1, BUTTON_SIZE]) |
---|
77 | #display default message |
---|
78 | self.msg_position = MSG_POSITION |
---|
79 | #save the position of the gauge |
---|
80 | width, height = self.GetSize() |
---|
81 | self.gauge = wx.Gauge(self, size=(width/10, height-3), |
---|
82 | style=wx.GA_HORIZONTAL) |
---|
83 | self.gauge.Hide() |
---|
84 | #status bar icon |
---|
85 | self.bitmap_bt_warning = wx.BitmapButton(self, -1, |
---|
86 | size=(BUTTON_SIZE,-1), |
---|
87 | style=wx.NO_BORDER) |
---|
88 | console_bmp = wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_TOOLBAR) |
---|
89 | self.bitmap_bt_console = wx.BitmapButton(self, -1, |
---|
90 | size=(BUTTON_SIZE-5, height-4)) |
---|
91 | self.bitmap_bt_console.SetBitmapLabel(console_bmp) |
---|
92 | console_hint = "History of status bar messages" |
---|
93 | self.bitmap_bt_console.SetToolTipString(console_hint) |
---|
94 | self.bitmap_bt_console.Bind(wx.EVT_BUTTON, self._onMonitor, |
---|
95 | id=self.bitmap_bt_console.GetId()) |
---|
96 | self.reposition() |
---|
97 | ## Current progress value of the bar |
---|
98 | self.nb_start = 0 |
---|
99 | self.nb_progress = 0 |
---|
100 | self.nb_stop = 0 |
---|
101 | self.frame = None |
---|
102 | self.list_msg = [] |
---|
103 | self.progress = 0 |
---|
104 | self.timer = wx.Timer(self, -1) |
---|
105 | self.timer_stop = wx.Timer(self, -1) |
---|
106 | self.thread = None |
---|
107 | self.Bind(wx.EVT_TIMER, self._on_time, self.timer) |
---|
108 | self.Bind(wx.EVT_TIMER, self._on_time_stop, self.timer_stop) |
---|
109 | self.Bind(wx.EVT_SIZE, self.OnSize) |
---|
110 | self.Bind(wx.EVT_IDLE, self.OnIdle) |
---|
111 | |
---|
112 | def reposition(self): |
---|
113 | """ |
---|
114 | """ |
---|
115 | rect = self.GetFieldRect(GAUGE_POSITION) |
---|
116 | self.gauge.SetPosition((rect.x + 5, rect.y - 2)) |
---|
117 | rect = self.GetFieldRect(ICON_POSITION) |
---|
118 | self.bitmap_bt_warning.SetPosition((rect.x + 5, rect.y - 2)) |
---|
119 | rect = self.GetFieldRect(CONSOLE_POSITION) |
---|
120 | self.bitmap_bt_console.SetPosition((rect.x - 5, rect.y - 2)) |
---|
121 | self.sizeChanged = False |
---|
122 | |
---|
123 | def OnIdle(self, event): |
---|
124 | """ |
---|
125 | """ |
---|
126 | if self.sizeChanged: |
---|
127 | self.reposition() |
---|
128 | |
---|
129 | def OnSize(self, evt): |
---|
130 | """ |
---|
131 | """ |
---|
132 | self.reposition() |
---|
133 | self.sizeChanged = True |
---|
134 | |
---|
135 | def get_msg_position(self): |
---|
136 | """ |
---|
137 | """ |
---|
138 | return self.msg_position |
---|
139 | |
---|
140 | def SetStatusText(self, text="", number=MSG_POSITION): |
---|
141 | """ |
---|
142 | """ |
---|
143 | wxStatusB.SetStatusText(self, text, number) |
---|
144 | self.list_msg.append(text) |
---|
145 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
146 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
147 | try: |
---|
148 | if self.frame is not None and self.frame.IsShown(): |
---|
149 | event = MessageEvent() |
---|
150 | event.status = text |
---|
151 | wx.PostEvent(self.frame, event) |
---|
152 | except: |
---|
153 | return |
---|
154 | |
---|
155 | def PopStatusText(self, *args, **kwds): |
---|
156 | """ |
---|
157 | Override status bar |
---|
158 | """ |
---|
159 | wxStatusB.PopStatusText(self, field=MSG_POSITION) |
---|
160 | |
---|
161 | def PushStatusText(self, *args, **kwds): |
---|
162 | """ |
---|
163 | """ |
---|
164 | wxStatusB.PushStatusText(self, field=MSG_POSITION, string=string) |
---|
165 | |
---|
166 | def enable_clear_gauge(self): |
---|
167 | """ |
---|
168 | clear the progress bar |
---|
169 | """ |
---|
170 | flag = False |
---|
171 | if (self.nb_start <= self.nb_stop) or \ |
---|
172 | (self.nb_progress <= self.nb_stop): |
---|
173 | flag = True |
---|
174 | return flag |
---|
175 | |
---|
176 | def _on_time_stop(self, evt): |
---|
177 | """ |
---|
178 | Clear the progress bar |
---|
179 | |
---|
180 | :param evt: wx.EVT_TIMER |
---|
181 | |
---|
182 | """ |
---|
183 | count = 0 |
---|
184 | while(count <= 100): |
---|
185 | count += 1 |
---|
186 | self.timer_stop.Stop() |
---|
187 | self.clear_gauge(msg="") |
---|
188 | self.nb_progress = 0 |
---|
189 | self.nb_start = 0 |
---|
190 | self.nb_stop = 0 |
---|
191 | |
---|
192 | def _on_time(self, evt): |
---|
193 | """ |
---|
194 | Update the progress bar while the timer is running |
---|
195 | |
---|
196 | :param evt: wx.EVT_TIMER |
---|
197 | |
---|
198 | """ |
---|
199 | # Check stop flag that can be set from non main thread |
---|
200 | if self.timer.IsRunning(): |
---|
201 | self.gauge.Pulse() |
---|
202 | |
---|
203 | def clear_gauge(self, msg=""): |
---|
204 | """ |
---|
205 | Hide the gauge |
---|
206 | """ |
---|
207 | self.progress = 0 |
---|
208 | self.gauge.SetValue(0) |
---|
209 | self.gauge.Hide() |
---|
210 | |
---|
211 | def set_icon(self, event): |
---|
212 | """ |
---|
213 | display icons related to the type of message sent to the statusbar |
---|
214 | when available |
---|
215 | """ |
---|
216 | if not hasattr(event, "info"): |
---|
217 | return |
---|
218 | msg = event.info.lower() |
---|
219 | if msg == "warning": |
---|
220 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_TOOLBAR) |
---|
221 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
222 | if msg == "error": |
---|
223 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_TOOLBAR) |
---|
224 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
225 | if msg == "info": |
---|
226 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, |
---|
227 | wx.ART_TOOLBAR) |
---|
228 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
229 | |
---|
230 | def set_message(self, event): |
---|
231 | """ |
---|
232 | display received message on the statusbar |
---|
233 | """ |
---|
234 | if hasattr(event, "status"): |
---|
235 | self.SetStatusText(str(event.status)) |
---|
236 | |
---|
237 | def set_gauge(self, event): |
---|
238 | """ |
---|
239 | change the state of the gauge according the state of the current job |
---|
240 | """ |
---|
241 | if not hasattr(event, "type"): |
---|
242 | return |
---|
243 | type = event.type |
---|
244 | self.gauge.Show(True) |
---|
245 | if type.lower() == "start": |
---|
246 | self.nb_start += 1 |
---|
247 | #self.timer.Stop() |
---|
248 | self.progress += 10 |
---|
249 | self.gauge.SetValue(int(self.progress)) |
---|
250 | self.progress += 10 |
---|
251 | if self.progress < self.gauge.GetRange() - 20: |
---|
252 | self.gauge.SetValue(int(self.progress)) |
---|
253 | if type.lower() == "progress": |
---|
254 | self.nb_progress += 1 |
---|
255 | self.timer.Start(1) |
---|
256 | self.gauge.Pulse() |
---|
257 | if type.lower() == "update": |
---|
258 | self.progress += 10 |
---|
259 | if self.progress < self.gauge.GetRange()- 20: |
---|
260 | self.gauge.SetValue(int(self.progress)) |
---|
261 | if type.lower() == "stop": |
---|
262 | self.nb_stop += 1 |
---|
263 | self.gauge.Show(True) |
---|
264 | if self.enable_clear_gauge(): |
---|
265 | self.timer.Stop() |
---|
266 | self.progress = 0 |
---|
267 | self.gauge.SetValue(90) |
---|
268 | self.timer_stop.Start(3) |
---|
269 | |
---|
270 | def set_status(self, event): |
---|
271 | """ |
---|
272 | Update the status bar . |
---|
273 | |
---|
274 | :param type: type of message send. |
---|
275 | type must be in ["start","progress","update","stop"] |
---|
276 | :param msg: the message itself as string |
---|
277 | :param thread: if updatting using a thread status |
---|
278 | |
---|
279 | """ |
---|
280 | self.set_message(event=event) |
---|
281 | self.set_icon(event=event) |
---|
282 | self.set_gauge(event=event) |
---|
283 | |
---|
284 | def _onMonitor(self, event): |
---|
285 | """ |
---|
286 | Pop up a frame with messages sent to the status bar |
---|
287 | """ |
---|
288 | self.frame = Console(parent=self) |
---|
289 | self.frame.set_multiple_messages(self.list_msg) |
---|
290 | self.frame.Show(True) |
---|
291 | |
---|
292 | |
---|
293 | if __name__ == "__main__": |
---|
294 | app = wx.PySimpleApp() |
---|
295 | frame = wx.Frame(None, wx.ID_ANY, 'test frame') |
---|
296 | statusBar = StatusBar(frame, wx.ID_ANY) |
---|
297 | frame.SetStatusBar(statusBar) |
---|
298 | frame.Show(True) |
---|
299 | event = MessageEvent() |
---|
300 | event.type = "progress" |
---|
301 | event.status = "statusbar...." |
---|
302 | event.info = "error" |
---|
303 | statusBar.set_status(event=event) |
---|
304 | app.MainLoop() |
---|
305 | |
---|