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