Exemple #1
0
    def _launch_refine_thread(self, refiner, gui_timeout_id):
        @run_when_idle
        def thread_completed(*args, **kwargs):
            """ Called when the refinement is completed """
            self.thread = None
            
            gobject.source_remove(gui_timeout_id)
            self.view.stop_spinner()
            
            # Make some plots:
            if self.model.make_psp_plots:
                self.view.update_refinement_status("Processing...")
                self.results_controller.generate_images()
            
            # Set the labels:
            self.results_controller.update_labels()
                
            # Hide our shit:
            self.view.hide_refinement_info()
            self.view.hide()
            
            # Show results:
            self.results_view.present()

        thread = CancellableThread(refiner.refine, thread_completed)
        thread.start()
        return thread
Exemple #2
0
        def on_accept(dialog):
            ## TODO MOVE THIS (PARTIALLY?) TO THE MODEL LEVEL ##

            filenames = dialog.get_filenames()
            parser = dialog.get_filter().get_data("parser")

            task = ThreadedTaskBox()
            window = DialogFactory.get_custom_dialog(
                task, parent=self.view.get_top_widget())

            # Status:
            status_dict = dict(total_files=len(filenames),
                               current_file=0,
                               specimens=[])

            # Task:
            def load_specimens(stop=None):
                for filename in filenames:
                    if stop is not None and stop.is_set():
                        return
                    try:
                        specimens = Specimen.from_experimental_data(
                            filename=filename,
                            parent=self.model,
                            parser=parser)
                    except Exception as msg:
                        message = "An unexpected error has occurred when trying to parse %s:\n\n<i>" % os.path.basename(
                            filename)
                        message += str(msg) + "</i>\n\n"
                        message += "This is most likely caused by an invalid or unsupported file format."
                        logger.exception(message)

                        @run_when_idle
                        def run_dialog():
                            DialogFactory.get_information_dialog(
                                message=message,
                                parent=self.view.get_top_widget()).run()
                            return False

                        run_dialog()
                    else:
                        status_dict["specimens"] += specimens
                    status_dict["current_file"] += 1

            # Cancel & stop events:
            def on_interrupted(*args, **kwargs):
                window.hide()

            # Status label update:
            def gui_callback():
                task.set_status(
                    "Loading file %d/%d ..." %
                    (status_dict["current_file"], status_dict["total_files"]))
                return True

            gui_timeout_id = gobject.timeout_add(250, gui_callback)

            # Complete event:
            @run_when_idle
            def on_complete(*args, **kwargs):
                last_iter = None
                for specimen in status_dict["specimens"]:
                    last_iter = self.model.specimens.append(specimen)
                if last_iter is not None:
                    self.view["project_specimens"].set_cursor(last_iter)
                gobject.source_remove(gui_timeout_id)
                window.hide()
                window.destroy()

            # Run task box:
            task.connect("cancelrequested", on_interrupted)
            task.connect("stoprequested", on_interrupted)
            task.set_status("Loading ...")
            task.start()
            window.show_all()

            # Run thread:
            self.thread = CancellableThread(load_specimens, on_complete)
            self.thread.start()