Salome HOME
sauve du 10/06
[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
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.tree.currentItem().item.get_nom() in self.appliEficas.mesScripts.dict_commandes.keys() : 
71                self.ajoutScript()
72     
73     def ajoutScript(self):
74
75     # cochon mais je n arrive pas a faire mieux avec le mecanisme de plugin
76         listeCommandes=self.appliEficas.mesScripts.dict_commandes[self.tree.currentItem().item.get_nom()]
77         if type(listeCommandes) != types.TupleType: listeCommandes=(listeCommandes,)
78         numero=0
79         for commande in listeCommandes :
80            conditionSalome=commande[3]
81            if (self.appliEficas.salome == 0 and conditionSalome == True): return
82            label=commande[1]
83            tip=commande[5]
84            self.action=QAction(label,self.tree)
85            self.action.setStatusTip(tip)
86            if monEnvQT5 :
87               if numero==4: 
88                  self.action.triggered.connect(self.AppelleFonction4)
89               if numero==3: 
90                  self.action.triggered.connect(self.AppelleFonction3)
91                  numero=4
92               if numero==2: 
93                  self.action.triggered.connect(self.AppelleFonction2)
94                  numero=3
95               if numero==1: 
96                  self.action.triggered.connect(self.AppelleFonction1)
97                  numero=2
98               if numero==0: 
99                  self.action.triggered.connect(self.AppelleFonction0)
100                  numero=1
101            else:
102               if numero==4: 
103                  self.tree.connect(self.action,SIGNAL("triggered()"),self.AppelleFonction4)
104               if numero==3: 
105                  self.tree.connect(self.action,SIGNAL("triggered()"),self.AppelleFonction3)
106                  numero=4
107               if numero==2: 
108                  self.tree.connect(self.action,SIGNAL("triggered()"),self.AppelleFonction2)
109                  numero=3
110               if numero==1: 
111                  self.tree.connect(self.action,SIGNAL("triggered()"),self.AppelleFonction1)
112                  numero=2
113               if numero==0: 
114                  self.tree.connect(self.action,SIGNAL("triggered()"),self.AppelleFonction0)
115                  numero=1
116            self.menu.addAction(self.action)
117
118
119     def AppelleFonction0(self):
120         self.AppelleFonction(0)
121
122     def AppelleFonction1(self):
123         self.AppelleFonction(1)
124
125     def AppelleFonction2(self):
126         self.AppelleFonction(2)
127
128     def AppelleFonction3(self):
129         self.AppelleFonction(3)
130
131     def AppelleFonction4(self):
132         self.AppelleFonction(4)
133
134
135     def AppelleFonction(self,numero):
136         listeCommandes=self.appliEficas.mesScripts.dict_commandes[self.tree.currentItem().item.get_nom()]
137         commande=listeCommandes[numero]
138         conditionValid=commande[4]
139         if (self.tree.currentItem().item.isvalid() == 0 and conditionValid == True):
140                  QMessageBox.warning( None, 
141                              tr("item invalide"),
142                              tr("l item doit etre valide"),)
143                  return
144         fonction=commande[0]
145         listenomparam=commande[2]
146         listeparam=[]
147         for p in listenomparam:
148             if hasattr(self.tree.currentItem(),p):
149                listeparam.append(getattr(self.tree.currentItem(),p))
150             if p=="self" : listeparam.append(self)
151         try :
152            fonction(listeparam,self.appliEficas)
153         except :
154            fonction(listeparam)
155
156
157     def createActionsQT4(self):
158         self.CommApres = QAction(tr('apres'),self.tree)
159         self.tree.connect(self.CommApres,SIGNAL("triggered()"),self.addCommApres)
160         self.CommApres.setStatusTip(tr("Insere un commentaire apres la commande "))
161         self.CommAvant = QAction(tr('avant'),self.tree)
162         self.tree.connect(self.CommAvant,SIGNAL("triggered()"),self.addCommAvant)
163         self.CommAvant.setStatusTip(tr("Insere un commentaire avant la commande "))
164
165         self.ParamApres = QAction(tr('apres'),self.tree)
166         self.tree.connect(self.ParamApres,SIGNAL("triggered()"),self.addParametersApres)
167         self.ParamApres.setStatusTip(tr("Insere un parametre apres la commande "))
168         self.ParamAvant = QAction(tr('avant'),self.tree)
169         self.tree.connect(self.ParamAvant,SIGNAL("triggered()"),self.addParametersAvant)
170         self.ParamAvant.setStatusTip(tr("Insere un parametre avant la commande "))
171
172         self.Supprime = QAction(tr('Supprimer'),self.tree)
173         self.tree.connect(self.Supprime,SIGNAL("triggered()"),self.supprimeNoeud)
174         self.Supprime.setStatusTip(tr("supprime le mot clef "))
175         self.Documentation = QAction(tr('Documentation'),self.tree)
176         self.tree.connect(self.Documentation,SIGNAL("triggered()"),self.viewDoc)
177
178     def createActions(self):
179         self.CommApres = QAction(tr('apres'),self.tree)
180         self.CommApres.triggered.connect(self.addCommApres)
181         self.CommApres.setStatusTip(tr("Insere un commentaire apres la commande "))
182         self.CommAvant = QAction(tr('avant'),self.tree)
183         self.CommAvant.triggered.connect(self.addCommAvant)
184         self.CommAvant.setStatusTip(tr("Insere un commentaire avant la commande "))
185
186         self.ParamApres = QAction(tr('apres'),self.tree)
187         self.ParamApres.triggered.connect(self.addParametersApres)
188         self.ParamApres.setStatusTip(tr("Insere un parametre apres la commande "))
189         self.ParamAvant = QAction(tr('avant'),self.tree)
190         self.ParamAvant.triggered.connect(self.addParametersAvant)
191         self.ParamAvant.setStatusTip(tr("Insere un parametre avant la commande "))
192
193         self.Supprime = QAction(tr('Supprimer'),self.tree)
194         self.Supprime.triggered.connect(self.supprimeNoeud)
195         self.Supprime.setStatusTip(tr("supprime le mot clef "))
196         self.Documentation = QAction(tr('Documentation'),self.tree)
197         self.Documentation.triggered.connect(self.viewDoc)
198         self.Documentation.setStatusTip(tr("documentation sur la commande "))
199
200     def supprimeNoeud(self):
201         item= self.tree.currentItem()
202         item.delete()
203
204     def viewDoc(self):
205         self.node=self.tree.currentItem()
206         cle_doc = self.node.item.get_docu()
207         if cle_doc == None :
208             QMessageBox.information( self.editor,tr( "Documentation Vide"), \
209                                     tr("Aucune documentation n'est associee a ce noeud"))
210             return
211         commande = self.editor.appliEficas.CONFIGURATION.exec_acrobat
212         try :
213             f=open(commande,"rb")
214         except :
215              texte=tr("impossible de trouver la commande  ") + commande
216              QMessageBox.information( self.editor, tr("Lecteur PDF"), texte)
217              return
218         import os
219         if cle_doc.startswith('http:'):
220            fichier = cle_doc
221         else :
222             fichier = os.path.abspath(os.path.join(self.editor.CONFIGURATION.path_doc,
223                                        cle_doc))
224             try :
225                f=open(fichier,"rb")
226             except :
227                texte=tr("impossible d'ouvrir ") + fichier
228                QMessageBox.information( self.editor, tr("Documentation Vide"), texte)
229                return
230
231        
232         if os.name == 'nt':
233            os.spawnv(os.P_NOWAIT,commande,(commande,fichier,))
234         elif os.name == 'posix':
235             script ="#!/usr/bin/sh \n%s %s&" %(commande,fichier)
236             pid = os.system(script)
237
238     def addParametersApres(self):
239         item= self.tree.currentItem()
240         item.addParameters(True)
241
242     def addParametersAvant(self):
243         item= self.tree.currentItem()
244         item.addParameters(False)
245
246     def addCommApres(self):
247         item= self.tree.currentItem()
248         item.addComment(True)
249
250     def addCommAvant(self):
251         item= self.tree.currentItem()
252         item.addComment(False)
253
254 #--------------------------------------------#
255 class PopUpMenuNodePartiel (PopUpMenuNodeMinimal):
256 #---------------------------------------------#
257     def createPopUpMenu(self):
258         PopUpMenuNodeMinimal.createPopUpMenu(self)
259         #ss-menu Comment:
260         self.commentMenu=self.menu.addMenu(tr('Commentaire'))
261         self.commentMenu.addAction(self.CommApres)
262         self.commentMenu.addAction(self.CommAvant)
263         #ss-menu Parameters:
264         self.paramMenu =self.menu.addMenu(tr('Parametre')) 
265         self.paramMenu.addAction(self.ParamApres)
266         self.paramMenu.addAction(self.ParamAvant)
267         self.menu.addAction(self.Documentation)
268         self.menu.removeAction(self.Supprime)
269         self.menu.addAction(self.Supprime)
270
271
272 #-----------------------------------------#
273 class PopUpMenuNode(PopUpMenuNodePartiel) :
274 #-----------------------------------------#
275     def createPopUpMenu(self):
276         PopUpMenuNodePartiel.createPopUpMenu(self)
277         self.Commente = QAction(tr('ce noeud'),self.tree)
278         self.Commente.triggered.connect(self.Commenter)
279         self.Commente.setStatusTip(tr("commente le noeud "))
280         self.commentMenu.addAction(self.Commente)
281         self.menu.removeAction(self.Supprime)
282         self.menu.addAction(self.Supprime)
283
284     def Commenter(self):
285         item= self.tree.currentItem()
286         item.commentIt()