Salome HOME
Modif V6_4_°
[tools/eficas.git] / InterfaceQT4 / viewManager.py
1 # -*- coding: utf-8 -*-
2 #            CONFIGURATION MANAGEMENT OF EDF VERSION
3 # ======================================================================
4 # COPYRIGHT (C) 1991 - 2002  EDF R&D                  WWW.CODE-ASTER.ORG
5 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
6 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
7 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
8 # (AT YOUR OPTION) ANY LATER VERSION.
9 #
10 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
11 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
12 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
13 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
14 #
15 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
16 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
17 #    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
18 #
19 #
20 # ======================================================================
21
22 import os, string
23 from PyQt4.QtGui  import *
24 from PyQt4.QtCore import *
25
26 class MyTabview:
27
28    def __init__(self,appliEficas):
29        self.appliEficas=appliEficas
30        self.tabWidgets = []
31        self.mesIndexes = {}
32        self.appliEficas=appliEficas
33        self.editors = []
34        self.dict_editors={}
35        self.untitledCount = 0
36        self.doubles = {}
37
38        self.gridLayout = QGridLayout(self.appliEficas.centralWidget())
39        self.myQtab = QTabWidget(self.appliEficas.centralWidget())
40        self.gridLayout.addWidget(self.myQtab)
41        if self.appliEficas.multi== True:
42           self.myQtab.connect(self.myQtab,SIGNAL("currentChanged(int)"),self.indexChanged)
43         
44    def indexChanged(self):
45        index=self.myQtab.currentIndex()
46        if  self.dict_editors.has_key(index):
47            editor=self.dict_editors[index]
48            self.appliEficas.CONFIGURATION=editor.CONFIGURATION
49            self.appliEficas.setWindowTitle(editor.titre)
50
51    def handleOpen(self,fichier=None,patron=0,units=None):
52        result = None
53        if fichier is None:
54             if self.appliEficas.multi==True : 
55                self.appliEficas.definitCode(None,None)
56             fichier = QFileDialog.getOpenFileName(self.appliEficas,
57                         self.appliEficas.trUtf8('Ouvrir Fichier'),
58                         self.appliEficas.CONFIGURATION.savedir,
59                         self.appliEficas.trUtf8('JDC Files (*.comm);;''All Files (*)'))
60             if fichier.isNull(): 
61               return result
62        fichier = os.path.abspath(unicode(fichier))
63        ulfile = os.path.abspath(unicode(fichier))
64        self.appliEficas.CONFIGURATION.savedir=os.path.split(ulfile)[0]
65        self.appliEficas.addToRecentList(fichier)
66        maPage=self.getEditor( fichier,units=units)
67        if maPage:
68          result = maPage
69        return result
70
71    def handleClose(self,doitSauverRecent = 1,texte='&Quitter'):
72        if doitSauverRecent : self.appliEficas.sauveRecents()
73        index=self.myQtab.currentIndex()
74        if index < 0 : return
75        res=self.checkDirty(self.dict_editors[index],texte)
76        if res == 2 : return 2             # l utilisateur a annule
77        index=self.myQtab.currentIndex()
78        idx=index
79        while idx < len(self.dict_editors) -1 :
80              self.dict_editors[idx]=self.dict_editors[idx+1]
81              idx = idx + 1
82        del self.dict_editors[len (self.dict_editors) -1]
83        try :
84            del self.doubles[self.dict_editors[index]]
85        except :
86            pass
87        self.myQtab.removeTab(index)
88        return res
89        
90
91    def run(self):
92        index=self.myQtab.currentIndex()
93        if index < 0 : return
94        editor=self.dict_editors[index]
95        editor.run()
96
97    def saveRun(self):
98        index=self.myQtab.currentIndex()
99        if index < 0 : return
100        editor=self.dict_editors[index]
101        editor.saveRun()
102
103    def runYACS(self):
104        index=self.myQtab.currentIndex()
105        if index < 0 : return
106        editor=self.dict_editors[index]
107        editor.runYACS()
108
109    def saveYACS(self):
110        index=self.myQtab.currentIndex()
111        if index < 0 : return
112        editor=self.dict_editors[index]
113        editor.saveYACS()
114
115    def handleCloseAll(self,texte='Quitter'):
116        res=0
117        self.appliEficas.sauveRecents()
118        while len(self.dict_editors) > 0 :
119              self.myQtab.setCurrentIndex(0)
120              res=self.handleClose(0,texte)
121              if res==2 : return res   # l utilsateur a annule
122        return res
123         
124    def handleEditCopy(self):
125        #print "passage dans handleEditCopy"
126        index=self.myQtab.currentIndex()
127        editor=self.dict_editors[index]
128        editor.handleEditCopy()
129
130    def handleEditCut(self):
131        #print "passage dans handleEditCut"
132        index=self.myQtab.currentIndex()
133        editor=self.dict_editors[index]
134        editor.handleEditCut()
135
136    def handleEditPaste(self):
137        #print "passage dans handleEditPaste"
138        index=self.myQtab.currentIndex()
139        editor=self.dict_editors[index]
140        editor.handleEditPaste()
141
142    def handleSupprimer(self):
143        index=self.myQtab.currentIndex()
144        editor=self.dict_editors[index]
145        editor.handleSupprimer()
146
147    def newEditor(self,include=0):
148        if self.appliEficas.multi==True : 
149            self.appliEficas.definitCode(None,None)
150        maPage=self.getEditor(include=include)
151
152    def newIncludeEditor(self):
153        self.newEditor(include=1)
154
155    def handleViewJdcFichierSource(self):
156        index=self.myQtab.currentIndex()
157        if index < 0 : return
158        self.dict_editors[index].viewJdcSource()
159
160    def handleViewJdcRapport(self):
161        index=self.myQtab.currentIndex()
162        if index < 0 : return
163        self.dict_editors[index].viewJdcRapport()
164
165    def handleViewJdcPy(self):
166        index=self.myQtab.currentIndex()
167        if index < 0 : return
168        self.dict_editors[index].viewJdcPy()
169
170    def saveCurrentEditor(self):
171        index=self.myQtab.currentIndex()
172        if index < 0 : return
173        editor=self.dict_editors[index]
174        if editor in self.doubles.keys() :
175            QMessageBox.warning(
176                      None,
177                      self.appliEficas.trUtf8("Fichier Duplique"),
178                      self.appliEficas.trUtf8("Le fichier ne sera pas sauvegarde."),
179                      self.appliEficas.trUtf8("&Annuler"))
180            return
181        ok, newName = editor.saveFile()
182        if ok :
183            fileName=os.path.basename(unicode(newName))
184            self.myQtab.setTabText(index,fileName)
185        return ok
186
187    def saveAsCurrentEditor(self):
188        index=self.myQtab.currentIndex()
189        editor=self.dict_editors[index]
190        oldName=editor.fichier
191        ok,newName = editor.saveFileAs()
192        if ok :
193            fileName=os.path.basename(unicode(newName))
194            self.myQtab.setTabText(index,fileName)
195        if editor in self.doubles.keys():
196           if oldName != newName :
197              del self.doubles[editor]
198        return ok
199
200    def displayJDC(self,jdc,fn=None):
201         """
202         Public slot to display a file in an editor.
203         @param fn name of file to be opened
204         # insert filename into list of recently opened files
205         """
206         titre=None
207         if fn != None : titre=fn.split("/")[-1]
208         editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
209         self.appliEficas.addToRecentList(editor.getFileName())
210
211    def getEditor(self,fichier = None,jdc = None, units = None,include=0):
212        newWin = 0
213        double = None
214        indexEditor=0
215        for indexEditor in self.dict_editors.keys():
216            editor=self.dict_editors[indexEditor]
217            if self.samepath(fichier, editor.getFileName()):
218               abort = QMessageBox.warning(self.appliEficas,
219                         self.appliEficas.trUtf8("Fichier"),
220                         self.appliEficas.trUtf8("Le fichier <b>%1</b> est deja ouvert.").arg(fichier),
221                         self.appliEficas.trUtf8("&Duplication"),
222                         self.appliEficas.trUtf8("&Abort"))
223               if abort: break
224               double=editor
225        else :
226             from editor import JDCEditor
227             editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,vm = self,include=include)
228             if double != None : 
229                self.doubles[editor]=double
230             if editor.jdc: # le fichier est bien un jdc
231                 self.editors.append(editor)
232                 newWin = 1
233             else:
234                 editor.closeIt()
235
236        if newWin:
237             self.addView(editor, fichier)
238        elif editor.jdc:
239             self.myQtab.setCurrentIndex(indexEditor)
240
241        index=self.myQtab.currentIndex()
242        if index != -1 :
243           self.dict_editors[index]=editor
244        return editor
245
246    def addView(self, win, fichier=None):
247         if fichier is None:
248             self.untitledCount += 1
249             self.myQtab.addTab(win, self.appliEficas.trUtf8("Untitled %1").arg(self.untitledCount))
250         else:
251             liste=fichier.split('/')
252             txt =  liste[-1]
253             if not QFileInfo(fichier).isWritable():
254                 txt = '%s (ro)' % txt
255             self.myQtab.addTab(win, txt)
256         self.myQtab.setCurrentWidget(win)
257         self.currentEditor=win
258         win.setFocus()
259
260    def getOpenStartDir(self) :
261        #PN --> Les Preferences
262         try :
263             userDir=os.path.expanduser("~/Eficas_install/")
264             return userDir
265         except :
266             return ""
267
268    def samepath(self,f1, f2):
269     """
270     compare two paths.
271     """
272     if f1 is None or f2 is None: return 0
273     if os.path.normcase(os.path.normpath(f1)) == os.path.normcase(os.path.normpath(f2)) : return 1
274     return 0
275
276
277    def checkDirty(self, editor,texte):
278         """
279         Private method to check dirty status and open a message window.
280         
281         @param editor editor window to check
282         @return flag indicating successful reset of the dirty flag (boolean)
283         """        
284         res=1 
285         if (editor.modified) and (editor in self.doubles.keys()) :
286             res = QMessageBox.warning(
287                      None,
288                      self.appliEficas.trUtf8("Fichier Duplique"),
289                      self.appliEficas.trUtf8("Le fichier ne sera pas sauvegarde."),
290                      self.appliEficas.trUtf8(texte), 
291                      self.appliEficas.trUtf8("&Annuler"))
292             if res == 0 : return 1
293             return 2
294         if editor.modified:
295             fn = editor.getFileName()
296             if fn is None:
297                 fn = self.appliEficas.trUtf8('Noname')
298             res = QMessageBox.warning(self.appliEficas, 
299                 self.appliEficas.trUtf8("Fichier Modifie"),
300                 self.appliEficas.trUtf8("Le fichier <b>%1</b> n a pas ete sauvegarde.") .arg(fn),
301                 self.appliEficas.trUtf8("&Sauvegarder"),
302                 self.appliEficas.trUtf8(texte),
303                 self.appliEficas.trUtf8("&Annuler") )
304             if res == 0:
305                 (ok, newName) = editor.saveFile()
306                 if ok:
307                     fileName=os.path.basename(unicode(newName))
308                     index=self.myQtab.currentIndex()
309                     self.myQtab.setTabText(index,fileName)
310                 return ok
311         return res
312
313    def handleAjoutGroup(self,listeGroup):
314        index=self.myQtab.currentIndex()
315        editor=self.dict_editors[index]
316        editor.handleAjoutGroup(listeGroup)