Digitale bierlijst

set_active.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """
  2. Provides a helper tool to (de-)activate multiple people at once.
  3. """
  4. import math
  5. import sys
  6. # pylint: disable=E0611
  7. import qdarkstyle
  8. from PySide2.QtCore import QObject, QSize, Qt, Signal, Slot
  9. from PySide2.QtGui import QIcon
  10. from PySide2.QtWidgets import (
  11. QAction,
  12. QActionGroup,
  13. QApplication,
  14. QGridLayout,
  15. QInputDialog,
  16. QLineEdit,
  17. QMainWindow,
  18. QMessageBox,
  19. QPushButton,
  20. QSizePolicy,
  21. QToolBar,
  22. QWidget,
  23. )
  24. from piket_client.model import NetworkError, Person, ServerStatus
  25. # pylint: enable=E0611
  26. class ActivationButton(QPushButton):
  27. def __init__(self, person, *args, **kwargs):
  28. super().__init__(*args, **kwargs)
  29. self.setCheckable(True)
  30. self.person = person
  31. self.setText(self.person.name)
  32. self.setChecked(self.person.active)
  33. self.toggled.connect(self.process_toggled)
  34. def process_toggled(self, new_state: bool) -> None:
  35. self.person = self.person.set_active(new_state)
  36. class ActivationButtons(QWidget):
  37. def __init__(self, *args, **kwargs) -> None:
  38. super().__init__(*args, **kwargs)
  39. self.layout = QGridLayout()
  40. self.setLayout(self.layout)
  41. self.init_ui()
  42. def init_ui(self) -> None:
  43. ps = Person.get_all()
  44. assert not isinstance(ps, NetworkError)
  45. num_columns = math.ceil(math.sqrt(len(ps)))
  46. for index, person in enumerate(ps):
  47. button = ActivationButton(person, self)
  48. self.layout.addWidget(button, index // num_columns, index % num_columns)
  49. class ActiveStateMainWindow(QMainWindow):
  50. def __init__(self) -> None:
  51. super().__init__()
  52. self.toolbar = QToolBar()
  53. self.init_ui()
  54. def init_ui(self) -> None:
  55. """ Initialize the UI: construct main widget, toolbar. """
  56. self.setWindowState(Qt.WindowActive | Qt.WindowFullScreen)
  57. font_metrics = self.fontMetrics()
  58. icon_size = font_metrics.height() * 1.45
  59. # Toolbar
  60. self.toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
  61. self.toolbar.setIconSize(QSize(icon_size, icon_size))
  62. # Left
  63. self.toolbar.addAction("Afsluiten", self.quit)
  64. self.addToolBar(self.toolbar)
  65. # Build main widget
  66. ps = Person.get_all()
  67. self.main_widget = ActivationButtons()
  68. self.setCentralWidget(self.main_widget)
  69. def quit(self) -> None:
  70. """ Exit immediately. """
  71. QApplication.instance().quit()
  72. def main() -> None:
  73. """ Main entry point of GUI client. """
  74. app = QApplication(sys.argv)
  75. # Set dark theme
  76. app.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
  77. # Enlarge font size
  78. font = app.font()
  79. size = font.pointSize()
  80. font.setPointSize(size * 0.8)
  81. app.setFont(font)
  82. # Test connectivity
  83. server_running = ServerStatus.is_server_running()
  84. if not isinstance(server_running, bool):
  85. QMessageBox.critical(
  86. None,
  87. "Help er is iets kapot",
  88. "Kan niet starten omdat de server niet reageert, stuur een foto van "
  89. "dit naar Maarten: " + repr(server_running.value),
  90. )
  91. return
  92. # Load main window
  93. main_window = ActiveStateMainWindow()
  94. main_window.show()
  95. app.exec_()
  96. if __name__ == "__main__":
  97. main()