Salome HOME
Switch development flag ON
[modules/smesh.git] / src / Tools / MGCleanerPlug / MGCleanerMonViewText.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2013-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 MGCleanerViewText_ui import Ui_ViewExe
30
31 class MGCleanerMonViewText(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.connect( self.PB_Ok,SIGNAL("clicked()"), self, SLOT("close()") )
40         self.PB_Ok.clicked.connect( self.theClose )
41         self.PB_Save.clicked.connect( self.saveFile )
42         self.PB_Save.setToolTip("Save trace in log file")
43         self.PB_Ok.setToolTip("Close view")
44         self.monExe=QProcess(self)
45
46         self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
47         self.monExe.readyReadStandardError.connect( self.readFromStdErr )
48       
49         # Je n arrive pas a utiliser le setEnvironment du QProcess
50         # fonctionne hors Salome mais pas dans Salome ???
51         cmds=''
52         cmds+='rm -f '+self.parent().fichierOut+'\n'
53         cmds+=txt+'\n'
54         cmds+='echo END_OF_MGCleaner\n'
55         pid=self.monExe.pid()
56         nomFichier='/tmp/MGCleaner_'+str(pid)+'.sh'
57         f=open(nomFichier,'w')
58         f.write(cmds)
59         f.close()
60
61         maBidouille='sh ' + nomFichier
62         self.monExe.start(maBidouille)
63         self.monExe.closeWriteChannel()
64         self.enregistreResultatsDone=False
65         self.show()
66
67     def saveFile(self):
68         #recuperation du nom du fichier
69         savedir=os.environ['HOME']
70         fn = QFileDialog.getSaveFileName(None, self.trUtf8("Save File"),savedir)
71         if fn.isNull() : return
72         ulfile = os.path.abspath(unicode(fn))
73         try:
74            f = open(fn, 'wb')
75            f.write(str(self.TB_Exe.toPlainText()))
76            f.close()
77         except IOError, why:
78            QMessageBox.critical(self, self.trUtf8('Save File'),
79                 self.trUtf8('The file <b>%1</b> could not be saved.<br>Reason: %2')
80                     .arg(unicode(fn)).arg(str(why)))
81
82     def readFromStdErr(self):
83         a=self.monExe.readAllStandardError()
84         self.TB_Exe.append(unicode(a.data()))
85
86     def readFromStdOut(self) :
87         a=self.monExe.readAllStandardOutput()
88         aa=unicode(a.data())
89         self.TB_Exe.append(aa)
90         if "END_OF_MGCleaner" in aa:
91           self.parent().enregistreResultat()
92           self.enregistreResultatsDone=True
93           #self.theClose()
94     
95     def theClose(self):
96       if not self.enregistreResultatsDone:
97         self.parent().enregistreResultat()
98         self.enregistreResultatsDone=True
99       self.close()