Salome HOME
bug
[tools/eficas.git] / InterfaceQT4 / gereListe.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
22 from __future__ import absolute_import
23 try :
24    from builtins import str
25    from builtins import range
26    from builtins import object
27 except : pass
28
29 import types,os
30 import traceback
31
32
33 import six
34 from six.moves import range
35 from PyQt5.QtWidgets import QLineEdit, QLabel, QFileDialog
36 from PyQt5.QtCore    import QEvent, Qt, QTimer
37 from PyQt5.QtGui     import QIcon, QPalette
38
39 from Extensions.i18n import tr
40 from InterfaceQT4.monViewTexte   import ViewText
41
42
43 # ---------------------- #
44 class LECustom(QLineEdit):
45 # ---------------------- #
46  def __init__(self,parent,parentQt,i):
47         """
48         Constructor
49         """
50         QLineEdit.__init__(self,parent)
51         
52         self.parentQt=parentQt
53         self.num=i
54         self.dansUnTuple=False
55         self.numDsLaListe=-1
56         self.parentTuple=None
57         self.valeur=None
58
59  def focusInEvent(self,event):
60      #print ("dans focusInEvent de LECustom")
61      self.parentQt.aEuLeFocus=True
62      self.aEuLeFocus=True
63      self.parentQt.LineEditEnCours=self
64      self.parentQt.numLineEditEnCours=self.num
65      self.parentQt.textSelected=self.text()
66      self.setStyleSheet("border: 2px solid gray")
67      QLineEdit.focusInEvent(self,event)
68
69  def focusOutEvent(self,event):
70      #print (self.aEuLeFocus)
71      self.setStyleSheet("border: 0px")
72      if self.dansUnTuple    : self.setStyleSheet("background:rgb(235,235,235); border: 0px;")
73      elif self.num % 2 == 1 : self.setStyleSheet("background:rgb(210,210,210)")
74      else                   : self.setStyleSheet("background:rgb(235,235,235)")
75      if self.aEuLeFocus:
76        self.aEuLeFocus=False
77        self.litValeur()
78        if self.dansUnTuple : self.parentTuple.getValeur()
79      QLineEdit.focusOutEvent(self,event)
80        
81     
82  def litValeur(self):
83      #print ("dans litValeur de LECustom")
84      self.aEuLeFocus=False
85      val=str(self.text())
86      if str(val)=="" or val==None :
87         self.valeur=None
88         return
89      try :
90        valeur=eval(val,{})
91      except :
92        try :
93           d=self.parentQt.parentQt.objSimp.jdc.getContexteAvant(self.parentQt.objSimp. etape)
94           valeur=eval(val,d)
95        except :
96           valeur=val
97      self.valeur=valeur
98      #print ('self.valeur', self.valeur)
99      
100      
101  def clean(self):
102      self.setText("")
103
104  def getValeur(self):
105      #return self.text()
106      return self.valeur
107
108  def setValeur(self,valeur):
109      self.setText(valeur)
110
111
112
113 # --------------------------- #
114 class LECustomTuple(LECustom):
115 # --------------------------- #
116  def __init__(self,parent):
117    #  index sera mis a jour par TupleCustom
118    parentQt=parent.parent().parent().parent()
119    LECustom. __init__(self,parent,parentQt,0)
120    #print (dir(self))
121
122 # ---------------------------- #
123 class MonLabelListeClic(QLabel):
124 # ---------------------------- #
125      def __init__(self,parent):
126         QLabel.__init__(self,parent)
127         self.parent=parent
128
129      def event(self,event) :
130          if event.type() == QEvent.MouseButtonRelease:
131             self.texte=self.text()
132             self.parent.traiteClicSurLabelListe(self.texte)
133          return QLabel.event(self,event)
134
135
136
137
138 # ------------- #
139 class GereListe(object):
140 # ------------- #
141
142    def __init__(self):
143        self.connecterSignaux()
144
145
146    def connecterSignaux(self):
147        if hasattr(self,'RBHaut'):
148           self.RBHaut.clicked.connect(self.hautPushed)
149           self.RBBas.clicked.connect(self.basPushed)
150           self.RBMoins.clicked.connect(self.moinsPushed)
151           self.RBPlus.clicked.connect(self.plusPushed)
152           self.RBVoisListe.clicked.connect(self.voisListePushed)
153        if hasattr(self,'PBAlpha'):
154           self.PBCata.clicked.connect(self.cataPushed)
155           self.PBAlpha.clicked.connect(self.alphaPushed)
156           self.PBFind.clicked.connect(self.findPushed)
157           self.LEFiltre.returnPressed.connect(self.LEFiltreReturnPressed)
158        if hasattr(self, 'PBValideFeuille'):
159           self.PBValideFeuille.clicked.connect(self.changeValeur)
160
161    def filtreListe(self):
162        l=[]
163        if self.filtre != "" :
164           for i in self.listeAAfficher :
165               if i.find(self.filtre) == 0 :l.append(i)
166           self.listeAAfficher=l
167        if self.alpha : self.listeAAfficher.sort()
168       
169    def LEFiltreReturnPressed(self):
170        self.filtre= self.LEFiltre.text()
171        self.prepareListeResultatFiltre()
172
173    def findPushed(self):
174        self.filtre= self.LEFiltre.text()
175        self.prepareListeResultatFiltre()
176
177    def alphaPushed(self):
178        #print "alphaPushed" ,self.alpha
179        if self.alpha == 1 : return
180        self.alpha=1
181        self.prepareListeResultat()
182
183    def cataPushed(self):
184        if self.alpha == 0 : return
185        self.alpha=0
186        self.prepareListeResultat()
187
188    def hautPushed(self):
189        if self.numLineEditEnCours == 1 : return
190        else : numEchange=self.numLineEditEnCours-1
191        self.echange(self.numLineEditEnCours,numEchange)
192        self.LineEditEnCours.setFocus(True)
193        self.scrollArea.ensureWidgetVisible(self.LineEditEnCours)
194
195
196    def basPushed(self):
197        if self.numLineEditEnCours == self.indexDernierLabel : return
198        else : numEchange=self.numLineEditEnCours+1
199        self.echange(self.numLineEditEnCours,numEchange)
200        self.LineEditEnCours.setFocus(True)
201        self.scrollArea.ensureWidgetVisible(self.LineEditEnCours)
202
203    def echange(self,num1,num2):
204        # on donne le focus au a celui ou on a bouge
205        # par convention le 2
206        nomLineEdit=self.nomLine+str(num1)
207        #print nomLineEdit
208        courant=getattr(self,nomLineEdit)
209        valeurAGarder=courant.text()
210        nomLineEdit2=self.nomLine+str(num2)
211        #print nomLineEdit2
212        courant2=getattr(self,nomLineEdit2)
213        courant.setText(courant2.text())
214        courant2.setText(valeurAGarder)
215        self.changeValeur(changeDePlace=False)
216        self.numLineEditEnCours=num2
217        self.LineEditEnCours=courant2
218        self.LineEditEnCours.setFocus(True)
219
220    def moinsPushed(self):
221        # on supprime le dernier
222        if self.numLineEditEnCours == 0 : return
223        if self.indexDernierLabel == 0 : return
224        if self.numLineEditEnCours==self.indexDernierLabel : 
225           nomLineEdit=self.nomLine+str(self.indexDernierLabel)
226           courant=getattr(self,nomLineEdit)
227           courant.clean()
228        else :
229          for i in range (self.numLineEditEnCours, self.indexDernierLabel):
230              aRemonter=i+1
231              nomLineEdit=self.nomLine+str(aRemonter)
232              courant=getattr(self,nomLineEdit)
233              valeurARemonter=courant.getValeur()
234              nomLineEdit=self.nomLine+str(i)
235              courant=getattr(self,nomLineEdit)
236              if valeurARemonter != None : courant.setValeur(valeurARemonter)
237              else : courant.clean()
238          nomLineEdit=self.nomLine+str(self.indexDernierLabel)
239          courant=getattr(self,nomLineEdit)
240          courant.clean()
241        self.changeValeur(changeDePlace=False,oblige=True)
242        self.setValide()
243
244    def plusPushed(self):
245        if self.indexDernierLabel == self.monSimpDef.max:
246           if len(self.listeValeursCourantes) < self.monSimpDef.max : self.chercheLigneVide()
247           else : self.editor.afficheInfos('nb max de valeurs : '+str(self.monSimpDef.max)+' atteint',Qt.red)
248           return
249        self.ajoutLineEdit()
250        self.descendLesLignes()
251        self.chercheLigneVide()
252        QTimer.singleShot(1, self.rendVisibleLigne)
253
254    def chercheLigneVide(self):
255        for i in range(self.indexDernierLabel) :
256           nomLineEdit=self.nomLine+str(i+1)
257           courant=getattr(self,nomLineEdit)
258           valeur=courant.getValeur()
259           if valeur=="" : 
260              courant.setFocus(True)
261              self.estVisible=courant
262              return
263
264
265    def descendLesLignes(self):
266        if self.numLineEditEnCours==self.indexDernierLabel : return
267        nomLineEdit=self.nomLine+str(self.numLineEditEnCours+1)
268        courant=getattr(self,nomLineEdit)
269        valeurADescendre=courant.getValeur()
270        courant.clean()
271        for i in range (self.numLineEditEnCours+1, self.indexDernierLabel):
272              aDescendre=i+1
273              nomLineEdit=self.nomLine+str(aDescendre)
274              courant=getattr(self,nomLineEdit)
275              valeurAGarder=courant.getValeur()
276              courant.setValeur(valeurADescendre)
277              valeurADescendre=valeurAGarder
278        self.changeValeur(changeDePlace=False)
279        if hasattr (self, 'LineEditEnCours') :self.scrollArea.ensureWidgetVisible(self.LineEditEnCours)
280
281    def voisListePushed(self):
282        texteValeurs=""
283        for v in self.node.item.getListeValeurs():
284           texteValeurs+=str(v)+", "
285        entete="Valeurs pour "+self.nom
286        f=ViewText(self,self.editor,entete,texteValeurs[0:-2])
287        f.show()
288
289
290    def selectInFile(self):
291        init=str( self.editor.maConfiguration.savedir)
292        fn = QFileDialog.getOpenFileName(self.node.appliEficas,
293                                          tr("Fichier de donnees"),
294                                          init,
295                                          tr('Tous les  Fichiers (*)',))
296        fn=fn[0]
297        if fn == None : return
298        if fn == "" : return
299        ulfile = os.path.abspath(six.text_type(fn))
300        self.editor.maConfiguration.savedir=os.path.split(ulfile)[0]
301
302        from .monSelectVal import MonSelectVal
303        MonSelectVal(file=fn,parent=self).show()
304
305    def noircirResultatFiltre(self):
306       filtre=str(self.LEFiltre.text())
307       for cb in self.listeCbRouge:
308           palette = QPalette(Qt.red)
309           palette.setColor(QPalette.WindowText,Qt.black)
310           cb.setPalette(palette)
311           t=cb.text()
312           cb.setText(t)
313       self.LEFiltre.setText("")
314       self.listeCbRouge = []
315
316   
317 # ----------- #
318 class GerePlie(object):
319 # ----------- #
320
321    def gereIconePlier(self):
322       if not(hasattr(self,'BFermeListe')) : return
323       self.editor.listeDesListesOuvertes.add(self.node.item)
324       repIcon=self.node.editor.appliEficas.repIcon
325       if not (self.editor.afficheListesPliees):
326          fichier=os.path.join(repIcon, 'empty.png')
327          icon = QIcon(fichier)
328          self.BFermeListe.setIcon(icon)
329          return
330       fichier=os.path.join(repIcon, 'minusnode.png')
331       icon = QIcon(fichier)
332       self.BFermeListe.setIcon(icon)
333       self.BFermeListe.clicked.connect( self.selectWidgetPlie)
334
335    def selectWidgetPlie(self):
336       self.editor.listeDesListesOuvertes.remove(self.node.item)
337       self.reaffichePourDeplier()
338
339         
340