[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]) |
---|
[db10f97] | 69 | |
---|
| 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) |
---|
| 77 | rect = self.GetFieldRect(GAUGE_POSITION) |
---|
| 78 | self.gauge.SetPosition((rect.x+5, rect.y-2)) |
---|
[dd66fbd] | 79 | self.gauge.Hide() |
---|
[db10f97] | 80 | |
---|
| 81 | #status bar icon |
---|
| 82 | self.bitmap_bt_warning = wx.BitmapButton(self, -1, size=(BUTTON_SIZE,-1), |
---|
| 83 | style=wx.NO_BORDER) |
---|
| 84 | rect = self.GetFieldRect(ICON_POSITION) |
---|
| 85 | self.bitmap_bt_warning.SetPosition((rect.x+5, rect.y-2)) |
---|
[0250d2f] | 86 | |
---|
| 87 | console_bmp = wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_TOOLBAR) |
---|
| 88 | self.bitmap_bt_console = wx.BitmapButton(self, -1, |
---|
| 89 | size=(BUTTON_SIZE-5, height-4)) |
---|
| 90 | self.bitmap_bt_console.SetBitmapLabel(console_bmp) |
---|
[f4fe424] | 91 | console_hint = "History of status bar messages" |
---|
| 92 | self.bitmap_bt_console.SetToolTipString(console_hint) |
---|
[0250d2f] | 93 | self.bitmap_bt_console.Bind(wx.EVT_BUTTON, self._onMonitor, |
---|
| 94 | id=self.bitmap_bt_console.GetId()) |
---|
| 95 | rect = self.GetFieldRect(CONSOLE_POSITION) |
---|
| 96 | self.bitmap_bt_console.SetPosition((rect.x+5, rect.y-2)) |
---|
[12aa9b5] | 97 | ## Current progress value of the bar |
---|
[04349fe] | 98 | self.nb_start = 0 |
---|
| 99 | self.nb_progress = 0 |
---|
| 100 | self.nb_stop = 0 |
---|
[0250d2f] | 101 | |
---|
| 102 | self.frame = None |
---|
| 103 | self.list_msg = [] |
---|
[12aa9b5] | 104 | self.progress = 0 |
---|
[db10f97] | 105 | self.timer = wx.Timer(self, -1) |
---|
| 106 | self.timer_stop = wx.Timer(self, -1) |
---|
| 107 | self.thread = None |
---|
[ef628a1] | 108 | self.Bind(wx.EVT_TIMER,self.OnTimer, self.timer) |
---|
[858f2ae5] | 109 | self.Bind(wx.EVT_TIMER,self.OnTimer_stop, self.timer_stop) |
---|
[12aa9b5] | 110 | |
---|
[db10f97] | 111 | def get_msg_position(self): |
---|
| 112 | """ |
---|
| 113 | """ |
---|
| 114 | return self.msg_position |
---|
| 115 | |
---|
| 116 | def SetStatusText(self, text="", number=MSG_POSITION): |
---|
| 117 | """ |
---|
| 118 | """ |
---|
| 119 | wxStatusB.SetStatusText(self, text, MSG_POSITION) |
---|
[0250d2f] | 120 | self.list_msg.append(text) |
---|
[04349fe] | 121 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
| 122 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
[0250d2f] | 123 | try: |
---|
| 124 | if self.frame is not None and self.frame.IsShown(): |
---|
| 125 | event = MessageEvent() |
---|
| 126 | event.status = text |
---|
| 127 | wx.PostEvent(self.frame, event) |
---|
| 128 | except: |
---|
| 129 | return |
---|
| 130 | |
---|
[db10f97] | 131 | def PopStatusText(self, *args, **kwds): |
---|
| 132 | wxStatusB.PopStatusText(self, field=MSG_POSITION) |
---|
| 133 | |
---|
| 134 | def PushStatusText(self, *args, **kwds): |
---|
| 135 | wxStatusB.PushStatusText(self, field=MSG_POSITION,string=string) |
---|
| 136 | |
---|
[04349fe] | 137 | def enable_clear_gauge(self): |
---|
| 138 | """ |
---|
| 139 | """ |
---|
| 140 | flag = False |
---|
| 141 | if (self.nb_start <= self.nb_stop) or (self.nb_progress <= self.nb_stop): |
---|
| 142 | flag = True |
---|
| 143 | return flag |
---|
| 144 | |
---|
[858f2ae5] | 145 | def OnTimer_stop(self, evt): |
---|
[12aa9b5] | 146 | """Clear the progress bar |
---|
[858f2ae5] | 147 | @param evt: wx.EVT_TIMER |
---|
| 148 | |
---|
| 149 | """ |
---|
[db10f97] | 150 | count = 0 |
---|
[e2289b4] | 151 | while(count <= 100): |
---|
[db10f97] | 152 | count += 1 |
---|
| 153 | self.timer_stop.Stop() |
---|
[b3644f3] | 154 | self.clear_gauge(msg="") |
---|
[04349fe] | 155 | self.nb_progress = 0 |
---|
| 156 | self.nb_start = 0 |
---|
| 157 | self.nb_stop = 0 |
---|
[db10f97] | 158 | |
---|
[dd66fbd] | 159 | def OnTimer(self, evt): |
---|
| 160 | """Update the progress bar while the timer is running |
---|
| 161 | @param evt: wx.EVT_TIMER |
---|
| 162 | |
---|
| 163 | """ |
---|
| 164 | # Check stop flag that can be set from non main thread |
---|
[858f2ae5] | 165 | if self.timer.IsRunning(): |
---|
| 166 | self.gauge.Pulse() |
---|
[04349fe] | 167 | |
---|
[dd66fbd] | 168 | def clear_gauge(self, msg=""): |
---|
[12aa9b5] | 169 | """ |
---|
| 170 | Hide the gauge |
---|
| 171 | """ |
---|
[db10f97] | 172 | self.progress = 0 |
---|
[dd66fbd] | 173 | self.gauge.SetValue(0) |
---|
| 174 | self.gauge.Hide() |
---|
| 175 | |
---|
[db10f97] | 176 | def set_icon(self, event): |
---|
| 177 | """ |
---|
| 178 | display icons related to the type of message sent to the statusbar |
---|
| 179 | when available |
---|
| 180 | """ |
---|
| 181 | if not hasattr(event, "info"): |
---|
| 182 | return |
---|
| 183 | msg = event.info.lower() |
---|
| 184 | if msg == "warning": |
---|
| 185 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_TOOLBAR) |
---|
| 186 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 187 | if msg == "error": |
---|
| 188 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_TOOLBAR) |
---|
| 189 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 190 | if msg == "info": |
---|
| 191 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
| 192 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
| 193 | |
---|
| 194 | def set_message(self, event): |
---|
| 195 | """ |
---|
| 196 | display received message on the statusbar |
---|
| 197 | """ |
---|
| 198 | if hasattr(event, "status"): |
---|
| 199 | self.SetStatusText(str(event.status)) |
---|
| 200 | |
---|
| 201 | def set_gauge(self, event): |
---|
| 202 | """ |
---|
| 203 | change the state of the gauge according the state of the current job |
---|
| 204 | """ |
---|
| 205 | if not hasattr(event, "type"): |
---|
| 206 | return |
---|
| 207 | type = event.type |
---|
| 208 | self.gauge.Show(True) |
---|
| 209 | if type.lower()=="start": |
---|
[04349fe] | 210 | self.nb_start += 1 |
---|
[db10f97] | 211 | self.timer.Stop() |
---|
| 212 | self.progress += 10 |
---|
| 213 | self.gauge.SetValue(int(self.progress)) |
---|
| 214 | self.progress += 10 |
---|
| 215 | if self.progress < self.gauge.GetRange()-20: |
---|
| 216 | self.gauge.SetValue(int(self.progress)) |
---|
| 217 | if type.lower()=="progress": |
---|
[04349fe] | 218 | self.nb_progress += 1 |
---|
[db10f97] | 219 | self.timer.Start(100) |
---|
| 220 | self.gauge.Pulse() |
---|
| 221 | if type.lower()=="update": |
---|
| 222 | self.progress += 10 |
---|
| 223 | if self.progress < self.gauge.GetRange()-20: |
---|
| 224 | self.gauge.SetValue(int(self.progress)) |
---|
| 225 | if type.lower()=="stop": |
---|
[04349fe] | 226 | self.nb_stop += 1 |
---|
[db10f97] | 227 | self.gauge.Show(True) |
---|
[04349fe] | 228 | if self.enable_clear_gauge(): |
---|
| 229 | self.timer.Stop() |
---|
| 230 | self.progress = 0 |
---|
| 231 | self.gauge.SetValue(90) |
---|
| 232 | self.timer_stop.Start(3) |
---|
[db10f97] | 233 | |
---|
| 234 | def set_status(self, event): |
---|
[12aa9b5] | 235 | """ |
---|
| 236 | Update the status bar . |
---|
| 237 | @param type: type of message send. |
---|
| 238 | type must be in ["start","progress","update","stop"] |
---|
| 239 | @param msg: the message itself as string |
---|
| 240 | @param thread: if updatting using a thread status |
---|
| 241 | """ |
---|
[db10f97] | 242 | self.set_message(event=event) |
---|
[5318c4c] | 243 | self.set_icon(event=event) |
---|
[db10f97] | 244 | self.set_gauge(event=event) |
---|
[0250d2f] | 245 | |
---|
| 246 | def _onMonitor(self, event): |
---|
| 247 | """ |
---|
| 248 | """ |
---|
| 249 | self.frame = Console(parent=self) |
---|
[a7d9e9c] | 250 | |
---|
[0250d2f] | 251 | self.frame.set_multiple_messages(self.list_msg) |
---|
| 252 | self.frame.Show(True) |
---|
| 253 | |
---|
[dd66fbd] | 254 | |
---|
| 255 | if __name__ == "__main__": |
---|
| 256 | app = wx.PySimpleApp() |
---|
| 257 | frame= wx.Frame(None,wx.ID_ANY,'test frame') |
---|
[db10f97] | 258 | statusBar = StatusBar(frame, wx.ID_ANY) |
---|
[dd66fbd] | 259 | frame.SetStatusBar(statusBar) |
---|
| 260 | frame.Show(True) |
---|
[0250d2f] | 261 | |
---|
| 262 | event = MessageEvent() |
---|
[db10f97] | 263 | event.type = "progress" |
---|
| 264 | event.status = "statusbar...." |
---|
[5318c4c] | 265 | event.info = "error" |
---|
[db10f97] | 266 | statusBar.set_status(event=event) |
---|
[dd66fbd] | 267 | app.MainLoop() |
---|
| 268 | |
---|