File: idbyname.py

package info (click to toggle)
solfege 2.0.4-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,200 kB
  • ctags: 1,844
  • sloc: python: 12,160; xml: 5,458; ansic: 1,486; makefile: 562; sh: 233
file content (207 lines) | stat: -rw-r--r-- 8,988 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# GNU Solfege - eartraining for GNOME
# Copyright (C) 2000, 2001, 2002, 2003  Tom Cato Amundsen
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import gtk, gnome
import gu, widgets
import abstract, const, mpd
import statistics, statisticsviewer
import lessonfile
import string
import dataparser
import soundcard


class Teacher(abstract.LessonbasedTeacher):
    OK = 0
    ERR_PICKY = 1
    def __init__(self, exname,  app, use=const.USE_ID_BY_NAME):
        abstract.LessonbasedTeacher.__init__(self, exname, app, use)
        self.lessonfileclass = lessonfile.IdByNameLessonfile
        try: # the lessonfile might contain errors.
            self.parse_lessonfile()
        except dataparser.DataparserException, e:
            self.m_app.m_ui.display_error_message(str(e))
        self.m_statistics = statistics.LessonStatistics(self)
    def give_up(self):
        self.q_status = const.QSTATUS_GIVE_UP
    def new_question(self):
        """
        UI will never call this function unless we have a usable lessonfile.
        """
        if self.m_timeout_handle:
            gtk.timeout_remove(self.m_timeout_handle)
            self.m_timeout_handle = None

        if self.get_bool('config/picky_on_new_question') \
                 and self.q_status in [const.QSTATUS_NEW, const.QSTATUS_WRONG]:
             return Teacher.ERR_PICKY

        self.q_status = const.QSTATUS_NO

        assert self.m_P
        self.m_P.select_random_question()
        self.q_status = const.QSTATUS_NEW
        return self.OK
    def guess_answer(self, answer):
        """
        Return: 1 if correct, None if wrong
        """
        if answer == self.m_P.get_untranslated_name():
            if self.q_status == const.QSTATUS_NEW:
                self.m_statistics.add_correct(answer)
            self.maybe_auto_new_question()
            self.q_status = const.QSTATUS_SOLVED
            return 1
        else:
            if self.q_status == const.QSTATUS_NEW:
                self.m_statistics.add_wrong(self.m_P.get_untranslated_name(),
                                            answer)
                self.q_status = const.QSTATUS_WRONG


