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