Added QEvent for player button presses

This commit is contained in:
Sebastian Lohff 2013-11-21 15:38:21 +01:00
parent b88c998bae
commit 5cf6231a90
2 changed files with 36 additions and 9 deletions

View File

@ -46,3 +46,17 @@ class Player(QtGui.QWidget):
return clazz(*clazz.DEFAULT_PLAYERS[num-1], parent=parent)
class ButtonEvent(QtCore.QEvent):
eventType = QtCore.QEvent.Type(QtCore.QEvent.registerEventType())
def __init__(self, playerno):
super(ButtonEvent, self).__init__(self.eventType)
if playerno < 0 or playerno > 10:
raise ValueError("That player number is just not plausable")
self.playerno = playerno
print("player", self.playerno, self.eventType)
def get_playerno(self):
return self.playerno

View File

@ -3,7 +3,7 @@ from PySide import QtGui, QtCore
import copy
from gamestate import QuestionAnswers
from player import Player
from player import Player, ButtonEvent
class QuestionWindow(QtGui.QDialog):
def __init__(self, players, section, qnumber, question, answers=None, parent=None):
@ -20,6 +20,7 @@ class QuestionWindow(QtGui.QDialog):
self._setupGui()
self.setWindowTitle("Seopardy - %s - %d" % (section, qnumber*100))
self._inWindow = False
def get_answers(self):
return self.answers
@ -57,20 +58,32 @@ class QuestionWindow(QtGui.QDialog):
self.setLayout(self.layout)
def event(self, e):
if e.type() == ButtonEvent.eventType:
if e.get_playerno() <= len(self.players) and not self._inWindow:
print("Button", e.get_playerno())
self._inWindow = True
player = self.players[e.get_playerno()-1]
qawin = QuestionAnswerWindow(player, self)
res = qawin.exec_()
if res == QuestionAnswerWindow.CORRECT:
self.answers.add_try(player, correct=True)
self.accept()
elif res == QuestionAnswerWindow.WRONG:
self.answers.add_try(player, correct=False)
self._inWindow = False
return True
else:
return super(QuestionWindow, self).event(e)
def keyPressEvent(self, e):
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:
self.answers.add_try(player, correct=True)
self.accept()
elif res == QuestionAnswerWindow.WRONG:
self.answers.add_try(player, correct=False)
QtCore.QCoreApplication.postEvent(self, ButtonEvent(int(chr(e.key()))))
class QuestionAnswerWindow(QtGui.QDialog):
CORRECT = 1