Salome HOME
67e4c75e48e582c905db7e993797bfea4d64a8e2
[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         
42
43    def handleOpen(self,fichier=None,patron=0,units=None):
44        if fichier is None:
45             fichier = QFileDialog.getOpenFileName(self.appliEficas,
46                         self.appliEficas.trUtf8('Ouvrir Fichier'),
47                         self.appliEficas.CONFIGURATION.savedir,
48                         self.appliEficas.trUtf8('JDC Files (*.comm);;''All Files (*)'))
49             if fichier.isNull(): return
50        fichier = os.path.abspath(unicode(fichier))
51        self.appliEficas.addToRecentList(fichier)
52        maPage=self.getEditor( fichier,units=units)
53
54    def handleClose(self,doitSauverRecent = 1):
55        #print "passage dans handleClose"
56        #print self.dict_editors
57        if doitSauverRecent : self.appliEficas.sauveRecents()
58        index=self.myQtab.currentIndex()
59        if index < 0 : return
60        self.checkDirty(self.dict_editors[index])
61        index=self.myQtab.currentIndex()
62        while index < len(self.dict_editors) -1 :
63              self.dict_editors[index]=self.dict_editors[index+1]
64              index = index + 1
65        del self.dict_editors[len (self.dict_editors) -1]
66        try :
67            del self.doubles[self.dict_editors[index]]
68        except :
69            pass
70        print self.dict_editors
71        self.myQtab.removeTab(index)
72        
73
74    def handleCloseAll(self):
75        self.appliEficas.sauveRecents()
76        #print "passage dans CloseAll"
77        print "self.dict_editors", self.dict_editors
78        while len(self.dict_editors) > 0 :
79              self.myQtab.setCurrentIndex(0)
80              self.handleClose(0)
81         
82    def handleEditCopy(self):
83        #print "passage dans handleEditCopy"
84        index=self.myQtab.currentIndex()
85        editor=self.dict_editors[index]
86        editor.handleEditCopy()
87
88    def handleEditCut(self):
89        #print "passage dans handleEditCut"
90        index=self.myQtab.currentIndex()
91        editor=self.dict_editors[index]
92        editor.handleEditCut()
93
94    def handleEditPaste(self):
95        #print "passage dans handleEditPaste"
96        index=self.myQtab.currentIndex()
97        editor=self.dict_editors[index]
98        editor.handleEditPaste()
99
100    def newEditor(self,include=0):
101        #print "passage dans newEditor"
102        maPage=self.getEditor()
103
104    def newIncludeEditor(self):
105        self.newEditor(include=1)
106
107    def handleViewJdcFichierSource(self):
108        index=self.myQtab.currentIndex()
109        if index < 0 : return
110        self.dict_editors[index].viewJdcSource()
111
112    def handleViewJdcRapport(self):
113        index=self.myQtab.currentIndex()
114        if index < 0 : return
115        self.dict_editors[index].viewJdcRapport()
116
117    def handleViewJdcPy(self):
118        index=self.myQtab.currentIndex()
119        if index < 0 : return
120        self.dict_editors[index].viewJdcPy()
121
122    def saveCurrentEditor(self):
123        index=self.myQtab.currentIndex()
124        if index < 0 : return
125        editor=self.dict_editors[index]
126        if editor in self.doubles.keys() :
127            QMessageBox.warning(
128                      None,
129                      self.appliEficas.trUtf8("Fichier Duplique"),
130                      self.appliEficas.trUtf8("Le fichier ne sera pas sauvegarde."),
131                      self.appliEficas.trUtf8("&Annuler"))
132            return
133        ok, newName = editor.saveFile()
134        if ok :
135            fileName=os.path.basename(unicode(newName))
136            self.myQtab.setTabText(index,fileName)
137
138    def saveAsCurrentEditor(self):
139        index=self.myQtab.currentIndex()
140        editor=self.dict_editors[index]
141        oldName=editor.fichier
142        ok,newName = editor.saveFileAs()
143        if ok :
144            fileName=os.path.basename(unicode(newName))
145            self.myQtab.setTabText(index,fileName)
146        if editor in self.doubles.keys():
147           if oldName != newName :
148              del self.doubles[editor]
149
150
151    def displayJDC(self,jdc,fn=None):
152         """
153         Public slot to display a file in an editor.
154         @param fn name of file to be opened
155         # insert filename into list of recently opened files
156         """
157         titre=None
158         if fn != None : titre=fn.split("/")[-1]
159         editor = self.getEditor(fichier= fn, jdc = jdc ,include=1)
160         self.appliEficas.addToRecentList(editor.getFileName())
161
162    def getEditor(self,fichier = None,jdc = None, units = None,include=0):
163        newWin = 0
164        double = None
165        indexEditor=0
166        for indexEditor in self.dict_editors.keys():
167            editor=self.dict_editors[indexEditor]
168            if self.samepath(fichier, editor.getFileName()):
169               abort = QMessageBox.warning(self.appliEficas,
170                         self.appliEficas.trUtf8("Fichier"),
171                         self.appliEficas.trUtf8("Le fichier <b>%1</b> est deja ouvert.").arg(fichier),
172                         self.appliEficas.trUtf8("&Duplication"),
173                         self.appliEficas.trUtf8("&Abort"))
174               if abort: break
175               double=editor
176        else :
177             from editor import JDCEditor
178             editor = JDCEditor(self.appliEficas,fichier, jdc, self.myQtab,units=units,vm = self,include=include)
179             if double != None : 
180                self.doubles[editor]=double
181             if editor.jdc: # le fichier est bien un jdc
182                 self.editors.append(editor)
183                 newWin = 1
184             else:
185                 editor.closeIt()
186
187        if newWin:
188             self.addView(editor, fichier)
189        elif editor.jdc:
190             self.myQtab.setCurrentIndex(indexEditor)
191
192        index=self.myQtab.currentIndex()
193        if index != -1 :
194           self.dict_editors[index]=editor
195        return editor
196
197    def addView(self, win, fichier=None):
198         if fichier is None:
199             self.untitledCount += 1
200             self.myQtab.addTab(win, self.appliEficas.trUtf8("Untitled %1").arg(self.untitledCount))
201         else:
202             liste=fichier.split('/')
203             txt =  liste[-1]
204             if not QFileInfo(fichier).isWritable():
205                 txt = '%s (ro)' % txt
206             self.myQtab.addTab(win, txt)
207         self.myQtab.setCurrentWidget(win)
208         self.currentEditor=win
209         win.setFocus()
210
211    def getOpenStartDir(self) :
212        #PN --> Les Preferences
213         try :
214             userDir=os.path.expanduser("~/Eficas_install/")
215             return userDir
216         except :
217             return ""
218
219    def samepath(self,f1, f2):
220     """
221     compare two paths.
222     """
223     if f1 is None or f2 is None: return 0
224     if os.path.normcase(os.path.normpath(f1)) == os.path.normcase(os.path.normpath(f2)) : return 1
225     return 0
226
227
228    def checkDirty(self, editor):
229         """
230         Private method to check dirty status and open a message window.
231         
232         @param editor editor window to check
233         @return flag indicating successful reset of the dirty flag (boolean)
234         """        
235      
236         print "checkDirty"
237         if (editor.modified) and (editor in self.doubles.keys()) :
238             res = QMessageBox.warning(
239                      None,
240                      self.appliEficas.trUtf8("Fichier Duplique"),
241                      self.appliEficas.trUtf8("Le fichier ne sera pas sauvegarde."),
242                      self.appliEficas.trUtf8("&Quitter"), 
243                      self.appliEficas.trUtf8("&Annuler"))
244             if res == 0 : return 1
245             return 0
246         if editor.modified:
247             fn = editor.getFileName()
248             if fn is None:
249                 fn = self.appliEficas.trUtf8('Noname')
250             res = QMessageBox.warning(self.appliEficas, 
251                 self.appliEficas.trUtf8("Fichier Modifie"),
252                 self.appliEficas.trUtf8("Le fichier <b>%1</b> n a pas ete sauvegarde.")
253                     .arg(fn),
254                 self.appliEficas.trUtf8("&Sauvegarder"),
255                 self.appliEficas.trUtf8("&Quitter "),
256                 self.appliEficas.trUtf8("&Annuler"), 0, 2)
257             if res == 0:
258                 (ok, newName) = editor.saveFile()
259                 if ok:
260                     fileName=os.path.basename(unicode(newName))
261                     index=self.myQtab.currentIndex()
262                     self.myQtab.setTabText(index,fileName)
263                 return ok
264             elif res == 2:
265                 return  0
266         return 1
267