Package application :: Package dialogs :: Module Progress
[hide private]
[frames] | no frames]

Source Code for Module application.dialogs.Progress

  1  #   Copyright (c) 2003-2007 Open Source Applications Foundation 
  2  # 
  3  #   Licensed under the Apache License, Version 2.0 (the "License"); 
  4  #   you may not use this file except in compliance with the License. 
  5  #   You may obtain a copy of the License at 
  6  # 
  7  #       http://www.apache.org/licenses/LICENSE-2.0 
  8  # 
  9  #   Unless required by applicable law or agreed to in writing, software 
 10  #   distributed under the License is distributed on an "AS IS" BASIS, 
 11  #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 12  #   See the License for the specific language governing permissions and 
 13  #   limitations under the License. 
 14   
 15   
 16  import wx 
 17  import threading 
 18  from i18n import ChandlerMessageFactory as _ 
 19  from osaf.activity import * 
 20   
 21   
22 -def Show(activity, parent=None):
23 win = ProgressFrame(parent, -1, activity.title, 24 size=(300,100), style=wx.DEFAULT_FRAME_STYLE, 25 activity=activity) 26 win.CenterOnParent() 27 win.Show()
28 29 30
31 -class ProgressFrame(wx.Frame):
32
33 - def __init__(self, *args, **kwds):
34 self.activity = kwds['activity'] 35 del kwds['activity'] 36 super(ProgressFrame, self).__init__(*args, **kwds) 37 self.listener = Listener(activity=self.activity, callback=self.callback) 38 39 self.panel = wx.Panel(self, -1) 40 self.sizer = wx.BoxSizer(wx.VERTICAL) 41 42 self.gaugeCtrl = wx.Gauge(self.panel, -1, size=(300,10)) 43 self.sizer.Add(self.gaugeCtrl, 0, 44 wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5) 45 self.gaugeCtrl.Pulse() 46 47 self.msgCtrl = wx.StaticText(self.panel, -1, "") 48 self.sizer.Add(self.msgCtrl, 0, wx.ALIGN_LEFT|wx.ALL, 5) 49 50 self.cancelCtrl = wx.Button(self.panel, wx.ID_CANCEL) 51 self.sizer.Add(self.cancelCtrl, 0, wx.ALIGN_LEFT|wx.ALL, 5) 52 53 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) 54 self.Bind(wx.EVT_BUTTON, self.OnCancel, id=wx.ID_CANCEL) 55 56 self.panel.SetSizer(self.sizer) 57 self.panel.Layout() 58 self.sizer.Fit(self) 59 60 self.cancel = False
61
62 - def OnCloseWindow(self, event):
63 self.listener.unregister() 64 self.Destroy()
65
66 - def OnCancel(self, event):
67 self.cancel = True
68
69 - def callback(self, activity, *args, **kwds):
70 # Can be called from any thread; will call _callback in main thread 71 72 if threading.currentThread().getName() != "MainThread": 73 wx.GetApp().PostAsyncEvent(self._callback, activity, *args, **kwds) 74 else: 75 self._callback(activity, *args, **kwds) 76 77 return self.cancel
78 79
80 - def _callback(self, activity, *args, **kwds):
81 # Must be called in main thread 82 83 if 'status' in kwds: 84 status = kwds['status'] 85 if status in (STATUS_ABORTED, STATUS_FAILED, STATUS_COMPLETE): 86 self.OnCloseWindow(None) 87 return 88 89 self.updateWidget(activity, *args, **kwds)
90
91 - def updateWidget(self, activity, *args, **kwds):
92 if 'msg' in kwds: 93 self.msgCtrl.SetLabel(kwds['msg']) 94 95 if 'percent' in kwds: 96 percent = kwds['percent'] 97 if percent is None: 98 self.gaugeCtrl.Pulse() 99 else: 100 self.gaugeCtrl.SetValue(percent) 101 102 wx.GetApp().Yield(True)
103