Salome HOME
e8f1dc3cd9b341ba9f7bc81436078a28200fcb63
[tools/eficas.git] / InterfaceQT4 / feuille.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 except :
25     pass
26 import types,os
27 import traceback
28
29 from PyQt5.QtWidgets import QToolButton ,QWidget
30 from PyQt5.QtGui import QFont, QFontMetrics, QFontInfo, QPalette
31 from PyQt5.QtCore import Qt
32
33 from Extensions.i18n import tr
34
35 from .gereIcones import ContientIcones
36 from .gereIcones import FacultatifOuOptionnel
37 from .qtSaisie    import SaisieValeur
38
39 nomMax=230
40 # empirique les metrics ne fonctionnent pas
41 # ---------------------------------------------------------------------- #
42 class Feuille(QWidget,ContientIcones,SaisieValeur,FacultatifOuOptionnel):
43 # --------------------------------------------------------------------- #
44
45
46     def __init__(self,node,monSimpDef,nom,objSimp,parentQt,commande):
47         #print ("Feuille", monSimpDef,nom,objSimp)
48         QWidget.__init__(self,None)
49         self.node=node
50         self.node.fenetre=self
51
52         # on se positionne pour les icones
53         #os.chdir(os.path.abspath(os.path.join(os.path.dirname(__file__))))
54         self.setupUi(self)
55         self.prendLeFocus=0
56
57         maPolice= QFont("Times", 10)
58         self.setFont(maPolice)
59         self.setFocusPolicy(Qt.StrongFocus)
60
61         self.parentQt=parentQt
62         self.editor=self.node.editor
63         self.appliEficas=self.editor.appliEficas
64         self.repIcon=self.appliEficas.repIcon
65         self.monSimpDef=monSimpDef
66         self.nom=nom
67         self.objSimp=objSimp
68         self.node.fenetre=self
69         self.maCommande=commande
70
71         self.aRedimensionner=0
72         self.setSuggestion()
73         self.setValeurs()
74         self.setNom()
75         self.setValide()
76         self.setIconePoubelle()
77         self.setIconesFichier()
78         self.setIconesSalome()
79         self.setIconesGenerales()
80         self.setCommentaire()
81         self.setZoneInfo()
82         self.setUnite()
83         self.setUQ()
84
85
86     def setUnite(self):
87         if self.monSimpDef.unite == None  :
88             if hasattr( self,'lineEditUnite') : self.lineEditUnite.setText(' ')
89         else :
90             if hasattr( self,'lineEditUnite') : self.lineEditUnite.setText(self.monSimpDef.unite)
91             else : self.editor.informe('Erreur de Catalogue','Champ Unite non prevu pour '+ self.nom + ' correction du catalogue souhaitable, prevenez la maintenance', False)
92
93     def setUQ(self):
94         if not (self.editor.appliEficas.maConfiguration.afficheUQ) and hasattr(self,'checkBoxUQ') :
95              self.checkBoxUQ.close()
96              return
97         if not self.monSimpDef.UQPossible(): 
98              self.checkBoxUQ.close()
99              return
100         if self.objSimp.isUQActivate() and hasattr(self,'checkBoxUQ') :
101              self.checkBoxUQ.setChecked(True)
102
103     def setNom(self):
104         self.debutToolTip=""
105         nomTraduit=tr(self.objSimp.nom)
106         #metrix= QFontMetrics(self.label.font())
107         #maxLongueur = self.label.width() - 2
108         #print ('______________________')
109         #print (nomTraduit)
110         #print (self.label.font().pixelSize())
111         #longueur2 = metrix.boundingRect(nomTraduit).width()
112         longueur=QFontMetrics(self.label.font()).width(nomTraduit)
113         if longueur >= nomMax :
114             nouveauNom=self.formate(nomTraduit)
115             self.label.setText(nouveauNom)
116         else :
117             self.label.setText(nomTraduit)
118         #clidedText = metrics.elidedText(text, Qt.ElideRight, label.width());
119         #if (clippedText != nomTraduit): self.label.setToolTip(nomTraduit)
120         #self.label.setText(clippedText)
121
122     #def agrandit(self):
123         # inutile pour certains widgets
124     #    if self.height() < 40 :
125     #       self.setMinimumHeight(50)
126     #       self.resize(self.width(),200)
127
128     #def mousePressEvent(self, event):
129         #print 'mousePressEvent'
130         #import inspect
131         #print (inspect.getmro(self.__class__))
132         #self.__class__.mousePressEvent(self, event)
133
134
135
136     def setValeurs(self):
137        # print "passe dans setValeurs pour ", self.objSimp.nom
138        # print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
139         pass
140
141     def finCommentaire(self):
142         return ""
143
144
145     def finCommentaireListe(self):
146         commentaire=""
147         mc = self.node.item.get_definition()
148         d_aides = { 'TXM' : 'chaines de caracteres',
149                   'R'   : 'reels',
150                   'I'   : 'entiers',
151                   'C'   : 'complexes'}
152         type = mc.type[0]
153         if not type in d_aides :
154             if mc.min == mc.max:
155                 commentaire=tr("Entrez ")+str(mc.min)+tr(" valeurs ")+'\n'
156             else :
157                 if mc.max != "**" :
158                     commentaire=tr("entre ")+str(mc.min)+tr(" et ")+str(mc.max)+tr(" valeurs ")+'\n'
159                 else :
160                     commentaire=""
161         else :
162             if mc.min == mc.max:
163                 commentaire=tr("Entrez ")+str(mc.min)+" "+tr(d_aides[type])+'\n'
164             elif mc.max == float('inf') :
165                 commentaire=tr("Entrez une liste de ") +" "+tr(d_aides[type])+'\n'
166             else :
167                 commentaire=tr("Entrez entre ")+"\n"+str(mc.min)+(" et  ")+str(mc.max) +" " +tr(d_aides[type])+'\n'
168         aideval=self.node.item.aide()
169         commentaire=commentaire + tr(aideval)
170         return str(commentaire)
171
172
173     def setSuggestion(self):
174         if self.monSimpDef.getSug() != None and self.monSimpDef.getSug() != "":
175             suggere=str('<html><head/><body><p><span style=" font-size:8pt;">suggestion : ')+str(self.monSimpDef.getSug())+"</span></p></body></html>"
176             if hasattr(self,'lineEditVal'): self.lineEditVal.setToolTip(suggere)
177
178     def setCommentaire(self):
179         c  = self.debutToolTip
180         #if self.node.item.definition.validators : c+=self.node.item.definition.validators.aide()
181         self.aide=c
182         if self.objSimp.getFr() != None and self.objSimp.getFr() != "":
183             #c2 = '<html><head/><body><p>'+c+self.objSimp.getFr().decode('latin-1','replace')+"</p></body></html>"
184             c2 = '<html><head/><body><p>'+c+self.objSimp.getFr()
185             #c2 = '<html><head/><body><p>'+c+self.objSimp.getFr()+"</p></body></html>"
186             self.label.setToolTip(c2)
187             #self.aide=self.objSimp.getFr().decode('latin-1','ignore')+" "+c
188             self.aide=self.objSimp.getFr()+" "+c
189         else :
190             c+=self.finCommentaire()
191             if c != "" and c != None :
192                 self.aide=c
193                 #c=str('<html><head/><body><p><span style=" font-size:8pt; ">')+c+"</span></p></body></html>"
194                 c=str('<html><head/><body><p>')+c+"</p></body></html>"
195                 self.label.setToolTip(c)
196
197         if self.editor.maConfiguration.differencieSiDefaut :
198             self.label.setToolTip('defaut : ' + tr(str(self.node.item.object.definition.defaut)))
199
200
201
202     def showEvent(self, event):
203         if self.prendLeFocus==1 :
204             self.activateWindow()
205             "il faut deriver le showEvent pour" , self.nom
206             self.prendLeFocus=0
207         QWidget.showEvent(self,event)
208
209     def aideALaSaisie(self):
210         mc = self.node.item.get_definition()
211         mctype = mc.type[0]
212         d_aides = { 'TXM' : tr(u"chaine de caracteres"),
213                     'R'   : tr("reel"),
214                     'I'   : tr("entier"),
215                     'C'   : tr("complexe"),
216                     'Matrice' : tr(u'Matrice'),
217                     'Fichier' : tr(u'fichier'),
218                     'FichierNoAbs' : tr(u'fichier existant'),
219                     'Repertoire' : tr(u'repertoire')}
220
221         if mc.min == mc.max: commentaire=tr("Entrez ")+" "+str(mc.min)+" "
222         else :               commentaire=tr("Entrez entre ")+str(mc.min)+tr(" et ")+str(mc.max) + " "
223
224         try :
225             if issubclass(mctype,object) : ctype = getattr(mctype, 'help_message', tr("Type de base inconnu"))
226             else : ctype = d_aides.get(mctype, tr("Type de base inconnu"))
227         except:
228             ctype = d_aides.get(mctype, tr("Type de base inconnu"))
229         if ctype == tr("Type de base inconnu") and "Tuple" in str(mctype): ctype=str(mctype)
230         if ctype == tr("Type de base inconnu") and "bool" in str(mctype): ctype='bool'
231
232         if mc.max!=1 : 
233            if ctype == 'chaine de caractere' and mc.max>1 : ctype = 'chaines de caractere' 
234            else : ctype = ctype + 's'
235         commentaire+=ctype
236         if mc.max!=1 : commentaire+="s"
237         return commentaire
238
239     def setZoneInfo(self):
240         # info=str(self.nom)+'  '
241         # if self.monSimpDef.getFr() != None and self.monSimpDef.getFr() != "": info+=self.monSimpDef.getSug() +" "
242         # if self.monSimpDef.getSug() != None and self.monSimpDef.getSug() != "": info+="Valeur suggeree : "self.monSimpDef.getSug()
243         pass
244
245     def reaffiche(self):
246
247         if self.editor.jdc.aReafficher==True :
248             self.parentQt.reaffiche()
249
250             #PN PN PN pas satisfaisant
251             #nodeAVoir=self.parentQt.node.chercheNoeudCorrespondant(self.objSimp)
252             #print nodeAVoir.fenetre
253             #print "nodeAVoir.fenetre.isVisible()", nodeAVoir.fenetre.isVisible()
254             #if nodeAVoir.fenetre.isVisible() : return
255             #self.editor.fenetreCentraleAffichee.rendVisibleNoeud(nodeAVoir)
256             #nodeAVoir.fenetre.setFocus()
257             # return  # on est bien postionne
258
259             if self.objSimp.isValid() and hasattr(self, 'AAfficher'):
260                 nodeAVoir=self.parentQt.node.chercheNoeudCorrespondant(self.objSimp)
261                 try :
262                     index=self.editor.fenetreCentraleAffichee.listeAffichageWidget.index(nodeAVoir.fenetre.AAfficher)
263                     if (index==len(self.editor.fenetreCentraleAffichee.listeAffichageWidget)-1) :
264                         try :
265                             nodeAVoir.fenetre.setValeursApresBouton()
266                         except :
267                             pass
268                     else :
269                         self.editor.fenetreCentraleAffichee.afficheSuivant(nodeAVoir.fenetre.AAfficher)
270                 except :
271                     pass
272         else :
273             if self.objSimp.isValid() and hasattr(self, 'AAfficher'):
274                 try :
275                     self.setValeursApresBouton()
276                 except :
277                     pass
278                 self.editor.fenetreCentraleAffichee.afficheSuivant(self.AAfficher)
279             else :
280                 if hasattr(self, 'AAfficher'): self.AAfficher.setFocus(7)
281
282     def reaffichePourDeplier(self):
283         self.parentQt.reaffiche()
284
285     def rendVisible(self):
286         pass
287
288     #def enterEvent(self,event):
289     #   print "je passe dans enterEvent", self.nom
290     #   QWidget.enterEvent(self,event)
291
292     def traiteClicSurLabel(self,texte):
293         #aide=self.aide.encode('latin-1', 'ignore').decode('latin-1')+"\n"+self.aideALaSaisie().encode('latin-1', 'ignore').decode('latin-1')
294         try :
295             aide=self.aide+"\n"+self.aideALaSaisie()
296         except :
297             aide=self.aideALaSaisie()
298         self.editor.afficheCommentaire(aide)
299
300     def formate(self,t):
301         l=len(t)//2
302         newText=t[0:l]+'-\n'+t[l:]
303         return newText