@param platform a platforms with blocks and contrcutors
@return the flow graph object
"""
- #hold connections and blocks
- self._elements = list()
#initialize
Element.__init__(self, platform)
#inital blank import
def get_elements(self):
"""
Get a list of all the elements.
- Always ensure that the options block is in the list.
+ Always ensure that the options block is in the list (only once).
@return the element list
"""
- if self._options_block not in self._elements: self._elements.append(self._options_block)
- #ensure uniqueness of the elements list
- element_set = set()
- element_list = list()
- for element in self._elements:
- if element not in element_set: element_list.append(element)
- element_set.add(element)
- #store cleaned up list
- self._elements = element_list
+ options_block_count = self._elements.count(self._options_block)
+ if not options_block_count:
+ self._elements.append(self._options_block)
+ for i in range(options_block_count-1):
+ self._elements.remove(self._options_block)
return self._elements
def get_enabled_blocks(self):
#remove block, remove all involved connections
if element.is_block():
for port in element.get_ports():
- map(lambda c: self.remove_element(c), port.get_connections())
- #remove a connection
- elif element.is_connection(): pass
+ map(self.remove_element, port.get_connections())
self.get_elements().remove(element)
def evaluate(self, expr):
All connections and blocks must be valid.
"""
for c in self.get_elements():
- try: assert(c.is_valid())
+ try: assert c.is_valid()
except AssertionError: self._add_error_message('Element "%s" is not valid.'%c)
##############################################
connections_n = fg_n.findall('connection')
#create option block
self._options_block = self.get_parent().get_new_block(self, 'options')
- self._options_block.get_param('id').set_value('options')
#build the blocks
for block_n in blocks_n:
key = block_n.find('key')
def update(self):
"""
+ Removed deleted elements from the selected elements list.
Update highlighting so only the selected is highlighted.
Call update on all elements.
"""
selected_elements = self.get_selected_elements()
+ #remove deleted elements
+ for selected in selected_elements:
+ if selected not in self.get_elements():
+ selected_elements.remove(selected)
+ #set highlight and update all
for element in self.get_elements():
element.set_highlighted(element in selected_elements)
element.update()
What is selected?
At the given coordinate, return the elements found to be selected.
If coor_m is unspecified, return a list of only the first element found to be selected:
- Iterate though the elements backwardssince top elements are at the end of the list.
+ Iterate though the elements backwards since top elements are at the end of the list.
If an element is selected, place it at the end of the list so that is is drawn last,
and hence on top. Update the selected port information.
@param coor the coordinate of the mouse click
if not coor_m: selected_port = selected_element
selected_element = selected_element.get_parent()
selected.add(selected_element)
+ #place at the end of the list
+ self.get_elements().remove(element)
+ self.get_elements().append(element)
#single select mode, break
- if not coor_m:
- self.get_elements().remove(element)
- self.get_elements().append(element)
- break;
+ if not coor_m: break
#update selected ports
self._old_selected_port = self._new_selected_port
self._new_selected_port = selected_port
@return true for change
"""
changed = False
- for ports in (self.get_sinks(), self.get_sources()):
- if ports and ports[0].get_nports():
- #find the param that controls port0
- for param in self.get_params():
- if not param.is_enum() and param.get_key() in ports[0]._nports:
- #try to increment the port controller by direction
- try:
- value = param.evaluate()
- value = value + direction
- assert 0 < value
- param.set_value(value)
- changed = True
- except: pass
+ #concat the nports string from the private nports settings of both port0
+ nports_str = \
+ (self.get_sinks() and self.get_sinks()[0]._nports or '') + \
+ (self.get_sources() and self.get_sources()[0]._nports or '')
+ #modify all params whose keys appear in the nports string
+ for param in self.get_params():
+ if param.is_enum() or param.get_key() not in nports_str: continue
+ #try to increment the port controller by direction
+ try:
+ value = param.evaluate()
+ value = value + direction
+ assert 0 < value
+ param.set_value(value)
+ changed = True
+ except: pass
return changed
def get_doc(self):