Salome HOME
complexe
[tools/eficas.git] / InterfaceQT4 / monWidgetSimpComplexe.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
22
23 # Modules Eficas
24 from PyQt4.QtGui import *
25 from PyQt4.QtCore import *
26 from Extensions.i18n import tr
27
28 from feuille                import Feuille
29 from desWidgetSimpComplexe  import Ui_WidgetSimpComplexe 
30 from politiquesValidation   import PolitiqueUnique
31 from qtSaisie               import SaisieValeur
32
33
34 class MonWidgetSimpComplexe (Ui_WidgetSimpComplexe,Feuille):
35
36   def __init__(self,node,monSimpDef,nom,objSimp,parentQt):
37         Feuille.__init__(self,node,monSimpDef,nom,objSimp,parentQt)
38         self.parentQt.commandesLayout.insertWidget(-1,self)
39         self.setFocusPolicy(Qt.StrongFocus)
40         self.connect(self.LEImag,SIGNAL("returnPressed()"),self.LEImagRPressed)
41         self.connect(self.LEReel,SIGNAL("returnPressed()"),self.LEReelRPressed)
42         self.connect(self.RBRI,SIGNAL("clicked()"), self.ValeurPressed )
43         self.connect(self.RBMP,SIGNAL("clicked()"), self.ValeurPressed )
44         self.connect(self.LEComp,SIGNAL("returnPressed()"),self.LECompRPressed)
45
46
47   def setValeurs(self):
48        self.politique=PolitiqueUnique(self.node,self.editor)
49        valeur=self.node.item.get_valeur()
50        if valeur == None or valeur == '' : return
51        if type(valeur) not in (types.ListType,types.TupleType) :
52            self.LEComp.setText(str(valeur))
53        else :
54            typ_cplx,x1,x2=valeur
55            self.LEReel.setText(str(x1))
56            self.LEImag.setText(str(x2))
57            if typ_cplx == "RI" :
58               self.RBRI.setChecked(1)
59            else :
60               self.RBMP.setChecked(1)
61
62   def LECompRPressed(self) :
63         self.LEReel.clear()
64         self.LEImag.clear()
65         commentaire=tr("expression valide")
66         valeur = str(self.LEComp.text())
67         d={}
68         try :
69           v=eval(valeur,d)
70         except :
71           commentaire=tr("expression invalide")
72           self.editor.affiche_infos(commentaire,Qt.red)
73           return
74         try :
75           i=v.imag
76           self.editor.affiche_infos(commentaire)
77           self.ValeurPressed()
78         except :
79           commentaire=tr("l expression n est pas de la forme a+bj")
80           self.editor.affiche_infos(commentaire,Qt.red)
81
82   def LEReelRPressed(self):
83         self.LEComp.clear()
84         commentaire=tr("expression valide")
85         valeur = str(self.LEReel.text())
86         try :
87           a=string.atof(valeur)
88           self.editor.affiche_infos(commentaire)
89         except :
90           commentaire=tr("expression invalide")
91           self.editor.affiche_infos(commentaire,Qt.red)
92         if self.LEImag.text()!="" : self.ValeurPressed()
93
94   def LEImagRPressed(self):
95         self.LEComp.clear()
96         commentaire=tr("expression valide")
97         valeur = str(self.LEImag.text())
98         try :
99           a=string.atof(valeur)
100           self.editor.affiche_infos(commentaire)
101         except :
102           commentaire=tr("expression invalide")
103           self.editor.affiche_infos(commentaire,Qt.red)
104         if self.LEReel.text()!="" : self.ValeurPressed()
105
106   def finCommentaire(self):
107       commentaire="valeur de type complexe"
108       return commentaire
109
110   def getValeurComp(self):
111         commentaire=tr("expression valide")
112         valeur = str(self.LEComp.text())
113         d={}
114         try :
115           v=eval(valeur,d)
116         except :
117           commentaire=tr("expression invalide")
118           self.editor.affiche_infos(commentaire,Qt.red)
119           return None
120         try :
121           i=v.imag
122         except :
123           commentaire=tr("expression n est pas de la forme a+bj")
124           self.editor.affiche_infos(commentaire,Qt.red)
125           return None
126         return v
127
128
129   def ValeurPressed(self):
130       if self.LEComp.text()== ""  and (self.LEReel.text()=="" or self.LEImag.text()=="") :
131          return
132       if self.LEComp.text()== "" : valeur = self.getValeurRI()
133       else :
134           if self.LEReel.text() != "" or self.LEImag.text() != "" :
135               commentaire=tr("entrer une seule valeur SVP")
136               self.editor.affiche_infos(commentaire,Qt.red)
137               return
138           valeur=  self.getValeurComp()
139       self.politique.RecordValeur(valeur)
140       self.parentQt.donneFocus()
141
142   def getValeurRI(self):
143       """
144       Retourne le complexe saisi par l'utilisateur
145       """
146       l=[]
147       if  (self.RBMP.isChecked() == 1 ) :
148          l.append("MP")
149       elif (self.RBRI.isChecked() == 1) :
150          l.append("RI")
151       else :
152          commentaire=tr("saisir le type de complexe")
153          self.editor.affiche_infos(commentaire,Qt.red)
154          return None
155       try :
156          l.append(string.atof(str(self.LEReel.text())))
157          l.append(string.atof(str(self.LEImag.text())))
158       except :
159          return None
160       return `tuple(l)`
161
162