File: selectlessonfilewidget.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 (172 lines) | stat: -rw-r--r-- 7,549 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
# 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, gu
from htmlwidget import HtmlWidget
import dataparser

class SelectLessonfileWidget(gtk.Frame):
    """
    The class will parse call the teacher to parse the
    lessonfile because the widget need some data from the
    lessonfile header.
    """
    def __init__(self, teacher, win, change_cb):
        gtk.Frame.__init__(self, _("Lesson file"))
        self.m_t = teacher
        self.m_ui = win
        self.change_cb = change_cb
        vbox = gtk.VBox()
        vbox.set_border_width(gnome.ui.PAD)
        vbox.set_spacing(gnome.ui.PAD)
        self.add(vbox)
        # the box the optionmenus go into
        hbox = gu.bHBox(vbox, gtk.FALSE)
        hbox.set_homogeneous(gtk.TRUE)

        self.g_collection = gtk.OptionMenu()
        hbox.pack_start(self.g_collection)

        self.g_files = gtk.OptionMenu()
        hbox.pack_start(self.g_files)

        # descriptions
        hbox = gu.bHBox(vbox, gtk.FALSE)
        gu.bLabel(hbox, _("Title")+": ", gtk.FALSE)
        self.g_title = gu.bLabel(hbox, "", gtk.FALSE)

        self.g_toggle_description = gtk.ToggleButton(_("Details"))
        self.g_toggle_description.connect('clicked', self.toggle_lessonfile_details)
        hbox.pack_end(self.g_toggle_description, gtk.FALSE, gtk.FALSE)
        self.g_description_frame = gtk.Frame()
        self.g_lessonfile_details = HtmlWidget(self.m_t.m_app.handle_href, None)
        self.g_description_frame.add(self.g_lessonfile_details)
        vbox.pack_start(self.g_description_frame)
        self.update()
        self._sc = self._sf = None
        self.connect_changed_signals()
        self._fw = self.m_t.add_watch('lessonfile', self._lessonfilewatch)
        self._cw = self.m_t.add_watch('lessoncollection', self._lessoncollectionwatch)
    def connect_changed_signals(self):
        assert self._sc == self._sf == None
        self._sc = self.g_collection.connect('changed', self.on_change_collection)
        self._sf = self.g_files.connect('changed', self.on_change_file)
    def disconnect_changed_signals(self):
        assert self._sc
        assert self._sf
        self.g_collection.disconnect(self._sc)
        self.g_files.disconnect(self._sf)
        self._sc = self._sf = None
    def _lessonfilewatch(self, s):
        self.do_change_file(self.m_t.get_string(s))
        v = self.m_t.m_app.lessonfile_manager.get_filenames(
           self.m_t.get_string('lessoncollection'), self.m_t.m_content_type
           )
        self.g_files.set_history(v.index(self.m_t.get_string(s)))
    def _lessoncollectionwatch(self, s):
        self.do_change_collection(self.m_t.get_string(s))
    def on_change_collection(self, widget=None):
        collection = self.m_t.m_app.lessonfile_manager.get_collections()[widget.get_history()]
        self.do_change_collection(collection)
    def do_change_collection(self, collection):
        self.disconnect_changed_signals()
        self.m_t.remove_watch('lessonfile', self._fw)
        self.m_t.remove_watch('lessoncollection', self._cw)
        try:
            self.m_t.set_lessoncollection(collection)
        except dataparser.DataparserException, e:
            self.m_ui.display_error_message(str(e))
            self.m_t.m_P = None
        self.update(1)
        self.change_cb()
        self._cw = self.m_t.add_watch('lessoncollection', self._lessoncollectionwatch)
        self._fw = self.m_t.add_watch('lessonfile', self._lessonfilewatch)
        self.connect_changed_signals()
    def on_change_file(self, widget):
        """This function is hooked up to the changed signal for the option menu
        """
        new_filename = self.m_t.m_app.lessonfile_manager.get_filenames(
           self.m_t.get_string('lessoncollection'), self.m_t.m_content_type
           )[widget.get_history()]
        self.do_change_file(new_filename)
    def do_change_file(self, new_filename):
        self.disconnect_changed_signals()
        self.m_t.remove_watch('lessonfile', self._fw)
        try:
            self.m_t.set_lessonfile(new_filename)
        except dataparser.DataparserException, e:
            self.m_ui.display_error_message(str(e))
            self.m_t.m_P = None
        self.update(2)
        self.change_cb()
        self._fw = self.m_t.add_watch('lessonfile', self._lessonfilewatch)
        self.connect_changed_signals()
    def update(self, level=0):
        """0: everything
           1: don't include collection combo
           2: only information label
        """
        if level == 0:
            if not self.g_collection.get_menu():
                menu = gtk.Menu()
                for coll in self.m_t.m_app.lessonfile_manager.get_collections():
                    menu.add(gtk.MenuItem(coll))
                self.g_collection.set_menu(menu)
                self.g_collection.set_history(self.m_t.m_app.lessonfile_manager.get_collections().index(self.m_t.get_string('lessoncollection')))
        if level <= 1:
            F = self.m_t.m_app.lessonfile_manager.get_filenames( \
                    self.m_t.get_string('lessoncollection'), self.m_t.m_content_type)
            if F:
                menu = self.g_files.get_menu()
                if menu:
                    self.g_files.remove_menu()
                    menu.destroy()
                menu = gtk.Menu()
                for lessonfile in F:
                    menu.add(gtk.MenuItem(lessonfile))
                self.g_files.set_menu(menu)
                menu.show_all()
                self.g_files.set_sensitive(gtk.TRUE)
                self.g_files.set_history(self.m_t.m_app.lessonfile_manager.get_filenames(self.m_t.get_string('lessoncollection'), self.m_t.m_content_type).index(self.m_t.get_string('lessonfile')))
            else:
                menu = self.g_files.get_menu()
                if menu:
                    self.g_files.remove_menu()
                    menu.destroy()
                self.g_files.set_sensitive(gtk.FALSE)
                self.g_title.set_text('')
                self.g_lessonfile_details.source("<html></html>")
        if not self.m_t.m_P:
            return
        # the info labels
        if self.m_t.m_P:
            self.g_title.set_text(self.m_t.m_P.header.title)
        else:
            self.g_title.set_text('')
        s = self.m_t.m_P.header.description
        if s:
            self.g_lessonfile_details.source("<html><body bgcolor=white>%s</body></html>" % s)
        else:
           self.g_lessonfile_details.source("<html></html>")
    def toggle_lessonfile_details(self, widget=None):
        if widget.get_active():
            self.g_description_frame.show()
        else:
            self.g_description_frame.hide()
    def show(self):
        self.show_all()
        self.g_description_frame.hide()