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