home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / contacts / sort_model.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  6.3 KB  |  202 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from common import pref, profile
  5. from peak.events import trellis
  6.  
  7. class ChoiceModel(trellis.Component):
  8.     values = trellis.attr([])
  9.     selection = trellis.attr(0)
  10.     enabled = trellis.attr(True)
  11.  
  12.  
  13. class CheckModel(trellis.Component):
  14.     checked = trellis.attr(False)
  15.     enabled = trellis.attr(True)
  16.  
  17.  
  18. class SortOptionWatcher(trellis.Component):
  19.     model = trellis.attr(None)
  20.     
  21.     def print_selection(self):
  22.         if self.model is not None:
  23.             print 'selected', self.model.selection
  24.         
  25.  
  26.     print_selection = trellis.perform(print_selection)
  27.  
  28.  
  29. class OptionLink(trellis.Component):
  30.     parent = trellis.attr()
  31.     this = trellis.attr()
  32.     child = trellis.attr()
  33.     dependant = trellis.attr(True)
  34.     name = trellis.attr()
  35.     
  36.     def selected_val(self):
  37.         
  38.         try:
  39.             return self.this.values[self.this.selection]
  40.         except Exception:
  41.             pass
  42.  
  43.  
  44.     selected_val = trellis.maintain(selected_val)
  45.     
  46.     def orig_values(self):
  47.         return self.this.values[:]
  48.  
  49.     orig_values = trellis.make(orig_values)
  50.     
  51.     def available_options(self):
  52.         newvals = _[1]
  53.         return newvals
  54.  
  55.     available_options = trellis.maintain(available_options)
  56.     
  57.     def parent_vals(self):
  58.         c = self
  59.         parent_vals = []
  60.         while c.parent is not None:
  61.             if c.parent.selected_val is not None:
  62.                 parent_vals.append(c.parent.selected_val)
  63.             
  64.             c = c.parent
  65.         return parent_vals
  66.  
  67.     parent_vals = trellis.maintain(parent_vals)
  68.     
  69.     def enabled(self):
  70.         if not self.dependant:
  71.             return True
  72.         if not self.parent:
  73.             return True
  74.         if not self.parent.enabled:
  75.             return False
  76.         if self.parent.selected_val is None or self.parent.selected_val[0] in ('none', 'name'):
  77.             return False
  78.         return True
  79.  
  80.     enabled = trellis.maintain(enabled)
  81.     
  82.     def keepenabled(self):
  83.         self.this.enabled = self.enabled
  84.  
  85.     keepenabled = trellis.maintain(keepenabled)
  86.     
  87.     def sync_values(self):
  88.         newvals = self.available_options
  89.         if not self.enabled:
  90.             self.this.selection = -1
  91.             self.this.values = []
  92.             return None
  93.         if self.this.selection > 0 and self.this.values and newvals:
  94.             oldval = self.this.values[self.this.selection]
  95.             if oldval in newvals:
  96.                 self.this.selection = newvals.index(oldval)
  97.             else:
  98.                 self.this.selection = 0
  99.         elif not newvals:
  100.             self.this.selection = -1
  101.         else:
  102.             self.this.selection = 0
  103.         self.this.values = newvals
  104.  
  105.     sync_values = trellis.maintain(sync_values)
  106.  
  107.  
  108. def set_sort_values(*a):
  109.     sortby = pref('buddylist.sortby')
  110.     sorts = sortby.split()
  111.     if not sortby.startswith('*'):
  112.         sorts.insert(0, 'none')
  113.     else:
  114.         sorts[0] = sorts[0][1:]
  115.     if len(a) > len(sorts):
  116.         sorts.extend([
  117.             'none'] * (len(a) - len(sorts)))
  118.     
  119.     for model, val in zip(a, sorts):
  120.         
  121.         try:
  122.             model.selection = [ v[0] for v in model.values ].index(val)
  123.         continue
  124.         except ValueError:
  125.             break
  126.             continue
  127.         
  128.  
  129.     
  130.  
  131.  
  132. def get_sort_value(group_by, *sort_bys):
  133.     out = []
  134.     if group_by.selection >= 0:
  135.         gb_val = group_by.values[group_by.selection][0]
  136.         if gb_val != 'none':
  137.             out.append('*' + gb_val)
  138.         
  139.     
  140.     for sort_by in sort_bys:
  141.         if sort_by.selection >= 0:
  142.             sb_val = sort_by.values[sort_by.selection][0]
  143.             if sb_val == 'none':
  144.                 break
  145.             
  146.             out.append(sb_val)
  147.             continue
  148.     
  149.     if len(out) < 2:
  150.         out.extend([
  151.             'none'] * (2 - len(out)))
  152.     
  153.     return ' '.join(out)
  154.  
  155.  
  156. class SortByWatcher(trellis.Component):
  157.     
  158.     def __init__(self, sorts):
  159.         trellis.Component.__init__(self)
  160.         self.sorts = sorts
  161.  
  162.     
  163.     def output_pref(self):
  164.         val = get_sort_value(*self.sorts)
  165.         mark_pref = mark_pref
  166.         import gui.pref.prefcontrols
  167.         mark_pref('buddylist.sortby', val)
  168.  
  169.     output_pref = trellis.perform(output_pref)
  170.  
  171. GROUP_BY_CHOICES = [
  172.     ('none', _('None (custom)')),
  173.     ('status', _('Status')),
  174.     ('service', _('Service'))]
  175. SORT_BY_CHOICES = [
  176.     ('none', _('None')),
  177.     ('log', _('Log Size')),
  178.     ('name', _('Name')),
  179.     ('status', _('Status')),
  180.     ('service', _('Service'))]
  181.  
  182. def build_models(then_bys = 1, obj = None):
  183.     models = [
  184.         ChoiceModel(values = GROUP_BY_CHOICES[:])]
  185.     []([ ChoiceModel(values = SORT_BY_CHOICES[:]) for _i in range(then_bys + 1) ])
  186.     models[0].link = OptionLink(this = models[0], child = models[1], name = 'group_by')
  187.     models[1].link = OptionLink(parent = models[0].link, this = models[1], child = models[2], dependant = False, name = 'sort_by')
  188.     for i in range(then_bys):
  189.         this_idx = i + 2
  190.         this = models[this_idx]
  191.         this.link = OptionLink(parent = models[this_idx - 1].link, this = this, child = chld, name = 'then_by%d' % (i + 1))
  192.     
  193.     set_sort_values(*models)
  194.     
  195.     def on_sortby_changed(src, attr, old, new):
  196.         import wx
  197.         wx.CallAfter(set_sort_values, *models)
  198.  
  199.     profile.prefs.add_observer(on_sortby_changed, 'buddylist.sortby', obj = obj)
  200.     return models
  201.  
  202.