__init__.py
来自「bittorrent source by python. please enj」· Python 代码 · 共 869 行 · 第 1/2 页
PY
869 行
def choose(self, event): """Pop up a choose dialog and set the result if the user clicks OK.""" dialog = self.gen_dialog() result = dialog.ShowModal() if result == wx.ID_OK: path = dialog.GetPath() if self.setfunc: self.setfunc(path)class ChooseDirectorySizer(wx.BoxSizer): def __init__(self, parent, path='', setfunc=None, editable=True, dialog_title=_("Choose a folder..."), button_label=_("&Browse...")): wx.BoxSizer.__init__(self, wx.HORIZONTAL) self.parent = parent self.setfunc = setfunc self.dialog_title = dialog_title self.button_label = button_label self.pathbox = wx.TextCtrl(self.parent, size=(250, -1)) self.pathbox.SetEditable(editable) self.Add(self.pathbox, proportion=1, flag=wx.RIGHT, border=SPACING) self.pathbox.SetValue(path) self.button = PathDialogButton(parent, gen_dialog=self.dialog, setfunc=self.set_choice, label=self.button_label) self.Add(self.button) def set_choice(self, path): self.pathbox.SetValue(path) if self.setfunc: self.setfunc(path) def get_choice(self): return self.pathbox.GetValue() def dialog(self): dialog = wx.DirDialog(self.parent, message=self.dialog_title, style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON) dialog.SetPath(self.get_choice()) return dialogclass ChooseFileSizer(ChooseDirectorySizer): def __init__(self, parent, path='', setfunc=None, editable=True, dialog_title=_("Choose a file..."), button_label=_("&Browse..."), wildcard=_("All files (*.*)|*.*"), dialog_style=wx.OPEN): ChooseDirectorySizer.__init__(self, parent, path=path, setfunc=setfunc, editable=editable, dialog_title=dialog_title, button_label=button_label) self.wildcard = wildcard self.dialog_style = dialog_style def dialog(self): directory, file = os.path.split(self.get_choice()) dialog = wx.FileDialog(self.parent, defaultDir=directory, defaultFile=file, message=self.dialog_title, wildcard=self.wildcard, style=self.dialog_style) #dialog.SetPath(self.get_choice()) return dialogclass ChooseFileOrDirectorySizer(wx.BoxSizer): def __init__(self, parent, path='', setfunc=None, editable=True, file_dialog_title=_("Choose a file..."), directory_dialog_title=_("Choose a folder..."), file_button_label=_("Choose &file..."), directory_button_label=_("Choose f&older..."), wildcard=_("All files (*.*)|*.*"), file_dialog_style=wx.OPEN): wx.BoxSizer.__init__(self, wx.VERTICAL) self.parent = parent self.setfunc = setfunc self.file_dialog_title = file_dialog_title self.directory_dialog_title = directory_dialog_title self.file_button_label = file_button_label self.directory_button_label = directory_button_label self.wildcard = wildcard self.file_dialog_style = file_dialog_style self.pathbox = wx.TextCtrl(self.parent, size=(250, -1)) self.pathbox.SetEditable(editable) self.Add(self.pathbox, flag=wx.EXPAND|wx.BOTTOM, border=SPACING) self.pathbox.SetValue(path) self.subsizer = wx.BoxSizer(wx.HORIZONTAL) self.Add(self.subsizer, flag=wx.ALIGN_RIGHT, border=0) self.fbutton = PathDialogButton(parent, gen_dialog=self.file_dialog, setfunc=self.set_choice, label=self.file_button_label) self.subsizer.Add(self.fbutton, flag=wx.LEFT, border=SPACING) self.dbutton = PathDialogButton(parent, gen_dialog=self.directory_dialog, setfunc=self.set_choice, label=self.directory_button_label) self.subsizer.Add(self.dbutton, flag=wx.LEFT, border=SPACING) def set_choice(self, path): self.pathbox.SetValue(path) if self.setfunc: self.setfunc(path) def get_choice(self): return self.pathbox.GetValue() def directory_dialog(self): dialog = wx.DirDialog(self.parent, message=self.directory_dialog_title, style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON) dialog.SetPath(self.get_choice()) return dialog def file_dialog(self): dialog = wx.FileDialog(self.parent, message=self.file_dialog_title, defaultDir=self.get_choice(), wildcard=self.wildcard, style=self.file_dialog_style) dialog.SetPath(self.get_choice()) return dialogclass Grid(wx.grid.Grid): def SetColRenderer(self, col, renderer): table = self.GetTable() attr = table.GetAttr(-1, col, wx.grid.GridCellAttr.Col) if (not attr): attr = wx.grid.GridCellAttr() attr.SetRenderer(renderer) self.SetColAttr(col, attr) def SetColEditor(self, col, editor): table = self.GetTable() attr = table.GetAttr(-1, col, wx.grid.GridCellAttr.Col) if (not attr): attr = wx.grid.GridCellAttr() attr.SetEditor(editor) self.SetColAttr(col, attr)class BTMenu(wx.Menu): """Base class for menus""" def __init__(self, *a, **k): wx.Menu.__init__(self, *a, **k) def add_item(self, label): iid = wx.NewId() self.Append(iid, label) return iid def add_check_item(self, label, value=False): iid = wx.NewId() self.AppendCheckItem(iid, label) self.Check(id=iid, check=value) return iidclass CheckButton(wx.CheckBox): """Base class for check boxes""" def __init__(self, parent, label, main, option_name, initial_value, extra_callback=None): wx.CheckBox.__init__(self, parent, label=label) self.main = main self.option_name = option_name self.option_type = type(initial_value) self.SetValue(bool(initial_value)) self.extra_callback = extra_callback self.Bind(wx.EVT_CHECKBOX, self.callback) def callback(self, *args): if self.option_type is not type(None): self.main.config[self.option_name] = self.option_type( not self.main.config[self.option_name]) self.main.setfunc(self.option_name, self.main.config[self.option_name]) if self.extra_callback is not None: self.extra_callback()class BTPanel(wx.Panel): sizer_class = wx.BoxSizer sizer_args = (wx.VERTICAL,) def __init__(self, *a, **k): k['style'] = k.get('style', 0) | wx.CLIP_CHILDREN wx.Panel.__init__(self, *a, **k) self.sizer = self.sizer_class(*self.sizer_args) self.SetSizer(self.sizer) def Add(self, widget, *a, **k): self.sizer.Add(widget, *a, **k) def AddFirst(self, widget, *a, **k): if hasattr(self.sizer, 'AddFirst'): self.sizer.AddFirst(widget, *a, **k) else: self.sizer.Add(widget, *a, **k)# handles quirks in the design of wx. For example, the wx.LogWindow is not# really a window, but this make it respond to shows as if it were.def MagicShow_func(win, show=True): win.Show(show) if show: win.Raise()class MagicShow: """You know, like with a guy pulling rabbits out of a hat""" def MagicShow(self, show=True): if hasattr(self, 'magic_window'): # hackery in case we aren't actually a window win = self.magic_window else: win = self MagicShow_func(win, show)class BTDialog(wx.Dialog, MagicShow): """Base class for all BitTorrent window dialogs""" def __init__(self, *a, **k): wx.Dialog.__init__(self, *a, **k) self.SetIcon(wx.the_app.icon)class BTFrame(wx.Frame, MagicShow): """Base class for all BitTorrent window frames""" def __init__(self, *a, **k): wx.Frame.__init__(self, *a, **k) self.SetIcon(wx.the_app.icon) def load_geometry(self, geometry, default_size=None): if '+' in geometry: s, x, y = geometry.split('+') x, y = int(x), int(y) else: x, y = -1, -1 s = geometry if 'x' in s: w, h = s.split('x') w, h = int(w), int(h) else: w, h = -1, -1 i = 0 if '__WXMSW__' in wx.PlatformInfo: i = wx.Display.GetFromWindow(self) d = wx.Display(i) (x1, y1, x2, y2) = d.GetGeometry() x = min(x, x2-64) y = min(y, y2-64) if (w,h) <= (0,0) and default_size is not None: w = default_size.width h = default_size.height self.SetDimensions(x, y, w, h, sizeFlags=wx.SIZE_USE_EXISTING) def _geometry_string(self): pos = self.GetPositionTuple() size = self.GetSizeTuple() g = '' g += 'x'.join(map(str, size)) if pos > (0,0): g += '+' + '+'.join(map(str, pos)) return g def SetTitle(self, title): if title != self.GetTitle(): wx.Frame.SetTitle(self, title)class BTFrameWithSizer(BTFrame): """BitTorrent window frames with sizers, which are less flexible than normal windows""" panel_class = BTPanel sizer_class = wx.BoxSizer sizer_args = (wx.VERTICAL,) def __init__(self, *a, **k): BTFrame.__init__(self, *a, **k) try: self.SetIcon(wx.the_app.icon) self.panel = self.panel_class(self) self.sizer = self.sizer_class(*self.sizer_args) self.Add(self.panel, flag=wx.GROW, proportion=1) self.SetSizer(self.sizer) except: self.Destroy() raise def Add(self, widget, *a, **k): self.sizer.Add(widget, *a, **k)class BTApp(wx.App): """Base class for all wx-based BitTorrent applications""" def __init__(self, *a, **k): wx.App.__init__(self, *a, **k) def OnInit(self): self.prof = Amaturefile() if profile: def start_profile(): self.prof = hotshot.Profile(prof_file_name) try: os.unlink(prof_file_name) except: pass wx.FutureCall(6, start_profile) wx.the_app = self self._DoIterationId = wx.NewEventType() self.Connect(-1, -1, self._DoIterationId, self._doIteration) self.evt = wx.PyEvent() self.evt.SetEventType(self._DoIterationId) self.event_queue = [] # this breaks TreeListCtrl, and I'm too lazy to figure out why #wx.IdleEvent_SetMode(wx.IDLE_PROCESS_SPECIFIED) # this fixes 24bit-color toolbar buttons wx.SystemOptions_SetOptionInt("msw.remap", 0) icon_path = os.path.join(image_root, 'bittorrent.ico') self.icon = wx.Icon(icon_path, wx.BITMAP_TYPE_ICO) self.doneflag = threading.Event() return True def OnExit(self): if profile: self.prof.close() stats = hotshot.stats.load(prof_file_name) stats.strip_dirs() stats.sort_stats('time', 'calls') print "UI MainLoop Profile:" stats.print_stats(20) pass def who(self, _f, a): if _f.__name__ == "_recall": if not hasattr(a[0], 'gen'): return str(a[0]) return a[0].gen.gi_frame.f_code.co_name return _f.__name__ def _doIteration(self, event): _f, a, kw = self.event_queue.pop(0) try: if self.doneflag.isSet(): #print "dropping", _f return except: # assume any kind of error means the app is dying return## t = bttime()## print self.who(_f, a) if profile: self.prof.start() _f(*a, **kw) self.prof.stop() else: _f(*a, **kw)## print self.who(_f, a), 'done in', bttime() - t## if bttime() - t > 1.0:## print 'TOO SLOW!'## assert False def CallAfter(self, callable, *args, **kw): """ Call the specified function after the current and pending event handlers have been completed. This is also good for making GUI method calls from non-GUI threads. Any extra positional or keyword args are passed on to the callable when it is called. """ # append (right) and pop (left) are atomic self.event_queue.append((callable, args, kw)) wx.PostEvent(self, self.evt)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?