Module synthSettingsRing
[hide private]
[frames] | no frames]

Source Code for Module synthSettingsRing

  1  import baseObject 
  2  import config 
  3  import synthDriverHandler 
  4  import queueHandler 
  5   
6 -class SynthSetting(baseObject.AutoPropertyObject):
7 """a numeric synth setting. Has functions to set, get, increase and decrease its value """
8 - def __init__(self,synth,setting,min=0,max=100):
9 self.synth=synth 10 self.setting=setting 11 self.min=min 12 self.max=max 13 self.step = setting.normalStep if isinstance(setting,synthDriverHandler.NumericSynthSetting) else 1
14
15 - def increase(self):
16 val = min(self.max,self.value+self.step) 17 self.value = val 18 return self._getReportValue(val)
19
20 - def decrease(self):
21 val = max(self.min,self.value-self.step) 22 self.value = val 23 return self._getReportValue(val)
24
25 - def _get_value(self):
26 return getattr(self.synth,self.setting.name)
27
28 - def _set_value(self,value):
29 setattr(self.synth,self.setting.name,value) 30 config.conf["speech"][self.synth.name][self.setting.name]=value
31
32 - def _getReportValue(self, val):
33 return str(val)
34
35 - def _get_reportValue(self):
36 return self._getReportValue(self.value)
37
38 -class StringSynthSetting(SynthSetting):
39 - def __init__(self,synth,setting):
40 self._values=getattr(synth,"available%ss"%setting.name.capitalize()).values() 41 super(StringSynthSetting,self).__init__(synth,setting,0,len(self._values)-1)
42
43 - def _get_value(self):
44 curID=getattr(self.synth,self.setting.name) 45 for e,v in enumerate(self._values): 46 if curID==v.ID: 47 return e
48
49 - def _set_value(self,value):
50 """Overridden to use code that supports updating speech dicts when changing voice""" 51 ID=self._values[value].ID 52 if self.setting.name=="voice": 53 synthDriverHandler.changeVoice(self.synth,ID) 54 # Voice parameters may change when the voice changes, so update the config. 55 self.synth.saveSettings() 56 else: 57 super(StringSynthSetting,self)._set_value(ID)
58
59 - def _getReportValue(self, val):
60 return self._values[val].name
61
62 -class BooleanSynthSetting(SynthSetting):
63
64 - def __init__(self, synth, setting):
65 super(BooleanSynthSetting, self).__init__(synth, setting, 0, 1)
66
67 - def _get_value(self):
68 return int(super(BooleanSynthSetting, self).value)
69
70 - def _set_value(self, val):
71 super(BooleanSynthSetting, self)._set_value(bool(val))
72
73 - def _getReportValue(self, val):
74 return _("on") if val else _("off")
75
76 -class SynthSettingsRing(baseObject.AutoPropertyObject):
77 """ 78 A synth settings ring which enables the user to change to the next and previous settings and ajust the selected one 79 It was written to facilitate the implementation of a way to change the settings resembling the window-eyes way. 80 """ 81
82 - def __init__(self,synth):
83 try: 84 self._current = synth.initialSettingsRingSetting 85 except ValueError: 86 self._current=None 87 self.updateSupportedSettings(synth)
88
89 - def _get_currentSettingName(self):
90 """ returns the current setting's name """ 91 if self._current is not None and hasattr(self,'settings'): 92 return self.settings[self._current].setting.i18nName.replace('&','') 93 return None
94
95 - def _get_currentSettingValue(self):
96 return self.settings[self._current].reportValue
97
98 - def _set_currentSettingValue(self,value):
99 if self._current is not None: 100 self.settings[_current].value = val
101
102 - def next(self):
103 """ changes to the next setting and returns its name """ 104 if self._current is not None: 105 self._current = (self._current + 1) % len(self.settings) 106 return self.currentSettingName 107 return None
108
109 - def previous(self):
110 if self._current is not None: 111 self._current = (self._current - 1) % len(self.settings) 112 return self.currentSettingName 113 return None
114
115 - def increase(self):
116 """ increases the currentSetting and returns its new value """ 117 if self._current is not None: 118 return self.settings[self._current].increase() 119 return None
120
121 - def decrease(self):
122 """ decreases the currentSetting and returns its new value """ 123 if self._current is not None: 124 return self.settings[self._current].decrease() 125 return None
126
127 - def updateSupportedSettings(self,synth):
128 import ui 129 from scriptHandler import _isScriptRunning 130 #Save name of the current setting to restore ring position after reconstruction 131 prevName=self.settings[self._current].setting.name if self._current is not None and hasattr(self,'settings') else None 132 list = [] 133 for s in synth.supportedSettings: 134 if not s.availableInSynthSettingsRing: continue 135 if prevName==s.name: #restore the last setting 136 self._current=len(list) 137 if isinstance(s,synthDriverHandler.NumericSynthSetting): 138 cls=SynthSetting 139 elif isinstance(s,synthDriverHandler.BooleanSynthSetting): 140 cls=BooleanSynthSetting 141 else: 142 cls=StringSynthSetting 143 list.append(cls(synth,s)) 144 if len(list) == 0: 145 self._current = None 146 self.settings = None 147 else: 148 self.settings = list 149 if not prevName or not self.settings or len(self.settings)<=self._current or prevName!=self.settings[self._current].setting.name: 150 #Previous chosen setting doesn't exists. Set position to default 151 self._current = synth.initialSettingsRingSetting 152 if _isScriptRunning: 153 #User changed some setting from ring and that setting no more exists. We have just reverted to first setting, so report this change to user 154 queueHandler.queueFunction(queueHandler.eventQueue,ui.message,"%s %s" % (self.currentSettingName,self.currentSettingValue))
155