Salome HOME
saauve du 26/05
[tools/eficas.git] / InterfaceQT4 / viewManager.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   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 import os, string
22 from Extensions.i18n import tr
23 from determine import monEnvQT5
24 if monEnvQT5:
25    from  PyQt5.QtWidgets  import QFileDialog, QMessageBox
26    from  PyQt5.QtCore  import QFileInfo
27 else :
28     from PyQt4.QtGui  import *
29     from PyQt4.QtCore import *
30
31 DictExtensions= {"MAP" : ".map"}
32 class MyTabview:
33
34    def __init__(self,appliEficas):
35        self.appliEficas=appliEficas
36        self.tabWidgets = []
37        self.mesIndexes = {}
38        self.appliEficas=appliEficas
39        self.editors = []
40        self.dict_editors={}
41        self.untitledCount = 0
42        self.doubles = {}
43
44        self.myQtab = self.appliEficas.myQtab
45
46        if monEnvQT5:
47           self.myQtab.currentChanged.connect(self.indexChanged)
48           self.myQtab.tabCloseRequested.connect(self.closeTab)
49        else :
50           self.myQtab.connect(self.myQtab, SIGNAL('tabCloseRequested(int)'), self.closeTab)
51           if self.appliEficas.multi== True:
52              self.myQtab.connect(self.myQtab,SIGNAL("currentChanged(int)"),self.indexChanged)
53         
54    def indexChanged(self):
55        index=self.myQtab.currentIndex()
56        if  self.dict_editors.has_key(index):
57            editor=self.dict_editors[index]
58            self.appliEficas.CONFIGURATION=editor.CONFIGURATION
59            self.appliEficas.code=editor.CONFIGURATION.code
60            self.appliEficas.setWindowTitle(editor.titre)
61            self.appliEficas.construitMenu()
62
63    def handleOpen(self,fichier=None,patron=0,units=None):
64        result = None
65        if fichier is None:
66             if self.appliEficas.multi==True : 
67                self.appliEficas.definitCode(None,None)
68                if self.appliEficas.code == None:return
69             
70             if DictExtensions.has_key(self.appliEficas.code) :
71                chaine="JDC (*"+DictExtensions[self.appliEficas.code]+");;"
72                extensions=tr(chaine+ "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.CONFIGURATION.savedir,
79                          extensions)
80             if monEnvQT5 : fichier=fichier[0]
81             if not fichier: return result
82        fichier = os.path.abspath(unicode(fichier))
83        ulfile = os.path.abspath(unicode(fichier))
84        self.appliEficas.CONFIGURATION.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):
92        self.handleClose()
93
94    def handleClose(self,doitSauverRecent = 1,texte=tr('&Quitter')):
95        if doitSauverRecent : self.appliEficas.sauveRecents()
96        index=self.myQtab.currentIndex()
97        if index < 0 : return
98        res=self.checkDirty(self.dict_editors[index],texte)
99        if res == 2 : return 2             # l utilisateur a annule
100        index=self.myQtab.currentIndex()
101        idx=index
102        while idx < len(self.dict_editors) -1 :
103              self.dict_editors[idx]=self.dict_editors[idx+1]
104              idx = idx + 1
105        del self.dict_editors[len (self.dict_editors) -1]
106        try :
107            del self.doubles[self.dict_editors[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.dict_editors[index]
118        editor.run()
119
120    def saveRun(self):
121        index=self.myQtab.currentIndex()
122        if index < 0 : return
123        editor=self.dict_editors[index]
124        editor.saveRun()
125
126    def handleCloseAll(self,texte=tr('Quitter')):
127        res=0
128        self.appliEficas.sauveRecents()
129        while len(self.dict_editors) > 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.dict_editors[index]
140        editor.handleRechercher()
141
142    def handleDeplier(self):
143        index=self.myQtab.currentIndex()
144        if index < 0 : return
145        editor=self.dict_editors[index]
146        editor.handleDeplier()
147    
148    def handleEditCopy(self):
149        #print "passage dans handleEditCopy"
150        index=self.myQtab.currentIndex()
151        if index < 0 : return
152        editor=self.dict_editors[index]
153        editor.handleEditCopy()
154
155    def handleEditCut(self):
156        #print "passage dans handleEditCut"
157        index=self.myQtab.currentIndex()
158        if index < 0 : return
159        editor=self.dict_editors[index]
160        editor.handleEditCut()
161
162    def handleEditPaste(self):
163        #print "passage dans handleEditPaste"
164        index=self.myQtab.currentIndex()
165        if index < 0 : return
166        editor=self.dict_editors[index]
167        editor.handleEditPaste()
168
169    def handleSupprimer(self):
170        index=self.myQtab.currentIndex()
171        if index < 0 : return
172        editor=self.dict_editors[index]
173        editor.handleSupprimer()
174
175    def newEditor(self,include=0):
176        if self.appliEficas.multi==True : 
177            self.appliEficas.definitCode(None,None)
178            if self.appliEficas.code == None:return
179        maPage=self.getEditor(include=include)
180
181    def newIncludeEditor(self):
182        self.newEditor(include=1)
183
184    def handleViewJdcFichierSource(self):
185        index=self.myQtab.currentIndex()
186        if index < 0 : return
187        self.dict_editors[index].viewJdcSource()
188
189    def handleViewJdcRegles(self):
190        index=self.myQtab.currentIndex()
191        if index < 0 : return
192        self.dict_editors[index].viewJdcRegles()
193
194    def handlegestionParam(self):
195        index=self.myQtab.currentIndex()
196        if index < 0 : 
197           QMessageBox.warning( self.appliEficas,tr(u"Creation Parametre indisponible"),tr(u"les parametres sont lies a un jeu de donnees"))
198           return
199        self.dict_editors[index].gestionParam()
200
201    def handleViewJdcRapport(self):
202        index=self.myQtab.currentIndex()
203        if index < 0 : return
204        self.dict_editors[index].viewJdcRapport()
205
206    def handleViewJdcPy(self):
207        index=self.myQtab.currentIndex()
208        if index < 0 : return
209        self.dict_editors[index].viewJdcPy()
210
211    def saveCurrentEditor(self):
212        index=self.myQtab.currentIndex()
213        if index < 0 : return
214        editor=self.dict_editors[index]
215        if editor in self.doubles.keys() :
216            QMessageBox.warning(
217                      None,
218                      tr("Fichier Duplique"),
219                      tr("Le fichier ne sera pas sauvegarde."),)
220            return
221        ok, newName = editor.saveFile()
222        if ok :
223            fileName=os.path.basename(unicode(newName))
224            self.myQtab.setTabText(index,fileName)
225        return ok
226
227    def sauveLigneCurrentEditor(self):
228        index=self.myQtab.currentIndex()
229        if index < 0 : return
230        editor=self.dict_editors[index]
231        if editor in self.doubles.keys() :
232            QMessageBox.warning(
233                      None,
234                      tr("Fichier Duplique"),
235                      tr("Le fichier ne sera pas sauvegarde."),)
236            return
237        ok, newName = editor.sauveLigneFile()
238        if ok :
239            fileName=os.path.basename(unicode(newName))
240            self.myQtab.setTabText(index,fileName)
241        return ok
242
243
244    def saveAsCurrentEditor(self):
245        index=self.myQtab.currentIndex()
246        editor=self.dict_editors[index]
247        oldName=editor.fichier
248        ok,newName = editor.saveFileAs()
249        if ok :
250            fileName=os.path.basename(unicode(newName))
251            self.myQtab.setTabText(index,fileName)
252        if editor in self.doubles.keys():
253           if oldName != newName :
254              del self.doubles[editor]
255        return ok
256
257    def displayJDC(self,jdc,fn=None):
258         """
259         Public slot to display a file in an editor.
260         @param fn name of file to be opened
261         # insert filename into list of recently opened files
262         """
263         titre=None
264         if fn != None : titre=fn.split("/")[-1]
265         editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
266         self.appliEficas.addToRecentList(editor.getFileName())
267
268    def getEditor(self,fichier = None,jdc = None, units = None,include=0):
269        newWin = 0
270        double = None
271        indexEditor=0
272        for indexEditor in self.dict_editors.keys():
273            editor=self.dict_editors[indexEditor]
274            if self.samepath(fichier, editor.getFileName()):
275               if monEnvQT5 :
276                 msgBox = QMessageBox()
277                 msgBox.setWindowTitle(tr("Fichier"))
278                 msgBox.setText(tr("Le fichier <b>%s</b> est deja ouvert", str(fichier)))
279                 msgBox.addButton(tr("&Duplication"),0)
280                 msgBox.addButton(tr("&Abandonner"),1)
281                 abort=msgBox.exec_()
282               else :
283                 abort = QMessageBox.warning(self.appliEficas,
284                       tr("Fichier"),
285                       tr("Le fichier <b>%s</b> est deja ouvert.",str(fichier)),
286                       tr("&Duplication"),
287                       tr("&Abort"))
288               if abort: break
289               double=editor
290        else :
291             from editor import JDCEditor
292             editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,vm = self,include=include)
293             if double != None : 
294                self.doubles[editor]=double
295             if editor.jdc: # le fichier est bien un jdc
296                 self.editors.append(editor)
297                 newWin = 1
298             else:
299                 editor.closeIt()
300
301        if newWin:
302             self.addView(editor, fichier)
303        elif editor.jdc:
304             self.myQtab.setCurrentIndex(indexEditor)
305
306        index=self.myQtab.currentIndex()
307        if index != -1 :
308           self.dict_editors[index]=editor
309        return editor
310
311    def addView(self, win, fichier=None):
312 #PNPNPNPN --> a affiner
313         if fichier is None:
314             self.untitledCount += 1
315             self.myQtab.addTab(win, tr("Fichier non encore nomme ", self.untitledCount))
316             #self.myQtab.addTab(win, str(self.appliEficas.code))
317         else:
318             liste=fichier.split('/')
319             txt =  liste[-1]
320             if not QFileInfo(fichier).isWritable():
321                 txt = '%s (ro)' % txt
322             self.myQtab.addTab(win,txt )
323         self.myQtab.setCurrentWidget(win)
324         self.currentEditor=win
325         win.setFocus()
326
327    def getOpenStartDir(self) :
328        #PN --> Les Preferences
329         try :
330             userDir=os.path.expanduser("~/Eficas_install/")
331             return userDir
332         except :
333             return ""
334
335    def samepath(self,f1, f2):
336     """
337     compare two paths.
338     """
339     if f1 is None or f2 is None: return 0
340     if os.path.normcase(os.path.normpath(f1)) == os.path.normcase(os.path.normpath(f2)) : return 1
341     return 0
342
343
344    def checkDirty(self, editor,texte):
345         """
346         Private method to check dirty status and open a message window.
347         
348         @param editor editor window to check
349         @return flag indicating successful reset of the dirty flag (boolean)
350         """        
351         res=1 
352         if (editor.modified) and (editor in self.doubles.keys()) :
353             if monEnvQT5 :
354               msgBox = QMessageBox(None)
355               msgBox.setWindowTitle(tr("Fichier Duplique"))
356               msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
357               msgBox.addButton(texte,0)
358               msgBox.addButton(tr("&Annuler"),1)
359               res=msgBox.exec_()
360             else:
361               res = QMessageBox.warning(
362                      None,
363                      tr("Fichier Duplique"),
364                      tr("Le fichier ne sera pas sauvegarde."),
365                      tr(texte), 
366                      tr("&Annuler"))
367             if res == 0 : return 1
368             return 2
369         if editor.modified:
370             fn = editor.getFileName()
371             if fn is None: fn = tr('Noname')
372             if monEnvQT5 :
373               msgBox = QMessageBox(None)
374               msgBox.setWindowTitle(tr("Fichier Modifie"))
375               msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
376               msgBox.addButton(tr("&Sauvegarder"),1)
377               msgBox.addButton(tr("&Quitter sans sauvegarder"),0)
378               res=msgBox.exec_()
379             else :
380               res = QMessageBox.warning(self.appliEficas, 
381                 tr("Fichier Modifie"),
382                 tr("Le fichier %s n a pas ete sauvegarde.",str(fn)),
383                 tr("&Sauvegarder"),
384                 tr(texte),
385                 tr("&Quitter sans sauvegarder") )
386               if res == 2 : res = 1
387             if res == 0:
388                 (ok, newName) = editor.saveFile()
389                 if ok:
390                     fileName=os.path.basename(unicode(newName))
391                     index=self.myQtab.currentIndex()
392                     self.myQtab.setTabText(index,fileName)
393                 return ok
394         return res
395
396    def handleAjoutGroup(self,listeGroup):
397        index=self.myQtab.currentIndex()
398        if index < 0 : return
399        editor=self.dict_editors[index]
400        editor.handleAjoutGroup(listeGroup)