Salome HOME
modifs pour Telemac
[tools/eficas.git] / InterfaceQT4 / monWidgetPlusieursTuple.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 str
24    from builtins import range
25    from builtins import object
26 except : pass
27
28 import types,os,sys
29
30 from six.moves import range
31 from PyQt5.QtWidgets  import QFrame,QApplication, QFrame, QWidget
32 from PyQt5.QtGui      import QIcon
33 from PyQt5.QtCore     import QSize, Qt, QTimer
34
35 from Extensions.i18n import tr
36
37
38 from InterfaceQT4.feuille               import Feuille
39 from InterfaceQT4.politiquesValidation  import PolitiquePlusieurs
40 from InterfaceQT4.qtSaisie              import SaisieValeur
41 from InterfaceQT4.gereListe             import GereListe
42 from InterfaceQT4.gereListe             import LECustom
43 from Tuple2                             import Ui_Tuple2
44 from Tuple3                             import Ui_Tuple3
45 from Tuple4                             import Ui_Tuple4
46 from Tuple5                             import Ui_Tuple5
47 from Tuple6                             import Ui_Tuple6
48 from Tuple7                             import Ui_Tuple7
49 from Tuple8                             import Ui_Tuple8
50 from Tuple9                             import Ui_Tuple9
51 from Tuple10                            import Ui_Tuple10
52
53
54 #--------------------------
55 class TupleCustom(object) :
56 #--------------------------
57
58   def __init__(self,tailleTuple,parent,parentQt,index):
59   #-------------------------------------------------
60       QWidget.__init__(self,parent)
61       self.setupUi(self)
62       self.tailleTuple=tailleTuple
63       self.parent=parent
64       self.parentQt=parentQt
65       self.valeur=[]
66       self.index=index
67       self.inFocusOutEvent=False
68
69
70       for i in range(self.tailleTuple):
71          nomLE="lineEditVal_"+str(i+1)
72          courant=getattr(self,nomLE)
73          courant.num=index
74          courant.dansUnTuple=True
75          courant.returnPressed.connect(self.valueChange)
76          courant.numDsLaListe = i+1
77          courant.tupleCustomParent=self
78          courant.parentTuple = self
79
80
81   def valueChange(self):
82   #----------------------
83
84       listeVal=[]
85       for i in range(self.tailleTuple):
86          nomLE="lineEditVal_"+str(i+1)
87          courant=getattr(self,nomLE)
88          val=str(courant.text())
89         
90          if str(val)=="" or val==None : 
91             if not self.inFocusOutEvent : courant.setFocus()
92             return
93
94          try :
95              valeur=eval(val,{})
96          except :
97            try :
98              d=self.parentQt.objSimp.jdc.getContexteAvant(self.parentQt.objSimp. etape)
99              valeur=eval(val,d)
100            except :
101              valeur=val
102          listeVal.append(valeur)
103       self.valeur=listeVal
104       self.parentQt.changeValeur()
105
106
107   def setValeur(self,value):
108   #----------------------
109
110       listeVal=[]
111       valeurNulle=True
112       for i in range(self.tailleTuple):
113          nomLE="lineEditVal_"+str(i+1)
114          courant=getattr(self,nomLE)
115          try :
116            if str(value[i]) != "" : valeurNulle=False
117          except :
118            pass
119
120          try :
121            courant.setText(str(value[i]))
122          except :
123            courant.setText("")
124          val=str(courant.text())
125          try :
126            valeur=eval(val,{})
127          except :
128            try :
129              d=self.parentQt.objSimp.jdc.getContexteAvant(self.parentQt.objSimp. etape)
130              valeur=eval(val,d)
131            except :
132              valeur=val
133          listeVal.append(valeur)
134       if  valeurNulle == True : self.valeur=None
135       else                    : self.valeur=listeVal
136
137   def getValeur(self):
138   #----------------------
139       self.valeur=[]
140       vide=True
141       for i in range(self.tailleTuple):
142          nomLE="lineEditVal_"+str(i+1)
143          courant=getattr(self,nomLE)
144          self.valeur.append(courant.valeur)
145          if courant.valeur!= None : vide = False 
146       if vide : self.valeur=[]
147       return self.valeur
148
149   def text(self):
150   #--------------
151       return self.valeur
152
153   def setText(self,value):
154   #-----------------------
155       self.setValeur(value)
156
157   def clean(self):
158   #-------------------
159       self.valeur=None
160       for i in range(self.tailleTuple):
161          nomLE="lineEditVal_"+str(i+1)
162          courant=getattr(self,nomLE)
163          courant.setText("")
164
165   def finCommentaire(self):
166   #-------------------
167         return self.finCommentaireListe()
168
169
170 #-------------------------------------------------
171 class TupleCustom2(QWidget,Ui_Tuple2,TupleCustom):
172 #-------------------------------------------------
173   def __init__(self,tailleTuple,parent,parentQt,index):
174   #-------------------
175       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
176       if self.parentQt.editor.maConfiguration.closeParenthese :
177            self.label_5.close()
178            self.label_7.close()
179
180
181 #-------------------------------------------------
182 class TupleCustom3(QWidget,Ui_Tuple3,TupleCustom):
183 #-------------------------------------------------
184   def __init__(self,tailleTuple,parent,parentQt,index):
185   #-------------------
186       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
187
188 #-------------------------------------------------
189 class TupleCustom4(QWidget,Ui_Tuple4,TupleCustom):
190 #-------------------------------------------------
191   def __init__(self,tailleTuple,parent,parentQt,index):
192   #-------------------
193       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
194
195 #-------------------------------------------------
196 class TupleCustom5(QWidget,Ui_Tuple5,TupleCustom):
197 #-------------------------------------------------
198   def __init__(self,tailleTuple,parent,parentQt,index):
199   #-------------------
200       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
201
202 #-------------------------------------------------
203 class TupleCustom6(QWidget,Ui_Tuple6,TupleCustom):
204 #-------------------------------------------------
205   def __init__(self,tailleTuple,parent,parentQt,index):
206   #-------------------
207       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
208
209 #-------------------------------------------------
210 class TupleCustom7(QWidget,Ui_Tuple7,TupleCustom):
211 #-------------------------------------------------
212   def __init__(self,tailleTuple,parent,parentQt,index):
213   #-------------------
214       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
215
216 #-------------------------------------------------
217 class TupleCustom8(QWidget,Ui_Tuple8,TupleCustom):
218 #-------------------------------------------------
219   def __init__(self,tailleTuple,parent,parentQt,index):
220   #-------------------
221       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
222
223 #-------------------------------------------------
224 class TupleCustom9(QWidget,Ui_Tuple9,TupleCustom):
225 #-------------------------------------------------
226   def __init__(self,tailleTuple,parent,parentQt,index):
227   #-------------------
228       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
229
230 #-------------------------------------------------
231 class TupleCustom10(QWidget,Ui_Tuple10,TupleCustom):
232 #-------------------------------------------------
233   def __init__(self,tailleTuple,parent,parentQt,index):
234   #-------------------
235       TupleCustom.__init__(self,tailleTuple,parent,parentQt,index)
236       if self.parentQt.editor.maConfiguration.closeParenthese :
237            self.label_5.close()
238            self.label_7.close()
239
240
241
242
243
244 # -------------------------------------------- #
245 class MonWidgetPlusieursTuple(Feuille,GereListe):
246 # -------------------------------------------- #
247
248   def __init__(self, node, monSimpDef, nom, objSimp, parentQt, commande):
249   #-----------------------------------------------------
250
251         self.indexDernierLabel=0
252         self.numLineEditEnCours=0
253         self.nomLine="TupleVal"
254         self.listeAffichageWidget=[]
255         Feuille.__init__(self,node,monSimpDef,nom,objSimp,parentQt,commande)
256         GereListe.__init__(self)
257         self.finCommentaireListe()
258         self.politique=PolitiquePlusieurs(self.node,self.editor)
259         self.parentQt.commandesLayout.insertWidget(-1,self)
260
261         if sys.platform[0:5]!="linux":
262           repIcon=self.node.editor.appliEficas.repIcon
263           fichier=os.path.join(repIcon, 'arrow_up.png')
264           icon = QIcon(fichier)
265           self.RBHaut.setIcon(icon)
266           self.RBHaut.setIconSize(QSize(32, 32))
267           fichier2=os.path.join(repIcon, 'arrow_down.png')
268           icon2 = QIcon(fichier2)
269           self.RBBas.setIcon(icon2)
270           fichier3=os.path.join(repIcon, 'file-explorer.png')
271           icon3 = QIcon(fichier3)
272           self.BSelectFichier.setIcon(icon3)
273           self.BSelectFichier.setIconSize(QSize(32, 32))
274         self.BSelectFichier.clicked.connect(self.selectInFile)
275  
276
277
278
279   def ajoutLineEdit(self,valeur=None,inInit=False):
280   #------------------------------------------------
281       self.indexDernierLabel=self.indexDernierLabel+1
282       nomLineEdit=self.nomLine+str(self.indexDernierLabel)
283       if hasattr(self,nomLineEdit) :
284          self.indexDernierLabel=self.indexDernierLabel-1
285          return
286
287       nomCustomTuple='TupleCustom'+str(self.nbValeurs)
288       laClasseDuTuple=globals()[nomCustomTuple]
289       nouveauLE=laClasseDuTuple(self.nbValeurs,self.scrollArea,self,self.indexDernierLabel)
290
291       #if self.nbValeurs == 2 : nouveauLE = TupleCustom2(self.nbValeurs,self.scrollArea,self,self.indexDernierLabel)
292       #else                   : nouveauLE = TupleCustom3(self.nbValeurs,self.scrollArea,self,self.indexDernierLabel)
293                  
294       self.verticalLayoutLE.insertWidget(self.indexDernierLabel-1,nouveauLE)
295       setattr(self,nomLineEdit,nouveauLE)
296       if valeur != None : nouveauLE.setValeur(valeur)
297
298       for i in range(self.nbValeurs) :
299           num=i+1
300           nomLineEdit='lineEditVal_'+str(num)
301           lineEditVal=getattr(nouveauLE,nomLineEdit)
302           self.listeAffichageWidget.append(lineEditVal)
303       #self.listeAffichageWidget.append(nouveauLE.lineEditVal_1)
304       #self.listeAffichageWidget.append(nouveauLE.lineEditVal_2)
305       #if self.nbValeurs == 3 : self.listeAffichageWidget.append(nouveauLE.lineEditVal_3)
306
307       self.etablitOrdre()
308
309       # deux lignes pour que le ensureVisible fonctionne
310       self.estVisible=nouveauLE.lineEditVal_1
311       if inInit==False :QTimer.singleShot(1, self.rendVisibleLigne)
312
313   def etablitOrdre(self):
314   # ---------------------
315       i=0
316       while(i +1 < len(self.listeAffichageWidget)):
317          self.listeAffichageWidget[i].setFocusPolicy(Qt.StrongFocus)
318          self.setTabOrder(self.listeAffichageWidget[i],self.listeAffichageWidget[i+1])
319          i=i+1
320
321
322   def setValeurs(self):
323   # ---------------------
324        if self.editor.code == 'PSEN' : self.RBListePush()
325        valeurs=self.node.item.getValeur()
326        min,max=self.node.item.getMinMax()
327        if max == "**" or max > 8 : aCreer= 8
328        else : aCreer=max 
329
330        if valeurs == () or valeurs == None :
331           for i in range(aCreer): self.ajoutLineEdit(inInit=True)
332           return
333
334        for v in valeurs:
335            self.ajoutLineEdit(v,inInit=True)
336
337        for i in range(len(valeurs),aCreer) : self.ajoutLineEdit(inInit=True)
338
339   def rendVisibleLigne(self):
340   # -------------------------
341       QApplication.processEvents()
342       self.estVisible.setFocus(True)
343       self.scrollArea.ensureWidgetVisible(self.estVisible,0,0)
344
345    
346   def changeValeur(self,changeDePlace=False,oblige=True):
347   # -----------------------------------------------------
348       #Pour compatibilite signature
349
350       aLeFocus=self.focusWidget()
351       listeComplete=[]
352       libre=False
353       #print (self.indexDernierLabel)
354       for i in range(self.indexDernierLabel) :
355           nom=self.nomLine+str(i+1)
356           courant=getattr(self,nom)
357           valeurTuple=courant.valeur
358           #print (courant, i, valeurTuple)
359           if valeurTuple == None or valeurTuple== "" or valeurTuple==[]: 
360              libre=True
361              continue
362           validite,comm,comm2,listeRetour= self.politique.ajoutTuple(valeurTuple,listeComplete)
363           if not validite:
364              if comm2 != '' : comm += " " + comm2
365              self.editor.afficheInfos(comm+" "+str(self.objSimp.definition.validators.typeDesTuples),Qt.red)
366              return
367           listeComplete.append(tuple(courant.valeur))
368       #print ('listeComplete', listeComplete)
369       if listeComplete == [] : listeComplete=None
370       self.node.item.setValeur(listeComplete)
371
372       if changeDePlace : return
373       min,max=self.node.item.getMinMax()
374       if self.indexDernierLabel == max  : self.editor.afficheInfos(tr('Nb maximum de valeurs atteint'))
375       if self.indexDernierLabel < max and libre==False :
376           self.ajoutLineEdit()
377           self.listeAffichageWidget[-2].setFocus(True)
378       else :
379          try :
380            QApplication.processEvents()
381            w=self.listeAffichageWidget[self.listeAffichageWidget.index(aLeFocus)+1]
382            w.setFocus(True)
383            self.scrollArea.ensureWidgetVisible(w,0,0)
384          except :
385            pass
386           
387   def ajoutNValeur(self,liste):
388   #----------------------------
389   # attention quand on charge par un fichier, on ne peut pas se contenter d ajouter N fois 1 valeur
390   # car alors le temps de verification devient prohibitif  reconstructu=ion et verification a 
391   # chaque valeur. d ou l ajout de ajoutNTuple a politique plusieurs
392
393         if len(liste)%self.nbValeurs != 0 :
394            texte="Nombre incorrect de valeurs"
395            self.editor.afficheInfos(tr(texte),Qt.red)
396            return
397
398         i=0
399         longueur=len(liste)//self.nbValeurs
400         increment=self.nbValeurs
401         listeFormatee=[liste[k*increment:(k+1)*increment] for k in range(longueur)]
402         listeFormatee=tuple(listeFormatee)
403
404         min,max=self.node.item.getMinMax()
405         if self.objSimp.valeur == None : listeComplete=listeFormatee
406         else : listeComplete =self.objSimp.valeur + listeFormatee
407
408         if len(listeComplete) > max : 
409            texte=tr("Nombre maximum de valeurs ")+str(max)+tr(" atteint")
410            self.editor.afficheInfos(texte,Qt.red)
411            return
412
413         validite,comm,comm2,listeRetour= self.politique.ajoutNTuple(listeComplete)
414         if not validite : 
415            self.editor.afficheInfos(comm+comm2,Qt.red)
416            return
417
418         # on calcule le dernier lineedit rempli avant de changer la valeur
419         if self.objSimp.valeur != None : indexDernierRempli=len(self.objSimp.valeur)
420         else : indexDernierRempli=0
421
422         self.politique.recordValeur(listeComplete)
423
424         while ( i < len(liste) ) :
425             try :
426               t=tuple(liste[i:i+self.nbValeurs])
427             except:
428               t=tuple(liste[i:len(liste)])
429             i=i+self.nbValeurs
430             if indexDernierRempli < self.indexDernierLabel:
431
432                nomLEARemplir=self.nomLine+str(indexDernierRempli+1)
433                LEARemplir=getattr(self,nomLEARemplir) 
434                for n in range(self.nbValeurs) :
435                   nomLineEdit='lineEditVal_'+str(n+1)
436                   lineEditVal=getattr(LEARemplir,nomLineEdit)
437                   lineEditVal.setText(str(t[n]))
438             else : 
439                # ne pas appeler ajoutLineEdit(t,False ) pb de boucle pb du a etablitOrdre et a listeWidgetAffichage qui bouge
440                self.indexDernierLabel=self.indexDernierLabel+1
441                nomLineEdit=self.nomLine+str(self.indexDernierLabel)
442
443                nomCustomTuple='TupleCustom'+str(self.nbValeurs)
444                laClasseDuTuple=globals()[nomCustomTuple]
445                nouveauLE=laClasseDuTuple(self.nbValeurs,self.scrollArea,self,self.indexDernierLabel)
446                  
447                self.verticalLayoutLE.insertWidget(self.indexDernierLabel-1,nouveauLE)
448                setattr(self,nomLineEdit,nouveauLE)
449                nouveauLE.setValeur(t)
450
451                for n in range(self.nbValeurs) :
452                   nomLineEdit='lineEditVal_'+str(n+1)
453                   lineEditVal=getattr(nouveauLE,nomLineEdit)
454                   self.listeAffichageWidget.append(lineEditVal)
455             indexDernierRempli = indexDernierRempli+1
456
457         self.etablitOrdre()
458
459         
460
461   def RBListePush(self):
462   #----------------------
463   # PN a rendre generique avec un truc tel prerempli
464   # pour l instant specifique PSEN
465
466       if self.editor.code == 'VP' : return
467       if self.objSimp.valeur != None and self.objSimp.valeur != [] : return
468       if not hasattr(self.editor.readercata.cata,'sd_ligne') : self.editor.readercata.cata.sd_ligne=None
469       if not hasattr(self.editor.readercata.cata,'sd_generateur') : self.editor.readercata.cata.sd_generateur=None
470       if not hasattr(self.editor.readercata.cata,'sd_transfo') : self.editor.readercata.cata.sd_transfo=None
471       if not hasattr(self.editor.readercata.cata,'sd_charge') : self.editor.readercata.cata.sd_charge=None
472       if not hasattr(self.editor.readercata.cata,'sd_moteur') : self.editor.readercata.cata.sd_moteur=None
473       if self.objSimp.definition.validators.typeDesTuples[0]==self.editor.readercata.cata.sd_ligne :
474          val=[]
475          if  hasattr(self.objSimp.jdc,'LineDico'):
476           for k in self.objSimp.jdc.LineDico :
477               try :
478                valeur=self.objSimp.jdc.getConcept(k)
479                val.append((valeur,0))
480               except :
481                pass
482          self.node.item.setValeur(val)
483       if self.objSimp.definition.validators.typeDesTuples[0]==self.editor.readercata.cata.sd_generateur :
484          val=[]
485          if  hasattr(self.objSimp.jdc,'MachineDico'):
486           for k in self.objSimp.jdc.MachineDico :
487               try :
488                valeur=self.objSimp.jdc.getConcept(k)
489                val.append((valeur,0))
490               except :
491                pass
492          self.node.item.setValeur(val)
493       if self.objSimp.definition.validators.typeDesTuples[0]==self.editor.readercata.cata.sd_transfo :
494          val=[]
495          if  hasattr(self.objSimp.jdc,'TransfoDico'):
496           for k in self.objSimp.jdc.TransfoDico :
497               try :
498                valeur=self.objSimp.jdc.getConcept(k)
499                val.append((valeur,0))
500               except :
501                pass
502          self.node.item.setValeur(val)
503       if self.objSimp.definition.validators.typeDesTuples[0]==self.editor.readercata.cata.sd_charge :
504          val=[]
505          if  hasattr(self.objSimp.jdc,'LoadDico'):
506           for k in self.objSimp.jdc.LoadDico :
507               try :
508                valeur=self.objSimp.jdc.getConcept(k)
509                val.append((valeur,0))
510               except :
511                pass
512          self.node.item.setValeur(val)
513       if self.objSimp.definition.validators.typeDesTuples[0]==self.editor.readercata.cata.sd_moteur :
514          val=[]
515          if  hasattr(self.objSimp.jdc,'MotorDico'):
516           for k in self.objSimp.jdc.MotorDico :
517               try :
518                valeur=self.objSimp.jdc.getConcept(k)
519                val.append((valeur,0))
520               except :
521                pass
522          self.node.item.setValeur(val)
523