""" find and loads the ui objects """ import os from PySide2.QtUiTools import QUiLoader from PySide2.QtCore import QFile, QObject from PySide2 import QtWidgets from typing import Any class DynamicLoader(QObject): """ the parent class of all the run time loadable widgets """ canMaximize = True isVisible = False menu_name = 'Dynamic Loader' 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): """ 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 = 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