class Gui(abstract.LessonbasedGui):
    def __init__(self, teacher, window):
        abstract.LessonbasedGui.__init__(self, teacher, window)
        ################
        # practise_box #
        ################
        self.g_bb = widgets.QuestionNameButtonTable()
        self.practise_box.pack_start(self.g_bb, gtk.FALSE)

        self.g_flashbar = gu.FlashBar()
        self.g_flashbar.show()
        self.practise_box.pack_start(self.g_flashbar, gtk.FALSE)
        self.practise_box.set_spacing(gnome.ui.PAD)

        self.g_new = gu.bButton(self.action_area, _("_New"), self.new_question)
        self.g_repeat = gu.bButton(self.action_area, _("_Repeat"),
                                   self.repeat_question)
        self.g_give_up = gu.bButton(self.action_area, _("_Give up"), self.give_up)
        self.g_give_up.set_sensitive(gtk.FALSE)

        self.g_show = gu.bButton(self.action_area, _("_Show"), self.show_answer)
        self.g_show.set_sensitive(gtk.FALSE)
        self.practise_box.show_all()
        ##############
        # config_box #
        ##############
        self.config_box.set_spacing(gnome.ui.PAD_SMALL)
        self.g_lessonfile_selector = widgets.SelectLessonfileWidget(
            self.m_t, self.g_win, self.update_gui_after_lessonfile_change)
        self.g_lessonfile_selector.show()
        self.config_box.pack_start(self.g_lessonfile_selector)
        # ------------------------------------------
        self._add_auto_new_question_gui(self.config_box)
        # ----------------------------------------------
        ##############
        # statistics #
        ##############
        self.setup_statisticsviewer(statisticsviewer.StatisticsViewer,
                                   _("Identify by name"))

        self.update_gui_after_lessonfile_change()
    def show_answer(self, widget=None):
        if self.m_t.q_status in (const.QSTATUS_SOLVED, const.QSTATUS_GIVE_UP):
            self.g_win.display_in_musicviewer(self.m_t.m_P.get_music())
    def update_answer_buttons(self):
        if self.m_t.m_P:
            self.g_bb.initialize(self.m_t.m_P.header.fillnum,
                                 self.m_t.m_P.header.filldir)
            for k in self.m_t.m_P.m_names_in_file_order:
                self.g_bb.add(k, self.m_t.m_P.m_names[k],
                      self.m_t.m_statistics.get_label_style(), self.on_click)
        else:
            self.g_bb.initialize(0, 0)
    def update_gui_after_lessonfile_change(self):
        if self.m_t.m_P:
            self.g_new.set_sensitive(gtk.TRUE)
        else:
            self.g_new.set_sensitive(gtk.FALSE)
        self.g_repeat.set_sensitive(gtk.FALSE)
        self.g_show.set_sensitive(gtk.FALSE)
        self.g_give_up.set_sensitive(gtk.FALSE)
        self.update_answer_buttons()
        self.g_win.set_title("Solfege - " + self.get_pretty_name())
    def give_up(self, _o=None):
        if self.m_t.q_status == const.QSTATUS_WRONG:
            self.g_flashbar.push(_("The answer is: %s") % self.m_t.m_P.get_name())
            self.m_t.give_up()
            self.g_show.set_sensitive(gtk.TRUE)
            self.g_new.set_sensitive(gtk.TRUE)
            self.g_new.grab_focus()
            self.g_give_up.set_sensitive(gtk.FALSE)
    def on_click(self, button):
        if self.m_t.q_status == const.QSTATUS_NO:
            self.g_flashbar.flash(_("Click 'New' to begin."))
            return
        if self.m_t.q_status == const.QSTATUS_SOLVED:
            if self.m_t.guess_answer(button.get_data('pcode')):
                self.g_flashbar.flash(_("Correct, but you have already solved this question"))
            else:
                self.g_flashbar.flash(_("Wrong, but you have already solved this question"))
        elif self.m_t.q_status in (const.QSTATUS_NEW, const.QSTATUS_WRONG):
            if self.m_t.guess_answer(button.get_data('pcode')):
                self.g_flashbar.flash(_("Correct"))
                self.g_new.set_sensitive(gtk.TRUE)
                self.g_new.grab_focus()
                self.g_show.set_sensitive(gtk.TRUE)
                self.g_give_up.set_sensitive(gtk.FALSE)
            else:
                self.g_flashbar.flash(_("Wrong"))
                if self.get_bool("config/auto_repeat_question_if_wrong_answer"):
                    self.m_t.play_question()
                self.g_give_up.set_sensitive(gtk.TRUE)
    def new_question(self, widget=None):
        # if we have no lessonfile, then we have to questions.
        if not self.m_t.m_P:
            return
        g = self.m_t.new_question()
        if g == self.m_t.OK:
            try:
                self.m_t.play_question()
            except mpd.MpdException, e:
                # since we could not play the new question, it is possible,
                # if the user are fast, that the last question is still playing.
                soundcard.synth.stop()
                self.g_win.display_question_music_error_message(
                    self.m_t.m_P._idx, self.get_string('lessonfile'), e)
                self.m_t.q_status = const.QSTATUS_NO
                self.g_repeat.set_sensitive(gtk.FALSE)
                self.g_show.set_sensitive(gtk.FALSE)
            else:
                self.g_repeat.set_sensitive(gtk.TRUE)
                self.g_new.set_sensitive(
                    not self.get_bool('config/picky_on_new_question'))
                self.g_bb.grab_focus_first_button()
                self.g_give_up.set_sensitive(gtk.FALSE)
                self.g_show.set_sensitive(gtk.FALSE)
                self.g_flashbar.clear()
    def repeat_question(self, *w):
        self.g_bb.grab_focus_first_button()
        self.m_t.play_question()
    def on_start_practise(self):
        self.g_new.grab_focus()
        self.m_t.m_statistics.reset_session()
        gtk.timeout_add(const.SHORT_WAIT, lambda self=self:
            self.g_flashbar.flash(_("Click 'New' to begin.")))
    def on_end_practise(self):
        self.m_t.end_practise()
        self.g_repeat.set_sensitive(gtk.FALSE)
        self.g_show.set_sensitive(gtk.FALSE)
        self.g_new.set_sensitive(gtk.TRUE)
        self.g_give_up.set_sensitive(gtk.FALSE)
        self.g_flashbar.clear()