root/ossiedev/trunk/tools/wavedash/src/ComponentModel.py @ 10162

Revision 10162, 7.9 KB (checked in by Snyder.Jason, 3 years ago)

formatting

Line 
1from xml.dom import minidom
2from xml.dom.minidom import Node
3from PropertyModel import Property
4from PropertyWidgets import WidgetContainer
5import sys
6
7class ComponentEvent:
8    """ serves as a call back object for methods of ComponentListener
9    interface"""
10   
11    def __init__(self,type):
12        self.component = None
13        self.type = type
14    def getSource(self):
15        return self.component
16    def getEventType(self):
17        return self.type
18
19class ComponentListener:
20    """ An abstract class acting as an interface to update the views about the
21    changes happening in ComponentModel. All classes wish to interface with
22    Component model must inherit this class and can implement their
23    own logic by overriding the methods of this class """
24   
25    def componentStateChanged(self, cEvent):
26        pass
27    def componentHidden(self, cEvent):
28        pass
29    def componentShown(self, cEvent):
30        pass
31   
32class Component:
33    """ The Model class for Component objects. Encapsulates the
34    components' attributes and methods """
35     
36    def __init__(self, parent, id, name, pos, spd_file, controller, visible = True):
37        self.id = id
38        self.parent = parent
39        self.name = str(name)
40        self.position = pos
41        self.spdFile = str(spd_file)
42        self.controller = controller
43        self.visible = visible
44        self.properties = []
45        self.buildProperties(controller)
46   
47    def __str__(self):
48        return "Component: %s, pos: %d, parent: %s" % (self.name, self.position, self.parent.getName())
49   
50    def getName(self):
51        return self.name
52   
53    def getPosition(self):
54        return self.position
55   
56    def setPosition(self, newPos):
57        self.position = newPos
58   
59    def getPanel(self):
60        return self.panel
61   
62    def setPanel(self, newPanel):
63        self.panel = newPanel
64       
65    def setPosition(self, newPos):
66        self.position = newPos
67   
68    def hide(self):
69        self.visible = False
70   
71    def show(self):
72      self.visible = True
73   
74    def isVisible(self):
75        return self.visible
76   
77    def getParent(self):
78        return self.parent
79   
80    def getAllProperties(self):
81        plist = self.properties[0:]
82        return plist
83   
84    def findPropertyByName(self, name):
85        for prop in self.properties:
86            if prop.name == name:
87                return prop
88   
89    def findPropertyByID(self, id):
90        for prp in self.properties:
91            if prp.id == id:
92                return prp
93   
94    def updatePropertyState(self, pName, visible):
95        property = self.findPropertyByName(pName)
96        if property.isVisible() == visible:
97            return False
98        if visible:
99            property.show()
100        else:
101            property.hide()
102        return True
103   
104    def queryProperties(self):
105        #This method queries the list of properties and their values of a component,
106        #update the values to the property objects in the model.
107        utilRef = self.controller.getUtilRef()
108        newPrpList = []
109        newPrpList = utilRef.query(self.parent.getName(), self.name, newPrpList)
110        #newPrpList is a lsit of corba objects of type
111        #ossie.cf.CF.DataType(id='DCE:a337c5f0-8245-11dc-860f-00123f63025f',
112        #                    value=CORBA.Any(CORBA.TC_short, 2502))
113        if newPrpList is None:
114            return
115        for newprp in newPrpList:
116            prpRef = self.findPropertyByID(newprp.id)
117            if (prpRef is not None):
118                if prpRef.corbaObj is None:
119                    prpRef.corbaObj = newprp   
120                else:
121                   #print "setting old corbaObj to " + str(newprp.value._v)
122                    prpRef.setValue(newprp.value._v)
123           
124       # print newPrpList
125        return
126   
127    def configureProperty(self, pName, newVal):
128        prpRef = self.findPropertyByName(pName)
129        #storing the old value in a temp varaible to revert the changes if configure() fails.
130        curVal = prpRef.getValue()
131   
132        prpRef.setValue(newVal)
133        #configure() method expects the argument to be passed as a LIST object.
134        #hence convert the corbaObj to a list
135        prpList = [prpRef.corbaObj]
136        utilRef = self.controller.getUtilRef()
137       
138       
139        status = utilRef.configure(self.parent.getName(), self.name, prpList)
140        if status is False:
141            prpRef.setValue(curVal)
142        return status
143       
144    def buildProperties(self, controller):
145        #Read the path of prf file from spd file of the component
146        spddom = controller.getDOM(self.spdFile)
147       
148        prfnode = spddom.getElementsByTagName('propertyfile')[0]
149       
150        # propertyfile tag is of type
151        # <propertyfile type = "PRF">
152        #    <localfile name = <path of prf file> />
153        # </propertyfile>
154       
155        # Extracting the path of the prf file from childNodes[1], i.e. <localfile> tag
156        # the file name may be written in unicode format. convert to string for internal use
157        prf_file = str(prfnode.childNodes[1].attributes['name'].value)
158               
159        #Now parsing .prf file to build property model
160        prfdom = controller.getDOM(prf_file)
161        if prfdom is None:
162            print "buildProperties(): Failed to create DOM for PRF file for ", self.parent.getName(), "/", self.name
163            sys.exit(-1)
164       
165        prpNode = prfdom.getElementsByTagName('properties')[0]
166        prpList = prpNode.childNodes
167       
168        try:
169            for prp in prpList:
170                if ( prp.nodeType != Node.ELEMENT_NODE or str(prp.tagName) == 'description' ):
171                    continue
172                #populate property fields and create the property object
173               
174                id = prp.attributes['id'].value
175                name = prp.attributes['name'].value
176                type = (str(prp.tagName), str(prp.attributes['type'].value))
177                mode = prp.attributes['mode'].value
178               
179                desc = ''
180                value = None
181                range = (0,10000)
182                actionType = ''
183                kindType = ''
184               
185                #Iterate through children of <simple> tag to update desc, value and kindType
186                for child in prp.childNodes:
187                    if (child.nodeType == Node.ELEMENT_NODE):
188                        if (child.tagName == 'description'):
189                            desc = child.firstChild.data
190                        elif (child.tagName == 'value'):
191                            value = child.firstChild.data
192                        elif (child.tagName == 'kind'):
193                            kindType = child.attributes['kindtype'].value
194                        elif (child.tagName == 'range'):
195                            min = child.childNodes[1].firstChild.data
196                            max = child.childNodes[3].firstChild.data
197                            range = (min, max)
198                        elif (child.tagName == 'action'):
199                            actionType = child.attributes['type'].value
200               
201               
202                widget = controller.getDefaultWidget(type)
203
204               
205                newProp = Property(self, id, name, type, mode, desc, value,
206                                   range, kindType, actionType, widget, prf_file)
207                self.properties.append(newProp)
208        except:
209            print "Error in reading PRF files for ", self.parent.getName(), "/", self.name
210            errorMsg = sys.exc_info()
211            print errorMsg
212            sys.exit(-1)
213           
214    def changePropWidget(self, property, newWidgetType):
215        prpRef = self.findPropertyByName(property)
216        curWidget = prpRef.getWidget()
217        if curWidget.type == newWidgetType:
218            return None
219        newWidget = self.controller.getWidgetByType(newWidgetType)
220        prpRef.widget = newWidget
221        return prpRef
222       
223   
224    def fireModelChange(self, event):
225        pass
Note: See TracBrowser for help on using the browser.