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