[010c251] | 1 | import wx |
---|
| 2 | from wx import StatusBar as wxStatusB |
---|
| 3 | from wx.lib import newevent |
---|
[a195ac9] | 4 | import time |
---|
[010c251] | 5 | #numner of fields of the status bar |
---|
| 6 | NB_FIELDS = 4 |
---|
| 7 | #position of the status bar's fields |
---|
| 8 | ICON_POSITION = 0 |
---|
| 9 | MSG_POSITION = 1 |
---|
| 10 | GAUGE_POSITION = 2 |
---|
| 11 | CONSOLE_POSITION = 3 |
---|
| 12 | BUTTON_SIZE = 40 |
---|
| 13 | |
---|
| 14 | CONSOLE_WIDTH = 340 |
---|
| 15 | CONSOLE_HEIGHT = 240 |
---|
| 16 | |
---|
| 17 | class ConsolePanel(wx.Panel): |
---|
| 18 | """ |
---|
| 19 | """ |
---|
| 20 | def __init__(self, parent, *args, **kwargs): |
---|
| 21 | """ |
---|
| 22 | """ |
---|
| 23 | wx.Panel.__init__(self, parent=parent, *args, **kwargs) |
---|
| 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) |
---|
| 29 | self.msg_txt.SetEditable(False) |
---|
| 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 | |
---|
| 34 | def set_message(self, status=""): |
---|
| 35 | """ |
---|
| 36 | """ |
---|
| 37 | msg = status + "\n" |
---|
| 38 | self.msg_txt.AppendText(str(msg)) |
---|
| 39 | |
---|
| 40 | class Console(wx.Frame): |
---|
| 41 | """ |
---|
| 42 | """ |
---|
| 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) |
---|
[bfa73ca] | 49 | wx.EVT_CLOSE(self, self.Close) |
---|
[783940c] | 50 | |
---|
[010c251] | 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 | |
---|
[bfa73ca] | 59 | def set_message(self, message): |
---|
| 60 | """ |
---|
| 61 | """ |
---|
| 62 | self.panel.set_message(str(message)) |
---|
| 63 | |
---|
| 64 | def Close(self, event): |
---|
[010c251] | 65 | """ |
---|
| 66 | """ |
---|
[bfa73ca] | 67 | self.Hide() |
---|
[010c251] | 68 | |
---|
| 69 | class StatusBar(wxStatusB): |
---|
| 70 | """ |
---|
| 71 | """ |
---|
| 72 | def __init__(self, parent, *args, **kargs): |
---|
| 73 | wxStatusB.__init__(self, parent, *args, **kargs) |
---|
| 74 | """ |
---|
| 75 | Implement statusbar functionalities |
---|
| 76 | """ |
---|
| 77 | self.parent = parent |
---|
| 78 | self.parent.SetStatusBarPane(MSG_POSITION) |
---|
| 79 | #Layout of status bar |
---|
| 80 | self.SetFieldsCount(NB_FIELDS) |
---|
| 81 | self.SetStatusWidths([BUTTON_SIZE, -2, -1, BUTTON_SIZE]) |
---|
| 82 | #display default message |
---|
| 83 | self.msg_position = MSG_POSITION |
---|
| 84 | #save the position of the gauge |
---|
| 85 | width, height = self.GetSize() |
---|
| 86 | self.gauge = wx.Gauge(self, size=(width/10, height-3), |
---|
| 87 | style=wx.GA_HORIZONTAL) |
---|
| 88 | self.gauge.Hide() |
---|
| 89 | #status bar icon |
---|
| 90 | self.bitmap_bt_warning = wx.BitmapButton(self, -1, |
---|
| 91 | size=(BUTTON_SIZE,-1), |
---|
| 92 | style=wx.NO_BORDER) |
---|
[5f15492] | 93 | console_bmp = wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_TOOLBAR, |
---|
| 94 | size = (16,16)) |
---|
[010c251] | 95 | self.bitmap_bt_console = wx.BitmapButton(self, -1, |
---|
| 96 | size=(BUTTON_SIZE-5, height-4)) |
---|
| 97 | self.bitmap_bt_console.SetBitmapLabel(console_bmp) |
---|
| 98 | console_hint = "History of status bar messages" |
---|
| 99 | self.bitmap_bt_console.SetToolTipString(console_hint) |
---|
| 100 | self.bitmap_bt_console.Bind(wx.EVT_BUTTON, self._onMonitor, |
---|
| 101 | id=self.bitmap_bt_console.GetId()) |
---|
[bfa73ca] | 102 | |
---|
[010c251] | 103 | self.reposition() |
---|
| 104 | ## Current progress value of the bar |
---|
| 105 | self.nb_start = 0 |
---|
| 106 | self.nb_progress = 0 |
---|
| 107 | self.nb_stop = 0 |
---|
| 108 | self.frame = None |
---|
| 109 | self.list_msg = [] |
---|
[bfa73ca] | 110 | self.frame = Console(parent=self) |
---|
| 111 | self.frame.set_multiple_messages(self.list_msg) |
---|
| 112 | self.frame.Hide() |
---|
[010c251] | 113 | self.progress = 0 |
---|
| 114 | self.timer = wx.Timer(self, -1) |
---|
| 115 | self.timer_stop = wx.Timer(self, -1) |
---|
| 116 | self.thread = None |
---|
| 117 | self.Bind(wx.EVT_TIMER, self._on_time, self.timer) |
---|
| 118 | self.Bind(wx.EVT_TIMER, self._on_time_stop, self.timer_stop) |
---|
| 119 | self.Bind(wx.EVT_SIZE, self.OnSize) |
---|
| 120 | self.Bind(wx.EVT_IDLE, self.OnIdle) |
---|
| 121 | |
---|
| 122 | def reposition(self): |
---|
| 123 | """ |
---|
| 124 | """ |
---|
| 125 | rect = self.GetFieldRect(GAUGE_POSITION) |
---|
| 126 | self.gauge.SetPosition((rect.x + 5, rect.y - 2)) |
---|
| 127 | rect = self.GetFieldRect(ICON_POSITION) |
---|
| 128 | self.bitmap_bt_warning.SetPosition((rect.x + 5, rect.y - 2)) |
---|
| 129 | rect = self.GetFieldRect(CONSOLE_POSITION) |
---|
| 130 | self.bitmap_bt_console.SetPosition((rect.x - 5, rect.y - 2)) |
---|
| 131 | self.sizeChanged = False |
---|
| 132 | |
---|
| 133 | def OnIdle(self, event): |
---|
| 134 | """ |
---|
| 135 | """ |
---|
| 136 | if self.sizeChanged: |
---|
| 137 | self.reposition() |
---|
| 138 | |
---|
| 139 | def OnSize(self, evt): |
---|
| 140 | """ |
---|
| 141 | """ |
---|
| 142 | self.reposition() |
---|
| 143 | self.sizeChanged = True |
---|
| 144 | |
---|
| 145 | def get_msg_position(self): |
---|
| 146 | """ |
---|
| 147 | """ |
---|
| 148 | return self.msg_position |
---|
| 149 | |
---|
| 150 | def SetStatusText(self, text="", number=MSG_POSITION): |
---|
| 151 | """ |
---|
| 152 | """ |
---|
| 153 | wxStatusB.SetStatusText(self, text, number) |
---|
| 154 | self.list_msg.append(text) |
---|
| 155 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
| 156 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
[bfa73ca] | 157 | |
---|
| 158 | if self.frame is not None : |
---|
| 159 | self.frame.set_message(text) |
---|
| 160 | |
---|
[010c251] | 161 | def PopStatusText(self, *args, **kwds): |
---|
| 162 | """ |
---|
| 163 | Override status bar |
---|
| 164 | """ |
---|
| 165 | wxStatusB.PopStatusText(self, field=MSG_POSITION) |
---|
| 166 | |
---|
| 167 | def PushStatusText(self, *args, **kwds): |
---|
| 168 | """ |
---|
| 169 | """ |
---|
| 170 | wxStatusB.PushStatusText(self, field=MSG_POSITION, string=string) |
---|
| 171 | |
---|
| 172 | def enable_clear_gauge(self): |
---|
| 173 | """ |
---|
| 174 | clear the progress bar |
---|
| 175 | """ |
---|
| 176 | flag = False |
---|
| 177 | if (self.nb_start <= self.nb_stop) or \ |
---|
| 178 | (self.nb_progress <= self.nb_stop): |
---|
| 179 | flag = True |
---|
| 180 | return flag |
---|
| 181 | |
---|
| 182 | def _on_time_stop(self, evt): |
---|
| 183 | """ |
---|
| 184 | Clear the progress bar |
---|
| 185 | |
---|
| 186 | :param evt: wx.EVT_TIMER |
---|
| 187 | |
---|
| 188 | """ |
---|
| 189 | count = 0 |
---|
| 190 | while(count <= 100): |
---|
| 191 | count += 1 |
---|
| 192 | self.timer_stop.Stop() |
---|
| 193 | self.clear_gauge(msg="") |
---|
| 194 | self.nb_progress = 0 |
---|
| 195 | self.nb_start = 0 |
---|
| 196 | self.nb_stop = 0 |
---|
| 197 | |
---|
| 198 | def _on_time(self, evt): |
---|
| 199 | """ |
---|
| 200 | Update the progress bar while the timer is running |
---|
| 201 | |
---|
| 202 | :param evt: wx.EVT_TIMER |
---|
| 203 | |
---|
| 204 | """ |
---|
| 205 | # Check stop flag that can be set from non main thread |
---|
| 206 | if self.timer.IsRunning(): |
---|
| 207 | self.gauge.Pulse() |
---|
| 208 | |
---|
| 209 | def clear_gauge(self, msg=""): |
---|
| 210 | """ |
---|
| 211 | Hide the gauge |
---|
| 212 | """ |
---|
| 213 | self.progress = 0 |
---|
| 214 | self.gauge.SetValue(0) |
---|
| 215 | self.gauge.Hide() |
---|
| 216 | |
---|
| 217 | def set_icon(self, event): |
---|
| 218 | """ |
---|
| 219 | display icons related to the type of message sent to the statusbar |
---|
| 220 | when available |
---|
| 221 | """ |
---|
| 222 | if not hasattr(event, "info"): |
---|
| 223 | return |
---|
| 224 | msg = event.info.lower() |
---|
| 225 | if msg == "warning": |
---|
| 226 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_TOOLBAR) |
---|
| 227 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 228 | if msg == "error": |
---|
| 229 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_TOOLBAR) |
---|
| 230 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 231 | if msg == "info": |
---|
| 232 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, |
---|
| 233 | wx.ART_TOOLBAR) |
---|
| 234 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 235 | |
---|
| 236 | def set_message(self, event): |
---|
| 237 | """ |
---|
| 238 | display received message on the statusbar |
---|
| 239 | """ |
---|
| 240 | if hasattr(event, "status"): |
---|
| 241 | self.SetStatusText(str(event.status)) |
---|
[bfa73ca] | 242 | |
---|
| 243 | |
---|
[010c251] | 244 | def set_gauge(self, event): |
---|
| 245 | """ |
---|
| 246 | change the state of the gauge according the state of the current job |
---|
| 247 | """ |
---|
| 248 | if not hasattr(event, "type"): |
---|
| 249 | return |
---|
| 250 | type = event.type |
---|
| 251 | self.gauge.Show(True) |
---|
| 252 | if type.lower() == "start": |
---|
| 253 | self.nb_start += 1 |
---|
| 254 | #self.timer.Stop() |
---|
| 255 | self.progress += 10 |
---|
| 256 | self.gauge.SetValue(int(self.progress)) |
---|
| 257 | self.progress += 10 |
---|
| 258 | if self.progress < self.gauge.GetRange() - 20: |
---|
| 259 | self.gauge.SetValue(int(self.progress)) |
---|
| 260 | if type.lower() == "progress": |
---|
| 261 | self.nb_progress += 1 |
---|
| 262 | self.timer.Start(1) |
---|
| 263 | self.gauge.Pulse() |
---|
| 264 | if type.lower() == "update": |
---|
| 265 | self.progress += 10 |
---|
| 266 | if self.progress < self.gauge.GetRange()- 20: |
---|
| 267 | self.gauge.SetValue(int(self.progress)) |
---|
| 268 | if type.lower() == "stop": |
---|
| 269 | self.nb_stop += 1 |
---|
| 270 | self.gauge.Show(True) |
---|
| 271 | if self.enable_clear_gauge(): |
---|
| 272 | self.timer.Stop() |
---|
| 273 | self.progress = 0 |
---|
| 274 | self.gauge.SetValue(90) |
---|
| 275 | self.timer_stop.Start(3) |
---|
| 276 | |
---|
| 277 | def set_status(self, event): |
---|
| 278 | """ |
---|
| 279 | Update the status bar . |
---|
| 280 | |
---|
| 281 | :param type: type of message send. |
---|
| 282 | type must be in ["start","progress","update","stop"] |
---|
| 283 | :param msg: the message itself as string |
---|
| 284 | :param thread: if updatting using a thread status |
---|
| 285 | |
---|
| 286 | """ |
---|
| 287 | self.set_message(event=event) |
---|
| 288 | self.set_icon(event=event) |
---|
| 289 | self.set_gauge(event=event) |
---|
| 290 | |
---|
| 291 | def _onMonitor(self, event): |
---|
| 292 | """ |
---|
| 293 | Pop up a frame with messages sent to the status bar |
---|
| 294 | """ |
---|
| 295 | self.frame.Show(True) |
---|
| 296 | |
---|
| 297 | |
---|
[7a955a9] | 298 | class SPageStatusbar(wxStatusB): |
---|
| 299 | def __init__(self, parent, timeout=None, *args, **kwds): |
---|
| 300 | wxStatusB.__init__(self, parent, *args, **kwds) |
---|
| 301 | self.SetFieldsCount(1) |
---|
| 302 | self.timeout = timeout |
---|
| 303 | self.gauge = wx.Gauge(self,style=wx.GA_HORIZONTAL, size=parent.GetSize()) |
---|
| 304 | rect = self.GetFieldRect(0) |
---|
| 305 | self.gauge.SetPosition((rect.x , rect.y )) |
---|
| 306 | if self.timeout is not None: |
---|
| 307 | self.gauge.SetRange(int(self.timeout)) |
---|
| 308 | self.timer = wx.Timer(self, -1) |
---|
| 309 | self.Bind(wx.EVT_TIMER, self._on_time, self.timer) |
---|
| 310 | self.timer.Start(1) |
---|
| 311 | self.pos = 0 |
---|
| 312 | |
---|
| 313 | def _on_time(self, evt): |
---|
| 314 | """ |
---|
| 315 | Update the progress bar while the timer is running |
---|
| 316 | |
---|
| 317 | :param evt: wx.EVT_TIMER |
---|
| 318 | |
---|
| 319 | """ |
---|
| 320 | # Check stop flag that can be set from non main thread |
---|
| 321 | if self.timeout is None and self.timer.IsRunning(): |
---|
| 322 | self.gauge.Pulse() |
---|
| 323 | |
---|
| 324 | |
---|
[010c251] | 325 | if __name__ == "__main__": |
---|
| 326 | app = wx.PySimpleApp() |
---|
| 327 | frame = wx.Frame(None, wx.ID_ANY, 'test frame') |
---|
[7a955a9] | 328 | #statusBar = StatusBar(frame, wx.ID_ANY) |
---|
| 329 | statusBar = SPageStatusbar(frame) |
---|
[010c251] | 330 | frame.SetStatusBar(statusBar) |
---|
| 331 | frame.Show(True) |
---|
[7a955a9] | 332 | #event = MessageEvent() |
---|
| 333 | #event.type = "progress" |
---|
| 334 | #event.status = "statusbar...." |
---|
| 335 | #event.info = "error" |
---|
| 336 | #statusBar.set_status(event=event) |
---|
[010c251] | 337 | app.MainLoop() |
---|
| 338 | |
---|