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