]> SALOME platform Git repositories - tools/eficas.git/blob - InterfaceQT4/viewManager.py
Salome HOME
gitignore V1
[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 PyQt4.QtGui  import *
23 from PyQt4.QtCore import *
24 from Extensions.i18n import tr
25
26 DictExtensions= {"MAP" : ".map"}
27 class MyTabview:
28
29    def __init__(self,appliEficas):
30        self.appliEficas=appliEficas
31        self.tabWidgets = []
32        self.mesIndexes = {}
33        self.appliEficas=appliEficas
34        self.editors = []
35        self.dict_editors={}
36        self.untitledCount = 0
37        self.doubles = {}
38
39        self.myQtab = self.appliEficas.myQtab
40        if self.appliEficas.multi== True:
41           self.myQtab.connect(self.myQtab,SIGNAL("currentChanged(int)"),self.indexChanged)
42         
43    def indexChanged(self):
44        index=self.myQtab.currentIndex()
45        if  self.dict_editors.has_key(index):
46            editor=self.dict_editors[index]
47            self.appliEficas.CONFIGURATION=editor.CONFIGURATION
48            self.appliEficas.code=editor.CONFIGURATION.code
49            self.appliEficas.setWindowTitle(editor.titre)
50            self.appliEficas.construitMenu()
51
52    def handleOpen(self,fichier=None,patron=0,units=None):
53        result = None
54        if fichier is None:
55             if self.appliEficas.multi==True : 
56                self.appliEficas.definitCode(None,None)
57                if self.appliEficas.code == None:return
58             
59             if DictExtensions.has_key(self.appliEficas.code) :
60                chaine="JDC (*"+DictExtensions[self.appliEficas.code]+");;"
61                extensions=tr(chaine+ "All Files (*)")
62             else :
63                extensions=tr('Fichiers JDC (*.comm);;''Tous les Fichiers (*)')
64
65             fichier = QFileDialog.getOpenFileName(self.appliEficas,
66                         tr('Ouvrir Fichier'),
67                         self.appliEficas.CONFIGURATION.savedir,
68                          extensions)
69             if fichier.isNull(): 
70               return result
71        fichier = os.path.abspath(unicode(fichier))
72        ulfile = os.path.abspath(unicode(fichier))
73        self.appliEficas.CONFIGURATION.savedir=os.path.split(ulfile)[0]
74        self.appliEficas.addToRecentList(fichier)
75        maPage=self.getEditor( fichier,units=units)
76        if maPage: result = maPage
77        if maPage : self.myQtab.setTabText(self.myQtab.indexOf(maPage),os.path.basename(fichier))
78        return result
79
80    def handleClose(self,doitSauverRecent = 1,texte=tr('&Quitter')):
81        if doitSauverRecent : self.appliEficas.sauveRecents()
82        index=self.myQtab.currentIndex()
83        if index < 0 : return
84        res=self.checkDirty(self.dict_editors[index],texte)
85        if res == 2 : return 2             # l utilisateur a annule
86        index=self.myQtab.currentIndex()
87        idx=index
88        while idx < len(self.dict_editors) -1 :
89              self.dict_editors[idx]=self.dict_editors[idx+1]
90              idx = idx + 1
91        del self.dict_editors[len (self.dict_editors) -1]
92        try :
93            del self.doubles[self.dict_editors[index]]
94        except :
95            pass
96        self.myQtab.removeTab(index)
97        return res
98        
99
100    def run(self):
101        index=self.myQtab.currentIndex()
102        if index < 0 : return
103        editor=self.dict_editors[index]
104        editor.run()
105
106    def saveRun(self):
107        index=self.myQtab.currentIndex()
108        if index < 0 : return
109        editor=self.dict_editors[index]
110        editor.saveRun()
111
112    def handleCloseAll(self,texte=tr('Quitter')):
113        res=0
114        self.appliEficas.sauveRecents()
115        while len(self.dict_editors) > 0 :
116              self.myQtab.setCurrentIndex(0)
117              res=self.handleClose(0,texte)
118              if res==2 : return res   # l utilsateur a annule
119        return res
120         
121    def handleRechercher(self):
122        #print "passage dans handleRechercher"
123        index=self.myQtab.currentIndex()
124        if index < 0 : return
125        editor=self.dict_editors[index]
126        editor.handleRechercher()
127
128    def handleDeplier(self):
129        index=self.myQtab.currentIndex()
130        if index < 0 : return
131        editor=self.dict_editors[index]
132        editor.handleDeplier()
133    
134    def handleEditCopy(self):
135        #print "passage dans handleEditCopy"
136        index=self.myQtab.currentIndex()
137        if index < 0 : return
138        editor=self.dict_editors[index]
139        editor.handleEditCopy()
140
141    def handleEditCut(self):
142        #print "passage dans handleEditCut"
143        index=self.myQtab.currentIndex()
144        if index < 0 : return
145        editor=self.dict_editors[index]
146        editor.handleEditCut()
147
148    def handleEditPaste(self):
149        #print "passage dans handleEditPaste"
150        index=self.myQtab.currentIndex()
151        if index < 0 : return
152        editor=self.dict_editors[index]
153        editor.handleEditPaste()
154
155    def handleSupprimer(self):
156        index=self.myQtab.currentIndex()
157        if index < 0 : return
158        editor=self.dict_editors[index]
159        editor.handleSupprimer()
160
161    def newEditor(self,include=0):
162        if self.appliEficas.multi==True : 
163            self.appliEficas.definitCode(None,None)
164            if self.appliEficas.code == None:return
165        maPage=self.getEditor(include=include)
166
167    def newIncludeEditor(self):
168        self.newEditor(include=1)
169
170    def handleViewJdcFichierSource(self):
171        index=self.myQtab.currentIndex()
172        if index < 0 : return
173        self.dict_editors[index].viewJdcSource()
174
175    def handleViewJdcRapport(self):
176        index=self.myQtab.currentIndex()
177        if index < 0 : return
178        self.dict_editors[index].viewJdcRapport()
179
180    def handleViewJdcPy(self):
181        index=self.myQtab.currentIndex()
182        if index < 0 : return
183        self.dict_editors[index].viewJdcPy()
184
185    def saveCurrentEditor(self):
186        index=self.myQtab.currentIndex()
187        if index < 0 : return
188        editor=self.dict_editors[index]
189        if editor in self.doubles.keys() :
190            QMessageBox.warning(
191                      None,
192                      tr("Fichier Duplique"),
193                      tr("Le fichier ne sera pas sauvegarde."),
194                      tr("&Annuler"))
195            return
196        ok, newName = editor.saveFile()
197        if ok :
198            fileName=os.path.basename(unicode(newName))
199            self.myQtab.setTabText(index,fileName)
200        return ok
201
202    def saveAsCurrentEditor(self):
203        index=self.myQtab.currentIndex()
204        editor=self.dict_editors[index]
205        oldName=editor.fichier
206        ok,newName = editor.saveFileAs()
207        if ok :
208            fileName=os.path.basename(unicode(newName))
209            self.myQtab.setTabText(index,fileName)
210        if editor in self.doubles.keys():
211           if oldName != newName :
212              del self.doubles[editor]
213        return ok
214
215    def displayJDC(self,jdc,fn=None):
216         """
217         Public slot to display a file in an editor.
218         @param fn name of file to be opened
219         # insert filename into list of recently opened files
220         """
221         titre=None
222         if fn != None : titre=fn.split("/")[-1]
223         editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
224         self.appliEficas.addToRecentList(editor.getFileName())
225
226    def getEditor(self,fichier = None,jdc = None, units = None,include=0):
227        newWin = 0
228        double = None
229        indexEditor=0
230        for indexEditor in self.dict_editors.keys():
231            editor=self.dict_editors[indexEditor]
232            if self.samepath(fichier, editor.getFileName()):
233               abort = QMessageBox.warning(self.appliEficas,
234                         tr("Fichier"),
235                         tr("Le fichier <b>%s</b> est deja ouvert.",str(fichier)),
236                         tr("&Duplication"),
237                         tr("&Abort"))
238               if abort: break
239               double=editor
240        else :
241             from editor import JDCEditor
242             editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,vm = self,include=include)
243             if double != None : 
244                self.doubles[editor]=double
245             if editor.jdc: # le fichier est bien un jdc
246                 self.editors.append(editor)
247                 newWin = 1
248             else:
249                 editor.closeIt()
250
251        if newWin:
252             self.addView(editor, fichier)
253        elif editor.jdc:
254             self.myQtab.setCurrentIndex(indexEditor)
255
256        index=self.myQtab.currentIndex()
257        if index != -1 :
258           self.dict_editors[index]=editor
259        return editor
260
261    def addView(self, win, fichier=None):
262 #PNPNPNPN --> a affiner
263         if fichier is None:
264             self.untitledCount += 1
265             self.myQtab.addTab(win, tr("Fichier non encore nommé ", self.untitledCount))
266             #self.myQtab.addTab(win, str(self.appliEficas.code))
267         else:
268             liste=fichier.split('/')
269             txt =  liste[-1]
270             if not QFileInfo(fichier).isWritable():
271                 txt = '%s (ro)' % txt
272             self.myQtab.addTab(win,txt )
273         self.myQtab.setCurrentWidget(win)
274         self.currentEditor=win
275         win.setFocus()
276
277    def getOpenStartDir(self) :
278        #PN --> Les Preferences
279         try :
280             userDir=os.path.expanduser("~/Eficas_install/")
281             return userDir
282         except :
283             return ""
284
285    def samepath(self,f1, f2):
286     """
287     compare two paths.
288     """
289     if f1 is None or f2 is None: return 0
290     if os.path.normcase(os.path.normpath(f1)) == os.path.normcase(os.path.normpath(f2)) : return 1
291     return 0
292
293
294    def checkDirty(self, editor,texte):
295         """
296         Private method to check dirty status and open a message window.
297         
298         @param editor editor window to check
299         @return flag indicating successful reset of the dirty flag (boolean)
300         """        
301         res=1 
302         if (editor.modified) and (editor in self.doubles.keys()) :
303             res = QMessageBox.warning(
304                      None,
305                      tr("Fichier Duplique"),
306                      tr("Le fichier ne sera pas sauvegarde."),
307                      tr(texte), 
308                      tr("&Annuler"))
309             if res == 0 : return 1
310             return 2
311         if editor.modified:
312             fn = editor.getFileName()
313             if fn is None:
314                 fn = self.appliEficas.trUtf8('Noname')
315             res = QMessageBox.warning(self.appliEficas, 
316                 tr("Fichier Modifie"),
317                 tr("Le fichier %s n a pas ete sauvegarde.",str(fn)),
318                 tr("&Sauvegarder"),
319                 tr(texte),
320                 tr("&Annuler") )
321             if res == 0:
322                 (ok, newName) = editor.saveFile()
323                 if ok:
324                     fileName=os.path.basename(unicode(newName))
325                     index=self.myQtab.currentIndex()
326                     self.myQtab.setTabText(index,fileName)
327                 return ok
328         return res
329
330    def handleAjoutGroup(self,listeGroup):
331        index=self.myQtab.currentIndex()
332        if index < 0 : return
333        editor=self.dict_editors[index]
334        editor.handleAjoutGroup(listeGroup)