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