Skip to content

Commit 0579ce2

Browse files
committed
Let menu items for 2D/3D vis run via non-blocking background processes.
1 parent 5a47e7a commit 0579ce2

1 file changed

Lines changed: 42 additions & 32 deletions

File tree

tools/Python/mcgui/mcgui.py

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,37 @@ def run(self, fixed_params, params, inspect=None):
459459
self.__emitter.status('Started simulation/trace in background shell...')
460460
subprocess.Popen(runstr, shell=True)
461461

462+
def vis_run(self, tool=''):
463+
464+
runstr = tool + ' ' + os.path.basename(self.__instrFile) + ' -n100 -y'
465+
466+
# Add & for backgrounding on Unix systems
467+
if not os.name == 'nt':
468+
runstr = runstr + ' &'
469+
else:
470+
runstr = 'start ' + runstr
471+
472+
# Ensure assembled runstr is a string, not a QString
473+
runstr = str(runstr)
474+
475+
if not os.name=='nt':
476+
# run simulation in a background thread
477+
self.__runthread = McRunQThread()
478+
self.__runthread.cmd = runstr
479+
self.__runthread.cwd = os.path.dirname(self.__instrFile)
480+
self.__runthread.finished.connect(lambda: self.__runFinished(self.__runthread.process_returncode))
481+
self.__runthread.thread_exception.connect(handleExceptionMsg)
482+
self.__runthread.error.connect(lambda msg: self.__emitter.message(msg, err_msg=True))
483+
self.__runthread.message.connect(lambda msg: self.__emitter.message(msg))
484+
self.__runthread.start()
485+
self.__emitter.message(runstr, gui=True)
486+
self.__emitter.status('Running visualisation ...')
487+
self.__fireSimStateUpdate()
488+
else:
489+
self.__emitter.message(runstr, gui=True)
490+
self.__emitter.status('Started visualisation in background shell...')
491+
subprocess.Popen(runstr, shell=True)
492+
462493
def __runFinished(self, process_returncode):
463494
self.__fireSimStateUpdate()
464495
if process_returncode == 0:
@@ -741,43 +772,22 @@ def handleMcDisplayWeb(self):
741772
DISPLAY="mcdisplay"
742773
else:
743774
DISPLAY="mxdisplay"
744-
self.emitter.status('Running ' + DISPLAY + '-webgl-classic...')
745-
try:
746-
cmd = DISPLAY+'-webgl-classic -y -n100 ' + os.path.basename(self.state.getInstrumentFile())
747-
if os.name == 'nt':
748-
cmd = 'start ' + cmd
749-
else:
750-
cmd = cmd + ' &'
751-
self.emitter.message(cmd, gui=True)
752-
self.emitter.message('', gui=True)
753-
754-
def messg(s): self.emitter.message(s)
755-
def messg_err(s): self.emitter.message(s, err_msg=True)
756-
utils.run_subtool_to_completion(cmd, stdout_cb=messg, stderr_cb=messg_err)
757-
finally:
758-
self.emitter.status('')
759-
775+
tool=DISPLAY + "-webgl-classic"
776+
777+
self.emitter.status('Running ' + tool)
778+
self.state.vis_run(tool=tool)
779+
760780
def handleMcDisplay2D(self):
761781
if mccode_config.configuration["MCCODE"]=="mcstas":
762782
DISPLAY="mcdisplay"
763783
else:
764784
DISPLAY="mxdisplay"
765-
self.emitter.status('Running ' + DISPLAY + '-pyqtgraph...')
766-
try:
767-
cmd = DISPLAY+'-pyqtgraph --default -n100 ' + os.path.basename(self.state.getInstrumentFile())
768-
if os.name == 'nt':
769-
cmd = 'start ' + cmd
770-
else:
771-
cmd = cmd + ' &'
772-
self.emitter.message(cmd, gui=True)
773-
self.emitter.message('', gui=True)
774-
775-
def messg(s): self.emitter.message(s)
776-
def messg_err(s): self.emitter.message(s, err_msg=True)
777-
utils.run_subtool_to_completion(cmd, stdout_cb=messg, stderr_cb=messg_err)
778-
finally:
779-
self.emitter.status('')
780-
785+
tool=DISPLAY + "-pyqtgraph"
786+
787+
self.emitter.status('Running ' + tool)
788+
self.state.vis_run(tool=tool)
789+
790+
781791
def handleHelpWeb(self):
782792
# open the mcstas homepage
783793
mcurl = 'http://www.'+mccode_config.configuration["MCCODE"]+'.org'

0 commit comments

Comments
 (0)