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
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.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()
59 def handleOpen(self,fichier=None,patron=0,units=None):
62 if self.appliEficas.demande==True :
63 self.appliEficas.definitCode(None,None)
64 if self.appliEficas.code == None:return
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 (*)')
71 extensions=tr('Fichiers JDC (*.comm);;''Tous les Fichiers (*)')
73 fichier = QFileDialog.getOpenFileName(self.appliEficas,
75 self.appliEficas.maConfiguration.savedir,
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))
88 def closeTab(self,indexAFermer):
89 self.handleClose(indexAFermer = indexAFermer)
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
96 res=self.checkDirty(self.dict_editors[index],texte)
97 if res == 2 : return 2 # l utilisateur a annule
99 while idx < len(self.dict_editors) -1 :
100 self.dict_editors[idx]=self.dict_editors[idx+1]
102 del self.dict_editors[len (self.dict_editors) -1]
104 del self.doubles[self.dict_editors[index]]
107 self.myQtab.removeTab(index)
112 index=self.myQtab.currentIndex()
113 if index < 0 : return
114 editor=self.dict_editors[index]
118 index=self.myQtab.currentIndex()
119 if index < 0 : return
120 editor=self.dict_editors[index]
123 def handleCloseAll(self,texte=tr('Quitter')):
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
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()
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()
146 def handleDeplier(self):
147 index=self.myQtab.currentIndex()
148 if index < 0 : return
149 editor=self.dict_editors[index]
150 editor.handleDeplier()
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()
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()
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()
173 def handleSupprimer(self):
174 index=self.myQtab.currentIndex()
175 if index < 0 : return
176 editor=self.dict_editors[index]
177 editor.handleSupprimer()
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)
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)
192 def newIncludeEditor(self):
193 self.newEditor(include=1)
195 def handleViewJdcFichierSource(self):
196 index=self.myQtab.currentIndex()
197 if index < 0 : return
198 self.dict_editors[index].viewJdcSource()
200 def ouvreArbre(self):
201 index=self.myQtab.currentIndex()
202 if index < 0 : return
203 self.dict_editors[index].ouvreArbre()
205 def fermeArbre(self):
206 index=self.myQtab.currentIndex()
207 if index < 0 : return
208 self.dict_editors[index].fermeArbre()
210 def ajoutCommentaire(self):
211 index=self.myQtab.currentIndex()
212 if index < 0 : return
213 editor=self.dict_editors[index]
214 editor.ajoutCommentaire()
216 def handleViewJdcRegles(self):
217 index=self.myQtab.currentIndex()
218 if index < 0 : return
219 self.dict_editors[index].viewJdcRegles()
221 def handleGestionParam(self):
222 index=self.myQtab.currentIndex()
224 QMessageBox.warning( self.appliEficas,tr(u"Creation Parametre indisponible"),tr(u"les parametres sont lies a un jeu de donnees"))
226 self.dict_editors[index].gestionParam()
228 def handleViewJdcRapport(self):
229 index=self.myQtab.currentIndex()
230 if index < 0 : return
231 self.dict_editors[index].viewJdcRapport()
233 def handleViewJdcPy(self):
234 index=self.myQtab.currentIndex()
235 if index < 0 : return
236 self.dict_editors[index].viewJdcPy()
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 :
245 tr("Fichier Duplique"),
246 tr("Le fichier ne sera pas sauvegarde."),)
248 ok, newName = editor.saveFile()
250 fileName=os.path.basename(newName)
251 self.myQtab.setTabText(index,fileName)
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()
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 :
268 tr("Fichier Duplique"),
269 tr("Le fichier ne sera pas sauvegarde."),)
271 ok, newName = editor.sauveLigneFile()
273 fileName=os.path.basename(newName)
274 self.myQtab.setTabText(index,fileName)
278 def saveAsCurrentEditor(self):
279 index=self.myQtab.currentIndex()
280 editor=self.dict_editors[index]
281 oldName=editor.fichier
282 ok,newName = editor.saveFileAs()
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]
291 def displayJDC(self,jdc,fn=None):
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
298 if fn != None : titre=fn.split("/")[-1]
299 editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
300 self.appliEficas.addToRecentList(editor.getFileName())
302 def getEditor(self,fichier = None,jdc = None, units = None,include=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)
318 from .editor import JDCEditor
319 editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,include=include)
321 self.doubles[editor]=double
322 if editor.jdc: # le fichier est bien un jdc
323 self.editors.append(editor)
329 self.addView(editor, fichier)
331 self.myQtab.setCurrentIndex(indexEditor)
333 index=self.myQtab.currentIndex()
335 self.dict_editors[index]=editor
338 def addView(self, win, fichier=None):
339 #PNPNPNPN --> a affiner
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))
345 liste=fichier.split('/')
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
354 def getOpenStartDir(self) :
355 #PN --> Les Preferences
357 userDir=os.path.expanduser("~/Eficas_install/")
362 def samePath(self,f1, f2):
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
371 def checkDirty(self, editor,texte):
373 Private method to check dirty status and open a message window.
375 @param editor editor window to check
376 @return flag indicating successful reset of the dirty flag (boolean)
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)
386 if res == 0 : return 1
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)
398 if res == 2 : return res
400 (ok, newName) = editor.saveFile()
402 fileName=os.path.basename(newName)
403 index=self.myQtab.currentIndex()
404 self.myQtab.setTabText(index,fileName)
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)
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"))
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)