File: statisticsviewer.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 (203 lines) | stat: -rw-r--r-- 9,104 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
# 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 utils, gtk, gu, widgets
import gnome
import os


class MatrixTable(gtk.VBox):
    def __init__(self, label_style, heading, keys, key_to_pretty_name, dict):
        gtk.VBox.__init__(self)
        label = gtk.Label(heading)
        label.set_name("Heading2")
        label.set_alignment(0.0, 0.0)
        self.pack_start(label, gtk.FALSE)
        hbox = gu.bHBox(self, gtk.FALSE)
        frame = gtk.Frame()
        hbox.pack_start(frame, gtk.FALSE)
        t = gtk.Table()
        frame.add(t)
        for x in range(len(keys)):
            t.attach(gtk.VSeparator(), x*2+1, x*2+2, 0, len(keys)*2)
        for x in range(len(keys)-1):
            t.attach(gtk.HSeparator(), 0, len(keys)*2+1, x*2+1, x*2+2)
        for y in range(len(keys)):
            if label_style == 'progression':
                l = widgets.HarmonicProgressionLabel(
                                     key_to_pretty_name(keys[y]), 'left')
            else:
                l = gtk.Label(key_to_pretty_name(keys[y]))
                l.set_alignment(0.0, 0.5)
            t.attach(l, 0, 1, y*2, y*2+1, xpadding=gnome.ui.PAD)
            for x in range(len(keys)):
                if dict.has_key(keys[x]) and dict[keys[x]].has_key(keys[y]):
                    s = str(dict[keys[x]][keys[y]])
                else:
                    s = "0"
                l = gtk.Label(s)
                if x == y:
                    l.set_name('StatisticsViewerDiagonal')
                t.attach(l, x*2+2, x*2+3, y*2, y*2+1, xpadding=gnome.ui.PAD)
        self.show_all()


class PercentagesTable(gtk.Frame):
    def __init__(self, statistics):
        gtk.Frame.__init__(self)
        table = gtk.Table()
        self.add(table)

        self.boxdict = {}
        for k, l, x in (('session', _("Session"), 2), ('today', _("Today"), 5),
                     ('last7', _("Last 7 days"), 8), ('total', _("Total"), 11)):
            table.attach(gtk.Label(l), x, x+2, 0, 1)
            b = gtk.VBox()
            table.attach(b, x, x+1, 4, 5)
            self.boxdict[k+'percent'] = b
            b = gtk.VBox()
            table.attach(b, x+1, x+2, 4, 5)
            self.boxdict[k+'count'] = b
        for x in (2, 5, 8, 11):
            table.attach(gtk.Label(_("Percent")), x, x+1, 2, 3)
            table.attach(gtk.Label(_("Count")), x+1, x+2, 2, 3)
        table.attach(gtk.HSeparator(), 0, 13, 1, 2)
        table.attach(gtk.HSeparator(), 0, 13, 3, 4)
        table.attach(gtk.VSeparator(), 1, 2, 0, 6)
        table.attach(gtk.VSeparator(), 4, 5, 0, 6)
        table.attach(gtk.VSeparator(), 7, 8, 0, 6)
        table.attach(gtk.VSeparator(), 10, 11, 0, 6)
        self.boxdict['keys'] = key_box = gtk.VBox()
        table.attach(key_box, 0, 1, 4, 5)
        for box in self.boxdict.values():
            box.set_border_width(gnome.ui.PAD_SMALL)
        self.update(statistics)
        self.show_all()
    def update(self, statistics):
        for box in self.boxdict.values():
            for o in box.get_children():
                o.destroy()
        for k in statistics.get_keys():
            # FIXME  Has this bug been fixed? This should NEVER happen.
            if k == None:
                print "WARNING: statistics: key==None. Please, please report to"
                print "bug-solfege@gnu.org if you get this message. Preferably,"
                print "make a copy ~/.solfege and ~/.gnome/solfegeX.Y so I can"
                print "debug this."
            if statistics.get_label_style() == 'progression':
                l = widgets.HarmonicProgressionLabel(statistics.key_to_pretty_name(k), 'left')
            else:
                l = gtk.Label(statistics.key_to_pretty_name(k))
                l.set_alignment(0.0, 0.5)
            self.boxdict['keys'].pack_start(l)
            for sk, st in (('session', statistics.m_session_stat),
                       ('today', statistics.m_today_stat),
                       ('last7', statistics.m_last7_stat),
                       ('total', statistics.m_total_stat)):

                self.boxdict[sk+'percent'].pack_start(
                    gtk.Label("%.0f" % statistics.get_percentage_correct(st, k)))
                self.boxdict[sk+'count'].pack_start(
                    gtk.Label(str(statistics.get_num_guess(st, k))))
        self.show_all()

