Digitale bierlijst

gui.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. """
  2. Provides the graphical front-end for Piket.
  3. """
  4. import os
  5. import sys
  6. # pylint: disable=E0611
  7. from PySide2.QtWidgets import (
  8. QApplication,
  9. QGridLayout,
  10. QInputDialog,
  11. QMainWindow,
  12. QPushButton,
  13. QSizePolicy,
  14. QToolBar,
  15. QWidget,
  16. )
  17. from PySide2.QtGui import QIcon
  18. from PySide2.QtCore import QSize, Qt
  19. # pylint: enable=E0611
  20. try:
  21. import dbus
  22. except ImportError:
  23. dbus = None
  24. from piket_client.sound import PLOP_WAVE
  25. from piket_client.model import Person
  26. def plop() -> None:
  27. """ Asynchronously play the plop sound. """
  28. PLOP_WAVE.play()
  29. class NameButton(QPushButton):
  30. """ Wraps a QPushButton to provide a counter. """
  31. def __init__(self, person: Person, *args, **kwargs) -> None:
  32. self.person = person
  33. self.count = person.consumptions["1"]
  34. super().__init__(self.current_label, *args, **kwargs)
  35. self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
  36. self.clicked.connect(self.process_click)
  37. @property
  38. def current_label(self) -> str:
  39. """ Return the label to show on the button. """
  40. return f"{self.person.name} ({self.count})"
  41. def process_click(self) -> None:
  42. """ Process a click on this button. """
  43. if self.person.add_consumption():
  44. self.count = self.person.consumptions["1"]
  45. self.setText(self.current_label)
  46. plop()
  47. else:
  48. print("Jantoeternuitje, kapot")
  49. class NameButtons(QWidget):
  50. """ Main widget responsible for capturing presses and registering them.
  51. """
  52. def __init__(self) -> None:
  53. super().__init__()
  54. self.layout = None
  55. self.init_ui()
  56. def init_ui(self) -> None:
  57. """ Initialize UI: build GridLayout, retrieve People and build a button
  58. for each. """
  59. self.layout = QGridLayout()
  60. for index, person in enumerate(Person.get_all()):
  61. button = NameButton(person)
  62. self.layout.addWidget(button, index // 2, index % 2)
  63. self.setLayout(self.layout)
  64. class PiketMainWindow(QMainWindow):
  65. """ QMainWindow subclass responsible for showing the main application
  66. window. """
  67. def __init__(self) -> None:
  68. super().__init__()
  69. self.main_widget = None
  70. self.toolbar = None
  71. self.osk = None
  72. self.init_ui()
  73. def init_ui(self) -> None:
  74. """ Initialize the UI: construct main widget and toolbar. """
  75. # Connect to dbus, get handle to virtual keyboard
  76. if dbus:
  77. try:
  78. session_bus = dbus.SessionBus()
  79. self.osk = session_bus.get_object('org.onboard.Onboard',
  80. '/org/onboard/Onboard/Keyboard')
  81. except dbus.exceptions.DBusException:
  82. # Onboard not present or dbus broken
  83. self.osk = None
  84. # Go full screen
  85. self.setWindowState(Qt.WindowActive | Qt.WindowFullScreen)
  86. # Initialize main widget
  87. self.main_widget = NameButtons()
  88. self.setCentralWidget(self.main_widget)
  89. font_metrics = self.fontMetrics()
  90. icon_size = font_metrics.height() * 2
  91. # Initialize toolbar
  92. self.toolbar = QToolBar()
  93. self.toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
  94. self.toolbar.setIconSize(QSize(icon_size, icon_size))
  95. # Left
  96. self.toolbar.addAction(
  97. self.load_icon("add_person.svg"), "Nieuw persoon", self.add_person
  98. )
  99. self.toolbar.addAction(self.load_icon("undo.svg"), "Oeps")
  100. self.toolbar.addWidget(self.create_spacer())
  101. # Right
  102. self.toolbar.addAction(self.load_icon("beer_bottle.svg"), "Bierrr")
  103. self.addToolBar(self.toolbar)
  104. def show_keyboard(self) -> None:
  105. ''' Show the virtual keyboard, if possible. '''
  106. if self.osk:
  107. self.osk.Show()
  108. def hide_keyboard(self) -> None:
  109. ''' Hide the virtual keyboard, if possible. '''
  110. if self.osk:
  111. self.osk.Hide()
  112. def add_person(self) -> None:
  113. """ Ask for a new Person and register it, then rebuild the central
  114. widget. """
  115. self.show_keyboard()
  116. name, ok = QInputDialog.getItem(
  117. self,
  118. "Persoon toevoegen",
  119. "Voer de naam van de nieuwe persoon in, of kies uit de lijst.",
  120. ["Cas", "Frenk"],
  121. 0,
  122. True,
  123. )
  124. self.hide_keyboard()
  125. if ok and name:
  126. person = Person(name=name)
  127. person = person.save()
  128. self.main_widget = NameButtons()
  129. self.setCentralWidget(self.main_widget)
  130. @staticmethod
  131. def create_spacer() -> QWidget:
  132. """ Return an empty QWidget that automatically expands. """
  133. spacer = QWidget()
  134. spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
  135. return spacer
  136. icons_dir = os.path.join(os.path.dirname(__file__), "icons")
  137. @classmethod
  138. def load_icon(cls, filename: str) -> QIcon:
  139. """ Return a QtIcon loaded from the given `filename` in the icons
  140. directory. """
  141. return QIcon(os.path.join(cls.icons_dir, filename))
  142. def main() -> None:
  143. """ Main entry point of GUI client. """
  144. app = QApplication(sys.argv)
  145. font = app.font()
  146. size = font.pointSize()
  147. font.setPointSize(size * 1.75)
  148. app.setFont(font)
  149. main_window = PiketMainWindow()
  150. main_window.show()
  151. app.exec_()
  152. if __name__ == "__main__":
  153. main()