Salome HOME
Merge remote branch 'origin/V7_dev' into V8_0_0_BR
[modules/smesh.git] / src / Tools / YamsPlug / monViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2015  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 # Modules Python
22 import string,types,os
23 import traceback
24
25 from qtsalome import *
26
27 # Import des panels
28
29 from ViewText_ui import Ui_ViewExe
30
31 class MonViewText(Ui_ViewExe, QDialog):
32     """
33     Classe permettant la visualisation de texte
34     """
35     def __init__(self, parent, txt):
36         QDialog.__init__(self,parent)
37         self.setupUi(self)
38         self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
39         # self.PB_Ok.clicked.connect(self.close)
40         self.PB_Ok.clicked.connect( self.theClose )
41         self.PB_Save.clicked.connect( self.saveFile )
42         self.monExe=QProcess(self)
43
44         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
45         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
46       
47         # Je n arrive pas a utiliser le setEnvironment du QProcess
48         # fonctionne hors Salome mais pas dans Salome ???
49         cmds=''
50         cmds+='rm -f '+self.parent().fichierOut+'\n'
51         cmds+=txt+'\n'
52         cmds+='echo END_OF_Yams\n'
53         pid=self.monExe.pid()
54         nomFichier='/tmp/Yams_'+str(pid)+'.sh'
55         f=open(nomFichier,'w')
56         f.write(cmds)
57         f.close()
58
59         maBidouille='sh  ' + nomFichier
60         self.monExe.start(maBidouille)
61         self.monExe.closeWriteChannel()
62         self.enregistreResultatsDone=False
63         self.show()
64
65     def saveFile(self):
66         #recuperation du nom du fichier
67         savedir=os.environ['HOME']
68         fn = QFileDialog.getSaveFileName(None,"Save File",savedir)
69         if fn.isNull() : return
70         ulfile = os.path.abspath(unicode(fn))
71         try:
72            f = open(fn, 'wb')
73            f.write(str(self.TB_Exe.toPlainText()))
74            f.close()
75         except IOError, why:
76            QMessageBox.critical(self, 'Save File',
77                 'The file <b>%1</b> could not be saved.<br>Reason: %2'%(unicode(fn), str(why)))
78
79     def readFromStdErr(self):
80         a=self.monExe.readAllStandardError()
81         self.TB_Exe.append(unicode(a.data().encode()))
82
83     def readFromStdOut(self) :
84         a=self.monExe.readAllStandardOutput()
85         aa=unicode(a.data(),len(a))
86         self.TB_Exe.append(aa)
87         if "END_OF_Yams" in aa:
88           self.parent().enregistreResultat()
89           self.enregistreResultatsDone=True
90           #self.theClose()
91     
92     def theClose(self):
93       if not self.enregistreResultatsDone:
94         self.parent().enregistreResultat()
95         self.enregistreResultatsDone=True
96       self.close()