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