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