Salome HOME
sauve du 9 mai
[tools/eficas.git] / InterfaceQT4 / monWidgetCreeParam.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 # Modules Python
21 import string,types,os,re
22 pattern_name       = re.compile(r'^[^\d\W]\w*\Z')
23
24 # Modules Eficas
25
26 from determine import monEnvQT5
27 if monEnvQT5 :
28    from PyQt5.QtWidgets import QDialog
29    from PyQt5.QtCore import Qt
30 else :
31    from PyQt4.QtGui import *
32    from PyQt4.QtCore import *
33 from Extensions.i18n import tr
34 from desWidgetCreeParam import Ui_desWidgetCreeParam
35
36
37 class MonWidgetCreeParam(Ui_desWidgetCreeParam,QDialog):
38   """
39   """
40   def __init__(self,editor, name = None,fl = 0):
41        self.editor=editor
42        self.editor.affiche_infos("")
43        QDialog.__init__(self,editor)
44        self.setupUi(self)
45        if monEnvQT5 : self.connecterSignaux()
46        else         : self.connecterSignauxQT4()
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
60   def CreeParametre(self):
61         nom=str(self.lineEditNom.text())
62         val=str(self.lineEditVal.text())
63         if val == "" or None : return
64         if nom == "" or None : return
65         if len(self.editor.tree.selectedItems()) == 0 : 
66            itemAvant=self.editor.tree.racine 
67         else :                                     
68            itemAvant=self.editor.tree.selectedItems()[0]
69         param=itemAvant.addParameters(True)
70         param.item.set_nom(nom)
71         #PN self.val permet d entrer du texte
72         param.item.set_valeur(self.val)
73         param.update_node_texte()
74         param.update_node_valid()
75         self.LBParam.addItem((repr(param.item)))
76         param.select()
77         self.lineEditVal.setText("")
78         self.lineEditNom.setText("")
79         self.lineEditNom.setFocus(True)
80
81
82
83   def lineEditValReturnPressed(self):
84         qtVal=self.lineEditVal.text()
85         valString=str(self.lineEditVal.text())
86         self.val=""
87         contexte={}
88         exec "from math import *" in contexte
89         jdc=self.editor.jdc
90         if jdc == None : 
91           self.editor.affiche_infos(tr(u"La Creation de parametre n est possible que dans un jeu de donnees"),Qt.red)
92           return
93
94         for p in jdc.params :
95            try:
96               tp=p.nom+'='+str(repr(p.valeur))
97               exec tp  in contexte
98            except :
99               pass
100         monTexte="monParam="+valString
101         try :
102           exec monTexte in contexte
103           self.val=valString
104         except :
105           try :
106             monTexte="monParam='"+valString+"'"
107             self.val="'"+valString+"'"
108           except :
109             self.editor.affiche_infos(tr("Valeur incorrecte"),Qt.red)
110         if self.lineEditNom.text()!="" and self.dejaExistant==False : self.CreeParametre()
111
112
113   def lineEditNomReturnPressed(self):
114         qtNom=self.lineEditNom.text()
115         nom=str(qtNom)
116         if not pattern_name.match(nom) :
117            self.lineEditNom.setText("")
118            commentaire=nom + tr(" n est pas un identifiant correct\n ")
119            self.editor.affiche_infos(commentaire,Qt.red)
120         for p in self.editor.jdc.params :
121            if p.nom==nom :
122              commentaire=nom + tr(" existe deja\n ")
123              self.editor.affiche_infos(commentaire,Qt.red)
124              return
125
126         if self.lineEditVal.text()!="" : self.CreeParametre()
127         self.lineEditVal.setFocus(Qt.OtherFocusReason)
128
129
130   def initToutesVal(self):
131         self.LBParam.clear()
132         for param in self.listeTousParam :
133             self.LBParam.addItem((repr(param)))
134             self.dictListe[repr(param)] = param
135
136   def valideParam(self):
137         if self.LBParam.selectedItems()== None : return
138         lParam=[]
139         for indice in range(len(self.LBParam.selectedItems())):
140             i=self.LBParam.selectedItems()[indice].text()
141             param=self.dictListe[i]
142             lParam.append(param)
143
144         try :
145           self.panel.ajoutNValeur(lParam)
146         except :
147           for p in lParam :
148              self.panel.ajout1Valeur(p)
149         self.close()
150