Salome HOME
legere difference ds VARIABLES_TO_BE...
[tools/eficas.git] / InterfaceQT4 / typeNode.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 from __future__ import absolute_import
21 try :
22    from builtins import object
23 except : pass
24
25 from PyQt5.QtWidgets import QAction, QMenu, QMessageBox
26
27 from Extensions.i18n import tr
28 import types
29
30
31
32 #---------------------------#
33 class PopUpMenuRacine(object) :
34 #---------------------------#
35
36
37     def createPopUpMenu(self):
38         #print "createPopUpMenu"
39         self.ParamApres = QAction(tr('Parametre'),self.tree)
40         self.ParamApres.triggered.connect(self.addParametersApres)
41         self.ParamApres.setStatusTip(tr("Insere un parametre"))
42         self.menu = QMenu(self.tree)
43         self.menu.addAction(self.ParamApres)
44         self.menu.setStyleSheet("background:rgb(220,220,220); ")
45
46
47     def addParametersApres(self):
48         item= self.tree.currentItem()
49         item.addParameters(True)
50
51 #---------------------------#
52 class PopUpMenuNodeMinimal(object) :
53 #---------------------------#
54
55     def createPopUpMenu(self):
56         #print "createPopUpMenu"
57         #self.appliEficas.salome=True
58         self.createActions()
59         self.menu = QMenu(self.tree)
60         #self.menu.setStyleSheet("background:rgb(235,235,235); QMenu::item:selected { background-color: red; }")
61         #ne fonctionne pas --> la ligne de commentaire devient rouge
62         self.menu.setStyleSheet("background:rgb(220,220,220); ")
63         #items du menu
64         self.menu.addAction(self.Supprime)
65         if hasattr(self.appliEficas, 'mesScripts'):
66             if self.editor.code in  self.editor.appliEficas.mesScripts :
67                self.dict_commandes_mesScripts=self.appliEficas.mesScripts[self.editor.code].dict_commandes
68                if self.tree.currentItem().item.get_nom() in self.dict_commandes_mesScripts : 
69                    self.ajoutScript()
70     
71     def ajoutScript(self):
72
73     # cochon mais je n arrive pas a faire mieux avec le mecanisme de plugin
74         if hasattr(self.appliEficas, 'mesScripts'):
75             if self.editor.code in  self.editor.appliEficas.mesScripts :
76                self.dict_commandes_mesScripts=self.appliEficas.mesScripts[self.editor.code].dict_commandes
77             else : return
78
79         from Extensions import jdc_include
80         if isinstance(self.item.jdc,jdc_include.JDC_INCLUDE) : return
81
82         listeCommandes=self.dict_commandes_mesScripts[self.tree.currentItem().item.get_nom()]
83         if type(listeCommandes) != tuple: listeCommandes=(listeCommandes,)
84         numero=0
85         for commande in listeCommandes :
86            conditionSalome=commande[3]
87            if (self.appliEficas.salome == 0 and conditionSalome == True): return
88            label=commande[1]
89            tip=commande[5]
90            self.action=QAction(label,self.tree)
91            self.action.setStatusTip(tip)
92            if numero==4: self.action.triggered.connect(self.AppelleFonction4)
93            if numero==3: self.action.triggered.connect(self.AppelleFonction3); numero=4
94            if numero==2: self.action.triggered.connect(self.AppelleFonction2); numero=3
95            if numero==1: self.action.triggered.connect(self.AppelleFonction1); numero=2
96            if numero==0: self.action.triggered.connect(self.AppelleFonction0); numero=1
97            self.menu.addAction(self.action)
98
99
100     def AppelleFonction0(self):
101         self.AppelleFonction(0)
102
103     def AppelleFonction1(self):
104         self.AppelleFonction(1)
105
106     def AppelleFonction2(self):
107         self.AppelleFonction(2)
108
109     def AppelleFonction3(self):
110         self.AppelleFonction(3)
111
112     def AppelleFonction4(self):
113         self.AppelleFonction(4)
114
115
116     def AppelleFonction(self,numero,nodeTraite=None):
117         if nodeTraite==None : nodeTraite=self.tree.currentItem()
118         nomCmd=nodeTraite.item.get_nom()
119         if hasattr(self.appliEficas, 'mesScripts'):
120             if self.editor.code in  self.editor.appliEficas.mesScripts :
121                self.dict_commandes_mesScripts=self.appliEficas.mesScripts[self.editor.code].dict_commandes
122             else : return
123         listeCommandes=self.dict_commandes_mesScripts[nomCmd]
124         commande=listeCommandes[numero]
125         conditionValid=commande[4]
126
127
128         if (nodeTraite.item.isvalid() == 0 and conditionValid == True):
129                  QMessageBox.warning( None, 
130                              tr("item invalide"),
131                              tr("l item doit etre valide"),)
132                  return
133         fonction=commande[0]
134         listenomparam=commande[2]
135         listeparam=[]
136         for p in listenomparam:
137             if hasattr(nodeTraite,p):
138                listeparam.append(getattr(nodeTraite,p))
139             if p=="self" : listeparam.append(self)
140            
141         try :
142            res, commentaire= fonction(listeparam)
143            if not res :
144                  QMessageBox.warning( None, 
145                              tr("echec de la fonction"),
146                              tr(commentaire),)
147                  return
148         except :
149            pass
150         
151
152
153     def createActionsQT4(self):
154         self.CommApres = QAction(tr('apres'),self.tree)
155         self.tree.connect(self.CommApres,SIGNAL("triggered()"),self.addCommApres)
156         self.CommApres.setStatusTip(tr("Insere un commentaire apres la commande "))
157         self.CommAvant = QAction(tr('avant'),self.tree)
158         self.tree.connect(self.CommAvant,SIGNAL("triggered()"),self.addCommAvant)
159         self.CommAvant.setStatusTip(tr("Insere un commentaire avant la commande "))
160
161         self.ParamApres = QAction(tr('apres'),self.tree)
162         self.tree.connect(self.ParamApres,SIGNAL("triggered()"),self.addParametersApres)
163         self.ParamApres.setStatusTip(tr("Insere un parametre apres la commande "))
164         self.ParamAvant = QAction(tr('avant'),self.tree)
165         self.tree.connect(self.ParamAvant,SIGNAL("triggered()"),self.addParametersAvant)
166         self.ParamAvant.setStatusTip(tr("Insere un parametre avant la commande "))
167
168         self.Supprime = QAction(tr('Supprimer'),self.tree)
169         self.tree.connect(self.Supprime,SIGNAL("triggered()"),self.supprimeNoeud)
170         self.Supprime.setStatusTip(tr("supprime le mot clef "))
171         self.Documentation = QAction(tr('Documentation'),self.tree)
172         self.tree.connect(self.Documentation,SIGNAL("triggered()"),self.viewDoc)
173
174     def createActions(self):
175         self.CommApres = QAction(tr('apres'),self.tree)
176         self.CommApres.triggered.connect(self.addCommApres)
177         self.CommApres.setStatusTip(tr("Insere un commentaire apres la commande "))
178         self.CommAvant = QAction(tr('avant'),self.tree)
179         self.CommAvant.triggered.connect(self.addCommAvant)
180         self.CommAvant.setStatusTip(tr("Insere un commentaire avant la commande "))
181
182         self.ParamApres = QAction(tr('apres'),self.tree)
183         self.ParamApres.triggered.connect(self.addParametersApres)
184         self.ParamApres.setStatusTip(tr("Insere un parametre apres la commande "))
185         self.ParamAvant = QAction(tr('avant'),self.tree)
186         self.ParamAvant.triggered.connect(self.addParametersAvant)
187         self.ParamAvant.setStatusTip(tr("Insere un parametre avant la commande "))
188
189         self.Supprime = QAction(tr('Supprimer'),self.tree)
190         self.Supprime.triggered.connect(self.supprimeNoeud)
191         self.Supprime.setStatusTip(tr("supprime le mot clef "))
192         self.Documentation = QAction(tr('Documentation'),self.tree)
193         self.Documentation.triggered.connect(self.viewDoc)
194         self.Documentation.setStatusTip(tr("documentation sur la commande "))
195
196     def supprimeNoeud(self):
197         item= self.tree.currentItem()
198         item.delete()
199
200     def viewDoc(self):
201         self.node=self.tree.currentItem()
202         cle_doc = self.node.item.get_docu()
203         if cle_doc == None :
204             QMessageBox.information( self.editor,tr( "Documentation Vide"), \
205                                     tr("Aucune documentation n'est associee a ce noeud"))
206             return
207         commande = self.editor.appliEficas.CONFIGURATION.exec_acrobat
208         try :
209             f=open(commande,"rb")
210         except :
211              texte=tr("impossible de trouver la commande  ") + commande
212              QMessageBox.information( self.editor, tr("Lecteur PDF"), texte)
213              return
214         import os
215         if cle_doc.startswith('http:'):
216            fichier = cle_doc
217         else :
218             fichier = os.path.abspath(os.path.join(self.editor.CONFIGURATION.path_doc,
219                                        cle_doc))
220             try :
221                f=open(fichier,"rb")
222             except :
223                texte=tr("impossible d'ouvrir ") + fichier
224                QMessageBox.information( self.editor, tr("Documentation Vide"), texte)
225                return
226
227        
228         if os.name == 'nt':
229            os.spawnv(os.P_NOWAIT,commande,(commande,fichier,))
230         elif os.name == 'posix':
231             script ="#!/usr/bin/sh \n%s %s&" %(commande,fichier)
232             pid = os.system(script)
233
234     def addParametersApres(self):
235         item= self.tree.currentItem()
236         item.addParameters(True)
237
238     def addParametersAvant(self):
239         item= self.tree.currentItem()
240         item.addParameters(False)
241
242     def addCommApres(self):
243         item= self.tree.currentItem()
244         item.addComment(True)
245
246     def addCommAvant(self):
247         item= self.tree.currentItem()
248         item.addComment(False)
249
250 #--------------------------------------------#
251 class PopUpMenuNodePartiel (PopUpMenuNodeMinimal):
252 #---------------------------------------------#
253     def createPopUpMenu(self):
254         PopUpMenuNodeMinimal.createPopUpMenu(self)
255         #ss-menu Comment:
256         self.commentMenu=self.menu.addMenu(tr('Commentaire'))
257         self.commentMenu.addAction(self.CommApres)
258         self.commentMenu.addAction(self.CommAvant)
259         #ss-menu Parameters:
260         self.paramMenu =self.menu.addMenu(tr('Parametre')) 
261         self.paramMenu.addAction(self.ParamApres)
262         self.paramMenu.addAction(self.ParamAvant)
263         self.menu.addAction(self.Documentation)
264         self.menu.removeAction(self.Supprime)
265         self.menu.addAction(self.Supprime)
266
267
268 #-----------------------------------------#
269 class PopUpMenuNode(PopUpMenuNodePartiel) :
270 #-----------------------------------------#
271     def createPopUpMenu(self):
272         PopUpMenuNodePartiel.createPopUpMenu(self)
273         self.Commente = QAction(tr('ce noeud'),self.tree)
274         self.Commente.triggered.connect(self.Commenter)
275         self.Commente.setStatusTip(tr("commente le noeud "))
276         self.commentMenu.addAction(self.Commente)
277         self.menu.removeAction(self.Supprime)
278         self.menu.addAction(self.Supprime)
279
280     def Commenter(self):
281         item= self.tree.currentItem()
282         item.commentIt()