diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2012-03-06 17:57:25 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2012-03-06 17:57:25 (GMT) |
commit | 36bf1a90be2e99eb9c4aef4fbb87044128ff8541 (patch) | |
tree | 1e8c5b8f6fe95f26502ec0a480f43c53e3e80473 /speechtoolbar.py | |
parent | 405e436dfb4e2dbd074b6feb6d9b734afc118678 (diff) |
Separate the speech toolbar in another file to load only if needed
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
Diffstat (limited to 'speechtoolbar.py')
-rw-r--r-- | speechtoolbar.py | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/speechtoolbar.py b/speechtoolbar.py new file mode 100644 index 0000000..3dcd3e1 --- /dev/null +++ b/speechtoolbar.py @@ -0,0 +1,112 @@ +# Copyright (C) 2006, Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import os +import simplejson +from gettext import gettext as _ + +from gi.repository import Gtk + +from sugar3.graphics.toggletoolbutton import ToggleToolButton +from sugar3.graphics.combobox import ComboBox +from sugar3.graphics.toolcombobox import ToolComboBox + +import speech + + +class SpeechToolbar(Gtk.Toolbar): + + def __init__(self, activity): + Gtk.Toolbar.__init__(self) + self._activity = activity + if not speech.supported: + return + + self.load_speech_parameters() + + self.sorted_voices = [i for i in speech.voices()] + self.sorted_voices.sort(self.compare_voices) + default = 0 + for voice in self.sorted_voices: + if voice[0] == speech.voice[0]: + break + default = default + 1 + + # Play button + self.play_btn = ToggleToolButton('media-playback-start') + self.play_btn.show() + self.play_btn.connect('toggled', self.play_cb) + self.insert(self.play_btn, -1) + self.play_btn.set_tooltip(_('Play / Pause')) + + self.voice_combo = ComboBox() + for voice in self.sorted_voices: + self.voice_combo.append_item(voice, voice[0]) + self.voice_combo.set_active(default) + + self.voice_combo.connect('changed', self.voice_changed_cb) + combotool = ToolComboBox(self.voice_combo) + self.insert(combotool, -1) + combotool.show() + + def compare_voices(self, a, b): + if a[0].lower() == b[0].lower(): + return 0 + if a[0] .lower() < b[0].lower(): + return -1 + if a[0] .lower() > b[0].lower(): + return 1 + + def voice_changed_cb(self, combo): + speech.voice = combo.props.value + speech.say(speech.voice[0]) + self.save_speech_parameters() + + def load_speech_parameters(self): + speech_parameters = {} + data_path = os.path.join(self._activity.get_activity_root(), 'data') + data_file_name = os.path.join(data_path, 'speech_params.json') + if os.path.exists(data_file_name): + f = open(data_file_name, 'r') + try: + speech_parameters = simplejson.load(f) + speech.voice = speech_parameters['voice'] + + # load from gconf + #speech.pitch = speech_parameters['pitch'] + #speech.rate = speech_parameters['rate'] + finally: + f.close() + + def save_speech_parameters(self): + speech_parameters = {} + speech_parameters['voice'] = speech.voice + data_path = os.path.join(self._activity.get_activity_root(), 'data') + data_file_name = os.path.join(data_path, 'speech_params.json') + f = open(data_file_name, 'w') + try: + simplejson.dump(speech_parameters, f) + finally: + f.close() + + def play_cb(self, widget): + if widget.get_active(): + self.play_btn.set_named_icon('media-playback-pause') + if speech.is_stopped(): + speech.play(self._activity._view.get_marked_words()) + else: + self.play_btn.set_named_icon('media-playback-start') + speech.stop() |