Salome HOME
Copyright update 2020
[modules/smesh.git] / src / Tools / MGCleanerPlug / MGCleanerMonViewText.py
index f2647a3fb99e9de51f7832cc6684d3973abf8bee..c45e4cdf5244f817c2ccabb2a19077e4d69536f8 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (C) 2013-2016  EDF R&D
+# Copyright (C) 2013-2020  EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -37,19 +37,24 @@ verbose = True
 
 force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
 if force != None:
-  os.environ["DISTENE_LICENSE_FILE"] = force
-  os.environ["DLIM8VAR"] = "NOTHING"
+    os.environ["DISTENE_LICENSE_FILE"] = force
+    os.environ["DLIM8VAR"] = "NOTHING"
 
 class MGCleanerMonViewText(Ui_ViewExe, QDialog):
     """
     Classe permettant la visualisation de texte
     """
-    def __init__(self, parent, txt):
+    def __init__(self, parent, txt):
         QDialog.__init__(self,parent)
         self.setupUi(self)
         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
-        #self.connect( self.PB_Ok,SIGNAL("clicked()"), self, SLOT("close()") )
         self.PB_Ok.clicked.connect( self.theClose )
+        # Button OK is disabled until computation is finished
+        self.PB_Ok.setEnabled(False)
+        # Button cancel allows to kill the computation
+        # It is disabled when the computation is finished
+        self.PB_Cancel.clicked.connect( self.cancelComputation )
+        self.PB_Cancel.setToolTip("Cancel computation")
         self.PB_Save.clicked.connect( self.saveFile )
         self.PB_Save.setToolTip("Save trace in log file")
         self.PB_Ok.setToolTip("Close view")
@@ -58,44 +63,15 @@ class MGCleanerMonViewText(Ui_ViewExe, QDialog):
         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
         self.monExe.finished.connect( self.finished )
+        self.monExe.errorOccurred.connect( self.errorOccured )
 
-        """ for test set environment
-        env = QProcessEnvironment().systemEnvironment()
-        env.insert("HELLO", "bonjour") #Add an environment variable for debug
-        self.monExe.setProcessEnvironment(env)
-        if verbose: 
-          PP.pprint([str(i) for i in sorted(self.monExe.processEnvironment().toStringList()) if 'DISTENE' in i])
-        """
+        if os.path.exists(self.parent().fichierOut):
+            os.remove(self.parent().fichierOut)
         
-        cmds = ''
-        ext = ''
-        if sys.platform == "win32":
-            if os.path.exists(self.parent().fichierOut):
-                cmds += 'del %s\n' % self.parent().fichierOut
-            ext = '.bat'
-        else:
-            cmds += '#!/bin/bash\n'
-            cmds += 'pwd\n'
-            #cmds += 'which mg-cleaner.exe\n'
-            cmds += 'echo "DISTENE_LICENSE_FILE="$DISTENE_LICENSE_FILE\n'
-            cmds += 'echo "DLIM8VAR="$DLIM8VAR\n'
-            cmds += 'rm -f %s\n' % self.parent().fichierOut
-            ext = '.bash'
-
-        cmds += 'echo %s\n' % txt #to see what is compute command
-        cmds += txt+'\n'
-        cmds += 'echo "END_OF_MGCleaner"\n'
-        
-        nomFichier = os.path.splitext(self.parent().fichierOut)[0] + ext
-        with open(nomFichier, 'w') as f:
-          f.write(cmds)
-        self.make_executable(nomFichier)
-        
-        if verbose: print(("INFO: MGCleaner launch script file: %s" % nomFichier))
-        
-        self.monExe.start(nomFichier)
+        self.monExe.start(txt)
         self.monExe.closeWriteChannel()
-        self.enregistreResultatsDone=False
+        self.hasBeenCanceled = False
+        self.anErrorOccured = False
         self.show()
 
     def make_executable(self, path):
@@ -126,13 +102,32 @@ class MGCleanerMonViewText(Ui_ViewExe, QDialog):
         a=self.monExe.readAllStandardOutput()
         aa=a.data().decode(errors='ignore')
         self.TB_Exe.append(aa)
-    
+
     def finished(self):
-        self.parent().enregistreResultat()
-        self.enregistreResultatsDone=True
-    
+        self.PB_Ok.setEnabled(True)
+        self.PB_Cancel.setEnabled(False)
+        exit_code = self.monExe.exitCode()
+        if exit_code == 0 and not self.anErrorOccured:
+            self.parent().enregistreResultat()
+        elif not self.hasBeenCanceled:
+            if os.path.exists(self.parent().fichierOut):
+                self.parent().enregistreResultat()
+                QMessageBox.critical(self, 'Computation ended in error',
+                  'A new mesh has been generated but with some errors.'+
+                  '<br>Please, check the log message.')
+            else:
+                QMessageBox.critical(self, 'Computation failed',
+                  'The computation has failed.<br>Please, check the log message.')
+        pass
+
+    def errorOccured(self):
+        # for instance if the executable is not found
+        self.anErrorOccured = True
+        self.finished()
+
+    def cancelComputation(self):
+        self.hasBeenCanceled = True
+        self.monExe.kill()
+
     def theClose(self):
-      if not self.enregistreResultatsDone:
-        self.parent().enregistreResultat()
-        self.enregistreResultatsDone=True
-      self.close()
+        self.close()