Salome HOME
pourTelemac
[tools/eficas.git] / InterfaceQT4 / viewManager.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   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.
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 from __future__ import absolute_import
22 try :
23    from builtins import str
24    from builtins import object
25 except : pass
26
27 import os
28 from Extensions.i18n import tr
29 import six
30 from  PyQt5.QtWidgets  import QFileDialog, QMessageBox
31 from  PyQt5.QtCore     import QFileInfo
32
33 DictExtensions= {"MAP" : ".map"}
34 class MyTabview(object):
35
36    def __init__(self,appliEficas):
37        self.appliEficas=appliEficas
38        self.tabWidgets = []
39        self.mesIndexes = {}
40        self.appliEficas=appliEficas
41        self.editors = []
42        self.dict_editors={}
43        self.untitledCount = 0
44        self.doubles = {}
45
46        self.myQtab = self.appliEficas.myQtab
47
48        self.myQtab.currentChanged.connect(self.indexChanged)
49        self.myQtab.tabCloseRequested.connect(self.closeTab)
50         
51    def indexChanged(self):
52        index=self.myQtab.currentIndex()
53        if index in self.dict_editors:
54            editor=self.dict_editors[index]
55            self.appliEficas.CONFIGURATION=editor.CONFIGURATION
56            self.appliEficas.code=editor.CONFIGURATION.code
57            self.appliEficas.setWindowTitle(editor.titre)
58            self.appliEficas.construitMenu()
59
60    def handleOpen(self,fichier=None,patron=0,units=None):
61        result = None
62        if fichier is None:
63             if self.appliEficas.demande==True : 
64                self.appliEficas.definitCode(None,None)
65                if self.appliEficas.code == None:return
66             
67             if self.appliEficas.code in DictExtensions:
68                chaine="JDC (*"+DictExtensions[self.appliEficas.code]+");;"
69                extensions=tr(chaine+ "All Files (*)")
70             elif self.appliEficas.code== "TELEMAC" : extensions=tr('CAS (*.cas);;All Files (*)')
71             else :
72                extensions=tr('Fichiers JDC (*.comm);;''Tous les Fichiers (*)')
73
74             fichier = QFileDialog.getOpenFileName(self.appliEficas,
75                         tr('Ouvrir Fichier'),
76                         self.appliEficas.CONFIGURATION.savedir,
77                          extensions)
78             fichier=fichier[0]
79        fichier = os.path.abspath(six.text_type(fichier))
80        ulfile = os.path.abspath(six.text_type(fichier))
81        self.appliEficas.CONFIGURATION.savedir=os.path.split(ulfile)[0]
82        self.appliEficas.addToRecentList(fichier)
83        maPage=self.getEditor( fichier,units=units)
84        if maPage: result = maPage
85        if maPage : self.myQtab.setTabText(self.myQtab.indexOf(maPage),os.path.basename(fichier))
86        return result
87
88    def closeTab(self):
89        self.handleClose()
90
91    def handleClose(self,doitSauverRecent = 1,texte=tr('&Quitter')):
92        if doitSauverRecent : self.appliEficas.sauveRecents()
93        index=self.myQtab.currentIndex()
94        if index < 0 : return
95        res=self.checkDirty(self.dict_editors[index],texte)
96        if res == 2 : return 2             # l utilisateur a annule
97        index=self.myQtab.currentIndex()
98        idx=index
99        while idx < len(self.dict_editors) -1 :
100              self.dict_editors[idx]=self.dict_editors[idx+1]
101              idx = idx + 1
102        del self.dict_editors[len (self.dict_editors) -1]
103        try :
104            del self.doubles[self.dict_editors[index]]
105        except :
106            pass
107        self.myQtab.removeTab(index)
108        return res
109        
110
111    def run(self):
112        index=self.myQtab.currentIndex()
113        if index < 0 : return
114        editor=self.dict_editors[index]
115        editor.run()
116
117    def saveRun(self):
118        index=self.myQtab.currentIndex()
119        if index < 0 : return
120        editor=self.dict_editors[index]
121        editor.saveRun()
122
123    def handleCloseAll(self,texte=tr('Quitter')):
124        res=0
125        self.appliEficas.sauveRecents()
126        while len(self.dict_editors) > 0 :
127              self.myQtab.setCurrentIndex(0)
128              res=self.handleClose(0,texte)
129              if res==2 : return res   # l utilsateur a annule
130        return res
131         
132    def handleRechercher(self):
133        #print "passage dans handleRechercher"
134        index=self.myQtab.currentIndex()
135        if index < 0 : return
136        editor=self.dict_editors[index]
137        editor.handleRechercher()
138
139    def handleRechercherDsCatalogue(self):
140        #print "passage dans handleRechercher"
141        index=self.myQtab.currentIndex()
142        if index < 0 : return
143        editor=self.dict_editors[index]
144        editor.handleRechercherDsCatalogue()
145
146    def handleDeplier(self):
147        index=self.myQtab.currentIndex()
148        if index < 0 : return
149        editor=self.dict_editors[index]
150        editor.handleDeplier()
151    
152    def handleEditCopy(self):
153        #print "passage dans handleEditCopy"
154        index=self.myQtab.currentIndex()
155        if index < 0 : return
156        editor=self.dict_editors[index]
157        editor.handleEditCopy()
158
159    def handleEditCut(self):
160        #print "passage dans handleEditCut"
161        index=self.myQtab.currentIndex()
162        if index < 0 : return
163        editor=self.dict_editors[index]
164        editor.handleEditCut()
165
166    def handleEditPaste(self):
167        #print "passage dans handleEditPaste"
168        index=self.myQtab.currentIndex()
169        if index < 0 : return
170        editor=self.dict_editors[index]
171        editor.handleEditPaste()
172
173    def handleSupprimer(self):
174        index=self.myQtab.currentIndex()
175        if index < 0 : return
176        editor=self.dict_editors[index]
177        editor.handleSupprimer()
178
179    def newEditor(self,include=0):
180        if self.appliEficas.demande==True : 
181            self.appliEficas.definitCode(None,None)
182            if self.appliEficas.code == None:return
183        maPage=self.getEditor(include=include)
184
185    def newIncludeEditor(self):
186        self.newEditor(include=1)
187
188    def handleViewJdcFichierSource(self):
189        index=self.myQtab.currentIndex()
190        if index < 0 : return
191        self.dict_editors[index].viewJdcSource()
192
193    def ouvreArbre(self):
194        index=self.myQtab.currentIndex()
195        if index < 0 : return
196        self.dict_editors[index].ouvreArbre()
197
198    def fermeArbre(self):
199        index=self.myQtab.currentIndex()
200        if index < 0 : return
201        self.dict_editors[index].fermeArbre()
202
203    def ajoutCommentaire(self):
204        index=self.myQtab.currentIndex()
205        if index < 0 : return
206        editor=self.dict_editors[index]
207        editor.ajoutCommentaire()
208
209    def handleViewJdcRegles(self):
210        index=self.myQtab.currentIndex()
211        if index < 0 : return
212        self.dict_editors[index].viewJdcRegles()
213
214    def handlegestionParam(self):
215        index=self.myQtab.currentIndex()
216        if index < 0 : 
217           QMessageBox.warning( self.appliEficas,tr(u"Creation Parametre indisponible"),tr(u"les parametres sont lies a un jeu de donnees"))
218           return
219        self.dict_editors[index].gestionParam()
220
221    def handleViewJdcRapport(self):
222        index=self.myQtab.currentIndex()
223        if index < 0 : return
224        self.dict_editors[index].viewJdcRapport()
225
226    def handleViewJdcPy(self):
227        index=self.myQtab.currentIndex()
228        if index < 0 : return
229        self.dict_editors[index].viewJdcPy()
230
231    def saveCurrentEditor(self):
232        index=self.myQtab.currentIndex()
233        if index < 0 : return
234        editor=self.dict_editors[index]
235        if editor in self.doubles :
236            QMessageBox.warning(
237                      None,
238                      tr("Fichier Duplique"),
239                      tr("Le fichier ne sera pas sauvegarde."),)
240            return
241        ok, newName = editor.saveFile()
242        if ok :
243            fileName=os.path.basename(six.text_type(newName))
244            self.myQtab.setTabText(index,fileName)
245        return ok
246
247    def saveLegerCurrentEditor(self):
248        index=self.myQtab.currentIndex()
249        if index < 0 : return
250        editor=self.dict_editors[index]
251        ok, newName = editor.saveFileLeger()
252        return ok
253
254    def sauveLigneCurrentEditor(self):
255        index=self.myQtab.currentIndex()
256        if index < 0 : return
257        editor=self.dict_editors[index]
258        if editor in self.doubles :
259            QMessageBox.warning(
260                      None,
261                      tr("Fichier Duplique"),
262                      tr("Le fichier ne sera pas sauvegarde."),)
263            return
264        ok, newName = editor.sauveLigneFile()
265        if ok :
266            fileName=os.path.basename(six.text_type(newName))
267            self.myQtab.setTabText(index,fileName)
268        return ok
269
270
271    def saveAsCurrentEditor(self):
272        index=self.myQtab.currentIndex()
273        editor=self.dict_editors[index]
274        oldName=editor.fichier
275        ok,newName = editor.saveFileAs()
276        if ok :
277            fileName=os.path.basename(six.text_type(newName))
278            self.myQtab.setTabText(index,fileName)
279        if editor in self.doubles :
280           if oldName != newName :
281              del self.doubles[editor]
282        return ok
283
284    def displayJDC(self,jdc,fn=None):
285         """
286         Public slot to display a file in an editor.
287         @param fn name of file to be opened
288         # insert filename into list of recently opened files
289         """
290         titre=None
291         if fn != None : titre=fn.split("/")[-1]
292         editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
293         self.appliEficas.addToRecentList(editor.getFileName())
294
295    def getEditor(self,fichier = None,jdc = None, units = None,include=0):
296        newWin = 0
297        double = None
298        indexEditor=0
299        for indexEditor in self.dict_editors :
300            editor=self.dict_editors[indexEditor]
301            if self.samepath(fichier, editor.getFileName()):
302               msgBox = QMessageBox()
303               msgBox.setWindowTitle(tr("Fichier"))
304               msgBox.setText(tr("Le fichier <b>%s</b> est deja ouvert", str(fichier)))
305               msgBox.addButton(tr("&Duplication"),0)
306               msgBox.addButton(tr("&Abandonner"),1)
307               abort=msgBox.exec_()
308               if abort: break
309               double=editor
310        else :
311             from .editor import JDCEditor
312             editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,vm = self,include=include)
313
314             if double != None : 
315                self.doubles[editor]=double
316             if editor.jdc: # le fichier est bien un jdc
317                 self.editors.append(editor)
318                 newWin = 1
319             else:
320                 editor.closeIt()
321
322        if newWin:
323             self.addView(editor, fichier)
324        elif editor.jdc:
325             self.myQtab.setCurrentIndex(indexEditor)
326
327        index=self.myQtab.currentIndex()
328        if index != -1 :
329           self.dict_editors[index]=editor
330        return editor
331
332    def addView(self, win, fichier=None):
333 #PNPNPNPN --> a affiner
334         if fichier is None:
335             self.untitledCount += 1
336             self.myQtab.addTab(win, tr("Fichier non encore nomme ", self.untitledCount))
337             #self.myQtab.addTab(win, str(self.appliEficas.code))
338         else:
339             liste=fichier.split('/')
340             txt =  liste[-1]
341             if not QFileInfo(fichier).isWritable():
342                 txt = '%s (ro)' % txt
343             self.myQtab.addTab(win,txt )
344         self.myQtab.setCurrentWidget(win)
345         self.currentEditor=win
346         win.setFocus()
347
348    def getOpenStartDir(self) :
349        #PN --> Les Preferences
350         try :
351             userDir=os.path.expanduser("~/Eficas_install/")
352             return userDir
353         except :
354             return ""
355
356    def samepath(self,f1, f2):
357     """
358     compare two paths.
359     """
360     if f1 is None or f2 is None: return 0
361     if os.path.normcase(os.path.normpath(f1)) == os.path.normcase(os.path.normpath(f2)) : return 1
362     return 0
363
364
365    def checkDirty(self, editor,texte):
366         """
367         Private method to check dirty status and open a message window.
368         
369         @param editor editor window to check
370         @return flag indicating successful reset of the dirty flag (boolean)
371         """        
372         res=1 
373         if (editor.modified) and (editor in self.doubles) :
374             msgBox = QMessageBox(None)
375             msgBox.setWindowTitle(tr("Fichier Duplique"))
376             msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
377             msgBox.addButton(texte,0)
378             msgBox.addButton(tr("&Annuler"),1)
379             res=msgBox.exec_()
380             if res == 0 : return 1
381             return 2
382         if editor.modified:
383             fn = editor.getFileName()
384             if fn is None: fn = tr('Noname')
385             msgBox = QMessageBox(None)
386             msgBox.setWindowTitle(tr("Fichier Modifie"))
387             msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
388             msgBox.addButton(tr("&Sauvegarder"),1)
389             msgBox.addButton(tr("&Quitter sans sauvegarder"),0)
390             res=msgBox.exec_()
391             if res == 2 : res = 1
392             if res == 0:
393                 (ok, newName) = editor.saveFile()
394                 if ok:
395                     fileName=os.path.basename(six.text_type(newName))
396                     index=self.myQtab.currentIndex()
397                     self.myQtab.setTabText(index,fileName)
398                 return ok
399         return res
400
401    def handleAjoutGroup(self,listeGroup):
402        index=self.myQtab.currentIndex()
403        if index < 0 : return
404        editor=self.dict_editors[index]
405        editor.handleAjoutGroup(listeGroup)