Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ReckonPrimer.activity/exstore.py
blob: 412c9d308978b632564b56b6d72a579adc485684 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-

import gtk
import pygtk
import pango
import random
import copy
from sugar.graphics import style

from settings import Settings #WN.100112 remove with "treeView-Kollektion"
from coach import Coach
from collection import Collection
from task import Task

from exercises.exaddsimp import ExAddSimp
from exercises import *
from exercises.exercise import Exercise


class ExStore:
    """ Holds all exercises avaiable in RP in a tree and stores them on disk.
    The tree ist presented looking like a filebrowser, see Display.treeview.
    The Learner starts an Exercise by selecting an item of the tree.
    The Author creates new Exercises copying certain Exercises by 
    drag-and-drop and then editing the title, the description and the settings.
    
    """
    _data = None #hold the tree with root of Collection and leafs of Task
    def __init__(self, display, coach):
        self._sett = Settings()
        #Provisorium für die "3-Button-Version" vvvvvvvvvvvvvvvvvvvvvvvvv
        self._data = [(self._sett.get_setting('addsub_simp'), []),
                      (self._sett.get_setting('passten'),     []),
                      (self._sett.get_setting('times_div'),   [])]
        #Provisorium für die "3-Button-Version" ^^^^^^^^^^^^^^^^^^^^^^^^^
        #Provisorium für impl. "treeView-Kollektion" vvvvvvvvvvvvvvvvvvvv
        _ex000 = Task({'topic'        : 'times_div', #for programmers only
         'title' : 'template extimesdiv',
         'descript': 'multiplication, division and "in" \
                            from 2 to 19 and 20 to 190. ',
         'icon' : None,     # for quick reference of the exercise      
         'calclines'    : 1,      # no. of lines for calc to be input.
         'MAX'          : 100,    # maximum of calcs generated;
                                  # TODO: Generate fills up by varying input.
         'MIN'          : 10,     # minimum of calcs generated 090416WN:UNUSED
         '*'            : True,   # eg.  7 . 2 =_
         '*commute'     : True,  # commute the operands 2 . 7 = _
         ':'            : False,  # 14 : 2 = _
         'in'           : True,  # 2 in 14 = _
         'remainder'    : False,  # : | in ... with remainder
         'min'          : 2,      # +: minimum number in right *operand
                                  # -: minimum result
         'max'          : 2,      # +: maximum number in right *operand
                                  # -: maximum result
         'shuffle_all'  : False,   # shuffle all calcs  
         'shuffle_inner': True,   # shuffle only 1st (inner) iteration
         'cut-max'      : True   # cut set of all calcs down to MAX
        }, [])
        _ex001 = Task({'topic'        : 'times_div', #for programmers only
         'title' : 'template extimesdiv',
         'descript': 'multiplication, division and "in" \
                            from 2 to 19 and 20 to 190. ',
         'icon' : None,     # for quick reference of the exercise      
         'calclines'    : 1,      # no. of lines for calc to be input.
         'MAX'          : 100,    # maximum of calcs generated;
                                  # TODO: Generate fills up by varying input.
         'MIN'          : 10,     # minimum of calcs generated 090416WN:UNUSED
         '*'            : True,   # eg.  7 . 2 =_
         '*commute'     : True,  # commute the operands 2 . 7 = _
         ':'            : False,  # 14 : 2 = _
         'in'           : True,  # 2 in 14 = _
         'remainder'    : False,  # : | in ... with remainder
         'min'          : 3,      # +: minimum number in right *operand
                                  # -: minimum result
         'max'          : 3,      # +: maximum number in right *operand
                                  # -: maximum result
         'shuffle_all'  : False,   # shuffle all calcs  
         'shuffle_inner': True,   # shuffle only 1st (inner) iteration
         'cut-max'      : True   # cut set of all calcs down to MAX
        }, [])
        _coll00 = Collection("einzeln * in",
                             "hier sind alle 1x1-Reihen mit 'in'",
                             None, [_ex000, _ex001])
        _coll0 = Collection("'*' und 'in' Reihen",
                             "hier sind alle 1x1-Reihen mit 'in'",
                             None, [_coll00])
        _coll0.set_data_in_pickle(_coll0) #WN.100112 remove asap
        #self._data = _coll0
        #Provisorium für impl. "treeView-Kollektion" ^^^^^^^^^^^^^^^^^^^^
        
        self._display = display
        self._coach = coach
        self._active_exerc = None
        #self.__data = []
        #self.__title = "Collection"
        #self.__description = "Description"
        self.imagecollection = gtk.gdk.pixbuf_new_from_file(
            "./img/collection.png")
        self.imageexercise = gtk.gdk.pixbuf_new_from_file(
            "./img/exercise.png")

    def select(self, key):
        """ Select an exercise by key. return instance of an exercise.
        Errors are retrieved for (future) use by Coach. """
        #WN.LV Code ersetzen: key ist dann fuer Listen von Listen !!
        (_sett, _errors) = self._data[key]
        exercise_label = _sett['topic']

        if not Exercise.EXERCISES.has_key(exercise_label):
            raise Exception('Collection#select: Wrong key. To register an exercise see exercises/__init__.py')
    
        klass = Exercise.EXERCISES[exercise_label]
        package = __import__("exercises." + klass.lower())
        module = getattr(package, klass.lower())
        return getattr(module, klass)(self._display, (_sett, _errors))    


    #@# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    def define_coll_gui(self):
        """ Define gui-elements for presenting the collection.
        TODO: define once at startup of RP ?"""
        #WN.LV diesen Code ersetzen: exstore_table.attach(self.colldata,...
        self.topic_box = gtk.HBox(True, 0)
        #self._display.exstore_table.attach(self.topic_box, 0, 6, 0, 1)
        self._display.exstore_table.attach(self.topic_box, 0, 9, 5, 6)

    def set_coll_gui(self, coll_data):
        """ Set gui-elements according to Collection.data. """
        #WN.LV diesen Code ersetzen !!!!!
        _i = 0
        for _t in ['addsub_simp','passten','times_div']:
            self.button = gtk.Button()
            self.image = gtk.Image()
            
            if(_t == 'addsub_simp'):
                self.image.set_from_file("img/addsub_simp.jpg")
            elif(_t == 'passten'):
                self.image.set_from_file("img/passten.jpg")
            elif(_t == 'times_div'):
                self.image.set_from_file("img/times_div.jpg")

            self.button.set_image(self.image)
            self.button.connect("clicked", self.select_exerc_callback, _i)
            self.topic_box.pack_start(self.button)
            self.button.show()
            _i = _i + 1
        self.topic_box.show()
        #@# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         
    def select_exerc_callback(self, widget, coll_key):
        """ Callback telling the item from the collection selected. """
        #WN.LV diesen Code ersetzen> statt 1..n fuer Listen kommt coll_key
        print('in Collection.select_exerc_callback')
        if(self._active_exerc == None):
            # at startup
            self._active_exerc = coll_key
            self._display.set_select_exerc_semaphore(coll_key)
            #self._coach.notify(('exerc-selected', coll_key))
        elif(self._active_exerc == coll_key):
            # hit the same button once more
            pass
        elif(self._active_exerc != coll_key):
            # switched to another exercise
            self._active_exerc = coll_key
            self._display.switch_exercise() # TODO rename
            self._display.set_select_exerc_semaphore(coll_key)
            #self._coach.notify(('exerc-selected', coll_key))