# -*- coding: utf-8 -*-
# Copyright (C) 2007-2016  EDF R&D
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#

# Modules Python
import string,types,os, sys
import traceback
import tempfile

from qtsalome import *

# Import des panels

from ViewText_ui import Ui_ViewExe

class MonViewText(Ui_ViewExe, QDialog):
    """
    Classe permettant la visualisation de texte
    """
    def __init__(self, parent, txt):
        QDialog.__init__(self,parent)
        self.setupUi(self)
        self.resize( QSize(1000,600).expandedTo(self.minimumSizeHint()) )
        # self.PB_Ok.clicked.connect(self.close)
        self.PB_Ok.clicked.connect( self.theClose )
        self.PB_Save.clicked.connect( self.saveFile )
        self.monExe=QProcess(self)

        self.monExe.readyReadStandardOutput.connect( self.readFromStdOut )
        self.monExe.readyReadStandardError.connect( self.readFromStdErr )
      
        # Je n arrive pas a utiliser le setEnvironment du QProcess
        # fonctionne hors Salome mais pas dans Salome ???
        cmds=''
        #cmds+='#! /usr/bin/env python\n'
        #cmds+='# -*- coding: utf-8 -*-\n'
        cmds+=txt+'\n'
        cmds+='echo "END_OF_Yams"\n'
        if os.path.exists(self.parent().fichierOut):
            os.remove(self.parent().fichierOut)

        ext=''
        if sys.platform == "win32":
            ext = '.bat'
        else:
            ext = '.sh'

        nomFichier=tempfile.mktemp(suffix=ext,prefix='Yams_')
        f=open(nomFichier,'w')
        f.write(cmds)
        f.close()

        maBidouille=nomFichier
        self.monExe.start(maBidouille)
        self.monExe.closeWriteChannel()
        self.enregistreResultatsDone=False
        self.show()

    def saveFile(self):
        #recuperation du nom du fichier
        savedir=os.environ['HOME']
        fn = QFileDialog.getSaveFileName(None,"Save File",savedir)
        if fn.isNull() : return
        ulfile = os.path.abspath(unicode(fn))
        try:
           f = open(fn, 'wb')
           f.write(str(self.TB_Exe.toPlainText()))
           f.close()
        except IOError, why:
           QMessageBox.critical(self, 'Save File',
        	'The file <b>%1</b> could not be saved.<br>Reason: %2'%(unicode(fn), str(why)))

    def readFromStdErr(self):
        a=self.monExe.readAllStandardError()
        self.TB_Exe.append(unicode(a.data().encode()))

    def readFromStdOut(self) :
        a=self.monExe.readAllStandardOutput()
        aa=unicode(a.data(),len(a))
        self.TB_Exe.append(aa)
        if "END_OF_Yams" in aa:
          self.parent().enregistreResultat()
          self.enregistreResultatsDone=True
          #self.theClose()
    
    def theClose(self):
      if not self.enregistreResultatsDone:
        self.parent().enregistreResultat()
        self.enregistreResultatsDone=True
      self.close()