예제 #1
0
    def __init__(self):
        super().__init__(Ui_scheduler)
        self.logger = logging.getLogger(__name__)
        self.ui.addTaskButton.clicked.connect(self.on_add_task_clicked)
        self.ui.clearTasksButton.clicked.connect(self.on_clear_tasks_clicked)
        self.ui.runTasksButton.clicked.connect(self.on_run_clicked)

        self.ui.outputDirectorylineEdit.setText(self.NO_OUTPUT_DIRECTORY_SELECTED)
        self.ui.outputDirectoryButton.clicked.connect(self.on_select_output_dir_clicked)

        self.last_directory = ''
        self.output_dir = None

        self.tasks = []

        self.widget.connect(self.ui.queueVerticalLayout,
                            QtCore.SIGNAL(TaskView.REMOVE_TASK_CLICKED_SIGNAL), self.on_remove_task)

        self._create_tasks_views()

        self.run_task_dn = DynamicNode(
            self.ui.runTasksButton,
            enabling_condition=
            lambda sch: sch.tasks and
            all(self._is_valid_task(x) for x in sch.tasks) and
            sch.ui.outputDirectorylineEdit.text() != sch.NO_OUTPUT_DIRECTORY_SELECTED
        )

        self.dynamic_nodes += [
            DynamicNode(
                self.ui.addTaskButton,
                enabling_condition=lambda sch: len(sch.tasks) < self.MAX_NUMBER_OF_TASKS
            ),
            DynamicNode(
                self.ui.clearTasksButton,
                enabling_condition=lambda sch: len(sch.tasks) > 0
            ),
            self.run_task_dn
        ]

        self.update_dynamic_nodes()
예제 #2
0
class Scheduler(GenericWidget):
    MAX_NUMBER_OF_TASKS = 50
    NO_OUTPUT_DIRECTORY_SELECTED = '<no output dir>'

    def __init__(self):
        super().__init__(Ui_scheduler)
        self.logger = logging.getLogger(__name__)
        self.ui.addTaskButton.clicked.connect(self.on_add_task_clicked)
        self.ui.clearTasksButton.clicked.connect(self.on_clear_tasks_clicked)
        self.ui.runTasksButton.clicked.connect(self.on_run_clicked)

        self.ui.outputDirectorylineEdit.setText(self.NO_OUTPUT_DIRECTORY_SELECTED)
        self.ui.outputDirectoryButton.clicked.connect(self.on_select_output_dir_clicked)

        self.last_directory = ''
        self.output_dir = None

        self.tasks = []

        self.widget.connect(self.ui.queueVerticalLayout,
                            QtCore.SIGNAL(TaskView.REMOVE_TASK_CLICKED_SIGNAL), self.on_remove_task)

        self._create_tasks_views()

        self.run_task_dn = DynamicNode(
            self.ui.runTasksButton,
            enabling_condition=
            lambda sch: sch.tasks and
            all(self._is_valid_task(x) for x in sch.tasks) and
            sch.ui.outputDirectorylineEdit.text() != sch.NO_OUTPUT_DIRECTORY_SELECTED
        )

        self.dynamic_nodes += [
            DynamicNode(
                self.ui.addTaskButton,
                enabling_condition=lambda sch: len(sch.tasks) < self.MAX_NUMBER_OF_TASKS
            ),
            DynamicNode(
                self.ui.clearTasksButton,
                enabling_condition=lambda sch: len(sch.tasks) > 0
            ),
            self.run_task_dn
        ]

        self.update_dynamic_nodes()

    def _create_tasks_views(self):
        for i in range(self.MAX_NUMBER_OF_TASKS):
            task = TaskView(i, self)
            self.dynamic_nodes += task.dynamic_nodes
            self.ui.queueVerticalLayout.addWidget(task.groupbox)

            self.widget.connect(
                task.groupbox,
                QtCore.SIGNAL(TaskView.REMOVE_TASK_CLICKED_SIGNAL),
                self.on_remove_task
            )

            self.widget.connect(
                task.groupbox,
                QtCore.SIGNAL(TaskView.MOVE_UP_TASK_CLICKED_SIGNAL),
                self.on_move_up_task
            )

            self.widget.connect(
                task.groupbox,
                QtCore.SIGNAL(TaskView.MOVE_DOWN_TASK_CLICKED_SIGNAL),
                self.on_move_down_task
            )

            self.widget.connect(
                task.groupbox,
                QtCore.SIGNAL(TaskView.DATA_CONFIG_SELECTED_SIGNAL),
                self.on_data_selected
            )

            self.widget.connect(
                task.groupbox,
                QtCore.SIGNAL(TaskView.PARAMS_SET_CONFIG_SELECTED_SIGNAL),
                self.on_params_set_selected
            )

            self.widget.connect(
                task.groupbox,
                QtCore.SIGNAL(TaskView.STARTING_RULES_CHOSEN_SIGNAL),
                self._on_starting_rules_chosen
            )

    def _on_starting_rules_chosen(self, task, selected_filename):
        self.logger.debug('Updating starting rules')
        self.tasks[task.index].population_configuration = selected_filename
        self.dynamic_gui_update()

    def on_remove_task(self, task):
        self.logger.debug('Removing task')
        del self.tasks[task.index]
        self.dynamic_gui_update()

    def on_move_up_task(self, task):
        self.logger.debug('Moving task up')
        current_index = task.index
        if current_index > 0:
            new_index = current_index - 1
            self.tasks[new_index], self.tasks[current_index] = \
                self.tasks[current_index], self.tasks[new_index]
            self.dynamic_gui_update()

    def on_move_down_task(self, task):
        self.logger.debug('Moving task down')
        current_index = task.index
        if current_index < len(self.tasks):
            new_index = current_index + 1
            self.tasks[new_index], self.tasks[current_index] = \
                self.tasks[current_index], self.tasks[new_index]
            self.dynamic_gui_update()

    def on_data_selected(self, task, selected_filename):
        self.logger.debug('Updating input data path')
        self.tasks[task.index].data_configuration = selected_filename
        self.dynamic_gui_update()

    def on_params_set_selected(self, task, selected_filename):
        self.logger.debug('Updating params config set path')
        self.tasks[task.index].params_configuration = selected_filename
        self.dynamic_gui_update()

    def on_add_task_clicked(self):
        self.logger.debug('Add task clicked!')
        if len(self.tasks) <= self.MAX_NUMBER_OF_TASKS:
            task = TaskModel()
            self.tasks.append(task)
            self.dynamic_gui_update()

    def on_clear_tasks_clicked(self):
        self.logger.debug('Clear tasks clicked!')
        self.tasks.clear()
        self.dynamic_gui_update()

    def on_run_clicked(self):
        if self.run_task_dn.enabling_condition(self):
            runner = Runner(self)
            self.logger.debug('Task runner started')
            runner.show()
            self.widget.setEnabled(False)
            self.logger.debug('Task runner ended')

    @staticmethod
    def _is_valid_task(task):
        return all(x is not None for x in [
            task.data_configuration,
            task.params_configuration
        ])

    def on_select_output_dir_clicked(self):
        selected_dirname = QtGui.QFileDialog.getExistingDirectory(
            self.widget, 'Select output directory...', self.last_directory)

        if selected_dirname:
            self.output_dir = selected_dirname
            self.last_directory = os.path.dirname(selected_dirname)
            self.ui.outputDirectorylineEdit.setText(self.output_dir)
            self.dynamic_gui_update()