some restructuring, fixed the printed 'segmentation fault' when exiting
[conlangery:conlangery.git] / PhonotacticsEditor.py
1 import sys
2 from PyQt4 import QtGui, QtCore
3 from tabs import Tab
4 from data import Data
5
6 class PhonotacticsEditor(Tab):
7     def __init__(self, mainWindow):
8         Tab.__init__(self, mainWindow)
9         
10         grid = QtGui.QGridLayout()
11         grid.setSpacing(10)
12         
13         grid.addWidget(QtGui.QLabel("Allowed Onsets"), 0, 0, 1, 2)
14         grid.addWidget(QtGui.QLabel("Allowed Medials"), 0, 2, 1, 2)
15         grid.addWidget(QtGui.QLabel("Allowed Finals"), 0, 4, 1, 2)
16         
17         self.onsetsList = QtGui.QListWidget(self)
18         grid.addWidget(self.onsetsList, 1, 0, 1, 2)
19         self.medialsList = QtGui.QListWidget(self)
20         grid.addWidget(self.medialsList, 1, 2, 1, 2)
21         self.finalsList = QtGui.QListWidget(self)
22         grid.addWidget(self.finalsList, 1, 4, 1, 2)
23         
24         self.onsetsEdit = QtGui.QLineEdit(self)
25         grid.addWidget(self.onsetsEdit, 2, 0, 1, 1)
26         self.medialsEdit = QtGui.QLineEdit(self)
27         grid.addWidget(self.medialsEdit, 2, 2, 1, 1)
28         self.finalsEdit = QtGui.QLineEdit(self)
29         grid.addWidget(self.finalsEdit, 2, 4, 1, 1)
30         
31         self.onsetsRemoveButton = QtGui.QPushButton("Remove")
32         grid.addWidget(self.onsetsRemoveButton, 2, 1, 1, 1)
33         self.medialsRemoveButton = QtGui.QPushButton("Remove")
34         grid.addWidget(self.medialsRemoveButton, 2, 3, 1, 1)
35         self.finalsRemoveButton = QtGui.QPushButton("Remove")
36         grid.addWidget(self.finalsRemoveButton, 2, 5, 1, 1)
37         
38         self.setLayout(grid)
39         
40         # Data
41         
42         Tab.Phonotactics = Data('Phonotactics.save', contents = [[], [], []])
43         if Tab.Phonotactics[0] != []:
44             self.populateWithAllowedOnsets(self.onsetsList, Tab.Phonotactics[0])
45         if Tab.Phonotactics[1] != []:
46             self.populateWithAllowedMedials(self.medialsList, Tab.Phonotactics[1])
47         if Tab.Phonotactics[2] != []:
48             self.populateWithAllowedFinals(self.finalsList, Tab.Phonotactics[2])
49         
50         # Signals
51         
52         self.onsetsEdit.returnPressed.connect(self.onAddOnset)
53         self.medialsEdit.returnPressed.connect(self.onAddMedial)
54         self.finalsEdit.returnPressed.connect(self.onAddFinal)
55         self.onsetsRemoveButton.clicked.connect(self.onRemOnset)
56         self.medialsRemoveButton.clicked.connect(self.onRemMedial)
57         self.finalsRemoveButton.clicked.connect(self.onRemFinal)
58         
59     def onAddOnset(self):
60         text = unicode(self.onsetsEdit.text())
61         if (text in Tab.Phonotactics[0]):
62             return # Key already exists, ignore
63         Tab.Phonotactics[0].append(text)
64         Tab.Phonotactics.Save()
65         self.onsetsEdit.setText('') # clear add box
66         self.onsetsList.addItem(self.wordAsPhonemic(text))
67         self.onsetsList.setCurrentRow(self.onsetsList.count() - 1)
68         
69     def onAddMedial(self):
70         text = unicode(self.medialsEdit.text())
71         if (text in Tab.Phonotactics[1]):
72             return # Key already exists, ignore
73         Tab.Phonotactics[1].append(text)
74         Tab.Phonotactics.Save()
75         self.medialsEdit.setText('') # clear add box
76         self.medialsList.addItem(self.wordAsPhonemic(text))
77         self.medialsList.setCurrentRow(self.medialsList.count() - 1)
78         
79     def onAddFinal(self):
80         text = unicode(self.finalsEdit.text())
81         if (text in Tab.Phonotactics[2]):
82             return # Key already exists, ignore
83         Tab.Phonotactics[2].append(text)
84         Tab.Phonotactics.Save()
85         self.finalsEdit.setText('') # clear add box
86         self.finalsList.addItem(self.wordAsPhonemic(text))
87         self.finalsList.setCurrentRow(self.finalsList.count() - 1)
88     
89     def onRemOnset(self):
90         word = unicode(self.onsetsList.currentItem().text())
91         if (word in Tab.Phonotactics[0]):
92             Tab.Phonotactics[0].remove(word)
93             Tab.Phonotactics.Save()
94         self.onsetsList.takeItem(self.onsetsList.currentRow())
95         
96     def onRemMedial(self):
97         word = unicode(self.medialsList.currentItem().text())
98         if (word in Tab.Phonotactics[1]):
99             Tab.Phonotactics[1].remove(word)
100             Tab.Phonotactics.Save()
101         self.medialsList.takeItem(self.medialsList.currentRow())
102         
103     def onRemFinal(self):
104         word = unicode(self.finalsList.currentItem().text())
105         if (word in Tab.Phonotactics[2]):
106             Tab.Phonotactics[2].remove(word)
107             Tab.Phonotactics.Save()
108         self.finalsList.takeItem(self.finalsList.currentRow())