Salome HOME
modif pour MT
[tools/eficas.git] / InterfaceQT4 / compomclist.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017   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 from __future__ import absolute_import
22 import types
23 import traceback
24
25 from . import compofact
26 from . import browser
27 from . import typeNode
28 from Extensions.i18n import tr
29
30 from Editeur     import Objecttreeitem
31 from Noyau.N_OBJECT import ErrorObj
32
33
34 class Node(browser.JDCNode,typeNode.PopUpMenuNodeMinimal):
35
36     def createPopUpMenu(self):
37         typeNode.PopUpMenuNodeMinimal.createPopUpMenu(self)
38
39     def getPanelGroupe(self,parentQt,commande,insertIn=-1):
40         maDefinition=self.item.get_definition()
41         monObjet=self.item.object
42         monNom=self.item.nom
43         maCommande=commande
44         if hasattr(parentQt,'niveau'): self.niveau=parentQt.niveau+1
45         else : self.niveau=1
46         # attention si l objet est une mclist on utilise bloc
47         if not (monObjet.isMCList()) :
48            if  hasattr(self,'plie') and self.plie==True : 
49                from InterfaceQT4.monWidgetFactPlie import MonWidgetFactPlie
50                widget=MonWidgetFactPlie(self,self.editor,parentQt,maDefinition,monObjet,self.niveau,maCommande,insertIn)
51            else:
52                from InterfaceQT4.monWidgetFact import MonWidgetFact
53                widget=MonWidgetFact(self,self.editor,parentQt,maDefinition,monObjet,self.niveau,maCommande,insertIn)
54         else :
55            from InterfaceQT4.monWidgetBloc import MonWidgetBloc
56            widget=MonWidgetBloc(self,self.editor,parentQt,maDefinition,monObjet,self.niveau,maCommande)
57         return widget
58
59
60
61     def doPaste(self,node_selected,pos):
62         objet_a_copier = self.item.getCopieObjet()
63         # before est un effet de bord heureux sur l index
64         child=self.appendBrother(objet_a_copier,'before')
65         if self.editor.fenetreCentraleAffichee : self.editor.fenetreCentraleAffichee.node.affichePanneau()
66         self.update_NodeLabelInBlack()
67         self.parent().buildChildren()
68         return child
69
70
71
72 class MCListTreeItem(Objecttreeitem.SequenceTreeItem,compofact.FACTTreeItem):
73     """ La classe MCListTreeItem joue le role d'un adaptateur pour les objets
74         du noyau Accas instances de la classe MCLIST.
75         Elle adapte ces objets pour leur permettre d'etre integres en tant que
76         noeuds dans un arbre graphique (voir treewidget.py et ObjectTreeItem.py).
77         Cette classe delegue les appels de methode et les acces
78         aux attributs a l'objet du noyau soit manuellement soit 
79         automatiquement (voir classe Delegate et attribut object).
80     """
81     itemNode=Node
82
83     def init(self):
84         # Si l'objet Accas (MCList) a moins d'un mot cle facteur
85         # on utilise directement ce mot cle facteur comme delegue
86         self.updateDelegate()
87
88     def updateDelegate(self):
89         if len(self._object) > 1:
90            self.setDelegate(self._object)
91         else:
92            self.setDelegate(self._object.data[0])
93
94     def panel(self,jdcdisplay,pane,node):
95         """ Retourne une instance de l'objet panneau associe a l'item (self)
96             Si la liste ne contient qu'un mot cle facteur, on utilise le panneau
97             FACTPanel.
98             Si la liste est plus longue on utilise le panneau MCLISTPanel.
99         """
100         if len(self._object) > 1:
101            return MCLISTPanel(jdcdisplay,pane,node)
102         elif isinstance(self._object.data[0],ErrorObj):
103            return compoerror.ERRORPanel(jdcdisplay,pane,node)
104         else:
105            return compofact.FACTPanel(jdcdisplay,pane,node)
106
107     def isExpandable(self):
108         if len(self._object) > 1:
109            return Objecttreeitem.SequenceTreeItem.isExpandable(self)
110         else:
111            return compofact.FACTTreeItem.isExpandable(self)
112
113     def getSubList(self):
114         self.updateDelegate()
115         if len(self._object) <= 1:
116            self._object.data[0].alt_parent=self._object
117            return compofact.FACTTreeItem.getSubList(self)
118
119         liste=self._object.data
120         sublist=[None]*len(liste)
121         # suppression des items lies aux objets disparus
122         for item in self.sublist:
123            old_obj=item.getObject()
124            if old_obj in liste:
125               pos=liste.index(old_obj)
126               sublist[pos]=item
127            else:
128               pass # objets supprimes ignores
129         # ajout des items lies aux nouveaux objets
130         pos=0
131         for obj in liste:
132            if sublist[pos] is None:
133               # nouvel objet : on cree un nouvel item
134               def setFunction(value, object=obj):
135                   object=value
136               item = self.makeObjecttreeitem(self.appli, obj.nom + " : ", obj, setFunction)
137               sublist[pos]=item
138               #Attention : on ajoute une information supplementaire pour l'actualisation de 
139               # la validite. L'attribut parent d'un MCFACT pointe sur le parent de la MCLISTE
140               # et pas sur la MCLISTE elle meme ce qui rompt la chaine de remontee des
141               # informations de validite. alt_parent permet de remedier a ce defaut.
142               obj.alt_parent=self._object
143            pos=pos+1
144
145         self.sublist=sublist
146         return self.sublist
147
148     def getIconName(self):
149         if self._object.isValid():
150             return "ast-green-los"
151         elif self._object.isOblig():
152             return "ast-red-los"
153         else:
154             return "ast-yel-los"
155
156     def getDocu(self):
157         """ Retourne la clef de doc de l'objet pointe par self """
158         return self.object.getDocu()    
159
160     def isCopiable(self):
161         if len(self._object) > 1:
162            return Objecttreeitem.SequenceTreeItem.isCopiable(self)
163         else:
164            return compofact.FACTTreeItem.isCopiable(self)
165
166     def isMCFact(self):
167         """
168         Retourne 1 si l'objet pointe par self est un MCFact, 0 sinon
169         """
170         return len(self._object) <= 1
171
172     def isMCList(self):
173         """
174         Retourne 1 si l'objet pointe par self est une MCList, 0 sinon
175         """
176         return len(self._object) > 1
177         
178     def getCopieObjet(self):
179         return self._object.data[0].copy()
180
181     def addItem(self,obj,pos):
182         #print "compomclist.addItem",obj,pos
183         if len(self._object) <= 1:
184            return compofact.FACTTreeItem.addItem(self,obj,pos)
185
186         o= self.object.addEntite(obj,pos)
187         return o
188
189     def suppItem(self,item):
190         """
191         Retire un objet MCFACT de la MCList (self.object) 
192         """
193         #print "compomclist.suppItem",item
194         obj=item.getObject()
195         if len(self._object) <= 1:
196            return compofact.FACTTreeItem.suppItem(self,item)
197
198         if self.object.suppEntite(obj):
199            if len(self._object) == 1: self.updateDelegate()     
200            message = "Mot-clef " + obj.nom + " supprime"
201            return (1,message)
202         else:
203            return (0,tr('Impossible de supprimer ce mot-clef'))
204
205             
206 import Accas
207 objet = Accas.MCList    
208
209 def treeitem(appli,labeltext,object,setFunction):
210   """ Factory qui produit un objet treeitem adapte a un objet 
211       Accas.MCList (attribut objet de ce module)
212   """
213   return MCListTreeItem(appli,labeltext,object,setFunction)