123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- """
- Provides a helper tool to (de-)activate multiple people at once.
- """
- import sys
- # pylint: disable=E0611
- from PySide2.QtWidgets import (
- QAction,
- QActionGroup,
- QApplication,
- QGridLayout,
- QInputDialog,
- QLineEdit,
- QMainWindow,
- QMessageBox,
- QPushButton,
- QSizePolicy,
- QToolBar,
- QWidget,
- )
- from PySide2.QtGui import QIcon
- from PySide2.QtCore import QObject, QSize, Qt, Signal, Slot
- # pylint: enable=E0611
- import qdarkstyle
- from piket_client.model import Person, ServerStatus
- class ActivationButton(QPushButton):
- def __init__(self, person, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.setCheckable(True)
- self.person = person
- self.setText(self.person.name)
- self.setChecked(self.person.active)
- self.toggled.connect(self.process_toggled)
- def process_toggled(self, new_state: bool) -> None:
- self.person = self.person.set_active(new_state)
- class ActivationButtons(QWidget):
- def __init__(self, *args, **kwargs) -> None:
- super().__init__(*args, **kwargs)
- self.layout = QGridLayout()
- self.setLayout(self.layout)
- self.init_ui()
- def init_ui(self) -> None:
- ps = Person.get_all()
- num_columns = round(len(ps) / 10) + 1
- for index, person in enumerate(ps):
- button = ActivationButton(person, self)
- self.layout.addWidget(button, index // num_columns, index % num_columns)
- class ActiveStateMainWindow(QMainWindow):
- def __init__(self) -> None:
- super().__init__()
- self.toolbar = None
- self.init_ui()
- def init_ui(self) -> None:
- """ Initialize the UI: construct main widget, toolbar. """
- self.setWindowState(Qt.WindowActive | Qt.WindowFullScreen)
- font_metrics = self.fontMetrics()
- icon_size = font_metrics.height() * 1.45
- # Toolbar
- self.toolbar = QToolBar()
- self.toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
- self.toolbar.setIconSize(QSize(icon_size, icon_size))
- # Left
- self.toolbar.addAction("Afsluiten", self.quit)
- self.addToolBar(self.toolbar)
- # Build main widget
- ps = Person.get_all()
- self.main_widget = ActivationButtons()
- self.setCentralWidget(self.main_widget)
- def quit(self) -> None:
- """ Exit immediately. """
- QApplication.instance().quit()
- def main() -> None:
- """ Main entry point of GUI client. """
- app = QApplication(sys.argv)
- # Set dark theme
- app.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
- # Enlarge font size
- font = app.font()
- size = font.pointSize()
- font.setPointSize(size * 0.8)
- app.setFont(font)
- # Test connectivity
- server_running, info = ServerStatus.is_server_running()
- if not server_running:
- LOG.critical("Could not connect to server", extra={"info": info})
- QMessageBox.critical(
- None,
- "Help er is iets kapot",
- "Kan niet starten omdat de server niet reageert, stuur een foto van "
- "dit naar Maarten: " + repr(info),
- )
- return 1
- # Load main window
- main_window = ActiveStateMainWindow()
- main_window.show()
- app.exec_()
- if __name__ == "__main__":
- main()
|