Esempio n. 1
0
class ViewsController(QMainWindow):
    """This class will handle all the views of the application.

    Responsible for showing the differnet views in a specific order depending on the input of the user. If the
    application is launched with a profile (configuration file), the main view of the application will be shown;
    otherwise, the title and the configuration views will be shown prior to the main view.
    """

    home_singal = pyqtSignal()
    robot_select_signal = pyqtSignal()

    def __init__(self, parent, configuration, controller=None):
        """Constructor of the class.

        Arguments:
            parent {ui.gui.views_controller.ParentWindow} -- Parent of this.
            configuration {utils.configuration.Config} -- Configuration instance of the application

        Keyword Arguments:
            controller {utils.controller.Controller} -- Controller of the application (default: {None})
        """
        QMainWindow.__init__(self)
        self.parent = parent
        self.controller = controller
        self.configuration = configuration
        self.main_view = None
        self.thread_gui = ThreadGUI(self)
        self.thread_gui.daemon = True

        # self.home_singal.connect(self.show_title)
        # self.robot_select_signal.connect(self.show_robot_selection)

    def show_title(self):
        """Shows the title view"""
        title = TitleWindow(self.parent)
        title.switch_window.connect(self.show_robot_selection)
        self.parent.main_layout.addWidget(title)
        self.fadein_animation()

    def show_robot_selection(self):
        """Shows the robot selection view"""
        from views.robot_selection import RobotSelection

        delete_widgets_from(self.parent.main_layout)
        robot_selector = RobotSelection(self.parent)
        robot_selector.switch_window.connect(self.show_world_selection)
        self.parent.main_layout.addWidget(robot_selector, 0)
        self.fadein_animation()

    def show_world_selection(self):
        """Shows the world selection view"""
        from views.world_selection import WorldSelection

        delete_widgets_from(self.parent.main_layout)
        world_selector = WorldSelection(self.parent.robot_selection,
                                        self.configuration, self.parent)
        world_selector.switch_window.connect(self.show_layout_selection)
        self.parent.main_layout.addWidget(world_selector)
        self.fadein_animation()

    def show_layout_selection(self):
        """Show the layout configuration view"""
        delete_widgets_from(self.parent.main_layout)
        self.layout_selector = LayoutSelection(self.configuration, self.parent)
        self.layout_selector.switch_window.connect(self.show_main_view_proxy)
        self.parent.main_layout.addWidget(self.layout_selector)
        self.fadein_animation()

    def show_main_view_proxy(self):
        """Helper function to show the main view. Will close the parent window to create  a new one"""
        # self.show_main_view(False)
        self.parent.close()

    def show_main_view(self, from_main):
        """Shows the main window depending on where the application comes from.

        If the from_main flag is true, the configuration comes from the previous GUI views. Otherwise, the configuration
        comes from a configuration file. Eitherway, the main view will be shown with the proper configuration.

        Arguments:
            from_main {bool} -- tells if the configuration comes from either configuration file or GUI.
        """
        if not from_main:
            layout_configuration = self.layout_selector.get_config()
            delete_widgets_from(self.parent.main_layout)
        else:
            layout_configuration = None
        self.main_view = MainView(layout_configuration, self.configuration,
                                  self.controller, self.parent)
        self.parent.main_layout.addWidget(self.main_view)
        self.fadein_animation()
        self.start_thread()

    def start_thread(self):
        """Start the GUI refresing loop"""
        self.thread_gui.start()

    def fadein_animation(self):
        """Start a fadein animation for views transitions"""
        self.w = QFrame(self.parent)
        # self.parent.main_layout.addWidget(self.w, 0)
        self.w.setFixedSize(WIDTH, HEIGHT)
        self.w.setStyleSheet('background-color: rgba(51,51,51,1)')
        self.w.show()

        effect = QGraphicsOpacityEffect()
        self.w.setGraphicsEffect(effect)

        self.animation = QPropertyAnimation(effect, b"opacity")
        self.animation.setDuration(500)
        self.animation.setStartValue(1)
        self.animation.setEndValue(0)

        self.animation.start(QPropertyAnimation.DeleteWhenStopped)
        self.animation.finished.connect(self.fade_animation)

    def fade_animation(self):
        """Safe kill the animation"""
        self.w.close()
        del self.w
        del self.animation

    def update_gui(self):
        """Update the GUI. Called from the refresing loop thread"""
        while not self.parent.closing:
            if self.main_view:
                self.main_view.update_gui()
            time.sleep(0.1)