Salome HOME
bug
[tools/eficas.git] / InterfaceQT4 / monWidgetCreeParam.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017   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 # Modules Python
21 from __future__ import absolute_import
22 try :
23    from builtins import object
24 except : pass
25
26 import types,os,re
27 from six.moves import range
28 pattern_name       = re.compile(r'^[^\d\W]\w*\Z')
29
30 # Modules Eficas
31
32 from PyQt5.QtWidgets import QDialog, QMessageBox
33 from PyQt5.QtCore import Qt
34 from Extensions.i18n import tr
35 from desWidgetCreeParam import Ui_desWidgetCreeParam
36
37
38 class MonWidgetCreeParam(Ui_desWidgetCreeParam,QDialog):
39   """
40   """
41   def __init__(self,editor, name = None,fl = 0):
42        self.editor=editor
43        self.editor.afficheInfos("")
44        QDialog.__init__(self,editor)
45        self.setupUi(self)
46        self.connecterSignaux()
47        self.dejaExistant=0
48        self.listeTousParam=self.editor.jdc.params
49        self.dictListe={}
50        self.initToutesVal()
51
52   def connecterSignauxQT4(self) :
53         self.connect(self.lineEditVal,SIGNAL("returnPressed()"),self.lineEditValReturnPressed)
54         self.connect(self.lineEditNom,SIGNAL("returnPressed()"),self.lineEditNomReturnPressed)
55
56   def connecterSignaux(self) :
57         self.lineEditVal.returnPressed.connect(self.lineEditValReturnPressed)
58         self.lineEditNom.returnPressed.connect(self.lineEditNomReturnPressed)
59         self.LBParam.itemDoubleClicked.connect(self.paramClicked)
60
61   def paramClicked(self,item):
62         if self.editor.nodeEnCours == None : 
63             QMessageBox.warning( self, tr("Pas de Mot-Clef"),tr("Attention! selectionnez un mot-clef"))
64             return
65         param= self.dictListe[item.text()]
66         self.editor.nodeEnCours.lineEditVal.setText(param)
67         self.editor.nodeEnCours.LEvaleurPressed()
68        
69
70   def creeParametre(self):
71         nom=str(self.lineEditNom.text())
72         val=str(self.lineEditVal.text())
73         if val == "" or None : return
74         if nom == "" or None : return
75         if len(self.editor.tree.selectedItems()) == 0 : 
76            itemAvant=self.editor.tree.racine 
77         else :                                     
78            itemAvant=self.editor.tree.selectedItems()[0]
79         param=itemAvant.addParameters(True)
80         param.item.setNom(nom)
81         #PN self.val permet d entrer du texte
82         param.item.setValeur(self.val)
83         param.updateNodeTexte()
84         param.updateNodeValid()
85         self.LBParam.addItem((repr(param.item.object)))
86         self.dictListe[repr(param.item.object)] = param.item.object.nom
87         self.lineEditVal.setText("")
88         self.lineEditNom.setText("")
89         self.lineEditNom.setFocus(True)
90
91
92
93   def lineEditValReturnPressed(self):
94         qtVal=self.lineEditVal.text()
95         valString=str(self.lineEditVal.text())
96         self.val=""
97         contexte={}
98         exec("from math import *", contexte)
99         jdc=self.editor.jdc
100         if jdc == None : 
101           self.editor.afficheInfos(tr(u"La Creation de parametre n est possible que dans un jeu de donnees"),Qt.red)
102           return
103
104         for p in jdc.params :
105            try:
106               tp=p.nom+'='+str(repr(p.valeur))
107               exec(tp, contexte)
108            except :
109               pass
110         monTexte="monParam="+valString
111         try :
112           exec(monTexte, contexte)
113           self.val=valString
114         except :
115           try :
116             monTexte="monParam='"+valString+"'"
117             self.val="'"+valString+"'"
118           except :
119             self.editor.afficheInfos(tr("Valeur incorrecte"),Qt.red)
120         if self.lineEditNom.text()!="" and self.dejaExistant==False : self.creeParametre()
121
122
123   def lineEditNomReturnPressed(self):
124         qtNom=self.lineEditNom.text()
125         nom=str(qtNom)
126         if not pattern_name.match(nom) :
127            self.lineEditNom.setText("")
128            commentaire=nom + tr(" n est pas un identifiant correct\n ")
129            self.editor.afficheInfos(commentaire,Qt.red)
130         for p in self.editor.jdc.params :
131            if p.nom==nom :
132              commentaire=nom + tr(" existe deja\n ")
133              self.editor.afficheInfos(commentaire,Qt.red)
134              return
135
136         if self.lineEditVal.text()!="" : self.creeParametre()
137         self.lineEditVal.setFocus(Qt.OtherFocusReason)
138
139
140   def initToutesVal(self):
141         self.LBParam.clear()
142         for param in self.listeTousParam :
143             self.LBParam.addItem((repr(param)))
144             self.dictListe[repr(param)] = param.nom
145          
146          
147   
148   def valideParam(self):
149         if self.LBParam.selectedItems()== None : return
150         lParam=[]
151         for indice in range(len(self.LBParam.selectedItems())):
152             i=self.LBParam.selectedItems()[indice].text()
153             param=self.dictListe[i]
154             lParam.append(param)
155
156         try :
157           self.panel.ajoutNValeur(lParam)
158         except :
159           for p in lParam :
160              self.panel.ajout1Valeur(p)
161         self.close()
162