1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017 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
30 from PyQt5.QtWidgets import QFileDialog, QMessageBox
31 from PyQt5.QtCore import QFileInfo
33 DictExtensions= {"MAP" : ".map"}
34 class MyViewManager(object):
36 def __init__(self,appliEficas):
37 self.appliEficas=appliEficas
40 self.appliEficas=appliEficas
43 self.untitledCount = 0
46 self.myQtab = self.appliEficas.myQtab
48 self.myQtab.currentChanged.connect(self.indexChanged)
49 self.myQtab.tabCloseRequested.connect(self.closeTab)
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.maConfiguration=editor.maConfiguration
56 self.appliEficas.code=editor.maConfiguration.code
57 self.appliEficas.setWindowTitle(editor.titre)
58 self.appliEficas.construitMenu()
60 def handleOpen(self,fichier=None,patron=0,units=None):
63 if self.appliEficas.demande==True :
64 self.appliEficas.definitCode(None,None)
65 if self.appliEficas.code == None:return
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 (*)')
72 extensions=tr('Fichiers JDC (*.comm);;''Tous les Fichiers (*)')
74 fichier = QFileDialog.getOpenFileName(self.appliEficas,
76 self.appliEficas.maConfiguration.savedir,
79 if len(fichier) == 0: return None
80 fichier = os.path.abspath(six.text_type(fichier))
81 ulfile = os.path.abspath(six.text_type(fichier))
82 self.appliEficas.maConfiguration.savedir=os.path.split(ulfile)[0]
83 self.appliEficas.addToRecentList(fichier)
84 maPage=self.getEditor( fichier,units=units)
85 if maPage: result = maPage
86 if maPage : self.myQtab.setTabText(self.myQtab.indexOf(maPage),os.path.basename(fichier))
89 def closeTab(self,indexAFermer):
90 self.handleClose(indexAFermer = indexAFermer)
92 def handleClose(self,doitSauverRecent = 1,texte=tr('&Quitter'),indexAFermer=None):
93 if doitSauverRecent : self.appliEficas.sauveRecents()
94 if indexAFermer == None :index=self.myQtab.currentIndex()
95 else : index = indexAFermer
97 res=self.checkDirty(self.dict_editors[index],texte)
98 if res == 2 : return 2 # l utilisateur a annule
100 while idx < len(self.dict_editors) -1 :
101 self.dict_editors[idx]=self.dict_editors[idx+1]
103 del self.dict_editors[len (self.dict_editors) -1]
105 del self.doubles[self.dict_editors[index]]
108 self.myQtab.removeTab(index)
113 index=self.myQtab.currentIndex()
114 if index < 0 : return
115 editor=self.dict_editors[index]
119 index=self.myQtab.currentIndex()
120 if index < 0 : return
121 editor=self.dict_editors[index]
124 def handleCloseAll(self,texte=tr('Quitter')):
126 self.appliEficas.sauveRecents()
127 while len(self.dict_editors) > 0 :
128 self.myQtab.setCurrentIndex(0)
129 res=self.handleClose(0,texte)
130 if res==2 : return res # l utilsateur a annule
133 def handleRechercher(self):
134 #print "passage dans handleRechercher"
135 index=self.myQtab.currentIndex()
136 if index < 0 : return
137 editor=self.dict_editors[index]
138 editor.handleRechercher()
140 def handleRechercherDsCatalogue(self):
141 #print "passage dans handleRechercher"
142 index=self.myQtab.currentIndex()
143 if index < 0 : return
144 editor=self.dict_editors[index]
145 editor.handleRechercherDsCatalogue()
147 def handleDeplier(self):
148 index=self.myQtab.currentIndex()
149 if index < 0 : return
150 editor=self.dict_editors[index]
151 editor.handleDeplier()
153 def handleEditCopy(self):
154 #print "passage dans handleEditCopy"
155 index=self.myQtab.currentIndex()
156 if index < 0 : return
157 editor=self.dict_editors[index]
158 editor.handleEditCopy()
160 def handleEditCut(self):
161 #print "passage dans handleEditCut"
162 index=self.myQtab.currentIndex()
163 if index < 0 : return
164 editor=self.dict_editors[index]
165 editor.handleEditCut()
167 def handleEditPaste(self):
168 #print "passage dans handleEditPaste"
169 index=self.myQtab.currentIndex()
170 if index < 0 : return
171 editor=self.dict_editors[index]
172 editor.handleEditPaste()
174 def handleSupprimer(self):
175 index=self.myQtab.currentIndex()
176 if index < 0 : return
177 editor=self.dict_editors[index]
178 editor.handleSupprimer()
180 def handleAjoutEtape(self,nomEtape):
181 index=self.myQtab.currentIndex()
182 if index < 0 : return
183 editor=self.dict_editors[index]
184 editor.handleAjoutEtape(nomEtape)
187 def newEditor(self,include=0):
188 if self.appliEficas.demande==True :
189 self.appliEficas.definitCode(None,None)
190 if self.appliEficas.code == None:return
191 maPage=self.getEditor(include=include)
193 def newIncludeEditor(self):
194 self.newEditor(include=1)
196 def handleViewJdcFichierSource(self):
197 index=self.myQtab.currentIndex()
198 if index < 0 : return
199 self.dict_editors[index].viewJdcSource()
201 def ouvreArbre(self):
202 index=self.myQtab.currentIndex()
203 if index < 0 : return
204 self.dict_editors[index].ouvreArbre()
206 def fermeArbre(self):
207 index=self.myQtab.currentIndex()
208 if index < 0 : return
209 self.dict_editors[index].fermeArbre()
211 def ajoutCommentaire(self):
212 index=self.myQtab.currentIndex()
213 if index < 0 : return
214 editor=self.dict_editors[index]
215 editor.ajoutCommentaire()
217 def handleViewJdcRegles(self):
218 index=self.myQtab.currentIndex()
219 if index < 0 : return
220 self.dict_editors[index].viewJdcRegles()
222 def handleGestionParam(self):
223 index=self.myQtab.currentIndex()
225 QMessageBox.warning( self.appliEficas,tr(u"Creation Parametre indisponible"),tr(u"les parametres sont lies a un jeu de donnees"))
227 self.dict_editors[index].gestionParam()
229 def handleViewJdcRapport(self):
230 index=self.myQtab.currentIndex()
231 if index < 0 : return
232 self.dict_editors[index].viewJdcRapport()
234 def handleViewJdcPy(self):
235 index=self.myQtab.currentIndex()
236 if index < 0 : return
237 self.dict_editors[index].viewJdcPy()
239 def saveCurrentEditor(self):
240 index=self.myQtab.currentIndex()
241 if index < 0 : return
242 editor=self.dict_editors[index]
243 if editor in self.doubles :
246 tr("Fichier Duplique"),
247 tr("Le fichier ne sera pas sauvegarde."),)
249 ok, newName = editor.saveFile()
251 fileName=os.path.basename(six.text_type(newName))
252 self.myQtab.setTabText(index,fileName)
255 def saveCompleteCurrentEditor(self):
256 index=self.myQtab.currentIndex()
257 if index < 0 : return
258 editor=self.dict_editors[index]
259 ok, newName = editor.saveCompleteFile()
262 def sauveLigneCurrentEditor(self):
263 index=self.myQtab.currentIndex()
264 if index < 0 : return
265 editor=self.dict_editors[index]
266 if editor in self.doubles :
269 tr("Fichier Duplique"),
270 tr("Le fichier ne sera pas sauvegarde."),)
272 ok, newName = editor.sauveLigneFile()
274 fileName=os.path.basename(six.text_type(newName))
275 self.myQtab.setTabText(index,fileName)
279 def saveAsCurrentEditor(self):
280 index=self.myQtab.currentIndex()
281 editor=self.dict_editors[index]
282 oldName=editor.fichier
283 ok,newName = editor.saveFileAs()
285 fileName=os.path.basename(six.text_type(newName))
286 self.myQtab.setTabText(index,fileName)
287 if editor in self.doubles :
288 if oldName != newName :
289 del self.doubles[editor]
292 def displayJDC(self,jdc,fn=None):
294 Public slot to display a file in an editor.
295 @param fn name of file to be opened
296 # insert filename into list of recently opened files
299 if fn != None : titre=fn.split("/")[-1]
300 editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
301 self.appliEficas.addToRecentList(editor.getFileName())
303 def getEditor(self,fichier = None,jdc = None, units = None,include=0):
307 for indexEditor in self.dict_editors :
308 editor=self.dict_editors[indexEditor]
309 if self.samePath(fichier, editor.getFileName()):
310 msgBox = QMessageBox()
311 msgBox.setWindowTitle(tr("Fichier"))
312 msgBox.setText(tr("Le fichier <b>%s</b> est deja ouvert", str(fichier)))
313 msgBox.addButton(tr("&Duplication"),0)
314 msgBox.addButton(tr("&Abandonner"),1)
319 from .editor import JDCEditor
320 editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,include=include)
322 self.doubles[editor]=double
323 if editor.jdc: # le fichier est bien un jdc
324 self.editors.append(editor)
330 self.addView(editor, fichier)
332 self.myQtab.setCurrentIndex(indexEditor)
334 index=self.myQtab.currentIndex()
336 self.dict_editors[index]=editor
339 def addView(self, win, fichier=None):
340 #PNPNPNPN --> a affiner
342 self.untitledCount += 1
343 self.myQtab.addTab(win, tr("Fichier non encore nomme " + self.appliEficas.readercata.versionCode, self.untitledCount))
344 #self.myQtab.addTab(win, str(self.appliEficas.code))
346 liste=fichier.split('/')
348 if not QFileInfo(fichier).isWritable():
349 txt = '%s (ro)' % txt
350 self.myQtab.addTab(win,txt )
351 self.myQtab.setCurrentWidget(win)
352 self.currentEditor=win
355 def getOpenStartDir(self) :
356 #PN --> Les Preferences
358 userDir=os.path.expanduser("~/Eficas_install/")
363 def samePath(self,f1, f2):
367 if f1 is None or f2 is None: return 0
368 if os.path.normcase(os.path.normpath(f1)) == os.path.normcase(os.path.normpath(f2)) : return 1
372 def checkDirty(self, editor,texte):
374 Private method to check dirty status and open a message window.
376 @param editor editor window to check
377 @return flag indicating successful reset of the dirty flag (boolean)
380 if (editor.modified) and (editor in self.doubles) :
381 msgBox = QMessageBox(None)
382 msgBox.setWindowTitle(tr("Fichier Duplique"))
383 msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
384 msgBox.addButton(texte,0)
385 msgBox.addButton(tr("&Annuler"),1)
387 if res == 0 : return 1
390 fn = editor.getFileName()
391 if fn is None: fn = tr('Noname')
392 msgBox = QMessageBox(None)
393 msgBox.setWindowTitle(tr("Fichier Modifie"))
394 msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
395 msgBox.addButton(tr("&Sauvegarder"),1)
396 msgBox.addButton(tr("&Quitter sans sauvegarder"),0)
397 msgBox.addButton(tr("&Annuler"),2)
399 if res == 2 : return res
401 (ok, newName) = editor.saveFile()
403 fileName=os.path.basename(six.text_type(newName))
404 index=self.myQtab.currentIndex()
405 self.myQtab.setTabText(index,fileName)
409 def handleAjoutGroup(self,listeGroup):
410 index=self.myQtab.currentIndex()
411 if index < 0 : return
412 editor=self.dict_editors[index]
413 editor.handleAjoutGroup(listeGroup)
415 def handleFonctionUtilisateur(self,laFonctionUtilisateur, lesArguments):
416 # Peut-etre a blinder un peu plus sur le nb d argument
417 index=self.myQtab.currentIndex()
418 if index < 0 : return
419 editor=self.dict_editors[index]
420 if editor.getEtapeCourante() == None :
421 QMessageBox.information( self.appliEficas,
422 tr("Selectionner une etape"),
423 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"))
427 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 apply(laFonctionUtilisateur,listeParam)