home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / gui / pref / prefsdialog.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  15.4 KB  |  409 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. import wx
  6. import metrics
  7. from gui.textutil import default_font
  8. from gui.toolbox import build_button_sizer, snap_pref, persist_window_pos, AutoDC, calllimit
  9. from gui.validators import LengthLimit
  10. from gui.pref.prefstrings import all as allprefstrings, tabnames
  11. from util import import_function, traceguard
  12. from util.primitives.funcs import Delegate
  13. from common import profile
  14. from logging import getLogger
  15. log = getLogger('prefsdialog')
  16. prefstrings = dict((lambda .0: for name, module in .0:
  17. (name, ' '.join((lambda .0: for s in .0:
  18. s.lower().replace('&', ''))(module.itervalues())))
  19.     
  20. )(allprefstrings.iteritems()))
  21. EXPAND_ALL = wx.EXPAND | wx.ALL
  22. wxMac = 'wxMac' in wx.PlatformInfo
  23.  
  24. def show(tabname = 'accounts'):
  25.     if not isinstance(tabname, str):
  26.         raise TypeError('prefsdialog.show takes a tab name')
  27.     isinstance(tabname, str)
  28.     import hooks
  29.     hooks.notify('digsby.statistics.prefs.prefs_opened')
  30.     tabindex = [ c[0] for c in tabnames ].index(tabname)
  31.     return show_prefs_window(None, tabindex)
  32.  
  33.  
  34. def show_prefs_window(parent, tab = 0):
  35.     win = PrefsDialog.RaiseExisting()
  36.     if win is not None:
  37.         win.show_tab(tab)
  38.         return win
  39.     pdiag = PrefsDialog(None, initial_tab = tab)
  40.     persist_window_pos(pdiag, position_only = True)
  41.     wx.CallAfter(pdiag.Show)
  42.     wx.CallAfter(pdiag.ReallyRaise)
  43.     return pdiag
  44.  
  45.  
  46. class PrefsSearch(wx.SearchCtrl):
  47.     
  48.     def __init__(self, parent):
  49.         wx.SearchCtrl.__init__(self, parent, -1, style = wx.TE_PROCESS_ENTER, validator = LengthLimit(128))
  50.         self.ShowSearchButton(True)
  51.  
  52.  
  53.  
  54. class PrefsTabs(wx.VListBox):
  55.     item_height = 27
  56.     
  57.     def __init__(self, parent):
  58.         wx.VListBox.__init__(self, parent, size = (130, -1))
  59.         self.ItemCount = len(tabnames)
  60.         self.spotlight_indices = set()
  61.         self.Bind(wx.EVT_MOTION, self.OnMotion)
  62.         self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
  63.         self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
  64.         self.UpdateSkin()
  65.         self.tab_indices = dict((lambda .0: for modname, nicename in .0:
  66. (modname, i))(enumerate(tabnames)))
  67.         self.hoveridx = -1
  68.  
  69.     
  70.     def OnKey(self, e):
  71.         e.Skip()
  72.         if e.KeyCode == wx.WXK_ESCAPE:
  73.             self.Top.Close()
  74.         
  75.  
  76.     
  77.     def OnLeave(self, e):
  78.         self.Hover = -1
  79.         e.Skip(True)
  80.  
  81.     
  82.     def OnMotion(self, e):
  83.         self.Hover = self.HitTest(e.Position)
  84.         e.Skip(True)
  85.  
  86.     
  87.     def get_hover(self):
  88.         return self.hoveridx
  89.  
  90.     
  91.     def set_hover(self, i):
  92.         old = self.hoveridx
  93.         self.hoveridx = i
  94.         if i != old:
  95.             if old != -1:
  96.                 self.RefreshLine(old)
  97.             
  98.             if i != -1:
  99.                 self.RefreshLine(i)
  100.             
  101.         
  102.  
  103.     Hover = property(get_hover, set_hover)
  104.     
  105.     def UpdateSkin(self):
  106.         skin = skin
  107.         import gui
  108.         self.bgs = skin.get('AppDefaults.preftabs.backgrounds')
  109.         self.BackgroundColour = wx.Colour(*self.bgs.normal)
  110.  
  111.     
  112.     def tabname(self, index):
  113.         return tabnames[index][1]
  114.  
  115.     
  116.     def spotlight(self, tab_names):
  117.         old = self.spotlight_indices
  118.         self.spotlight_indices = (set,)((lambda .0: for name in .0:
  119. self.tab_indices[name])(tab_names))
  120.         self.RefreshAll()
  121.  
  122.     
  123.     def OnMeasureItem(self, n):
  124.         return self.item_height
  125.  
  126.     
  127.     def OnDrawItem(self, dc, rect, n):
  128.         selected = self.GetSelection() == n
  129.         font = default_font()
  130.         if selected:
  131.             font.SetWeight(wx.FONTWEIGHT_BOLD)
  132.         
  133.         fh = font.Height
  134.         dc.Font = font
  135.         dc.TextForeground = wx.BLACK
  136.         pt = wx.Point(*rect[:2]) + (5, self.item_height / 2 - fh / 2)
  137.         dc.DrawText(self.tabname(n), *pt)
  138.  
  139.     
  140.     def OnDrawBackground(self, dc, rect, n):
  141.         selected = self.Selection == n
  142.         hover = self.Hover == n
  143.         dc.Pen = wx.TRANSPARENT_PEN
  144.         if n in self.spotlight_indices:
  145.             self.bgs.search.Draw(dc, rect, n)
  146.         elif selected:
  147.             self.bgs.selected.Draw(dc, rect, n)
  148.         elif hover:
  149.             self.bgs.hover.Draw(dc, rect, n)
  150.         else:
  151.             self.bgs.normal.Draw(dc, rect, n)
  152.  
  153.  
  154. prefs_dialog_style = wx.DEFAULT_FRAME_STYLE & ~(wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX)
  155.  
  156. class PrefsDialog(wx.Frame):
  157.     border = 5
  158.     default_selected_tab = 1
  159.     default_size = (700, 525)
  160.     
  161.     def __init__(self, parent, initial_tab = default_selected_tab):
  162.         wx.Frame.__init__(self, parent, title = _('Digsby Preferences'), size = self.default_size, style = prefs_dialog_style, name = 'Preferences Window')
  163.         self.loaded_panels = { }
  164.         self.SetMinSize(self.default_size)
  165.         metrics.event('Prefs Dialog Opened')
  166.         self.create_gui()
  167.         self.bind_events()
  168.         self.layout_gui()
  169.         self.exithooks = Delegate()
  170.         traceguard.__enter__()
  171.         
  172.         try:
  173.             skin = skin
  174.             import gui
  175.             self.SetFrameIcon(skin.get('AppDefaults.TaskbarIcon'))
  176.         finally:
  177.             pass
  178.  
  179.         self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  180.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  181.         self.tabs.SetSelection(initial_tab)
  182.         self.on_tab_selected(initial_tab)
  183.         self.Bind(wx.EVT_CLOSE, self.on_close)
  184.         self._loaded = 0
  185.         snap_pref(self)
  186.         profile.prefs.add_observer(self.incoming_network_prefs)
  187.         KeyCatcher = KeyCatcher
  188.         import gui.uberwidgets.keycatcher
  189.         k.OnDown('ctrl+w', self.Close)
  190.         k.OnDown('escape', self.Close)
  191.  
  192.     
  193.     def OnPaint(self, e):
  194.         dc = AutoDC(self)
  195.         crect = self.ClientRect
  196.         if not wxMac:
  197.             dc.Brush = wx.WHITE_BRUSH
  198.             dc.Pen = wx.TRANSPARENT_PEN
  199.             dc.DrawRectangleRect(crect)
  200.         
  201.         self.tabs.bgs.normal.Draw(dc, wx.Rect(0, 0, self.tabs.ClientRect.width, crect.height))
  202.  
  203.     
  204.     def incoming_network_prefs(self, src, *a):
  205.         wx.CallAfter(self._gui_incoming_network_prefs, src)
  206.  
  207.     
  208.     def _gui_incoming_network_prefs(self, src):
  209.         if src.isflagged('network'):
  210.             wx.CallAfter(self.reload)
  211.         
  212.  
  213.     _gui_incoming_network_prefs = calllimit(2)(_gui_incoming_network_prefs)
  214.     
  215.     def on_close(self, e):
  216.         self.Hide()
  217.         e.Skip()
  218.         profile = profile
  219.         import common
  220.         profile.prefs.remove_observer(self.incoming_network_prefs)
  221.         self.exithooks()
  222.         for panel in self.loaded_panels.itervalues():
  223.             if hasattr(panel, 'on_close'):
  224.                 traceguard.__enter__()
  225.                 
  226.                 try:
  227.                     panel.on_close()
  228.                 finally:
  229.                     pass
  230.  
  231.                 continue
  232.             traceguard.__exit__
  233.         
  234.         profile.save('prefs')
  235.         profile.save('notifications')
  236.         memory_event = memory_event
  237.         import gui.native
  238.         memory_event()
  239.  
  240.     
  241.     def create_gui(self):
  242.         self.tabs = PrefsTabs(self)
  243.         self.search = PrefsSearch(self)
  244.         self.search.Bind(wx.EVT_TEXT, self.search_text)
  245.         self.search.Bind(wx.EVT_TEXT_ENTER, self.search_enter)
  246.         if False:
  247.             self.save_button = wx.Button(self, wx.ID_SAVE, _('&Done'))
  248.             self.save_button.SetDefault()
  249.             self.save_button.Bind((wx.EVT_BUTTON,), (lambda e: self.Close()))
  250.         
  251.  
  252.     
  253.     def search_enter(self, e):
  254.         if len(self.tabs.spotlight_indices) == 1:
  255.             i = self.tabs.spotlight_indices.copy().pop()
  256.             self.tabs.SetSelection(i)
  257.             self.on_tab_selected(i)
  258.         
  259.  
  260.     
  261.     def search_text(self, e):
  262.         wx.CallAfter(self._search)
  263.  
  264.     
  265.     def _search(self):
  266.         val = self.search.Value
  267.         if val == 'Search':
  268.             return None
  269.         val = val.lower()
  270.         if val == '':
  271.             return self.tabs.spotlight([])
  272.         tab_highlights = set()
  273.         for module_name, stringset in prefstrings.iteritems():
  274.             if val in stringset:
  275.                 tab_highlights.add(module_name)
  276.                 continue
  277.             val == ''
  278.         
  279.         self.tabs.spotlight(tab_highlights)
  280.  
  281.     
  282.     def bind_events(self):
  283.         self.tabs.Bind(wx.EVT_LISTBOX, self.on_tab_selected)
  284.  
  285.     
  286.     def layout_gui(self):
  287.         self.content_sizer = wx.BoxSizer(wx.VERTICAL)
  288.         hz = wx.BoxSizer(wx.HORIZONTAL)
  289.         hz.Add(self.build_tab_sizer(), 0, EXPAND_ALL)
  290.         hz.Add(self.content_sizer, 1, EXPAND_ALL)
  291.         v = wx.BoxSizer(wx.VERTICAL)
  292.         v.Add(hz, 1, EXPAND_ALL)
  293.         if getattr(self, 'save_button', False):
  294.             v.Add(build_button_sizer(self.save_button, border = self.border), 0, wx.EXPAND | wx.SOUTH | wx.EAST, 4)
  295.         
  296.         self.Sizer = v
  297.  
  298.     
  299.     def build_tab_sizer(self):
  300.         sz = wx.BoxSizer(wx.VERTICAL)
  301.         sz.Add(self.search, 0, EXPAND_ALL, self.border)
  302.         if wxMac:
  303.             sz.AddSpacer(6)
  304.         
  305.         sz.Add(self.tabs, 1, EXPAND_ALL)
  306.         return sz
  307.  
  308.     
  309.     def on_tab_selected(self, e):
  310.         index = None if isinstance(e, int) else e.Int
  311.         if index != -1:
  312.             self.tabs.Update()
  313.             wx.CallAfter(self.show_panel, self.panel_for_tab(index))
  314.         
  315.  
  316.     
  317.     def show_tab(self, n):
  318.         log.info('show_tab %d', n)
  319.         self.tabs.SetSelection(n)
  320.         self.show_panel(self.panel_for_tab(n))
  321.  
  322.     
  323.     def reload(self):
  324.         log.info('reloading Prefs dialog')
  325.         self.Frozen().__enter__()
  326.         
  327.         try:
  328.             child = self.content_sizer.Children[0]
  329.             child.Show(False)
  330.             self.content_sizer.Detach(0)
  331.             for panel in self.loaded_panels.itervalues():
  332.                 
  333.                 try:
  334.                     panel.on_close()
  335.                 except AttributeError:
  336.                     self.Frozen().__exit__
  337.                     self.Frozen().__exit__
  338.                     self.Frozen()
  339.                 except:
  340.                     self.Frozen().__exit__
  341.  
  342.                 panel.Destroy()
  343.             
  344.             self.loaded_panels.clear()
  345.             del self.exithooks[:]
  346.             self.show_tab(self.tabs.GetSelection())
  347.         finally:
  348.             pass
  349.  
  350.  
  351.     
  352.     def show_panel(self, panel):
  353.         if not isinstance(panel, wx.WindowClass):
  354.             raise TypeError('show_panel takes a Window, you gave %r' % panel)
  355.         isinstance(panel, wx.WindowClass)
  356.         self.FrozenQuick().__enter__()
  357.         
  358.         try:
  359.             s = self.content_sizer
  360.             s.Add(panel, 1, EXPAND_ALL, 10)
  361.             self.Layout()
  362.         finally:
  363.             pass
  364.  
  365.         on_show = getattr(panel, 'on_show', None)
  366.         
  367.         def later():
  368.             panel.Show()
  369.             panel.Parent.Layout()
  370.  
  371.         wx.CallAfter(later)
  372.  
  373.     
  374.     def panel_for_tab(self, i):
  375.         module_name = tabnames[i][0]
  376.         if module_name not in self.loaded_panels:
  377.             log.info('loading panel "%s"', module_name)
  378.             func = import_function('gui.pref.pg_%s.panel' % module_name)
  379.             panel = self._construct_sub_panel(func)
  380.             self.loaded_panels[module_name] = panel
  381.         
  382.         return self.loaded_panels[module_name]
  383.  
  384.     
  385.     def _construct_sub_panel(self, func):
  386.         PrefsPanel = PrefsPanel
  387.         import gui.pref.prefcontrols
  388.         p = PrefsPanel(self)
  389.         p.Sizer = sz = wx.BoxSizer(wx.VERTICAL)
  390.         szAdd = sz.Add
  391.         PrefPanel = PrefPanel
  392.         PrefCollection = PrefCollection
  393.         import gui.uberwidgets.PrefPanel
  394.         
  395.         def addgroup(titleortuple, *workers, **options):
  396.             if isinstance(titleortuple, tuple):
  397.                 (title, prefix) = titleortuple
  398.             else:
  399.                 title = titleortuple
  400.                 prefix = ''
  401.             group = PrefCollection(*workers, **options)
  402.             panel = PrefPanel(p, group, title, prefix = prefix)
  403.             szAdd(panel, 0, EXPAND_ALL, 3)
  404.             return panel
  405.  
  406.         return func(p, sz, addgroup, self.exithooks)
  407.  
  408.  
  409.