Salome HOME
Merge remote branch 'origin/nouvelEficas' into nouvelEficas
[tools/eficas.git] / InterfaceQT4 / browser.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   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
21 import string,re
22 import types,sys,os
23 import traceback
24 import typeNode
25 from PyQt4 import *
26 from PyQt4.QtGui  import *
27 from PyQt4.QtCore import *
28 from Extensions.i18n import tr
29 from monChoixCommande import MonChoixCommande
30
31 class JDCTree( QTreeWidget ):
32     def __init__( self, jdc_item, QWParent):        
33         #if hasattr(QWParent,'widgetTree') : 
34         if QWParent.widgetTree !=None  :
35            QTreeWidget.__init__(self, QWParent.widgetTree ) 
36            QWParent.verticalLayout_2.addWidget(self)
37            self.headerItem().setText(0,  "Commande   ")
38            self.headerItem().setText(1, "Concept/Valeur")
39            self.setColumnWidth(0,200)
40            self.setExpandsOnDoubleClick(False)
41            self.setSelectionMode(3)
42         else :
43            QTreeWidget.__init__(self, None ) 
44         self.item          = jdc_item
45         self.tree          = self        
46         self.editor        = QWParent
47         self.appliEficas   = self.editor.appliEficas
48         self.childrenComplete=[]
49         self.childrenIssusDesBlocs=[]
50         self.racine=self.item.itemNode(self,self.item)
51  
52         self.itemCourrant=None
53
54         self.connect(self, SIGNAL("itemClicked ( QTreeWidgetItem * ,int) "), self.handleOnItem)
55         #self.connect(self, SIGNAL("itemDoubleClicked ( QTreeWidgetItem * ,int) "), self.handleDoubleClickedOnItem)
56
57         #PNPNPN verifier dans quel cas on se trouve : affiche l arbre ou la commande
58         self.node_selected=self.racine
59         self.expandItem(self.racine)
60         if self.racine.children !=[] :  
61            self.racine.children[0].affichePanneau()
62            if self.editor.afficheCommandesPliees : self.racine.children[0].plieToutEtReaffiche()
63         else : self.racine.affichePanneau()
64
65
66     def contextMenuEvent(self,event) :
67         print "contextMenuEvent"
68         coord=event.globalPos()
69         item= self.currentItem()
70         self.handleContextMenu(item,coord)
71
72     def handleContextMenu(self,item,coord):
73         """
74         Private slot to show the context menu of the listview.
75         
76         @param itm the selected listview item (QListWidgetItem)
77         @param coord the position of the mouse pointer (QPoint)
78         Attention : existeMenu permet de savoir si un menu est associe a cet item
79         """
80         print "handleContextMenu"
81         if item == None : return
82         if item.existeMenu == 0 : return
83         if item.menu == None:
84            item.createPopUpMenu()
85         if item.menu != None:
86            if item.item.get_nom() == "DISTRIBUTION" and item.item.isvalid() :
87               item.Graphe.setEnabled(1)
88            item.menu.exec_(coord)            
89             
90
91     def handleOnItem(self,item,int):
92         if (len(self.selectedIndexes())!=2): return
93         self.itemCourrant=item
94         self.handleDoubleClickedOnItem(item,int)
95         #try :
96         if 1:
97            fr = item.item.get_fr()
98            if self.editor: self.editor.labelCommentaire.setText(unicode(fr))
99         #except:
100         else :
101             pass
102
103     def handleDoubleClickedOnItem(self,item,int):
104         itemParent=item
105         while not (hasattr (itemParent,'getPanel2')) : itemParent=item.treeParent 
106         itemParent.affichePanneau()
107         if itemParent!=item:
108            item.fenetre.rendVisible()
109
110     def choisitPremier(self,name):
111         self.editor.layoutJDCCHOIX.removeWidget(self.racine.fenetre)
112         self.racine.fenetre.close()
113         new_node=self.racine.append_brother(name,'after')
114  
115 # type de noeud
116 COMMENT     = "COMMENTAIRE"
117 PARAMETERS  = "PARAMETRE"
118  
119 class JDCNode(QTreeWidgetItem):
120     def __init__( self, treeParent, item):
121         #print "creation d'un noeud : ", item, " ",item.nom,"", treeParent
122         self.a=0
123         self.item        = item
124         self.vraiParent  = treeParent
125         self.treeParent  = treeParent
126         self.tree        = self.treeParent.tree
127         self.editor      = self.treeParent.editor
128         self.appliEficas = treeParent.appliEficas
129         self.treeParent.childrenIssusDesBlocs=[]
130         self.childrenComplete=[]
131                         
132         from InterfaceQT4 import compocomm
133         from InterfaceQT4 import compoparam
134         if   (isinstance(self.item,compocomm.COMMTreeItem)) : name=tr("Commentaire")
135         elif (isinstance(self.item,compoparam.PARAMTreeItem)) : name=self.appliEficas.trUtf8(str(item.GetLabelText()[0]))
136         else:   name  = self.appliEficas.trUtf8(str(tr( item.nom))+" :")
137         value = self.appliEficas.trUtf8(str( item.GetText() ) )
138
139         mesColonnes=QStringList()
140         mesColonnes <<  name << value
141
142         ajoutAuParentduNoeud=0
143         from InterfaceQT4 import compobloc
144         while (isinstance(self.treeParent,compobloc.Node)) :
145               self.treeParent=self.treeParent.treeParent
146               ajoutAuParentduNoeud=1
147         if ajoutAuParentduNoeud :
148            treeParent.childrenComplete.append(self)
149            self.treeParent.childrenIssusDesBlocs.append(self)
150         while (isinstance(self.treeParent,compobloc.Node)) : self.treeParent=self.treeParent.treeParent
151
152         if isinstance(self,compobloc.Node) : 
153            QTreeWidgetItem.__init__(self,None,mesColonnes)
154         else :
155            QTreeWidgetItem.__init__(self,self.treeParent,mesColonnes)
156            self.treeParent.childrenComplete.append(self)
157
158         self.setToolTip(0,QString(self.item.get_fr()))
159         self.setToolTip(1,QString(self.item.get_fr()))
160
161         repIcon=QString(self.appliEficas.repIcon)
162         monIcone = QIcon(repIcon+"/" +self.item.GetIconName() + ".png")
163         self.setIcon(0,monIcone)
164
165         self.children = []
166         self.build_children()
167         self.menu=None
168         self.existeMenu=1
169
170         self.item.connect("valid",self.onValid,())
171         self.item.connect("supp" ,self.onSupp,())
172         self.item.connect("add"  ,self.onAdd,())
173         self.state=""
174         self.fenetre=None
175         try :
176           if self.item.getObject().isBLOC() : self.setExpanded(True) 
177         except :
178           pass
179
180
181     def build_children(self,posInsertion=10000):
182         """ Construit la liste des enfants de self """
183         """ Se charge de remettre les noeuds Expanded dans le meme etat """
184         #print "*********** build_children ",self.item, self.item.GetLabelText()
185         
186         listeExpanded=[]
187         for item in self.childrenComplete :
188             #try :
189             #  print "              je detruis ",  item.item.GetLabelText() ," parent : ", item.treeParent.item.GetLabelText()
190             #except :
191             #  print "mot clef fact"
192             if item.isExpanded():
193                if self.childrenComplete.index(item) < posInsertion :
194                   listeExpanded.append(self.childrenComplete.index(item))
195                else :
196                   listeExpanded.append( self.childrenComplete.index(item) +1)
197             self.detruit_les_noeuds_issus_de_blocs(item)
198             parent=item.treeParent
199             parent.removeChild(item)
200
201         self.children = []
202         self.childrenComplete = []
203         sublist = self.item._GetSubList()
204         ind=0
205         for item in sublist :
206             nouvelItem=item.itemNode(self,item)
207             self.children.append(nouvelItem)
208             #print "         J ajoute ", nouvelItem ,nouvelItem.item.GetLabelText(),"dans" ,self.item.GetLabelText()
209             if ind in listeExpanded : nouvelItem.setExpanded(1)
210             ind=ind+1
211         #print "*********** fin build_children ",self.item, self.item.GetLabelText()
212         
213     def chercheNoeudCorrespondant(self,objSimp):
214         sublist = self.item._GetSubList()
215         for node in self.childrenComplete:
216             if node.item.object==objSimp : return node
217         return None
218
219     def affichePanneau(self) :
220         #print "______________________________"
221         #print "dans affichePanneau appel getPanel2", self.item.GetLabelText()
222
223         if self.item.isactif(): self.fenetre=self.getPanel2()
224         else:
225             from monInactifPanel import PanelInactif
226             self.fenetre = PanelInactif(self,self.editor)
227         self.editor.widgetCentraleLayout.addWidget(self.fenetre)
228
229         self.editor.anciennefenetre=self.editor.fenetreCentraleAffichee
230         self.editor.fenetreCentraleAffichee=self.fenetre
231
232         if self.editor.anciennefenetre != None : 
233            self.editor.anciennefenetre.close()
234
235         if self.editor.widgetTree !=None  : index=1
236         else : index=0
237
238         if self.editor.first :
239            self.editor.splitter.setSizes((400,1400,400))
240            if not(isinstance(self.fenetre,MonChoixCommande)): self.editor.first=False
241         self.tree.expandItem(self)
242         self.select()
243         #print "fin de affichePanneau"
244         #print "______________________________"
245           
246
247     def createPopUpMenu(self):
248         #implemente dans les noeuds derives si necessaire
249         self.existeMenu = 0
250
251     def commentIt(self):
252         """
253         Cette methode a pour but de commentariser la commande pointee par self
254         """
255         # On traite par une exception le cas ou l'utilisateur final cherche a désactiver
256         # (commentariser) un commentaire.
257         try :
258             pos=self.treeParent.children.index(self)
259             commande_comment = self.item.get_objet_commentarise()
260             # On signale a l editeur du panel (le JDCDisplay) une modification
261             self.editor.init_modif()
262             self.treeParent.build_children()
263             self.treeParent.children[pos].select()
264             self.treeParent.children[pos].affichePanneau()
265         except Exception,e:
266             traceback.print_exc()
267             QMessageBox.critical( self.editor, "TOO BAD",str(e))
268         
269     def unCommentIt(self):
270         """
271         Realise la decommentarisation de self
272         """
273         try :
274             pos=self.treeParent.children.index(self)
275             commande,nom = self.item.uncomment()
276             self.editor.init_modif()
277             self.treeParent.build_children()
278             self.treeParent.children[pos].select()
279             self.treeParent.children[pos].affichePanneau()
280         except Exception,e:
281             QMessageBox.critical( self.editor, "Erreur !",str(e))
282         
283     def addComment( self, after=True ):
284         """
285         Ajoute un commentaire a l'interieur du JDC :
286         """
287         self.editor.init_modif()
288         if after:
289             pos = 'after'
290         else:
291             pos = 'before'
292         return self.append_brother( COMMENT, pos )
293                 
294     def addParameters( self, after=True ):
295         """
296         Ajoute un parametre a l'interieur du JDC :
297         """
298         self.editor.init_modif()
299         if after:
300             pos = 'after'
301         else:
302             pos = 'before'
303         return self.append_brother( PARAMETERS, pos )
304     
305     
306     def select( self ):
307         """
308         Rend le noeud courant (self) selectionne et deselectionne
309         tous les autres
310         """        
311         for item in self.tree.selectedItems() :
312             item.setSelected(0)
313         self.setSelected( True )    
314         self.setExpanded( True )    
315         self.tree.setCurrentItem( self )    
316         self.tree.node_selected= self
317                                
318     #------------------------------------------------------------------
319     # Methodes de creation et destruction de noeuds
320     # Certaines de ces methodes peuvent etre appelees depuis l'externe
321     #------------------------------------------------------------------
322     def append_brother(self,name,pos='after'):
323         """
324         Permet d'ajouter un objet frere a l'objet associe au noeud self
325         par defaut on l'ajoute immediatement apres 
326         Methode externe
327         """
328         #print "*********** append_brother ", self.item.GetLabelText()
329         self.editor.init_modif()
330         index = self.treeParent.children.index(self)
331         if pos == 'before':
332             index = index
333         elif pos == 'after':
334             index = index +1
335         else:
336             print unicode(pos), tr("  n'est pas un index valide pour append_brother")
337             return 0
338         return self.treeParent.append_child(name,pos=index)
339
340     def append_child(self,name,pos=None):
341         """
342            Methode pour ajouter un objet fils a l'objet associe au noeud self.
343            On peut l'ajouter en debut de liste (pos='first'), en fin (pos='last')
344            ou en position intermediaire.
345            Si pos vaut None, on le place a la position du catalogue.
346         """
347         print "************** append_child ",self.item.GetLabelText()
348         self.editor.init_modif()
349         if pos == 'first':
350             index = 0
351         elif pos == 'last':
352             index = len(self.children)
353         elif type(pos) == types.IntType :
354             # position fixee
355             index = pos
356         elif type(pos) == types.InstanceType:
357             # pos est un item. Il faut inserer name apres pos
358             index = self.item.get_index(pos) +1
359         elif type(name) == types.InstanceType:
360             index = self.item.get_index_child(name.nom)
361         else:
362             index = self.item.get_index_child(name)
363         obj=self.item.additem(name,index) #CS_pbruno emet le signal 'add'
364         if obj is None:obj=0
365         if obj == 0:return 0
366         try :
367           old_obj = self.item.object.get_child(name.nom,restreint = 'oui')
368           child=old_obj[-1]
369           child.affichePanneau() 
370           print "dans le 1er Try"
371         #else :
372         except:
373           # Souci pour gerer les copies des AFFE d'une commande à l autre
374           try :
375              child=self.children[index]
376              child.affichePanneau() 
377              print "dans le 2nd Try"
378           except :
379              child=self.children[index]
380              print "dans le except"
381         return child
382
383     def deplace(self):
384         self.editor.init_modif()
385         index = self.treeParent.children.index(self) - 1 
386         if index < 0 : index =0
387         ret=self.treeParent.item.deplaceEntite(self.item.getObject())
388
389     def delete(self):
390         """ 
391             Methode externe pour la destruction de l'objet associe au noeud
392         """
393         self.editor.init_modif()
394         index = self.vraiParent.children.index(self) - 1 
395         if index < 0 : index =0
396         recalcule=0
397         if self.item.nom == "VARIABLE" :
398            recalcule=1
399            jdc=self.item.jdc
400         ret=self.vraiParent.item.suppitem(self.item)
401         self.treeParent.build_children()
402         if self.treeParent.childrenComplete : toselect=self.treeParent.childrenComplete[index]
403         else: toselect=self.treeParent
404         if recalcule : jdc.recalcule_etat_correlation()
405         from InterfaceQT4 import compojdc
406         # cas ou on detruit dans l arbre sans affichage
407         if isinstance(self.treeParent,compojdc.Node) : 
408            toselect.affichePanneau()
409         else :
410            if self.treeParent.fenetre== None : return
411            print "J appelle reaffiche de browser apres delete"
412            self.treeParent.fenetre.reaffiche(toselect)
413
414     def deleteMultiple(self,liste=()):
415         """ 
416             Methode externe pour la destruction d une liste de noeud
417         """
418         from InterfaceQT4 import compojdc 
419         self.editor.init_modif()
420         index=9999
421         recalcule=0
422         jdc=self.treeParent
423         parentPosition=jdc
424         while not(isinstance(jdc,compojdc.Node)):
425               jdc=jdc.treeParent
426         for noeud in liste :
427             if not( isinstance(noeud.treeParent, compojdc.Node)): continue
428             if noeud.item.nom == "VARIABLE" : recalcule=1
429             if noeud.treeParent.children.index(noeud) < index : index=noeud.treeParent.children.index(noeud)
430         if index < 0 : index =0
431
432         # Cas ou on détruit dans une ETape
433         if index == 9999 : 
434               parentPosition=self.treeParent
435               while not(isinstance(parentPosition, compojdc.Node)):
436                  index=parentPosition.treeParent.children.index(parentPosition)
437                  parentPosition=parentPosition.treeParent
438
439         for noeud in liste:
440             noeud.treeParent.item.suppitem(noeud.item)
441
442         jdc.build_children()
443         if recalcule : jdc.recalcule_etat_correlation()
444         try    : toselect=parentPosition.children[index]
445         except : toselect=jdc
446         toselect.select()
447         toselect.affichePanneau()
448 #        
449 #    #------------------------------------------------------------------
450     def onValid(self):        
451
452         #print "onValid pour ", self.item.nom
453         if hasattr(self,'fenetre') and self.fenetre: 
454            self.fenetre.setValide()
455         if self.item.nom == "VARIABLE" and self.item.isvalid():
456            self.item.jdc.recalcule_etat_correlation()
457         if hasattr(self.item,'forceRecalcul'):
458            self.forceRecalculChildren(self.item.forceRecalcul)
459         self.editor.init_modif()
460         
461         self.update_node_valid()
462         self.update_node_label()
463         self.update_node_texte()
464
465     def onAdd(self,object):
466         #print "onAdd pour ", self.item.nom
467         self.editor.init_modif()
468         self.update_nodes()
469         print "dans onAdd" ,self.item 
470         # PN -- non necessaire si item=jdc
471         if hasattr(self.item,'jdc'): self.item.jdc.aReafficher=True
472  
473     def onSupp(self,object):
474         #print "onSupp pour ", self.item.nom
475         self.editor.init_modif()
476         self.update_nodes()
477         # PN -- non necessaire si item=jdc
478         if hasattr(self.item,'jdc'): self.item.jdc.aReafficher=True
479          
480     def detruit_les_noeuds_issus_de_blocs(self,bloc):
481         from InterfaceQT4 import compobloc
482         if (isinstance(bloc,compobloc.Node)) :
483            for node in bloc.childrenComplete :
484                self.detruit_les_noeuds_issus_de_blocs(node)
485                parent=node.treeParent
486                #print "je detruit " , node.item.GetLabelText()
487                parent.removeChild(node)
488
489     def update_node_valid(self):
490         """Cette methode remet a jour la validite du noeud (icone)
491            Elle appelle isvalid
492         """
493         repIcon=QString(self.appliEficas.repIcon)
494         monIcone = QIcon(repIcon+"/" +self.item.GetIconName() + ".png")
495         self.setIcon(0,monIcone)
496
497
498     def update_node_label(self):
499         """ Met a jour le label du noeud """
500         #print "NODE update_node_label", self.item.GetLabelText()
501         labeltext,fonte,couleur = self.item.GetLabelText()
502         # PNPN a reflechir
503         #self.setText(0, labeltext)        
504     
505     
506     def update_node_label_in_blue(self):
507         if hasattr(self.appliEficas,'noeudColore'):
508            self.appliEficas.noeudColore.setTextColor( 0,Qt.black)
509            self.appliEficas.noeudColore.update_node_label()
510         self.setTextColor( 0,Qt.blue )
511         labeltext,fonte,couleur = self.item.GetLabelText()
512         self.setText(0, labeltext)        
513         self.appliEficas.noeudColore=self
514
515     def update_plusieurs_node_label_in_blue(self,liste):
516         if hasattr(self.appliEficas,'listeNoeudsColores'):
517            for noeud in self.appliEficas.listeNoeudsColores:
518                noeud.setTextColor( 0,Qt.black)
519                noeud.update_node_label()
520         self.appliEficas.listeNoeudsColores=[]
521         for noeud in liste :
522             noeud.setTextColor( 0,Qt.blue )
523             labeltext,fonte,couleur = noeud.item.GetLabelText()
524             noeud.setText(0, labeltext)        
525             self.appliEficas.listeNoeudsColores.append(noeud)
526
527     def update_node_texte_in_black(self):
528         """ Met a jour les noms des SD et valeurs des mots-cles """
529         self.setTextColor( 1,Qt.black )
530         value = self.item.GetText()
531         self.setText(1, value)
532
533     def update_node_texte(self):
534         """ Met a jour les noms des SD et valeurs des mots-cles """
535         value = self.item.GetText()
536         self.setText(1, value)
537
538     def update_node_texte_in_blue(self):
539         self.setTextColor( 1,Qt.blue )
540         value = self.item.GetText()
541         self.setText(1, value)
542
543     def update_nodes(self):
544         #print 'NODE update_nodes', self.item.GetLabelText()
545         self.build_children()
546
547     def update_valid(self) :
548         """Cette methode a pour but de mettre a jour la validite du noeud
549            et de propager la demande de mise a jour a son parent
550         """
551         #print "NODE update_valid", self.item.GetLabelText()
552         self.update_node_valid()
553         try :
554           self.treeParent.update_valid()
555         except:
556           pass
557             
558     def update_texte(self):
559         """ Met a jour les noms des SD et valeurs des mots-cles """
560         #print "NODE update_texte", self.item.GetLabelText()
561         self.update_node_texte()
562         if self.isExpanded() :
563             for child in self.children:
564                 if child.isHidden() == false : child.update_texte()
565
566
567     def forceRecalculChildren(self,niveau):
568         if self.state=='recalcule' : 
569            self.state=""
570            return
571         self.state='recalcule'
572         if hasattr(self.item,'object'):
573            self.item.object.state="modified"
574         for child in self.children:
575            if niveau > 0 : child.forceRecalculChildren(niveau - 1)
576               
577         
578
579     def doPaste(self,node_selected,pos='after'):
580         """
581             Déclenche la copie de l'objet item avec pour cible
582             l'objet passé en argument : node_selected
583         """
584         #print 'je passe dans doPaste'
585         objet_a_copier = self.item.get_copie_objet()
586         child=node_selected.doPasteCommande(objet_a_copier,pos)
587         return child
588
589     def doPasteCommande(self,objet_a_copier,pos='after'):
590         """
591           Réalise la copie de l'objet passé en argument qui est nécessairement
592           une commande
593         """
594         child=None
595         try :
596           child = self.append_brother(objet_a_copier,pos)
597         except :
598            pass
599         return child
600
601     def doPastePremier(self,objet_a_copier):
602         """
603            Réalise la copie de l'objet passé en argument (objet_a_copier)
604         """
605         objet = objet_a_copier.item.get_copie_objet()
606         child = self.append_child(objet,pos='first')
607         return child
608
609     def plieToutEtReaffiche(self):
610         self.editor.deplier = False
611         for item in self.children :
612             item.setPlie()
613         self.affichePanneau()
614
615     def deplieToutEtReaffiche(self):
616         self.editor.deplier = True
617         for item in self.children :
618             item.setDeplie()
619         self.affichePanneau()
620
621     def setPlie(self):
622         self.plie=True
623         self.setPlieChildren()
624
625     def setPlieChildren(self):
626         self.appartientAUnNoeudPlie=True
627         for item in self.children :
628             item.setPlieChildren()
629             
630     def setDeplie(self):
631         self.plie=False
632         self.setDeplieChildren()
633
634     def setDeplieChildren(self):
635         self.appartientAUnNoeudPlie=False
636         for item in self.children :
637             item.setDeplieChildren()
638             
639
640