1 | import wx |
---|
2 | from wx import StatusBar as wxStatusB |
---|
3 | import wx.lib |
---|
4 | from wx.lib import newevent |
---|
5 | #numner of fields of the status bar |
---|
6 | NB_FIELDS = 3 |
---|
7 | #position of the status bar's fields |
---|
8 | ICON_POSITION = 0 |
---|
9 | MSG_POSITION = 1 |
---|
10 | GAUGE_POSITION = 2 |
---|
11 | BUTTON_SIZE = 40 |
---|
12 | class StatusBar(wxStatusB): |
---|
13 | def __init__(self, parent, *args, **kargs): |
---|
14 | wxStatusB.__init__(self, parent, *args, **kargs) |
---|
15 | """ |
---|
16 | Implement statusbar functionalities |
---|
17 | """ |
---|
18 | self.parent= parent |
---|
19 | self.parent.SetStatusBarPane(MSG_POSITION) |
---|
20 | |
---|
21 | #Layout of status bar |
---|
22 | self.SetFieldsCount(NB_FIELDS) |
---|
23 | self.SetStatusWidths([BUTTON_SIZE, -2, -1]) |
---|
24 | |
---|
25 | #display default message |
---|
26 | self.msg_position = MSG_POSITION |
---|
27 | |
---|
28 | #save the position of the gauge |
---|
29 | width, height = self.GetSize() |
---|
30 | self.gauge = wx.Gauge(self, size=(width/10,height-3), |
---|
31 | style= wx.GA_HORIZONTAL) |
---|
32 | rect = self.GetFieldRect(GAUGE_POSITION) |
---|
33 | self.gauge.SetPosition((rect.x+5, rect.y-2)) |
---|
34 | self.gauge.Hide() |
---|
35 | |
---|
36 | #status bar icon |
---|
37 | self.bitmap_bt_warning = wx.BitmapButton(self, -1, size=(BUTTON_SIZE,-1), |
---|
38 | style=wx.NO_BORDER) |
---|
39 | rect = self.GetFieldRect(ICON_POSITION) |
---|
40 | self.bitmap_bt_warning.SetPosition((rect.x+5, rect.y-2)) |
---|
41 | |
---|
42 | ## Current progress value of the bar |
---|
43 | self.nb_start = 0 |
---|
44 | self.nb_progress = 0 |
---|
45 | self.nb_stop = 0 |
---|
46 | self.progress = 0 |
---|
47 | self.timer = wx.Timer(self, -1) |
---|
48 | self.timer_stop = wx.Timer(self, -1) |
---|
49 | self.thread = None |
---|
50 | self.Bind(wx.EVT_TIMER,self.OnTimer, self.timer) |
---|
51 | self.Bind(wx.EVT_TIMER,self.OnTimer_stop, self.timer_stop) |
---|
52 | |
---|
53 | def get_msg_position(self): |
---|
54 | """ |
---|
55 | """ |
---|
56 | return self.msg_position |
---|
57 | |
---|
58 | def SetStatusText(self, text="", number=MSG_POSITION): |
---|
59 | """ |
---|
60 | """ |
---|
61 | wxStatusB.SetStatusText(self, text, MSG_POSITION) |
---|
62 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
63 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
64 | |
---|
65 | def PopStatusText(self, *args, **kwds): |
---|
66 | wxStatusB.PopStatusText(self, field=MSG_POSITION) |
---|
67 | |
---|
68 | def PushStatusText(self, *args, **kwds): |
---|
69 | wxStatusB.PushStatusText(self, field=MSG_POSITION,string=string) |
---|
70 | |
---|
71 | def enable_clear_gauge(self): |
---|
72 | """ |
---|
73 | """ |
---|
74 | flag = False |
---|
75 | if (self.nb_start <= self.nb_stop) or (self.nb_progress <= self.nb_stop): |
---|
76 | flag = True |
---|
77 | return flag |
---|
78 | |
---|
79 | def OnTimer_stop(self, evt): |
---|
80 | """Clear the progress bar |
---|
81 | @param evt: wx.EVT_TIMER |
---|
82 | |
---|
83 | """ |
---|
84 | count = 0 |
---|
85 | while(count <= 100): |
---|
86 | count += 1 |
---|
87 | self.timer_stop.Stop() |
---|
88 | self.clear_gauge(msg="") |
---|
89 | self.nb_progress = 0 |
---|
90 | self.nb_start = 0 |
---|
91 | self.nb_stop = 0 |
---|
92 | |
---|
93 | def OnTimer(self, evt): |
---|
94 | """Update the progress bar while the timer is running |
---|
95 | @param evt: wx.EVT_TIMER |
---|
96 | |
---|
97 | """ |
---|
98 | # Check stop flag that can be set from non main thread |
---|
99 | if self.timer.IsRunning(): |
---|
100 | self.gauge.Pulse() |
---|
101 | |
---|
102 | def clear_gauge(self, msg=""): |
---|
103 | """ |
---|
104 | Hide the gauge |
---|
105 | """ |
---|
106 | self.progress = 0 |
---|
107 | self.gauge.SetValue(0) |
---|
108 | self.gauge.Hide() |
---|
109 | |
---|
110 | def set_icon(self, event): |
---|
111 | """ |
---|
112 | display icons related to the type of message sent to the statusbar |
---|
113 | when available |
---|
114 | """ |
---|
115 | if not hasattr(event, "info"): |
---|
116 | return |
---|
117 | msg = event.info.lower() |
---|
118 | if msg == "warning": |
---|
119 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_WARNING, wx.ART_TOOLBAR) |
---|
120 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
121 | if msg == "error": |
---|
122 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_TOOLBAR) |
---|
123 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
124 | if msg == "info": |
---|
125 | icon_bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_TOOLBAR) |
---|
126 | self.bitmap_bt_warning.SetBitmapLabel(icon_bmp) |
---|
127 | |
---|
128 | def set_message(self, event): |
---|
129 | """ |
---|
130 | display received message on the statusbar |
---|
131 | """ |
---|
132 | if hasattr(event, "status"): |
---|
133 | self.SetStatusText(str(event.status)) |
---|
134 | |
---|
135 | def set_gauge(self, event): |
---|
136 | """ |
---|
137 | change the state of the gauge according the state of the current job |
---|
138 | """ |
---|
139 | if not hasattr(event, "type"): |
---|
140 | return |
---|
141 | type = event.type |
---|
142 | self.gauge.Show(True) |
---|
143 | if type.lower()=="start": |
---|
144 | self.nb_start += 1 |
---|
145 | self.timer.Stop() |
---|
146 | self.progress += 10 |
---|
147 | self.gauge.SetValue(int(self.progress)) |
---|
148 | self.progress += 10 |
---|
149 | if self.progress < self.gauge.GetRange()-20: |
---|
150 | self.gauge.SetValue(int(self.progress)) |
---|
151 | if type.lower()=="progress": |
---|
152 | self.nb_progress += 1 |
---|
153 | self.timer.Start(100) |
---|
154 | self.gauge.Pulse() |
---|
155 | if type.lower()=="update": |
---|
156 | self.timer.Stop() |
---|
157 | self.progress += 10 |
---|
158 | if self.progress < self.gauge.GetRange()-20: |
---|
159 | self.gauge.SetValue(int(self.progress)) |
---|
160 | if type.lower()=="stop": |
---|
161 | self.nb_stop += 1 |
---|
162 | self.gauge.Show(True) |
---|
163 | print "self.enable_clear_gauge()",self.enable_clear_gauge() |
---|
164 | if self.enable_clear_gauge(): |
---|
165 | self.timer.Stop() |
---|
166 | self.progress = 0 |
---|
167 | self.gauge.SetValue(90) |
---|
168 | self.timer_stop.Start(3) |
---|
169 | |
---|
170 | def set_status(self, event): |
---|
171 | """ |
---|
172 | Update the status bar . |
---|
173 | @param type: type of message send. |
---|
174 | type must be in ["start","progress","update","stop"] |
---|
175 | @param msg: the message itself as string |
---|
176 | @param thread: if updatting using a thread status |
---|
177 | """ |
---|
178 | self.set_icon(event=event) |
---|
179 | self.set_message(event=event) |
---|
180 | self.set_gauge(event=event) |
---|
181 | |
---|
182 | if __name__ == "__main__": |
---|
183 | app = wx.PySimpleApp() |
---|
184 | frame= wx.Frame(None,wx.ID_ANY,'test frame') |
---|
185 | statusBar = StatusBar(frame, wx.ID_ANY) |
---|
186 | frame.SetStatusBar(statusBar) |
---|
187 | frame.Show(True) |
---|
188 | (NewPlotEvent, EVT_NEW_PLOT) = wx.lib.newevent.NewEvent() |
---|
189 | event = NewPlotEvent() |
---|
190 | event.type = "progress" |
---|
191 | event.status = "statusbar...." |
---|
192 | event.info = "info" |
---|
193 | statusBar.set_status(event=event) |
---|
194 | app.MainLoop() |
---|
195 | |
---|