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