fixed word generation error
[conlangery:conlangery.git] / DictionaryTab.py
1 # -*- coding: utf-8 -*-
2 import sys
3 from PyQt4 import QtGui, QtCore
4 from tabs import *
5 from data import Data
6
7 '''
8 This class defines the data field 'Dictionary', which is defined as
9 the following.
10
11 'Dictionary' is a dictionary. Its keys are words, defined as a string
12 of graphemes. Its values are lists.
13
14 Each of these lists has three strings. They are as follows:
15 at index:
16     0 - The meaning of the word
17     1 - The part of speech
18     2 - Additional notes
19 '''
20
21 class DictionaryTab(Tab):
22     ''' This class defines the content in tab 2 '''
23     currentWord = None
24     def __init__(self, mainWindow):
25         Tab.__init__(self, mainWindow)
26         
27         grid = QtGui.QGridLayout()
28         grid.setSpacing(10)
29         
30         list = self.list = QtGui.QListWidget(self)
31         grid.addWidget(list, 0, 0, 4, 1)
32         
33         self.writtenLabel = QtGui.QLabel("Written")
34         self.writtenLabel.setTextFormat(0)
35         self.writtenLabel.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction)
36         grid.addWidget(self.writtenLabel, 0, 2)
37         
38         self.phonemicLabel = QtGui.QLabel("Phonemic")
39         self.phonemicLabel.setTextFormat(0)
40         self.phonemicLabel.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction)
41         grid.addWidget(self.phonemicLabel, 0, 3)
42         
43         self.phoneticLabel = QtGui.QLabel("Phonetic")
44         self.phoneticLabel.setTextFormat(0)
45         self.phoneticLabel.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction)
46         grid.addWidget(self.phoneticLabel, 0, 4)
47         
48         grid.addWidget(QtGui.QLabel("Meaning"), 1, 2)
49         
50         self.meaningEdit = QtGui.QLineEdit(self);
51         grid.addWidget(self.meaningEdit, 1, 3, 1, 2)
52         
53         grid.addWidget(QtGui.QLabel("Parts of Speech\n(Space Separated)"), 2, 2)
54         
55         self.POSEdit = QtGui.QLineEdit(self);
56         grid.addWidget(self.POSEdit, 2, 3, 1, 2)
57         
58         grid.addWidget(QtGui.QLabel("Notes"), 3, 2, 3, 1)
59         
60         self.notesEdit = QtGui.QTextEdit(self)
61         grid.addWidget(self.notesEdit, 3, 3, 3, 2)
62         
63         self.addEdit = QtGui.QLineEdit(self)
64         grid.addWidget(self.addEdit, 4, 0)
65         
66         self.removeButton = QtGui.QPushButton("&Remove")
67         grid.addWidget(self.removeButton, 5, 0)
68         
69         grid.setRowStretch(0, 0)
70         grid.setRowStretch(3, 1)
71
72         self.setLayout(grid)
73         
74         # Data
75         
76         if not Tab.Data.has_key('Dictionary'):
77             Tab.Data['Dictionary'] = {}
78         
79         # Signals
80         
81         self.POSEdit.textChanged.connect(self.onPOSChange)
82         self.addEdit.returnPressed.connect(self.onAdd)
83         self.removeButton.clicked.connect(self.onRemove)
84         list.itemSelectionChanged.connect(self.onWordSelected)
85         self.meaningEdit.textChanged.connect(self.onMeaningChange)
86         self.notesEdit.textChanged.connect(self.onNotesChange)
87         
88     def onOpen(self):
89         self.list.clear()
90         for word in self.Dictionary.iterkeys():
91             self.list.addItem(word)
92         self.list.setCurrentRow(self.list.count())
93         
94     def resetCurrent(self, everything = True):
95         ''' Reset the info on the right-hand side '''
96         if everything:
97             self.currentWord = None
98             self.writtenLabel.setText("Written")
99             self.phonemicLabel.setText("Phonemic")
100             self.phoneticLabel.setText("Phonetic")
101         self.meaningEdit.setText('')
102         self.notesEdit.setText('')
103         self.POSEdit.setText('')
104     
105     def onAdd(self):
106         text = unicode(self.addEdit.text())
107         if (self.Dictionary.has_key(text)):
108             return # Key already exists, ignore
109         self.addEdit.setText('') # clear add box
110         self.list.addItem(text)
111         self.list.setCurrentRow(self.list.count() - 1)
112         self.list.emit(QtCore.SIGNAL('itemSelectionChanged()'))
113         
114     def onRemove(self):
115         if (self.Dictionary.has_key(self.currentWord)):
116             del self.Dictionary[self.currentWord]
117             Tab.Data.Save()
118         self.list.takeItem(self.list.currentRow())
119         self.list.emit(QtCore.SIGNAL('itemSelectionChanged()'))
120     
121     def onWordSelected(self):
122         ''' This method is called when an item has been chosen in the menu '''
123         try:
124             self.currentWord = unicode(self.list.currentItem().text())
125         except AttributeError:
126             return # no list.currentItem, ignore
127         
128         if (self.Dictionary.has_key(self.currentWord)):
129             # Set entries
130             self.meaningEdit.setText(self.Dictionary[self.currentWord][0])
131             self.notesEdit.setText(self.Dictionary[self.currentWord][1])
132             self.POSEdit.setText(self.Dictionary[self.currentWord][2])
133             # Set labels
134             self.writtenLabel.setText(self.currentWord)
135             try:
136                 phonemic = self.wordAsPhonemic(self.currentWord)
137                 self.phonemicLabel.setText('/' + phonemic + '/');
138                 self.phoneticLabel.setText('[' + self.phonemicAsPhonetic(phonemic) + ']')
139             except MissingPhonemeException:
140                 self.removeButton.clicked.emit(True)
141             except InvalidPhonotacticsException as e:
142                 self.phoneticLabel.setText('Invalid Phonotactics')
143         else:
144             self.resetCurrent(self.currentWord == '')
145     
146     def onMeaningChange(self):
147         ''' This method is called when the "meaning" field has been edited '''
148         if self.currentWord == None:
149             return
150             
151         text = unicode(self.meaningEdit.text())
152             
153         if (self.Dictionary.has_key(self.currentWord)):
154             self.Dictionary[self.currentWord][0] = text
155         else:
156             self.Dictionary[self.currentWord] = [text, '', '']
157         Tab.Data.Save()
158
159     def onPOSChange(self):
160         ''' This method is called when the "meaning" field has been edited '''
161         if self.currentWord == None:
162             return
163             
164         text = unicode(self.POSEdit.text())
165             
166         if (self.Dictionary.has_key(self.currentWord)):
167             self.Dictionary[self.currentWord][1] = text
168         else:
169             self.Dictionary[self.currentWord] = ['', text, '']
170         Tab.Data.Save()
171         
172     def onNotesChange(self):
173         ''' This method is called when the "notes" field has been edited '''
174         if self.currentWord == None:
175             return
176         
177         text = unicode(self.notesEdit.toPlainText())
178         
179         if (self.Dictionary.has_key(self.currentWord)):
180             self.Dictionary[self.currentWord][2] = text
181         else:
182             self.Dictionary[self.currentWord] = ['', '', text]
183         Tab.Data.Save()
184         
185     def getDictionary(self):
186         return Tab.Data['Dictionary']
187         
188     def setDictionary(self, value):
189         Tab.Data['Dictionary'] = value
190         
191     Dictionary = property(getDictionary, setDictionary)