1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 from __future__ import absolute_import
23 from builtins import str
24 from builtins import object
28 from Extensions.i18n import tr
29 from PyQt5.QtWidgets import QFileDialog, QMessageBox
30 from PyQt5.QtCore import QFileInfo
32 DictExtensions= {"MAP" : ".map"}
33 class MyViewManager(object):
35 def __init__(self,appliEficas):
36 self.appliEficas=appliEficas
39 self.appliEficas=appliEficas
42 self.untitledCount = 0
45 self.myQtab = self.appliEficas.myQtab
47 self.myQtab.currentChanged.connect(self.indexChanged)
48 self.myQtab.tabCloseRequested.connect(self.closeTab)
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()
62 def handleOpen(self,fichier=None,patron=0,units=None):
65 if self.appliEficas.demande==True :
66 self.appliEficas.definitCode(None,None)
67 if self.appliEficas.code == None:return
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 (*)')
74 extensions=tr('Fichiers JDC (*.comm);;''Tous les Fichiers (*)')
76 fichier = QFileDialog.getOpenFileName(self.appliEficas,
78 self.appliEficas.maConfiguration.savedir,
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))
91 def closeTab(self,indexAFermer):
92 self.handleClose(indexAFermer = indexAFermer)
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
99 res=self.checkDirty(self.dictEditors[index],texte)
100 if res == 2 : return 2 # l utilisateur a annule
102 while idx < len(self.dictEditors) -1 :
103 self.dictEditors[idx]=self.dictEditors[idx+1]
105 del self.dictEditors[len (self.dictEditors) -1]
107 del self.doubles[self.dictEditors[index]]
110 self.myQtab.removeTab(index)
115 index=self.myQtab.currentIndex()
116 if index < 0 : return
117 editor=self.dictEditors[index]
121 index=self.myQtab.currentIndex()
122 if index < 0 : return
123 editor=self.dictEditors[index]
126 def handleCloseAll(self,texte=tr('Quitter')):
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
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()
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()
149 def handleDeplier(self):
150 index=self.myQtab.currentIndex()
151 if index < 0 : return
152 editor=self.dictEditors[index]
153 editor.handleDeplier()
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()
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()
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()
176 def handleSupprimer(self):
177 index=self.myQtab.currentIndex()
178 if index < 0 : return
179 editor=self.dictEditors[index]
180 editor.handleSupprimer()
182 def handleAjoutEtape(self,nomEtape):
183 index=self.myQtab.currentIndex()
184 if index < 0 : return
185 editor=self.dictEditors[index]
186 editor.handleAjoutEtape(nomEtape)
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)
195 def newIncludeEditor(self):
196 self.newEditor(include=1)
198 def handleViewJdcFichierSource(self):
199 index=self.myQtab.currentIndex()
200 if index < 0 : return
201 self.dictEditors[index].viewJdcSource()
203 def ouvreArbre(self):
204 index=self.myQtab.currentIndex()
205 if index < 0 : return
206 self.dictEditors[index].ouvreArbre()
208 def fermeArbre(self):
209 index=self.myQtab.currentIndex()
210 if index < 0 : return
211 self.dictEditors[index].fermeArbre()
213 def ajoutCommentaire(self):
214 index=self.myQtab.currentIndex()
215 if index < 0 : return
216 editor=self.dictEditors[index]
217 editor.ajoutCommentaire()
219 def handleViewJdcRegles(self):
220 index=self.myQtab.currentIndex()
221 if index < 0 : return
222 self.dictEditors[index].viewJdcRegles()
224 def handleGestionParam(self):
225 index=self.myQtab.currentIndex()
227 QMessageBox.warning( self.appliEficas,tr(u"Creation Parametre indisponible"),tr(u"les parametres sont lies a un jeu de donnees"))
229 self.dictEditors[index].gestionParam()
231 def handleViewJdcRapport(self):
232 index=self.myQtab.currentIndex()
233 if index < 0 : return
234 self.dictEditors[index].viewJdcRapport()
236 def handleViewJdcPy(self):
237 index=self.myQtab.currentIndex()
238 if index < 0 : return
239 self.dictEditors[index].viewJdcPy()
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 :
248 tr("Fichier Duplique"),
249 tr("Le fichier ne sera pas sauvegarde."),)
251 ok, newName = editor.saveFile()
253 fileName=os.path.basename(newName)
254 self.myQtab.setTabText(index,fileName)
257 def saveCompleteCurrentEditor(self):
258 index=self.myQtab.currentIndex()
259 if index < 0 : return
260 editor=self.dictEditors[index]
261 ok, newName = editor.saveCompleteFile()
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 :
271 tr("Fichier Duplique"),
272 tr("Le fichier ne sera pas sauvegarde."),)
274 ok, newName = editor.sauveLigneFile()
276 fileName=os.path.basename(newName)
277 self.myQtab.setTabText(index,fileName)
280 def saveAsCurrentEditor(self):
281 index=self.myQtab.currentIndex()
282 editor=self.dictEditors[index]
283 oldName=editor.fichier
284 ok,newName = editor.saveFileAs()
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]
293 def displayJDC(self,jdc,fn=None):
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
300 if fn != None : titre=fn.split("/")[-1]
301 editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
302 self.appliEficas.addToRecentList(editor.getFileName())
304 def getEditor(self,fichier = None,jdc = None, units = None,include=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)
320 from .editor import JDCEditor
321 editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,include=include)
323 self.doubles[editor]=double
324 if editor.jdc: # le fichier est bien un jdc
325 self.editors.append(editor)
331 self.addView(editor, fichier)
333 self.myQtab.setCurrentIndex(indexEditor)
335 index=self.myQtab.currentIndex()
337 self.dictEditors[index]=editor
340 def addView(self, win, fichier=None):
341 #PNPNPNPN --> a affiner
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))
347 liste=fichier.split('/')
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
356 def getOpenStartDir(self) :
357 #PN --> Les Preferences
359 userDir=os.path.expanduser("~/Eficas_install/")
364 def samePath(self,f1, f2):
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
373 def checkDirty(self, editor,texte):
375 Private method to check dirty status and open a message window.
377 @param editor editor window to check
378 @return flag indicating successful reset of the dirty flag (boolean)
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)
388 if res == 0 : return 1
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)
400 if res == 2 : return res
402 (ok, newName) = editor.saveFile()
404 fileName=os.path.basename(newName)
405 index=self.myQtab.currentIndex()
406 self.myQtab.setTabText(index,fileName)
410 def handleAjoutGroup(self,listeGroup):
411 index=self.myQtab.currentIndex()
412 if index < 0 : return
413 editor=self.dictEditors[index]
414 editor.handleAjoutGroup(listeGroup)
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"))
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)