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