Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
diff options
context:
space:
mode:
authorRafael Ortiz <rafael@activitycentral.com>2012-11-08 03:06:20 (GMT)
committer Rafael Ortiz <rafael@activitycentral.com>2012-11-08 03:06:20 (GMT)
commite52817e6c332128af560d0f94b528eb0777a9d79 (patch)
tree5fbf8d1cacda3e80bfe3340920124bf8bc99126f /activity.py
parenta672c6d389f2eec99d99b8cb3b5123e6d6a4aca8 (diff)
erasing cjson leftovers for json
Diffstat (limited to 'activity.py')
-rw-r--r--activity.py336
1 files changed, 106 insertions, 230 deletions
diff --git a/activity.py b/activity.py
index f632032..93d9caa 100644
--- a/activity.py
+++ b/activity.py
@@ -21,37 +21,37 @@
# You should have received a copy of the GNU General Public License
# along with Speak.activity. If not, see <http://www.gnu.org/licenses/>.
-from gi.repository import Gtk
-from gi.repository import Gdk
-from gi.repository import Pango
+from sugar.activity import activity
+from sugar.presence import presenceservice
import logging
+import gtk
+import gobject
+import pango
import json
-
-from sugar3.presence import presenceservice
-from sugar3.graphics.toolbarbox import ToolbarButton
-from sugar3.graphics.radiotoolbutton import RadioToolButton
-from sugar3.graphics.toolbarbox import ToolbarBox
-from sugar3.activity.widgets import ActivityToolbarButton
-from sugar3.activity.widgets import StopButton
-
-from shared_activity import SharedActivity
-from sugar3.activity import activity
-from combobox import ComboBox
-from messenger import Messenger, SERVICE
from gettext import gettext as _
-from eye import Eye
-from eye import Glasses
-from mouth import Mouth
-from mouth import FFTMouth
-from mouth import WaveformMouth
+from sugar.graphics.toolbutton import ToolButton
+from sugar.graphics.toggletoolbutton import ToggleToolButton
+from sugar.graphics.radiotoolbutton import RadioToolButton
+
+from toolkit.toolitem import ToolWidget
+from toolkit.combobox import ComboBox
+from toolkit.toolbarbox import ToolbarBox
+from toolkit.activity import SharedActivity
+from toolkit.activity_widgets import *
+
+import eye
+import glasses
+import mouth
+import fft_mouth
+import waveform_mouth
import voice
import face
import brain
import chat
import espeak
-
+from messenger import Messenger, SERVICE
logger = logging.getLogger('speak')
@@ -59,88 +59,59 @@ MODE_TYPE = 1
MODE_BOT = 2
MODE_CHAT = 3
-_NEW_INSTANCE = 0
-_NEW_INSTANCE = 1
-_PRE_INSTANCE = 2
-_POST_INSTANCE = 3
-
-
-class CursorFactory:
-
- __shared_state = {"cursors": {}}
-
- def __init__(self):
- self.__dict__ = self.__shared_state
-
- def get_cursor(self, cur_type):
- if not cur_type in self.cursors:
- cur = Gdk.Cursor.new(cur_type)
- self.cursors[cur_type] = cur
- return self.cursors[cur_type]
-
class SpeakActivity(SharedActivity):
-
def __init__(self, handle):
-
- self.notebook = Gtk.Notebook()
- self.notebook.connect_after('map', self.__map_canvasactivity_cb)
-
+ self.notebook = gtk.Notebook()
+
SharedActivity.__init__(self, self.notebook, SERVICE, handle)
-
- self._cursor = None
- self.set_cursor(Gdk.CursorType.LEFT_PTR)
- self.__resume_filename = None
- self.__postponed_share = []
- self.__on_save_instance = []
- self.__state = _NEW_INSTANCE
+
self._mode = MODE_TYPE
self.numeyesadj = None
-
+
# make an audio device for playing back and rendering audio
self.connect("notify::active", self._activeCb)
-
- # make a box to type into - combo for user to type
- self.entrycombo = Gtk.ComboBoxText.new_with_entry()
+
+ # make a box to type into
+ self.entrycombo = gtk.combo_box_entry_new_text()
self.entrycombo.connect("changed", self._combo_changed_cb)
- self.entry = self.entrycombo.get_child()
- self.entry.can_activate_accel(True)
+ self.entry = self.entrycombo.child
+ self.entry.set_editable(True)
self.entry.connect('activate', self._entry_activate_cb)
self.entry.connect("key-press-event", self._entry_key_press_cb)
- self.input_font = Pango.FontDescription('sans bold 24')
+ self.input_font = pango.FontDescription(str='sans bold 24')
self.entry.modify_font(self.input_font)
self.face = face.View()
self.face.show()
# layout the screen
- box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
- box.set_homogeneous(False)
- box.pack_start(self.face, True, True, 0)
- box.pack_start(self.entrycombo, False, True, 0)
-
- self.add_events(Gdk.EventMask.POINTER_MOTION_HINT_MASK
- | Gdk.EventMask.POINTER_MOTION_MASK)
-
+ box = gtk.VBox(homogeneous=False)
+ box.pack_start(self.face)
+ box.pack_start(self.entrycombo, expand=False)
+
+ self.add_events(gtk.gdk.POINTER_MOTION_HINT_MASK
+ | gtk.gdk.POINTER_MOTION_MASK)
self.connect("motion_notify_event", self._mouse_moved_cb)
- box.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
+ box.add_events(gtk.gdk.BUTTON_PRESS_MASK)
box.connect("button_press_event", self._mouse_clicked_cb)
# desktop
self.notebook.show()
- self.notebook.set_show_border(False)
- self.notebook.set_show_tabs(False)
+ self.notebook.props.show_border=False
+ self.notebook.props.show_tabs=False
box.show_all()
- self.notebook.append_page(box, Gtk.Label(""))
+ self.notebook.append_page(box)
self.chat = chat.View()
self.chat.show_all()
- self.notebook.append_page(self.chat, Gtk.Label(""))
+ self.notebook.append_page(self.chat)
# make the text box active right away
self.entry.grab_focus()
+
self.entry.connect("move-cursor", self._cursor_moved_cb)
self.entry.connect("changed", self._cursor_moved_cb)
@@ -149,25 +120,22 @@ class SpeakActivity(SharedActivity):
toolbox.toolbar.insert(ActivityToolbarButton(self), -1)
- #importing voices to combobox
self.voices = ComboBox()
for name in sorted(voice.allVoices().keys()):
vn = voice.allVoices()[name]
- n = name[:26] + ".."
+ n = name [ : 26 ] + ".."
self.voices.append_item(vn, n)
self.voices.select(voice.defaultVoice())
all_voices = self.voices.get_model()
brain_voices = brain.get_voices()
- #toolbar: speak mode button
mode_type = RadioToolButton(
named_icon='mode-type',
tooltip=_('Type something to hear it'))
mode_type.connect('toggled', self.__toggled_mode_type_cb, all_voices)
toolbox.toolbar.insert(mode_type, -1)
- #toolbar: robot mode button
mode_robot = RadioToolButton(
named_icon='mode-robot',
group=mode_type,
@@ -176,108 +144,42 @@ class SpeakActivity(SharedActivity):
brain_voices)
toolbox.toolbar.insert(mode_robot, -1)
- #toolbar: mode chat button
mode_chat = RadioToolButton(
named_icon='mode-chat',
group=mode_type,
tooltip=_('Voice chat'))
mode_chat.connect('toggled', self.__toggled_mode_chat_cb, all_voices)
toolbox.toolbar.insert(mode_chat, -1)
-
- #toolbar: voices combo (not working)
+
voices_toolitem = ToolWidget(widget=self.voices)
toolbox.toolbar.insert(voices_toolitem, -1)
- #toolbar: voice button
voice_button = ToolbarButton(
page=self.make_voice_bar(),
label=_('Voice'),
icon_name='voice')
toolbox.toolbar.insert(voice_button, -1)
- #toolbar: face button
face_button = ToolbarButton(
page=self.make_face_bar(),
label=_('Face'),
icon_name='face')
toolbox.toolbar.insert(face_button, -1)
-
- #separator
- separator = Gtk.SeparatorToolItem()
+
+ separator = gtk.SeparatorToolItem()
separator.set_draw(False)
separator.set_expand(True)
toolbox.toolbar.insert(separator, -1)
-
+
toolbox.toolbar.insert(StopButton(self), -1)
-
+
toolbox.show_all()
self.toolbar_box = toolbox
- def _share(self, tube_conn, initiator):
- logging.debug('Activity._share state=%s' % self.__state)
-
- if self.__state == _NEW_INSTANCE:
- self.__postponed_share.append((tube_conn, initiator))
- self.__state = _PRE_INSTANCE
- elif self.__state == _PRE_INSTANCE:
- self.__postponed_share.append((tube_conn, initiator))
- self.__instance()
- elif self.__state == _POST_INSTANCE:
- self.share_instance(tube_conn, initiator)
-
- def set_cursor(self, cursor):
- if not isinstance(cursor, Gdk.Cursor):
- cursor = CursorFactory().get_cursor(cursor)
-
- if self._cursor != cursor:
- self._cursor = cursor
- self.get_property('window').set_cursor(self._cursor)
-
- def __map_canvasactivity_cb(self, widget):
- logging.debug('Activity.__map_canvasactivity_cb state=%s' % \
- self.__state)
-
- if self.__state == _NEW_INSTANCE:
- self.__instance()
- elif self.__state == _NEW_INSTANCE:
- self.__state = _PRE_INSTANCE
- elif self.__state == _PRE_INSTANCE:
- self.__instance()
-
- return False
-
- def __instance(self):
- logging.debug('Activity.__instance')
-
- if self.__resume_filename:
- self.resume_instance(self.__resume_filename)
- else:
- self.new_instance()
-
- for i in self.__postponed_share:
- self.share_instance(*i)
- self.__postponed_share = []
-
- self.__state = _POST_INSTANCE
-
- def read_file(self, file_path):
- self.__resume_filename = file_path
- if self.__state == _NEW_INSTANCE:
- self.__state = _PRE_INSTANCE
- elif self.__state == _PRE_INSTANCE:
- self.__instance()
-
- def write_file(self, file_path):
- for cb, args in self.__on_save_instance:
- cb(*args)
- self.save_instance(file_path)
-
def new_instance(self):
self.voices.connect('changed', self.__changed_voices_cb)
- self.pitchadj.connect("value_changed", self.pitch_adjusted_cb,
- self.pitchadj)
- self.rateadj.connect("value_changed", self.rate_adjusted_cb,
- self.rateadj)
+ self.pitchadj.connect("value_changed", self.pitch_adjusted_cb, self.pitchadj)
+ self.rateadj.connect("value_changed", self.rate_adjusted_cb, self.rateadj)
self.mouth_shape_combo.connect('changed', self.mouth_changed_cb, False)
self.mouth_changed_cb(self.mouth_shape_combo, True)
self.numeyesadj.connect("value_changed", self.eyes_changed_cb, False)
@@ -297,13 +199,13 @@ class SpeakActivity(SharedActivity):
status = self.face.status = face.Status().deserialize(cfg['status'])
self.voices.select(status.voice)
- self.pitchadj.set_value(self.face.status.pitch)
- self.rateadj.set_value(self.face.status.rate)
+ self.pitchadj.value = self.face.status.pitch
+ self.rateadj.value = self.face.status.rate
self.mouth_shape_combo.select(status.mouth)
self.eye_shape_combo.select(status.eyes[0])
- self.numeyesadj.set_value(len(status.eyes))
+ self.numeyesadj.value = len(status.eyes)
- self.entry.set_text(cfg['text'].encode('utf-8', 'ignore'))
+ self.entry.props.text = cfg['text'].encode('utf-8', 'ignore')
for i in cfg['history']:
self.entrycombo.append_text(i.encode('utf-8', 'ignore'))
@@ -311,26 +213,26 @@ class SpeakActivity(SharedActivity):
def save_instance(self, file_path):
cfg = {'status': self.face.status.serialize(),
- 'text': unicode(self.entry.get_text(), 'utf-8', 'ignore'),
+ 'text': unicode(self.entry.props.text, 'utf-8', 'ignore'),
'history': [unicode(i[0], 'utf-8', 'ignore') \
for i in self.entrycombo.get_model()],
}
- file(file_path, 'w').write(json.dumps(cfg))
+ file(file_path, 'w').write(cjson.encode(cfg))
def share_instance(self, connection, is_initiator):
self.chat.messenger = Messenger(connection, is_initiator, self.chat)
def _cursor_moved_cb(self, entry, *ignored):
# make the eyes track the motion of the text cursor
- index = entry.get_property('cursor_position')
+ index = entry.props.cursor_position
layout = entry.get_layout()
pos = layout.get_cursor_pos(index)
- x = pos[0].x / Pango.SCALE - entry.get_property('scroll_offset')
+ x = pos[0][0] / pango.SCALE - entry.props.scroll_offset
y = entry.get_allocation().y
self.face.look_at(pos=(x, y))
def get_mouse(self):
- display = Gdk.Display.get_default()
+ display = gtk.gdk.display_get_default()
screen, mouseX, mouseY, modifiers = display.get_pointer()
return mouseX, mouseY
@@ -338,21 +240,19 @@ class SpeakActivity(SharedActivity):
# make the eyes track the motion of the mouse cursor
self.face.look_at()
self.chat.look_at()
-
+
def _mouse_clicked_cb(self, widget, event):
pass
def make_voice_bar(self):
- voicebar = Gtk.Toolbar()
+ voicebar = gtk.Toolbar()
- self.pitchadj = Gtk.Adjustment(self.face.status.pitch, 0,
- espeak.PITCH_MAX, 1, espeak.PITCH_MAX / 10, 0)
- pitchbar = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL)
- pitchbar.set_adjustment(self.pitchadj)
+ self.pitchadj = gtk.Adjustment(self.face.status.pitch, 0,
+ espeak.PITCH_MAX, 1, espeak.PITCH_MAX/10, 0)
+ pitchbar = gtk.HScale(self.pitchadj)
pitchbar.set_draw_value(False)
- # FIXME: Gtk.UPDATE_DISCONTINUOUS
# pitchbar.set_inverted(True)
- #pitchbar.set_update_policy(Gtk.UPDATE_DISCONTINUOUS)
+ pitchbar.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
pitchbar.set_size_request(240, 15)
pitchbar_toolitem = ToolWidget(
@@ -360,14 +260,12 @@ class SpeakActivity(SharedActivity):
label_text=_('Pitch:'))
voicebar.insert(pitchbar_toolitem, -1)
- self.rateadj = Gtk.Adjustment(self.face.status.rate, 0,
- espeak.RATE_MAX, 1, espeak.RATE_MAX / 10, 0)
- ratebar = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL)
- ratebar.set_adjustment(self.rateadj)
+ self.rateadj = gtk.Adjustment(self.face.status.rate, 0, espeak.RATE_MAX,
+ 1, espeak.RATE_MAX / 10, 0)
+ ratebar = gtk.HScale(self.rateadj)
ratebar.set_draw_value(False)
- # FIXME: Gtk.UPDATE_DISCONTINUOUS
# ratebar.set_inverted(True)
- #ratebar.set_update_policy(Gtk.UPDATE_DISCONTINUOUS)
+ ratebar.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
ratebar.set_size_request(240, 15)
ratebar_toolitem = ToolWidget(
@@ -378,23 +276,21 @@ class SpeakActivity(SharedActivity):
voicebar.show_all()
return voicebar
- def pitch_adjusted_cb(self, adj, data=None):
- self.face.status.pitch = adj.get_value()
+ def pitch_adjusted_cb(self, get, data=None):
+ self.face.status.pitch = get.value
self.face.say_notification(_("pitch adjusted"))
- def rate_adjusted_cb(self, adj, data=None):
- self.face.status.rate = adj.get_value()
+ def rate_adjusted_cb(self, get, data=None):
+ self.face.status.rate = get.value
self.face.say_notification(_("rate adjusted"))
- # face subtoolbar
def make_face_bar(self):
- facebar = Gtk.Toolbar()
+ facebar = gtk.Toolbar()
self.mouth_shape_combo = ComboBox()
- self.mouth_shape_combo.append_item(Mouth, _("Simple"))
- self.mouth_shape_combo.append_item(WaveformMouth,
- _("Waveform"))
- self.mouth_shape_combo.append_item(FFTMouth, _("Frequency"))
+ self.mouth_shape_combo.append_item(mouth.Mouth, _("Simple"))
+ self.mouth_shape_combo.append_item(waveform_mouth.WaveformMouth, _("Waveform"))
+ self.mouth_shape_combo.append_item(fft_mouth.FFTMouth, _("Frequency"))
self.mouth_shape_combo.set_active(0)
mouth_shape_toolitem = ToolWidget(
@@ -403,8 +299,8 @@ class SpeakActivity(SharedActivity):
facebar.insert(mouth_shape_toolitem, -1)
self.eye_shape_combo = ComboBox()
- self.eye_shape_combo.append_item(Eye, _("Round"))
- self.eye_shape_combo.append_item(Glasses, _("Glasses"))
+ self.eye_shape_combo.append_item(eye.Eye, _("Round"))
+ self.eye_shape_combo.append_item(glasses.Glasses, _("Glasses"))
self.eye_shape_combo.set_active(0)
eye_shape_toolitem = ToolWidget(
@@ -412,12 +308,10 @@ class SpeakActivity(SharedActivity):
label_text=_('Eyes:'))
facebar.insert(eye_shape_toolitem, -1)
- self.numeyesadj = Gtk.Adjustment(2, 1, 5, 1, 1, 0)
- numeyesbar = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL)
- numeyesbar.set_adjustment(self.numeyesadj)
+ self.numeyesadj = gtk.Adjustment(2, 1, 5, 1, 1, 0)
+ numeyesbar = gtk.HScale(self.numeyesadj)
numeyesbar.set_draw_value(False)
- # FIXME: Gtk.UPDATE_DISCONTINUOUS
- #numeyesbar.set_update_policy(Gtk.UPDATE_DISCONTINUOUS)
+ numeyesbar.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
numeyesbar.set_size_request(240, 15)
numeyesbar_toolitem = ToolWidget(
@@ -428,7 +322,6 @@ class SpeakActivity(SharedActivity):
facebar.show_all()
return facebar
- # mouth changed callback
def mouth_changed_cb(self, combo, quiet):
self.face.status.mouth = combo.props.value
self._update_face()
@@ -437,12 +330,12 @@ class SpeakActivity(SharedActivity):
if not quiet:
self.face.say_notification(_("mouth changed"))
- # eyes changed callback
def eyes_changed_cb(self, ignored, quiet):
if self.numeyesadj is None:
return
+
self.face.status.eyes = [self.eye_shape_combo.props.value] \
- * int(self.numeyesadj.get_value())
+ * int(self.numeyesadj.value)
self._update_face()
# this SegFaults: self.face.say(self.eye_shape_combo.get_active_text())
@@ -461,18 +354,18 @@ class SpeakActivity(SharedActivity):
def _entry_key_press_cb(self, combo, event):
# make the up/down arrows navigate through our history
- keyname = Gdk.keyval_name(event.keyval)
+ keyname = gtk.gdk.keyval_name(event.keyval)
if keyname == "Up":
index = self.entrycombo.get_active()
- if index > 0:
- index -= 1
+ if index>0:
+ index-=1
self.entrycombo.set_active(index)
- self.entry.select_region(0, -1)
+ self.entry.select_region(0,-1)
return True
elif keyname == "Down":
index = self.entrycombo.get_active()
- if index < len(self.entrycombo.get_model()) - 1:
- index += 1
+ if index<len(self.entrycombo.get_model())-1:
+ index+=1
self.entrycombo.set_active(index)
self.entry.select_region(0, -1)
return True
@@ -480,33 +373,32 @@ class SpeakActivity(SharedActivity):
def _entry_activate_cb(self, entry):
# the user pressed Return, say the text and clear it out
- text = entry.get_text()
+ text = entry.props.text
if text:
self.face.look_ahead()
# speak the text
if self._mode == MODE_BOT:
self.face.say(
- brain.respond(self.voices.get_value(), text))
+ brain.respond(self.voices.props.value, text))
else:
self.face.say(text)
- # add this text to our history unless
- # it is the same as the last item
+ # add this text to our history unless it is the same as the last item
history = self.entrycombo.get_model()
- if len(history) == 0 or history[-1][0] != text:
+ if len(history)==0 or history[-1][0] != text:
self.entrycombo.append_text(text)
# don't let the history get too big
- while len(history) > 20:
- self.entrycombo.remove(0)
+ while len(history)>20:
+ self.entrycombo.remove_text(0)
# select the new item
- self.entrycombo.set_active(len(history) - 1)
+ self.entrycombo.set_active(len(history)-1)
# select the whole text
entry.select_region(0, -1)
def _activeCb(self, widget, pspec):
# only generate sound when this activity is active
- if not self.is_active():
+ if not self.props.active:
self.face.shut_up()
self.chat.shut_up()
@@ -527,7 +419,7 @@ class SpeakActivity(SharedActivity):
self.face.shut_up()
self.notebook.set_current_page(0)
- old_voice = self.voices.get_value()
+ old_voice = self.voices.props.value
self.voices.set_model(voices_model)
self._set_voice(old_voice)
@@ -540,7 +432,7 @@ class SpeakActivity(SharedActivity):
self.face.shut_up()
self.notebook.set_current_page(0)
- old_voice = self.voices.get_value()
+ old_voice = self.voices.props.value
self.voices.set_model(voices_model)
new_voice = [i[0] for i in voices_model
@@ -558,7 +450,7 @@ class SpeakActivity(SharedActivity):
self._set_voice(new_voice)
- if not brain.load(self, self.voices.get_value(), sorry):
+ if not brain.load(self, self.voices.props.value, sorry):
if sorry:
self.face.say_notification(sorry)
@@ -566,13 +458,13 @@ class SpeakActivity(SharedActivity):
if not button.props.active:
return
- is_first_session = not self.chat.me.get_mapped()
+ is_first_session = not self.chat.me.flags() & gtk.MAPPED
self._mode = MODE_CHAT
self.face.shut_up()
self.notebook.set_current_page(1)
- old_voice = self.voices.get_value()
+ old_voice = self.voices.props.value
self.voices.set_model(voices_model)
self._set_voice(old_voice)
@@ -586,22 +478,6 @@ class SpeakActivity(SharedActivity):
if self._mode == MODE_BOT:
brain.load(self, voice)
+
# activate gtk threads when this module loads
-Gdk.threads_init()
-
-
-class ToolWidget(Gtk.ToolItem):
-
- def __init__(self, widget=None, label_text=""):
- Gtk.ToolItem.__init__(self)
- self.wid = widget
- self.label = Gtk.Label(label_text)
- self._box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
-
- if self.label:
- self._box.pack_start(self.label, True, True, 5)
- if self.wid:
- self._box.pack_start(self.wid, True, True, 5)
-
- self.add(self._box)
- self.show_all()
+gtk.gdk.threads_init()