Salome HOME
Update of CheckDone
[modules/smesh.git] / src / Tools / MGCleanerPlug / MGCleanerMonViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2013-2024  EDF
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 # Modules Python
22 import os
23 import sys
24 import string
25 import types
26 import tempfile
27 import traceback
28 import pprint as PP #pretty print
29
30 from qtsalome import *
31
32 # Import des panels
33
34 from MGCleanerViewText_ui import Ui_ViewExe
35
36 verbose = True
37
38 class MGCleanerMonViewText(Ui_ViewExe, QDialog):
39     """
40     Classe permettant la visualisation de texte
41     """
42     def __init__(self, parent, txt):
43         QDialog.__init__(self,parent)
44         self.setupUi(self)
45         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
46         self.PB_Ok.clicked.connect( self.theClose )
47         # Button OK is disabled until computation is finished
48         self.PB_Ok.setEnabled(False)
49         # Button cancel allows to kill the computation
50         # It is disabled when the computation is finished
51         self.PB_Cancel.clicked.connect( self.cancelComputation )
52         self.PB_Cancel.setToolTip("Cancel computation")
53         self.PB_Save.clicked.connect( self.saveFile )
54         self.PB_Save.setToolTip("Save trace in log file")
55         self.PB_Ok.setToolTip("Close view")
56         self.monExe=QProcess(self)
57
58         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
59         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
60         self.monExe.finished.connect( self.finished )
61         self.monExe.errorOccurred.connect( self.errorOccured )
62
63         if os.path.exists(self.parent().fichierOut):
64             os.remove(self.parent().fichierOut)
65         
66         self.monExe.start(txt)
67         self.monExe.closeWriteChannel()
68         self.hasBeenCanceled = False
69         self.anErrorOccured = False
70         self.show()
71
72     def make_executable(self, path):
73         mode = os.stat(path).st_mode
74         mode |= (mode & 0o444) >> 2    # copy R bits to X
75         os.chmod(path, mode)
76
77     def saveFile(self):
78         #recuperation du nom du fichier
79         savedir=os.path.expanduser("~")
80         fn, mask = QFileDialog.getSaveFileName(None,"Save File",savedir)
81         if not fn: return
82         ulfile = os.path.abspath(str(fn))
83         try:
84             f = open(fn, 'wb')
85             f.write(self.TB_Exe.toPlainText().encode("utf-8"))
86             f.close()
87         except IOError as why:
88             QMessageBox.critical(self, 'Save File',
89                  'The file <b>%s</b> could not be saved.<br>Reason: %s'%(str(fn), str(why)))
90
91     def readFromStdErr(self):
92         a=self.monExe.readAllStandardError()
93         aa=a.data().decode(errors='ignore')
94         self.TB_Exe.append(aa)
95
96     def readFromStdOut(self) :
97         a=self.monExe.readAllStandardOutput()
98         aa=a.data().decode(errors='ignore')
99         self.TB_Exe.append(aa)
100
101     def finished(self):
102         self.PB_Ok.setEnabled(True)
103         self.PB_Cancel.setEnabled(False)
104         exit_code = self.monExe.exitCode()
105         if exit_code == 0 and not self.anErrorOccured:
106             self.parent().enregistreResultat()
107         elif not self.hasBeenCanceled:
108             if os.path.exists(self.parent().fichierOut):
109                 self.parent().enregistreResultat()
110                 QMessageBox.critical(self, 'Computation ended in error',
111                   'A new mesh has been generated but with some errors.'+
112                   '<br>Please, check the log message.')
113             else:
114                 QMessageBox.critical(self, 'Computation failed',
115                   'The computation has failed.<br>Please, check the log message.')
116         pass
117
118     def errorOccured(self):
119         # for instance if the executable is not found
120         self.anErrorOccured = True
121         self.finished()
122
123     def cancelComputation(self):
124         self.hasBeenCanceled = True
125         self.monExe.kill()
126
127     def theClose(self):
128         self.close()