forked from foprea/Jeopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestionEditorWidget.py
154 lines (112 loc) · 4.93 KB
/
QuestionEditorWidget.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
import sys, os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class QuestionEditor(QWidget):
def __init__(self, number, category, pred, parent = None):
super(QuestionEditor, self).__init__(parent)
self.template = ''
self.setupGui(number, category)
self.pred = pred
self.d = {}
#print self.parent()
def setupGui(self, number, category):
frame = QVBoxLayout()
info = QHBoxLayout()
centerLayout = QGridLayout()
templateLayout = QHBoxLayout()
buttonLayout = QHBoxLayout()
roundL = QLabel('Category "%s"' % category)
roundL.setAlignment(Qt.AlignCenter)
font = QFont.defaultFamily(roundL.font())
roundL.setFont(QFont(font, 10))
questionL = QLabel("Question %d" % number)
questionL.setAlignment(Qt.AlignCenter)
questionL.setFont(QFont(font, 10))
# editing all Labels and LineEdit widgets
statementLine = QLineEdit("Dennis Ritchie")
statementLine.setFixedWidth(250)
answerLine = QLineEdit("Who developed C?")
valueSpinBox = QDoubleSpinBox()
valueSpinBox.setRange(0, 100000)
valueSpinBox.setSingleStep(100)
valueSpinBox.setDecimals(0)
valueSpinBox.setFixedWidth(80)
valueSpinBox.setValue(100)
#valueSpinBox.setSuffix(" p")
self.template = 'template.html'
path = os.path.dirname(sys.argv[0]) + "\\cdl_talk\\template.html"
path = path.replace('\\', '/')
self.templateBrowse = QLineEdit(path)
templateButton = QPushButton("Browse")
templateButton.setFixedSize(60, 25)
# adding Save and Cancel buttons
saveButton = QPushButton("Save")
cancelButton = QPushButton("Cancel")
# signal setup
templateButton.clicked.connect(self.getTemplate)
saveButton.clicked.connect(self.saveQuestion)
cancelButton.clicked.connect(self.close)
# layout setup
info.addWidget(roundL)
info.addWidget(questionL)
centerLayout.addWidget(QLabel("Statement:"), 0, 0)
centerLayout.addWidget(QLabel("Answer:"), 1, 0)
centerLayout.addWidget(QLabel("Value:"), 2, 0)
centerLayout.addWidget(QLabel("Template:"), 3, 0)
centerLayout.addWidget(statementLine, 0, 1)
centerLayout.addWidget(answerLine, 1, 1)
centerLayout.addWidget(valueSpinBox, 2, 1)
templateLayout.addWidget(self.templateBrowse)
templateLayout.addWidget(templateButton)
centerLayout.addLayout(buttonLayout, 4, 1)
centerLayout.addLayout(templateLayout, 3, 1)
buttonLayout.addWidget(saveButton)
buttonLayout.addWidget(cancelButton)
# formating
centerLayout.itemAtPosition(0, 0).widget().setAlignment(Qt.AlignRight | Qt.AlignCenter)
centerLayout.itemAtPosition(1, 0).widget().setAlignment(Qt.AlignRight | Qt.AlignCenter)
centerLayout.itemAtPosition(2, 0).widget().setAlignment(Qt.AlignRight | Qt.AlignCenter)
centerLayout.itemAtPosition(3, 0).widget().setAlignment(Qt.AlignRight | Qt.AlignCenter)
centerLayout.itemAtPosition(2, 1).setAlignment(Qt.AlignCenter)
statementLine.setAlignment(Qt.AlignCenter)
answerLine.setAlignment(Qt.AlignCenter)
valueSpinBox.setAlignment(Qt.AlignCenter)
statementLine.setFocus()
statementLine.selectAll()
frame.addLayout(info)
frame.addLayout(centerLayout)
self.setLayout(frame)
def getTemplate(self):
file_ = ''
file_ = str(QFileDialog.getOpenFileName(self,
'Select a question template', 'template.html', 'Question template (*.html)'))
print file_
if file_ != '':
self.templateBrowse.setText(file_)
self.template = file_[file_.rfind('/')+1:]
def getCenterLayout(self):
return self.layout().itemAt(1).layout()
def saveQuestion(self):
self.d["statement"] = str(self.getCenterLayout().itemAtPosition(0, 1).widget().text());
self.d["answer"] = str(self.getCenterLayout().itemAtPosition(1, 1).widget().text())
self.d["value"] = int(self.getCenterLayout().itemAtPosition(2, 1).widget().text())
self.d["template"] = self.template
self.pred.d[self.c]["questions"][self.q] = self.d
# where self.c is the category number [in the "categories" list]
# and self.q is the question number [in the "questions" list] self.q
print self.d["statement"]
print self.d["answer"]
print self.d["value"]
print self.d["template"]
self.close()
#return self.value
def closeEvent(self, event):
print "QuestionEdutir is being closed on 'x, save or cancel'"
self.pred.isOpen = False
def main():
app = QApplication(sys.argv)
gui = QuestionEditor()
gui.show()
app.exec_()
if __name__ == '__main__':
main()