class NewAbstractStatisticsViewer(gtk.ScrolledWindow):
    def __init__(self, statistics, heading):
        gtk.ScrolledWindow.__init__(self)
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.vbox = gtk.VBox()
        self.vbox.set_spacing(gnome.ui.PAD)
        self.vbox.set_border_width(gnome.ui.PAD)
        self.add_with_viewport(self.vbox)
        label = gtk.Label(heading)
        label.set_alignment(0.0, 0.5)
        label.set_name("Heading1")
        self.vbox.pack_start(label, gtk.FALSE)
        self.m_statistics = statistics
        self.g_tables = gtk.VBox()
        self.g_tables.show()
        self.vbox.pack_start(self.g_tables)
        self.show_all()

class SingIntervalStatisticsViewer(NewAbstractStatisticsViewer):
    def __init__(self, statistics, heading):
        NewAbstractStatisticsViewer.__init__(self, statistics, heading)
        #self.clear = self.g_p.clear
    def clear(self):
        #UGH why cant we just destroy the children of g_tables??!!
        #for c in self.g_tables.children():
        #    c.destroy()
        self.g_tables.destroy()
        self.g_tables = gtk.VBox()
        self.g_tables.show()
        self.vbox.pack_start(self.g_tables)
    def update(self):
        self.clear()
        self.g_p = PercentagesTable(self.m_statistics)
        self.g_p.show_all()
        self.g_tables.pack_start(self.g_p, gtk.FALSE, gtk.FALSE)



class StatisticsViewer(NewAbstractStatisticsViewer):
    def __init__(self, statistics, heading):
        NewAbstractStatisticsViewer.__init__(self, statistics, heading)
        self.matrix_dict = {}
    def clear(self):
        #UGH why cant we just destroy the children of g_tables??!!
        #for c in self.g_tables.children():
        #    c.destroy()
        self.g_tables.destroy()
        self.g_tables = gtk.VBox()
        self.g_tables.show()
        self.vbox.pack_start(self.g_tables)
    def update(self):
        self.clear()
        self.g_p = PercentagesTable(self.m_statistics)
        self.g_p.show_all()
        self.g_tables.pack_start(self.g_p, gtk.FALSE)
        self.matrix_dict['session'] = MatrixTable(
                                           self.m_statistics.get_label_style(), 
                                           _("Session"),
                                           self.m_statistics.get_keys(1),
                                           self.m_statistics.key_to_pretty_name,
                                           self.m_statistics.m_session_stat)
        self.matrix_dict['today'] = MatrixTable(
                                           self.m_statistics.get_label_style(),
                                           _("Today"),
                                           self.m_statistics.get_keys(1),
                                           self.m_statistics.key_to_pretty_name,
                                           self.m_statistics.m_today_stat)
        self.matrix_dict['last7'] = MatrixTable(
                                           self.m_statistics.get_label_style(),
                                           _("Last 7 days"),
                                           self.m_statistics.get_keys(1),
                                           self.m_statistics.key_to_pretty_name,
                                           self.m_statistics.m_last7_stat)
        self.matrix_dict['total'] = MatrixTable(
                                           self.m_statistics.get_label_style(),
                                           _("Total"),
                                           self.m_statistics.get_keys(1),
                                           self.m_statistics.key_to_pretty_name,
                                           self.m_statistics.m_total_stat)

        for k in ('session', 'today', 'last7', 'total'):
            self.g_tables.pack_start(self.matrix_dict[k], gtk.FALSE)
    #def transform_key(self, k):
    #    return utils.int_to_intervalname(k, 0, 0)

StatisticsViewer = StatisticsViewer