Digitale bierlijst

set_active.py 3.2KB

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