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