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