From 0d17137aaff76290b0f497bf10885620454f0a5a Mon Sep 17 00:00:00 2001 From: Sai Vineet Date: Wed, 04 Dec 2013 15:58:45 +0000 Subject: Export body positions in Physics Enhancement: Physics should let the user export the body positions for use in another activity like Chart, etc. --- (limited to 'activity.py') diff --git a/activity.py b/activity.py index 9994dea..eef0e36 100644 --- a/activity.py +++ b/activity.py @@ -22,6 +22,8 @@ import os import gtk from gettext import gettext as _ import logging +import csv +import tempfile import pygame import sugargame @@ -36,6 +38,7 @@ from sugar.graphics.alert import ConfirmationAlert from sugar.graphics.toolbarbox import ToolbarBox from sugar.graphics.toolbarbox import ToolbarButton from sugar.graphics.style import GRID_CELL_SIZE +from sugar.datastore import datastore import tools import physics @@ -120,6 +123,12 @@ class PhysicsActivity(activity.Activity): self._insert_stop_play_button(toolbar_box.toolbar) + export_csv = ToolButton("document-save") + export_csv.set_tooltip(_("Export tracked objects to journal")) + export_csv.connect('clicked', self._export_csv_cb) + toolbar_box.toolbar.insert(export_csv, -1) + export_csv.show() + separator = gtk.SeparatorToolItem() toolbar_box.toolbar.insert(separator, -1) separator.show() @@ -203,8 +212,9 @@ class PhysicsActivity(activity.Activity): def clear_all_cb(self, button): clear_all_alert = ConfirmationAlert() - clear_all_alert.props.title=_('Are You Sure?') - clear_all_alert.props.msg = _('All you work will be discarded. This cannot be undone!') + clear_all_alert.props.title = _('Are You Sure?') + clear_all_alert.props.msg = \ + _('All you work will be discarded. This cannot be undone!') clear_all_alert.connect('response', self.clear_all_alert_cb) self.add_alert(clear_all_alert) @@ -216,6 +226,23 @@ class PhysicsActivity(activity.Activity): else: pass + def _export_csv_cb(self, button): + jobject = datastore.create() + jobject.metadata['title'] = _("Physics export") + jobject.metadata['mime_type'] = "text/csv" + + tmp_dir = os.path.join(self.get_activity_root(), 'instance') + fd, file_path = tempfile.mkstemp(dir=tmp_dir) + os.close(fd) + + data = self.game.full_pos_list + csvfile = open(file_path, "wb") + writer = csv.writer(csvfile) + writer.writerows(data) + jobject.set_file_path(os.path.abspath(csvfile.name)) + csvfile.close() + datastore.write(jobject) + def radioClicked(self, button): pygame.event.post(pygame.event.Event(pygame.USEREVENT, action=self.radioList[button])) -- cgit v0.9.1