]> SALOME platform Git repositories - tools/eficas.git/blob - InterfaceQT4/viewManager.py
Salome HOME
Merge branch 'master' of https://git.forge.pleiade.edf.fr/git/eficas
[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 import six
30 from  PyQt5.QtWidgets  import QFileDialog, QMessageBox
31 from  PyQt5.QtCore     import QFileInfo
32
33 DictExtensions= {"MAP" : ".map"}
34 class MyViewManager(object):
35
36    def __init__(self,appliEficas):
37        self.appliEficas=appliEficas
38        self.tabWidgets = []
39        self.mesIndexes = {}
40        self.appliEficas=appliEficas
41        self.editors = []
42        self.dict_editors={}
43        self.untitledCount = 0
44        self.doubles = {}
45
46        self.myQtab = self.appliEficas.myQtab
47
48        self.myQtab.currentChanged.connect(self.indexChanged)
49        self.myQtab.tabCloseRequested.connect(self.closeTab)
50         
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()
59
60    def handleOpen(self,fichier=None,patron=0,units=None):
61        result = None
62        if fichier is None:
63             if self.appliEficas.demande==True : 
64                self.appliEficas.definitCode(None,None)
65                if self.appliEficas.code == None:return
66             
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 (*)')
71             else :
72                extensions=tr('Fichiers JDC (*.comm);;''Tous les Fichiers (*)')
73
74             fichier = QFileDialog.getOpenFileName(self.appliEficas,
75                         tr('Ouvrir Fichier'),
76                         self.appliEficas.maConfiguration.savedir,
77                          extensions)
78             fichier=fichier[0]
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))
87        return result
88
89    def closeTab(self,indexAFermer):
90        self.handleClose(indexAFermer = indexAFermer)
91
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
96        if index < 0 : return
97        res=self.checkDirty(self.dict_editors[index],texte)
98        if res == 2 : return 2             # l utilisateur a annule
99        idx=index
100        while idx < len(self.dict_editors) -1 :
101              self.dict_editors[idx]=self.dict_editors[idx+1]
102              idx = idx + 1
103        del self.dict_editors[len (self.dict_editors) -1]
104        try :
105            del self.doubles[self.dict_editors[index]]
106        except :
107            pass
108        self.myQtab.removeTab(index)
109        return res
110        
111
112    def run(self):
113        index=self.myQtab.currentIndex()
114        if index < 0 : return
115        editor=self.dict_editors[index]
116        editor.run()
117
118    def saveRun(self):
119        index=self.myQtab.currentIndex()
120        if index < 0 : return
121        editor=self.dict_editors[index]
122        editor.saveRun()
123
124    def handleCloseAll(self,texte=tr('Quitter')):
125        res=0
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
131        return res
132         
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()
139
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()
146
147    def handleDeplier(self):
148        index=self.myQtab.currentIndex()
149        if index < 0 : return
150        editor=self.dict_editors[index]
151        editor.handleDeplier()
152    
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()
159
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()
166
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()
173
174    def handleSupprimer(self):
175        index=self.myQtab.currentIndex()
176        if index < 0 : return
177        editor=self.dict_editors[index]
178        editor.handleSupprimer()
179
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)
185
186
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)
192
193    def newIncludeEditor(self):
194        self.newEditor(include=1)
195
196    def handleViewJdcFichierSource(self):
197        index=self.myQtab.currentIndex()
198        if index < 0 : return
199        self.dict_editors[index].viewJdcSource()
200
201    def ouvreArbre(self):
202        index=self.myQtab.currentIndex()
203        if index < 0 : return
204        self.dict_editors[index].ouvreArbre()
205
206    def fermeArbre(self):
207        index=self.myQtab.currentIndex()
208        if index < 0 : return
209        self.dict_editors[index].fermeArbre()
210
211    def ajoutCommentaire(self):
212        index=self.myQtab.currentIndex()
213        if index < 0 : return
214        editor=self.dict_editors[index]
215        editor.ajoutCommentaire()
216
217    def handleViewJdcRegles(self):
218        index=self.myQtab.currentIndex()
219        if index < 0 : return
220        self.dict_editors[index].viewJdcRegles()
221
222    def handleGestionParam(self):
223        index=self.myQtab.currentIndex()
224        if index < 0 : 
225           QMessageBox.warning( self.appliEficas,tr(u"Creation Parametre indisponible"),tr(u"les parametres sont lies a un jeu de donnees"))
226           return
227        self.dict_editors[index].gestionParam()
228
229    def handleViewJdcRapport(self):
230        index=self.myQtab.currentIndex()
231        if index < 0 : return
232        self.dict_editors[index].viewJdcRapport()
233
234    def handleViewJdcPy(self):
235        index=self.myQtab.currentIndex()
236        if index < 0 : return
237        self.dict_editors[index].viewJdcPy()
238
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 :
244            QMessageBox.warning(
245                      None,
246                      tr("Fichier Duplique"),
247                      tr("Le fichier ne sera pas sauvegarde."),)
248            return
249        ok, newName = editor.saveFile()
250        if ok :
251            fileName=os.path.basename(six.text_type(newName))
252            self.myQtab.setTabText(index,fileName)
253        return ok
254
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()
260        return ok
261
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 :
267            QMessageBox.warning(
268                      None,
269                      tr("Fichier Duplique"),
270                      tr("Le fichier ne sera pas sauvegarde."),)
271            return
272        ok, newName = editor.sauveLigneFile()
273        if ok :
274            fileName=os.path.basename(six.text_type(newName))
275            self.myQtab.setTabText(index,fileName)
276        return ok
277
278
279    def saveAsCurrentEditor(self):
280        index=self.myQtab.currentIndex()
281        editor=self.dict_editors[index]
282        oldName=editor.fichier
283        ok,newName = editor.saveFileAs()
284        if ok :
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]
290        return ok
291
292    def displayJDC(self,jdc,fn=None):
293         """
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
297         """
298         titre=None
299         if fn != None : titre=fn.split("/")[-1]
300         editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
301         self.appliEficas.addToRecentList(editor.getFileName())
302
303    def getEditor(self,fichier = None,jdc = None, units = None,include=0):
304        newWin = 0
305        double = None
306        indexEditor=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)
315               abort=msgBox.exec_()
316               if abort: break
317               double=editor
318        else :
319             from .editor import JDCEditor
320             editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,include=include)
321             if double != None : 
322                self.doubles[editor]=double
323             if editor.jdc: # le fichier est bien un jdc
324                 self.editors.append(editor)
325                 newWin = 1
326             else:
327                 editor.closeIt()
328
329        if newWin:
330             self.addView(editor, fichier)
331        elif editor.jdc:
332             self.myQtab.setCurrentIndex(indexEditor)
333
334        index=self.myQtab.currentIndex()
335        if index != -1 :
336           self.dict_editors[index]=editor
337        return editor
338
339    def addView(self, win, fichier=None):
340 #PNPNPNPN --> a affiner
341         if fichier is None:
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))
345         else:
346             liste=fichier.split('/')
347             txt =  liste[-1]
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
353         win.setFocus()
354
355    def getOpenStartDir(self) :
356        #PN --> Les Preferences
357         try :
358             userDir=os.path.expanduser("~/Eficas_install/")
359             return userDir
360         except :
361             return ""
362
363    def samePath(self,f1, f2):
364     """
365     compare two paths.
366     """
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
369     return 0
370
371
372    def checkDirty(self, editor,texte):
373         """
374         Private method to check dirty status and open a message window.
375         
376         @param editor editor window to check
377         @return flag indicating successful reset of the dirty flag (boolean)
378         """        
379         res=1 
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)
386             res=msgBox.exec_()
387             if res == 0 : return 1
388             return 2
389         if editor.modified:
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)
398             res=msgBox.exec_()
399             if res == 2 : return res
400             if res == 0:
401                 (ok, newName) = editor.saveFile()
402                 if ok:
403                     fileName=os.path.basename(six.text_type(newName))
404                     index=self.myQtab.currentIndex()
405                     self.myQtab.setTabText(index,fileName)
406                 return ok
407         return res
408
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)
414   
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"))
424           return
425
426        listeParam = [] 
427        for p in lesArguments:
428           print (p)
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)
433