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