ASubWindow.py 7.45 KiB
# -*- coding: utf-8 -*-

import os

from PyQt5.QtWidgets import (
    QMainWindow, QApplication, QDesktopWidget,
    QMdiArea, QMdiSubWindow, QDialog,
    QPushButton, QLineEdit, QCheckBox,
    QTimeEdit, QSpinBox, QTextEdit,
    QRadioButton, QComboBox, QFileDialog,
    QMessageBox,
)
from PyQt5.QtCore import (
    QTime,
)
from PyQt5.uic import loadUi

class ASubWindow(QDialog):
    def __init__(self, name="", ui="dummy", parent=None):
        super(ASubWindow, self).__init__(parent=parent)
        self.ui = loadUi(
            os.path.join(os.path.dirname(__file__), "ui", f"{ui}.ui"),
            self
        )
        self.name = name
        self.parent = parent
        self.parent.sub_win_add(name, self)
        self.fixed_size()

    def fixed_size(self):
        width = self.frameGeometry().width()
        height = self.frameGeometry().height()
        # self.setFixedSize(width, height)

    def closeEvent(self, event):
        if not self.parent is None:
            self.parent.sub_win_del(self.name)

    # Commun use features

    def find(self, qtype, name):
        """Find an ui component

        Args:
            qtype: Type of QT component
            name: Name for component

        Returns:
            return the component
        """
        return self.ui.findChild(qtype, name)

    def set_line_edit_text(self, name:str, text:str):
        """Set text of line edit component

        Args:
            line_edit: The line edit component name
            text: The text

        Returns:
            Nothing
        """
        try:
            self.find(QLineEdit, name).setText(text)
        except AttributeError as e:
            print(e)
            print(f"{name}")

    def get_line_edit_text(self, name:str):
        """Get text of line edit component

        Args:
            line_edit: The line edit component name

        Returns:
            Text
        """
        return self.find(QLineEdit, name).text()

    def set_text_edit_text(self, name:str, text:str):
        """Set text of text edit component

        Args:
            text_edit: The text edit component name
            text: The text

        Returns:
            Nothing
        """
        self.find(QTextEdit, name).setText(text)

    def get_text_edit_text(self, name:str):
        """Get text of text edit component

        Args:
            text_edit: The text edit component name

        Returns:
            Text
        """
        return self.find(QTextEdit, name).toHtml()

    def set_check_box(self, name:str, checked:bool):
        """Set status of checkbox component

        Args:
            name: The check box component name
            checked: Bool

        Returns:
            Nothing
        """
        self.find(QCheckBox, name).setChecked(checked)

    def get_check_box(self, name:str):
        """Get status of checkbox component

        Args:
            name: The check box component name

        Returns:
            Status of checkbox (bool)
        """
        return self.find(QCheckBox, name).isChecked()


    def set_time_edit(self, name:str, time:str):
        """Set time of timeedit component

        Args:
            name: The timeedit component name
            time: The new time in format "HH:mm:ss"

        Returns:
            Nothing
        """
        qtime = QTime.fromString(time)
        self.find(QTimeEdit, name).setTime(qtime)

    def get_time_edit(self, name:str):
        """Get time of timeedit component

        Args:
            name: The timeedit component name

        Returns:
            The time of timeedit in format "HH:mm:ss"
        """
        return self.find(QTimeEdit, name).time().toString()

    def set_spin_box(self, name:str, value:int):
        """Set value of spinbox component

        Args:
            name: The spinbox component name
            value: The new value

        Returns:
            Nothing
        """
        self.find(QSpinBox, name).setValue(value)

    def get_spin_box(self, name:str):
        """Get time of spin box component

        Args:
            name: The spin box component

        Returns:
            The value of spin box
        """
        return self.find(QSpinBox, name).value()

    def set_push_button_checkable(self, name:str, checked:bool):
        """Set value of push button component

        Args:
            name: The push button component name
            value: The new value

        Returns:
            Nothing
        """
        self.find(QPushButton, name).setChecked(checked)

    def get_push_button_checkable(self, name:str):
        """Get status of push button

        Args:
            name: The push button component name

        Returns:
            The status of push button
        """
        return self.find(QPushButton, name).isChecked()

    def set_radio_button(self, name:str, checked:bool):
        """Set value of radio button component

        Args:
            name: The radio button component name
            checked: Checked

        Returns:
            Nothing
        """
        self.find(QRadioButton, name).setChecked(checked)

    def get_radio_button(self, name:str):
        """Get status of radio button

        Args:
            name: The radio button component name

        Returns:
            The status of radio button
        """
        return self.find(QRadioButton, name).isChecked()

    def combobox_add_item(self, name:str, item:str):
        """Add item in combo box

        Args:
            name: The combo box component name
            item: The item to add

        Returns:
            Nothing
        """
        self.find(QComboBox, name).addItem(item)

    def set_combobox_text(self, name:str, item:str):
        """Set current text of combo box

        Args:
            name: The combo box component name
            item: The item to add

        Returns:
            Nothing
        """
        self.find(QComboBox, name).setCurrentText(item)

    def get_combobox_text(self, name:str):
        """Get current text of combo box

        Args:
            name: The combo box component name

        Returns:
            Current text
        """
        return self.find(QComboBox, name).currentText()


    # Custom dialog
    def file_dialog(self, select_file=True, callback=lambda x: None):
        """Open a new file dialog and send result to callback function

        Args:
            select_file: Select a file if True, else select a dir
            callback: The callback function

        Returns:
            Nothing
        """
        dialog = QFileDialog(self)

        if select_file:
            mode = QFileDialog.FileMode.ExistingFile
        else:
            mode = QFileDialog.FileMode.Directory

        dialog.setFileMode(mode)

        if dialog.exec_():
            file_names = dialog.selectedFiles()
            callback(file_names)

    def message_box(self, value,
                    text: str,
                    informative_text: str,
                    window_title: str = "Warning"):
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Warning)
        msg.setText(f"{value} : {text}")
        msg.setInformativeText(f"{informative_text}")
        msg.setWindowTitle(f"{window_title}")
        # msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
        # _width = len(f"{text} : {value}")
        # msg.setStyleSheet("QLabel{min-width:200 px; font-size: 13px;} QPushButton{width:10px; font-size: 12px};"
        #                   "background-color: Ligthgray; color : gray; font-size: 8pt; color: #888a80;")
        msg.exec_()