seopardy/windows.py

114 lines
3.4 KiB
Python

from PySide import QtGui, QtCore
from gamestate import QuestionAnswers
#class QuestionWindow(QtGui.QWidget):
class QuestionWindow(QtGui.QDialog):
def __init__(self, players, section, qnumber, question, answers=None, parent=None):
super(QuestionWindow, self).__init__(parent)
self.players = players
self.section = section
self.qnumber = qnumber
self.question = question
if answers is not None:
self.answers = answers
else:
self.answers = QuestionAnswers(self.section, self.qnumber)
self._setupGui()
self.setWindowTitle("Seopardy - %s - %d" % (section, qnumber*100))
def get_answers(self):
return self.answers
def _mkQuestionLabel(self, text):
question = QtGui.QLabel(text, alignment=QtCore.Qt.AlignCenter)
question.setWordWrap(True)
question.setStyleSheet("QLabel { font-size: 40px; }")
return question
def _setupGui(self):
self.layout = QtGui.QVBoxLayout()
seclabel = QtGui.QLabel(self.section)
seclabel.setStyleSheet("QLabel { font-size: 30px; }")
self.layout.addWidget(seclabel, alignment=QtCore.Qt.AlignCenter)
self.layout.addStretch()
qlabel = None
if self.question["Type"] == "Text":
qlabel = self._mkQuestionLabel(self.question["Question"])
print(self.question["Question"])
elif self.question["Type"] == "Music":
qlabel = self._mkQuestionLabel("Listen...")
elif self.question["Type"] == "Image":
qlabel = QtGui.QLabel()
pixmap = QtGui.QPixmap()
pixmap.loadFromData(open(self.question["Question"]).read())
qlabel.setPixmap(pixmap)
else:
raise ValueError("%s is an unknown type for section %s question name %s" % (self.question["Type"], self.section, self.question["Name"]))
self.layout.addWidget(qlabel)
self.layout.addStretch()
self.setLayout(self.layout)
def keyPressEvent(self, e):
print("question", e, e.key())
if e.key() == QtCore.Qt.Key_Escape:
if not self.answers.is_answered():
self.answers.nobody_knew()
self.close()
elif not self.answers.is_answered() and e.key() >= ord('1') and e.key() <= ord(str(len(self.players))):
player = self.players[e.key() - ord('1')]
qawin = QuestionAnswerWindow(player, self)
res = qawin.exec_()
if res == QuestionAnswerWindow.CORRECT:
print("Done!")
self.answers.add_try(player, correct=True)
self.accept()
elif res == QuestionAnswerWindow.WRONG:
self.answers.add_try(player, correct=False)
class QuestionAnswerWindow(QtGui.QDialog):
CORRECT = 1
WRONG = 2
OOPS = 3
def __init__(self, player, parent=None):
super(QuestionAnswerWindow, self).__init__(parent)
self.player = player
self._setupGui()
# move window to bottom right of screen
g = QtGui.QApplication.desktop().screenGeometry()
self.show()
cPos = self.rect()
self.move(g.width() - cPos.width(), g.height() - cPos.height())
def _setupGui(self):
self.layout = QtGui.QVBoxLayout()
self.plabel = QtGui.QLabel(self.player.name)
self.plabel.setStyleSheet("QLabel { font-size: 60px; }")
self.layout.addWidget(self.plabel, alignment=QtCore.Qt.AlignCenter)
btnbox = QtGui.QHBoxLayout()
right = QtGui.QPushButton("Correct")
right.clicked.connect(lambda: self.done(self.CORRECT))
btnbox.addWidget(right)
wrong = QtGui.QPushButton("Wrong")
wrong.clicked.connect(lambda: self.done(self.WRONG))
btnbox.addWidget(wrong)
btnbox.addStretch()
oops = QtGui.QPushButton("Oops")
oops.clicked.connect(lambda: self.done(self.OOPS))
btnbox.addWidget(oops)
self.layout.addLayout(btnbox)
self.setLayout(self.layout)