diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2016-07-08 15:18:58 +0200 |
---|---|---|
committer | Sebastian Koslowski <koslowski@kit.edu> | 2016-07-13 16:35:49 +0200 |
commit | 4f29b9ae0b518bcc41038d6d300429e5d656d8e0 (patch) | |
tree | 8ebc70938eb96adabdc3d428cce673d3e2345015 | |
parent | 5cef62d03e3a750bce7f180930ceb777f7dba6b2 (diff) |
grc: refactor: block state handling
-rw-r--r-- | grc/core/Block.py | 25 | ||||
-rw-r--r-- | grc/gui/ActionHandler.py | 23 | ||||
-rw-r--r-- | grc/gui/FlowGraph.py | 25 |
3 files changed, 27 insertions, 46 deletions
diff --git a/grc/core/Block.py b/grc/core/Block.py index 743c9de99f..ec65a1a494 100644 --- a/grc/core/Block.py +++ b/grc/core/Block.py @@ -50,8 +50,7 @@ class Block(Element): is_block = True - # block states - DISABLED, ENABLED, BYPASSED = range(3) + STATE_LABELS = ['disabled', 'enabled', 'bypassed'] def __init__(self, flow_graph, n): """ @@ -410,9 +409,9 @@ class Block(Element): DISABLED - 2 """ try: - return int(self.params['_enabled'].get_value()) + return self.STATE_LABELS[int(self.params['_enabled'].get_value())] except ValueError: - return self.ENABLED + return 'enabled' @state.setter def state(self, value): @@ -424,9 +423,11 @@ class Block(Element): BYPASSED - 1 DISABLED - 2 """ - if value not in [self.ENABLED, self.BYPASSED, self.DISABLED]: - value = self.ENABLED - self.params['_enabled'].set_value(str(value)) + try: + encoded = str(self.STATE_LABELS.index(value)) + except ValueError: + encoded = str(self.STATE_LABELS.index('enabled')) + self.params['_enabled'].set_value(encoded) # Enable/Disable Aliases def get_enabled(self): @@ -436,7 +437,7 @@ class Block(Element): Returns: true for enabled """ - return self.state != self.DISABLED + return self.state != 'disabled' def set_enabled(self, enabled): """ @@ -449,7 +450,7 @@ class Block(Element): True if block changed state """ old_state = self.state - new_state = self.ENABLED if enabled else self.DISABLED + new_state = 'enabled' if enabled else 'disabled' self.state = new_state return old_state != new_state @@ -458,7 +459,7 @@ class Block(Element): """ Check if the block is bypassed """ - return self.state == self.BYPASSED + return self.state == 'bypassed' def set_bypassed(self): """ @@ -467,8 +468,8 @@ class Block(Element): Returns: True if block chagnes state """ - if self.state != self.BYPASSED and self.can_bypass(): - self.state = self.BYPASSED + if self.state != 'bypassed' and self.can_bypass(): + self.state = 'bypassed' return True return False diff --git a/grc/gui/ActionHandler.py b/grc/gui/ActionHandler.py index 732c48f50f..bb2f488b07 100644 --- a/grc/gui/ActionHandler.py +++ b/grc/gui/ActionHandler.py @@ -172,18 +172,13 @@ class ActionHandler: ################################################## # Enable/Disable ################################################## - elif action == Actions.BLOCK_ENABLE: - if flow_graph.enable_selected(True): - flow_graph_update() - page.state_cache.save_new_state(flow_graph.export_data()) - page.saved = False - elif action == Actions.BLOCK_DISABLE: - if flow_graph.enable_selected(False): - flow_graph_update() - page.state_cache.save_new_state(flow_graph.export_data()) - page.saved = False - elif action == Actions.BLOCK_BYPASS: - if flow_graph.bypass_selected(): + elif action in (Actions.BLOCK_ENABLE, Actions.BLOCK_DISABLE, Actions.BLOCK_BYPASS): + changed = flow_graph.change_state_selected(new_state={ + Actions.BLOCK_ENABLE: 'enabled', + Actions.BLOCK_DISABLE: 'disabled', + Actions.BLOCK_BYPASS: 'bypassed', + }[action]) + if changed: flow_graph_update() page.state_cache.save_new_state(flow_graph.export_data()) page.saved = False @@ -670,9 +665,9 @@ class ActionHandler: Actions.BLOCK_COPY.set_sensitive(bool(selected_blocks)) Actions.BLOCK_PASTE.set_sensitive(bool(self.clipboard)) #update enable/disable/bypass - can_enable = any(block.state != block.ENABLED + can_enable = any(block.state != 'enabled' for block in selected_blocks) - can_disable = any(block.state != block.DISABLED + can_disable = any(block.state != 'disabled' for block in selected_blocks) can_bypass_all = ( all(block.can_bypass() for block in selected_blocks) and diff --git a/grc/gui/FlowGraph.py b/grc/gui/FlowGraph.py index dc80a4c87e..87bd91d880 100644 --- a/grc/gui/FlowGraph.py +++ b/grc/gui/FlowGraph.py @@ -273,35 +273,20 @@ class FlowGraph(Element, _Flowgraph): """ return any(sb.port_controller_modify(direction) for sb in self.get_selected_blocks()) - def enable_selected(self, enable): + def change_state_selected(self, new_state): """ Enable/disable the selected blocks. Args: - enable: true to enable + new_state: a block state Returns: true if changed """ changed = False - for selected_block in self.get_selected_blocks(): - if selected_block.set_enabled(enable): - changed = True - return changed - - def bypass_selected(self): - """ - Bypass the selected blocks. - - Args: - None - Returns: - true if changed - """ - changed = False - for selected_block in self.get_selected_blocks(): - if selected_block.set_bypassed(): - changed = True + for block in self.selected_blocks(): + changed |= block.state != new_state + block.state = new_state return changed def move_selected(self, delta_coordinate): |