some restructuring, fixed the printed 'segmentation fault' when exiting
[conlangery:conlangery.git] / AlphabetCreator.py
1 import sys
2 from PyQt4 import QtGui, QtCore
3 from tabs import Tab
4 from data import Data
5
6 class AlphabetCreator(Tab):
7     ''' This class defines the content in tab 1 '''
8     Alphabet = None
9     currentGrapheme = None
10     def __init__(self, mainWindow):
11         Tab.__init__(self, mainWindow)
12         
13         grid = QtGui.QGridLayout()
14         grid.setSpacing(10)
15         
16         list = self.list = QtGui.QListWidget(self)
17         grid.addWidget(list, 0, 0, 3, 1)
18         
19         label = self.graphemeLabel = QtGui.QLabel("Grapheme")
20         # set format to plaintext, otherwise <x> will not show up
21         # because it thinks it's html
22         label.setTextFormat(0)
23         grid.addWidget(label, 0, 2)
24         
25         grid.addWidget(QtGui.QLabel("Phoneme"), 1, 2)
26         
27         self.phonemeEdit = QtGui.QLineEdit(self);
28         grid.addWidget(self.phonemeEdit, 1, 3)
29         
30         self.convertButton = QtGui.QPushButton("&Convert XSampa to IPA")
31         grid.addWidget(self.convertButton, 1, 4)
32         
33         grid.addWidget(QtGui.QLabel("Notes"), 2, 2, 3, 1)
34         
35         self.notesEdit = QtGui.QTextEdit(self)
36         self.notesEdit.setAcceptRichText(True)
37         grid.addWidget(self.notesEdit, 2, 3, 3, 2)
38         
39         self.addEdit = QtGui.QLineEdit(self)
40         grid.addWidget(self.addEdit, 3, 0)
41         
42         removeButton = QtGui.QPushButton("&Remove")
43         grid.addWidget(removeButton, 4, 0)
44         
45         grid.setRowStretch(0, 0)
46         grid.setRowStretch(2, 1)
47         
48         self.setLayout(grid)
49         
50         # Data
51         
52         Tab.Alphabet = Data("Alphabet.save")
53         if (Tab.Alphabet != {}):
54             self.populateWithGraphemes(self.list, Tab.Alphabet)
55         
56         # Signals
57         
58         self.addEdit.returnPressed.connect(self.onAdd)
59         removeButton.clicked.connect(self.onRemove)
60         list.itemSelectionChanged.connect(self.onGraphemeSelected)
61         self.phonemeEdit.returnPressed.connect(self.onConvertPhoneme)
62         self.phonemeEdit.textChanged.connect(self.onPhonemeChange)
63         self.convertButton.clicked.connect(self.onConvertPhoneme)
64         self.notesEdit.textChanged.connect(self.onNotesChange)
65     
66     def onConvertPhoneme(self):
67         self.phonemeEdit.setText(
68             self.xsampaToIPA(unicode(self.phonemeEdit.text()))
69         )
70     
71     def resetCurrent(self, everything = True):
72         if everything:
73             self.currentWord = None
74             self.graphemeLabel.setText("Grapheme")
75         self.phonemeEdit.setText('')
76         self.notesEdit.setText('')
77         
78     def onAdd(self):
79         text = unicode(self.addEdit.text())
80         if (Tab.Alphabet.has_key(text) or text == None):
81             return # Key already exists, ignore
82         self.addEdit.setText('') # clear add box
83         self.list.addItem(text)
84         self.list.setCurrentRow(self.list.count() - 1)
85         self.list.emit(QtCore.SIGNAL('itemSelectionChanged()'))
86         
87     def onRemove(self):
88         if (Tab.Alphabet.has_key(self.currentGrapheme)):
89             del Tab.Alphabet[self.currentGrapheme]
90             Tab.Alphabet.Save()
91         self.list.takeItem(self.list.currentRow())
92         self.list.emit(QtCore.SIGNAL('itemSelectionChanged()'))
93     
94     def onGraphemeSelected(self):
95         ''' This method is called when an item has been chosen in the menu '''
96         try:
97             self.currentGrapheme = unicode(self.list.currentItem().text())
98         except AttributeError:
99             return # no list.currentItem, ignore
100         
101         if (Tab.Alphabet.has_key(self.currentGrapheme)):
102             self.phonemeEdit.setText(Tab.Alphabet[self.currentGrapheme][0])
103             self.notesEdit.setText(Tab.Alphabet[self.currentGrapheme][1])
104             # Set label
105             self.graphemeLabel.setText('<' + self.currentGrapheme + '>')
106         else:
107             self.resetCurrent(self.currentGrapheme == '')
108     
109     def onNotesChange(self):
110         ''' This method is called when the "notes" field has been edited '''
111         if self.currentGrapheme == None:
112             return
113         
114         text = unicode(self.notesEdit.toPlainText())
115         
116         if (Tab.Alphabet.has_key(self.currentGrapheme)):
117             Tab.Alphabet[self.currentGrapheme][1] = text
118         else:
119             Tab.Alphabet[self.currentGrapheme] = ['', text]
120         Tab.Alphabet.Save();
121     
122     def onPhonemeChange(self):
123         ''' This method is called when the "phoneme" field has been edited '''
124         if self.currentGrapheme == None:
125             return
126         
127         text = unicode(self.phonemeEdit.text())
128                 
129         if (Tab.Alphabet.has_key(self.currentGrapheme)):
130             Tab.Alphabet[self.currentGrapheme][0] = text
131         else:
132             Tab.Alphabet[self.currentGrapheme] = [text, '']
133         Tab.Alphabet.Save();