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 | rect = self.GetFieldRect(GAUGE_POSITION) |
---|
78 | self.gauge.SetPosition((rect.x+5, rect.y-2)) |
---|
79 | self.gauge.Hide() |
---|
80 | |
---|
81 | #status bar icon |
---|
82 | self.bitmap_bt_warning = wx.BitmapButton(self, -1, size=(BUTTON_SIZE,-1), |
---|
83 | style=wx.NO_BORDER) |
---|
84 | rect = self.GetFieldRect(ICON_POSITION) |
---|
85 | self.bitmap_bt_warning.SetPosition((rect.x+5, rect.y-2)) |
---|
86 | |
---|
87 | console_bmp = wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_TOOLBAR) |
---|
88 | self.bitmap_bt_console = wx.BitmapButton(self, -1, |
---|
89 | size=(BUTTON_SIZE-5, height-4)) |
---|
90 | self.bitmap_bt_console.SetBitmapLabel(console_bmp) |
---|
91 | console_hint = "History of status bar messages" |
---|
92 | self.bitmap_bt_console.SetToolTipString(console_hint) |
---|
93 | self.bitmap_bt_console.Bind(wx.EVT_BUTTON, self._onMonitor, |
---|
94 | id=self.bitmap_bt_console.GetId()) |
---|
95 | rect = self.GetFieldRect(CONSOLE_POSITION) |
---|
96 | self.bitmap_bt_console.SetPosition((rect.x+5, rect.y-2)) |
---|
97 | ## Current progress value of the bar |
---|
98 | self.nb_start = 0 |
---|
99 | self.nb_progress = 0 |
---|
100 | self.nb_stop = 0 |
---|
101 | |
---|
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.OnTimer, self.timer) |
---|
109 | self.Bind(wx.EVT_TIMER,self.OnTimer_stop, self.timer_stop) |
---|
110 | |
---|
111 | def get_msg_position(self): |
---|
112 | """ |
---|
113 | """ |
---|
114 | return self.msg_position |
---|
115 | |
---|
116 | def SetStatusText(self, text="", number=MSG_POSITION): |
---|
117 | """ |
---|
118 | """ |
---|
119 | wxStatusB.SetStatusText(self, text, MSG_POSITION) |
---|
120 | self.list_msg.append(text) |
---|
121 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
122 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
123 | try: |
---|
124 | if self.frame is not None and self.frame.IsShown(): |
---|
125 | event = MessageEvent() |
---|
126 | event.status = text |
---|
127 | wx.PostEvent(self.frame, event) |
---|
128 | except: |
---|
129 | return |
---|
130 | |
---|
131 | def PopStatusText(self, *args, **kwds): |
---|
132 | wxStatusB.PopStatusText(self, field=MSG_POSITION) |
---|
133 | |
---|
134 | def PushStatusText(self, *args, **kwds): |
---|
135 | wxStatusB.PushStatusText(self, field=MSG_POSITION,string=string) |
---|
136 | |
---|
137 | def enable_clear_gauge(self): |
---|
138 | """ |
---|
139 | """ |
---|
140 | flag = False |
---|
141 | if (self.nb_start <= self.nb_stop) or (self.nb_progress <= self.nb_stop): |
---|
142 | flag = True |
---|
143 | return flag |
---|
144 | |
---|
145 | def OnTimer_stop(self, evt): |
---|
146 | """Clear the progress bar |
---|
147 | @param evt: wx.EVT_TIMER |
---|
148 | |
---|
149 | """ |
---|
150 | count = 0 |
---|
151 | while(count <= 100): |
---|
152 | count += 1 |
---|
153 | self.timer_stop.Stop() |
---|
154 | self.clear_gauge(msg="") |
---|
155 | self.nb_progress = 0 |
---|
156 | self.nb_start = 0 |
---|
157 | self.nb_stop = 0 |
---|
158 | |
---|
159 | def OnTimer(self, evt): |
---|
160 | """Update the progress bar while the timer is running |
---|
161 | @param evt: wx.EVT_TIMER |
---|
162 | |
---|
163 | """ |
---|
164 | # Check stop flag that can be set from non main thread |
---|
165 | if self.timer.IsRunning(): |
---|
166 | self.gauge.Pulse() |
---|
167 | |
---|
168 | def clear_gauge(self, msg=""): |
---|
169 | """ |
---|
170 | Hide the gauge |
---|
171 | """ |
---|
172 | self.progress = 0 |
---|
173 | self.gauge.SetValue(0) |
---|
174 | self.gauge.Hide() |
---|
175 | |
---|
176 | def set_icon(self, event): |
---|
177 | """ |
---|
178 | display icons related to the type of message sent to the statusbar |
---|
179 | when available |
---|
180 | """ |
---|
181 | if not hasattr(event, "info"): |
---|
182 | return |
---|
183 | msg = event.info.lower() |
---|
184 | if msg == "warning": |
---|
185 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_TOOLBAR) |
---|
186 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
187 | if msg == "error": |
---|
188 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_TOOLBAR) |
---|
189 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
190 | if msg == "info": |
---|
191 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
192 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
193 | |
---|
194 | def set_message(self, event): |
---|
195 | """ |
---|
196 | display received message on the statusbar |
---|
197 | """ |
---|
198 | if hasattr(event, "status"): |
---|
199 | self.SetStatusText(str(event.status)) |
---|
200 | |
---|
201 | def set_gauge(self, event): |
---|
202 | """ |
---|
203 | change the state of the gauge according the state of the current job |
---|
204 | """ |
---|
205 | if not hasattr(event, "type"): |
---|
206 | return |
---|
207 | type = event.type |
---|
208 | self.gauge.Show(True) |
---|
209 | if type.lower()=="start": |
---|
210 | self.nb_start += 1 |
---|
211 | self.timer.Stop() |
---|
212 | self.progress += 10 |
---|
213 | self.gauge.SetValue(int(self.progress)) |
---|
214 | self.progress += 10 |
---|
215 | if self.progress < self.gauge.GetRange()-20: |
---|
216 | self.gauge.SetValue(int(self.progress)) |
---|
217 | if type.lower()=="progress": |
---|
218 | self.nb_progress += 1 |
---|
219 | self.timer.Start(100) |
---|
220 | self.gauge.Pulse() |
---|
221 | if type.lower()=="update": |
---|
222 | self.progress += 10 |
---|
223 | if self.progress < self.gauge.GetRange()-20: |
---|
224 | self.gauge.SetValue(int(self.progress)) |
---|
225 | if type.lower()=="stop": |
---|
226 | self.nb_stop += 1 |
---|
227 | self.gauge.Show(True) |
---|
228 | if self.enable_clear_gauge(): |
---|
229 | self.timer.Stop() |
---|
230 | self.progress = 0 |
---|
231 | self.gauge.SetValue(90) |
---|
232 | self.timer_stop.Start(3) |
---|
233 | |
---|
234 | def set_status(self, event): |
---|
235 | """ |
---|
236 | Update the status bar . |
---|
237 | @param type: type of message send. |
---|
238 | type must be in ["start","progress","update","stop"] |
---|
239 | @param msg: the message itself as string |
---|
240 | @param thread: if updatting using a thread status |
---|
241 | """ |
---|
242 | self.set_message(event=event) |
---|
243 | self.set_icon(event=event) |
---|
244 | self.set_gauge(event=event) |
---|
245 | |
---|
246 | def _onMonitor(self, event): |
---|
247 | """ |
---|
248 | """ |
---|
249 | self.frame = Console(parent=self) |
---|
250 | |
---|
251 | self.frame.set_multiple_messages(self.list_msg) |
---|
252 | self.frame.Show(True) |
---|
253 | |
---|
254 | |
---|
255 | if __name__ == "__main__": |
---|
256 | app = wx.PySimpleApp() |
---|
257 | frame= wx.Frame(None,wx.ID_ANY,'test frame') |
---|
258 | statusBar = StatusBar(frame, wx.ID_ANY) |
---|
259 | frame.SetStatusBar(statusBar) |
---|
260 | frame.Show(True) |
---|
261 | |
---|
262 | event = MessageEvent() |
---|
263 | event.type = "progress" |
---|
264 | event.status = "statusbar...." |
---|
265 | event.info = "error" |
---|
266 | statusBar.set_status(event=event) |
---|
267 | app.MainLoop() |
---|
268 | |
---|