Salome HOME
Add COPYING license file
[tools/eficas.git] / InterfaceQT4 / monWidgetPlusieursIntoOrdonne.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 # Modules Python
21 from __future__ import absolute_import
22 try :
23     from builtins import str
24     from builtins import range
25 except : pass
26
27 import types,os,sys
28
29 # Modules Eficas
30 from .feuille                import Feuille
31 from desWidgetPlusieursIntoOrdonne import Ui_WidgetPlusieursIntoOrdonne
32 from .politiquesValidation   import PolitiquePlusieurs
33 from .qtSaisie               import SaisieValeur
34 from .gereListe              import GereListe
35 from .gereListe              import GerePlie
36 from .gereListe              import LECustom
37 from .gereListe              import MonLabelListeClic
38 from Extensions.i18n import tr
39
40 from PyQt5.QtWidgets  import   QFrame, QApplication, QScrollBar
41 from PyQt5.QtCore import QTimer, QSize, Qt
42 from PyQt5.QtGui  import QIcon, QPalette
43
44
45 class MonWidgetPlusieursIntoOrdonne (Ui_WidgetPlusieursIntoOrdonne, Feuille,GereListe,GerePlie):
46
47     def __init__(self,node,monSimpDef,nom,objSimp,parent,commande):
48         self.nomLine="LEResultat"
49         self.listeLE=[]
50         self.ouAjouter=0
51         self.numLineEditEnCours=0
52         self.alpha=0
53         self.filtre=""
54         Feuille.__init__(self,node,monSimpDef,nom,objSimp,parent,commande)
55         GereListe.__init__(self)
56         #self.finCommentaireListe()
57         self.gereIconePlier()
58         self.listeValeursCourantes=self.node.item.getListeValeurs()
59         try :
60             self.maCommande.listeAffichageWidget.append(self.lineEditVal1)
61         except :
62             # cas ou on ne peut rien ajouter
63             pass
64         self.prepareListeResultat()
65         if len(self.listeAAfficher) < 20 : self.frameRecherche2.close()
66         if len(self.listeAAfficher) < 20 : self.frameRecherche.close()
67         self.adjustSize()
68         #if sys.platform[0:5]!="linux":
69         repIcon=self.node.editor.appliEficas.repIcon
70         fichier=os.path.join(repIcon, 'arrow_up.png')
71         icon = QIcon(fichier)
72         self.RBHaut.setIcon(icon)
73         self.RBHaut.setIconSize(QSize(32, 32))
74         fichier2=os.path.join(repIcon, 'arrow_down.png')
75         icon2 = QIcon(fichier2)
76         self.RBBas.setIcon(icon2)
77         icon=QIcon(self.repIcon+"/MoinsBleu.png")
78         self.RBMoins.setIcon(icon)
79         icon=QIcon(self.repIcon+"/PlusBleu.png")
80         self.RBPlus.setIcon(icon)
81         icon=QIcon(self.repIcon+"/verre-loupe-icone-6087-64.png")
82         self.RBVoisListe.setIcon(icon)
83
84         self.PBClean.clicked.connect(self.cleanListeResultatFiltre)
85         self.parentQt.commandesLayout.insertWidget(-1,self)
86         self.listeRouge=[]
87
88
89     def prepareListeResultat(self):
90         for i in self.listeLE: i.close()
91         self.listeLE=[]
92         self.vScrollBar = self.scrollArea.verticalScrollBar()
93         self.listeValeursCourantes=self.node.item.getListeValeurs()
94         if hasattr(self.node.item.definition.validators,'set_MCSimp'):
95             obj=self.node.item.getObject()
96             self.node.item.definition.validators.set_MCSimp(obj)
97             if self.node.item.isValid() == 0 :
98                 liste=[]
99                 for item in self.listeValeursCourantes:
100                     if self.node.item.definition.validators.verifItem(item)==1: liste.append(item)
101                 self.listeAAfficher=self.node.item.getListePossible(liste)
102             else:
103                 self.listeAAfficher=self.node.item.getListePossible([])
104         else :
105             self.listeAAfficher=self.node.item.getListePossible(self.listeValeursCourantes)
106
107         if self.listeAAfficher==[] :
108             self.ajoutLE(0)
109             return
110         self.filtreListe()
111         if len(self.listeAAfficher)*20 > 400 : self.setMinimumHeight(400)
112         else :
113             if self.monSimpDef.min > len(self.listeAAfficher)    : self.setMinimumHeight(self.monSimpDef.min*30+300)
114             elif  self.monSimpDef.max > len(self.listeAAfficher) : self.setMinimumHeight(400)
115             else  : self.setMinimumHeight(len(self.listeAAfficher)*30+30)
116         self.setMinimumHeight(300)
117         self.adjustSize()
118
119         self.politique=PolitiquePlusieurs(self.node,self.editor)
120         for i in range(1,len(self.listeAAfficher)+1): self.ajoutLE(i)
121         for i in range(len(self.listeAAfficher)):
122             nomLE="lineEditVal"+str(i+1)
123             courant=getattr(self,nomLE)
124             courant.setText(str(self.listeAAfficher[i]))
125         self.vScrollBar.triggerAction(QScrollBar.SliderToMinimum)
126         if len(self.listeAAfficher) < 15 and hasattr(self,'frameRecherche') : self.frameRecherche.close()
127         if len(self.listeAAfficher) < 15 and hasattr(self,'frameRecherche2') : self.frameRecherche2.close()
128
129
130     def setValeurs(self,first=True):
131         self.listeValeursCourantes=self.node.item.getListeValeurs()
132         if first :
133             if self.monSimpDef.max == "**" or self.monSimpDef.max == float('inf') : aConstruire=7
134             else : aConstruire=self.monSimpDef.max
135             if len(self.listeValeursCourantes) > aConstruire : aConstruire=len(self.listeValeursCourantes)
136             self.indexDernierLabel = aConstruire
137             for i in range(1,aConstruire+1): self.ajoutLEResultat(i)
138         index=1
139         for val in self.listeValeursCourantes :
140             nomLE="LEResultat"+str(index)
141             courant=getattr(self,nomLE)
142             courant.setText(str(val))
143             courant.setReadOnly(True)
144             index=index+1
145         while (index < self.indexDernierLabel) :
146             nomLE="LEResultat"+str(index)
147             courant=getattr(self,nomLE)
148             courant.setText("")
149             courant.setReadOnly(True)
150             index=index+1
151         #self.prepareListeResultat()
152
153     def moinsPushed(self):
154         self.ouAjouter=self.ouAjouter-1
155         GereListe.moinsPushed(self)
156         self.setValeurs(first=False)
157
158     def prepareListeResultatFiltre(self):
159         for i in self.listeRouge :
160             nomLE="lineEditVal"+str(i+1)
161             courant=getattr(self,nomLE)
162             texte=courant.text()
163             palette = QPalette(Qt.black)
164             palette.setColor(QPalette.WindowText,Qt.black)
165             courant.setPalette(palette)
166             courant.setText(texte)
167
168         self.listeRouge = []
169         filtre=str(self.LEFiltre.text())
170         if filtre == '' : return
171         for i in range(len(self.listeAAfficher)):
172             nomLE="lineEditVal"+str(i+1)
173             courant=getattr(self,nomLE)
174             texte=courant.text()
175             if texte.find(filtre) == 0 :
176                 palette = QPalette(Qt.red)
177                 palette.setColor(QPalette.WindowText,Qt.red)
178                 courant.setPalette(palette)
179                 courant.setText(texte)
180             self.listeRouge.append(i)
181
182     def cleanListeResultatFiltre(self):
183         self.LEFiltre.setText('')
184         self.prepareListeResultatFiltre()
185
186     def ajoutLEResultat (self,index,valeur=None):
187         #print ('ajoutLEResultat', index, valeur)
188         nomLE="LEResultat"+str(index)
189         if not (hasattr(self,nomLE)) :
190             nouveauLE = LECustom(self.scrollAreaRE,self,index)
191             nouveauLE.setFrame(False)
192             self.CBChoisis.insertWidget(self.ouAjouter,nouveauLE)
193             self.ouAjouter=self.ouAjouter+1
194             nouveauLE.setReadOnly(True)
195             if index % 2 == 1 : nouveauLE.setStyleSheet("background:rgb(210,210,210)")
196             else :            nouveauLE.setStyleSheet("background:rgb(240,240,240)")
197             self.vScrollBarRE = self.scrollAreaRE.verticalScrollBar()
198             self.vScrollBarRE.triggerAction(QScrollBar.SliderToMaximum)
199             setattr(self,nomLE,nouveauLE)
200             self.estVisibleRE=nouveauLE
201         else :
202             nouveauLE=getattr(self,nomLE)
203
204         if valeur == None : nouveauLE.setText("")
205         else : nouveauLE.setText(str(valeur))
206
207     def ajoutLE(self,index,valeur=None):
208         #print ('ajoutLE')
209         nomLE="lineEditVal"+str(index)
210         nouveauLE = MonLabelListeClic(self)
211         #self.CBLayout.addWidget(nouveauLE)
212         self.CBLayout.insertWidget(index -1,nouveauLE)
213         self.listeLE.append(nouveauLE)
214         nouveauLE.setFrameShape(QFrame.NoFrame)
215         QApplication.processEvents()
216         nouveauLE.setText("")
217         if index % 2 == 1 : nouveauLE.setStyleSheet("background:rgb(210,210,210)")
218         else :              nouveauLE.setStyleSheet("background:rgb(240,240,240)")
219         self.vScrollBar.triggerAction(QScrollBar.SliderToMaximum)
220         nouveauLE.setFocus()
221         setattr(self,nomLE,nouveauLE)
222
223     def ajoutLineEdit(self):
224         #print ('ajoutLineEdit')
225         self.indexDernierLabel=self.indexDernierLabel+1
226         self.ajoutLEResultat (self.indexDernierLabel)
227
228
229     def traiteClicSurLabelListe(self,valeur):
230         if valeur == None : return
231         liste,validite=SaisieValeur.TraiteLEValeur(self,str(valeur))
232         if validite == 0 : return
233         if liste ==[]    : return
234         listeVal=[]
235
236         self.listeValeursCourantes=self.node.item.getListeValeurs()
237         min,max = self.node.item.getMinMax()
238         if len(self.listeValeursCourantes) +1 > max :
239             self.editor.afficheInfos(tr("Nombre maximal de valeurs : ") + str(max),Qt.red)
240             return
241         else :
242             self.editor.afficheInfos("")
243
244         affiche=False
245         for i in range(1,self.indexDernierLabel+1):
246             nomLE="LEResultat"+str(i)
247             courant=getattr(self,nomLE)
248             if str(courant.text())==str("") :
249                 courant.setText(valeur)
250                 courant.setReadOnly(True)
251                 affiche=True
252                 self.estVisibleRE=courant
253                 QTimer.singleShot(1, self.rendVisibleLigneRE)
254                 break
255
256         if affiche == False:
257             self.indexDernierLabel = self.indexDernierLabel+1
258             self.ajoutLEResultat (self.indexDernierLabel,str(valeur))
259             self.vScrollBarRE.triggerAction(QScrollBar.SliderToMaximum)
260             QTimer.singleShot(1, self.rendVisibleLigneRE)
261         self.changeValeur()
262         self.setValeurs(first=False)
263
264     def changeValeur(self,changeDePlace=False,oblige=False):
265 #def changeValeur(self,changeDePlace=False,oblige=False, numero=None):
266 #PN les 2 arg sont pour que la signature de ma fonction soit identique a monWidgetPlusieursBase
267         listeVal=[]
268         for i in range(1,self.indexDernierLabel+1):
269             nomLE="LEResultat"+str(i)
270             courant=getattr(self,nomLE)
271             valeur=courant.text()
272             if str(valeur)=="" : continue
273             liste,validite=SaisieValeur.TraiteLEValeur(self,str(valeur))
274             listeVal.append(str(valeur))
275
276         validite,comm,comm2,listeRetour=self.politique.ajoutValeurs(listeVal,-1,[])
277
278
279         self.listeValeursCourantes=self.node.item.getListeValeurs()
280         min,max = self.node.item.getMinMax()
281         if len(self.listeValeursCourantes) < min :
282             self.editor.afficheInfos(tr("Nombre minimal de valeurs : ") + str(min),Qt.red)
283         else :
284             self.editor.afficheInfos("")
285
286         if len(listeRetour) == 0 :
287             self.node.item.setValeur(None)
288         elif validite :
289             self.node.item.setValeur(listeRetour)
290         else :
291             commentaire=comm+" "+comm2
292             self.editor.afficheInfos(commentaire,Qt.red)
293         self.setValide()
294         self.reaffiche()
295       
296 #
297     def rendVisibleLigneRE(self):
298         QApplication.processEvents()
299         self.estVisibleRE.setFocus()
300         self.scrollArea.ensureWidgetVisible(self.estVisibleRE,0,0)
301 #
302     def rendVisibleLigne(self):
303         self.estVisibleRE=self.estVisible
304         #rendVisibleLigneRE()