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