""" find and loads the ui objects """ import os from PySide2.QtUiTools import QUiLoader from PySide2.QtCore import QFile, QObject from PySide2 import QtWidgets class DynamicLoader(QObject): """ the parent class of all the run time loadable widgets """ canMaximize = False isVisible = False menu_name = 'Dynamic Loader' loader: QUiLoader window: QtWidgets.QWidget def __init__(self, location: str): super().__init__(None) self.location = location self.loader = QUiLoader() self.__load_ui() self._init_loader() self._init_widgets() self._init_connections() def _init_loader(self): """ Virtual method to be implemented in the child class and should be called in order Main purpose is to find/create the widgets objects in this method :return: None """ raise NotImplementedError() def _init_widgets(self): """ Virtual method to be implemented in the child class and should be called in order Main purpose is to setup/initialize widgets properties in this method :return: None """ raise NotImplementedError() def _init_connections(self): """ Virtual method to be implemented in the child class and should be called in order Main purpose is to initial the widgets signal/slots connections in this method :return: None """ raise NotImplementedError() def __load_ui(self): """ loads the ui file of the GUI at run time :return: none """ ui_file = QFile(self.location + "/interface.ui") ui_file.open(QFile.ReadOnly) self.window = self.loader.load(ui_file) # assert if the ui file can't be loaded error = self.loader.errorString() assert len(error) == 0, error ui_file.close() def show(self): """ shows the main container window :return: none """ self.window.show() def find_widget(self, child_type, child_name: str) -> QtWidgets.QWidget: """ finds a child in the loaded ui and returns the reference to it if found otherwise quits the application :param child_type: (var) type of the child :param child_name: (str) name of the child :return: (QtWidgets.QWidget) reference to the child """ child: QtWidgets.QWidget = self.window.findChild(child_type, child_name) assert child is not None, "child name '{}' with type '{}' can't be found.".format(child_name, child_type) return child def find_action(self, name: str) -> QtWidgets.QAction: """ convenient method of find_widget for QAction :param name: (str) name of the QLabel Object :return: (QAction) reference to the QAction """ child = self.find_widget(QtWidgets.QAction, name) return child def find_label(self, name: str) -> QtWidgets.QLabel: """ convenient method of find_widget for QLabel :param name: (str) name of the QLabel Object :return: (QLabel) reference to the QLabel """ child = self.find_widget(QtWidgets.QLabel, name) return child def find_line_edit(self, name: str) -> QtWidgets.QLineEdit: """ convenient method of find_widget for QLineEdit :param name: (str) name of the QLabel Object :return: (QLabel) reference to the QLineEdit """ child = self.find_widget(QtWidgets.QLineEdit, name) return child def find_button(self, name: str) -> QtWidgets.QPushButton: """ convenient method of find_widget for QPushButton :param name: (str) name of the QPushButton Object :return: (QPushButton) reference to the QPushButton """ child = self.find_widget(QtWidgets.QPushButton, name) return child def find_combobox(self, name: str) -> QtWidgets.QComboBox: """ convenient method of find_widget for QComboBox :param name: (str) name of the QComboBox Object :return: (QComboBox) reference to the QComboBox """ child = self.find_widget(QtWidgets.QComboBox, name) return child def find_checkbox(self, name: str) -> QtWidgets.QCheckBox: """ convenient method of find_widget for QCheckBox :param name: (str) name of the QCheckBox Object :return: (QCheckBox) reference to the QComboBox """ child = self.find_widget(QtWidgets.QCheckBox, name) return child def find_spinbox(self, name: str) -> QtWidgets.QSpinBox: """ convenient method of find_widget for QSpinBox :param name: (str) name of the QSpinBox Object :return: (QSpinBox) reference to the QSpinBox """ child = self.find_widget(QtWidgets.QSpinBox, name) return child def find_double_spinbox(self, name: str) -> QtWidgets.QDoubleSpinBox: """ convenient method of find_widget for QDoubleSpinBox :param name: (str) name of the QDoubleSpinBox Object :return: (QDoubleSpinBox) reference to the QDoubleSpinBox """ child = self.find_widget(QtWidgets.QDoubleSpinBox, name) return child def find_radio_button(self, name: str) -> QtWidgets.QRadioButton: """ convenient method of find_widget for QRadioButton :param name: (str) name of the QRadioButton Object :return: (QRadioButton) reference to the QSpinBox """ child = self.find_widget(QtWidgets.QRadioButton, name) return child def find_slider(self, name: str) -> QtWidgets.QSlider: """ convenient method of find_widget for QSlider :param name: (str) name of the QSlider Object :return: (QSlider) reference to the QSlider """ child = self.find_widget(QtWidgets.QSlider, name) return child def find_table_widget(self, name: str) -> QtWidgets.QTableWidget: """ convenient method of find_widget for QTableWidget :param name: (str) name of the QTableWidget Object :return: (QTableWidget) reference to the QTableWidget """ child: QtWidgets.QTableWidget = self.find_widget(QtWidgets.QTableWidget, name) return child def find_list_widget(self, name: str) -> QtWidgets.QListWidget: """ convenient method of find_widget for QListWidget :param name: (str) name of the QListWidget Object :return: (QListWidget) reference to the QListWidget """ child: QtWidgets.QListWidget = self.find_widget(QtWidgets.QListWidget, name) return child def find_tool_button(self, name: str) -> QtWidgets.QToolButton: """ convenient method of find_widget for QToolButton :param name: (str) name of the QToolButton Object :return: (QToolButton) reference to the QToolButton """ child: QtWidgets.QToolButton = self.find_widget(QtWidgets.QToolButton, name) return child