[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) |
---|
| 93 | console_bmp = wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_TOOLBAR) |
---|
| 94 | self.bitmap_bt_console = wx.BitmapButton(self, -1, |
---|
| 95 | size=(BUTTON_SIZE-5, height-4)) |
---|
| 96 | self.bitmap_bt_console.SetBitmapLabel(console_bmp) |
---|
| 97 | console_hint = "History of status bar messages" |
---|
| 98 | self.bitmap_bt_console.SetToolTipString(console_hint) |
---|
| 99 | self.bitmap_bt_console.Bind(wx.EVT_BUTTON, self._onMonitor, |
---|
| 100 | id=self.bitmap_bt_console.GetId()) |
---|
[bfa73ca] | 101 | |
---|
[010c251] | 102 | self.reposition() |
---|
| 103 | ## Current progress value of the bar |
---|
| 104 | self.nb_start = 0 |
---|
| 105 | self.nb_progress = 0 |
---|
| 106 | self.nb_stop = 0 |
---|
| 107 | self.frame = None |
---|
| 108 | self.list_msg = [] |
---|
[bfa73ca] | 109 | self.frame = Console(parent=self) |
---|
| 110 | self.frame.set_multiple_messages(self.list_msg) |
---|
| 111 | self.frame.Hide() |
---|
[010c251] | 112 | self.progress = 0 |
---|
| 113 | self.timer = wx.Timer(self, -1) |
---|
| 114 | self.timer_stop = wx.Timer(self, -1) |
---|
| 115 | self.thread = None |
---|
| 116 | self.Bind(wx.EVT_TIMER, self._on_time, self.timer) |
---|
| 117 | self.Bind(wx.EVT_TIMER, self._on_time_stop, self.timer_stop) |
---|
| 118 | self.Bind(wx.EVT_SIZE, self.OnSize) |
---|
| 119 | self.Bind(wx.EVT_IDLE, self.OnIdle) |
---|
| 120 | |
---|
| 121 | def reposition(self): |
---|
| 122 | """ |
---|
| 123 | """ |
---|
| 124 | rect = self.GetFieldRect(GAUGE_POSITION) |
---|
| 125 | self.gauge.SetPosition((rect.x + 5, rect.y - 2)) |
---|
| 126 | rect = self.GetFieldRect(ICON_POSITION) |
---|
| 127 | self.bitmap_bt_warning.SetPosition((rect.x + 5, rect.y - 2)) |
---|
| 128 | rect = self.GetFieldRect(CONSOLE_POSITION) |
---|
| 129 | self.bitmap_bt_console.SetPosition((rect.x - 5, rect.y - 2)) |
---|
| 130 | self.sizeChanged = False |
---|
| 131 | |
---|
| 132 | def OnIdle(self, event): |
---|
| 133 | """ |
---|
| 134 | """ |
---|
| 135 | if self.sizeChanged: |
---|
| 136 | self.reposition() |
---|
| 137 | |
---|
| 138 | def OnSize(self, evt): |
---|
| 139 | """ |
---|
| 140 | """ |
---|
| 141 | self.reposition() |
---|
| 142 | self.sizeChanged = True |
---|
| 143 | |
---|
| 144 | def get_msg_position(self): |
---|
| 145 | """ |
---|
| 146 | """ |
---|
| 147 | return self.msg_position |
---|
| 148 | |
---|
| 149 | def SetStatusText(self, text="", number=MSG_POSITION): |
---|
| 150 | """ |
---|
| 151 | """ |
---|
| 152 | wxStatusB.SetStatusText(self, text, number) |
---|
| 153 | self.list_msg.append(text) |
---|
| 154 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
| 155 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
[bfa73ca] | 156 | |
---|
| 157 | if self.frame is not None : |
---|
| 158 | self.frame.set_message(text) |
---|
| 159 | |
---|
[010c251] | 160 | def PopStatusText(self, *args, **kwds): |
---|
| 161 | """ |
---|
| 162 | Override status bar |
---|
| 163 | """ |
---|
| 164 | wxStatusB.PopStatusText(self, field=MSG_POSITION) |
---|
| 165 | |
---|
| 166 | def PushStatusText(self, *args, **kwds): |
---|
| 167 | """ |
---|
| 168 | """ |
---|
| 169 | wxStatusB.PushStatusText(self, field=MSG_POSITION, string=string) |
---|
| 170 | |
---|
| 171 | def enable_clear_gauge(self): |
---|
| 172 | """ |
---|
| 173 | clear the progress bar |
---|
| 174 | """ |
---|
| 175 | flag = False |
---|
| 176 | if (self.nb_start <= self.nb_stop) or \ |
---|
| 177 | (self.nb_progress <= self.nb_stop): |
---|
| 178 | flag = True |
---|
| 179 | return flag |
---|
| 180 | |
---|
| 181 | def _on_time_stop(self, evt): |
---|
| 182 | """ |
---|
| 183 | Clear the progress bar |
---|
| 184 | |
---|
| 185 | :param evt: wx.EVT_TIMER |
---|
| 186 | |
---|
| 187 | """ |
---|
| 188 | count = 0 |
---|
| 189 | while(count <= 100): |
---|
| 190 | count += 1 |
---|
| 191 | self.timer_stop.Stop() |
---|
| 192 | self.clear_gauge(msg="") |
---|
| 193 | self.nb_progress = 0 |
---|
| 194 | self.nb_start = 0 |
---|
| 195 | self.nb_stop = 0 |
---|
| 196 | |
---|
| 197 | def _on_time(self, evt): |
---|
| 198 | """ |
---|
| 199 | Update the progress bar while the timer is running |
---|
| 200 | |
---|
| 201 | :param evt: wx.EVT_TIMER |
---|
| 202 | |
---|
| 203 | """ |
---|
| 204 | # Check stop flag that can be set from non main thread |
---|
| 205 | if self.timer.IsRunning(): |
---|
| 206 | self.gauge.Pulse() |
---|
| 207 | |
---|
| 208 | def clear_gauge(self, msg=""): |
---|
| 209 | """ |
---|
| 210 | Hide the gauge |
---|
| 211 | """ |
---|
| 212 | self.progress = 0 |
---|
| 213 | self.gauge.SetValue(0) |
---|
| 214 | self.gauge.Hide() |
---|
| 215 | |
---|
| 216 | def set_icon(self, event): |
---|
| 217 | """ |
---|
| 218 | display icons related to the type of message sent to the statusbar |
---|
| 219 | when available |
---|
| 220 | """ |
---|
| 221 | if not hasattr(event, "info"): |
---|
| 222 | return |
---|
| 223 | msg = event.info.lower() |
---|
| 224 | if msg == "warning": |
---|
| 225 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_TOOLBAR) |
---|
| 226 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 227 | if msg == "error": |
---|
| 228 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_TOOLBAR) |
---|
| 229 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 230 | if msg == "info": |
---|
| 231 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, |
---|
| 232 | wx.ART_TOOLBAR) |
---|
| 233 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 234 | |
---|
| 235 | def set_message(self, event): |
---|
| 236 | """ |
---|
| 237 | display received message on the statusbar |
---|
| 238 | """ |
---|
| 239 | if hasattr(event, "status"): |
---|
| 240 | self.SetStatusText(str(event.status)) |
---|
[bfa73ca] | 241 | |
---|
| 242 | |
---|
[010c251] | 243 | def set_gauge(self, event): |
---|
| 244 | """ |
---|
| 245 | change the state of the gauge according the state of the current job |
---|
| 246 | """ |
---|
| 247 | if not hasattr(event, "type"): |
---|
| 248 | return |
---|
| 249 | type = event.type |
---|
| 250 | self.gauge.Show(True) |
---|
| 251 | if type.lower() == "start": |
---|
| 252 | self.nb_start += 1 |
---|
| 253 | #self.timer.Stop() |
---|
| 254 | self.progress += 10 |
---|
| 255 | self.gauge.SetValue(int(self.progress)) |
---|
| 256 | self.progress += 10 |
---|
| 257 | if self.progress < self.gauge.GetRange() - 20: |
---|
| 258 | self.gauge.SetValue(int(self.progress)) |
---|
| 259 | if type.lower() == "progress": |
---|
| 260 | self.nb_progress += 1 |
---|
| 261 | self.timer.Start(1) |
---|
| 262 | self.gauge.Pulse() |
---|
| 263 | if type.lower() == "update": |
---|
| 264 | self.progress += 10 |
---|
| 265 | if self.progress < self.gauge.GetRange()- 20: |
---|
| 266 | self.gauge.SetValue(int(self.progress)) |
---|
| 267 | if type.lower() == "stop": |
---|
| 268 | self.nb_stop += 1 |
---|
| 269 | self.gauge.Show(True) |
---|
| 270 | if self.enable_clear_gauge(): |
---|
| 271 | self.timer.Stop() |
---|
| 272 | self.progress = 0 |
---|
| 273 | self.gauge.SetValue(90) |
---|
| 274 | self.timer_stop.Start(3) |
---|
| 275 | |
---|
| 276 | def set_status(self, event): |
---|
| 277 | """ |
---|
| 278 | Update the status bar . |
---|
| 279 | |
---|
| 280 | :param type: type of message send. |
---|
| 281 | type must be in ["start","progress","update","stop"] |
---|
| 282 | :param msg: the message itself as string |
---|
| 283 | :param thread: if updatting using a thread status |
---|
| 284 | |
---|
| 285 | """ |
---|
| 286 | self.set_message(event=event) |
---|
| 287 | self.set_icon(event=event) |
---|
| 288 | self.set_gauge(event=event) |
---|
| 289 | |
---|
| 290 | def _onMonitor(self, event): |
---|
| 291 | """ |
---|
| 292 | Pop up a frame with messages sent to the status bar |
---|
| 293 | """ |
---|
| 294 | self.frame.Show(True) |
---|
| 295 | |
---|
| 296 | |
---|
[7a955a9] | 297 | class SPageStatusbar(wxStatusB): |
---|
| 298 | def __init__(self, parent, timeout=None, *args, **kwds): |
---|
| 299 | wxStatusB.__init__(self, parent, *args, **kwds) |
---|
| 300 | self.SetFieldsCount(1) |
---|
| 301 | self.timeout = timeout |
---|
| 302 | self.gauge = wx.Gauge(self,style=wx.GA_HORIZONTAL, size=parent.GetSize()) |
---|
| 303 | rect = self.GetFieldRect(0) |
---|
| 304 | self.gauge.SetPosition((rect.x , rect.y )) |
---|
| 305 | if self.timeout is not None: |
---|
| 306 | self.gauge.SetRange(int(self.timeout)) |
---|
| 307 | self.timer = wx.Timer(self, -1) |
---|
| 308 | self.Bind(wx.EVT_TIMER, self._on_time, self.timer) |
---|
| 309 | self.timer.Start(1) |
---|
| 310 | self.pos = 0 |
---|
| 311 | |
---|
| 312 | def _on_time(self, evt): |
---|
| 313 | """ |
---|
| 314 | Update the progress bar while the timer is running |
---|
| 315 | |
---|
| 316 | :param evt: wx.EVT_TIMER |
---|
| 317 | |
---|
| 318 | """ |
---|
| 319 | # Check stop flag that can be set from non main thread |
---|
| 320 | if self.timeout is None and self.timer.IsRunning(): |
---|
| 321 | self.gauge.Pulse() |
---|
[a195ac9] | 322 | time.sleep(0.05) |
---|
[7a955a9] | 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 | |
---|