some restructuring, fixed the printed 'segmentation fault' when exiting
[conlangery:conlangery.git] / DictionaryCreator.py
1 import sys
2 from PyQt4 import QtGui, QtCore
3 from tabs import Tab
4 from data import Data
5
6 class DictionaryCreator(Tab):
7     ''' This class defines the content in tab 2 '''
8     currentWord = None
9     def __init__(self, mainWindow):
10         Tab.__init__(self, mainWindow)
11         
12         grid = QtGui.QGridLayout()
13         grid.setSpacing(10)
14         
15         list = self.list = QtGui.QListWidget(self)
16         grid.addWidget(list, 0, 0, 4, 1)
17         
18         label = self.writtenLabel = QtGui.QLabel("Written")
19         label.setTextFormat(0)
20         grid.addWidget(label, 0, 2)
21         
22         label2 = self.saidLabel = QtGui.QLabel("Said")
23         label2.setTextFormat(0)
24         grid.addWidget(label2, 0, 3)
25         
26         grid.addWidget(QtGui.QLabel("Meaning"), 1, 2)
27         
28         self.meaningEdit = QtGui.QLineEdit(self);
29         grid.addWidget(self.meaningEdit, 1, 3)
30         
31         grid.addWidget(QtGui.QLabel("Part of Speech"), 2, 2)
32         
33         self.POSEdit = QtGui.QLineEdit(self);
34         grid.addWidget(self.POSEdit, 2, 3)
35         
36         grid.addWidget(QtGui.QLabel("Notes"), 3, 2, 3, 1)
37         
38         self.notesEdit = QtGui.QTextEdit(self)
39         grid.addWidget(self.notesEdit, 3, 3, 3, 2)
40         
41         self.addEdit = QtGui.QLineEdit(self)
42         grid.addWidget(self.addEdit, 4, 0)
43         
44         self.removeButton = QtGui.QPushButton("&Remove")
45         grid.addWidget(self.removeButton, 5, 0)
46         
47         grid.setRowStretch(0, 0)
48         grid.setRowStretch(3, 1)
49
50         self.setLayout(grid)
51         
52         # Data
53         
54         Tab.Dictionary = Data("Tab.Dictionary.save")
55         if (Tab.Dictionary != {}):
56             self.populateWithWords(self.list, Tab.Dictionary)
57         
58         # Signals
59         
60         self.POSEdit.textChanged.connect(self.onPOSChange)
61         self.addEdit.returnPressed.connect(self.onAdd)
62         self.removeButton.clicked.connect(self.onRemove)
63         list.itemSelectionChanged.connect(self.onWordSelected)
64         self.meaningEdit.textChanged.connect(self.onMeaningChange)
65         self.notesEdit.textChanged.connect(self.onNotesChange)
66     
67     def resetCurrent(self, everything = True):
68         if everything:
69             self.currentWord = None
70             self.writtenLabel.setText("Written")
71             self.saidLabel.setText("Said")
72         self.meaningEdit.setText('')
73         self.notesEdit.setText('')
74         self.POSEdit.setText('')
75     
76     def onAdd(self):
77         text = unicode(self.addEdit.text())
78         if (Tab.Dictionary.has_key(text)):
79             return # Key already exists, ignore
80         self.addEdit.setText('') # clear add box
81         self.list.addItem(text)
82         self.list.setCurrentRow(self.list.count() - 1)
83         self.list.emit(QtCore.SIGNAL('itemSelectionChanged()'))
84         
85     def onRemove(self):
86         if (Tab.Dictionary.has_key(self.currentWord)):
87             del Tab.Dictionary[self.currentWord]
88             Tab.Dictionary.Save()
89         self.list.takeItem(self.list.currentRow())
90         self.list.emit(QtCore.SIGNAL('itemSelectionChanged()'))
91     
92     def onWordSelected(self):
93         ''' This method is called when an item has been chosen in the menu '''
94         try:
95             self.currentWord = unicode(self.list.currentItem().text())
96         except AttributeError:
97             return # no list.currentItem, ignore
98         
99         if (Tab.Dictionary.has_key(self.currentWord)):
100             # Set entries
101             self.meaningEdit.setText(Tab.Dictionary[self.currentWord][0])
102             self.notesEdit.setText(Tab.Dictionary[self.currentWord][1])
103             self.POSEdit.setText(Tab.Dictionary[self.currentWord][2])
104             # Set labels
105             self.writtenLabel.setText(self.currentWord)
106             try:
107                 self.saidLabel.setText('/' + self.wordAsPhonemic(self.currentWord) + '/');
108             except Exception:
109                 self.removeButton.clicked.emit(True)
110         else:
111             self.resetCurrent(self.currentWord == '')
112     
113     def onNotesChange(self):
114         ''' This method is called when the "notes" field has been edited '''
115         if self.currentWord == None:
116             return
117         
118         text = unicode(self.notesEdit.toPlainText())
119         
120         if (Tab.Dictionary.has_key(self.currentWord)):
121             Tab.Dictionary[self.currentWord][1] = text
122         else:
123             Tab.Dictionary[self.currentWord] = ['', text, '']
124         Tab.Dictionary.Save()
125     
126     def onMeaningChange(self):
127         ''' This method is called when the "meaning" field has been edited '''
128         if self.currentWord == None:
129             return
130             
131         text = unicode(self.meaningEdit.text())
132             
133         if (Tab.Dictionary.has_key(self.currentWord)):
134             Tab.Dictionary[self.currentWord][0] = text
135         else:
136             Tab.Dictionary[self.currentWord] = [text, '', '']
137         Tab.Dictionary.Save()
138
139     def onPOSChange(self):
140         ''' This method is called when the "meaning" field has been edited '''
141         if self.currentWord == None:
142             return
143             
144         text = unicode(self.POSEdit.text())
145             
146         if (Tab.Dictionary.has_key(self.currentWord)):
147             Tab.Dictionary[self.currentWord][2] = text
148         else:
149             Tab.Dictionary[self.currentWord] = ['', '', text]
150         Tab.Dictionary.Save()