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