Package _Framework :: Module LogicalDisplaySegment
[hide private]
[frames] | no frames]

Source Code for Module _Framework.LogicalDisplaySegment

 1  #Embedded file name: /Users/versonator/Hudson/live/Projects/AppLive/Resources/MIDI Remote Scripts/_Framework/LogicalDisplaySegment.py 
 2   
 3   
4 -def adjust_string(original, length):
5 """ 6 Brings the string to the given length by either removing 7 characters or adding spaces. The algorithm is adopted from ede's 8 old implementation for the Mackie. 9 """ 10 if not length > 0: 11 raise AssertionError 12 resulting_string = original 13 if len(resulting_string) > length: 14 if resulting_string.endswith('dB'): 15 unit_db = resulting_string.find('.') != -1 16 resulting_string = len(resulting_string.strip()) > length and unit_db and resulting_string[:-2] 17 if len(resulting_string) > length: 18 for char in (' ', '_', 'i', 'o', 'u', 'e', 'a'): 19 offset = 0 if char == ' ' else 1 20 while len(resulting_string) > length and resulting_string.rfind(char, offset) > 0: 21 char_pos = resulting_string.rfind(char, offset) 22 resulting_string = resulting_string[:char_pos] + resulting_string[char_pos + 1:] 23 24 resulting_string = resulting_string[:length] 25 resulting_string = len(resulting_string) < length and resulting_string.ljust(length) 26 return resulting_string
27 28
29 -class LogicalDisplaySegment(object):
30 """ 31 Class representing a specific segment of a display on the controller 32 """ 33
34 - def __init__(self, width = None, update_callback = None, *a, **k):
35 super(LogicalDisplaySegment, self).__init__(*a, **k) 36 raise width is not None or AssertionError 37 raise callable(update_callback) or AssertionError 38 self._update_callback = update_callback 39 self._width = width 40 self._position_identifier = () 41 self._data_source = None
42
43 - def disconnect(self):
44 self._update_callback = None 45 self._position_identifier = None 46 if self._data_source != None: 47 self._data_source.set_update_callback(None) 48 self._data_source = None
49
50 - def set_data_source(self, data_source):
51 if self._data_source != None: 52 self._data_source.set_update_callback(None) 53 self._data_source = data_source 54 if self._data_source != None: 55 self._data_source.set_update_callback(self.update)
56
57 - def data_source(self):
58 return self._data_source
59
60 - def set_position_identifier(self, position_identifier):
61 """ 62 Sets position identifier as a tuple of HW related data. 63 """ 64 self._position_identifier = position_identifier
65
66 - def position_identifier(self):
67 return self._position_identifier
68
69 - def update(self):
70 self._update_callback()
71
72 - def display_string(self):
73 separator = self._data_source != None and (self._data_source.separator or '') 74 width = self._width - len(separator) 75 if not width >= 0: 76 raise AssertionError 77 resulting_string = adjust_string(self._data_source.display_string(), width) + separator 78 else: 79 resulting_string = ' ' * self._width 80 return resulting_string
81