forked from dfilimon/Jeopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundsEditorWidget.py
189 lines (143 loc) · 4.99 KB
/
RoundsEditorWidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from __future__ import print_function
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from gameEditor import mainWindow
from QuestionEditorWidget import QuestionEditor
class RoundsEditor(QWidget):
buttonClicked = pyqtSignal(int)
def __init__(self, parent = None):
super(RoundsEditor, self).__init__(parent)
self.type = "html"
self.template = "template.html"
self.size = {}
self.rounds = []
self.jsondict = {}
self.jsondict["type"] = "html"
self.jsondict["template"] = "template.html"
self.isOpen = False
def setupGui(self):
self.frame = QGridLayout()
self.setLayout(self.frame)
label = QLabel("Rounds:")
label.setAlignment(Qt.AlignCenter)
label.setFont(QFont(QFont.defaultFamily(label.font()), 10))
self.frame.addWidget(label, 0, 0)
# adding size buttons
sizes = QGridLayout()
self.widthSpin = QDoubleSpinBox()
self.widthSpin.setRange(0, 3000)
self.widthSpin.setSingleStep(100)
self.widthSpin.setDecimals(0)
self.widthSpin.setFixedWidth(80)
self.widthSpin.setValue(600)
self.heightSpin = QDoubleSpinBox()
self.heightSpin.setRange(0, 3000)
self.heightSpin.setSingleStep(100)
self.heightSpin.setDecimals(0)
self.heightSpin.setFixedWidth(80)
self.heightSpin.setValue(400)
sizes.addWidget(QLabel("Width:"), 0, 0)
sizes.addWidget(self.widthSpin, 0, 1)
sizes.addWidget(QLabel("Height:"), 0, 2)
sizes.addWidget(self.heightSpin, 0, 3)
self.frame.addLayout(sizes, 1, 0)
self.widthSpin.valueChanged.connect(self.values)
self.heightSpin.valueChanged.connect(self.values)
self.layout = QGridLayout()
self.frame.addLayout(self.layout, 2, 0)
# adding Save button
buttonLayout = QGridLayout() # in case we need another button
saveButton = QPushButton("Save")
saveButton.setFixedWidth(100)
buttonLayout.addWidget(saveButton, 0, 0)
self.frame.addLayout(buttonLayout, 3, 0) # added last in the grid
saveButton.clicked.connect(self.saveGame)
self.count = -1 # number of rounds created
self.addRound()
self.getLineEdit(self.count).setFocus()
def values(self):
self.size["width"] = self.widthSpin.value()
self.size["height"] = self.heightSpin.value()
print(self.size)
def addRound(self):
self.count += 1
lineedit = QLineEdit()
lineedit.setFixedWidth(200)
button = QPushButton("Edit")
button.setEnabled(False)
button.setFixedWidth(40)
button.setFixedHeight(22)
self.layout.addWidget(QLabel("%d." %(self.count+1)), self.count, 0)
self.layout.addWidget(lineedit, self.count, 1)
self.layout.addWidget(button, self.count, 2)
self.rounds.append({"buttonFontSize":14, "labelFontSize":18, "title":""})
button.clicked.connect(self.editRound)
self.getLineEdit(self.count).textChanged.connect(self.enableButton)
def removeRound(self):
w = self.getLabelNumber(self.count)
self.layout.removeWidget(w)
w.setParent(None)
w = self.getLineEdit(self.count)
self.layout.removeWidget(w)
w.setParent(None)
w = self.getButton(self.count)
self.layout.removeWidget(w)
w.setParent(None)
self.frame.setSizeConstraint(QLayout.SetFixedSize) #resizes window
self.count -= 1
self.rounds = self.rounds[:-1] #removing last entry from list
self.getButton(self.count).setEnabled(False)
def enableButton(self, string):
button = self.getButton(self.count) # button -> last made button
lineedit = self.getLineEdit(self.count) # lineedit -> last made lineedit
if lineedit.hasFocus() == True:
if string == "":
button.setEnabled(False)
else:
button.setEnabled(True)
self.addRound()
else:
if self.getLineEdit(self.count-1).text() == "": # if second to last one is empty, remove last one
self.removeRound()
def editRound(self):
row = (self.layout.indexOf(self.sender())-2) /3
#before editing the round, the round list must be updated
aux = str(self.getLineEdit(row).text()) # adding round to list
self.rounds[row]["title"] = aux
self.size["width"] = int(self.widthSpin.value())
self.size["height"] = int(self.heightSpin.value())
print("button is on row: ", row)
print(self.rounds)
if self.isOpen == False:
round_ = mainWindow(self.rounds[row]['title'], self.size["width"], self.size["height"], self)
self.isOpen = True
round_.show()
else:
print("cannot open other editor, current window still running")
#maybe add a warning
def saveGame(self):
# if any changes are made tot the rounds names after 'edit'
# they will be updated in the self.rounds list
for i in range(self.count):
aux = str(self.getLineEdit(i).text())
self.rounds[i]["title"] = aux
self.rounds = self.rounds[:-1]
self.jsondict["size"] = self.size
self.jsondict["rounds"] = self.rounds
print(self.jsondict)
self.close()
def getLabelNumber(self, i):
return self.layout.itemAtPosition(i, 0).widget()
def getLineEdit(self, i):
return self.layout.itemAtPosition(i, 1).widget()
def getButton(self, i):
return self.layout.itemAtPosition(i, 2).widget()
def main():
app = QApplication(sys.argv)
gui = RoundsEditor()
gui.setupGui()
gui.show()
app.exec_()
if __name__ == '__main__':
main()