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