Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAneesh Dogra <lionaneesh@gmail.com>2013-01-02 02:59:16 (GMT)
committer Aneesh Dogra <lionaneesh@gmail.com>2013-01-03 17:14:00 (GMT)
commitf2e67a74490c05f14cc2248e80900432a237ed49 (patch)
treee873690722c07fb6591bf514a9a9d4fdf4309415
parent4bb021623c85db7d09f3bcbfb30fcdbaf8133a5b (diff)
[FEATURE] Add images & sound from journal.
-rw-r--r--LetterMatch.py84
-rw-r--r--page.py119
2 files changed, 149 insertions, 54 deletions
diff --git a/LetterMatch.py b/LetterMatch.py
index 2db42fb..ea06299 100644
--- a/LetterMatch.py
+++ b/LetterMatch.py
@@ -24,23 +24,27 @@ if _HAVE_TOOLBOX:
from sugar.activity.widgets import ActivityToolbarButton
from sugar.activity.widgets import StopButton
+
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.combobox import ComboBox
from sugar.graphics.toolcombobox import ToolComboBox
from sugar.datastore import datastore
from sugar import profile
+from sugar.graphics.objectchooser import ObjectChooser
+from sugar import mime
from gettext import gettext as _
import os.path
from page import Page
from utils.play_audio import play_audio_from_file
-from utils.toolbar_utils import separator_factory, label_factory, radio_factory
+from utils.toolbar_utils import separator_factory, label_factory, \
+ radio_factory, button_factory
+import json
import logging
_logger = logging.getLogger('lettermatch-activity')
-
class LetterMatch(activity.Activity):
''' Learning the alphabet.
@@ -59,6 +63,9 @@ class LetterMatch(activity.Activity):
self.datapath = get_path(activity, 'instance')
+ self.image_id = None
+ self.audio_id = None
+
if 'LANG' in os.environ:
language = os.environ['LANG'][0:2]
elif 'LANGUAGE' in os.environ:
@@ -68,6 +75,7 @@ class LetterMatch(activity.Activity):
# FIXME: find some reasonable default situation
language = 'es'
+ self.letter = None
if os.path.exists(os.path.join('~', 'Activities',
'IKnowMyABCs.activity')):
@@ -79,6 +87,9 @@ class LetterMatch(activity.Activity):
self._images_path = self._lessons_path.replace('lessons', 'images')
self._sounds_path = self._lessons_path.replace('lessons', 'sounds')
+ self.data_from_journal = {}
+ if 'data_from_journal' in self.metadata:
+ self.data_from_journal = json.load(self.metadata['data_from_journal'])
self._setup_toolbars()
# Create a canvas
@@ -137,7 +148,36 @@ class LetterMatch(activity.Activity):
self.status = label_factory(primary_toolbar, '', width=300)
+ self.letter_entry = None
+
if _HAVE_TOOLBOX:
+ separator_factory(primary_toolbar, False, True)
+
+ journal_toolbar = ToolbarBox()
+
+ button_factory('letter', journal_toolbar.toolbar,
+ self._choose_from_journal_cb,
+ tooltip=_("Import from journal"))
+
+ container = gtk.ToolItem()
+ self.letter_entry = gtk.Entry()
+ self.letter_entry.connect('changed', self._set_letter)
+ self.letter_entry.set_sensitive(False)
+ self.letter_entry.show()
+ container.add(self.letter_entry)
+ container.show_all()
+ journal_toolbar.toolbar.insert(container, -1)
+
+ self.add_button = button_factory('image', journal_toolbar.toolbar,
+ self._copy_to_journal,
+ tooltip=_("Add"))
+ self.add_button.set_sensitive(False)
+
+ # Add journal toolbar
+ journal_toolbar_button = ToolbarButton(icon_name='view-source',
+ page=journal_toolbar)
+ toolbox.toolbar.insert(journal_toolbar_button, -1)
+
separator_factory(primary_toolbar, True, False)
stop_button = StopButton(self)
@@ -145,6 +185,45 @@ class LetterMatch(activity.Activity):
toolbox.toolbar.insert(stop_button, -1)
stop_button.show()
+ def _set_letter(self, event):
+ text = self.letter_entry.get_text().strip()
+ if text and len(text) > 0:
+ if len(text) != 1:
+ text = text[0]
+ self.letter_entry.set_text(text)
+ self.letter = text
+ if self.letter in self.data_from_journal:
+ self.data_from_journal[self.letter].append(
+ (self.image_id, self.audio_id))
+ else:
+ self.data_from_journal[self.letter] = \
+ [(self.image_id, self.audio_id)]
+ self.add_button.set_sensitive(True)
+ else:
+ self.letter = None
+ self.add_button.set_sensitive(False)
+
+ def _copy_to_journal(self, event):
+ self.metadata['data_from_journal'] = json.dumps(self.data_from_journal)
+
+ def _choose_from_journal_cb(self, event):
+ self.add_button.set_sensitive(False)
+ self.letter_entry.set_sensitive(False)
+ self.image_id = None
+ self.audio_id = None
+ chooser = ObjectChooser(what_filter=mime.GENERIC_TYPE_IMAGE)
+ result = chooser.run()
+ if result == gtk.RESPONSE_ACCEPT:
+ jobject = chooser.get_selected_object()
+ self.image_id = str(jobject._object_id)
+ chooser = ObjectChooser(what_filter=mime.GENERIC_TYPE_AUDIO)
+ result = chooser.run()
+ if result == gtk.RESPONSE_ACCEPT:
+ jobject = chooser.get_selected_object()
+ self.audio_id = str(jobject._object_id)
+ if self.image_id and self.audio_id:
+ self.letter_entry.set_sensitive(True)
+
def _letter_cb(self, event=None):
''' click on card to hear the letter name '''
self.mode = 'letter'
@@ -167,7 +246,6 @@ class LetterMatch(activity.Activity):
return
self.metadata['page'] = str(self._page.current_card)
-
def get_path(activity, subpath):
""" Find a Rainbow-approved place for temporary files. """
try:
diff --git a/page.py b/page.py
index 61d8bdb..0964dd2 100644
--- a/page.py
+++ b/page.py
@@ -15,10 +15,11 @@ import gtk
import gobject
import os
import codecs
-from random import uniform
+from random import uniform, choice
from gettext import gettext as _
+from sugar.datastore import datastore
from utils.play_audio import play_audio_from_file
import logging
@@ -53,7 +54,6 @@ class Page():
self._card_data = []
self._color_data = []
self._image_data = []
- self._media_data = [] # (image sound, letter sound)
self._word_data = []
# Starting from command line
@@ -96,7 +96,7 @@ class Page():
# Create the cards we'll need
self._alpha_cards()
- self._image_cards()
+ self.load_from_journal(self._activity.data_from_journal)
self.new_page()
@@ -114,15 +114,20 @@ class Page():
x = self._grid_x_offset + GUTTER
y = self._grid_y_offset + self._card_height + GUTTER * 3
for i in range(len(self.answers)):
- self._pictures[self.answers[i]].move((x, y))
- self._pictures[self.answers[i]].set_layer(100)
+ alphabet = self._card_data[self.answers[i]][0]
+ # select the sprite randomly
+ s = choice(self._image_data[alphabet])[0]
+ s.move((x, y))
+ s.set_layer(100)
x += self._card_width + GUTTER * 2
if x > self._width - (self._card_width / 2):
x = self._grid_x_offset + GUTTER
y += self._card_height + GUTTER * 2
else:
- self._pictures[self.target].move((x, y))
- self._pictures[self.target].set_layer(100)
+ alphabet = self._card_data[self.target][0]
+ s = choice(self._image_data[alphabet])[0]
+ s.move((x, y))
+ s.set_layer(100)
x = self._grid_x_offset + GUTTER
y = self._grid_y_offset + self._card_height + GUTTER * 3
for i in range(len(self.answers)):
@@ -141,15 +146,6 @@ class Page():
for card in self._pictures:
card.hide()
- def _image_cards(self):
- for card in self._card_data:
- self.current_card = self._card_data.index(card)
- imagefilename = self._image_data[self.current_card]
- imagepath = os.path.join(self._images_path, imagefilename)
- pixbuf = image_file_to_pixbuf(imagepath, self._card_width,
- self._card_height)
- self._pictures.append(Sprite(self._sprites, 0, 0, pixbuf))
-
def _alpha_cards(self):
for card in self._card_data:
self.current_card = self._card_data.index(card)
@@ -223,13 +219,10 @@ class Page():
def _play_target_sound(self):
if self._activity.mode == 'letter':
- play_audio_from_file(os.path.join(
- self._sounds_path,
- self._media_data[self.target][1]))
+ play_audio_from_file(self._card_data[self.target][-1])
else:
- play_audio_from_file(os.path.join(
- self._sounds_path,
- self._media_data[self.target][0]))
+ # XXX: Fix me
+ pass
self.timeout = None
def _button_press_cb(self, win, event):
@@ -248,32 +241,34 @@ class Page():
x, y = map(int, event.get_coords())
spr = self._sprites.find_sprite((x, y))
+
self.current_card = -1
- if spr in self._cards:
- self.current_card = self._cards.index(spr)
- elif spr in self._pictures:
- self.current_card = self._pictures.index(spr)
- if self.current_card == -1:
- return
if self._activity.mode == 'letter':
if spr in self._cards:
- play_audio_from_file(os.path.join(
- self._sounds_path,
- self._media_data[self.current_card][1]))
+ self.current_card = self._cards.index(spr)
+ play_audio_from_file(self._card_data[self.current_card][-1])
return
- play_audio_from_file(os.path.join(
- self._sounds_path,
- self._media_data[self.current_card][0]))
+
+ for a in self._image_data:
+ for b in self._image_data[a]:
+ if spr == b[0]:
+ play_audio_from_file(b[1])
+ for c in range(len(self._card_data)):
+ if self._card_data[c][0] == a:
+ self.current_card = c
+ break
+ break
else:
- if spr in self._pictures:
- play_audio_from_file(os.path.join(
- self._sounds_path,
- self._media_data[self.current_card][0]))
- return
- play_audio_from_file(os.path.join(
- self._sounds_path,
- self._media_data[self.current_card][1]))
+ for a in self._image_data:
+ for b in self._image_data[a]:
+ if spr == b[0]:
+ play_audio_from_file(b[1])
+ return
+
+ if spr in self._cards:
+ self.current_card = self._cards.index(spr)
+ play_audio_from_file(self._card_data[self.current_card][-1])
if self.current_card == self.target:
self._activity.status.set_text(_('Very good!'))
@@ -320,31 +315,53 @@ class Page():
def load_level(self, path):
''' Load a level (CSV) from path: letter, word, color, image,
image sound, letter sound '''
- self._card_data = []
- self._color_data = []
- self._image_data = []
- self._media_data = [] # (image sound, letter sound)
+ self._card_data = [] # (letter, word, letter_sound_path)
+ self._color_data = []
+ self._image_data = {} # {letter: [(Sprite, image_sound_path)...]}
+ self._pictures = []
f = codecs.open(path, encoding='utf-8')
for line in f:
if len(line) > 0 and line[0] not in '#\n':
words = line.split(', ')
- self._card_data.append([words[0],
- words[1].replace('-', ', ')])
+ self._card_data.append((words[0],
+ words[1].replace('-', ', '),
+ os.path.join(self._sounds_path, words[5])))
if words[2].count('#') > 1:
self._color_data.append(
[words[2].split('/')])
else:
self._color_data.append(
[words[2]])
- self._image_data.append(words[3])
- self._media_data.append((words[4], words[5]))
+ imagefilename = words[3]
+ imagepath = os.path.join(self._images_path, imagefilename)
+ pixbuf = image_file_to_pixbuf(imagepath, self._card_width,
+ self._card_height)
+ s = Sprite(self._sprites, 0, 0, pixbuf)
+ self._image_data[words[0]] = \
+ [(s, os.path.join(self._sounds_path, words[4]))]
+ self._pictures.append(s)
f.close()
-
self._clear_all()
self._cards = []
self._colored_letters_lower = []
self._colored_letters_upper = []
+ def load_from_journal(self, journal_data):
+ for card in self._card_data:
+ alphabet = card[0]
+ if alphabet in journal_data:
+ for images in journal_data[alphabet]:
+ imagedataobject = datastore.get(images[0])
+ audiodataobject = datastore.get(images[1])
+ if imagedataobject and audiodataobject:
+ imagepath = imagedataobject.get_file_path()
+ pixbuf = image_file_to_pixbuf(imagepath, self._card_width,
+ self._card_height)
+ audiopath = audiodataobject.get_file_path()
+ s = Sprite(self._sprites, 0, 0, pixbuf)
+ self._image_data[alphabet].append((s, audiopath))
+ self._pictures.append(s)
+
def _clear_all(self):
''' Hide everything so we can begin a new page. '''
self._hide_cards()