Exemplo n.º 1
0
class PyAeroMainGUI (PyAeroGUI):
    def __init__(self, qcm=None, length=30, start=True, chapters=[]):

        if isinstance(qcm, str):
            self.playing = True
            self.qcm = XmlQCM(qcm)
            self.length = -1
        elif isinstance(qcm, XmlQCM):
            self.playing = True
            self.qcm = qcm
            self.length = qcm.length
        elif isinstance(qcm, dict):
            self.playing = False
            self.qcm = qcm.items()
            self.length = len(qcm)
            start = False
        else:
            print "unknwon type: ", type(qcm)
            
        filename = os.path.join(os.path.dirname(__file__), interface_file)

        self.xml = gtk.glade.XML(filename, root="pymachin_window")
        self.xml.signal_autoconnect(self)

        self.main_window = self.xml.get_widget("pymachin_window")
        
        self.qbody_txtview = self.xml.get_widget("q_textview")

        self.labels = {}
        self.chkbox = {}

        self.qcount = 0
        self.score = 0
        self.qcurrent = None

        self.qfailed = {}
        
        self.qpossibilities = ['a', 'b', 'c', 'd']
		
        for name in self.qpossibilities:
            self.labels[name] = self.xml.get_widget("label_%s" %name)
            self.chkbox[name] = self.xml.get_widget("chkbtn_%s" %name)

        if self.playing:
            self.qcm.select(number=length, chapters=chapters)
            self.length = self.qcm.length
            if start:
                self.display_next_question()
        else:
            self.replayed_idx = 0
            self.replay_questions()

    def replay_questions(self):
        quest = self.qcm[self.replayed_idx]
        self.replayed_idx += 1
        self.display_question(quest[0], True)

        for name in quest[1]:
            self.chkbox[name].set_active(True)

        
        
    def display_question(self, question=None, displayScore=False):
        if question == None:
            question = self.qcurrent

        self.clear_answers()
        self.qbody_txtview.get_buffer().set_text(question.qbody)

        k = self.labels.keys()[:len(question.answers)]

        for idx,ans in question.answers.items():
            if displayScore:
                self.labels[idx].set_text("%s [%d]" %(ans[0], ans[1]))
            else:
                self.labels[idx].set_text(ans[0])
        

    def display_next_question(self):
        self.qcount += 1

        self.qcurrent = self.qcm.getNext()
        self.display_question()
        

    def clear_answers(self):
        for name in self.qpossibilities:
            self.labels[name].set_text("")
            self.chkbox[name].set_active(False)
        

    def on_ok_button_clicked(self, widget):
        if self.playing :
            if self.qcurrent != None:
                t_score = 0
                answered_idx = []
            
                for idx,ans in self.qcurrent.answers.items() :
                    if self.chkbox[idx].get_active():
                        t_score += ans[1]
                        answered_idx += [idx]
                    

                # negative score not possible. At most 6, at least 0 :)
                if t_score > 0:
                    self.score += t_score

                if t_score != 6 :
                    self.qfailed[self.qcurrent] = answered_idx

                if self.qcount != self.length:
                    self.display_next_question()
                else:
                    disp_score = PyAeroDisplayScore(self.score, self.qcount, self.qfailed)
                    self.main_window.destroy()

        else: ## not playing, replaying
            if self.replayed_idx != len(self.qcm):
                self.replay_questions()
            else:
                self.main_window.destroy()