Salome HOME
Improve behaviour of Cleaner and SurfOpt in case of error and add a cancel button.
[modules/smesh.git] / src / Tools / YamsPlug / monViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2016  EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import os
22 import sys
23 import string
24 import types
25 import tempfile
26 import traceback
27 import pprint as PP #pretty print
28
29 from qtsalome import *
30
31 # Import des panels
32
33 from ViewText_ui import Ui_ViewExe
34
35 verbose = True
36
37 force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
38 if force != None:
39     os.environ["DISTENE_LICENSE_FILE"] = force
40     os.environ["DLIM8VAR"] = "NOTHING"
41
42 class MonViewText(Ui_ViewExe, QDialog):
43     """
44     Classe permettant la visualisation de texte
45     """
46     def __init__(self, parent, txt):
47         QDialog.__init__(self,parent)
48         self.setupUi(self)
49         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
50         self.PB_Ok.clicked.connect( self.theClose )
51         # Button OK is disabled until computation is finished
52         self.PB_Ok.setEnabled(False)
53         # Button cancel allows to kill the computation
54         # It is disabled when the computation is finished
55         self.PB_Cancel.clicked.connect( self.cancelComputation )
56         self.PB_Cancel.setToolTip("Cancel computation")
57         self.PB_Save.clicked.connect( self.saveFile )
58         self.PB_Save.setToolTip("Save trace in log file")
59         self.PB_Ok.setToolTip("Close view")
60         self.monExe=QProcess(self)
61
62         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
63         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
64         self.monExe.finished.connect( self.finished )
65         self.monExe.errorOccurred.connect( self.errorOccured )
66
67         if os.path.exists(self.parent().fichierOut):
68             os.remove(self.parent().fichierOut)
69
70         self.monExe.start(txt)
71         self.monExe.closeWriteChannel()
72         self.hasBeenCanceled = False
73         self.anErrorOccured = False
74         self.show()
75
76     def make_executable(self, path):
77         mode = os.stat(path).st_mode
78         mode |= (mode & 0o444) >> 2    # copy R bits to X
79         os.chmod(path, mode)
80
81     def saveFile(self):
82         #recuperation du nom du fichier
83         savedir=os.environ['HOME']
84         fn, mask = QFileDialog.getSaveFileName(None,"Save File",savedir)
85         if not fn: return
86         ulfile = os.path.abspath(str(fn))
87         try:
88             f = open(fn, 'wb')
89             f.write(self.TB_Exe.toPlainText().encode("utf-8"))
90             f.close()
91         except IOError as why:
92             QMessageBox.critical(self, 'Save File',
93                  'The file <b>%s</b> could not be saved.<br>Reason: %s'%(str(fn), str(why)))
94
95     def readFromStdErr(self):
96         a=self.monExe.readAllStandardError()
97         aa=a.data().decode(errors='ignore')
98         self.TB_Exe.append(aa)
99
100     def readFromStdOut(self) :
101         a=self.monExe.readAllStandardOutput()
102         aa=a.data().decode(errors='ignore')
103         self.TB_Exe.append(aa)
104
105     def finished(self):
106         self.PB_Ok.setEnabled(True)
107         self.PB_Cancel.setEnabled(False)
108         exit_code = self.monExe.exitCode()
109         if exit_code == 0 and not self.anErrorOccured:
110             self.parent().enregistreResultat()
111         elif not self.hasBeenCanceled:
112             QMessageBox.critical(self, 'Computation failed',
113                  'The computation has failed.<br>Please, check the log message.')
114         pass
115
116     def errorOccured(self):
117         # for instance if the executable is not found
118         self.anErrorOccured = True
119         self.finished()
120
121     def cancelComputation(self):
122         self.hasBeenCanceled = True
123         self.monExe.kill()
124
125     def theClose(self):
126         self.close()