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