No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

player.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Licensed under GPLv3
  2. # Written by Sebastian Lohff (seba@someserver.de)
  3. # http://seba-geek.de/projects/seopardy/
  4. from __future__ import print_function
  5. import yaml
  6. from PySide import QtGui, QtCore
  7. class Player(QtGui.QWidget):
  8. DEFAULT_PLAYERS = [
  9. ("Foo", QtGui.QColor(255, 0, 0)),
  10. ("Bar", QtGui.QColor(0, 255, 0)),
  11. ("Baz", QtGui.QColor(0, 0, 255)),
  12. ("Blubb", QtGui.QColor(0, 255, 255)),
  13. ("Murr", QtGui.QColor(255, 0, 255)),
  14. ("Maunz", QtGui.QColor(255, 255, 0)),
  15. ]
  16. def __init__(self, name, color, points=0, parent=None):
  17. super(Player, self).__init__(parent)
  18. self.name = name
  19. self.color = color
  20. self.points = points
  21. self._setup_gui()
  22. def _setup_gui(self):
  23. self.layout = QtGui.QVBoxLayout()
  24. self.name_label = QtGui.QLabel(self.name, self)
  25. self.name_label.setStyleSheet("QLabel { font-size: 40px; background-color: %s; color: white; }" % (self.color.name(),))
  26. self.layout.addWidget(self.name_label)
  27. self.points_label = QtGui.QLabel(str(self.points), self)
  28. self.points_label.setStyleSheet("QLabel { font-size: 40px;}")
  29. self.layout.addWidget(self.points_label, alignment=QtCore.Qt.AlignRight)
  30. self.setLayout(self.layout)
  31. def change_name(self, new_name):
  32. self.name = new_name
  33. self.name_label.setText(new_name)
  34. def add_points(self, amount):
  35. self.points += amount
  36. self.points_label.setText("% 5s" % (self.points,))
  37. @classmethod
  38. def gen_player(clazz, num, parent=None):
  39. if num > len(clazz.DEFAULT_PLAYERS):
  40. raise ValueError("You tried to generate too many players")
  41. return clazz(*clazz.DEFAULT_PLAYERS[num-1], parent=parent)
  42. @classmethod
  43. def _yaml_representer(clazz, dumper, data):
  44. #def __init__(self, name, color, points=0, parent=None):
  45. return dumper.represent_mapping(u'!player', {
  46. 'name': data.name,
  47. 'points': data.points,
  48. 'color': data.color.name()
  49. })
  50. @classmethod
  51. def _yaml_constructor(clazz, loader, node):
  52. pdict = loader.construct_mapping(node)
  53. pdict["color"] = QtGui.QColor(pdict["color"])
  54. return Player(**pdict)
  55. class ButtonEvent(QtCore.QEvent):
  56. eventType = QtCore.QEvent.Type(QtCore.QEvent.registerEventType())
  57. def __init__(self, playerno):
  58. super(ButtonEvent, self).__init__(self.eventType)
  59. if playerno < 0 or playerno > 10:
  60. raise ValueError("That player number is just not plausable")
  61. self.playerno = playerno
  62. def get_playerno(self):
  63. return self.playerno
  64. nobodyColor = QtGui.QColor(128, 128, 128)
  65. # yaml dumpers/loaders
  66. yaml.add_representer(Player, Player._yaml_representer)
  67. yaml.add_constructor(u'!player', Player._yaml_constructor)