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