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