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