Esempio n. 1
0
class InOutWindow(qtw.QWidget, Ui_InOutWindow):
    """The Check In/Out window contains a Check In button, a Check Out button, a Cancel button,
        and the 'Checked In' List."""

    # Signal to indicate that the Admin window was closed.
    window_closed = qtc.pyqtSignal(str)

    def __init__(self, parent: qtw.QWidget, db_filename: str, barcode: str):
        super().__init__(parent)
        self.setupUi(self)

        # Force the user to interact with this window
        self.setWindowModality(
            qtc.Qt.ApplicationModal)  # block input to all other windows
        self.setWindowFlag(
            qtc.Qt.Dialog)  # dialog box without min or max buttons
        self.setWindowFlag(qtc.Qt.FramelessWindowHint
                           )  # borderless window that cannot be resized

        self.__db_manager = DatabaseManager(db_filename)
        self.__barcode = barcode

        # "data" is a 4-tuple: (firstname, lastname, status, total_hours)
        success, message, data = self.__db_manager.get_student_data(
            self.__barcode)

        # Set the data to display in the Check In/Out window.
        self.studentName.setText(data[0] + ' ' + data[1])
        self.__status = data[2]
        self.totalHours.setNum(data[3])

        # The "hours_table_model" is a list of 3-tuples:  [ ('day of week', 'date', 'hours'), ... ]
        success, message, hours_table_model, total_hours = self.__db_manager.get_student_hours_table(
            self.__barcode)
        self.totalHours.setNum(total_hours)

        # Create the Model for the hoursTable
        hours_table_header = ('Day', 'Date', 'Hours')
        hours_table_column_width = (60, 130, 90
                                    )  # Needs to be less than 300 total
        self.table_model = HoursTableModel(hours_table_model,
                                           hours_table_header)
        self.hoursTable.setModel(self.table_model)
        self.hoursTable.setEditTriggers(qtw.QAbstractItemView.NoEditTriggers)
        self.hoursTable.horizontalHeader().setFixedHeight(45)
        self.hoursTable.setSelectionMode(qtw.QAbstractItemView.NoSelection)
        self.hoursTable.setFocusPolicy(qtc.Qt.NoFocus)
        self.hoursTable.setVerticalScrollBarPolicy(qtc.Qt.ScrollBarAlwaysOn)
        self.hoursTable.setHorizontalScrollBarPolicy(qtc.Qt.ScrollBarAlwaysOff)
        self.hoursTable.setAutoScroll(False)
        self.hoursTable.setAutoScrollMargin(400)

        for col in range(len(hours_table_column_width)):
            self.hoursTable.setColumnWidth(col, hours_table_column_width[col])

        # Disable either the Check In or Check Out button.
        if self.__status == 'Checked In':
            self.disable_button(self.checkinButton, 'Already Checked In')
        elif self.__status == 'Checked Out':
            self.disable_button(self.checkoutButton, 'Not Checked In')

        # Signals to indicate which button was clicked.
        self.cancelButton.clicked.connect(lambda: self.clicked('Cancel'))
        self.checkinButton.clicked.connect(lambda: self.clicked('Check In'))
        self.checkoutButton.clicked.connect(lambda: self.clicked('Check Out'))

        if platform.system() == 'Windows':
            self.show()
        else:
            self.showFullScreen()

    @qtc.pyqtSlot(str)
    def clicked(self, button_name: str) -> None:
        """
        This slot is called when a button is clicked.

        :param button_name: the name of the button clicked
        :return: None, emits the "window_closed" signal and passes a message to display on the Main window
        """

        # Determine which button was clicked and set the message to be displayed.
        if button_name == 'Cancel':
            message = 'Cancelled.'
        elif button_name == 'Check In' and self.__status == 'Checked Out':
            success, message = self.__db_manager.checkin_student(
                self.__barcode)
        elif button_name == 'Check Out' and self.__status == 'Checked In':
            success, message = self.__db_manager.checkout_student(
                self.__barcode)
        else:
            message = 'Nothing done.'

        self.window_closed.emit(message)
        self.close()

    def disable_button(self, button: qtw.QPushButton, tool_tip: str) -> None:
        """
        This method enables the correct buttons and sets the tool tips font.

        :param button: the button to disable
        :param tool_tip: the tool tip to display
        :return: None
        """

        if button:
            button.setEnabled(False)
            button.setToolTip(tool_tip)
            font = qtg.QFont()
            font.setBold(False)
            font.setPointSize(24)
            button.setFont(font)

    def __display(
            self,
            title: str,
            text: str,
            informative_text: str = '',
            detailed_text: str = '',
            buttons: qtw.QMessageBox.StandardButton = qtw.QMessageBox.Ok
    ) -> int:
        message_box = qtw.QMessageBox(self)
        message_box.setIcon(qtw.QMessageBox.Information)

        message_box.setWindowTitle(title)
        message_box.setText(text)

        if informative_text:
            message_box.setInformativeText(informative_text)
        if detailed_text:
            message_box.setDetailedText(detailed_text)

        message_box.setStandardButtons(buttons)

        return_value = message_box.exec_()
        return return_value