Digitale bierlijst

gui.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. """
  2. Provides the graphical front-end for Piket.
  3. """
  4. import os
  5. import sys
  6. from urllib.parse import urljoin
  7. # pylint: disable=E0611
  8. from PySide2.QtWidgets import (
  9. QApplication,
  10. QPushButton,
  11. QGridLayout,
  12. QWidget,
  13. QMainWindow,
  14. QSizePolicy,
  15. QToolBar,
  16. )
  17. from PySide2.QtGui import QIcon
  18. from PySide2.QtCore import QSize
  19. # pylint: enable=E0611
  20. import requests
  21. from piket_client.sound import PLOP_WAVE
  22. def plop() -> None:
  23. """ Asynchronously play the plop sound. """
  24. PLOP_WAVE.play()
  25. SERVER_URL = "http://127.0.0.1:5000"
  26. def get_people() -> [dict]:
  27. ''' Request list of active people from the server. '''
  28. request = requests.get(urljoin(SERVER_URL, "/people"))
  29. return request.json()["people"]
  30. class NameButton(QPushButton):
  31. """ Wraps a QPushButton to provide a counter. """
  32. def __init__(self, person: dict, *args, **kwargs) -> None:
  33. self.person_id = person["id"]
  34. self.name = person["name"]
  35. self.count = person["count"]
  36. super().__init__(self.current_label, *args, **kwargs)
  37. self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
  38. self.clicked.connect(self.plop)
  39. @property
  40. def current_label(self) -> str:
  41. ''' Return the label to show on the button. '''
  42. return f"{self.name} ({self.count})"
  43. def plop(self) -> None:
  44. ''' Process a click on this button. '''
  45. req = requests.post(
  46. urljoin(SERVER_URL, f"people/{self.person_id}/add_consumption")
  47. )
  48. if req.status_code == 200:
  49. json = req.json()
  50. person = json["person"]
  51. self.count = person["count"]
  52. self.setText(self.current_label)
  53. plop()
  54. else:
  55. print("Oh shit kapot")
  56. class NameButtons(QWidget):
  57. ''' Main widget responsible for capturing presses and registering them.
  58. '''
  59. def __init__(self) -> None:
  60. super().__init__()
  61. self.layout = None
  62. self.init_ui()
  63. def init_ui(self) -> None:
  64. ''' Initialize UI: build GridLayout, retrieve People and build a button
  65. for each. '''
  66. self.layout = QGridLayout()
  67. for index, person in enumerate(get_people()):
  68. button = NameButton(person)
  69. self.layout.addWidget(button, index // 2, index % 2)
  70. self.setLayout(self.layout)
  71. class PiketMainWindow(QMainWindow):
  72. ''' QMainWindow subclass responsible for showing the main application
  73. window. '''
  74. def __init__(self) -> None:
  75. super().__init__()
  76. self.main_widget = None
  77. self.toolbar = None
  78. self.init_ui()
  79. def init_ui(self) -> None:
  80. ''' Initialize the UI: construct main widget and toolbar. '''
  81. self.main_widget = NameButtons()
  82. self.setCentralWidget(self.main_widget)
  83. font_metrics = self.fontMetrics()
  84. icon_size = font_metrics.height() * 2.5
  85. # Initialize toolbar
  86. self.toolbar = QToolBar()
  87. self.toolbar.setIconSize(QSize(icon_size, icon_size))
  88. # Left
  89. self.toolbar.addAction(self.load_icon("add_person.svg"), "Nieuw persoon")
  90. self.toolbar.addAction(self.load_icon("undo.svg"), "Heydrich")
  91. self.toolbar.addWidget(self.create_spacer())
  92. # Right
  93. self.toolbar.addAction(self.load_icon("beer_bottle.svg"), "Bierrr")
  94. self.addToolBar(self.toolbar)
  95. @staticmethod
  96. def create_spacer() -> QWidget:
  97. """ Return an empty QWidget that automatically expands. """
  98. spacer = QWidget()
  99. spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
  100. return spacer
  101. icons_dir = os.path.join(os.path.dirname(__file__), "icons")
  102. @classmethod
  103. def load_icon(cls, filename: str) -> QIcon:
  104. """ Return a QtIcon loaded from the given `filename` in the icons
  105. directory. """
  106. return QIcon(os.path.join(cls.icons_dir, filename))
  107. def main() -> None:
  108. ''' Main entry point of GUI client. '''
  109. app = QApplication(sys.argv)
  110. font = app.font()
  111. size = font.pointSize()
  112. font.setPointSize(size * 1.75)
  113. app.setFont(font)
  114. main_window = PiketMainWindow()
  115. main_window.show()
  116. app.exec_()
  117. if __name__ == "__main__":
  118. main()