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