Beispiel #1
0
class Controller(object):
    
    def editList(self):
        EditListWindow(self)
    
    def startNewList(self):
        NewListToplevel(self.views[0], self)
    
    def speakWord(self, word):
        self.festival.speak(word.name)
        
    def speakWords(self):
        if self.fromDictionary:
            for word in self.getWordsFromDictionary(self.listSelected): self.speakWord(word)
        else:
            for word in self.getWordsFromSpellingList(self.listSelected): self.speakWord(word)
            
    def stopFestival(self):
        self.festival.reset()
    
    def newListCreated(self, listName, numWords, dictionaryName):
        """listName = name of the new list
        numWords = number of words to randomly select
        dictionaryName = Name of the dictionary to select from"""
        wordsList = []
        
        if numWords > 0:
            wordsList = self.dictionaryDB.getRandomWordsFromList(dictionaryName, numWords)
        
        #Insert new list into DB
        self.spellingListDB.insertWords(wordsList, listName)
        
        for view in self.views:
            view.notifyDataSetChanged(wordsList)
    
    def __init__(self):
        self.dictionaryDB = DictionaryDatabase()
        self.spellingListDB = SpellingListDatabase()
        self.views = []
        self.festival = FestivalController()
    
    def addView(self, view):
        self.views.append(view)
        
    def addModel(self, model):
        self.views.append(model)
    
    def onListSelected(self, listbox, fromDictionary = False):
        self.listSelected = listbox.get(listbox.curselection())
        self.fromDictionary = fromDictionary
    
    def changeToDictionary(self):
        self.writeTLDR(path = self.listSelected)
        self.readTLDR(path = self.listSelected)
        #remove("%s" % self.listSelected)
    
    def writeTLDR(self, path=None):
        if path == None:
            import tkFileDialog
            path = tkFileDialog.asksaveasfilename(initialfile=self.listSelected, filetypes=[("Word List", ".tldr")])
        writeTLDR(open(path, 'w'), self.getWordsFromSpellingList(self.listSelected))

    def readTLDR(self, path=None):
        if path == None:
            import tkFileDialog
            path = tkFileDialog.askopenfiles(mode='r', filetypes=[("Word List", ".tldr")])
        else:
            path = [path.encode('utf-8')]
        for f in path:
            wordsInFile = readTLDR(open(f))
            fileName = f.split("/")[-1].split(".")[0]
            self.dictionaryDB.updateWords(wordsInFile, fileName)
        for view in self.views:
            view.notifyDataSetChanged([])

    def deleteList(self):
        if self.listSelected in ["Festival", "1000 Common Words", "Essential Spelling Lists"]:
            tkMessageBox.showwarning(parent=self.views[0], title="Warning", \
                message="Cannot delete a default dictionary.")
            return
        db = self.dictionaryDB if self.fromDictionary else self.spellingListDB
        db.deleteList(self.listSelected)
        for view in self.views:
            view.notifyDataSetChanged([])

    def updateWords(self, words):
        db = self.dictionaryDB if self.fromDictionary else self.spellingListDB
        db.updateWords(words, self.listSelected)
        for view in self.views:
            view.notifyDataSetChanged(words)

    def getDictionaryNames(self):
        return self.dictionaryDB.getListNames()
        
    def getWords(self):
        db = self.dictionaryDB.openDictionary if self.fromDictionary else self.spellingListDB.openSpellingList
        return db(self.listSelected)
    
    def getWordsFromDictionary(self, dictionaryName):
        return self.dictionaryDB.openDictionary(dictionaryName)

    def getSpellingListNames(self):
        return self.spellingListDB.getListNames()
        
    def getWordsFromSpellingList(self, spellingListName):
        return self.spellingListDB.openSpellingList(spellingListName)
Beispiel #2
0
 def __init__(self):
     self.dictionaryDB = DictionaryDatabase()
     self.spellingListDB = SpellingListDatabase()
     self.views = []
     self.festival = FestivalController()