]> SALOME platform Git repositories - tools/eficas.git/blob - InterfaceQT4/viewManager.py
Salome HOME
b939951a6fd4f5f3f9fd16ac3d877924555c6670
[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.demande==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 handleRechercherDsCatalogue(self):
143        #print "passage dans handleRechercher"
144        index=self.myQtab.currentIndex()
145        if index < 0 : return
146        editor=self.dict_editors[index]
147        editor.handleRechercherDsCatalogue()
148
149    def handleDeplier(self):
150        index=self.myQtab.currentIndex()
151        if index < 0 : return
152        editor=self.dict_editors[index]
153        editor.handleDeplier()
154    
155    def handleEditCopy(self):
156        #print "passage dans handleEditCopy"
157        index=self.myQtab.currentIndex()
158        if index < 0 : return
159        editor=self.dict_editors[index]
160        editor.handleEditCopy()
161
162    def handleEditCut(self):
163        #print "passage dans handleEditCut"
164        index=self.myQtab.currentIndex()
165        if index < 0 : return
166        editor=self.dict_editors[index]
167        editor.handleEditCut()
168
169    def handleEditPaste(self):
170        #print "passage dans handleEditPaste"
171        index=self.myQtab.currentIndex()
172        if index < 0 : return
173        editor=self.dict_editors[index]
174        editor.handleEditPaste()
175
176    def handleSupprimer(self):
177        index=self.myQtab.currentIndex()
178        if index < 0 : return
179        editor=self.dict_editors[index]
180        editor.handleSupprimer()
181
182    def newEditor(self,include=0):
183        if self.appliEficas.demande==True : 
184            self.appliEficas.definitCode(None,None)
185            if self.appliEficas.code == None:return
186        maPage=self.getEditor(include=include)
187
188    def newIncludeEditor(self):
189        self.newEditor(include=1)
190
191    def handleViewJdcFichierSource(self):
192        index=self.myQtab.currentIndex()
193        if index < 0 : return
194        self.dict_editors[index].viewJdcSource()
195
196    def handleViewJdcRegles(self):
197        index=self.myQtab.currentIndex()
198        if index < 0 : return
199        self.dict_editors[index].viewJdcRegles()
200
201    def handlegestionParam(self):
202        index=self.myQtab.currentIndex()
203        if index < 0 : 
204           QMessageBox.warning( self.appliEficas,tr(u"Creation Parametre indisponible"),tr(u"les parametres sont lies a un jeu de donnees"))
205           return
206        self.dict_editors[index].gestionParam()
207
208    def handleViewJdcRapport(self):
209        index=self.myQtab.currentIndex()
210        if index < 0 : return
211        self.dict_editors[index].viewJdcRapport()
212
213    def handleViewJdcPy(self):
214        index=self.myQtab.currentIndex()
215        if index < 0 : return
216        self.dict_editors[index].viewJdcPy()
217
218    def saveCurrentEditor(self):
219        index=self.myQtab.currentIndex()
220        if index < 0 : return
221        editor=self.dict_editors[index]
222        if editor in self.doubles.keys() :
223            QMessageBox.warning(
224                      None,
225                      tr("Fichier Duplique"),
226                      tr("Le fichier ne sera pas sauvegarde."),)
227            return
228        ok, newName = editor.saveFile()
229        if ok :
230            fileName=os.path.basename(unicode(newName))
231            self.myQtab.setTabText(index,fileName)
232        return ok
233
234    def saveLegerCurrentEditor(self):
235        index=self.myQtab.currentIndex()
236        if index < 0 : return
237        editor=self.dict_editors[index]
238        ok, newName = editor.saveFileLeger()
239        return ok
240
241    def sauveLigneCurrentEditor(self):
242        index=self.myQtab.currentIndex()
243        if index < 0 : return
244        editor=self.dict_editors[index]
245        if editor in self.doubles.keys() :
246            QMessageBox.warning(
247                      None,
248                      tr("Fichier Duplique"),
249                      tr("Le fichier ne sera pas sauvegarde."),)
250            return
251        ok, newName = editor.sauveLigneFile()
252        if ok :
253            fileName=os.path.basename(unicode(newName))
254            self.myQtab.setTabText(index,fileName)
255        return ok
256
257
258    def saveAsCurrentEditor(self):
259        index=self.myQtab.currentIndex()
260        editor=self.dict_editors[index]
261        oldName=editor.fichier
262        ok,newName = editor.saveFileAs()
263        if ok :
264            fileName=os.path.basename(unicode(newName))
265            self.myQtab.setTabText(index,fileName)
266        if editor in self.doubles.keys():
267           if oldName != newName :
268              del self.doubles[editor]
269        return ok
270
271    def displayJDC(self,jdc,fn=None):
272         """
273         Public slot to display a file in an editor.
274         @param fn name of file to be opened
275         # insert filename into list of recently opened files
276         """
277         titre=None
278         if fn != None : titre=fn.split("/")[-1]
279         editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
280         self.appliEficas.addToRecentList(editor.getFileName())
281
282    def getEditor(self,fichier = None,jdc = None, units = None,include=0):
283        newWin = 0
284        double = None
285        indexEditor=0
286        for indexEditor in self.dict_editors.keys():
287            editor=self.dict_editors[indexEditor]
288            if self.samepath(fichier, editor.getFileName()):
289               if monEnvQT5 :
290                 msgBox = QMessageBox()
291                 msgBox.setWindowTitle(tr("Fichier"))
292                 msgBox.setText(tr("Le fichier <b>%s</b> est deja ouvert", str(fichier)))
293                 msgBox.addButton(tr("&Duplication"),0)
294                 msgBox.addButton(tr("&Abandonner"),1)
295                 abort=msgBox.exec_()
296               else :
297                 abort = QMessageBox.warning(self.appliEficas,
298                       tr("Fichier"),
299                       tr("Le fichier <b>%s</b> est deja ouvert.",str(fichier)),
300                       tr("&Duplication"),
301                       tr("&Abort"))
302               if abort: break
303               double=editor
304        else :
305             from editor import JDCEditor
306             editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,vm = self,include=include)
307
308             if double != None : 
309                self.doubles[editor]=double
310             if editor.jdc: # le fichier est bien un jdc
311                 self.editors.append(editor)
312                 newWin = 1
313             else:
314                 editor.closeIt()
315
316        if newWin:
317             self.addView(editor, fichier)
318        elif editor.jdc:
319             self.myQtab.setCurrentIndex(indexEditor)
320
321        index=self.myQtab.currentIndex()
322        if index != -1 :
323           self.dict_editors[index]=editor
324        return editor
325
326    def addView(self, win, fichier=None):
327 #PNPNPNPN --> a affiner
328         if fichier is None:
329             self.untitledCount += 1
330             self.myQtab.addTab(win, tr("Fichier non encore nomme ", self.untitledCount))
331             #self.myQtab.addTab(win, str(self.appliEficas.code))
332         else:
333             liste=fichier.split('/')
334             txt =  liste[-1]
335             if not QFileInfo(fichier).isWritable():
336                 txt = '%s (ro)' % txt
337             self.myQtab.addTab(win,txt )
338         self.myQtab.setCurrentWidget(win)
339         self.currentEditor=win
340         win.setFocus()
341
342    def getOpenStartDir(self) :
343        #PN --> Les Preferences
344         try :
345             userDir=os.path.expanduser("~/Eficas_install/")
346             return userDir
347         except :
348             return ""
349
350    def samepath(self,f1, f2):
351     """
352     compare two paths.
353     """
354     if f1 is None or f2 is None: return 0
355     if os.path.normcase(os.path.normpath(f1)) == os.path.normcase(os.path.normpath(f2)) : return 1
356     return 0
357
358
359    def checkDirty(self, editor,texte):
360         """
361         Private method to check dirty status and open a message window.
362         
363         @param editor editor window to check
364         @return flag indicating successful reset of the dirty flag (boolean)
365         """        
366         res=1 
367         if (editor.modified) and (editor in self.doubles.keys()) :
368             if monEnvQT5 :
369               msgBox = QMessageBox(None)
370               msgBox.setWindowTitle(tr("Fichier Duplique"))
371               msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
372               msgBox.addButton(texte,0)
373               msgBox.addButton(tr("&Annuler"),1)
374               res=msgBox.exec_()
375             else:
376               res = QMessageBox.warning(
377                      None,
378                      tr("Fichier Duplique"),
379                      tr("Le fichier ne sera pas sauvegarde."),
380                      tr(texte), 
381                      tr("&Annuler"))
382             if res == 0 : return 1
383             return 2
384         if editor.modified:
385             fn = editor.getFileName()
386             if fn is None: fn = tr('Noname')
387             if monEnvQT5 :
388               msgBox = QMessageBox(None)
389               msgBox.setWindowTitle(tr("Fichier Modifie"))
390               msgBox.setText(tr("Le fichier ne sera pas sauvegarde."))
391               msgBox.addButton(tr("&Sauvegarder"),1)
392               msgBox.addButton(tr("&Quitter sans sauvegarder"),0)
393               res=msgBox.exec_()
394             else :
395               res = QMessageBox.warning(self.appliEficas, 
396                 tr("Fichier Modifie"),
397                 tr("Le fichier %s n a pas ete sauvegarde.",str(fn)),
398                 tr("&Sauvegarder"),
399                 tr(texte),
400                 tr("&Quitter sans sauvegarder") )
401               if res == 2 : res = 1
402             if res == 0:
403                 (ok, newName) = editor.saveFile()
404                 if ok:
405                     fileName=os.path.basename(unicode(newName))
406                     index=self.myQtab.currentIndex()
407                     self.myQtab.setTabText(index,fileName)
408                 return ok
409         return res
410
411    def handleAjoutGroup(self,listeGroup):
412        index=self.myQtab.currentIndex()
413        if index < 0 : return
414        editor=self.dict_editors[index]
415        editor.handleAjoutGroup(listeGroup)