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