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