Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mathactivity.py
blob: 427e6535e7c4f71bbea2ba507521537b007c67e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright 2008 by Peter Moxhay and Wade Brainerd.  
# This file is part of Math.
#
# Math 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 3 of the License, or
# (at your option) any later version.
# 
# Math 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 Math.  If not, see <http://www.gnu.org/licenses/>.
"""Math - Software for developing math concepts."""

# Import standard Python modules.
import logging, os, math, time, copy, json, random 
from gettext import gettext as _

# Import PyGTK.
import gobject, pygtk, gtk, pango, cairo

# Import Sugar UI modules.
import sugar.activity.activity
from sugar.graphics import toggletoolbutton
from sugar.graphics import *

# Import Math modules.
from vector import Vector
import testlesson, compare3lesson

# Change to bundle directory.
bundle_path = sugar.activity.activity.get_bundle_path() 
os.chdir(bundle_path)
        
# Initialize logging.
log = logging.getLogger('Math')
log.setLevel(logging.DEBUG)
logging.basicConfig()

class MathActivity(sugar.activity.activity.Activity):
    """The main activity class which creates the user interface, manages loading and saving state,
       and controls the flow of the activity."""
    
    def __init__(self, handle):
        sugar.activity.activity.Activity.__init__(self, handle)
        self.set_title(_("Math"))
        
        # The data dictionary is saved to and loaded from the Journal.  All data about the state of the activity
        # which should be remembered goes in here.
        self.data = {}
        
        # Create screens.
        self.areascreen = compare3lesson.Compare3Lesson(self)
        
        # Initialize the screen stack.          
        self.screens = []
        self.screenbox = gtk.VBox()
        
        # Start with the area screen.
        self.push_screen(self.areascreen)
        
        # Initialize toolbar menus.        
        self.build_toolbox()
        
        # This has to happen last, because it calls the read_file method when restoring from the Journal.
        # The set_canvas call sets the widget that covers the main activity window.
        self.set_canvas(self.screenbox)
        
        # Make everything visible.
        self.show_all()
        
        # Hide the sharing button from the activity toolbar since we don't support sharing (yet). 
        activity_toolbar = self.tbox.get_activity_toolbar()
        activity_toolbar.share.props.visible = False

    def build_toolbox(self):
        self.tbox = sugar.activity.activity.ActivityToolbox(self)
        self.tbox.show_all()
        
        self.set_toolbox(self.tbox)
    
    # --------------------------------------------------------------------------------------------
    # Screen stack
    #
    # The activity stores the active set of screens as a stack.  To open a new screen, push the
    # screen onto the stack, and when the screen should be closed, pop it from the stack.
    #
    # For example, if the user starts the activity, chooses a lesson, and then finishes the
    # lesson and views a certificate award screen, the stack might look like:
    #
    # MainScreen -> LessonScreen -> CertificateScreen
    
    def push_screen(self, screen):
        """Pushes a new screen onto the screen stack.  The new screen will become visible
           and the previous top screen will be hidden until the new screen is popped."""
        if len(self.screens):
            self.screenbox.remove(self.screens[-1])
        
        self.screenbox.pack_start(screen, True, True)
        self.screens.append(screen)

    def pop_screen(self):
        """Pops the top screen off the screen stack.  The current top screen will
           be destroyed, and the next screen on the stack will become visible."""
        self.screenbox.remove(self.screens[-1])
        self.screens.pop()
        if len(self.screens):
            self.screenbox.pack_start(self.screens[-1])

    # --------------------------------------------------------------------------------------------
    # Journal interaction
    
    def read_file(self, file_path):
        """Called when the user resumes the activity from the Journal."""
        if self.metadata['mime_type'] != 'text/plain':
            return
        
        fd = open(file_path, 'r')
        try:
            text = fd.read()
            self.data = json.read(text)
        finally:
            fd.close()

    def write_file(self, file_path):
        """Called when the user saves the activity to the Journal."""
        if not self.metadata['mime_type']:
            self.metadata['mime_type'] = 'text/plain'
        
        fd = open(file_path, 'w')
        try:
            text = json.write(self.data)
            fd.write(text)
        finally:
            fd.close()

# vim: et sw=4