1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 from PyQt4.QtGui import *
27 from PyQt4.QtCore import *
28 from Extensions.i18n import tr
29 from monChoixCommande import MonChoixCommande
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)
43 QTreeWidget.__init__(self, None )
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)
53 self.itemCourrant=None
55 self.connect(self, SIGNAL("itemClicked ( QTreeWidgetItem * ,int) "), self.handleOnItem)
56 self.connect(self, SIGNAL("itemDoubleClicked ( QTreeWidgetItem * ,int) "), self.handleDoubleClickedOnItem)
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()
65 def contextMenuEvent(self,event) :
66 print "contextMenuEvent"
67 coord=event.globalPos()
68 item= self.currentItem()
69 self.handleContextMenu(item,coord)
71 def handleContextMenu(self,item,coord):
73 Private slot to show the context menu of the listview.
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
79 print "handleContextMenu"
80 if item == None : return
81 if item.existeMenu == 0 : return
83 item.createPopUpMenu()
85 if item.item.get_nom() == "DISTRIBUTION" and item.item.isvalid() :
86 item.Graphe.setEnabled(1)
87 item.menu.exec_(coord)
90 def handleOnItem(self,item,int):
91 if (len(self.selectedIndexes())!=2): return
92 self.itemCourrant=item
95 fr = item.item.get_fr()
96 if self.editor: self.editor.labelCommentaire.setText(unicode(fr))
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)
110 item.fenetre.rendVisible()
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')
118 COMMENT = "COMMENTAIRE"
119 PARAMETERS = "PARAMETRE"
121 class JDCNode(QTreeWidgetItem):
122 def __init__( self, treeParent, item):
123 #print "creation d'un noeud : ", item, " ",item.nom,"", treeParent
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=[]
133 name = self.appliEficas.trUtf8( str( item.GetLabelText()[0] ) )
134 value = self.appliEficas.trUtf8( str( item.GetText() ) )
135 mesColonnes=QStringList()
136 mesColonnes << name << value
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
148 if isinstance(self,compobloc.Node) :
149 QTreeWidgetItem.__init__(self,None,mesColonnes)
151 QTreeWidgetItem.__init__(self,self.treeParent,mesColonnes)
152 self.treeParent.childrenComplete.append(self)
154 self.setToolTip(0,QString(self.item.get_fr()))
155 self.setToolTip(1,QString(self.item.get_fr()))
157 repIcon=QString(self.appliEficas.repIcon)
158 monIcone = QIcon(repIcon+"/" +self.item.GetIconName() + ".png")
159 self.setIcon(0,monIcone)
162 self.build_children()
166 self.item.connect("valid",self.onValid,())
167 self.item.connect("supp" ,self.onSupp,())
168 self.item.connect("add" ,self.onAdd,())
172 if self.item.getObject().isBLOC() : self.setExpanded(True)
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
184 for item in self.childrenComplete :
186 # print " je detruis ", item.item.GetLabelText() ," parent : ", item.treeParent.item.GetLabelText()
188 # print "mot clef fact"
189 if item.isExpanded():
190 if self.childrenComplete.index(item) < posInsertion :
191 listeExpanded.append(self.childrenComplete.index(item))
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)
199 self.childrenComplete = []
200 sublist = self.item._GetSubList()
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)
208 #print "*********** fin build_children ",self.item, self.item.GetLabelText()
211 def affichePanneau(self) :
212 #print "dans affichePanneau"
213 if self.item.isactif():
214 panel=self.getPanel2()
216 from monInactifPanel import PanelInactif
217 panel = PanelInactif(self,self.editor)
218 if hasattr(self,'fenetre') and self.fenetre:
221 if self.editor.fenetreCentraleAffichee != None :
222 self.editor.fenetreCentraleAffichee.close()
223 self.editor.fenetreCentraleAffichee=panel
224 if self.editor.widgetTree !=None : index=1
226 self.editor.widgetCentraleLayout.addWidget(self.fenetre)
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)
235 def createPopUpMenu(self):
236 #implemente dans les noeuds derives si necessaire
241 Cette methode a pour but de commentariser la commande pointee par self
243 # On traite par une exception le cas ou l'utilisateur final cherche a désactiver
244 # (commentariser) un commentaire.
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()
254 traceback.print_exc()
255 QMessageBox.critical( self.editor, "TOO BAD",str(e))
257 def unCommentIt(self):
259 Realise la decommentarisation de self
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()
269 QMessageBox.critical( self.editor, "Erreur !",str(e))
271 def addComment( self, after=True ):
273 Ajoute un commentaire a l'interieur du JDC :
275 self.editor.init_modif()
280 return self.append_brother( COMMENT, pos )
282 def addParameters( self, after=True ):
284 Ajoute un parametre a l'interieur du JDC :
286 self.editor.init_modif()
291 return self.append_brother( PARAMETERS, pos )
296 Rend le noeud courant (self) selectionne et deselectionne
299 for item in self.tree.selectedItems() :
301 self.setSelected( True )
302 self.setExpanded( True )
303 self.tree.setCurrentItem( self )
304 self.tree.node_selected= self
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'):
312 Permet d'ajouter un objet frere a l'objet associe au noeud self
313 par defaut on l'ajoute immediatement apres
316 #print "*********** append_brother ", self.item.GetLabelText()
317 self.editor.init_modif()
318 index = self.treeParent.children.index(self)
324 print unicode(pos), tr(" n'est pas un index valide pour append_brother")
326 return self.treeParent.append_child(name,pos=index)
328 def append_child(self,name,pos=None):
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.
335 #print "************** append_child ",self.item.GetLabelText()
336 self.editor.init_modif()
340 index = len(self.children)
341 elif type(pos) == types.IntType :
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)
350 index = self.item.get_index_child(name)
351 obj=self.item.additem(name,index) #CS_pbruno emet le signal 'add'
354 ## PNPNPN : cas de Map nouvelle version
358 old_obj = self.item.object.get_child(name.nom,restreint = 'oui')
360 child.affichePanneau()
363 # Souci pour gerer les copies des AFFE d'une commande à l autre
365 child=self.children[index]
366 child.affichePanneau()
368 child=self.children[index]
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())
380 Methode externe pour la destruction de l'objet associe au noeud
382 self.editor.init_modif()
383 index = self.vraiParent.children.index(self) - 1
384 if index < 0 : index =0
386 if self.item.nom == "VARIABLE" :
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()
400 self.treeParent.fenetre.reaffiche(toselect)
402 def deleteMultiple(self,liste=()):
404 Methode externe pour la destruction d une liste de noeud
406 from InterfaceQT4 import compojdc
407 self.editor.init_modif()
412 while not(isinstance(jdc,compojdc.Node)):
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
420 # Cas ou on détruit dans une ETape
422 parentPosition=self.treeParent
423 while not(isinstance(parentPosition, compojdc.Node)):
424 index=parentPosition.treeParent.children.index(parentPosition)
425 parentPosition=parentPosition.treeParent
428 noeud.treeParent.item.suppitem(noeud.item)
431 if recalcule : jdc.recalcule_etat_correlation()
432 try : toselect=parentPosition.children[index]
433 except : toselect=jdc
435 toselect.affichePanneau()
437 # #------------------------------------------------------------------
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()
448 self.update_node_valid()
449 self.update_node_label()
450 self.update_node_texte()
452 def onAdd(self,object):
453 #print "___________________________ onAdd", object
454 self.editor.init_modif()
457 def onSupp(self,object):
458 #print "___________________________ onSupp", self.item, self.item.GetLabelText()
459 self.editor.init_modif()
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)
471 def update_node_valid(self):
472 """Cette methode remet a jour la validite du noeud (icone)
475 repIcon=QString(self.appliEficas.repIcon)
476 monIcone = QIcon(repIcon+"/" +self.item.GetIconName() + ".png")
477 self.setIcon(0,monIcone)
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()
485 #self.setText(0, labeltext)
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
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=[]
504 noeud.setTextColor( 0,Qt.blue )
505 labeltext,fonte,couleur = noeud.item.GetLabelText()
506 noeud.setText(0, labeltext)
507 self.appliEficas.listeNoeudsColores.append(noeud)
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)
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)
520 def update_node_texte_in_blue(self):
521 self.setTextColor( 1,Qt.blue )
522 value = self.item.GetText()
523 self.setText(1, value)
525 def update_nodes(self):
526 #print 'NODE update_nodes', self.item.GetLabelText()
527 self.build_children()
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
533 #print "NODE update_valid", self.item.GetLabelText()
534 self.update_node_valid()
536 self.treeParent.update_valid()
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()
549 def forceRecalculChildren(self,niveau):
550 if self.state=='recalcule' :
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)
561 def doPaste(self,node_selected,pos='after'):
563 Déclenche la copie de l'objet item avec pour cible
564 l'objet passé en argument : node_selected
566 #print 'je passe dans doPaste'
567 objet_a_copier = self.item.get_copie_objet()
568 child=node_selected.doPasteCommande(objet_a_copier,pos)
571 def doPasteCommande(self,objet_a_copier,pos='after'):
573 Réalise la copie de l'objet passé en argument qui est nécessairement
578 child = self.append_brother(objet_a_copier,pos)
583 def doPastePremier(self,objet_a_copier):
585 Réalise la copie de l'objet passé en argument (objet_a_copier)
587 objet = objet_a_copier.item.get_copie_objet()
588 child = self.append_child(objet,pos='first')
593 self.setPlieChildren()
595 def setPlieChildren(self):
596 self.appartientAUnNoeudPlie=True
597 for item in self.children :
598 item.setPlieChildren()
603 self.setDeplieChildren()
605 def setDeplieChildren(self):
606 self.appartientAUnNoeudPlie=False
607 for item in self.children :
608 item.setDeplieChildren()