Salome HOME
modif pour MT
[tools/eficas.git] / InterfaceQT4 / viewManager.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017   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 from  PyQt5.QtWidgets  import QFileDialog, QMessageBox
30 from  PyQt5.QtCore     import QFileInfo
31
32 DictExtensions= {"MAP" : ".map"}
33 class MyViewManager(object):
34
35    def __init__(self,appliEficas):
36        self.appliEficas=appliEficas
37        self.tabWidgets = []
38        self.mesIndexes = {}
39        self.appliEficas=appliEficas
40        self.editors = []
41        self.dict_editors={}
42        self.untitledCount = 0
43        self.doubles = {}
44
45        self.myQtab = self.appliEficas.myQtab
46
47        self.myQtab.currentChanged.connect(self.indexChanged)
48        self.myQtab.tabCloseRequested.connect(self.closeTab)
49         
50    def indexChanged(self):
51        index=self.myQtab.currentIndex()
52        if index in self.dict_editors:
53            editor=self.dict_editors[index]
54            self.appliEficas.maConfiguration=editor.maConfiguration
55            self.appliEficas.code=editor.maConfiguration.code
56            self.appliEficas.setWindowTitle(editor.titre)
57            self.appliEficas.construitMenu()
58
59    def handleOpen(self,fichier=None,patron=0,units=None):
60        result = None
61        if fichier is None:
62             if self.appliEficas.demande==True : 
63                self.appliEficas.definitCode(None,None)
64                if self.appliEficas.code == None:return
65             
66             if self.appliEficas.code in DictExtensions:
67                chaine="JDC (*"+DictExtensions[self.appliEficas.code]+");;"
68                extensions=tr(chaine+ "All Files (*)")
69             elif self.appliEficas.code== "TELEMAC" : extensions=tr('CAS (*.cas);;All Files (*)')
70             else :
71                extensions=tr('Fichiers JDC (*.comm);;''Tous les Fichiers (*)')
72
73             fichier = QFileDialog.getOpenFileName(self.appliEficas,
74                         tr('Ouvrir Fichier'),
75                         self.appliEficas.maConfiguration.savedir,
76                          extensions)
77             fichier=fichier[0]
78        if len(fichier) == 0: return None
79        fichier = os.path.abspath(fichier)
80        ulfile = os.path.abspath(fichier)
81        self.appliEficas.maConfiguration.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,indexAFermer):
89        self.handleClose(indexAFermer = indexAFermer)
90
91    def handleClose(self,doitSauverRecent = 1,texte=tr('&Quitter'),indexAFermer=None):
92        if doitSauverRecent : self.appliEficas.sauveRecents()
93        if indexAFermer == None :index=self.myQtab.currentIndex()
94        else : index = indexAFermer
95        if index < 0 : return
96        res=self.checkDirty(self.dict_editors[index],texte)
97        if res == 2 : return 2             # l utilisateur a annule
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 handleAjoutEtape(self,nomEtape):
180        index=self.myQtab.currentIndex()
181        if index < 0 : return
182        editor=self.dict_editors[index]
183        editor.handleAjoutEtape(nomEtape)
184
185
186    def newEditor(self,include=0):
187        if self.appliEficas.demande==True : 
188            self.appliEficas.definitCode(None,None)
189            if self.appliEficas.code == None:return
190        maPage=self.getEditor(include=include)
191
192    def newIncludeEditor(self):
193        self.newEditor(include=1)
194
195    def handleViewJdcFichierSource(self):
196        index=self.myQtab.currentIndex()
197        if index < 0 : return
198        self.dict_editors[index].viewJdcSource()
199
200    def ouvreArbre(self):
201        index=self.myQtab.currentIndex()
202        if index < 0 : return
203        self.dict_editors[index].ouvreArbre()
204
205    def fermeArbre(self):
206        index=self.myQtab.currentIndex()
207        if index < 0 : return
208        self.dict_editors[index].fermeArbre()
209
210    def ajoutCommentaire(self):
211        index=self.myQtab.currentIndex()
212        if index < 0 : return
213        editor=self.dict_editors[index]
214        editor.ajoutCommentaire()
215
216    def handleViewJdcRegles(self):
217        index=self.myQtab.currentIndex()
218        if index < 0 : return
219        self.dict_editors[index].viewJdcRegles()
220
221    def handleGestionParam(self):
222        index=self.myQtab.currentIndex()
223        if index < 0 : 
224           QMessageBox.warning( self.appliEficas,tr(u"Creation Parametre indisponible"),tr(u"les parametres sont lies a un jeu de donnees"))
225           return
226        self.dict_editors[index].gestionParam()
227
228    def handleViewJdcRapport(self):
229        index=self.myQtab.currentIndex()
230        if index < 0 : return
231        self.dict_editors[index].viewJdcRapport()
232
233    def handleViewJdcPy(self):
234        index=self.myQtab.currentIndex()
235        if index < 0 : return
236        self.dict_editors[index].viewJdcPy()
237
238    def saveCurrentEditor(self):
239        index=self.myQtab.currentIndex()
240        if index < 0 : return
241        editor=self.dict_editors[index]
242        if editor in self.doubles :
243            QMessageBox.warning(
244                      None,
245                      tr("Fichier Duplique"),
246                      tr("Le fichier ne sera pas sauvegarde."),)
247            return
248        ok, newName = editor.saveFile()
249        if ok :
250            fileName=os.path.basename(newName)
251            self.myQtab.setTabText(index,fileName)
252        return ok
253
254    def saveCompleteCurrentEditor(self):
255        index=self.myQtab.currentIndex()
256        if index < 0 : return
257        editor=self.dict_editors[index]
258        ok, newName = editor.saveCompleteFile()
259        return ok
260
261    def sauveLigneCurrentEditor(self):
262        index=self.myQtab.currentIndex()
263        if index < 0 : return
264        editor=self.dict_editors[index]
265        if editor in self.doubles :
266            QMessageBox.warning(
267                      None,
268                      tr("Fichier Duplique"),
269                      tr("Le fichier ne sera pas sauvegarde."),)
270            return
271        ok, newName = editor.sauveLigneFile()
272        if ok :
273            fileName=os.path.basename(newName)
274            self.myQtab.setTabText(index,fileName)
275        return ok
276
277
278    def saveAsCurrentEditor(self):
279        index=self.myQtab.currentIndex()
280        editor=self.dict_editors[index]
281        oldName=editor.fichier
282        ok,newName = editor.saveFileAs()
283        if ok :
284            fileName=os.path.basename(newName)
285            self.myQtab.setTabText(index,fileName)
286        if editor in self.doubles :
287           if oldName != newName :
288              del self.doubles[editor]
289        return ok
290
291    def displayJDC(self,jdc,fn=None):
292         """
293         Public slot to display a file in an editor.
294         @param fn name of file to be opened
295         # insert filename into list of recently opened files
296         """
297         titre=None
298         if fn != None : titre=fn.split("/")[-1]
299         editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
300         self.appliEficas.addToRecentList(editor.getFileName())
301
302    def getEditor(self,fichier = None,jdc = None, units = None,include=0):
303        newWin = 0
304        double = None
305        indexEditor=0
306        for indexEditor in self.dict_editors :
307            editor=self.dict_editors[indexEditor]
308            if self.samePath(fichier, editor.getFileName()):
309               msgBox = QMessageBox()
310               msgBox.setWindowTitle(tr("Fichier"))
311               msgBox.setText(tr("Le fichier <b>%s</b> est deja ouvert", str(fichier)))
312               msgBox.addButton(tr("&Duplication"),0)
313               msgBox.addButton(tr("&Abandonner"),1)
314               abort=msgBox.exec_()
315               if abort: break
316               double=editor
317        else :
318             from .editor import JDCEditor
319             editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,include=include)
320             if double != None : 
321                self.doubles[editor]=double
322             if editor.jdc: # le fichier est bien un jdc
323                 self.editors.append(editor)
324                 newWin = 1
325             else:
326                 editor.closeIt()
327
328        if newWin:
329             self.addView(editor, fichier)
330        elif editor.jdc:
331             self.myQtab.setCurrentIndex(indexEditor)
332
333        index=self.myQtab.currentIndex()
334        if index != -1 :
335           self.dict_editors[index]=editor
336        return editor
337
338    def addView(self, win, fichier=None):
339 #PNPNPNPN --> a affiner
340         if fichier is None:
341             self.untitledCount += 1
342             self.myQtab.addTab(win, tr("Fichier non encore nomme") +  str(self.untitledCount))
343             #self.myQtab.addTab(win, str(self.appliEficas.code))
344         else:
345             liste=fichier.split('/')
346             txt =  liste[-1]
347             if not QFileInfo(fichier).isWritable():
348                 txt = '%s (ro)' % txt
349             self.myQtab.addTab(win,txt )
350         self.myQtab.setCurrentWidget(win)
351         self.currentEditor=win
352         win.setFocus()
353
354    def getOpenStartDir(self) :
355        #PN --> Les Preferences
356         try :
357             userDir=os.path.expanduser("~/Eficas_install/")
358             return userDir
359         except :
360             return ""
361
362    def samePath(self,f1, f2):
363     """
364     compare two paths.
365     """
366     if f1 is None or f2 is None: return 0
367     if os.path.normcase(os.path.normpath(f1)) == os.path.normcase(os.path.normpath(f2)) : return 1
368     return 0
369
370
371    def checkDirty(self, editor,texte):
372         """
373         Private method to check dirty status and open a message window.
374         
375         @param editor editor window to check
376         @return flag indicating successful reset of the dirty flag (boolean)
377         """        
378         res=1 
379         if (editor.modified) and (editor in self.doubles) :
380             msgBox = QMessageBox(None)
381             msgBox.setWindowTitle(tr("Fichier Duplique"))
382             msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
383             msgBox.addButton(texte,0)
384             msgBox.addButton(tr("&Annuler"),1)
385             res=msgBox.exec_()
386             if res == 0 : return 1
387             return 2
388         if editor.modified:
389             fn = editor.getFileName()
390             if fn is None: fn = tr('Noname')
391             msgBox = QMessageBox(None)
392             msgBox.setWindowTitle(tr("Fichier Modifie"))
393             msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
394             msgBox.addButton(tr("&Sauvegarder"),1)
395             msgBox.addButton(tr("&Quitter sans sauvegarder"),0)
396             msgBox.addButton(tr("&Annuler"),2)
397             res=msgBox.exec_()
398             if res == 2 : return res
399             if res == 0:
400                 (ok, newName) = editor.saveFile()
401                 if ok:
402                     fileName=os.path.basename(newName)
403                     index=self.myQtab.currentIndex()
404                     self.myQtab.setTabText(index,fileName)
405                 return ok
406         return res
407
408    def handleAjoutGroup(self,listeGroup):
409        index=self.myQtab.currentIndex()
410        if index < 0 : return
411        editor=self.dict_editors[index]
412        editor.handleAjoutGroup(listeGroup)
413   
414    def handleFonctionUtilisateur(self,laFonctionUtilisateur, lesArguments):
415        # Peut-etre a blinder un peu plus sur le nb d argument
416        index=self.myQtab.currentIndex()
417        if index < 0 : return
418        editor=self.dict_editors[index]
419        if editor.getEtapeCourante() == None :
420           QMessageBox.information( self.appliEficas,
421                       tr("Selectionner une etape"),
422                       tr("Le texte ne peut pas etre insere dans un fichier vide,\nCreer un materiau vide ou une zone vide et inserer le catalogue apres"))
423           return
424
425        listeParam = [] 
426        for p in lesArguments:
427           if hasattr(editor,p): listeParam.append(getattr(editor,p))
428           if p=="editor"      : listeParam.append(editor)
429           if p=="etapeCourante" : listeParam.append(editor.getEtapeCourante())
430        laFonctionUtilisateur(*listeParam)
431