diff options
53 files changed, 4302 insertions, 4733 deletions
diff --git a/grc/blocks/dummy.xml b/grc/blocks/dummy.xml deleted file mode 100644 index c0ca3b6698..0000000000 --- a/grc/blocks/dummy.xml +++ /dev/null @@ -1,11 +0,0 @@ -<?xml version="1.0"?> -<!-- -################################################### -##Dummy Block -################################################### ---> -<block> - <name>Missing Block</name> - <key>dummy_block</key> - <make>raise NotImplementedError()</make> -</block> diff --git a/grc/core/Block.py b/grc/core/Block.py index de1d83ee9a..9a1d72c2de 100644 --- a/grc/core/Block.py +++ b/grc/core/Block.py @@ -17,226 +17,200 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import + import collections import itertools +import ast + +import six +from six.moves import map, range from Cheetah.Template import Template -from .utils import epy_block_io, odict +from . import utils + from . Constants import ( BLOCK_FLAG_NEED_QT_GUI, - ADVANCED_PARAM_TAB, DEFAULT_PARAM_TAB, + ADVANCED_PARAM_TAB, BLOCK_FLAG_THROTTLE, BLOCK_FLAG_DISABLE_BYPASS, BLOCK_FLAG_DEPRECATED, - BLOCK_ENABLED, BLOCK_BYPASSED, BLOCK_DISABLED ) -from . Element import Element +from . Element import Element, lazy_property -def _get_keys(lst): - return [elem.get_key() for elem in lst] - - -def _get_elem(lst, key): - try: - return lst[_get_keys(lst).index(key)] - except ValueError: - raise ValueError('Key "{}" not found in {}.'.format(key, _get_keys(lst))) +def _get_elem(iterable, key): + items = list(iterable) + for item in items: + if item.key == key: + return item + return ValueError('Key "{}" not found in {}.'.format(key, items)) class Block(Element): is_block = True - def __init__(self, flow_graph, n): - """ - Make a new block from nested data. + STATE_LABELS = ['disabled', 'enabled', 'bypassed'] - Args: - flow: graph the parent element - n: the nested odict + def __init__(self, parent, key, name, **n): + """Make a new block from nested data.""" + super(Block, self).__init__(parent) + + self.key = key + self.name = name + self.category = [cat.strip() for cat in n.get('category', '').split('/') if cat.strip()] + self.flags = n.get('flags', '') + self._doc = n.get('doc', '').strip('\n').replace('\\\n', '') - Returns: - block a new block - """ - # Grab the data - self._doc = (n.find('doc') or '').strip('\n').replace('\\\n', '') - self._imports = map(lambda i: i.strip(), n.findall('import')) - self._make = n.find('make') - self._var_make = n.find('var_make') - self._checks = n.findall('check') - self._callbacks = n.findall('callback') - self._bus_structure_source = n.find('bus_structure_source') or '' - self._bus_structure_sink = n.find('bus_structure_sink') or '' - self.port_counters = [itertools.count(), itertools.count()] - - # Build the block - Element.__init__(self, flow_graph) - - # Grab the data - params = n.findall('param') - sources = n.findall('source') - sinks = n.findall('sink') - self._name = n.find('name') - self._key = n.find('key') - category = (n.find('category') or '').split('/') - self.category = [cat.strip() for cat in category if cat.strip()] - self._flags = n.find('flags') or '' # Backwards compatibility - if n.find('throttle') and BLOCK_FLAG_THROTTLE not in self._flags: - self._flags += BLOCK_FLAG_THROTTLE - self._grc_source = n.find('grc_source') or '' - self._block_wrapper_path = n.find('block_wrapper_path') - self._bussify_sink = n.find('bus_sink') - self._bussify_source = n.find('bus_source') - self._var_value = n.find('var_value') or '$value' + if n.get('throttle') and BLOCK_FLAG_THROTTLE not in self.flags: + self.flags += BLOCK_FLAG_THROTTLE - # Get list of param tabs - n_tabs = n.find('param_tab_order') or None - self._param_tab_labels = n_tabs.findall('tab') if n_tabs is not None else [DEFAULT_PARAM_TAB] - - # Create the param objects - self._params = list() - - # Add the id param - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({ - 'name': 'ID', - 'key': 'id', - 'type': 'id', - }) - )) - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({ - 'name': 'Enabled', - 'key': '_enabled', - 'type': 'raw', - 'value': 'True', - 'hide': 'all', - }) - )) - for param in itertools.imap(lambda n: self.get_parent().get_parent().Param(block=self, n=n), params): - key = param.get_key() - # Test against repeated keys - if key in self.get_param_keys(): - raise Exception('Key "{}" already exists in params'.format(key)) - # Store the param - self.get_params().append(param) - # Create the source objects - self._sources = list() - for source in map(lambda n: self.get_parent().get_parent().Port(block=self, n=n, dir='source'), sources): - key = source.get_key() - # Test against repeated keys - if key in self.get_source_keys(): - raise Exception('Key "{}" already exists in sources'.format(key)) - # Store the port - self.get_sources().append(source) - self.back_ofthe_bus(self.get_sources()) - # Create the sink objects - self._sinks = list() - for sink in map(lambda n: self.get_parent().get_parent().Port(block=self, n=n, dir='sink'), sinks): - key = sink.get_key() - # Test against repeated keys - if key in self.get_sink_keys(): - raise Exception('Key "{}" already exists in sinks'.format(key)) - # Store the port - self.get_sinks().append(sink) - self.back_ofthe_bus(self.get_sinks()) - self.current_bus_structure = {'source': '', 'sink': ''} + self._imports = [i.strip() for i in n.get('import', [])] + self._make = n.get('make') + self._var_make = n.get('var_make') + self._var_value = n.get('var_value', '$value') + self._checks = n.get('check', []) + self._callbacks = n.get('callback', []) + + self._grc_source = n.get('grc_source', '') + self.block_wrapper_path = n.get('block_wrapper_path') # Virtual source/sink and pad source/sink blocks are # indistinguishable from normal GR blocks. Make explicit # checks for them here since they have no work function or # buffers to manage. - self.is_virtual_or_pad = self._key in ( + self.is_virtual_or_pad = self.key in ( "virtual_source", "virtual_sink", "pad_source", "pad_sink") - self.is_variable = self._key.startswith('variable') - self.is_import = (self._key == 'import') + self.is_variable = self.key.startswith('variable') + self.is_import = (self.key == 'import') # Disable blocks that are virtual/pads or variables if self.is_virtual_or_pad or self.is_variable: - self._flags += BLOCK_FLAG_DISABLE_BYPASS - - if not (self.is_virtual_or_pad or self.is_variable or self._key == 'options'): - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({'name': 'Block Alias', - 'key': 'alias', - 'type': 'string', - 'hide': 'part', - 'tab': ADVANCED_PARAM_TAB - }) - )) - - if (len(sources) or len(sinks)) and not self.is_virtual_or_pad: - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({'name': 'Core Affinity', - 'key': 'affinity', - 'type': 'int_vector', - 'hide': 'part', - 'tab': ADVANCED_PARAM_TAB - }) - )) - if len(sources) and not self.is_virtual_or_pad: - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({'name': 'Min Output Buffer', - 'key': 'minoutbuf', - 'type': 'int', - 'hide': 'part', - 'value': '0', - 'tab': ADVANCED_PARAM_TAB - }) - )) - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({'name': 'Max Output Buffer', - 'key': 'maxoutbuf', - 'type': 'int', - 'hide': 'part', - 'value': '0', - 'tab': ADVANCED_PARAM_TAB - }) - )) - - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({'name': 'Comment', - 'key': 'comment', - 'type': '_multiline', - 'hide': 'part', - 'value': '', - 'tab': ADVANCED_PARAM_TAB - }) - )) + self.flags += BLOCK_FLAG_DISABLE_BYPASS - self._epy_source_hash = -1 # for epy blocks - self._epy_reload_error = None + params_n = n.get('param', []) + sources_n = n.get('source', []) + sinks_n = n.get('sink', []) - if self._bussify_sink: - self.bussify({'name': 'bus', 'type': 'bus'}, 'sink') - if self._bussify_source: - self.bussify({'name': 'bus', 'type': 'bus'}, 'source') + # Get list of param tabs + self.params = collections.OrderedDict() + self._init_params( + params_n=params_n, + has_sinks=len(sinks_n), + has_sources=len(sources_n) + ) - def get_bus_structure(self, direction): - if direction == 'source': - bus_structure = self._bus_structure_source - else: - bus_structure = self._bus_structure_sink + self.sources = self._init_ports(sources_n, direction='source') + self.sinks = self._init_ports(sinks_n, direction='sink') + self.active_sources = [] # on rewrite + self.active_sinks = [] # on rewrite - bus_structure = self.resolve_dependencies(bus_structure) + self.states = {'_enabled': True} - if not bus_structure: - return '' # TODO: Don't like empty strings. should change this to None eventually + self._init_bus_ports(n) - try: - clean_bus_structure = self.get_parent().evaluate(bus_structure) - return clean_bus_structure - except: - return '' + def _init_params(self, params_n, has_sources, has_sinks): + param_factory = self.parent_platform.get_new_param + + def add_param(key, **kwargs): + self.params[key] = param_factory(self, key=key, **kwargs) + + add_param(key='id', name='ID', type='id') + + if not (self.is_virtual_or_pad or self.is_variable or self.key == 'options'): + add_param(key='alias', name='Block Alias', type='string', + hide='part', tab=ADVANCED_PARAM_TAB) + + if not self.is_virtual_or_pad and (has_sources or has_sinks): + add_param(key='affinity', name='Core Affinity', type='int_vector', + hide='part', tab=ADVANCED_PARAM_TAB) + + if not self.is_virtual_or_pad and has_sources: + add_param(key='minoutbuf', name='Min Output Buffer', type='int', + hide='part', value='0', tab=ADVANCED_PARAM_TAB) + add_param(key='maxoutbuf', name='Max Output Buffer', type='int', + hide='part', value='0', tab=ADVANCED_PARAM_TAB) + + base_params_n = {n['key']: n for n in params_n} + for param_n in params_n: + key = param_n['key'] + if key in self.params: + raise Exception('Key "{}" already exists in params'.format(key)) + + extended_param_n = base_params_n.get(param_n.pop('base_key', None), {}) + extended_param_n.update(param_n) + self.params[key] = param_factory(self, **extended_param_n) + + add_param(key='comment', name='Comment', type='_multiline', hide='part', + value='', tab=ADVANCED_PARAM_TAB) + + def _init_ports(self, ports_n, direction): + port_factory = self.parent_platform.get_new_port + ports = [] + port_keys = set() + stream_port_keys = itertools.count() + for i, port_n in enumerate(ports_n): + port_n.setdefault('key', str(next(stream_port_keys))) + port = port_factory(parent=self, direction=direction, **port_n) + key = port.key + if key in port_keys: + raise Exception('Key "{}" already exists in {}'.format(key, direction)) + port_keys.add(key) + ports.append(port) + return ports + + ############################################## + # validation and rewrite + ############################################## + def rewrite(self): + """ + Add and remove ports to adjust for the nports. + """ + Element.rewrite(self) + + def rekey(ports): + """Renumber non-message/message ports""" + domain_specific_port_index = collections.defaultdict(int) + for port in [p for p in ports if p.key.isdigit()]: + domain = port.domain + port.key = str(domain_specific_port_index[domain]) + domain_specific_port_index[domain] += 1 + + # Adjust nports + for ports in (self.sources, self.sinks): + self._rewrite_nports(ports) + self.back_ofthe_bus(ports) + rekey(ports) + + self._rewrite_bus_ports() + + # disconnect hidden ports + for port in itertools.chain(self.sources, self.sinks): + if port.get_hide(): + for connection in port.get_connections(): + self.parent_flowgraph.remove_element(connection) + + + self.active_sources = [p for p in self.get_sources_gui() if not p.get_hide()] + self.active_sinks = [p for p in self.get_sinks_gui() if not p.get_hide()] + + def _rewrite_nports(self, ports): + for port in ports: + if port.is_clone: # Not a master port and no left-over clones + continue + nports = port.get_nports() or 1 + for clone in port.clones[nports-1:]: + # Remove excess connections + for connection in clone.get_connections(): + self.parent_flowgraph.remove_element(connection) + port.remove_clone(clone) + ports.remove(clone) + # Add more cloned ports + for j in range(1 + len(port.clones), nports): + clone = port.add_clone() + ports.insert(ports.index(port) + j, clone) def validate(self): """ @@ -245,114 +219,95 @@ class Block(Element): Evaluate the checks: each check must evaluate to True. """ Element.validate(self) - # Evaluate the checks + self._run_checks() + self._validate_generate_mode_compat() + self._validate_var_value() + + def _run_checks(self): + """Evaluate the checks""" for check in self._checks: check_res = self.resolve_dependencies(check) try: - if not self.get_parent().evaluate(check_res): + if not self.parent.evaluate(check_res): self.add_error_message('Check "{}" failed.'.format(check)) except: self.add_error_message('Check "{}" did not evaluate.'.format(check)) - # For variables check the value (only if var_value is used - if self.is_variable and self._var_value != '$value': - value = self._var_value - try: - value = self.get_var_value() - self.get_parent().evaluate(value) - except Exception as err: - self.add_error_message('Value "{}" cannot be evaluated:\n{}'.format(value, err)) - - # check if this is a GUI block and matches the selected generate option - current_generate_option = self.get_parent().get_option('generate_options') + def _validate_generate_mode_compat(self): + """check if this is a GUI block and matches the selected generate option""" + current_generate_option = self.parent.get_option('generate_options') def check_generate_mode(label, flag, valid_options): block_requires_mode = ( - flag in self.get_flags() or - self.get_name().upper().startswith(label) + flag in self.flags or self.name.upper().startswith(label) ) if block_requires_mode and current_generate_option not in valid_options: self.add_error_message("Can't generate this block in mode: {} ".format( repr(current_generate_option))) check_generate_mode('QT GUI', BLOCK_FLAG_NEED_QT_GUI, ('qt_gui', 'hb_qt_gui')) - if self._epy_reload_error: - self.get_param('_source_code').add_error_message(str(self._epy_reload_error)) - - def rewrite(self): - """ - Add and remove ports to adjust for the nports. - """ - Element.rewrite(self) - # Check and run any custom rewrite function for this block - getattr(self, 'rewrite_' + self._key, lambda: None)() - - # Adjust nports, disconnect hidden ports - for ports in (self.get_sources(), self.get_sinks()): - for i, master_port in enumerate(ports): - nports = master_port.get_nports() or 1 - num_ports = 1 + len(master_port.get_clones()) - if master_port.get_hide(): - for connection in master_port.get_connections(): - self.get_parent().remove_element(connection) - if not nports and num_ports == 1: # Not a master port and no left-over clones - continue - # Remove excess cloned ports - for port in master_port.get_clones()[nports-1:]: - # Remove excess connections - for connection in port.get_connections(): - self.get_parent().remove_element(connection) - master_port.remove_clone(port) - ports.remove(port) - # Add more cloned ports - for j in range(num_ports, nports): - port = master_port.add_clone() - ports.insert(ports.index(master_port) + j, port) - self.back_ofthe_bus(ports) - # Renumber non-message/message ports - domain_specific_port_index = collections.defaultdict(int) - for port in filter(lambda p: p.get_key().isdigit(), ports): - domain = port.get_domain() - port._key = str(domain_specific_port_index[domain]) - domain_specific_port_index[domain] += 1 + def _validate_var_value(self): + """or variables check the value (only if var_value is used)""" + if self.is_variable and self._var_value != '$value': + value = self._var_value + try: + value = self.get_var_value() + self.parent.evaluate(value) + except Exception as err: + self.add_error_message('Value "{}" cannot be evaluated:\n{}'.format(value, err)) - def port_controller_modify(self, direction): - """ - Change the port controller. + ############################################## + # props + ############################################## - Args: - direction: +1 or -1 + @lazy_property + def is_throtteling(self): + return BLOCK_FLAG_THROTTLE in self.flags - Returns: - true for change - """ - changed = False - # Concat the nports string from the private nports settings of all ports - nports_str = ' '.join([port._nports for port in self.get_ports()]) - # 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.get_evaluated() - value = value + direction - if 0 < value: - param.set_value(value) - changed = True - except: - pass - return changed + @lazy_property + def is_deprecated(self): + return BLOCK_FLAG_DEPRECATED in self.flags - def get_doc(self): - platform = self.get_parent().get_parent() - documentation = platform.block_docstrings.get(self._key, {}) + @property + def documentation(self): + documentation = self.parent_platform.block_docstrings.get(self.key, {}) from_xml = self._doc.strip() if from_xml: documentation[''] = from_xml return documentation + @property + def comment(self): + return self.params['comment'].get_value() + + @property + def state(self): + """Gets the block's current state.""" + try: + return self.STATE_LABELS[int(self.states['_enabled'])] + except ValueError: + return 'enabled' + + @state.setter + def state(self, value): + """Sets the state for the block.""" + try: + encoded = self.STATE_LABELS.index(value) + except ValueError: + encoded = 1 + self.states['_enabled'] = encoded + + # Enable/Disable Aliases + @property + def enabled(self): + """Get the enabled state of the block""" + return self.state != 'disabled' + + ############################################## + # Getters (old) + ############################################## + def get_imports(self, raw=False): """ Resolve all import statements. @@ -365,7 +320,8 @@ class Block(Element): """ if raw: return self._imports - return filter(lambda i: i, sum(map(lambda i: self.resolve_dependencies(i).split('\n'), self._imports), [])) + return [i for i in sum((self.resolve_dependencies(i).split('\n') + for i in self._imports), []) if i] def get_make(self, raw=False): if raw: @@ -390,173 +346,20 @@ class Block(Element): if 'self.' in callback: return callback return 'self.{}.{}'.format(self.get_id(), callback) - return map(make_callback, self._callbacks) + return [make_callback(c) for c in self._callbacks] def is_virtual_sink(self): - return self.get_key() == 'virtual_sink' + return self.key == 'virtual_sink' def is_virtual_source(self): - return self.get_key() == 'virtual_source' - - ########################################################################### - # Custom rewrite functions - ########################################################################### - - def rewrite_epy_block(self): - flowgraph = self.get_parent() - platform = flowgraph.get_parent() - param_blk = self.get_param('_io_cache') - param_src = self.get_param('_source_code') - - src = param_src.get_value() - src_hash = hash((self.get_id(), src)) - if src_hash == self._epy_source_hash: - return - - try: - blk_io = epy_block_io.extract(src) - - except Exception as e: - self._epy_reload_error = ValueError(str(e)) - try: # Load last working block io - blk_io_args = eval(param_blk.get_value()) - if len(blk_io_args) == 6: - blk_io_args += ([],) # add empty callbacks - blk_io = epy_block_io.BlockIO(*blk_io_args) - except Exception: - return - else: - self._epy_reload_error = None # Clear previous errors - param_blk.set_value(repr(tuple(blk_io))) - - # print "Rewriting embedded python block {!r}".format(self.get_id()) - - self._epy_source_hash = src_hash - self._name = blk_io.name or blk_io.cls - self._doc = blk_io.doc - self._imports[0] = 'import ' + self.get_id() - self._make = '{0}.{1}({2})'.format(self.get_id(), blk_io.cls, ', '.join( - '{0}=${{ {0} }}'.format(key) for key, _ in blk_io.params)) - self._callbacks = ['{0} = ${{ {0} }}'.format(attr) for attr in blk_io.callbacks] - - params = {} - for param in list(self._params): - if hasattr(param, '__epy_param__'): - params[param.get_key()] = param - self._params.remove(param) - - for key, value in blk_io.params: - try: - param = params[key] - param.set_default(value) - except KeyError: # need to make a new param - name = key.replace('_', ' ').title() - n = odict(dict(name=name, key=key, type='raw', value=value)) - param = platform.Param(block=self, n=n) - setattr(param, '__epy_param__', True) - self._params.append(param) - - def update_ports(label, ports, port_specs, direction): - ports_to_remove = list(ports) - iter_ports = iter(ports) - ports_new = [] - port_current = next(iter_ports, None) - for key, port_type in port_specs: - reuse_port = ( - port_current is not None and - port_current.get_type() == port_type and - (key.isdigit() or port_current.get_key() == key) - ) - if reuse_port: - ports_to_remove.remove(port_current) - port, port_current = port_current, next(iter_ports, None) - else: - n = odict(dict(name=label + str(key), type=port_type, key=key)) - if port_type == 'message': - n['name'] = key - n['optional'] = '1' - port = platform.Port(block=self, n=n, dir=direction) - ports_new.append(port) - # replace old port list with new one - del ports[:] - ports.extend(ports_new) - # remove excess port connections - for port in ports_to_remove: - for connection in port.get_connections(): - flowgraph.remove_element(connection) - - update_ports('in', self.get_sinks(), blk_io.sinks, 'sink') - update_ports('out', self.get_sources(), blk_io.sources, 'source') - self.rewrite() - - def back_ofthe_bus(self, portlist): - portlist.sort(key=lambda p: p._type == 'bus') - - def filter_bus_port(self, ports): - buslist = [p for p in ports if p._type == 'bus'] - return buslist or ports - - # Main functions to get and set the block state - # Also kept get_enabled and set_enabled to keep compatibility - def get_state(self): - """ - Gets the block's current state. - - Returns: - ENABLED - 0 - BYPASSED - 1 - DISABLED - 2 - """ - try: - return int(eval(self.get_param('_enabled').get_value())) - except: - return BLOCK_ENABLED - - def set_state(self, state): - """ - Sets the state for the block. - - Args: - ENABLED - 0 - BYPASSED - 1 - DISABLED - 2 - """ - if state in [BLOCK_ENABLED, BLOCK_BYPASSED, BLOCK_DISABLED]: - self.get_param('_enabled').set_value(str(state)) - else: - self.get_param('_enabled').set_value(str(BLOCK_ENABLED)) - - # Enable/Disable Aliases - def get_enabled(self): - """ - Get the enabled state of the block. - - Returns: - true for enabled - """ - return not (self.get_state() == BLOCK_DISABLED) - - def set_enabled(self, enabled): - """ - Set the enabled state of the block. - - Args: - enabled: true for enabled - - Returns: - True if block changed state - """ - old_state = self.get_state() - new_state = BLOCK_ENABLED if enabled else BLOCK_DISABLED - self.set_state(new_state) - return old_state != new_state + return self.key == 'virtual_source' # Block bypassing def get_bypassed(self): """ Check if the block is bypassed """ - return self.get_state() == BLOCK_BYPASSED + return self.state == 'bypassed' def set_bypassed(self): """ @@ -565,8 +368,8 @@ class Block(Element): Returns: True if block chagnes state """ - if self.get_state() != BLOCK_BYPASSED and self.can_bypass(): - self.set_state(BLOCK_BYPASSED) + if self.state != 'bypassed' and self.can_bypass(): + self.state = 'bypassed' return True return False @@ -574,112 +377,60 @@ class Block(Element): """ Check the number of sinks and sources and see if this block can be bypassed """ # Check to make sure this is a single path block # Could possibly support 1 to many blocks - if len(self.get_sources()) != 1 or len(self.get_sinks()) != 1: + if len(self.sources) != 1 or len(self.sinks) != 1: return False - if not (self.get_sources()[0].get_type() == self.get_sinks()[0].get_type()): + if not (self.sources[0].get_type() == self.sinks[0].get_type()): return False - if self.bypass_disabled(): + if BLOCK_FLAG_DISABLE_BYPASS in self.flags: return False return True def __str__(self): - return 'Block - {} - {}({})'.format(self.get_id(), self.get_name(), self.get_key()) + return 'Block - {} - {}({})'.format(self.get_id(), self.name, self.key) def get_id(self): - return self.get_param('id').get_value() - - def get_name(self): - return self._name - - def get_key(self): - return self._key + return self.params['id'].get_value() def get_ports(self): - return self.get_sources() + self.get_sinks() + return self.sources + self.sinks def get_ports_gui(self): - return self.filter_bus_port(self.get_sources()) + self.filter_bus_port(self.get_sinks()) + return self.get_sources_gui() + self.get_sinks_gui() + + def active_ports(self): + return itertools.chain(self.active_sources, self.active_sinks) def get_children(self): - return self.get_ports() + self.get_params() + return self.get_ports() + self.params.values() def get_children_gui(self): - return self.get_ports_gui() + self.get_params() - - def get_block_wrapper_path(self): - return self._block_wrapper_path - - def get_comment(self): - return self.get_param('comment').get_value() - - def get_flags(self): - return self._flags - - def throtteling(self): - return BLOCK_FLAG_THROTTLE in self._flags - - def bypass_disabled(self): - return BLOCK_FLAG_DISABLE_BYPASS in self._flags - - @property - def is_deprecated(self): - return BLOCK_FLAG_DEPRECATED in self._flags + return self.get_ports_gui() + self.params.values() ############################################## - # Access Params + # Access ############################################## - def get_param_tab_labels(self): - return self._param_tab_labels - - def get_param_keys(self): - return _get_keys(self._params) def get_param(self, key): - return _get_elem(self._params, key) - - def get_params(self): - return self._params - - def has_param(self, key): - try: - _get_elem(self._params, key) - return True - except: - return False - - ############################################## - # Access Sinks - ############################################## - def get_sink_keys(self): - return _get_keys(self._sinks) + return self.params[key] def get_sink(self, key): - return _get_elem(self._sinks, key) - - def get_sinks(self): - return self._sinks + return _get_elem(self.sinks, key) def get_sinks_gui(self): - return self.filter_bus_port(self.get_sinks()) - - ############################################## - # Access Sources - ############################################## - def get_source_keys(self): - return _get_keys(self._sources) + return self.filter_bus_port(self.sinks) def get_source(self, key): - return _get_elem(self._sources, key) - - def get_sources(self): - return self._sources + return _get_elem(self.sources, key) def get_sources_gui(self): - return self.filter_bus_port(self.get_sources()) + return self.filter_bus_port(self.sources) def get_connections(self): - return sum([port.get_connections() for port in self.get_ports()], []) + return sum((port.get_connections() for port in self.get_ports()), []) + ############################################## + # Resolve + ############################################## def resolve_dependencies(self, tmpl): """ Resolve a paramater dependency with cheetah templates. @@ -693,103 +444,14 @@ class Block(Element): tmpl = str(tmpl) if '$' not in tmpl: return tmpl - n = dict((param.get_key(), param.template_arg) - for param in self.get_params()) # TODO: cache that + # TODO: cache that + n = {key: param.template_arg for key, param in six.iteritems(self.params)} try: return str(Template(tmpl, n)) except Exception as err: return "Template error: {}\n {}".format(tmpl, err) ############################################## - # Controller Modify - ############################################## - def type_controller_modify(self, direction): - """ - Change the type controller. - - Args: - direction: +1 or -1 - - Returns: - true for change - """ - changed = False - type_param = None - for param in filter(lambda p: p.is_enum(), self.get_params()): - children = self.get_ports() + self.get_params() - # Priority to the type controller - if param.get_key() in ' '.join(map(lambda p: p._type, children)): type_param = param - # Use param if type param is unset - if not type_param: - type_param = param - if type_param: - # Try to increment the enum by direction - try: - keys = type_param.get_option_keys() - old_index = keys.index(type_param.get_value()) - new_index = (old_index + direction + len(keys)) % len(keys) - type_param.set_value(keys[new_index]) - changed = True - except: - pass - return changed - - def form_bus_structure(self, direc): - if direc == 'source': - get_p = self.get_sources - get_p_gui = self.get_sources_gui - bus_structure = self.get_bus_structure('source') - else: - get_p = self.get_sinks - get_p_gui = self.get_sinks_gui - bus_structure = self.get_bus_structure('sink') - - struct = [range(len(get_p()))] - if True in map(lambda a: isinstance(a.get_nports(), int), get_p()): - structlet = [] - last = 0 - for j in [i.get_nports() for i in get_p() if isinstance(i.get_nports(), int)]: - structlet.extend(map(lambda a: a+last, range(j))) - last = structlet[-1] + 1 - struct = [structlet] - if bus_structure: - - struct = bus_structure - - self.current_bus_structure[direc] = struct - return struct - - def bussify(self, n, direc): - if direc == 'source': - get_p = self.get_sources - get_p_gui = self.get_sources_gui - bus_structure = self.get_bus_structure('source') - else: - get_p = self.get_sinks - get_p_gui = self.get_sinks_gui - bus_structure = self.get_bus_structure('sink') - - for elt in get_p(): - for connect in elt.get_connections(): - self.get_parent().remove_element(connect) - - if ('bus' not in map(lambda a: a.get_type(), get_p())) and len(get_p()) > 0: - struct = self.form_bus_structure(direc) - self.current_bus_structure[direc] = struct - if get_p()[0].get_nports(): - n['nports'] = str(1) - - for i in range(len(struct)): - n['key'] = str(len(get_p())) - n = odict(n) - port = self.get_parent().get_parent().Port(block=self, n=n, dir=direc) - get_p().append(port) - elif 'bus' in map(lambda a: a.get_type(), get_p()): - for elt in get_p_gui(): - get_p().remove(elt) - self.current_bus_structure[direc] = '' - - ############################################## # Import/Export Methods ############################################## def export_data(self): @@ -799,17 +461,19 @@ class Block(Element): Returns: a nested data odict """ - n = odict() - n['key'] = self.get_key() - n['param'] = map(lambda p: p.export_data(), sorted(self.get_params(), key=str)) - if 'bus' in map(lambda a: a.get_type(), self.get_sinks()): - n['bus_sink'] = str(1) - if 'bus' in map(lambda a: a.get_type(), self.get_sources()): - n['bus_source'] = str(1) - return n + n = collections.OrderedDict() + n['key'] = self.key + + params = (param.export_data() for param in six.itervalues(self.params)) + states = (collections.OrderedDict([('key', key), ('value', repr(value))]) + for key, value in six.iteritems(self.states)) + n['param'] = sorted(itertools.chain(states, params), key=lambda p: p['key']) - def get_hash(self): - return hash(tuple(map(hash, self.get_params()))) + if any('bus' in a.get_type() for a in self.sinks): + n['bus_sink'] = '1' + if any('bus' in a.get_type() for a in self.sources): + n['bus_source'] = '1' + return n def import_data(self, n): """ @@ -823,27 +487,293 @@ class Block(Element): Args: n: the nested data odict """ - my_hash = 0 - while self.get_hash() != my_hash: - params_n = n.findall('param') - for param_n in params_n: - key = param_n.find('key') - value = param_n.find('value') - # The key must exist in this block's params - if key in self.get_param_keys(): - self.get_param(key).set_value(value) + param_data = {p['key']: p['value'] for p in n.get('param', [])} + + for key in self.states: + try: + self.states[key] = ast.literal_eval(param_data.pop(key)) + except (KeyError, SyntaxError, ValueError): + pass + + def get_hash(): + return hash(tuple(hash(v) for v in self.params.values())) + + pre_rewrite_hash = -1 + while pre_rewrite_hash != get_hash(): + for key, value in six.iteritems(param_data): + try: + self.params[key].set_value(value) + except KeyError: + continue # Store hash and call rewrite - my_hash = self.get_hash() + pre_rewrite_hash = get_hash() self.rewrite() - bussinks = n.findall('bus_sink') - if len(bussinks) > 0 and not self._bussify_sink: - self.bussify({'name': 'bus', 'type': 'bus'}, 'sink') - elif len(bussinks) > 0: - self.bussify({'name': 'bus', 'type': 'bus'}, 'sink') - self.bussify({'name': 'bus', 'type': 'bus'}, 'sink') - bussrcs = n.findall('bus_source') - if len(bussrcs) > 0 and not self._bussify_source: - self.bussify({'name': 'bus', 'type': 'bus'}, 'source') - elif len(bussrcs) > 0: - self.bussify({'name': 'bus', 'type': 'bus'}, 'source') - self.bussify({'name': 'bus', 'type': 'bus'}, 'source') + + self._import_bus_stuff(n) + + ############################################## + # Bus ports stuff + ############################################## + + def get_bus_structure(self, direction): + bus_structure = self.resolve_dependencies(self._bus_structure[direction]) + if not bus_structure: + return + try: + return self.parent_flowgraph.evaluate(bus_structure) + except: + return + + @staticmethod + def back_ofthe_bus(portlist): + portlist.sort(key=lambda p: p._type == 'bus') + + @staticmethod + def filter_bus_port(ports): + buslist = [p for p in ports if p._type == 'bus'] + return buslist or ports + + def _import_bus_stuff(self, n): + bus_sinks = n.get('bus_sink', []) + if len(bus_sinks) > 0 and not self._bussify_sink: + self.bussify('sink') + elif len(bus_sinks) > 0: + self.bussify('sink') + self.bussify('sink') + bus_sources = n.get('bus_source', []) + if len(bus_sources) > 0 and not self._bussify_source: + self.bussify('source') + elif len(bus_sources) > 0: + self.bussify('source') + self.bussify('source') + + def form_bus_structure(self, direc): + ports = self.sources if direc == 'source' else self.sinks + struct = self.get_bus_structure(direc) + + if not struct: + struct = [list(range(len(ports)))] + + elif any(isinstance(p.get_nports(), int) for p in ports): + last = 0 + structlet = [] + for port in ports: + nports = port.get_nports() + if not isinstance(nports, int): + continue + structlet.extend(a + last for a in range(nports)) + last += nports + struct = [structlet] + + self.current_bus_structure[direc] = struct + return struct + + def bussify(self, direc): + ports = self.sources if direc == 'source' else self.sinks + + for elt in ports: + for connect in elt.get_connections(): + self.parent.remove_element(connect) + + if ports and all('bus' != p.get_type() for p in ports): + struct = self.current_bus_structure[direc] = self.form_bus_structure(direc) + n = {'type': 'bus'} + if ports[0].get_nports(): + n['nports'] = '1' + + for i, structlet in enumerate(struct): + name = 'bus{}#{}'.format(i, len(structlet)) + port = self.parent_platform.get_new_port( + self, direction=direc, key=str(len(ports)), name=name, **n) + ports.append(port) + elif any('bus' == p.get_type() for p in ports): + get_p_gui = self.get_sources_gui if direc == 'source' else self.get_sinks_gui + for elt in get_p_gui(): + ports.remove(elt) + self.current_bus_structure[direc] = '' + + def _init_bus_ports(self, n): + self.current_bus_structure = {'source': '', 'sink': ''} + self._bus_structure = {'source': n.get('bus_structure_source', ''), + 'sink': n.get('bus_structure_sink', '')} + self._bussify_sink = n.get('bus_sink') + self._bussify_source = n.get('bus_source') + if self._bussify_sink: + self.bussify('sink') + if self._bussify_source: + self.bussify('source') + + def _rewrite_bus_ports(self): + return # fixme: probably broken + + def doit(ports, ports_gui, direc): + if not self.current_bus_structure[direc]: + return + + bus_structure = self.form_bus_structure(direc) + for port in ports_gui[len(bus_structure):]: + for connect in port.get_connections(): + self.parent_flowgraph.remove_element(connect) + ports.remove(port) + + port_factory = self.parent_platform.get_new_port + + if len(ports_gui) < len(bus_structure): + for i in range(len(ports_gui), len(bus_structure)): + port = port_factory(self, direction=direc, key=str(1 + i), + name='bus', type='bus') + ports.append(port) + + doit(self.sources, self.get_sources_gui(), 'source') + doit(self.sinks, self.get_sinks_gui(), 'sink') + + if 'bus' in [a.get_type() for a in self.get_sources_gui()]: + for i in range(len(self.get_sources_gui())): + if not self.get_sources_gui()[i].get_connections(): + continue + source = self.get_sources_gui()[i] + sink = [] + + for j in range(len(source.get_connections())): + sink.append(source.get_connections()[j].sink_port) + for elt in source.get_connections(): + self.parent_flowgraph.remove_element(elt) + for j in sink: + self.parent_flowgraph.connect(source, j) + + +class EPyBlock(Block): + + def __init__(self, flow_graph, **n): + super(EPyBlock, self).__init__(flow_graph, **n) + self._epy_source_hash = -1 # for epy blocks + self._epy_reload_error = None + + def rewrite(self): + Element.rewrite(self) + + param_blk = self.params['_io_cache'] + param_src = self.params['_source_code'] + + src = param_src.get_value() + src_hash = hash((self.get_id(), src)) + if src_hash == self._epy_source_hash: + return + + try: + blk_io = utils.epy_block_io.extract(src) + + except Exception as e: + self._epy_reload_error = ValueError(str(e)) + try: # Load last working block io + blk_io_args = eval(param_blk.get_value()) + if len(blk_io_args) == 6: + blk_io_args += ([],) # add empty callbacks + blk_io = utils.epy_block_io.BlockIO(*blk_io_args) + except Exception: + return + else: + self._epy_reload_error = None # Clear previous errors + param_blk.set_value(repr(tuple(blk_io))) + + # print "Rewriting embedded python block {!r}".format(self.get_id()) + + self._epy_source_hash = src_hash + self.name = blk_io.name or blk_io.cls + self._doc = blk_io.doc + self._imports[0] = 'import ' + self.get_id() + self._make = '{0}.{1}({2})'.format(self.get_id(), blk_io.cls, ', '.join( + '{0}=${{ {0} }}'.format(key) for key, _ in blk_io.params)) + self._callbacks = ['{0} = ${{ {0} }}'.format(attr) for attr in blk_io.callbacks] + self._update_params(blk_io.params) + self._update_ports('in', self.sinks, blk_io.sinks, 'sink') + self._update_ports('out', self.sources, blk_io.sources, 'source') + + super(EPyBlock, self).rewrite() + + def _update_params(self, params_in_src): + param_factory = self.parent_platform.get_new_param + params = {} + for param in list(self.params): + if hasattr(param, '__epy_param__'): + params[param.key] = param + del self.params[param.key] + + for key, value in params_in_src: + try: + param = params[key] + if param.default == param.value: + param.set_value(value) + param.default = str(value) + except KeyError: # need to make a new param + param = param_factory( + parent=self, key=key, type='raw', value=value, + name=key.replace('_', ' ').title(), + ) + setattr(param, '__epy_param__', True) + self.params[key] = param + + def _update_ports(self, label, ports, port_specs, direction): + port_factory = self.parent_platform.get_new_port + ports_to_remove = list(ports) + iter_ports = iter(ports) + ports_new = [] + port_current = next(iter_ports, None) + for key, port_type in port_specs: + reuse_port = ( + port_current is not None and + port_current.get_type() == port_type and + (key.isdigit() or port_current.key == key) + ) + if reuse_port: + ports_to_remove.remove(port_current) + port, port_current = port_current, next(iter_ports, None) + else: + n = dict(name=label + str(key), type=port_type, key=key) + if port_type == 'message': + n['name'] = key + n['optional'] = '1' + port = port_factory(self, direction=direction, **n) + ports_new.append(port) + # replace old port list with new one + del ports[:] + ports.extend(ports_new) + # remove excess port connections + for port in ports_to_remove: + for connection in port.get_connections(): + self.parent_flowgraph.remove_element(connection) + + def validate(self): + super(EPyBlock, self).validate() + if self._epy_reload_error: + self.params['_source_code'].add_error_message(str(self._epy_reload_error)) + + +class DummyBlock(Block): + + is_dummy_block = True + build_in_param_keys = 'id alias affinity minoutbuf maxoutbuf comment' + + def __init__(self, parent, key, missing_key, params_n): + super(DummyBlock, self).__init__(parent=parent, key=missing_key, name='Missing Block') + param_factory = self.parent_platform.get_new_param + for param_n in params_n: + key = param_n['key'] + self.params.setdefault(key, param_factory(self, key=key, name=key, type='string')) + + def is_valid(self): + return False + + @property + def enabled(self): + return False + + def add_missing_port(self, key, dir): + port = self.parent_platform.get_new_port( + parent=self, direction=dir, key=key, name='?', type='', + ) + if port.is_source: + self.sources.append(port) + else: + self.sinks.append(port) + return port diff --git a/grc/core/Config.py b/grc/core/Config.py index 78ff344998..b65a421b7a 100644 --- a/grc/core/Config.py +++ b/grc/core/Config.py @@ -17,6 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import import os from os.path import expanduser, normpath, expandvars, exists diff --git a/grc/core/Connection.py b/grc/core/Connection.py index c028d89ddc..066532149b 100644 --- a/grc/core/Connection.py +++ b/grc/core/Connection.py @@ -17,16 +17,21 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import + +import collections + +from six.moves import range + from . import Constants -from .Element import Element -from .utils import odict +from .Element import Element, lazy_property class Connection(Element): is_connection = True - def __init__(self, flow_graph, porta, portb): + def __init__(self, parent, porta, portb): """ Make a new connection given the parent and 2 ports. @@ -39,75 +44,87 @@ class Connection(Element): Returns: a new connection """ - Element.__init__(self, flow_graph) + Element.__init__(self, parent) + + source, sink = self._get_sink_source(porta, portb) + + self.source_port = source + self.sink_port = sink + + # Ensure that this connection (source -> sink) is unique + if self in self.parent_flowgraph.connections: + raise LookupError('This connection between source and sink is not unique.') + + if self.is_bus(): + self._make_bus_connect() + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return self.source_port == other.source_port and self.sink_port == other.sink_port + + @staticmethod + def _get_sink_source(porta, portb): source = sink = None # Separate the source and sink for port in (porta, portb): if port.is_source: source = port - else: + if port.is_sink: sink = port if not source: raise ValueError('Connection could not isolate source') if not sink: raise ValueError('Connection could not isolate sink') - busses = len(filter(lambda a: a.get_type() == 'bus', [source, sink])) % 2 - if not busses == 0: - raise ValueError('busses must get with busses') + return source, sink - if not len(source.get_associated_ports()) == len(sink.get_associated_ports()): - raise ValueError('port connections must have same cardinality') - # Ensure that this connection (source -> sink) is unique - for connection in flow_graph.connections: - if connection.get_source() is source and connection.get_sink() is sink: - raise LookupError('This connection between source and sink is not unique.') - self._source = source - self._sink = sink - if source.get_type() == 'bus': - - sources = source.get_associated_ports() - sinks = sink.get_associated_ports() - - for i in range(len(sources)): - try: - flow_graph.connect(sources[i], sinks[i]) - except: - pass + @lazy_property + def source_block(self): + return self.source_port.parent_block + + @lazy_property + def sink_block(self): + return self.sink_port.parent_block + + @property + def enabled(self): + """ + Get the enabled state of this connection. + + Returns: + true if source and sink blocks are enabled + """ + return self.source_block.enabled and self.sink_block.enabled def __str__(self): return 'Connection (\n\t{}\n\t\t{}\n\t{}\n\t\t{}\n)'.format( - self.get_source().get_parent(), - self.get_source(), - self.get_sink().get_parent(), - self.get_sink(), + self.source_block, self.source_port, self.sink_block, self.sink_port, ) def is_bus(self): - return self.get_source().get_type() == self.get_sink().get_type() == 'bus' + return self.source_port.get_type() == 'bus' def validate(self): """ Validate the connections. The ports must match in io size. """ - """ - Validate the connections. - The ports must match in type. - """ Element.validate(self) - platform = self.get_parent().get_parent() - source_domain = self.get_source().get_domain() - sink_domain = self.get_sink().get_domain() + platform = self.parent_platform + + source_domain = self.source_port.domain + sink_domain = self.sink_port.domain + if (source_domain, sink_domain) not in platform.connection_templates: self.add_error_message('No connection known for domains "{}", "{}"'.format( - source_domain, sink_domain)) + source_domain, sink_domain)) too_many_other_sinks = ( not platform.domains.get(source_domain, []).get('multiple_sinks', False) and - len(self.get_source().get_enabled_connections()) > 1 + len(self.source_port.get_enabled_connections()) > 1 ) too_many_other_sources = ( not platform.domains.get(sink_domain, []).get('multiple_sources', False) and - len(self.get_sink().get_enabled_connections()) > 1 + len(self.sink_port.get_enabled_connections()) > 1 ) if too_many_other_sinks: self.add_error_message( @@ -116,30 +133,11 @@ class Connection(Element): self.add_error_message( 'Domain "{}" can have only one upstream block'.format(sink_domain)) - source_size = Constants.TYPE_TO_SIZEOF[self.get_source().get_type()] * self.get_source().get_vlen() - sink_size = Constants.TYPE_TO_SIZEOF[self.get_sink().get_type()] * self.get_sink().get_vlen() + source_size = Constants.TYPE_TO_SIZEOF[self.source_port.get_type()] * self.source_port.get_vlen() + sink_size = Constants.TYPE_TO_SIZEOF[self.sink_port.get_type()] * self.sink_port.get_vlen() if source_size != sink_size: self.add_error_message('Source IO size "{}" does not match sink IO size "{}".'.format(source_size, sink_size)) - def get_enabled(self): - """ - Get the enabled state of this connection. - - Returns: - true if source and sink blocks are enabled - """ - return self.get_source().get_parent().get_enabled() and \ - self.get_sink().get_parent().get_enabled() - - ############################# - # Access Ports - ############################# - def get_sink(self): - return self._sink - - def get_source(self): - return self._source - ############################################## # Import/Export Methods ############################################## @@ -150,9 +148,23 @@ class Connection(Element): Returns: a nested data odict """ - n = odict() - n['source_block_id'] = self.get_source().get_parent().get_id() - n['sink_block_id'] = self.get_sink().get_parent().get_id() - n['source_key'] = self.get_source().get_key() - n['sink_key'] = self.get_sink().get_key() + n = collections.OrderedDict() + n['source_block_id'] = self.source_block.get_id() + n['sink_block_id'] = self.sink_block.get_id() + n['source_key'] = self.source_port.key + n['sink_key'] = self.sink_port.key return n + + def _make_bus_connect(self): + source, sink = self.source_port, self.sink_port + + if source.get_type() == sink.get_type() == 'bus': + raise ValueError('busses must get with busses') + + sources = source.get_associated_ports() + sinks = sink.get_associated_ports() + if len(sources) != len(sinks): + raise ValueError('port connections must have same cardinality') + + for ports in zip(sources, sinks): + self.parent_flowgraph.connect(*ports) diff --git a/grc/core/Constants.py b/grc/core/Constants.py index edd3442a94..caf301be60 100644 --- a/grc/core/Constants.py +++ b/grc/core/Constants.py @@ -17,10 +17,14 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import + import os -import numpy import stat +import numpy +import six + # Data files DATA_DIR = os.path.dirname(__file__) FLOW_GRAPH_DTD = os.path.join(DATA_DIR, 'flow_graph.dtd') @@ -50,29 +54,35 @@ BLOCK_FLAG_DISABLE_BYPASS = 'disable_bypass' BLOCK_FLAG_NEED_QT_GUI = 'need_qt_gui' BLOCK_FLAG_DEPRECATED = 'deprecated' -# Block States -BLOCK_DISABLED = 0 -BLOCK_ENABLED = 1 -BLOCK_BYPASSED = 2 - # File creation modes TOP_BLOCK_FILE_MODE = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | \ stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH HIER_BLOCK_FILE_MODE = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH +PARAM_TYPE_NAMES = ( + 'raw', 'enum', + 'complex', 'real', 'float', 'int', + 'complex_vector', 'real_vector', 'float_vector', 'int_vector', + 'hex', 'string', 'bool', + 'file_open', 'file_save', '_multiline', '_multiline_python_external', + 'id', 'stream_id', + 'gui_hint', + 'import', +) + # Define types, native python + numpy VECTOR_TYPES = (tuple, list, set, numpy.ndarray) COMPLEX_TYPES = [complex, numpy.complex, numpy.complex64, numpy.complex128] REAL_TYPES = [float, numpy.float, numpy.float32, numpy.float64] -INT_TYPES = [int, long, numpy.int, numpy.int8, numpy.int16, numpy.int32, numpy.uint64, +INT_TYPES = [int, numpy.int, numpy.int8, numpy.int16, numpy.int32, numpy.uint64, numpy.uint, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64] # Cast to tuple for isinstance, concat subtypes COMPLEX_TYPES = tuple(COMPLEX_TYPES + REAL_TYPES + INT_TYPES) REAL_TYPES = tuple(REAL_TYPES + INT_TYPES) INT_TYPES = tuple(INT_TYPES) -# Updating colors. Using the standard color pallette from: -# http://www.google.com/design/spec/style/color.html#color-color-palette +# Updating colors. Using the standard color palette from: +# http://www.google.com/design/spec/style/color.html#color-color-palette # Most are based on the main, primary color standard. Some are within # that color's spectrum when it was deemed necessary. GRC_COLOR_BROWN = '#795548' @@ -95,54 +105,32 @@ GRC_COLOR_GREY = '#BDBDBD' GRC_COLOR_WHITE = '#FFFFFF' CORE_TYPES = ( # name, key, sizeof, color - ('Complex Float 64', 'fc64', 16, GRC_COLOR_BROWN), - ('Complex Float 32', 'fc32', 8, GRC_COLOR_BLUE), - ('Complex Integer 64', 'sc64', 16, GRC_COLOR_LIGHT_GREEN), - ('Complex Integer 32', 'sc32', 8, GRC_COLOR_GREEN), - ('Complex Integer 16', 'sc16', 4, GRC_COLOR_AMBER), - ('Complex Integer 8', 'sc8', 2, GRC_COLOR_PURPLE), - ('Float 64', 'f64', 8, GRC_COLOR_CYAN), - ('Float 32', 'f32', 4, GRC_COLOR_ORANGE), - ('Integer 64', 's64', 8, GRC_COLOR_LIME), - ('Integer 32', 's32', 4, GRC_COLOR_TEAL), - ('Integer 16', 's16', 2, GRC_COLOR_YELLOW), - ('Integer 8', 's8', 1, GRC_COLOR_PURPLE_A400), - ('Bits (unpacked byte)', 'bit', 1, GRC_COLOR_PURPLE_A100), - ('Async Message', 'message', 0, GRC_COLOR_GREY), - ('Bus Connection', 'bus', 0, GRC_COLOR_WHITE), - ('Wildcard', '', 0, GRC_COLOR_WHITE), + ('Complex Float 64', 'fc64', 16, GRC_COLOR_BROWN), + ('Complex Float 32', 'fc32', 8, GRC_COLOR_BLUE), + ('Complex Integer 64', 'sc64', 16, GRC_COLOR_LIGHT_GREEN), + ('Complex Integer 32', 'sc32', 8, GRC_COLOR_GREEN), + ('Complex Integer 16', 'sc16', 4, GRC_COLOR_AMBER), + ('Complex Integer 8', 'sc8', 2, GRC_COLOR_PURPLE), + ('Float 64', 'f64', 8, GRC_COLOR_CYAN), + ('Float 32', 'f32', 4, GRC_COLOR_ORANGE), + ('Integer 64', 's64', 8, GRC_COLOR_LIME), + ('Integer 32', 's32', 4, GRC_COLOR_TEAL), + ('Integer 16', 's16', 2, GRC_COLOR_YELLOW), + ('Integer 8', 's8', 1, GRC_COLOR_PURPLE_A400), + ('Bits (unpacked byte)', 'bit', 1, GRC_COLOR_PURPLE_A100), + ('Async Message', 'message', 0, GRC_COLOR_GREY), + ('Bus Connection', 'bus', 0, GRC_COLOR_WHITE), + ('Wildcard', '', 0, GRC_COLOR_WHITE), ) ALIAS_TYPES = { 'complex': (8, GRC_COLOR_BLUE), - 'float': (4, GRC_COLOR_ORANGE), - 'int': (4, GRC_COLOR_TEAL), - 'short': (2, GRC_COLOR_YELLOW), - 'byte': (1, GRC_COLOR_PURPLE_A400), - 'bits': (1, GRC_COLOR_PURPLE_A100), + 'float': (4, GRC_COLOR_ORANGE), + 'int': (4, GRC_COLOR_TEAL), + 'short': (2, GRC_COLOR_YELLOW), + 'byte': (1, GRC_COLOR_PURPLE_A400), + 'bits': (1, GRC_COLOR_PURPLE_A100), } -TYPE_TO_COLOR = dict() -TYPE_TO_SIZEOF = dict() - -for name, key, sizeof, color in CORE_TYPES: - TYPE_TO_COLOR[key] = color - TYPE_TO_SIZEOF[key] = sizeof - -for key, (sizeof, color) in ALIAS_TYPES.iteritems(): - TYPE_TO_COLOR[key] = color - TYPE_TO_SIZEOF[key] = sizeof - -# Coloring -COMPLEX_COLOR_SPEC = '#3399FF' -FLOAT_COLOR_SPEC = '#FF8C69' -INT_COLOR_SPEC = '#00FF99' -SHORT_COLOR_SPEC = '#FFFF66' -BYTE_COLOR_SPEC = '#FF66FF' -COMPLEX_VECTOR_COLOR_SPEC = '#3399AA' -FLOAT_VECTOR_COLOR_SPEC = '#CC8C69' -INT_VECTOR_COLOR_SPEC = '#00CC99' -SHORT_VECTOR_COLOR_SPEC = '#CCCC33' -BYTE_VECTOR_COLOR_SPEC = '#CC66CC' -ID_COLOR_SPEC = '#DDDDDD' -WILDCARD_COLOR_SPEC = '#FFFFFF' +TYPE_TO_SIZEOF = {key: sizeof for name, key, sizeof, color in CORE_TYPES} +TYPE_TO_SIZEOF.update((key, sizeof) for key, (sizeof, _) in ALIAS_TYPES.items()) diff --git a/grc/core/Element.py b/grc/core/Element.py index 67c36e12b4..86e0746655 100644 --- a/grc/core/Element.py +++ b/grc/core/Element.py @@ -1,28 +1,50 @@ -""" -Copyright 2008, 2009, 2015 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" +# Copyright 2008, 2009, 2015, 2016 Free Software Foundation, Inc. +# This file is part of GNU Radio +# +# GNU Radio Companion is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# GNU Radio Companion is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + +import weakref +import functools + + +class lazy_property(object): + + def __init__(self, func): + self.func = func + functools.update_wrapper(self, func) + + def __get__(self, instance, cls): + if instance is None: + return self + value = self.func(instance) + setattr(instance, self.func.__name__, value) + return value + + +def nop_write(prop): + """Make this a property with a nop setter""" + def nop(self, value): + pass + return prop.setter(nop) class Element(object): def __init__(self, parent=None): - self._parent = parent - self._error_messages = list() + self._parent = weakref.ref(parent) if parent else lambda: None + self._error_messages = [] ################################################## # Element Validation API @@ -33,6 +55,7 @@ class Element(object): Call this base method before adding error messages in the subclass. """ del self._error_messages[:] + for child in self.get_children(): child.validate() @@ -43,7 +66,9 @@ class Element(object): Returns: true when the element is enabled and has no error messages or is bypassed """ - return (not self.get_error_messages() or not self.get_enabled()) or self.get_bypassed() + if not self.enabled or self.get_bypassed(): + return True + return not next(self.iter_error_messages(), False) def add_error_message(self, msg): """ @@ -63,11 +88,20 @@ class Element(object): Returns: a list of error message strings """ - error_messages = list(self._error_messages) # Make a copy - for child in filter(lambda c: c.get_enabled() and not c.get_bypassed(), self.get_children()): - for msg in child.get_error_messages(): - error_messages.append("{}:\n\t{}".format(child, msg.replace("\n", "\n\t"))) - return error_messages + return [msg if elem is self else "{}:\n\t{}".format(elem, msg.replace("\n", "\n\t")) + for elem, msg in self.iter_error_messages()] + + def iter_error_messages(self): + """ + Iterate over error messages. Yields tuples of (element, message) + """ + for msg in self._error_messages: + yield self, msg + for child in self.get_children(): + if not child.enabled or child.get_bypassed(): + continue + for element_msg in child.iter_error_messages(): + yield element_msg def rewrite(self): """ @@ -77,7 +111,8 @@ class Element(object): for child in self.get_children(): child.rewrite() - def get_enabled(self): + @property + def enabled(self): return True def get_bypassed(self): @@ -86,8 +121,39 @@ class Element(object): ############################################## # Tree-like API ############################################## - def get_parent(self): - return self._parent + @property + def parent(self): + return self._parent() + + def get_parent_by_type(self, cls): + parent = self.parent + if parent is None: + return None + elif isinstance(parent, cls): + return parent + else: + return parent.get_parent_by_type(cls) + + @lazy_property + def parent_platform(self): + from .Platform import Platform + return self.get_parent_by_type(Platform) + + @lazy_property + def parent_flowgraph(self): + from .FlowGraph import FlowGraph + return self.get_parent_by_type(FlowGraph) + + @lazy_property + def parent_block(self): + from .Block import Block + return self.get_parent_by_type(Block) + + def reset_parents_by_type(self): + """Reset all lazy properties""" + for name, obj in vars(Element): # explicitly only in Element, not subclasses + if isinstance(obj, lazy_property): + delattr(self, name) def get_children(self): return list() diff --git a/grc/core/Element.pyi b/grc/core/Element.pyi index c81180a33e..2a2aed401c 100644 --- a/grc/core/Element.pyi +++ b/grc/core/Element.pyi @@ -1,4 +1,4 @@ -# Copyright 2008, 2009, 2015, 2016 Free Software Foundation, Inc. +# Copyright 2016 Free Software Foundation, Inc. # This file is part of GNU Radio # # GNU Radio Companion is free software; you can redistribute it and/or @@ -15,40 +15,27 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +from typing import Union + from . import Platform, FlowGraph, Block -def lazy_property(func): - return func +lazy_property = property # fixme: descriptors don't seems to be supported class Element(object): - def __init__(self, parent=None): - ... + def __init__(self, parent: Union[None, 'Element'] = None): ... - @property - def parent(self): - ... + @lazy_property + def parent(self) -> 'Element': ... - def get_parent_by_type(self, cls): - parent = self.parent - if parent is None: - return None - elif isinstance(parent, cls): - return parent - else: - return parent.get_parent_by_type(cls) + def get_parent_by_type(self, cls) -> Union[None, 'Element']: ... @lazy_property - def parent_platform(self): -> Platform.Platform - ... + def parent_platform(self) -> Platform.Platform: ... @lazy_property - def parent_flowgraph(self): -> FlowGraph.FlowGraph - ... + def parent_flowgraph(self) -> FlowGraph.FlowGraph: ... @lazy_property - def parent_block(self): -> Block.Block - ... - - + def parent_block(self) -> Block.Block: ... diff --git a/grc/core/FlowGraph.py b/grc/core/FlowGraph.py index 949eecaa71..18a5778015 100644 --- a/grc/core/FlowGraph.py +++ b/grc/core/FlowGraph.py @@ -15,17 +15,18 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +from __future__ import absolute_import, print_function + import imp import time -from itertools import ifilter, chain -from operator import methodcaller, attrgetter - import re +from operator import methodcaller +import collections from . import Messages from .Constants import FLOW_GRAPH_FILE_FORMAT_VERSION from .Element import Element -from .utils import odict, expr_utils +from .utils import expr_utils _parameter_matcher = re.compile('^(parameter)$') _monitors_searcher = re.compile('(ctrlport_monitor)') @@ -39,36 +40,31 @@ class FlowGraph(Element): is_flow_graph = True - def __init__(self, platform): + def __init__(self, parent): """ Make a flow graph from the arguments. Args: - platform: a platforms with blocks and contrcutors + parent: a platforms with blocks and element factories Returns: the flow graph object """ - Element.__init__(self, platform) - self._elements = [] + Element.__init__(self, parent) self._timestamp = time.ctime() + self._options_block = self.parent_platform.get_new_block(self, 'options') - self.platform = platform # todo: make this a lazy prop - self.blocks = [] + self.blocks = [self._options_block] self.connections = [] self._eval_cache = {} self.namespace = {} self.grc_file_path = '' - self._options_block = self.new_block('options') def __str__(self): return 'FlowGraph - {}({})'.format(self.get_option('title'), self.get_option('id')) - ############################################## - # TODO: Move these to new generator package - ############################################## def get_imports(self): """ Get a set of all import statements in this flow graph namespace. @@ -87,7 +83,7 @@ class FlowGraph(Element): Returns: a sorted list of variable blocks in order of dependency (indep -> dep) """ - variables = filter(attrgetter('is_variable'), self.iter_enabled_blocks()) + variables = [block for block in self.iter_enabled_blocks() if block.is_variable] return expr_utils.sort_objects(variables, methodcaller('get_id'), methodcaller('get_var_make')) def get_parameters(self): @@ -97,54 +93,53 @@ class FlowGraph(Element): Returns: a list of parameterized variables """ - parameters = filter(lambda b: _parameter_matcher.match(b.get_key()), self.iter_enabled_blocks()) + parameters = [b for b in self.iter_enabled_blocks() if _parameter_matcher.match(b.key)] return parameters def get_monitors(self): """ Get a list of all ControlPort monitors """ - monitors = filter(lambda b: _monitors_searcher.search(b.get_key()), - self.iter_enabled_blocks()) + monitors = [b for b in self.iter_enabled_blocks() if _monitors_searcher.search(b.key)] return monitors def get_python_modules(self): """Iterate over custom code block ID and Source""" for block in self.iter_enabled_blocks(): - if block.get_key() == 'epy_module': + if block.key == 'epy_module': yield block.get_id(), block.get_param('source_code').get_value() def get_bussink(self): - bussink = filter(lambda b: _bussink_searcher.search(b.get_key()), self.get_enabled_blocks()) + bussink = [b for b in self.get_enabled_blocks() if _bussink_searcher.search(b.key)] for i in bussink: - for j in i.get_params(): - if j.get_name() == 'On/Off' and j.get_value() == 'on': + for j in i.params.values(): + if j.name == 'On/Off' and j.get_value() == 'on': return True return False def get_bussrc(self): - bussrc = filter(lambda b: _bussrc_searcher.search(b.get_key()), self.get_enabled_blocks()) + bussrc = [b for b in self.get_enabled_blocks() if _bussrc_searcher.search(b.key)] for i in bussrc: - for j in i.get_params(): - if j.get_name() == 'On/Off' and j.get_value() == 'on': + for j in i.params.values(): + if j.name == 'On/Off' and j.get_value() == 'on': return True return False def get_bus_structure_sink(self): - bussink = filter(lambda b: _bus_struct_sink_searcher.search(b.get_key()), self.get_enabled_blocks()) + bussink = [b for b in self.get_enabled_blocks() if _bus_struct_sink_searcher.search(b.key)] return bussink def get_bus_structure_src(self): - bussrc = filter(lambda b: _bus_struct_src_searcher.search(b.get_key()), self.get_enabled_blocks()) + bussrc = [b for b in self.get_enabled_blocks() if _bus_struct_src_searcher.search(b.key)] return bussrc def iter_enabled_blocks(self): """ Get an iterator of all blocks that are enabled and not bypassed. """ - return ifilter(methodcaller('get_enabled'), self.blocks) + return (block for block in self.blocks if block.enabled) def get_enabled_blocks(self): """ @@ -162,7 +157,7 @@ class FlowGraph(Element): Returns: a list of blocks """ - return filter(methodcaller('get_bypassed'), self.blocks) + return [block for block in self.blocks if block.get_bypassed()] def get_enabled_connections(self): """ @@ -171,7 +166,7 @@ class FlowGraph(Element): Returns: a list of connections """ - return filter(methodcaller('get_enabled'), self.connections) + return [connection for connection in self.connections if connection.enabled] def get_option(self, key): """ @@ -196,19 +191,6 @@ class FlowGraph(Element): raise KeyError('No block with ID {!r}'.format(id)) def get_elements(self): - """ - Get a list of all the elements. - Always ensure that the options block is in the list (only once). - - Returns: - the element list - """ - options_block_count = self.blocks.count(self._options_block) - if not options_block_count: - self.blocks.append(self._options_block) - for i in range(options_block_count-1): - self.blocks.remove(self._options_block) - return self.blocks + self.connections get_children = get_elements @@ -217,26 +199,22 @@ class FlowGraph(Element): """ Flag the namespace to be renewed. """ - self.renew_namespace() - for child in chain(self.blocks, self.connections): - child.rewrite() - - self.bus_ports_rewrite() + Element.rewrite(self) def renew_namespace(self): namespace = {} # Load imports for expr in self.get_imports(): try: - exec expr in namespace + exec(expr, namespace) except: pass for id, expr in self.get_python_modules(): try: module = imp.new_module(id) - exec expr in module.__dict__ + exec(expr, module.__dict__) namespace[id] = module except: pass @@ -283,7 +261,7 @@ class FlowGraph(Element): # Add/remove stuff ############################################## - def new_block(self, key): + def new_block(self, key, **kwargs): """ Get a new block of the specified key. Add the block to the list of elements. @@ -294,8 +272,10 @@ class FlowGraph(Element): Returns: the new block or None if not found """ + if key == 'options': + return self._options_block try: - block = self.platform.get_new_block(self, key) + block = self.parent_platform.get_new_block(self, key, **kwargs) self.blocks.append(block) except KeyError: block = None @@ -314,8 +294,8 @@ class FlowGraph(Element): the new connection """ - connection = self.platform.Connection( - flow_graph=self, porta=porta, portb=portb) + connection = self.parent_platform.Connection( + parent=self, porta=porta, portb=portb) self.connections.append(connection) return connection @@ -326,22 +306,25 @@ class FlowGraph(Element): If the element is a block, remove its connections. If the element is a connection, just remove the connection. """ + if element is self._options_block: + return + if element.is_port: # Found a port, set to parent signal block - element = element.get_parent() + element = element.parent if element in self.blocks: # Remove block, remove all involved connections for port in element.get_ports(): - map(self.remove_element, port.get_connections()) + for connection in port.get_connections(): + self.remove_element(connection) self.blocks.remove(element) elif element in self.connections: if element.is_bus(): - cons_list = [] - for i in map(lambda a: a.get_connections(), element.get_source().get_associated_ports()): - cons_list.extend(i) - map(self.remove_element, cons_list) + for port in element.source_port.get_associated_ports(): + for connection in port.get_connections(): + self.remove_element(connection) self.connections.remove(element) ############################################## @@ -357,25 +340,24 @@ class FlowGraph(Element): """ # sort blocks and connections for nicer diffs blocks = sorted(self.blocks, key=lambda b: ( - b.get_key() != 'options', # options to the front - not b.get_key().startswith('variable'), # then vars + b.key != 'options', # options to the front + not b.key.startswith('variable'), # then vars str(b) )) connections = sorted(self.connections, key=str) - n = odict() + n = collections.OrderedDict() n['timestamp'] = self._timestamp n['block'] = [b.export_data() for b in blocks] n['connection'] = [c.export_data() for c in connections] - instructions = odict({ - 'created': '.'.join(self.get_parent().config.version_parts), - 'format': FLOW_GRAPH_FILE_FORMAT_VERSION, - }) - return odict({'flow_graph': n, '_instructions': instructions}) + instructions = collections.OrderedDict() + instructions['created'] = '.'.join(self.parent.config.version_parts) + instructions['format'] = FLOW_GRAPH_FILE_FORMAT_VERSION + return {'flow_graph': n, '_instructions': instructions} def import_data(self, n): """ Import blocks and connections into this flow graph. - Clear this flowgraph of all previous blocks and connections. + Clear this flow graph of all previous blocks and connections. Any blocks or connections in error will be ignored. Args: @@ -386,38 +368,37 @@ class FlowGraph(Element): del self.connections[:] # set file format try: - instructions = n.find('_instructions') or {} + instructions = n.get('_instructions', {}) file_format = int(instructions.get('format', '0')) or _guess_file_format_1(n) except: file_format = 0 - fg_n = n and n.find('flow_graph') or odict() # use blank data if none provided - self._timestamp = fg_n.find('timestamp') or time.ctime() + fg_n = n and n.get('flow_graph', {}) # use blank data if none provided + self._timestamp = fg_n.get('timestamp', time.ctime()) # build the blocks - self._options_block = self.new_block('options') - for block_n in fg_n.findall('block'): - key = block_n.find('key') - block = self._options_block if key == 'options' else self.new_block(key) + self.blocks.append(self._options_block) + for block_n in fg_n.get('block', []): + key = block_n['key'] + block = self.new_block(key) if not block: # we're before the initial fg update(), so no evaluated values! # --> use raw value instead path_param = self._options_block.get_param('hier_block_src_path') - file_path = self.platform.find_file_in_paths( + file_path = self.parent_platform.find_file_in_paths( filename=key + '.grc', paths=path_param.get_value(), cwd=self.grc_file_path ) if file_path: # grc file found. load and get block - self.platform.load_and_generate_flow_graph(file_path) + self.parent_platform.load_and_generate_flow_graph(file_path) block = self.new_block(key) # can be None if not block: # looks like this block key cannot be found # create a dummy block instead - block = self.new_block('dummy_block') - # Ugly ugly ugly - _initialize_dummy_block(block, block_n) + block = self.new_block('_dummy', missing_key=key, + params_n=block_n.get('param', [])) print('Block key "%s" not found' % key) block.import_data(block_n) @@ -426,26 +407,26 @@ class FlowGraph(Element): # build the connections def verify_and_get_port(key, block, dir): - ports = block.get_sinks() if dir == 'sink' else block.get_sources() + ports = block.sinks if dir == 'sink' else block.sources for port in ports: - if key == port.get_key(): + if key == port.key: break - if not key.isdigit() and port.get_type() == '' and key == port.get_name(): + if not key.isdigit() and port.get_type() == '' and key == port.name: break else: if block.is_dummy_block: - port = _dummy_block_add_port(block, key, dir) + port = block.add_missing_port(key, dir) else: raise LookupError('%s key %r not in %s block keys' % (dir, key, dir)) return port errors = False - for connection_n in fg_n.findall('connection'): + for connection_n in fg_n.get('connection', []): # get the block ids and port keys - source_block_id = connection_n.find('source_block_id') - sink_block_id = connection_n.find('sink_block_id') - source_key = connection_n.find('source_key') - sink_key = connection_n.find('sink_key') + source_block_id = connection_n.get('source_block_id') + sink_block_id = connection_n.get('sink_block_id') + source_key = connection_n.get('source_key') + sink_key = connection_n.get('sink_key') try: source_block = self.get_block(source_block_id) sink_block = self.get_block(sink_block_id) @@ -468,61 +449,6 @@ class FlowGraph(Element): self.rewrite() # global rewrite return errors - ############################################## - # Needs to go - ############################################## - def bus_ports_rewrite(self): - # todo: move to block.rewrite() - for block in self.blocks: - for direc in ['source', 'sink']: - if direc == 'source': - get_p = block.get_sources - get_p_gui = block.get_sources_gui - bus_structure = block.form_bus_structure('source') - else: - get_p = block.get_sinks - get_p_gui = block.get_sinks_gui - bus_structure = block.form_bus_structure('sink') - - if 'bus' in map(lambda a: a.get_type(), get_p_gui()): - if len(get_p_gui()) > len(bus_structure): - times = range(len(bus_structure), len(get_p_gui())) - for i in times: - for connect in get_p_gui()[-1].get_connections(): - block.get_parent().remove_element(connect) - get_p().remove(get_p_gui()[-1]) - elif len(get_p_gui()) < len(bus_structure): - n = {'name': 'bus', 'type': 'bus'} - if True in map( - lambda a: isinstance(a.get_nports(), int), - get_p()): - n['nports'] = str(1) - - times = range(len(get_p_gui()), len(bus_structure)) - - for i in times: - n['key'] = str(len(get_p())) - n = odict(n) - port = block.get_parent().get_parent().Port( - block=block, n=n, dir=direc) - get_p().append(port) - - if 'bus' in map(lambda a: a.get_type(), - block.get_sources_gui()): - for i in range(len(block.get_sources_gui())): - if len(block.get_sources_gui()[ - i].get_connections()) > 0: - source = block.get_sources_gui()[i] - sink = [] - - for j in range(len(source.get_connections())): - sink.append( - source.get_connections()[j].get_sink()) - for elt in source.get_connections(): - self.remove_element(elt) - for j in sink: - self.connect(source, j) - def _update_old_message_port_keys(source_key, sink_key, source_block, sink_block): """ @@ -539,10 +465,10 @@ def _update_old_message_port_keys(source_key, sink_key, source_block, sink_block """ try: # get ports using the "old way" (assuming liner indexed keys) - source_port = source_block.get_sources()[int(source_key)] - sink_port = sink_block.get_sinks()[int(sink_key)] + source_port = source_block.sources[int(source_key)] + sink_port = sink_block.sinks[int(sink_key)] if source_port.get_type() == "message" and sink_port.get_type() == "message": - source_key, sink_key = source_port.get_key(), sink_port.get_key() + source_key, sink_key = source_port.key, sink_port.key except (ValueError, IndexError): pass return source_key, sink_key # do nothing @@ -554,39 +480,11 @@ def _guess_file_format_1(n): """ try: has_non_numeric_message_keys = any(not ( - connection_n.find('source_key').isdigit() and - connection_n.find('sink_key').isdigit() - ) for connection_n in n.find('flow_graph').findall('connection')) + connection_n.get('source_key', '').isdigit() and + connection_n.get('sink_key', '').isdigit() + ) for connection_n in n.get('flow_graph', []).get('connection', [])) if has_non_numeric_message_keys: return 1 except: pass return 0 - - -def _initialize_dummy_block(block, block_n): - """ - This is so ugly... dummy-fy a block - Modify block object to get the behaviour for a missing block - """ - - block._key = block_n.find('key') - block.is_dummy_block = lambda: True - block.is_valid = lambda: False - block.get_enabled = lambda: False - for param_n in block_n.findall('param'): - if param_n['key'] not in block.get_param_keys(): - new_param_n = odict({'key': param_n['key'], 'name': param_n['key'], 'type': 'string'}) - params = block.get_parent().get_parent().Param(block=block, n=new_param_n) - block.get_params().append(params) - - -def _dummy_block_add_port(block, key, dir): - """ This is so ugly... Add a port to a dummy-field block """ - port_n = odict({'name': '?', 'key': key, 'type': ''}) - port = block.get_parent().get_parent().Port(block=block, n=port_n, dir=dir) - if port.is_source: - block.get_sources().append(port) - else: - block.get_sinks().append(port) - return port diff --git a/grc/core/Messages.py b/grc/core/Messages.py index 8daa12c33f..c2d216ef61 100644 --- a/grc/core/Messages.py +++ b/grc/core/Messages.py @@ -16,9 +16,10 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +from __future__ import absolute_import + import traceback import sys -import os # A list of functions that can receive a message. MESSENGERS_LIST = list() @@ -124,8 +125,8 @@ def send_fail_save(file_path): send('>>> Error: Cannot save: %s\n' % file_path) -def send_fail_connection(): - send('>>> Error: Cannot create connection.\n') +def send_fail_connection(msg=''): + send('>>> Error: Cannot create connection.\n' + ('\t' + str(msg) if msg else '')) def send_fail_load_preferences(prefs_file_path): diff --git a/grc/core/Param.py b/grc/core/Param.py index d0af9792b8..31393b1d79 100644 --- a/grc/core/Param.py +++ b/grc/core/Param.py @@ -17,20 +17,19 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import + import ast -import weakref import re +import collections + +from six.moves import builtins, filter, map, range, zip from . import Constants -from .Constants import VECTOR_TYPES, COMPLEX_TYPES, REAL_TYPES, INT_TYPES -from .Element import Element -from .utils import odict +from .Element import Element, nop_write # Blacklist certain ids, its not complete, but should help -import __builtin__ - - -ID_BLACKLIST = ['self', 'options', 'gr', 'blks2', 'math', 'firdes'] + dir(__builtin__) +ID_BLACKLIST = ['self', 'options', 'gr', 'blks2', 'math', 'firdes'] + dir(builtins) try: from gnuradio import gr ID_BLACKLIST.extend(attr for attr in dir(gr.top_block()) if not attr.startswith('_')) @@ -41,86 +40,6 @@ _check_id_matcher = re.compile('^[a-z|A-Z]\w*$') _show_id_matcher = re.compile('^(variable\w*|parameter|options|notebook)$') -def _get_keys(lst): - return [elem.get_key() for elem in lst] - - -def _get_elem(lst, key): - try: - return lst[_get_keys(lst).index(key)] - except ValueError: - raise ValueError('Key "{}" not found in {}.'.format(key, _get_keys(lst))) - - -def num_to_str(num): - """ Display logic for numbers """ - def eng_notation(value, fmt='g'): - """Convert a number to a string in engineering notation. E.g., 5e-9 -> 5n""" - template = '{:' + fmt + '}{}' - magnitude = abs(value) - for exp, symbol in zip(range(9, -15-1, -3), 'GMk munpf'): - factor = 10 ** exp - if magnitude >= factor: - return template.format(value / factor, symbol.strip()) - return template.format(value, '') - - if isinstance(num, COMPLEX_TYPES): - num = complex(num) # Cast to python complex - if num == 0: - return '0' - output = eng_notation(num.real) if num.real else '' - output += eng_notation(num.imag, '+g' if output else 'g') + 'j' if num.imag else '' - return output - else: - return str(num) - - -class Option(Element): - - def __init__(self, param, n): - Element.__init__(self, param) - self._name = n.find('name') - self._key = n.find('key') - self._opts = dict() - opts = n.findall('opt') - # Test against opts when non enum - if not self.get_parent().is_enum() and opts: - raise Exception('Options for non-enum types cannot have sub-options') - # Extract opts - for opt in opts: - # Separate the key:value - try: - key, value = opt.split(':') - except: - raise Exception('Error separating "{}" into key:value'.format(opt)) - # Test against repeated keys - if key in self._opts: - raise Exception('Key "{}" already exists in option'.format(key)) - # Store the option - self._opts[key] = value - - def __str__(self): - return 'Option {}({})'.format(self.get_name(), self.get_key()) - - def get_name(self): - return self._name - - def get_key(self): - return self._key - - ############################################## - # Access Opts - ############################################## - def get_opt_keys(self): - return self._opts.keys() - - def get_opt(self, key): - return self._opts[key] - - def get_opts(self): - return self._opts.values() - - class TemplateArg(object): """ A cheetah template argument created from a param. @@ -130,10 +49,12 @@ class TemplateArg(object): """ def __init__(self, param): - self._param = weakref.proxy(param) + self._param = param def __getitem__(self, item): - return str(self._param.get_opt(item)) if self._param.is_enum() else NotImplemented + param = self._param + opts = param.options_opts[param.get_value()] + return str(opts[item]) if param.is_enum() else NotImplemented def __str__(self): return str(self._param.to_code()) @@ -146,181 +67,66 @@ class Param(Element): is_param = True - def __init__(self, block, n): - """ - Make a new param from nested data. + def __init__(self, parent, key, name, type='raw', value='', **n): + """Make a new param from nested data""" + super(Param, self).__init__(parent) + self.key = key + self._name = name + self.value = self.default = value + self._type = type - Args: - block: the parent element - n: the nested odict - """ - # If the base key is a valid param key, copy its data and overlay this params data - base_key = n.find('base_key') - if base_key and base_key in block.get_param_keys(): - n_expanded = block.get_param(base_key)._n.copy() - n_expanded.update(n) - n = n_expanded - # Save odict in case this param will be base for another - self._n = n - # Parse the data - self._name = n.find('name') - self._key = n.find('key') - value = n.find('value') or '' - self._type = n.find('type') or 'raw' - self._hide = n.find('hide') or '' - self._tab_label = n.find('tab') or block.get_param_tab_labels()[0] - if self._tab_label not in block.get_param_tab_labels(): - block.get_param_tab_labels().append(self._tab_label) - # Build the param - Element.__init__(self, block) - # Create the Option objects from the n data - self._options = list() + self._hide = n.get('hide', '') + self.tab_label = n.get('tab', Constants.DEFAULT_PARAM_TAB) self._evaluated = None - for option in map(lambda o: Option(param=self, n=o), n.findall('option')): - key = option.get_key() - # Test against repeated keys - if key in self.get_option_keys(): - raise Exception('Key "{}" already exists in options'.format(key)) - # Store the option - self.get_options().append(option) - # Test the enum options - if self.is_enum(): - # Test against options with identical keys - if len(set(self.get_option_keys())) != len(self.get_options()): - raise Exception('Options keys "{}" are not unique.'.format(self.get_option_keys())) - # Test against inconsistent keys in options - opt_keys = self.get_options()[0].get_opt_keys() - for option in self.get_options(): - if set(opt_keys) != set(option.get_opt_keys()): - raise Exception('Opt keys "{}" are not identical across all options.'.format(opt_keys)) - # If a value is specified, it must be in the options keys - if value or value in self.get_option_keys(): - self._value = value - else: - self._value = self.get_option_keys()[0] - if self.get_value() not in self.get_option_keys(): - raise Exception('The value "{}" is not in the possible values of "{}".'.format(self.get_value(), self.get_option_keys())) - else: - self._value = value or '' - self._default = value + + self.options = [] + self.options_names = [] + self.options_opts = {} + self._init_options(options_n=n.get('option', [])) + self._init = False self._hostage_cells = list() self.template_arg = TemplateArg(self) - def get_types(self): - return ( - 'raw', 'enum', - 'complex', 'real', 'float', 'int', - 'complex_vector', 'real_vector', 'float_vector', 'int_vector', - 'hex', 'string', 'bool', - 'file_open', 'file_save', '_multiline', '_multiline_python_external', - 'id', 'stream_id', - 'gui_hint', - 'import', - ) - - def __repr__(self): - """ - Get the repr (nice string format) for this param. - - Returns: - the string representation - """ - ################################################## - # Truncate helper method - ################################################## - def _truncate(string, style=0): - max_len = max(27 - len(self.get_name()), 3) - if len(string) > max_len: - if style < 0: # Front truncate - string = '...' + string[3-max_len:] - elif style == 0: # Center truncate - string = string[:max_len/2 - 3] + '...' + string[-max_len/2:] - elif style > 0: # Rear truncate - string = string[:max_len-3] + '...' - return string - - ################################################## - # Simple conditions - ################################################## - if not self.is_valid(): - return _truncate(self.get_value()) - if self.get_value() in self.get_option_keys(): - return self.get_option(self.get_value()).get_name() - - ################################################## - # Split up formatting by type - ################################################## - # Default center truncate - truncate = 0 - e = self.get_evaluated() - t = self.get_type() - if isinstance(e, bool): - return str(e) - elif isinstance(e, COMPLEX_TYPES): - dt_str = num_to_str(e) - elif isinstance(e, VECTOR_TYPES): - # Vector types - if len(e) > 8: - # Large vectors use code - dt_str = self.get_value() - truncate = 1 - else: - # Small vectors use eval - dt_str = ', '.join(map(num_to_str, e)) - elif t in ('file_open', 'file_save'): - dt_str = self.get_value() - truncate = -1 - else: - # Other types - dt_str = str(e) - - # Done - return _truncate(dt_str, truncate) - - def __repr2__(self): - """ - Get the repr (nice string format) for this param. + def _init_options(self, options_n): + """Create the Option objects from the n data""" + option_keys = set() + for option_n in options_n: + key, name = option_n['key'], option_n['name'] + # Test against repeated keys + if key in option_keys: + raise KeyError('Key "{}" already exists in options'.format(key)) + option_keys.add(key) + # Store the option + self.options.append(key) + self.options_names.append(name) - Returns: - the string representation - """ if self.is_enum(): - return self.get_option(self.get_value()).get_name() - return self.get_value() - - def __str__(self): - return 'Param - {}({})'.format(self.get_name(), self.get_key()) + self._init_enum(options_n) - def get_color(self): - """ - Get the color that represents this param's type. + def _init_enum(self, options_n): + opt_ref = None + for option_n in options_n: + key, opts_raw = option_n['key'], option_n.get('opt', []) + try: + self.options_opts[key] = opts = dict(opt.split(':') for opt in opts_raw) + except TypeError: + raise ValueError('Error separating opts into key:value') + + if opt_ref is None: + opt_ref = set(opts.keys()) + elif opt_ref != set(opts): + raise ValueError('Opt keys ({}) are not identical across all options.' + ''.format(', '.join(opt_ref))) + if not self.value: + self.value = self.default = self.options[0] + elif self.value not in self.options: + self.value = self.default = self.options[0] # TODO: warn + # raise ValueError('The value {!r} is not in the possible values of {}.' + # ''.format(self.get_value(), ', '.join(self.options))) - Returns: - a hex color code. - """ - try: - return { - # Number types - 'complex': Constants.COMPLEX_COLOR_SPEC, - 'real': Constants.FLOAT_COLOR_SPEC, - 'float': Constants.FLOAT_COLOR_SPEC, - 'int': Constants.INT_COLOR_SPEC, - # Vector types - 'complex_vector': Constants.COMPLEX_VECTOR_COLOR_SPEC, - 'real_vector': Constants.FLOAT_VECTOR_COLOR_SPEC, - 'float_vector': Constants.FLOAT_VECTOR_COLOR_SPEC, - 'int_vector': Constants.INT_VECTOR_COLOR_SPEC, - # Special - 'bool': Constants.INT_COLOR_SPEC, - 'hex': Constants.INT_COLOR_SPEC, - 'string': Constants.BYTE_VECTOR_COLOR_SPEC, - 'id': Constants.ID_COLOR_SPEC, - 'stream_id': Constants.ID_COLOR_SPEC, - 'raw': Constants.WILDCARD_COLOR_SPEC, - }[self.get_type()] - except: - return '#FFFFFF' + def __str__(self): + return 'Param - {}({})'.format(self.name, self.key) def get_hide(self): """ @@ -333,20 +139,17 @@ class Param(Element): Returns: hide the hide property string """ - hide = self.get_parent().resolve_dependencies(self._hide).strip() + hide = self.parent.resolve_dependencies(self._hide).strip() if hide: return hide # Hide ID in non variable blocks - if self.get_key() == 'id' and not _show_id_matcher.match(self.get_parent().get_key()): + if self.key == 'id' and not _show_id_matcher.match(self.parent.key): return 'part' # Hide port controllers for type and nports - if self.get_key() in ' '.join(map(lambda p: ' '.join([p._type, p._nports]), - self.get_parent().get_ports())): + if self.key in ' '.join([' '.join([p._type, p._nports]) for p in self.parent.get_ports()]): return 'part' # Hide port controllers for vlen, when == 1 - if self.get_key() in ' '.join(map( - lambda p: p._vlen, self.get_parent().get_ports()) - ): + if self.key in ' '.join(p._vlen for p in self.parent.get_ports()): try: if int(self.get_evaluated()) == 1: return 'part' @@ -360,13 +163,13 @@ class Param(Element): The value must be evaluated and type must a possible type. """ Element.validate(self) - if self.get_type() not in self.get_types(): + if self.get_type() not in Constants.PARAM_TYPE_NAMES: self.add_error_message('Type "{}" is not a possible type.'.format(self.get_type())) self._evaluated = None try: self._evaluated = self.evaluate() - except Exception, e: + except Exception as e: self.add_error_message(str(e)) def get_evaluated(self): @@ -398,22 +201,22 @@ class Param(Element): elif t in ('raw', 'complex', 'real', 'float', 'int', 'hex', 'bool'): # Raise exception if python cannot evaluate this value try: - e = self.get_parent().get_parent().evaluate(v) - except Exception, e: + e = self.parent_flowgraph.evaluate(v) + except Exception as e: raise Exception('Value "{}" cannot be evaluated:\n{}'.format(v, e)) # Raise an exception if the data is invalid if t == 'raw': return e elif t == 'complex': - if not isinstance(e, COMPLEX_TYPES): + if not isinstance(e, Constants.COMPLEX_TYPES): raise Exception('Expression "{}" is invalid for type complex.'.format(str(e))) return e elif t == 'real' or t == 'float': - if not isinstance(e, REAL_TYPES): + if not isinstance(e, Constants.REAL_TYPES): raise Exception('Expression "{}" is invalid for type float.'.format(str(e))) return e elif t == 'int': - if not isinstance(e, INT_TYPES): + if not isinstance(e, Constants.INT_TYPES): raise Exception('Expression "{}" is invalid for type integer.'.format(str(e))) return e elif t == 'hex': @@ -433,29 +236,29 @@ class Param(Element): v = '()' # Raise exception if python cannot evaluate this value try: - e = self.get_parent().get_parent().evaluate(v) - except Exception, e: + e = self.parent.parent.evaluate(v) + except Exception as e: raise Exception('Value "{}" cannot be evaluated:\n{}'.format(v, e)) # Raise an exception if the data is invalid if t == 'complex_vector': - if not isinstance(e, VECTOR_TYPES): + if not isinstance(e, Constants.VECTOR_TYPES): self._lisitify_flag = True e = [e] - if not all([isinstance(ei, COMPLEX_TYPES) for ei in e]): + if not all([isinstance(ei, Constants.COMPLEX_TYPES) for ei in e]): raise Exception('Expression "{}" is invalid for type complex vector.'.format(str(e))) return e elif t == 'real_vector' or t == 'float_vector': - if not isinstance(e, VECTOR_TYPES): + if not isinstance(e, Constants.VECTOR_TYPES): self._lisitify_flag = True e = [e] - if not all([isinstance(ei, REAL_TYPES) for ei in e]): + if not all([isinstance(ei, Constants.REAL_TYPES) for ei in e]): raise Exception('Expression "{}" is invalid for type float vector.'.format(str(e))) return e elif t == 'int_vector': - if not isinstance(e, VECTOR_TYPES): + if not isinstance(e, Constants.VECTOR_TYPES): self._lisitify_flag = True e = [e] - if not all([isinstance(ei, INT_TYPES) for ei in e]): + if not all([isinstance(ei, Constants.INT_TYPES) for ei in e]): raise Exception('Expression "{}" is invalid for type integer vector.'.format(str(e))) return e ######################### @@ -464,7 +267,7 @@ class Param(Element): elif t in ('string', 'file_open', 'file_save', '_multiline', '_multiline_python_external'): # Do not check if file/directory exists, that is a runtime issue try: - e = self.get_parent().get_parent().evaluate(v) + e = self.parent.parent.evaluate(v) if not isinstance(e, str): raise Exception() except: @@ -495,16 +298,16 @@ class Param(Element): elif t == 'stream_id': # Get a list of all stream ids used in the virtual sinks ids = [param.get_value() for param in filter( - lambda p: p.get_parent().is_virtual_sink(), + lambda p: p.parent.is_virtual_sink(), self.get_all_params(t), )] # Check that the virtual sink's stream id is unique - if self.get_parent().is_virtual_sink(): + if self.parent.is_virtual_sink(): # Id should only appear once, or zero times if block is disabled if ids.count(v) > 1: raise Exception('Stream ID "{}" is not unique.'.format(v)) # Check that the virtual source's steam id is found - if self.get_parent().is_virtual_source(): + if self.parent.is_virtual_source(): if v not in ids: raise Exception('Stream ID "{}" is not found.'.format(v)) return v @@ -552,12 +355,12 @@ class Param(Element): # New namespace n = dict() try: - exec v in n + exec(v, n) except ImportError: raise Exception('Import "{}" failed.'.format(v)) except Exception: raise Exception('Bad import syntax: "{}".'.format(v)) - return filter(lambda k: str(k) != '__builtins__', n.keys()) + return [k for k in list(n.keys()) if str(k) != '__builtins__'] ######################### else: @@ -601,62 +404,46 @@ class Param(Element): Returns: a list of params """ - return sum([filter(lambda p: p.get_type() == type, block.get_params()) for block in self.get_parent().get_parent().get_enabled_blocks()], []) + params = [] + for block in self.parent_flowgraph.get_enabled_blocks(): + params.extend(p for p in block.params.values() if p.get_type() == type) + return params def is_enum(self): return self._type == 'enum' def get_value(self): - value = self._value - if self.is_enum() and value not in self.get_option_keys(): - value = self.get_option_keys()[0] + value = self.value + if self.is_enum() and value not in self.options: + value = self.options[0] self.set_value(value) return value def set_value(self, value): # Must be a string - self._value = str(value) + self.value = str(value) def set_default(self, value): - if self._default == self._value: + if self.default == self.value: self.set_value(value) - self._default = str(value) + self.default = str(value) def get_type(self): - return self.get_parent().resolve_dependencies(self._type) + return self.parent.resolve_dependencies(self._type) def get_tab_label(self): - return self._tab_label - - def get_name(self): - return self.get_parent().resolve_dependencies(self._name).strip() + return self.tab_label - def get_key(self): - return self._key + @nop_write + @property + def name(self): + return self.parent.resolve_dependencies(self._name).strip() ############################################## # Access Options ############################################## - def get_option_keys(self): - return _get_keys(self.get_options()) - - def get_option(self, key): - return _get_elem(self.get_options(), key) - - def get_options(self): - return self._options - - ############################################## - # Access Opts - ############################################## - def get_opt_keys(self): - return self.get_option(self.get_value()).get_opt_keys() - - def get_opt(self, key): - return self.get_option(self.get_value()).get_opt(key) - - def get_opts(self): - return self.get_option(self.get_value()).get_opts() + def opt_value(self, key): + return self.options_opts[self.get_value()][key] ############################################## # Import/Export Methods @@ -668,7 +455,7 @@ class Param(Element): Returns: a nested data odict """ - n = odict() - n['key'] = self.get_key() + n = collections.OrderedDict() + n['key'] = self.key n['value'] = self.get_value() return n diff --git a/grc/core/ParseXML.py b/grc/core/ParseXML.py index c9f6541ee7..163289ba06 100644 --- a/grc/core/ParseXML.py +++ b/grc/core/ParseXML.py @@ -17,9 +17,13 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import + from lxml import etree -from .utils import odict +import six +from six.moves import map + xml_failures = {} etree.set_default_parser(etree.XMLParser(remove_comments=True)) @@ -75,17 +79,35 @@ def from_file(xml_file): the nested data with grc version information """ xml = etree.parse(xml_file) - nested_data = _from_file(xml.getroot()) + + tag, nested_data = _from_file(xml.getroot()) + nested_data = {tag: nested_data, '_instructions': {}} # Get the embedded instructions and build a dictionary item - nested_data['_instructions'] = {} xml_instructions = xml.xpath('/processing-instruction()') - for inst in filter(lambda i: i.target == 'grc', xml_instructions): - nested_data['_instructions'] = odict(inst.attrib) + for inst in xml_instructions: + if inst.target != 'grc': + continue + nested_data['_instructions'] = dict(inst.attrib) return nested_data -def _from_file(xml): +WANT_A_LIST = { + '/block': 'import callback param check sink source'.split(), + '/block/param_tab_order': 'tab'.split(), + '/block/param': 'option'.split(), + '/block/param/option': 'opt'.split(), + '/flow_graph': 'block connection'.split(), + '/flow_graph/block': 'param'.split(), + '/cat': 'cat block'.split(), + '/cat/cat': 'cat block'.split(), + '/cat/cat/cat': 'cat block'.split(), + '/cat/cat/cat/cat': 'cat block'.split(), + '/domain': 'connection'.split(), +} + + +def _from_file(xml, parent_tag=''): """ Recursively parse the xml tree into nested data format. @@ -96,21 +118,24 @@ def _from_file(xml): the nested data """ tag = xml.tag + tag_path = parent_tag + '/' + tag + if not len(xml): - return odict({tag: xml.text or ''}) # store empty tags (text is None) as empty string - nested_data = odict() + return tag, xml.text or '' # store empty tags (text is None) as empty string + + nested_data = {} for elem in xml: - key, value = _from_file(elem).items()[0] - if key in nested_data: - nested_data[key].append(value) + key, value = _from_file(elem, tag_path) + + if key in WANT_A_LIST.get(tag_path, []): + try: + nested_data[key].append(value) + except KeyError: + nested_data[key] = [value] else: - nested_data[key] = [value] - # Delistify if the length of values is 1 - for key, values in nested_data.iteritems(): - if len(values) == 1: - nested_data[key] = values[0] + nested_data[key] = value - return odict({tag: nested_data}) + return tag, nested_data def to_file(nested_data, xml_file): @@ -127,7 +152,7 @@ def to_file(nested_data, xml_file): if instructions: xml_data += etree.tostring(etree.ProcessingInstruction( 'grc', ' '.join( - "{0}='{1}'".format(*item) for item in instructions.iteritems()) + "{0}='{1}'".format(*item) for item in six.iteritems(instructions)) ), xml_declaration=True, pretty_print=True, encoding='utf-8') xml_data += etree.tostring(_to_file(nested_data)[0], pretty_print=True, encoding='utf-8') @@ -146,14 +171,14 @@ def _to_file(nested_data): the xml tree filled with child nodes """ nodes = list() - for key, values in nested_data.iteritems(): + for key, values in six.iteritems(nested_data): # Listify the values if not a list if not isinstance(values, (list, set, tuple)): values = [values] for value in values: node = etree.Element(key) - if isinstance(value, (str, unicode)): - node.text = unicode(value) + if isinstance(value, (str, six.text_type)): + node.text = six.text_type(value) else: node.extend(_to_file(value)) nodes.append(node) diff --git a/grc/core/Platform.py b/grc/core/Platform.py index 0dc6eb055a..7ef944b3aa 100644 --- a/grc/core/Platform.py +++ b/grc/core/Platform.py @@ -17,9 +17,14 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import, print_function + import os import sys +import six +from six.moves import range + from . import ParseXML, Messages, Constants from .Config import Config @@ -27,31 +32,22 @@ from .Element import Element from .generator import Generator from .FlowGraph import FlowGraph from .Connection import Connection -from .Block import Block -from .Port import Port +from . import Block +from .Port import Port, PortClone from .Param import Param -from .utils import odict, extract_docs +from .utils import extract_docs class Platform(Element): - Config = Config - Generator = Generator - FlowGraph = FlowGraph - Connection = Connection - Block = Block - Port = Port - Param = Param - is_platform = True def __init__(self, *args, **kwargs): """ Make a platform for GNU Radio """ - Element.__init__(self) + Element.__init__(self, parent=None) self.config = self.Config(*args, **kwargs) - self.block_docstrings = {} self.block_docstrings_loaded_callback = lambda: None # dummy to be replaced by BlockTreeWindow @@ -60,18 +56,19 @@ class Platform(Element): callback_finished=lambda: self.block_docstrings_loaded_callback() ) - # Create a dummy flow graph for the blocks - self._flow_graph = Element(self) - self._flow_graph.connections = [] - - self.blocks = odict() - self._blocks_n = odict() + self.blocks = {} + self._blocks_n = {} self._block_categories = {} self.domains = {} self.connection_templates = {} self._auto_hier_block_generate_chain = set() + # Create a dummy flow graph for the blocks + self._flow_graph = Element.__new__(FlowGraph) + Element.__init__(self._flow_graph, self) + self._flow_graph.connections = [] + self.build_block_library() def __str__(self): @@ -127,12 +124,13 @@ class Platform(Element): Messages.send('>>> Generate Error: {}: {}\n'.format(file_path, str(e))) return False - self.load_block_xml(generator.get_file_path_xml()) + self.load_block_xml(generator.file_path_xml) return True def build_block_library(self): """load the blocks and block tree from the search paths""" self._docstring_extractor.start() + # Reset self.blocks.clear() self._blocks_n.clear() @@ -154,10 +152,11 @@ class Platform(Element): # print >> sys.stderr, 'Warning: Block validation failed:\n\t%s\n\tIgnoring: %s' % (e, xml_file) pass except Exception as e: - print >> sys.stderr, 'Warning: XML parsing failed:\n\t%r\n\tIgnoring: %s' % (e, xml_file) + raise + print('Warning: XML parsing failed:\n\t%r\n\tIgnoring: %s' % (e, xml_file), file=sys.stderr) # Add blocks to block tree - for key, block in self.blocks.iteritems(): + for key, block in six.iteritems(self.blocks): category = self._block_categories.get(key, block.category) # Blocks with empty categories are hidden if not category: @@ -179,26 +178,27 @@ class Platform(Element): yield block_path elif os.path.isdir(block_path): for dirpath, dirnames, filenames in os.walk(block_path): - for filename in sorted(filter(lambda f: f.endswith('.xml'), filenames)): + for filename in sorted(f for f in filenames if f.endswith('.xml')): yield os.path.join(dirpath, filename) def load_block_xml(self, xml_file): """Load block description from xml file""" # Validate and import ParseXML.validate_dtd(xml_file, Constants.BLOCK_DTD) - n = ParseXML.from_file(xml_file).find('block') + n = ParseXML.from_file(xml_file).get('block', {}) n['block_wrapper_path'] = xml_file # inject block wrapper path - # Get block instance and add it to the list of blocks - block = self.Block(self._flow_graph, n) - key = block.get_key() + key = n.pop('key') + if key in self.blocks: - print >> sys.stderr, 'Warning: Block with key "{}" already exists.\n\tIgnoring: {}'.format(key, xml_file) - else: # Store the block - self.blocks[key] = block - self._blocks_n[key] = n + print('Warning: Block with key "{}" already exists.\n' + '\tIgnoring: {}'.format(key, xml_file), file=sys.stderr) + return + # Store the block + self.blocks[key] = block = self.get_new_block(self._flow_graph, key, **n) + self._blocks_n[key] = n self._docstring_extractor.query( - block.get_key(), + key, block.get_imports(raw=True), block.get_make(raw=True) ) @@ -210,62 +210,62 @@ class Platform(Element): path = [] def load_category(cat_n): - path.append(cat_n.find('name').strip()) - for block_key in cat_n.findall('block'): + path.append(cat_n.get('name').strip()) + for block_key in cat_n.get('block', []): if block_key not in self._block_categories: self._block_categories[block_key] = list(path) - for sub_cat_n in cat_n.findall('cat'): + for sub_cat_n in cat_n.get('cat', []): load_category(sub_cat_n) path.pop() - load_category(xml.find('cat')) + load_category(xml.get('cat', {})) def load_domain_xml(self, xml_file): """Load a domain properties and connection templates from XML""" ParseXML.validate_dtd(xml_file, Constants.DOMAIN_DTD) - n = ParseXML.from_file(xml_file).find('domain') + n = ParseXML.from_file(xml_file).get('domain') - key = n.find('key') + key = n.get('key') if not key: - print >> sys.stderr, 'Warning: Domain with emtpy key.\n\tIgnoring: {}'.format(xml_file) + print('Warning: Domain with emtpy key.\n\tIgnoring: {}'.format(xml_file), file=sys.stderr) return if key in self.domains: # test against repeated keys - print >> sys.stderr, 'Warning: Domain with key "{}" already exists.\n\tIgnoring: {}'.format(key, xml_file) + print('Warning: Domain with key "{}" already exists.\n\tIgnoring: {}'.format(key, xml_file), file=sys.stderr) return - #to_bool = lambda s, d: d if s is None else s.lower() not in ('false', 'off', '0', '') + # to_bool = lambda s, d: d if s is None else s.lower() not in ('false', 'off', '0', '') def to_bool(s, d): if s is not None: return s.lower() not in ('false', 'off', '0', '') return d - color = n.find('color') or '' + color = n.get('color') or '' try: - import gtk # ugly but handy - gtk.gdk.color_parse(color) - except (ValueError, ImportError): + chars_per_color = 2 if len(color) > 4 else 1 + tuple(int(color[o:o + 2], 16) / 255.0 for o in range(1, 3 * chars_per_color, chars_per_color)) + except ValueError: if color: # no color is okay, default set in GUI - print >> sys.stderr, 'Warning: Can\'t parse color code "{}" for domain "{}" '.format(color, key) + print('Warning: Can\'t parse color code "{}" for domain "{}" '.format(color, key), file=sys.stderr) color = None self.domains[key] = dict( - name=n.find('name') or key, - multiple_sinks=to_bool(n.find('multiple_sinks'), True), - multiple_sources=to_bool(n.find('multiple_sources'), False), + name=n.get('name') or key, + multiple_sinks=to_bool(n.get('multiple_sinks'), True), + multiple_sources=to_bool(n.get('multiple_sources'), False), color=color ) - for connection_n in n.findall('connection'): - key = (connection_n.find('source_domain'), connection_n.find('sink_domain')) + for connection_n in n.get('connection', []): + key = (connection_n.get('source_domain'), connection_n.get('sink_domain')) if not all(key): - print >> sys.stderr, 'Warning: Empty domain key(s) in connection template.\n\t{}'.format(xml_file) + print('Warning: Empty domain key(s) in connection template.\n\t{}'.format(xml_file), file=sys.stderr) elif key in self.connection_templates: - print >> sys.stderr, 'Warning: Connection template "{}" already exists.\n\t{}'.format(key, xml_file) + print('Warning: Connection template "{}" already exists.\n\t{}'.format(key, xml_file), file=sys.stderr) else: - self.connection_templates[key] = connection_n.find('make') or '' + self.connection_templates[key] = connection_n.get('make') or '' def _save_docstring_extraction_result(self, key, docstrings): docs = {} - for match, docstring in docstrings.iteritems(): + for match, docstring in six.iteritems(docstrings): if not docstring or match.endswith('_sptr'): continue docstring = docstring.replace('\n\n', '\n').strip() @@ -293,14 +293,48 @@ class Platform(Element): ParseXML.validate_dtd(flow_graph_file, Constants.FLOW_GRAPH_DTD) return ParseXML.from_file(flow_graph_file) + def get_blocks(self): + return list(self.blocks.values()) + + def get_generate_options(self): + gen_opts = self.blocks['options'].get_param('generate_options') + generate_mode_default = gen_opts.get_value() + return [(key, name, key == generate_mode_default) + for key, name in zip(gen_opts.options, gen_opts.options_names)] + + ############################################## + # Factories + ############################################## + Config = Config + Generator = Generator + FlowGraph = FlowGraph + Connection = Connection + block_classes = { + None: Block.Block, # default + 'epy_block': Block.EPyBlock, + '_dummy': Block.DummyBlock, + } + port_classes = { + None: Port, # default + 'clone': PortClone, # default + } + param_classes = { + None: Param, # default + } + def get_new_flow_graph(self): - return self.FlowGraph(platform=self) + return self.FlowGraph(parent=self) - def get_blocks(self): - return self.blocks.values() + def get_new_block(self, parent, key, **kwargs): + cls = self.block_classes.get(key, self.block_classes[None]) + if not kwargs: + kwargs = self._blocks_n[key] + return cls(parent, key=key, **kwargs) - def get_new_block(self, flow_graph, key): - return self.Block(flow_graph, n=self._blocks_n[key]) + def get_new_param(self, parent, **kwargs): + cls = self.param_classes[kwargs.pop('cls_key', None)] + return cls(parent, **kwargs) - def get_colors(self): - return [(name, color) for name, key, sizeof, color in Constants.CORE_TYPES] + def get_new_port(self, parent, **kwargs): + cls = self.port_classes[kwargs.pop('cls_key', None)] + return cls(parent, **kwargs) diff --git a/grc/core/Port.py b/grc/core/Port.py index 88601dc87a..41388df409 100644 --- a/grc/core/Port.py +++ b/grc/core/Port.py @@ -17,8 +17,11 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -from .Constants import DEFAULT_DOMAIN, GR_STREAM_DOMAIN, GR_MESSAGE_DOMAIN -from .Element import Element +from __future__ import absolute_import + +from six.moves import filter + +from .Element import Element, lazy_property from . import Constants @@ -30,7 +33,7 @@ def _get_source_from_virtual_sink_port(vsp): """ try: return _get_source_from_virtual_source_port( - vsp.get_enabled_connections()[0].get_source()) + vsp.get_enabled_connections()[0].source_port) except: raise Exception('Could not resolve source for virtual sink port {}'.format(vsp)) @@ -40,20 +43,20 @@ def _get_source_from_virtual_source_port(vsp, traversed=[]): Recursively resolve source ports over the virtual connections. Keep track of traversed sources to avoid recursive loops. """ - if not vsp.get_parent().is_virtual_source(): + if not vsp.parent.is_virtual_source(): return vsp if vsp in traversed: raise Exception('Loop found when resolving virtual source {}'.format(vsp)) try: return _get_source_from_virtual_source_port( _get_source_from_virtual_sink_port( - filter( # Get all virtual sinks with a matching stream id - lambda vs: vs.get_param('stream_id').get_value() == vsp.get_parent().get_param('stream_id').get_value(), - filter( # Get all enabled blocks that are also virtual sinks + list(filter( # Get all virtual sinks with a matching stream id + lambda vs: vs.get_param('stream_id').get_value() == vsp.parent.get_param('stream_id').get_value(), + list(filter( # Get all enabled blocks that are also virtual sinks lambda b: b.is_virtual_sink(), - vsp.get_parent().get_parent().get_enabled_blocks(), - ), - )[0].get_sinks()[0] + vsp.parent.parent.get_enabled_blocks(), + )), + ))[0].sinks[0] ), traversed + [vsp], ) except: @@ -68,7 +71,7 @@ def _get_sink_from_virtual_source_port(vsp): try: # Could have many connections, but use first return _get_sink_from_virtual_sink_port( - vsp.get_enabled_connections()[0].get_sink()) + vsp.get_enabled_connections()[0].sink_port) except: raise Exception('Could not resolve source for virtual source port {}'.format(vsp)) @@ -78,7 +81,7 @@ def _get_sink_from_virtual_sink_port(vsp, traversed=[]): Recursively resolve sink ports over the virtual connections. Keep track of traversed sinks to avoid recursive loops. """ - if not vsp.get_parent().is_virtual_sink(): + if not vsp.parent.is_virtual_sink(): return vsp if vsp in traversed: raise Exception('Loop found when resolving virtual sink {}'.format(vsp)) @@ -86,12 +89,12 @@ def _get_sink_from_virtual_sink_port(vsp, traversed=[]): return _get_sink_from_virtual_sink_port( _get_sink_from_virtual_source_port( filter( # Get all virtual source with a matching stream id - lambda vs: vs.get_param('stream_id').get_value() == vsp.get_parent().get_param('stream_id').get_value(), - filter( # Get all enabled blocks that are also virtual sinks + lambda vs: vs.get_param('stream_id').get_value() == vsp.parent.get_param('stream_id').get_value(), + list(filter( # Get all enabled blocks that are also virtual sinks lambda b: b.is_virtual_source(), - vsp.get_parent().get_parent().get_enabled_blocks(), - ), - )[0].get_sources()[0] + vsp.parent.parent.get_enabled_blocks(), + )), + )[0].sources[0] ), traversed + [vsp], ) except: @@ -101,8 +104,9 @@ def _get_sink_from_virtual_sink_port(vsp, traversed=[]): class Port(Element): is_port = True + is_clone = False - def __init__(self, block, n, dir): + def __init__(self, parent, direction, **n): """ Make a new port from nested data. @@ -113,50 +117,44 @@ class Port(Element): """ self._n = n if n['type'] == 'message': - n['domain'] = GR_MESSAGE_DOMAIN + n['domain'] = Constants.GR_MESSAGE_DOMAIN + if 'domain' not in n: - n['domain'] = DEFAULT_DOMAIN - elif n['domain'] == GR_MESSAGE_DOMAIN: + n['domain'] = Constants.DEFAULT_DOMAIN + elif n['domain'] == Constants.GR_MESSAGE_DOMAIN: n['key'] = n['name'] n['type'] = 'message' # For port color - if not n.find('key'): - n['key'] = str(next(block.port_counters[dir == 'source'])) # Build the port - Element.__init__(self, block) + Element.__init__(self, parent) # Grab the data - self._name = n['name'] - self._key = n['key'] - self._type = n['type'] or '' - self._domain = n['domain'] - self._hide = n.find('hide') or '' - self._dir = dir + self.name = n['name'] + self.key = n['key'] + self.domain = n.get('domain') + self._type = n.get('type', '') + self.inherit_type = not self._type + self._hide = n.get('hide', '') + self._dir = direction self._hide_evaluated = False # Updated on rewrite() - self._nports = n.find('nports') or '' - self._vlen = n.find('vlen') or '' - self._optional = bool(n.find('optional')) - self._clones = [] # References to cloned ports (for nports > 1) + self._nports = n.get('nports', '') + self._vlen = n.get('vlen', '') + self._optional = bool(n.get('optional')) + self.clones = [] # References to cloned ports (for nports > 1) def __str__(self): if self.is_source: - return 'Source - {}({})'.format(self.get_name(), self.get_key()) + return 'Source - {}({})'.format(self.name, self.key) if self.is_sink: - return 'Sink - {}({})'.format(self.get_name(), self.get_key()) - - def get_types(self): - return Constants.TYPE_TO_SIZEOF.keys() - - def is_type_empty(self): - return not self._n['type'] + return 'Sink - {}({})'.format(self.name, self.key) def validate(self): Element.validate(self) - if self.get_type() not in self.get_types(): + if self.get_type() not in Constants.TYPE_TO_SIZEOF.keys(): self.add_error_message('Type "{}" is not a possible type.'.format(self.get_type())) - platform = self.get_parent().get_parent().get_parent() - if self.get_domain() not in platform.domains: - self.add_error_message('Domain key "{}" is not registered.'.format(self.get_domain())) + platform = self.parent_platform + if self.domain not in platform.domains: + self.add_error_message('Domain key "{}" is not registered.'.format(self.domain)) if not self.get_enabled_connections() and not self.get_optional(): self.add_error_message('Port is not connected.') @@ -164,7 +162,7 @@ class Port(Element): """ Handle the port cloning for virtual blocks. """ - if self.is_type_empty(): + if self.inherit_type: try: # Clone type and vlen source = self.resolve_empty_type() @@ -176,44 +174,44 @@ class Port(Element): self._vlen = '' Element.rewrite(self) - hide = self.get_parent().resolve_dependencies(self._hide).strip().lower() + hide = self.parent.resolve_dependencies(self._hide).strip().lower() self._hide_evaluated = False if hide in ('false', 'off', '0') else bool(hide) # Update domain if was deduced from (dynamic) port type type_ = self.get_type() - if self._domain == GR_STREAM_DOMAIN and type_ == "message": - self._domain = GR_MESSAGE_DOMAIN - self._key = self._name - if self._domain == GR_MESSAGE_DOMAIN and type_ != "message": - self._domain = GR_STREAM_DOMAIN - self._key = '0' # Is rectified in rewrite() + if self.domain == Constants.GR_STREAM_DOMAIN and type_ == "message": + self.domain = Constants.GR_MESSAGE_DOMAIN + self.key = self.name + if self.domain == Constants.GR_MESSAGE_DOMAIN and type_ != "message": + self.domain = Constants.GR_STREAM_DOMAIN + self.key = '0' # Is rectified in rewrite() def resolve_virtual_source(self): - if self.get_parent().is_virtual_sink(): + if self.parent.is_virtual_sink(): return _get_source_from_virtual_sink_port(self) - if self.get_parent().is_virtual_source(): + if self.parent.is_virtual_source(): return _get_source_from_virtual_source_port(self) def resolve_empty_type(self): if self.is_sink: try: src = _get_source_from_virtual_sink_port(self) - if not src.is_type_empty(): + if not src.inherit_type: return src except: pass sink = _get_sink_from_virtual_sink_port(self) - if not sink.is_type_empty(): + if not sink.inherit_type: return sink if self.is_source: try: src = _get_source_from_virtual_source_port(self) - if not src.is_type_empty(): + if not src.inherit_type: return src except: pass sink = _get_sink_from_virtual_source_port(self) - if not sink.is_type_empty(): + if not sink.inherit_type: return sink def get_vlen(self): @@ -224,9 +222,9 @@ class Port(Element): Returns: the vector length or 1 """ - vlen = self.get_parent().resolve_dependencies(self._vlen) + vlen = self.parent.resolve_dependencies(self._vlen) try: - return int(self.get_parent().get_parent().evaluate(vlen)) + return max(1, int(self.parent_flowgraph.evaluate(vlen))) except: return 1 @@ -240,52 +238,17 @@ class Port(Element): the number of ports or 1 """ if self._nports == '': - return '' + return 1 - nports = self.get_parent().resolve_dependencies(self._nports) + nports = self.parent.resolve_dependencies(self._nports) try: - return max(1, int(self.get_parent().get_parent().evaluate(nports))) + return max(1, int(self.parent_flowgraph.evaluate(nports))) except: return 1 def get_optional(self): return bool(self._optional) - def get_color(self): - """ - Get the color that represents this port's type. - Codes differ for ports where the vec length is 1 or greater than 1. - - Returns: - a hex color code. - """ - try: - color = Constants.TYPE_TO_COLOR[self.get_type()] - vlen = self.get_vlen() - if vlen == 1: - return color - color_val = int(color[1:], 16) - r = (color_val >> 16) & 0xff - g = (color_val >> 8) & 0xff - b = (color_val >> 0) & 0xff - dark = (0, 0, 30, 50, 70)[min(4, vlen)] - r = max(r-dark, 0) - g = max(g-dark, 0) - b = max(b-dark, 0) - # TODO: Change this to .format() - return '#%.2x%.2x%.2x' % (r, g, b) - except: - return '#FFFFFF' - - def get_clones(self): - """ - Get the clones of this master port (nports > 1) - - Returns: - a list of ports - """ - return self._clones - def add_clone(self): """ Create a clone of this (master) port and store a reference in self._clones. @@ -297,24 +260,23 @@ class Port(Element): the cloned port """ # Add index to master port name if there are no clones yet - if not self._clones: - self._name = self._n['name'] + '0' + if not self.clones: + self.name = self._n['name'] + '0' # Also update key for none stream ports - if not self._key.isdigit(): - self._key = self._name - - # Prepare a copy of the odict for the clone - n = self._n.copy() - # Remove nports from the key so the copy cannot be a duplicator - if 'nports' in n: - n.pop('nports') - n['name'] = self._n['name'] + str(len(self._clones) + 1) + if not self.key.isdigit(): + self.key = self.name + + name = self._n['name'] + str(len(self.clones) + 1) # Dummy value 99999 will be fixed later - n['key'] = '99999' if self._key.isdigit() else n['name'] + key = '99999' if self.key.isdigit() else name # Clone - port = self.__class__(self.get_parent(), n, self._dir) - self._clones.append(port) + port_factory = self.parent_platform.get_new_port + port = port_factory(self.parent, direction=self._dir, + name=name, key=key, + master=self, cls_key='clone') + + self.clones.append(port) return port def remove_clone(self, port): @@ -322,37 +284,24 @@ class Port(Element): Remove a cloned port (from the list of clones only) Remove the index 0 of the master port name (and key9 if there are no more clones left """ - self._clones.remove(port) + self.clones.remove(port) # Remove index from master port name if there are no more clones - if not self._clones: - self._name = self._n['name'] + if not self.clones: + self.name = self._n['name'] # Also update key for none stream ports - if not self._key.isdigit(): - self._key = self._name - - def get_name(self): - number = '' - if self.get_type() == 'bus': - busses = filter(lambda a: a._dir == self._dir, self.get_parent().get_ports_gui()) - number = str(busses.index(self)) + '#' + str(len(self.get_associated_ports())) - return self._name + number - - def get_key(self): - return self._key + if not self.key.isdigit(): + self.key = self.name - @property + @lazy_property def is_sink(self): return self._dir == 'sink' - @property + @lazy_property def is_source(self): return self._dir == 'source' def get_type(self): - return self.get_parent().resolve_dependencies(self._type) - - def get_domain(self): - return self._domain + return self.parent_block.resolve_dependencies(self._type) def get_hide(self): return self._hide_evaluated @@ -364,8 +313,8 @@ class Port(Element): Returns: a list of connection objects """ - connections = self.get_parent().get_parent().connections - connections = filter(lambda c: c.get_source() is self or c.get_sink() is self, connections) + connections = self.parent_flowgraph.connections + connections = [c for c in connections if c.source_port is self or c.sink_port is self] return connections def get_enabled_connections(self): @@ -375,22 +324,52 @@ class Port(Element): Returns: a list of connection objects """ - return filter(lambda c: c.get_enabled(), self.get_connections()) + return [c for c in self.get_connections() if c.enabled] def get_associated_ports(self): if not self.get_type() == 'bus': return [self] + + block = self.parent_block + if self.is_source: + block_ports = block.sources + bus_structure = block.current_bus_structure['source'] else: - if self.is_source: - get_ports = self.get_parent().get_sources - bus_structure = self.get_parent().current_bus_structure['source'] - else: - get_ports = self.get_parent().get_sinks - bus_structure = self.get_parent().current_bus_structure['sink'] - - ports = [i for i in get_ports() if not i.get_type() == 'bus'] - if bus_structure: - busses = [i for i in get_ports() if i.get_type() == 'bus'] - bus_index = busses.index(self) - ports = filter(lambda a: ports.index(a) in bus_structure[bus_index], ports) - return ports + block_ports = block.sinks + bus_structure = block.current_bus_structure['sink'] + + ports = [i for i in block_ports if not i.get_type() == 'bus'] + if bus_structure: + bus_index = [i for i in block_ports if i.get_type() == 'bus'].index(self) + ports = [p for i, p in enumerate(ports) if i in bus_structure[bus_index]] + return ports + + +class PortClone(Port): + + is_clone = True + + def __init__(self, parent, direction, master, name, key): + """ + Make a new port from nested data. + + Args: + block: the parent element + n: the nested odict + dir: the direction + """ + Element.__init__(self, parent) + self.master = master + self.name = name + self._key = key + self._nports = '1' + + def __getattr__(self, item): + return getattr(self.master, item) + + def add_clone(self): + raise NotImplementedError() + + def remove_clone(self, port): + raise NotImplementedError() + diff --git a/grc/core/generator/FlowGraphProxy.py b/grc/core/generator/FlowGraphProxy.py index 3723005576..23ccf95c4b 100644 --- a/grc/core/generator/FlowGraphProxy.py +++ b/grc/core/generator/FlowGraphProxy.py @@ -16,13 +16,17 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -class FlowGraphProxy(object): +from __future__ import absolute_import +from six.moves import range + + +class FlowGraphProxy(object): # TODO: move this in a refactored Generator def __init__(self, fg): - self._fg = fg + self.orignal_flowgraph = fg def __getattr__(self, item): - return getattr(self._fg, item) + return getattr(self.orignal_flowgraph, item) def get_hier_block_stream_io(self, direction): """ @@ -34,7 +38,7 @@ class FlowGraphProxy(object): Returns: a list of dicts with: type, label, vlen, size, optional """ - return filter(lambda p: p['type'] != "message", self.get_hier_block_io(direction)) + return [p for p in self.get_hier_block_io(direction) if p['type'] != "message"] def get_hier_block_message_io(self, direction): """ @@ -46,7 +50,7 @@ class FlowGraphProxy(object): Returns: a list of dicts with: type, label, vlen, size, optional """ - return filter(lambda p: p['type'] == "message", self.get_hier_block_io(direction)) + return [p for p in self.get_hier_block_io(direction) if p['type'] == "message"] def get_hier_block_io(self, direction): """ @@ -66,12 +70,12 @@ class FlowGraphProxy(object): 'label': str(pad.get_param('label').get_evaluated()), 'type': str(pad.get_param('type').get_evaluated()), 'vlen': str(pad.get_param('vlen').get_value()), - 'size': pad.get_param('type').get_opt('size'), + 'size': pad.get_param('type').opt_value('size'), 'optional': bool(pad.get_param('optional').get_evaluated()), } num_ports = pad.get_param('num_streams').get_evaluated() if num_ports > 1: - for i in xrange(num_ports): + for i in range(num_ports): clone = master.copy() clone['label'] += str(i) ports.append(clone) @@ -86,7 +90,7 @@ class FlowGraphProxy(object): Returns: a list of pad source blocks in this flow graph """ - pads = filter(lambda b: b.get_key() == 'pad_source', self.get_enabled_blocks()) + pads = [b for b in self.get_enabled_blocks() if b.key == 'pad_source'] return sorted(pads, lambda x, y: cmp(x.get_id(), y.get_id())) def get_pad_sinks(self): @@ -96,7 +100,7 @@ class FlowGraphProxy(object): Returns: a list of pad sink blocks in this flow graph """ - pads = filter(lambda b: b.get_key() == 'pad_sink', self.get_enabled_blocks()) + pads = [b for b in self.get_enabled_blocks() if b.key == 'pad_sink'] return sorted(pads, lambda x, y: cmp(x.get_id(), y.get_id())) def get_pad_port_global_key(self, port): @@ -113,14 +117,14 @@ class FlowGraphProxy(object): # using the block param 'type' instead of the port domain here # to emphasize that hier block generation is domain agnostic is_message_pad = pad.get_param('type').get_evaluated() == "message" - if port.get_parent() == pad: + if port.parent == pad: if is_message_pad: key = pad.get_param('label').get_value() else: - key = str(key_offset + int(port.get_key())) + key = str(key_offset + int(port.key)) return key else: # assuming we have either only sources or sinks if not is_message_pad: key_offset += len(pad.get_ports()) - return -1
\ No newline at end of file + return -1 diff --git a/grc/core/generator/Generator.py b/grc/core/generator/Generator.py index 33d50d6b19..8b073293ce 100644 --- a/grc/core/generator/Generator.py +++ b/grc/core/generator/Generator.py @@ -16,11 +16,16 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +from __future__ import absolute_import + import codecs import os import tempfile +import operator +import collections from Cheetah.Template import Template +import six from .FlowGraphProxy import FlowGraphProxy from .. import ParseXML, Messages @@ -28,7 +33,7 @@ from ..Constants import ( TOP_BLOCK_FILE_MODE, BLOCK_FLAG_NEED_QT_GUI, HIER_BLOCK_FILE_MODE, BLOCK_DTD ) -from ..utils import expr_utils, odict +from ..utils import expr_utils DATA_DIR = os.path.dirname(__file__) FLOW_GRAPH_TEMPLATE = os.path.join(DATA_DIR, 'flow_graph.tmpl') @@ -83,20 +88,18 @@ class TopBlockGenerator(object): self.file_path = os.path.join(dirname, filename) self._dirname = dirname - def get_file_path(self): - return self.file_path - def write(self): """generate output and write it to files""" # Do throttle warning - throttling_blocks = filter(lambda b: b.throtteling(), self._flow_graph.get_enabled_blocks()) + throttling_blocks = [b for b in self._flow_graph.get_enabled_blocks() + if b.is_throtteling] if not throttling_blocks and not self._generate_options.startswith('hb'): Messages.send_warning("This flow graph may not have flow control: " "no audio or RF hardware blocks found. " "Add a Misc->Throttle block to your flow " "graph to avoid CPU congestion.") if len(throttling_blocks) > 1: - keys = set(map(lambda b: b.get_key(), throttling_blocks)) + keys = set([b.key for b in throttling_blocks]) if len(keys) > 1 and 'blocks_throttle' in keys: Messages.send_warning("This flow graph contains a throttle " "block and another rate limiting block, " @@ -139,18 +142,18 @@ class TopBlockGenerator(object): return code blocks_all = expr_utils.sort_objects( - filter(lambda b: b.get_enabled() and not b.get_bypassed(), fg.blocks), - lambda b: b.get_id(), _get_block_sort_text + [b for b in fg.blocks if b.enabled and not b.get_bypassed()], + operator.methodcaller('get_id'), _get_block_sort_text ) - deprecated_block_keys = set(block.get_name() for block in blocks_all if block.is_deprecated) + deprecated_block_keys = set(b.name for b in blocks_all if b.is_deprecated) for key in deprecated_block_keys: Messages.send_warning("The block {!r} is deprecated.".format(key)) # List of regular blocks (all blocks minus the special ones) - blocks = filter(lambda b: b not in (imports + parameters), blocks_all) + blocks = [b for b in blocks_all if b not in imports and b not in parameters] for block in blocks: - key = block.get_key() + key = block.key file_path = os.path.join(self._dirname, block.get_id() + '.py') if key == 'epy_block': src = block.get_param('_source_code').get_value() @@ -159,17 +162,17 @@ class TopBlockGenerator(object): src = block.get_param('source_code').get_value() output.append((file_path, src)) - # Filter out virtual sink connections - def cf(c): - return not (c.is_bus() or c.get_sink().get_parent().is_virtual_sink()) - connections = filter(cf, fg.get_enabled_connections()) + # Filter out bus and virtual sink connections + connections = [con for con in fg.get_enabled_connections() + if not (con.is_bus() or con.sink_block.is_virtual_sink())] # Get the virtual blocks and resolve their connections - virtual = filter(lambda c: c.get_source().get_parent().is_virtual_source(), connections) + connection_factory = fg.parent_platform.Connection + virtual = [c for c in connections if c.source_block.is_virtual_source()] for connection in virtual: - source = connection.get_source().resolve_virtual_source() - sink = connection.get_sink() - resolved = fg.get_parent().Connection(flow_graph=fg, porta=source, portb=sink) + source = connection.source_port.resolve_virtual_source() + sink = connection.sink_port + resolved = connection_factory(fg.orignal_flowgraph, source, sink) connections.append(resolved) # Remove the virtual connection connections.remove(connection) @@ -183,20 +186,19 @@ class TopBlockGenerator(object): for block in bypassed_blocks: # Get the upstream connection (off of the sink ports) # Use *connections* not get_connections() - source_connection = filter(lambda c: c.get_sink() == block.get_sinks()[0], connections) + source_connection = [c for c in connections if c.sink_port == block.sinks[0]] # The source connection should never have more than one element. assert (len(source_connection) == 1) # Get the source of the connection. - source_port = source_connection[0].get_source() + source_port = source_connection[0].source_port # Loop through all the downstream connections - for sink in filter(lambda c: c.get_source() == block.get_sources()[0], connections): - if not sink.get_enabled(): + for sink in (c for c in connections if c.source_port == block.sources[0]): + if not sink.enabled: # Ignore disabled connections continue - sink_port = sink.get_sink() - connection = fg.get_parent().Connection(flow_graph=fg, porta=source_port, portb=sink_port) + connection = connection_factory(fg.orignal_flowgraph, source_port, sink.sink_port) connections.append(connection) # Remove this sink connection connections.remove(sink) @@ -205,11 +207,11 @@ class TopBlockGenerator(object): # List of connections where each endpoint is enabled (sorted by domains, block names) connections.sort(key=lambda c: ( - c.get_source().get_domain(), c.get_sink().get_domain(), - c.get_source().get_parent().get_id(), c.get_sink().get_parent().get_id() + c.source_port.domain, c.sink_port.domain, + c.source_block.get_id(), c.sink_block.get_id() )) - connection_templates = fg.get_parent().connection_templates + connection_templates = fg.parent.connection_templates # List of variable names var_ids = [var.get_id() for var in parameters + variables] @@ -259,7 +261,7 @@ class HierBlockGenerator(TopBlockGenerator): file_path: where to write the py file (the xml goes into HIER_BLOCK_LIB_DIR) """ TopBlockGenerator.__init__(self, flow_graph, file_path) - platform = flow_graph.get_parent() + platform = flow_graph.parent hier_block_lib_dir = platform.config.hier_block_lib_dir if not os.path.exists(hier_block_lib_dir): @@ -267,18 +269,15 @@ class HierBlockGenerator(TopBlockGenerator): self._mode = HIER_BLOCK_FILE_MODE self.file_path = os.path.join(hier_block_lib_dir, self._flow_graph.get_option('id') + '.py') - self._file_path_xml = self.file_path + '.xml' - - def get_file_path_xml(self): - return self._file_path_xml + self.file_path_xml = self.file_path + '.xml' def write(self): """generate output and write it to files""" TopBlockGenerator.write(self) - ParseXML.to_file(self._build_block_n_from_flow_graph_io(), self.get_file_path_xml()) - ParseXML.validate_dtd(self.get_file_path_xml(), BLOCK_DTD) + ParseXML.to_file(self._build_block_n_from_flow_graph_io(), self.file_path_xml) + ParseXML.validate_dtd(self.file_path_xml, BLOCK_DTD) try: - os.chmod(self.get_file_path_xml(), self._mode) + os.chmod(self.file_path_xml, self._mode) except: pass @@ -294,12 +293,12 @@ class HierBlockGenerator(TopBlockGenerator): parameters = self._flow_graph.get_parameters() def var_or_value(name): - if name in map(lambda p: p.get_id(), parameters): - return "$"+name + if name in (p.get_id() for p in parameters): + return "$" + name return name # Build the nested data - block_n = odict() + block_n = collections.OrderedDict() block_n['name'] = self._flow_graph.get_option('title') or \ self._flow_graph.get_option('id').replace('_', ' ').title() block_n['key'] = block_key @@ -324,7 +323,7 @@ class HierBlockGenerator(TopBlockGenerator): # Parameters block_n['param'] = list() for param in parameters: - param_n = odict() + param_n = collections.OrderedDict() param_n['name'] = param.get_param('label').get_value() or param.get_id() param_n['key'] = param.get_id() param_n['value'] = param.get_param('value').get_value() @@ -341,7 +340,7 @@ class HierBlockGenerator(TopBlockGenerator): for direction in ('sink', 'source'): block_n[direction] = list() for port in self._flow_graph.get_hier_block_io(direction): - port_n = odict() + port_n = collections.OrderedDict() port_n['name'] = port['label'] port_n['type'] = port['type'] if port['type'] != "message": @@ -374,14 +373,18 @@ class QtHierBlockGenerator(HierBlockGenerator): def _build_block_n_from_flow_graph_io(self): n = HierBlockGenerator._build_block_n_from_flow_graph_io(self) - block_n = n['block'] + block_n = collections.OrderedDict() + + # insert flags after category + for key, value in six.iteritems(n['block']): + block_n[key] = value + if key == 'category': + block_n['flags'] = BLOCK_FLAG_NEED_QT_GUI if not block_n['name'].upper().startswith('QT GUI'): block_n['name'] = 'QT GUI ' + block_n['name'] - block_n.insert_after('category', 'flags', BLOCK_FLAG_NEED_QT_GUI) - - gui_hint_param = odict() + gui_hint_param = collections.OrderedDict() gui_hint_param['name'] = 'GUI Hint' gui_hint_param['key'] = 'gui_hint' gui_hint_param['value'] = '' @@ -393,4 +396,5 @@ class QtHierBlockGenerator(HierBlockGenerator): "\n#set $win = 'self.%s' % $id" "\n${gui_hint()($win)}" ) - return n + + return {'block': block_n} diff --git a/grc/core/generator/__init__.py b/grc/core/generator/__init__.py index f44b94a85d..98f410c8d4 100644 --- a/grc/core/generator/__init__.py +++ b/grc/core/generator/__init__.py @@ -15,4 +15,5 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from Generator import Generator +from __future__ import absolute_import +from .Generator import Generator diff --git a/grc/core/generator/flow_graph.tmpl b/grc/core/generator/flow_graph.tmpl index 059703340e..b8913bb087 100644 --- a/grc/core/generator/flow_graph.tmpl +++ b/grc/core/generator/flow_graph.tmpl @@ -205,17 +205,17 @@ gr.io_signaturev($(len($io_sigs)), $(len($io_sigs)), [$(', '.join($size_strs))]) $indent($blk.get_make()) #else self.$blk.get_id() = $indent($blk.get_make()) - #if $blk.has_param('alias') and $blk.get_param('alias').get_evaluated() - (self.$blk.get_id()).set_block_alias("$blk.get_param('alias').get_evaluated()") + #if 'alias' in $blk.params and $blk.params['alias'].get_evaluated() + (self.$blk.get_id()).set_block_alias("$blk.params['alias'].get_evaluated()") #end if - #if $blk.has_param('affinity') and $blk.get_param('affinity').get_evaluated() - (self.$blk.get_id()).set_processor_affinity($blk.get_param('affinity').get_evaluated()) + #if 'affinity' in $blk.params and $blk.params['affinity'].get_evaluated() + (self.$blk.get_id()).set_processor_affinity($blk.params['affinity'].get_evaluated()) #end if - #if (len($blk.get_sources())>0) and $blk.has_param('minoutbuf') and (int($blk.get_param('minoutbuf').get_evaluated()) > 0) - (self.$blk.get_id()).set_min_output_buffer($blk.get_param('minoutbuf').get_evaluated()) + #if len($blk.sources) > 0 and 'minoutbuf' in $blk.params and int($blk.params['minoutbuf'].get_evaluated()) > 0 + (self.$blk.get_id()).set_min_output_buffer($blk.params['minoutbuf'].get_evaluated()) #end if - #if (len($blk.get_sources())>0) and $blk.has_param('maxoutbuf') and (int($blk.get_param('maxoutbuf').get_evaluated()) > 0) - (self.$blk.get_id()).set_max_output_buffer($blk.get_param('maxoutbuf').get_evaluated()) + #if len($blk.sources) > 0 and 'maxoutbuf' in $blk.params and int($blk.params['maxoutbuf'].get_evaluated()) > 0 + (self.$blk.get_id()).set_max_output_buffer($blk.params['maxoutbuf'].get_evaluated()) #end if #end if #end for @@ -225,12 +225,12 @@ gr.io_signaturev($(len($io_sigs)), $(len($io_sigs)), [$(', '.join($size_strs))]) ## However, port names for IO pads should be self. ######################################################## #def make_port_sig($port) - #if $port.get_parent().get_key() in ('pad_source', 'pad_sink') + #if $port.parent.key in ('pad_source', 'pad_sink') #set block = 'self' #set key = $flow_graph.get_pad_port_global_key($port) #else - #set block = 'self.' + $port.get_parent().get_id() - #set key = $port.get_key() + #set block = 'self.' + $port.parent.get_id() + #set key = $port.key #end if #if not $key.isdigit() #set key = repr($key) @@ -244,9 +244,9 @@ gr.io_signaturev($(len($io_sigs)), $(len($io_sigs)), [$(', '.join($size_strs))]) $DIVIDER #end if #for $con in $connections - #set global $source = $con.get_source() - #set global $sink = $con.get_sink() - #include source=$connection_templates[($source.get_domain(), $sink.get_domain())] + #set global $source = $con.source_port + #set global $sink = $con.sink_port + #include source=$connection_templates[($source.domain, $sink.domain)] #end for ######################################################## @@ -376,8 +376,8 @@ def main(top_block_cls=$(class_name), options=None): tb.wait() qapp.aboutToQuit.connect(quitting) #for $m in $monitors - if $m.has_param('en'): - if $m.get_param('en').get_value(): + if 'en' in $m.params: + if $m.params['en'].get_value(): (tb.$m.get_id()).start() else: sys.stderr.write("Monitor '{0}' does not have an enable ('en') parameter.".format("tb.$m.get_id()")) diff --git a/grc/core/utils/__init__.py b/grc/core/utils/__init__.py index 6b23da2723..d095179a10 100644 --- a/grc/core/utils/__init__.py +++ b/grc/core/utils/__init__.py @@ -15,8 +15,10 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import expr_utils -import epy_block_io -import extract_docs +from __future__ import absolute_import -from odict import odict +from . import expr_utils +from . import epy_block_io +from . import extract_docs + +from ._complexity import calculate_flowgraph_complexity diff --git a/grc/core/utils/complexity.py b/grc/core/utils/_complexity.py index baa8040db4..c0f3ae9de4 100644 --- a/grc/core/utils/complexity.py +++ b/grc/core/utils/_complexity.py @@ -4,12 +4,12 @@ def calculate_flowgraph_complexity(flowgraph): dbal = 0 for block in flowgraph.blocks: # Skip options block - if block.get_key() == 'options': + if block.key == 'options': continue # Don't worry about optional sinks? - sink_list = filter(lambda c: not c.get_optional(), block.get_sinks()) - source_list = filter(lambda c: not c.get_optional(), block.get_sources()) + sink_list = [c for c in block.sinks if not c.get_optional()] + source_list = [c for c in block.sources if not c.get_optional()] sinks = float(len(sink_list)) sources = float(len(source_list)) base = max(min(sinks, sources), 1) @@ -22,14 +22,15 @@ def calculate_flowgraph_complexity(flowgraph): multi = 1 # Connection ratio multiplier - sink_multi = max(float(sum(map(lambda c: len(c.get_connections()), sink_list)) / max(sinks, 1.0)), 1.0) - source_multi = max(float(sum(map(lambda c: len(c.get_connections()), source_list)) / max(sources, 1.0)), 1.0) - dbal = dbal + (base * multi * sink_multi * source_multi) + sink_multi = max(float(sum(len(c.get_connections()) for c in sink_list) / max(sinks, 1.0)), 1.0) + source_multi = max(float(sum(len(c.get_connections()) for c in source_list) / max(sources, 1.0)), 1.0) + dbal += base * multi * sink_multi * source_multi blocks = float(len(flowgraph.blocks)) connections = float(len(flowgraph.connections)) elements = blocks + connections - disabled_connections = len(filter(lambda c: not c.get_enabled(), flowgraph.connections)) + disabled_connections = sum(not c.enabled for c in flowgraph.connections) + variables = elements - blocks - connections enabled = float(len(flowgraph.get_enabled_blocks())) diff --git a/grc/core/utils/epy_block_io.py b/grc/core/utils/epy_block_io.py index 76b50051db..7a2006a833 100644 --- a/grc/core/utils/epy_block_io.py +++ b/grc/core/utils/epy_block_io.py @@ -1,7 +1,12 @@ +from __future__ import absolute_import + import inspect import collections +import six +from six.moves import zip + TYPE_MAP = { 'complex64': 'complex', 'complex': 'complex', @@ -31,10 +36,10 @@ def _ports(sigs, msgs): def _find_block_class(source_code, cls): ns = {} try: - exec source_code in ns + exec(source_code, ns) except Exception as e: raise ValueError("Can't interpret source code: " + str(e)) - for var in ns.itervalues(): + for var in six.itervalues(ns): if inspect.isclass(var) and issubclass(var, cls): return var raise ValueError('No python block class found in code') @@ -52,7 +57,7 @@ def extract(cls): spec = inspect.getargspec(cls.__init__) init_args = spec.args[1:] - defaults = map(repr, spec.defaults or ()) + defaults = [repr(arg) for arg in (spec.defaults or ())] doc = cls.__doc__ or cls.__init__.__doc__ or '' cls_name = cls.__name__ diff --git a/grc/core/utils/expr_utils.py b/grc/core/utils/expr_utils.py index 2059ceff9f..555bd709b1 100644 --- a/grc/core/utils/expr_utils.py +++ b/grc/core/utils/expr_utils.py @@ -17,7 +17,12 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import, print_function + import string + +import six + VAR_CHARS = string.letters + string.digits + '_' @@ -50,7 +55,7 @@ class graph(object): self._graph[src_node_key].remove(dest_node_key) def get_nodes(self): - return self._graph.keys() + return list(self._graph.keys()) def get_edges(self, node_key): return self._graph[node_key] @@ -85,7 +90,7 @@ def expr_split(expr, var_chars=VAR_CHARS): toks.append(char) tok = '' toks.append(tok) - return filter(lambda t: t, toks) + return [t for t in toks if t] def expr_replace(expr, replace_dict): @@ -101,7 +106,7 @@ def expr_replace(expr, replace_dict): """ expr_splits = expr_split(expr, var_chars=VAR_CHARS + '.') for i, es in enumerate(expr_splits): - if es in replace_dict.keys(): + if es in list(replace_dict.keys()): expr_splits[i] = replace_dict[es] return ''.join(expr_splits) @@ -118,7 +123,7 @@ def get_variable_dependencies(expr, vars): a subset of vars used in the expression """ expr_toks = expr_split(expr) - return set(var for var in vars if var in expr_toks) + return set(v for v in vars if v in expr_toks) def get_graph(exprs): @@ -131,12 +136,12 @@ def get_graph(exprs): Returns: a graph of variable deps """ - vars = exprs.keys() + vars = list(exprs.keys()) # Get dependencies for each expression, load into graph var_graph = graph() for var in vars: var_graph.add_node(var) - for var, expr in exprs.iteritems(): + for var, expr in six.iteritems(exprs): for dep in get_variable_dependencies(expr, vars): if dep != var: var_graph.add_edge(dep, var) @@ -159,7 +164,7 @@ def sort_variables(exprs): # Determine dependency order while var_graph.get_nodes(): # Get a list of nodes with no edges - indep_vars = filter(lambda var: not var_graph.get_edges(var), var_graph.get_nodes()) + indep_vars = [var for var in var_graph.get_nodes() if not var_graph.get_edges(var)] if not indep_vars: raise Exception('circular dependency caught in sort_variables') # Add the indep vars to the end of the list @@ -189,3 +194,4 @@ def sort_objects(objects, get_id, get_expr): sorted_ids = sort_variables(id2expr) # Return list of sorted objects return [id2obj[id] for id in sorted_ids] + diff --git a/grc/core/utils/extract_docs.py b/grc/core/utils/extract_docs.py index a6e0bc971e..cff8a81099 100644 --- a/grc/core/utils/extract_docs.py +++ b/grc/core/utils/extract_docs.py @@ -17,15 +17,19 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import, print_function + import sys import re import subprocess import threading import json -import Queue import random import itertools +import six +from six.moves import queue, filter, range + ############################################################################### # The docstring extraction @@ -124,7 +128,7 @@ class SubprocessLoader(object): self.callback_query_result = callback_query_result self.callback_finished = callback_finished or (lambda: None) - self._queue = Queue.Queue() + self._queue = queue.Queue() self._thread = None self._worker = None self._shutdown = threading.Event() @@ -157,14 +161,14 @@ class SubprocessLoader(object): cmd, args = self._last_cmd if cmd == 'query': msg += " (crashed while loading {0!r})".format(args[0]) - print >> sys.stderr, msg + print(msg, file=sys.stderr) continue # restart else: break # normal termination, return finally: self._worker.terminate() else: - print >> sys.stderr, "Warning: docstring loader crashed too often" + print("Warning: docstring loader crashed too often", file=sys.stderr) self._thread = None self._worker = None self.callback_finished() @@ -203,9 +207,9 @@ class SubprocessLoader(object): key, docs = args self.callback_query_result(key, docs) elif cmd == 'error': - print args + print(args) else: - print >> sys.stderr, "Unknown response:", cmd, args + print("Unknown response:", cmd, args, file=sys.stderr) def query(self, key, imports=None, make=None): """ Request docstring extraction for a certain key """ @@ -270,12 +274,12 @@ if __name__ == '__worker__': elif __name__ == '__main__': def callback(key, docs): - print key - for match, doc in docs.iteritems(): - print '-->', match - print doc.strip() - print - print + print(key) + for match, doc in six.iteritems(docs): + print('-->', match) + print(doc.strip()) + print() + print() r = SubprocessLoader(callback) diff --git a/grc/core/utils/odict.py b/grc/core/utils/odict.py deleted file mode 100644 index 85927e869f..0000000000 --- a/grc/core/utils/odict.py +++ /dev/null @@ -1,115 +0,0 @@ -""" -Copyright 2008-2015 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" - -from UserDict import DictMixin - - -class odict(DictMixin): - - def __init__(self, d={}): - self._keys = list(d.keys()) - self._data = dict(d.copy()) - - def __setitem__(self, key, value): - if key not in self._data: - self._keys.append(key) - self._data[key] = value - - def __getitem__(self, key): - return self._data[key] - - def __delitem__(self, key): - del self._data[key] - self._keys.remove(key) - - def keys(self): - return list(self._keys) - - def copy(self): - copy_dict = odict() - copy_dict._data = self._data.copy() - copy_dict._keys = list(self._keys) - return copy_dict - - def insert_after(self, pos_key, key, val): - """ - Insert the new key, value entry after the entry given by the position key. - If the positional key is None, insert at the end. - - Args: - pos_key: the positional key - key: the key for the new entry - val: the value for the new entry - """ - index = (pos_key is None) and len(self._keys) or self._keys.index(pos_key) - if key in self._keys: - raise KeyError('Cannot insert, key "{}" already exists'.format(str(key))) - self._keys.insert(index+1, key) - self._data[key] = val - - def insert_before(self, pos_key, key, val): - """ - Insert the new key, value entry before the entry given by the position key. - If the positional key is None, insert at the begining. - - Args: - pos_key: the positional key - key: the key for the new entry - val: the value for the new entry - """ - index = (pos_key is not None) and self._keys.index(pos_key) or 0 - if key in self._keys: - raise KeyError('Cannot insert, key "{}" already exists'.format(str(key))) - self._keys.insert(index, key) - self._data[key] = val - - def find(self, key): - """ - Get the value for this key if exists. - - Args: - key: the key to search for - - Returns: - the value or None - """ - if key in self: - return self[key] - return None - - def findall(self, key): - """ - Get a list of values for this key. - - Args: - key: the key to search for - - Returns: - a list of values or empty list - """ - obj = self.find(key) - if obj is None: - obj = list() - if isinstance(obj, list): - return obj - return [obj] - - def clear(self): - self._data.clear() - del self._keys[:]
\ No newline at end of file diff --git a/grc/gui/ActionHandler.py b/grc/gui/ActionHandler.py index f18fcea3e1..913eb5a8d1 100644 --- a/grc/gui/ActionHandler.py +++ b/grc/gui/ActionHandler.py @@ -18,23 +18,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import gobject -import gtk +from __future__ import absolute_import, print_function + import os import subprocess -from . import Dialogs, Preferences, Actions, Executor, Constants -from .FileDialogs import (OpenFlowGraphFileDialog, SaveFlowGraphFileDialog, - SaveConsoleFileDialog, SaveScreenShotDialog, - OpenQSSFileDialog) +from gi.repository import Gtk + +from . import Dialogs, Preferences, Actions, Executor, FileDialogs, Utils from .MainWindow import MainWindow from .ParserErrorsDialog import ParserErrorsDialog from .PropsDialog import PropsDialog from ..core import ParseXML, Messages -gobject.threads_init() - class ActionHandler: """ @@ -60,7 +57,6 @@ class ActionHandler: self.main_window = MainWindow(platform, self._handle_action) self.main_window.connect('delete-event', self._quit) self.main_window.connect('key-press-event', self._handle_key_press) - self.get_page = self.main_window.get_page self.get_focus_flag = self.main_window.get_focus_flag #setup the messages Messages.register_messenger(self.main_window.add_console_line) @@ -85,9 +81,8 @@ class ActionHandler: # prevent key event stealing while the search box is active # .has_focus() only in newer versions 2.17+? # .is_focus() seems to work, but exactly the same - if self.main_window.btwin.search_entry.flags() & gtk.HAS_FOCUS: + if self.main_window.btwin.search_entry.has_focus(): return False - if not self.get_focus_flag(): return False return Actions.handle_key_press(event) def _quit(self, window, event): @@ -105,11 +100,11 @@ class ActionHandler: def _handle_action(self, action, *args): #print action main = self.main_window - page = main.get_page() - flow_graph = page.get_flow_graph() if page else None + page = main.current_page + flow_graph = page.flow_graph if page else None def flow_graph_update(fg=flow_graph): - main.vars.update_gui() + main.vars.update_gui(fg.blocks) fg.update() ################################################## @@ -120,7 +115,7 @@ class ActionHandler: for file_path in (self.init_file_paths or Preferences.get_open_files()): if os.path.exists(file_path): main.new_page(file_path, show=file_path_to_show == file_path) - if not self.get_page(): + if not main.current_page: main.new_page() # ensure that at least a blank page exists main.btwin.search_entry.hide() @@ -156,7 +151,7 @@ class ActionHandler: self.init = True elif action == Actions.APPLICATION_QUIT: if main.close_pages(): - gtk.main_quit() + Gtk.main_quit() exit(0) ################################################## # Selections @@ -170,21 +165,16 @@ class ActionHandler: ################################################## # Enable/Disable ################################################## - elif action == Actions.BLOCK_ENABLE: - if flow_graph.enable_selected(True): + 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.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) - elif action == Actions.BLOCK_DISABLE: - if flow_graph.enable_selected(False): - flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) - elif action == Actions.BLOCK_BYPASS: - if flow_graph.bypass_selected(): - flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False ################################################## # Cut/Copy/Paste ################################################## @@ -197,15 +187,15 @@ class ActionHandler: if self.clipboard: flow_graph.paste_from_clipboard(self.clipboard) flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False ################################################## # Create heir block ################################################## elif action == Actions.BLOCK_CREATE_HIER: # keeping track of coordinates for pasting later - coords = flow_graph.get_selected_blocks()[0].get_coordinate() + coords = flow_graph.selected_blocks()[0].coordinate x,y = coords x_min = x y_min = y @@ -214,10 +204,10 @@ class ActionHandler: params = []; # Save the state of the leaf blocks - for block in flow_graph.get_selected_blocks(): + for block in flow_graph.selected_blocks(): # Check for string variables within the blocks - for param in block.get_params(): + for param in block.params.values(): for variable in flow_graph.get_variables(): # If a block parameter exists that is a variable, create a parameter for it if param.get_value() == variable.get_id(): @@ -229,7 +219,7 @@ class ActionHandler: # keep track of x,y mins for pasting later - (x,y) = block.get_coordinate() + (x,y) = block.coordinate if x < x_min: x_min = x if y < y_min: @@ -238,15 +228,15 @@ class ActionHandler: for connection in block.connections: # Get id of connected blocks - source_id = connection.get_source().get_parent().get_id() - sink_id = connection.get_sink().get_parent().get_id() + source_id = connection.source_block.get_id() + sink_id = connection.sink_block.get_id() # If connected block is not in the list of selected blocks create a pad for it - if flow_graph.get_block(source_id) not in flow_graph.get_selected_blocks(): - pads.append({'key': connection.get_sink().get_key(), 'coord': connection.get_source().get_coordinate(), 'block_id' : block.get_id(), 'direction': 'source'}) + if flow_graph.get_block(source_id) not in flow_graph.selected_blocks(): + pads.append({'key': connection.sink_port.key, 'coord': connection.source_port.coordinate, 'block_id' : block.get_id(), 'direction': 'source'}) - if flow_graph.get_block(sink_id) not in flow_graph.get_selected_blocks(): - pads.append({'key': connection.get_source().get_key(), 'coord': connection.get_sink().get_coordinate(), 'block_id' : block.get_id(), 'direction': 'sink'}) + if flow_graph.get_block(sink_id) not in flow_graph.selected_blocks(): + pads.append({'key': connection.source_port.key, 'coord': connection.sink_port.coordinate, 'block_id' : block.get_id(), 'direction': 'sink'}) # Copy the selected blocks and paste them into a new page @@ -287,7 +277,7 @@ class ActionHandler: # setup the references to the sink and source pad_block = flow_graph.get_block(pad_id) - pad_sink = pad_block.get_sinks()[0] + pad_sink = pad_block.sinks[0] source_block = flow_graph.get_block(pad['block_id']) source = source_block.get_source(pad['key']) @@ -308,7 +298,7 @@ class ActionHandler: # setup the references to the sink and source pad_block = flow_graph.get_block(pad_id) - pad_source = pad_block.get_sources()[0] + pad_source = pad_block.sources[0] sink_block = flow_graph.get_block(pad['block_id']) sink = sink_block.get_sink(pad['key']) @@ -331,65 +321,64 @@ class ActionHandler: # Move/Rotate/Delete/Create ################################################## elif action == Actions.BLOCK_MOVE: - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False elif action in Actions.BLOCK_ALIGNMENTS: if flow_graph.align_selected(action): - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False elif action == Actions.BLOCK_ROTATE_CCW: if flow_graph.rotate_selected(90): flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False elif action == Actions.BLOCK_ROTATE_CW: if flow_graph.rotate_selected(-90): flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False elif action == Actions.ELEMENT_DELETE: if flow_graph.remove_selected(): flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) + page.state_cache.save_new_state(flow_graph.export_data()) Actions.NOTHING_SELECT() - page.set_saved(False) + page.saved = False elif action == Actions.ELEMENT_CREATE: flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) + page.state_cache.save_new_state(flow_graph.export_data()) Actions.NOTHING_SELECT() - page.set_saved(False) + page.saved = False elif action == Actions.BLOCK_INC_TYPE: if flow_graph.type_controller_modify_selected(1): flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False elif action == Actions.BLOCK_DEC_TYPE: if flow_graph.type_controller_modify_selected(-1): flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False elif action == Actions.PORT_CONTROLLER_INC: if flow_graph.port_controller_modify_selected(1): flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False elif action == Actions.PORT_CONTROLLER_DEC: if flow_graph.port_controller_modify_selected(-1): flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False ################################################## # Window stuff ################################################## elif action == Actions.ABOUT_WINDOW_DISPLAY: - platform = flow_graph.get_parent() - Dialogs.AboutDialog(platform.config) + Dialogs.show_about(main, self.platform.config) elif action == Actions.HELP_WINDOW_DISPLAY: - Dialogs.HelpDialog() + Dialogs.show_help(main) elif action == Actions.TYPES_WINDOW_DISPLAY: - Dialogs.TypesDialog(flow_graph.get_parent()) + Dialogs.show_types(main) elif action == Actions.ERRORS_WINDOW_DISPLAY: - Dialogs.ErrorsDialog(flow_graph) + Dialogs.ErrorsDialog(main, flow_graph).run_and_destroy() elif action == Actions.TOGGLE_CONSOLE_WINDOW: main.update_panel_visibility(main.CONSOLE, action.get_active()) action.save_to_preferences() @@ -405,7 +394,7 @@ class ActionHandler: elif action == Actions.CLEAR_CONSOLE: main.text_display.clear() elif action == Actions.SAVE_CONSOLE: - file_path = SaveConsoleFileDialog(page.get_file_path()).run() + file_path = FileDialogs.SaveConsole(main, page.file_path).run() if file_path is not None: main.text_display.save(file_path) elif action == Actions.TOGGLE_HIDE_DISABLED_BLOCKS: @@ -413,7 +402,7 @@ class ActionHandler: elif action == Actions.TOGGLE_AUTO_HIDE_PORT_LABELS: action.save_to_preferences() for page in main.get_pages(): - page.get_flow_graph().create_shapes() + page.flow_graph.create_shapes() elif action in (Actions.TOGGLE_SNAP_TO_GRID, Actions.TOGGLE_SHOW_BLOCK_COMMENTS, Actions.TOGGLE_SHOW_CODE_PREVIEW_TAB): @@ -421,7 +410,7 @@ class ActionHandler: elif action == Actions.TOGGLE_SHOW_FLOWGRAPH_COMPLEXITY: action.save_to_preferences() for page in main.get_pages(): - flow_graph_update(page.get_flow_graph()) + flow_graph_update(page.flow_graph) elif action == Actions.TOGGLE_HIDE_VARIABLES: # Call the variable editor TOGGLE in case it needs to be showing Actions.TOGGLE_FLOW_GRAPH_VAR_EDITOR() @@ -441,13 +430,13 @@ class ActionHandler: # Leave it enabled action.set_sensitive(True) action.set_active(True) - #Actions.TOGGLE_FLOW_GRAPH_VAR_EDITOR_SIDEBAR.set_sensitive(action.get_active()) + # Actions.TOGGLE_FLOW_GRAPH_VAR_EDITOR_SIDEBAR.set_sensitive(action.get_active()) action.save_to_preferences() elif action == Actions.TOGGLE_FLOW_GRAPH_VAR_EDITOR_SIDEBAR: if self.init: - md = gtk.MessageDialog(main, - gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, - gtk.BUTTONS_CLOSE, "Moving the variable editor requires a restart of GRC.") + md = Gtk.MessageDialog(main, + Gtk.DIALOG_DESTROY_WITH_PARENT, Gtk.MESSAGE_INFO, + Gtk.BUTTONS_CLOSE, "Moving the variable editor requires a restart of GRC.") md.run() md.destroy() action.save_to_preferences() @@ -455,38 +444,35 @@ class ActionHandler: # Param Modifications ################################################## elif action == Actions.BLOCK_PARAM_MODIFY: - if action.args: - selected_block = action.args[0] - else: - selected_block = flow_graph.get_selected_block() + selected_block = action.args[0] if action.args else flow_graph.selected_block if selected_block: - self.dialog = PropsDialog(selected_block) - response = gtk.RESPONSE_APPLY - while response == gtk.RESPONSE_APPLY: # rerun the dialog if Apply was hit + self.dialog = PropsDialog(self.main_window, selected_block) + response = Gtk.ResponseType.APPLY + while response == Gtk.ResponseType.APPLY: # rerun the dialog if Apply was hit response = self.dialog.run() - if response in (gtk.RESPONSE_APPLY, gtk.RESPONSE_ACCEPT): + if response in (Gtk.ResponseType.APPLY, Gtk.ResponseType.ACCEPT): flow_graph_update() - page.get_state_cache().save_new_state(flow_graph.export_data()) - page.set_saved(False) + page.state_cache.save_new_state(flow_graph.export_data()) + page.saved = False else: # restore the current state - n = page.get_state_cache().get_current_state() + n = page.state_cache.get_current_state() flow_graph.import_data(n) flow_graph_update() - if response == gtk.RESPONSE_APPLY: + if response == Gtk.ResponseType.APPLY: # null action, that updates the main window Actions.ELEMENT_SELECT() self.dialog.destroy() self.dialog = None elif action == Actions.EXTERNAL_UPDATE: - page.get_state_cache().save_new_state(flow_graph.export_data()) + page.state_cache.save_new_state(flow_graph.export_data()) flow_graph_update() if self.dialog is not None: self.dialog.update_gui(force=True) - page.set_saved(False) + page.saved = False elif action == Actions.VARIABLE_EDITOR_UPDATE: - page.get_state_cache().save_new_state(flow_graph.export_data()) + page.state_cache.save_new_state(flow_graph.export_data()) flow_graph_update() - page.set_saved(False) + page.saved = False ################################################## # View Parser Errors ################################################## @@ -496,19 +482,19 @@ class ActionHandler: # Undo/Redo ################################################## elif action == Actions.FLOW_GRAPH_UNDO: - n = page.get_state_cache().get_prev_state() + n = page.state_cache.get_prev_state() if n: flow_graph.unselect() flow_graph.import_data(n) flow_graph_update() - page.set_saved(False) + page.saved = False elif action == Actions.FLOW_GRAPH_REDO: - n = page.get_state_cache().get_next_state() + n = page.state_cache.get_next_state() if n: flow_graph.unselect() flow_graph.import_data(n) flow_graph_update() - page.set_saved(False) + page.saved = False ################################################## # New/Open/Save/Close ################################################## @@ -518,18 +504,16 @@ class ActionHandler: flow_graph._options_block.get_param('generate_options').set_value(args[0]) flow_graph_update() elif action == Actions.FLOW_GRAPH_OPEN: - file_paths = args if args else OpenFlowGraphFileDialog(page.get_file_path()).run() - if file_paths: #open a new page for each file, show only the first + file_paths = args if args else FileDialogs.OpenFlowGraph(main, page.file_path).run() + if file_paths: # Open a new page for each file, show only the first for i,file_path in enumerate(file_paths): main.new_page(file_path, show=(i==0)) Preferences.add_recent_file(file_path) main.tool_bar.refresh_submenus() main.menu_bar.refresh_submenus() - main.vars.update_gui() - elif action == Actions.FLOW_GRAPH_OPEN_QSS_THEME: - file_paths = OpenQSSFileDialog(self.platform.config.install_prefix + - '/share/gnuradio/themes/').run() + file_paths = FileDialogs.OpenQSS(main, self.platform.config.install_prefix + + '/share/gnuradio/themes/').run() if file_paths: try: prefs = self.platform.config.prefs @@ -541,66 +525,69 @@ class ActionHandler: main.close_page() elif action == Actions.FLOW_GRAPH_SAVE: #read-only or undefined file path, do save-as - if page.get_read_only() or not page.get_file_path(): + if page.get_read_only() or not page.file_path: Actions.FLOW_GRAPH_SAVE_AS() #otherwise try to save else: try: - ParseXML.to_file(flow_graph.export_data(), page.get_file_path()) - flow_graph.grc_file_path = page.get_file_path() - page.set_saved(True) + ParseXML.to_file(flow_graph.export_data(), page.file_path) + flow_graph.grc_file_path = page.file_path + page.saved = True except IOError: - Messages.send_fail_save(page.get_file_path()) - page.set_saved(False) + Messages.send_fail_save(page.file_path) + page.saved = False elif action == Actions.FLOW_GRAPH_SAVE_AS: - file_path = SaveFlowGraphFileDialog(page.get_file_path()).run() + file_path = FileDialogs.SaveFlowGraph(main, page.file_path).run() if file_path is not None: - page.set_file_path(file_path) + page.file_path = os.path.abspath(file_path) Actions.FLOW_GRAPH_SAVE() Preferences.add_recent_file(file_path) main.tool_bar.refresh_submenus() main.menu_bar.refresh_submenus() elif action == Actions.FLOW_GRAPH_SCREEN_CAPTURE: - file_path, background_transparent = SaveScreenShotDialog(page.get_file_path()).run() + file_path, background_transparent = FileDialogs.SaveScreenShot(main, page.file_path).run() if file_path is not None: - pixbuf = flow_graph.get_drawing_area().get_screenshot(background_transparent) - pixbuf.save(file_path, Constants.IMAGE_FILE_EXTENSION[1:]) + try: + Utils.make_screenshot(flow_graph, file_path, background_transparent) + except ValueError: + Messages.send('Failed to generate screen shot\n') ################################################## # Gen/Exec/Stop ################################################## elif action == Actions.FLOW_GRAPH_GEN: - if not page.get_proc(): - if not page.get_saved() or not page.get_file_path(): - Actions.FLOW_GRAPH_SAVE() #only save if file path missing or not saved - if page.get_saved() and page.get_file_path(): + if not page.process: + if not page.saved or not page.file_path: + Actions.FLOW_GRAPH_SAVE() # only save if file path missing or not saved + if page.saved and page.file_path: generator = page.get_generator() try: - Messages.send_start_gen(generator.get_file_path()) + Messages.send_start_gen(generator.file_path) generator.write() except Exception as e: Messages.send_fail_gen(e) else: self.generator = None elif action == Actions.FLOW_GRAPH_EXEC: - if not page.get_proc(): + if not page.process: Actions.FLOW_GRAPH_GEN() xterm = self.platform.config.xterm_executable + Dialogs.show_missing_xterm(main, xterm) if Preferences.xterm_missing() != xterm: if not os.path.exists(xterm): - Dialogs.MissingXTermDialog(xterm) + Dialogs.show_missing_xterm(main, xterm) Preferences.xterm_missing(xterm) - if page.get_saved() and page.get_file_path(): + if page.saved and page.file_path: Executor.ExecFlowGraphThread( flow_graph_page=page, xterm_executable=xterm, callback=self.update_exec_stop ) elif action == Actions.FLOW_GRAPH_KILL: - if page.get_proc(): + if page.process: try: - page.get_proc().kill() + page.process.kill() except: - print "could not kill process: %d" % page.get_proc().pid + print("could not kill process: %d" % page.process.pid) elif action == Actions.PAGE_CHANGE: # pass and run the global actions pass elif action == Actions.RELOAD_BLOCKS: @@ -617,21 +604,19 @@ class ActionHandler: main.btwin.search_entry.show() main.btwin.search_entry.grab_focus() elif action == Actions.OPEN_HIER: - for b in flow_graph.get_selected_blocks(): + for b in flow_graph.selected_blocks(): if b._grc_source: main.new_page(b._grc_source, show=True) elif action == Actions.BUSSIFY_SOURCES: - n = {'name':'bus', 'type':'bus'} - for b in flow_graph.get_selected_blocks(): - b.bussify(n, 'source') + for b in flow_graph.selected_blocks(): + b.bussify('source') flow_graph._old_selected_port = None flow_graph._new_selected_port = None Actions.ELEMENT_CREATE() elif action == Actions.BUSSIFY_SINKS: - n = {'name':'bus', 'type':'bus'} - for b in flow_graph.get_selected_blocks(): - b.bussify(n, 'sink') + for b in flow_graph.selected_blocks(): + b.bussify('sink') flow_graph._old_selected_port = None flow_graph._new_selected_port = None Actions.ELEMENT_CREATE() @@ -641,19 +626,19 @@ class ActionHandler: shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) else: - print '!!! Action "%s" not handled !!!' % action + print('!!! Action "%s" not handled !!!' % action) ################################################## # Global Actions for all States ################################################## - page = main.get_page() # page and flowgraph might have changed - flow_graph = page.get_flow_graph() if page else None + page = main.current_page # page and flow graph might have changed + flow_graph = page.flow_graph if page else None - selected_blocks = flow_graph.get_selected_blocks() + selected_blocks = list(flow_graph.selected_blocks()) selected_block = selected_blocks[0] if selected_blocks else None #update general buttons Actions.ERRORS_WINDOW_DISPLAY.set_sensitive(not flow_graph.is_valid()) - Actions.ELEMENT_DELETE.set_sensitive(bool(flow_graph.get_selected_elements())) + Actions.ELEMENT_DELETE.set_sensitive(bool(flow_graph.selected_elements)) Actions.BLOCK_PARAM_MODIFY.set_sensitive(bool(selected_block)) Actions.BLOCK_ROTATE_CCW.set_sensitive(bool(selected_blocks)) Actions.BLOCK_ROTATE_CW.set_sensitive(bool(selected_blocks)) @@ -666,12 +651,14 @@ 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.get_state() != Constants.BLOCK_ENABLED + can_enable = any(block.state != 'enabled' for block in selected_blocks) - can_disable = any(block.get_state() != Constants.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 any (not block.get_bypassed() for block in selected_blocks) + can_bypass_all = ( + all(block.can_bypass() for block in selected_blocks) and + any(not block.get_bypassed() for block in selected_blocks) + ) Actions.BLOCK_ENABLE.set_sensitive(can_enable) Actions.BLOCK_DISABLE.set_sensitive(can_disable) Actions.BLOCK_BYPASS.set_sensitive(can_bypass_all) @@ -682,28 +669,24 @@ class ActionHandler: Actions.BUSSIFY_SINKS.set_sensitive(bool(selected_blocks)) Actions.RELOAD_BLOCKS.set_sensitive(True) Actions.FIND_BLOCKS.set_sensitive(True) - #set the exec and stop buttons + self.update_exec_stop() - #saved status - Actions.FLOW_GRAPH_SAVE.set_sensitive(not page.get_saved()) + + Actions.FLOW_GRAPH_SAVE.set_sensitive(not page.saved) main.update() - try: #set the size of the flow graph area (if changed) - new_size = (flow_graph.get_option('window_size') or - self.platform.config.default_canvas_size) - if flow_graph.get_size() != tuple(new_size): - flow_graph.set_size(*new_size) - except: pass - #draw the flow graph + flow_graph.update_selected() - flow_graph.queue_draw() - return True #action was handled + page.drawing_area.queue_draw() + + return True # action was handled def update_exec_stop(self): """ Update the exec and stop buttons. Lock and unlock the mutex for race conditions with exec flow graph threads. """ - sensitive = self.main_window.get_flow_graph().is_valid() and not self.get_page().get_proc() + page = self.main_window.current_page + sensitive = page.flow_graph.is_valid() and not page.process Actions.FLOW_GRAPH_GEN.set_sensitive(sensitive) Actions.FLOW_GRAPH_EXEC.set_sensitive(sensitive) - Actions.FLOW_GRAPH_KILL.set_sensitive(self.get_page().get_proc() is not None) + Actions.FLOW_GRAPH_KILL.set_sensitive(page.process is not None) diff --git a/grc/gui/Actions.py b/grc/gui/Actions.py index 9b2af36b93..4759e56294 100644 --- a/grc/gui/Actions.py +++ b/grc/gui/Actions.py @@ -17,19 +17,26 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk +from __future__ import absolute_import -import Preferences +import six + +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject + +from . import Preferences NO_MODS_MASK = 0 + ######################################################################## # Actions API ######################################################################## _actions_keypress_dict = dict() -_keymap = gtk.gdk.keymap_get_default() +_keymap = Gdk.Keymap.get_default() _used_mods_mask = NO_MODS_MASK @@ -44,12 +51,11 @@ def handle_key_press(event): Returns: true if handled """ - _used_mods_mask = reduce(lambda x, y: x | y, [mod_mask for keyval, mod_mask in _actions_keypress_dict], NO_MODS_MASK) # extract the key value and the consumed modifiers - keyval, egroup, level, consumed = _keymap.translate_keyboard_state( - event.hardware_keycode, event.state, event.group) + _unused, keyval, egroup, level, consumed = _keymap.translate_keyboard_state( + event.hardware_keycode, event.get_state(), event.group) # get the modifier mask and ignore irrelevant modifiers - mod_mask = event.state & ~consumed & _used_mods_mask + mod_mask = event.get_state() & ~consumed & _used_mods_mask # look up the keypress and call the action try: _actions_keypress_dict[(keyval, mod_mask)]() @@ -64,7 +70,7 @@ _all_actions_list = list() def get_all_actions(): return _all_actions_list -_accel_group = gtk.AccelGroup() +_accel_group = Gtk.AccelGroup() def get_accel_group(): @@ -77,13 +83,16 @@ class _ActionBase(object): Register actions and keypresses with this module. """ def __init__(self, label, keypresses): + global _used_mods_mask + _all_actions_list.append(self) for i in range(len(keypresses)/2): keyval, mod_mask = keypresses[i*2:(i+1)*2] # register this keypress - if _actions_keypress_dict.has_key((keyval, mod_mask)): + if (keyval, mod_mask) in _actions_keypress_dict: raise KeyError('keyval/mod_mask pair already registered "%s"' % str((keyval, mod_mask))) _actions_keypress_dict[(keyval, mod_mask)] = self + _used_mods_mask |= mod_mask # set the accelerator group, and accelerator path # register the key name and mod mask with the accelerator path if label is None: @@ -91,7 +100,7 @@ class _ActionBase(object): accel_path = '<main>/' + self.get_name() self.set_accel_group(get_accel_group()) self.set_accel_path(accel_path) - gtk.accel_map_add_entry(accel_path, keyval, mod_mask) + Gtk.AccelMap.add_entry(accel_path, keyval, mod_mask) self.args = None def __str__(self): @@ -99,7 +108,7 @@ class _ActionBase(object): The string representation should be the name of the action id. Try to find the action id for this action by searching this module. """ - for name, value in globals().iteritems(): + for name, value in six.iteritems(globals()): if value == self: return name return self.get_name() @@ -114,9 +123,9 @@ class _ActionBase(object): self.emit('activate') -class Action(gtk.Action, _ActionBase): +class Action(Gtk.Action, _ActionBase): """ - A custom Action class based on gtk.Action. + A custom Action class based on Gtk.Action. Pass additional arguments such as keypresses. """ @@ -127,18 +136,18 @@ class Action(gtk.Action, _ActionBase): Args: key_presses: a tuple of (keyval1, mod_mask1, keyval2, mod_mask2, ...) - the: regular gtk.Action parameters (defaults to None) + the: regular Gtk.Action parameters (defaults to None) """ if name is None: name = label - gtk.Action.__init__(self, name=name, label=label, tooltip=tooltip, + GObject.GObject.__init__(self, name=name, label=label, tooltip=tooltip, stock_id=stock_id) _ActionBase.__init__(self, label, keypresses) -class ToggleAction(gtk.ToggleAction, _ActionBase): +class ToggleAction(Gtk.ToggleAction, _ActionBase): """ - A custom Action class based on gtk.ToggleAction. + A custom Action class based on Gtk.ToggleAction. Pass additional arguments such as keypresses. """ @@ -149,11 +158,11 @@ class ToggleAction(gtk.ToggleAction, _ActionBase): Args: key_presses: a tuple of (keyval1, mod_mask1, keyval2, mod_mask2, ...) - the: regular gtk.Action parameters (defaults to None) + the: regular Gtk.Action parameters (defaults to None) """ if name is None: name = label - gtk.ToggleAction.__init__(self, name=name, label=label, + GObject.GObject.__init__(self, name=name, label=label, tooltip=tooltip, stock_id=stock_id) _ActionBase.__init__(self, label, keypresses) self.preference_name = preference_name @@ -177,114 +186,114 @@ VARIABLE_EDITOR_UPDATE = Action() FLOW_GRAPH_NEW = Action( label='_New', tooltip='Create a new flow graph', - stock_id=gtk.STOCK_NEW, - keypresses=(gtk.keysyms.n, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_NEW, + keypresses=(Gdk.KEY_n, Gdk.ModifierType.CONTROL_MASK), ) FLOW_GRAPH_OPEN = Action( label='_Open', tooltip='Open an existing flow graph', - stock_id=gtk.STOCK_OPEN, - keypresses=(gtk.keysyms.o, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_OPEN, + keypresses=(Gdk.KEY_o, Gdk.ModifierType.CONTROL_MASK), ) FLOW_GRAPH_OPEN_RECENT = Action( label='Open _Recent', tooltip='Open a recently used flow graph', - stock_id=gtk.STOCK_OPEN, + stock_id=Gtk.STOCK_OPEN, ) FLOW_GRAPH_SAVE = Action( label='_Save', tooltip='Save the current flow graph', - stock_id=gtk.STOCK_SAVE, - keypresses=(gtk.keysyms.s, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_SAVE, + keypresses=(Gdk.KEY_s, Gdk.ModifierType.CONTROL_MASK), ) FLOW_GRAPH_SAVE_AS = Action( label='Save _As', tooltip='Save the current flow graph as...', - stock_id=gtk.STOCK_SAVE_AS, - keypresses=(gtk.keysyms.s, gtk.gdk.CONTROL_MASK | gtk.gdk.SHIFT_MASK), + stock_id=Gtk.STOCK_SAVE_AS, + keypresses=(Gdk.KEY_s, Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK), ) FLOW_GRAPH_CLOSE = Action( label='_Close', tooltip='Close the current flow graph', - stock_id=gtk.STOCK_CLOSE, - keypresses=(gtk.keysyms.w, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_CLOSE, + keypresses=(Gdk.KEY_w, Gdk.ModifierType.CONTROL_MASK), ) APPLICATION_INITIALIZE = Action() APPLICATION_QUIT = Action( label='_Quit', tooltip='Quit program', - stock_id=gtk.STOCK_QUIT, - keypresses=(gtk.keysyms.q, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_QUIT, + keypresses=(Gdk.KEY_q, Gdk.ModifierType.CONTROL_MASK), ) FLOW_GRAPH_UNDO = Action( label='_Undo', tooltip='Undo a change to the flow graph', - stock_id=gtk.STOCK_UNDO, - keypresses=(gtk.keysyms.z, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_UNDO, + keypresses=(Gdk.KEY_z, Gdk.ModifierType.CONTROL_MASK), ) FLOW_GRAPH_REDO = Action( label='_Redo', tooltip='Redo a change to the flow graph', - stock_id=gtk.STOCK_REDO, - keypresses=(gtk.keysyms.y, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_REDO, + keypresses=(Gdk.KEY_y, Gdk.ModifierType.CONTROL_MASK), ) NOTHING_SELECT = Action() SELECT_ALL = Action( label='Select _All', tooltip='Select all blocks and connections in the flow graph', - stock_id=gtk.STOCK_SELECT_ALL, - keypresses=(gtk.keysyms.a, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_SELECT_ALL, + keypresses=(Gdk.KEY_a, Gdk.ModifierType.CONTROL_MASK), ) ELEMENT_SELECT = Action() ELEMENT_CREATE = Action() ELEMENT_DELETE = Action( label='_Delete', tooltip='Delete the selected blocks', - stock_id=gtk.STOCK_DELETE, - keypresses=(gtk.keysyms.Delete, NO_MODS_MASK), + stock_id=Gtk.STOCK_DELETE, + keypresses=(Gdk.KEY_Delete, NO_MODS_MASK), ) BLOCK_MOVE = Action() BLOCK_ROTATE_CCW = Action( label='Rotate Counterclockwise', tooltip='Rotate the selected blocks 90 degrees to the left', - stock_id=gtk.STOCK_GO_BACK, - keypresses=(gtk.keysyms.Left, NO_MODS_MASK), + stock_id=Gtk.STOCK_GO_BACK, + keypresses=(Gdk.KEY_Left, NO_MODS_MASK), ) BLOCK_ROTATE_CW = Action( label='Rotate Clockwise', tooltip='Rotate the selected blocks 90 degrees to the right', - stock_id=gtk.STOCK_GO_FORWARD, - keypresses=(gtk.keysyms.Right, NO_MODS_MASK), + stock_id=Gtk.STOCK_GO_FORWARD, + keypresses=(Gdk.KEY_Right, NO_MODS_MASK), ) BLOCK_VALIGN_TOP = Action( label='Vertical Align Top', tooltip='Align tops of selected blocks', - keypresses=(gtk.keysyms.t, gtk.gdk.SHIFT_MASK), + keypresses=(Gdk.KEY_t, Gdk.ModifierType.SHIFT_MASK), ) BLOCK_VALIGN_MIDDLE = Action( label='Vertical Align Middle', tooltip='Align centers of selected blocks vertically', - keypresses=(gtk.keysyms.m, gtk.gdk.SHIFT_MASK), + keypresses=(Gdk.KEY_m, Gdk.ModifierType.SHIFT_MASK), ) BLOCK_VALIGN_BOTTOM = Action( label='Vertical Align Bottom', tooltip='Align bottoms of selected blocks', - keypresses=(gtk.keysyms.b, gtk.gdk.SHIFT_MASK), + keypresses=(Gdk.KEY_b, Gdk.ModifierType.SHIFT_MASK), ) BLOCK_HALIGN_LEFT = Action( label='Horizontal Align Left', tooltip='Align left edges of blocks selected blocks', - keypresses=(gtk.keysyms.l, gtk.gdk.SHIFT_MASK), + keypresses=(Gdk.KEY_l, Gdk.ModifierType.SHIFT_MASK), ) BLOCK_HALIGN_CENTER = Action( label='Horizontal Align Center', tooltip='Align centers of selected blocks horizontally', - keypresses=(gtk.keysyms.c, gtk.gdk.SHIFT_MASK), + keypresses=(Gdk.KEY_c, Gdk.ModifierType.SHIFT_MASK), ) BLOCK_HALIGN_RIGHT = Action( label='Horizontal Align Right', tooltip='Align right edges of selected blocks', - keypresses=(gtk.keysyms.r, gtk.gdk.SHIFT_MASK), + keypresses=(Gdk.KEY_r, Gdk.ModifierType.SHIFT_MASK), ) BLOCK_ALIGNMENTS = [ BLOCK_VALIGN_TOP, @@ -298,26 +307,26 @@ BLOCK_ALIGNMENTS = [ BLOCK_PARAM_MODIFY = Action( label='_Properties', tooltip='Modify params for the selected block', - stock_id=gtk.STOCK_PROPERTIES, - keypresses=(gtk.keysyms.Return, NO_MODS_MASK), + stock_id=Gtk.STOCK_PROPERTIES, + keypresses=(Gdk.KEY_Return, NO_MODS_MASK), ) BLOCK_ENABLE = Action( label='E_nable', tooltip='Enable the selected blocks', - stock_id=gtk.STOCK_CONNECT, - keypresses=(gtk.keysyms.e, NO_MODS_MASK), + stock_id=Gtk.STOCK_CONNECT, + keypresses=(Gdk.KEY_e, NO_MODS_MASK), ) BLOCK_DISABLE = Action( label='D_isable', tooltip='Disable the selected blocks', - stock_id=gtk.STOCK_DISCONNECT, - keypresses=(gtk.keysyms.d, NO_MODS_MASK), + stock_id=Gtk.STOCK_DISCONNECT, + keypresses=(Gdk.KEY_d, NO_MODS_MASK), ) BLOCK_BYPASS = Action( label='_Bypass', tooltip='Bypass the selected block', - stock_id=gtk.STOCK_MEDIA_FORWARD, - keypresses=(gtk.keysyms.b, NO_MODS_MASK), + stock_id=Gtk.STOCK_MEDIA_FORWARD, + keypresses=(Gdk.KEY_b, NO_MODS_MASK), ) TOGGLE_SNAP_TO_GRID = ToggleAction( label='_Snap to grid', @@ -327,8 +336,8 @@ TOGGLE_SNAP_TO_GRID = ToggleAction( TOGGLE_HIDE_DISABLED_BLOCKS = ToggleAction( label='Hide _Disabled Blocks', tooltip='Toggle visibility of disabled blocks and connections', - stock_id=gtk.STOCK_MISSING_IMAGE, - keypresses=(gtk.keysyms.d, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_MISSING_IMAGE, + keypresses=(Gdk.KEY_d, Gdk.ModifierType.CONTROL_MASK), ) TOGGLE_HIDE_VARIABLES = ToggleAction( label='Hide Variables', @@ -339,9 +348,9 @@ TOGGLE_HIDE_VARIABLES = ToggleAction( TOGGLE_FLOW_GRAPH_VAR_EDITOR = ToggleAction( label='Show _Variable Editor', tooltip='Show the variable editor. Modify variables and imports in this flow graph', - stock_id=gtk.STOCK_EDIT, + stock_id=Gtk.STOCK_EDIT, default=True, - keypresses=(gtk.keysyms.e, gtk.gdk.CONTROL_MASK), + keypresses=(Gdk.KEY_e, Gdk.ModifierType.CONTROL_MASK), preference_name='variable_editor_visable', ) TOGGLE_FLOW_GRAPH_VAR_EDITOR_SIDEBAR = ToggleAction( @@ -376,42 +385,42 @@ TOGGLE_SHOW_FLOWGRAPH_COMPLEXITY = ToggleAction( BLOCK_CREATE_HIER = Action( label='C_reate Hier', tooltip='Create hier block from selected blocks', - stock_id=gtk.STOCK_CONNECT, -# keypresses=(gtk.keysyms.c, NO_MODS_MASK), + stock_id=Gtk.STOCK_CONNECT, +# keypresses=(Gdk.KEY_c, NO_MODS_MASK), ) BLOCK_CUT = Action( label='Cu_t', tooltip='Cut', - stock_id=gtk.STOCK_CUT, - keypresses=(gtk.keysyms.x, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_CUT, + keypresses=(Gdk.KEY_x, Gdk.ModifierType.CONTROL_MASK), ) BLOCK_COPY = Action( label='_Copy', tooltip='Copy', - stock_id=gtk.STOCK_COPY, - keypresses=(gtk.keysyms.c, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_COPY, + keypresses=(Gdk.KEY_c, Gdk.ModifierType.CONTROL_MASK), ) BLOCK_PASTE = Action( label='_Paste', tooltip='Paste', - stock_id=gtk.STOCK_PASTE, - keypresses=(gtk.keysyms.v, gtk.gdk.CONTROL_MASK), + stock_id=Gtk.STOCK_PASTE, + keypresses=(Gdk.KEY_v, Gdk.ModifierType.CONTROL_MASK), ) ERRORS_WINDOW_DISPLAY = Action( label='Flowgraph _Errors', tooltip='View flow graph errors', - stock_id=gtk.STOCK_DIALOG_ERROR, + stock_id=Gtk.STOCK_DIALOG_ERROR, ) TOGGLE_CONSOLE_WINDOW = ToggleAction( label='Show _Console Panel', tooltip='Toggle visibility of the console', - keypresses=(gtk.keysyms.r, gtk.gdk.CONTROL_MASK), + keypresses=(Gdk.KEY_r, Gdk.ModifierType.CONTROL_MASK), preference_name='console_window_visible' ) TOGGLE_BLOCKS_WINDOW = ToggleAction( label='Show _Block Tree Panel', tooltip='Toggle visibility of the block tree widget', - keypresses=(gtk.keysyms.b, gtk.gdk.CONTROL_MASK), + keypresses=(Gdk.KEY_b, Gdk.ModifierType.CONTROL_MASK), preference_name='blocks_window_visible' ) TOGGLE_SCROLL_LOCK = ToggleAction( @@ -422,106 +431,106 @@ TOGGLE_SCROLL_LOCK = ToggleAction( ABOUT_WINDOW_DISPLAY = Action( label='_About', tooltip='About this program', - stock_id=gtk.STOCK_ABOUT, + stock_id=Gtk.STOCK_ABOUT, ) HELP_WINDOW_DISPLAY = Action( label='_Help', tooltip='Usage tips', - stock_id=gtk.STOCK_HELP, - keypresses=(gtk.keysyms.F1, NO_MODS_MASK), + stock_id=Gtk.STOCK_HELP, + keypresses=(Gdk.KEY_F1, NO_MODS_MASK), ) TYPES_WINDOW_DISPLAY = Action( label='_Types', tooltip='Types color mapping', - stock_id=gtk.STOCK_DIALOG_INFO, + stock_id=Gtk.STOCK_DIALOG_INFO, ) FLOW_GRAPH_GEN = Action( label='_Generate', tooltip='Generate the flow graph', - stock_id=gtk.STOCK_CONVERT, - keypresses=(gtk.keysyms.F5, NO_MODS_MASK), + stock_id=Gtk.STOCK_CONVERT, + keypresses=(Gdk.KEY_F5, NO_MODS_MASK), ) FLOW_GRAPH_EXEC = Action( label='_Execute', tooltip='Execute the flow graph', - stock_id=gtk.STOCK_MEDIA_PLAY, - keypresses=(gtk.keysyms.F6, NO_MODS_MASK), + stock_id=Gtk.STOCK_MEDIA_PLAY, + keypresses=(Gdk.KEY_F6, NO_MODS_MASK), ) FLOW_GRAPH_KILL = Action( label='_Kill', tooltip='Kill the flow graph', - stock_id=gtk.STOCK_STOP, - keypresses=(gtk.keysyms.F7, NO_MODS_MASK), + stock_id=Gtk.STOCK_STOP, + keypresses=(Gdk.KEY_F7, NO_MODS_MASK), ) FLOW_GRAPH_SCREEN_CAPTURE = Action( label='Screen Ca_pture', tooltip='Create a screen capture of the flow graph', - stock_id=gtk.STOCK_PRINT, - keypresses=(gtk.keysyms.Print, NO_MODS_MASK), + stock_id=Gtk.STOCK_PRINT, + keypresses=(Gdk.KEY_p, Gdk.ModifierType.CONTROL_MASK), ) PORT_CONTROLLER_DEC = Action( - keypresses=(gtk.keysyms.minus, NO_MODS_MASK, gtk.keysyms.KP_Subtract, NO_MODS_MASK), + keypresses=(Gdk.KEY_minus, NO_MODS_MASK, Gdk.KEY_KP_Subtract, NO_MODS_MASK), ) PORT_CONTROLLER_INC = Action( - keypresses=(gtk.keysyms.plus, NO_MODS_MASK, gtk.keysyms.KP_Add, NO_MODS_MASK), + keypresses=(Gdk.KEY_plus, NO_MODS_MASK, Gdk.KEY_KP_Add, NO_MODS_MASK), ) BLOCK_INC_TYPE = Action( - keypresses=(gtk.keysyms.Down, NO_MODS_MASK), + keypresses=(Gdk.KEY_Down, NO_MODS_MASK), ) BLOCK_DEC_TYPE = Action( - keypresses=(gtk.keysyms.Up, NO_MODS_MASK), + keypresses=(Gdk.KEY_Up, NO_MODS_MASK), ) RELOAD_BLOCKS = Action( label='Reload _Blocks', tooltip='Reload Blocks', - stock_id=gtk.STOCK_REFRESH + stock_id=Gtk.STOCK_REFRESH ) FIND_BLOCKS = Action( label='_Find Blocks', tooltip='Search for a block by name (and key)', - stock_id=gtk.STOCK_FIND, - keypresses=(gtk.keysyms.f, gtk.gdk.CONTROL_MASK, - gtk.keysyms.slash, NO_MODS_MASK), + stock_id=Gtk.STOCK_FIND, + keypresses=(Gdk.KEY_f, Gdk.ModifierType.CONTROL_MASK, + Gdk.KEY_slash, NO_MODS_MASK), ) CLEAR_CONSOLE = Action( label='_Clear Console', tooltip='Clear Console', - stock_id=gtk.STOCK_CLEAR, + stock_id=Gtk.STOCK_CLEAR, ) SAVE_CONSOLE = Action( label='_Save Console', tooltip='Save Console', - stock_id=gtk.STOCK_SAVE, + stock_id=Gtk.STOCK_SAVE, ) OPEN_HIER = Action( label='Open H_ier', tooltip='Open the source of the selected hierarchical block', - stock_id=gtk.STOCK_JUMP_TO, + stock_id=Gtk.STOCK_JUMP_TO, ) BUSSIFY_SOURCES = Action( label='Toggle So_urce Bus', tooltip='Gang source ports into a single bus port', - stock_id=gtk.STOCK_JUMP_TO, + stock_id=Gtk.STOCK_JUMP_TO, ) BUSSIFY_SINKS = Action( label='Toggle S_ink Bus', tooltip='Gang sink ports into a single bus port', - stock_id=gtk.STOCK_JUMP_TO, + stock_id=Gtk.STOCK_JUMP_TO, ) XML_PARSER_ERRORS_DISPLAY = Action( label='_Parser Errors', tooltip='View errors that occurred while parsing XML files', - stock_id=gtk.STOCK_DIALOG_ERROR, + stock_id=Gtk.STOCK_DIALOG_ERROR, ) FLOW_GRAPH_OPEN_QSS_THEME = Action( label='Set Default QT GUI _Theme', tooltip='Set a default QT Style Sheet file to use for QT GUI', - stock_id=gtk.STOCK_OPEN, + stock_id=Gtk.STOCK_OPEN, ) TOOLS_RUN_FDESIGN = Action( label='Filter Design Tool', tooltip='Execute gr_filter_design', - stock_id=gtk.STOCK_EXECUTE, + stock_id=Gtk.STOCK_EXECUTE, ) TOOLS_MORE_TO_COME = Action( label='More to come', diff --git a/grc/gui/Bars.py b/grc/gui/Bars.py index a4819b973c..d8d57843d7 100644 --- a/grc/gui/Bars.py +++ b/grc/gui/Bars.py @@ -17,9 +17,11 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk +from __future__ import absolute_import +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +from gi.repository import GObject from . import Actions @@ -60,9 +62,10 @@ TOOLBAR_LIST = ( Actions.OPEN_HIER, ) + # The list of actions and categories for the menu bar. MENU_BAR_LIST = ( - (gtk.Action('File', '_File', None, None), [ + (Gtk.Action(name='File', label='_File'), [ 'flow_graph_new', Actions.FLOW_GRAPH_OPEN, 'flow_graph_recent', @@ -75,7 +78,7 @@ MENU_BAR_LIST = ( Actions.FLOW_GRAPH_CLOSE, Actions.APPLICATION_QUIT, ]), - (gtk.Action('Edit', '_Edit', None, None), [ + (Gtk.Action(name='Edit', label='_Edit'), [ Actions.FLOW_GRAPH_UNDO, Actions.FLOW_GRAPH_REDO, None, @@ -87,7 +90,7 @@ MENU_BAR_LIST = ( None, Actions.BLOCK_ROTATE_CCW, Actions.BLOCK_ROTATE_CW, - (gtk.Action('Align', '_Align', None, None), Actions.BLOCK_ALIGNMENTS), + (Gtk.Action(name='Align', label='_Align', tooltip=None, stock_id=None), Actions.BLOCK_ALIGNMENTS), None, Actions.BLOCK_ENABLE, Actions.BLOCK_DISABLE, @@ -95,7 +98,7 @@ MENU_BAR_LIST = ( None, Actions.BLOCK_PARAM_MODIFY, ]), - (gtk.Action('View', '_View', None, None), [ + (Gtk.Action(name='View', label='_View'), [ Actions.TOGGLE_BLOCKS_WINDOW, None, Actions.TOGGLE_CONSOLE_WINDOW, @@ -117,12 +120,12 @@ MENU_BAR_LIST = ( Actions.ERRORS_WINDOW_DISPLAY, Actions.FIND_BLOCKS, ]), - (gtk.Action('Run', '_Run', None, None), [ + (Gtk.Action(name='Run', label='_Run'), [ Actions.FLOW_GRAPH_GEN, Actions.FLOW_GRAPH_EXEC, Actions.FLOW_GRAPH_KILL, ]), - (gtk.Action('Tools', '_Tools', None, None), [ + (Gtk.Action(name='Tools', label='_Tools'), [ Actions.TOOLS_RUN_FDESIGN, Actions.FLOW_GRAPH_OPEN_QSS_THEME, None, @@ -130,7 +133,7 @@ MENU_BAR_LIST = ( None, Actions.TOOLS_MORE_TO_COME, ]), - (gtk.Action('Help', '_Help', None, None), [ + (Gtk.Action(name='Help', label='_Help'), [ Actions.HELP_WINDOW_DISPLAY, Actions.TYPES_WINDOW_DISPLAY, Actions.XML_PARSER_ERRORS_DISPLAY, @@ -139,6 +142,7 @@ MENU_BAR_LIST = ( ]), ) + # The list of actions for the context menu. CONTEXT_MENU_LIST = [ Actions.BLOCK_CUT, @@ -152,7 +156,7 @@ CONTEXT_MENU_LIST = [ Actions.BLOCK_DISABLE, Actions.BLOCK_BYPASS, None, - (gtk.Action('More', '_More', None, None), [ + (Gtk.Action(name='More', label='_More'), [ Actions.BLOCK_CREATE_HIER, Actions.OPEN_HIER, None, @@ -189,13 +193,13 @@ class SubMenuCreator(object): def _fill_flow_graph_new_submenu(self, action): """Sub menu to create flow-graph with pre-set generate mode""" - menu = gtk.Menu() + menu = Gtk.Menu() for key, name, default in self.generate_modes: if default: item = Actions.FLOW_GRAPH_NEW.create_menu_item() item.set_label(name) else: - item = gtk.MenuItem(name, use_underline=False) + item = Gtk.MenuItem(name=name, use_underline=False) item.connect('activate', self.callback_adaptor, (action, key)) menu.append(item) menu.show_all() @@ -203,12 +207,12 @@ class SubMenuCreator(object): def _fill_flow_graph_recent_submenu(self, action): """menu showing recent flow-graphs""" - import Preferences - menu = gtk.Menu() + from . import Preferences + menu = Gtk.Menu() recent_files = Preferences.get_recent_files() if len(recent_files) > 0: for i, file_name in enumerate(recent_files): - item = gtk.MenuItem("%d. %s" % (i+1, file_name), use_underline=False) + item = Gtk.MenuItem(name="%d. %s" % (i+1, file_name), use_underline=False) item.connect('activate', self.callback_adaptor, (action, file_name)) menu.append(item) @@ -217,7 +221,7 @@ class SubMenuCreator(object): return None -class Toolbar(gtk.Toolbar, SubMenuCreator): +class Toolbar(Gtk.Toolbar, SubMenuCreator): """The gtk toolbar with actions added from the toolbar list.""" def __init__(self, generate_modes, action_handler_callback): @@ -226,23 +230,25 @@ class Toolbar(gtk.Toolbar, SubMenuCreator): Look up the action for each name in the action list and add it to the toolbar. """ - gtk.Toolbar.__init__(self) - self.set_style(gtk.TOOLBAR_ICONS) + GObject.GObject.__init__(self) + self.set_style(Gtk.ToolbarStyle.ICONS) SubMenuCreator.__init__(self, generate_modes, action_handler_callback) for action in TOOLBAR_LIST: if isinstance(action, tuple) and isinstance(action[1], str): # create a button with a sub-menu - action[0].set_tool_item_type(gtk.MenuToolButton) + # TODO: Fix later + #action[0].set_tool_item_type(Gtk.MenuToolButton) item = action[0].create_tool_item() - self.create_submenu(action, item) - self.refresh_submenus() + #self.create_submenu(action, item) + #self.refresh_submenus() elif action is None: - item = gtk.SeparatorToolItem() + item = Gtk.SeparatorToolItem() else: - action.set_tool_item_type(gtk.ToolButton) + #TODO: Fix later + #action.set_tool_item_type(Gtk.ToolButton) item = action.create_tool_item() # this reset of the tooltip property is required # (after creating the tool item) for the tooltip to show @@ -255,14 +261,14 @@ class MenuHelperMixin(object): def _fill_menu(self, actions, menu=None): """Create a menu from list of actions""" - menu = menu or gtk.Menu() + menu = menu or Gtk.Menu() for item in actions: if isinstance(item, tuple): menu_item = self._make_sub_menu(*item) elif isinstance(item, str): menu_item = getattr(self, 'create_' + item)() elif item is None: - menu_item = gtk.SeparatorMenuItem() + menu_item = Gtk.SeparatorMenuItem() else: menu_item = item.create_menu_item() menu.append(menu_item) @@ -276,7 +282,7 @@ class MenuHelperMixin(object): return main -class MenuBar(gtk.MenuBar, MenuHelperMixin, SubMenuCreator): +class MenuBar(Gtk.MenuBar, MenuHelperMixin, SubMenuCreator): """The gtk menu bar with actions added from the menu bar list.""" def __init__(self, generate_modes, action_handler_callback): @@ -286,13 +292,13 @@ class MenuBar(gtk.MenuBar, MenuHelperMixin, SubMenuCreator): Look up the action for each name in the action list and add it to the submenu. Add the submenu to the menu bar. """ - gtk.MenuBar.__init__(self) + GObject.GObject.__init__(self) SubMenuCreator.__init__(self, generate_modes, action_handler_callback) for main_action, actions in MENU_BAR_LIST: self.append(self._make_sub_menu(main_action, actions)) def create_flow_graph_new(self): - main = gtk.ImageMenuItem(gtk.STOCK_NEW) + main = Gtk.ImageMenuItem(label=Gtk.STOCK_NEW) main.set_label(Actions.FLOW_GRAPH_NEW.get_label()) func = self._fill_flow_graph_new_submenu self.submenus.append((Actions.FLOW_GRAPH_NEW, func, main)) @@ -300,7 +306,7 @@ class MenuBar(gtk.MenuBar, MenuHelperMixin, SubMenuCreator): return main def create_flow_graph_recent(self): - main = gtk.ImageMenuItem(gtk.STOCK_OPEN) + main = Gtk.ImageMenuItem(label=Gtk.STOCK_OPEN) main.set_label(Actions.FLOW_GRAPH_OPEN_RECENT.get_label()) func = self._fill_flow_graph_recent_submenu self.submenus.append((Actions.FLOW_GRAPH_OPEN, func, main)) @@ -310,9 +316,9 @@ class MenuBar(gtk.MenuBar, MenuHelperMixin, SubMenuCreator): return main -class ContextMenu(gtk.Menu, MenuHelperMixin): +class ContextMenu(Gtk.Menu, MenuHelperMixin): """The gtk menu with actions added from the context menu list.""" def __init__(self): - gtk.Menu.__init__(self) + GObject.GObject.__init__(self) self._fill_menu(CONTEXT_MENU_LIST, self) diff --git a/grc/gui/Block.py b/grc/gui/Block.py index 55c8805fae..b37bec6dfa 100644 --- a/grc/gui/Block.py +++ b/grc/gui/Block.py @@ -17,101 +17,60 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk -import pango +from __future__ import absolute_import +import math + +import six +from gi.repository import Gtk, Pango, PangoCairo from . import Actions, Colors, Utils, Constants +from .Constants import ( + BLOCK_LABEL_PADDING, PORT_SPACING, PORT_SEPARATION, LABEL_SEPARATION, + PORT_BORDER_SEPARATION, BLOCK_FONT, PARAM_FONT +) from . Element import Element -from ..core.Param import num_to_str -from ..core.utils import odict -from ..core.utils.complexity import calculate_flowgraph_complexity -from ..core.Block import Block as _Block - -BLOCK_MARKUP_TMPL="""\ -#set $foreground = $block.is_valid() and 'black' or 'red' -<span foreground="$foreground" font_desc="$font"><b>$encode($block.get_name())</b></span>""" - -# Includes the additional complexity markup if enabled -COMMENT_COMPLEXITY_MARKUP_TMPL="""\ -#set $foreground = $block.get_enabled() and '#444' or '#888' -#if $complexity -<span foreground="#444" size="medium" font_desc="$font"><b>$encode($complexity)</b></span>#slurp -#end if -#if $complexity and $comment -<span></span> -#end if -#if $comment -<span foreground="$foreground" font_desc="$font">$encode($comment)</span>#slurp -#end if -""" +from ..core import utils +from ..core.Block import Block as CoreBlock -class Block(Element, _Block): +class Block(CoreBlock, Element): """The graphical signal block.""" - def __init__(self, flow_graph, n): + def __init__(self, parent, **n): """ - Block contructor. + Block constructor. Add graphics related params to the block. """ - _Block.__init__(self, flow_graph, n) - - self.W = 0 - self.H = 0 - #add the position param - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({ - 'name': 'GUI Coordinate', - 'key': '_coordinate', - 'type': 'raw', - 'value': '(0, 0)', - 'hide': 'all', - }) - )) - self.get_params().append(self.get_parent().get_parent().Param( - block=self, - n=odict({ - 'name': 'GUI Rotation', - 'key': '_rotation', - 'type': 'raw', - 'value': '0', - 'hide': 'all', - }) - )) - Element.__init__(self) - self._comment_pixmap = None - self.has_busses = [False, False] # source, sink - - def get_coordinate(self): + super(self.__class__, self).__init__(parent, **n) + + self.states.update(_coordinate=(0, 0), _rotation=0) + self.width = self.height = 0 + Element.__init__(self) # needs the states and initial sizes + + self._surface_layouts = [ + Gtk.DrawingArea().create_pango_layout(''), # title + Gtk.DrawingArea().create_pango_layout(''), # params + ] + self._surface_layout_offsets = 0, 0 + self._comment_layout = None + + self._area = [] + self._border_color = self._bg_color = Colors.BLOCK_ENABLED_COLOR + self._font_color = list(Colors.FONT_COLOR) + + @property + def coordinate(self): """ Get the coordinate from the position param. Returns: the coordinate tuple (x, y) or (0, 0) if failure """ - proximity = Constants.BORDER_PROXIMITY_SENSITIVITY - try: #should evaluate to tuple - coor = eval(self.get_param('_coordinate').get_value()) - x, y = map(int, coor) - fgW,fgH = self.get_parent().get_size() - if x <= 0: - x = 0 - elif x >= fgW - proximity: - x = fgW - proximity - if y <= 0: - y = 0 - elif y >= fgH - proximity: - y = fgH - proximity - return (x, y) - except: - self.set_coordinate((0, 0)) - return (0, 0) + return self.states['_coordinate'] - def set_coordinate(self, coor): + @coordinate.setter + def coordinate(self, coor): """ Set the coordinate into the position param. @@ -119,208 +78,202 @@ class Block(Element, _Block): coor: the coordinate tuple (x, y) """ if Actions.TOGGLE_SNAP_TO_GRID.get_active(): - offset_x, offset_y = (0, self.H/2) if self.is_horizontal() else (self.H/2, 0) + offset_x, offset_y = (0, self.height / 2) if self.is_horizontal() else (self.height / 2, 0) coor = ( Utils.align_to_grid(coor[0] + offset_x) - offset_x, Utils.align_to_grid(coor[1] + offset_y) - offset_y ) - self.get_param('_coordinate').set_value(str(coor)) - - def bound_move_delta(self, delta_coor): - """ - Limit potential moves from exceeding the bounds of the canvas - - Args: - delta_coor: requested delta coordinate (dX, dY) to move - - Returns: - The delta coordinate possible to move while keeping the block on the canvas - or the input (dX, dY) on failure - """ - dX, dY = delta_coor - - try: - fgW, fgH = self.get_parent().get_size() - x, y = map(int, eval(self.get_param("_coordinate").get_value())) - if self.is_horizontal(): - sW, sH = self.W, self.H - else: - sW, sH = self.H, self.W - - if x + dX < 0: - dX = -x - elif dX + x + sW >= fgW: - dX = fgW - x - sW - if y + dY < 0: - dY = -y - elif dY + y + sH >= fgH: - dY = fgH - y - sH - except: - pass + self.states['_coordinate'] = coor - return ( dX, dY ) - - def get_rotation(self): + @property + def rotation(self): """ Get the rotation from the position param. Returns: the rotation in degrees or 0 if failure """ - try: #should evaluate to dict - rotation = eval(self.get_param('_rotation').get_value()) - return int(rotation) - except: - self.set_rotation(Constants.POSSIBLE_ROTATIONS[0]) - return Constants.POSSIBLE_ROTATIONS[0] + return self.states['_rotation'] - def set_rotation(self, rot): + @rotation.setter + def rotation(self, rot): """ Set the rotation into the position param. Args: rot: the rotation in degrees """ - self.get_param('_rotation').set_value(str(rot)) + self.states['_rotation'] = rot + + def _update_colors(self): + self._bg_color = ( + Colors.MISSING_BLOCK_BACKGROUND_COLOR if self.is_dummy_block else + Colors.BLOCK_BYPASSED_COLOR if self.state == 'bypassed' else + Colors.BLOCK_ENABLED_COLOR if self.state == 'enabled' else + Colors.BLOCK_DISABLED_COLOR + ) + self._font_color[-1] = 1.0 if self.state == 'enabled' else 0.4 + self._border_color = ( + Colors.MISSING_BLOCK_BORDER_COLOR if self.is_dummy_block else + Colors.BORDER_COLOR_DISABLED if not self.state == 'enabled' else Colors.BORDER_COLOR + ) def create_shapes(self): """Update the block, parameters, and ports when a change occurs.""" - Element.create_shapes(self) - if self.is_horizontal(): self.add_area((0, 0), (self.W, self.H)) - elif self.is_vertical(): self.add_area((0, 0), (self.H, self.W)) + if self.is_horizontal(): + self._area = (0, 0, self.width, self.height) + elif self.is_vertical(): + self._area = (0, 0, self.height, self.width) + self.bounds_from_area(self._area) + + bussified = self.current_bus_structure['source'], self.current_bus_structure['sink'] + for ports, has_busses in zip((self.active_sources, self.active_sinks), bussified): + if not ports: + continue + port_separation = PORT_SEPARATION if not has_busses else ports[0].height + PORT_SPACING + offset = (self.height - (len(ports) - 1) * port_separation - ports[0].height) / 2 + for index, port in enumerate(ports): + port.create_shapes() + + port.coordinate = { + 0: (+self.width, offset), + 90: (offset, -port.width), + 180: (-port.width, offset), + 270: (offset, +self.width), + }[port.get_connector_direction()] + offset += PORT_SEPARATION if not has_busses else port.height + PORT_SPACING + + port.connector_length = Constants.CONNECTOR_EXTENSION_MINIMAL + \ + Constants.CONNECTOR_EXTENSION_INCREMENT * index def create_labels(self): """Create the labels for the signal block.""" - Element.create_labels(self) - self._bg_color = self.is_dummy_block and Colors.MISSING_BLOCK_BACKGROUND_COLOR or \ - self.get_bypassed() and Colors.BLOCK_BYPASSED_COLOR or \ - self.get_enabled() and Colors.BLOCK_ENABLED_COLOR or Colors.BLOCK_DISABLED_COLOR - - layouts = list() - #create the main layout - layout = gtk.DrawingArea().create_pango_layout('') - layouts.append(layout) - layout.set_markup(Utils.parse_template(BLOCK_MARKUP_TMPL, block=self, font=Constants.BLOCK_FONT)) - self.label_width, self.label_height = layout.get_pixel_size() - #display the params - if self.is_dummy_block: - markups = [ - '<span foreground="black" font_desc="{font}"><b>key: </b>{key}</span>' - ''.format(font=Constants.PARAM_FONT, key=self._key) - ] + title_layout, params_layout = self._surface_layouts + + title_layout.set_markup( + '<span {foreground} font_desc="{font}"><b>{name}</b></span>'.format( + foreground='foreground="red"' if not self.is_valid() else '', font=BLOCK_FONT, + name=Utils.encode(self.name) + ) + ) + title_width, title_height = title_layout.get_pixel_size() + + # update the params layout + if not self.is_dummy_block: + markups = [param.format_block_surface_markup() + for param in self.params.values() if param.get_hide() not in ('all', 'part')] else: - markups = [param.get_markup() for param in self.get_params() if param.get_hide() not in ('all', 'part')] - if markups: - layout = gtk.DrawingArea().create_pango_layout('') - layout.set_spacing(Constants.LABEL_SEPARATION * pango.SCALE) - layout.set_markup('\n'.join(markups)) - layouts.append(layout) - w, h = layout.get_pixel_size() - self.label_width = max(w, self.label_width) - self.label_height += h + Constants.LABEL_SEPARATION - width = self.label_width - height = self.label_height - #setup the pixmap - pixmap = self.get_parent().new_pixmap(width, height) - gc = pixmap.new_gc() - gc.set_foreground(self._bg_color) - pixmap.draw_rectangle(gc, True, 0, 0, width, height) - #draw the layouts - h_off = 0 - for i,layout in enumerate(layouts): - w,h = layout.get_pixel_size() - if i == 0: w_off = (width-w)/2 - else: w_off = 0 - pixmap.draw_layout(gc, w_off, h_off, layout) - h_off = h + h_off + Constants.LABEL_SEPARATION - #create vertical and horizontal pixmaps - self.horizontal_label = pixmap - if self.is_vertical(): - self.vertical_label = self.get_parent().new_pixmap(height, width) - Utils.rotate_pixmap(gc, self.horizontal_label, self.vertical_label) - #calculate width and height needed - W = self.label_width + 2 * Constants.BLOCK_LABEL_PADDING - - def get_min_height_for_ports(): - visible_ports = filter(lambda p: not p.get_hide(), ports) - min_height = 2*Constants.PORT_BORDER_SEPARATION + len(visible_ports) * Constants.PORT_SEPARATION - if visible_ports: - min_height -= ports[0].H + markups = ['<span font_desc="{font}"><b>key: </b>{key}</span>'.format(font=PARAM_FONT, key=self.key)] + + params_layout.set_spacing(LABEL_SEPARATION * Pango.SCALE) + params_layout.set_markup('\n'.join(markups)) + params_width, params_height = params_layout.get_pixel_size() if markups else (0, 0) + + label_width = max(title_width, params_width) + label_height = title_height + LABEL_SEPARATION + params_height + + title_layout.set_width(label_width * Pango.SCALE) + title_layout.set_alignment(Pango.Alignment.CENTER) + + # calculate width and height needed + width = label_width + 2 * BLOCK_LABEL_PADDING + height = label_height + 2 * BLOCK_LABEL_PADDING + + self._update_colors() + self.create_port_labels() + + def get_min_height_for_ports(ports): + min_height = 2 * PORT_BORDER_SEPARATION + len(ports) * PORT_SEPARATION + if ports: + min_height -= ports[-1].height return min_height - H = max( - [ # labels - self.label_height + 2 * Constants.BLOCK_LABEL_PADDING - ] + - [ # ports - get_min_height_for_ports() for ports in (self.get_sources_gui(), self.get_sinks_gui()) - ] + - [ # bus ports only - 2 * Constants.PORT_BORDER_SEPARATION + - sum([port.H + Constants.PORT_SPACING for port in ports if port.get_type() == 'bus']) - Constants.PORT_SPACING - for ports in (self.get_sources_gui(), self.get_sinks_gui()) - ] - ) - self.W, self.H = Utils.align_to_grid((W, H)) - self.has_busses = [ - any(port.get_type() == 'bus' for port in ports) - for ports in (self.get_sources_gui(), self.get_sinks_gui()) + + height = max(height, + get_min_height_for_ports(self.active_sinks), + get_min_height_for_ports(self.active_sources)) + + def get_min_height_for_bus_ports(ports): + return 2 * PORT_BORDER_SEPARATION + sum( + port.height + PORT_SPACING for port in ports if port.get_type() == 'bus' + ) - PORT_SPACING + + if self.current_bus_structure['sink']: + height = max(height, get_min_height_for_bus_ports(self.active_sinks)) + if self.current_bus_structure['source']: + height = max(height, get_min_height_for_bus_ports(self.active_sources)) + + self.width, self.height = width, height = Utils.align_to_grid((width, height)) + + self._surface_layout_offsets = [ + (width - label_width) / 2.0, + (height - label_height) / 2.0 ] - self.create_comment_label() - - def create_comment_label(self): - comment = self.get_comment() # Returns None if there are no comments - complexity = None - - # Show the flowgraph complexity on the top block if enabled - if Actions.TOGGLE_SHOW_FLOWGRAPH_COMPLEXITY.get_active() and self.get_key() == "options": - complexity = calculate_flowgraph_complexity(self.get_parent()) - complexity = "Complexity: {}bal".format(num_to_str(complexity)) - - layout = gtk.DrawingArea().create_pango_layout('') - layout.set_markup(Utils.parse_template(COMMENT_COMPLEXITY_MARKUP_TMPL, - block=self, - comment=comment, - complexity=complexity, - font=Constants.BLOCK_FONT)) - - # Setup the pixel map. Make sure that layout not empty - width, height = layout.get_pixel_size() - if width and height: - padding = Constants.BLOCK_LABEL_PADDING - pixmap = self.get_parent().new_pixmap(width + 2 * padding, - height + 2 * padding) - gc = pixmap.new_gc() - gc.set_foreground(Colors.COMMENT_BACKGROUND_COLOR) - pixmap.draw_rectangle( - gc, True, 0, 0, width + 2 * padding, height + 2 * padding) - pixmap.draw_layout(gc, padding, padding, layout) - self._comment_pixmap = pixmap + + self.create_comment_layout() + + def create_port_labels(self): + for ports in (self.active_sinks, self.active_sources): + max_width = 0 + for port in ports: + port.create_labels() + max_width = max(max_width, port.width_with_label) + for port in ports: + port.width = max_width + + def create_comment_layout(self): + markups = [] + + # Show the flow graph complexity on the top block if enabled + if Actions.TOGGLE_SHOW_FLOWGRAPH_COMPLEXITY.get_active() and self.key == "options": + complexity = utils.calculate_flowgraph_complexity(self.parent) + markups.append( + '<span foreground="#444" size="medium" font_desc="{font}">' + '<b>Complexity: {num}bal</b></span>'.format(num=Utils.num_to_str(complexity), font=BLOCK_FONT) + ) + comment = self.comment # Returns None if there are no comments + if comment: + if markups: + markups.append('<span></span>') + + markups.append('<span foreground="{foreground}" font_desc="{font}">{comment}</span>'.format( + foreground='#444' if self.enabled else '#888', font=BLOCK_FONT, comment=Utils.encode(comment) + )) + if markups: + layout = self._comment_layout = Gtk.DrawingArea().create_pango_layout('') + layout.set_markup(''.join(markups)) else: - self._comment_pixmap = None + self._comment_layout = None - def draw(self, gc, window): + def draw(self, cr): """ Draw the signal block with label and inputs/outputs. - - Args: - gc: the graphics context - window: the gtk window to draw on """ - # draw ports - for port in self.get_ports_gui(): - port.draw(gc, window) - # draw main block - x, y = self.get_coordinate() - Element.draw( - self, gc, window, bg_color=self._bg_color, - border_color=self.is_highlighted() and Colors.HIGHLIGHT_COLOR or - self.is_dummy_block and Colors.MISSING_BLOCK_BORDER_COLOR or Colors.BORDER_COLOR, - ) - #draw label image - if self.is_horizontal(): - window.draw_drawable(gc, self.horizontal_label, 0, 0, x+Constants.BLOCK_LABEL_PADDING, y+(self.H-self.label_height)/2, -1, -1) - elif self.is_vertical(): - window.draw_drawable(gc, self.vertical_label, 0, 0, x+(self.H-self.label_height)/2, y+Constants.BLOCK_LABEL_PADDING, -1, -1) + border_color = Colors.HIGHLIGHT_COLOR if self.highlighted else self._border_color + cr.translate(*self.coordinate) + + for port in self.active_ports(): # ports first + cr.save() + port.draw(cr) + cr.restore() + + cr.rectangle(*self._area) + cr.set_source_rgba(*self._bg_color) + cr.fill_preserve() + cr.set_source_rgba(*border_color) + cr.stroke() + + # title and params label + if self.is_vertical(): + cr.rotate(-math.pi / 2) + cr.translate(-self.width, 0) + cr.translate(*self._surface_layout_offsets) + + cr.set_source_rgba(*self._font_color) + for layout in self._surface_layouts: + PangoCairo.update_layout(cr, layout) + PangoCairo.show_layout(cr, layout) + _, h = layout.get_pixel_size() + cr.translate(0, h + LABEL_SEPARATION) def what_is_selected(self, coor, coor_m=None): """ @@ -333,19 +286,103 @@ class Block(Element, _Block): Returns: this block, a port, or None """ - for port in self.get_ports_gui(): - port_selected = port.what_is_selected(coor, coor_m) - if port_selected: return port_selected + for port in self.active_ports(): + port_selected = port.what_is_selected( + coor=[a - b for a, b in zip(coor, self.coordinate)], + coor_m=[a - b for a, b in zip(coor, self.coordinate)] if coor_m is not None else None + ) + if port_selected: + return port_selected return Element.what_is_selected(self, coor, coor_m) - def draw_comment(self, gc, window): - if not self._comment_pixmap: + def draw_comment(self, cr): + if not self._comment_layout: return - x, y = self.get_coordinate() + x, y = self.coordinate if self.is_horizontal(): - y += self.H + Constants.BLOCK_LABEL_PADDING + y += self.height + BLOCK_LABEL_PADDING else: - x += self.H + Constants.BLOCK_LABEL_PADDING + x += self.height + BLOCK_LABEL_PADDING + + cr.save() + cr.translate(x, y) + PangoCairo.update_layout(cr, self._comment_layout) + PangoCairo.show_layout(cr, self._comment_layout) + cr.restore() + + @property + def extend(self): + extend = Element.extend.fget(self) + x, y = self.coordinate + for port in self.active_ports(): + extend = (min_or_max(xy, offset + p_xy) for offset, min_or_max, xy, p_xy in zip( + (x, y, x, y), (min, min, max, max), extend, port.extend + )) + return tuple(extend) + + ############################################## + # Controller Modify + ############################################## + def type_controller_modify(self, direction): + """ + Change the type controller. + + Args: + direction: +1 or -1 + + Returns: + true for change + """ + type_templates = ' '.join(p._type for p in self.get_children()) + type_param = None + for key, param in six.iteritems(self.params): + if not param.is_enum(): + continue + # Priority to the type controller + if param.key in type_templates: + type_param = param + break + # Use param if type param is unset + if not type_param: + type_param = param + if not type_param: + return False + + # Try to increment the enum by direction + try: + keys = list(type_param.options) + old_index = keys.index(type_param.get_value()) + new_index = (old_index + direction + len(keys)) % len(keys) + type_param.set_value(keys[new_index]) + return True + except: + return False + + def port_controller_modify(self, direction): + """ + Change the port controller. + + Args: + direction: +1 or -1 + + Returns: + true for change + """ + changed = False + # Concat the nports string from the private nports settings of all ports + nports_str = ' '.join(port._nports for port in self.get_ports()) + # Modify all params whose keys appear in the nports string + for key, param in six.iteritems(self.params): + if param.is_enum() or param.key not in nports_str: + continue + # Try to increment the port controller by direction + try: + value = param.get_evaluated() + direction + if value > 0: + param.set_value(value) + changed = True + except: + pass + return changed - window.draw_drawable(gc, self._comment_pixmap, 0, 0, x, y, -1, -1) diff --git a/grc/gui/BlockTreeWindow.py b/grc/gui/BlockTreeWindow.py index 900cbd3151..8504200459 100644 --- a/grc/gui/BlockTreeWindow.py +++ b/grc/gui/BlockTreeWindow.py @@ -1,5 +1,5 @@ """ -Copyright 2007, 2008, 2009 Free Software Foundation, Inc. +Copyright 2007, 2008, 2009, 2016 Free Software Foundation, Inc. This file is part of GNU Radio GNU Radio Companion is free software; you can redistribute it and/or @@ -17,71 +17,56 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk -import gobject - -from . import Actions, Utils -from . import Constants - - -NAME_INDEX = 0 -KEY_INDEX = 1 -DOC_INDEX = 2 - -DOC_MARKUP_TMPL = """\ -#set $docs = [] -#if $doc.get('') - #set $docs += $doc.pop('').splitlines() + [''] -#end if -#for b, d in $doc.iteritems() - #set $docs += ['--- {0} ---'.format(b)] + d.splitlines() + [''] -#end for -#set $len_out = 0 -#for $n, $line in $enumerate($docs[:-1]) -#if $n - -#end if -$encode($line)#slurp -#set $len_out += $len($line) -#if $n > 10 or $len_out > 500 - -...#slurp -#break -#end if -#end for -#if $len_out == 0 -undocumented#slurp -#end if""" - -CAT_MARKUP_TMPL = """ -#set $name = $cat[-1] -#if len($cat) > 1 -Category: $cat[-1] -## -#elif $name == 'Core' -Module: Core - -This subtree is meant for blocks included with GNU Radio (in-tree). -## -#elif $name == $default_module -This subtree holds all blocks (from OOT modules) that specify no module name. \ -The module name is the root category enclosed in square brackets. - -Please consider contacting OOT module maintainer for any block in here \ -and kindly ask to update their GRC Block Descriptions or Block Tree to include a module name. -#else -Module: $name -## -#end if -""".strip() - - -class BlockTreeWindow(gtk.VBox): +from __future__ import absolute_import +import six + +from gi.repository import Gtk, Gdk, GObject + +from . import Actions, Utils, Constants + + +NAME_INDEX, KEY_INDEX, DOC_INDEX = range(3) + + +def _format_doc(doc): + docs = [] + if doc.get(''): + docs += doc.pop('').splitlines() + [''] + for block_name, docstring in six.iteritems(doc): + docs.append('--- {0} ---'.format(block_name)) + docs += docstring.splitlines() + docs.append('') + out = '' + for n, line in enumerate(docs[:-1]): + if n: + out += '\n' + out += Utils.encode(line) + if n > 10 or len(out) > 500: + out += '\n...' + break + return out or 'undocumented' + + +def _format_cat_tooltip(category): + tooltip = '{}: {}'.format('Category' if len(category) > 1 else 'Module', category[-1]) + + if category == ('Core',): + tooltip += '\n\nThis subtree is meant for blocks included with GNU Radio (in-tree).' + + elif category == (Constants.DEFAULT_BLOCK_MODULE_NAME,): + tooltip += '\n\n' + Constants.DEFAULT_BLOCK_MODULE_TOOLTIP + + return tooltip + + +class BlockTreeWindow(Gtk.VBox): """The block selection panel.""" - def __init__(self, platform, get_flow_graph): + __gsignals__ = { + 'create_new_block': (GObject.SignalFlags.RUN_FIRST, None, (str,)) + } + + def __init__(self, platform): """ BlockTreeWindow constructor. Create a tree view of the possible blocks in the platform. @@ -90,58 +75,52 @@ class BlockTreeWindow(gtk.VBox): Args: platform: the particular platform will all block prototypes - get_flow_graph: get the selected flow graph """ - gtk.VBox.__init__(self) + Gtk.VBox.__init__(self) self.platform = platform - self.get_flow_graph = get_flow_graph # search entry - self.search_entry = gtk.Entry() + self.search_entry = Gtk.Entry() try: - self.search_entry.set_icon_from_stock(gtk.ENTRY_ICON_PRIMARY, gtk.STOCK_FIND) - self.search_entry.set_icon_activatable(gtk.ENTRY_ICON_PRIMARY, False) - self.search_entry.set_icon_from_stock(gtk.ENTRY_ICON_SECONDARY, gtk.STOCK_CLOSE) + self.search_entry.set_icon_from_icon_name(Gtk.EntryIconPosition.PRIMARY, 'edit-find') + self.search_entry.set_icon_activatable(Gtk.EntryIconPosition.PRIMARY, False) + self.search_entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, 'window-close') self.search_entry.connect('icon-release', self._handle_icon_event) except AttributeError: pass # no icon for old pygtk self.search_entry.connect('changed', self._update_search_tree) self.search_entry.connect('key-press-event', self._handle_search_key_press) - self.pack_start(self.search_entry, False) + self.pack_start(self.search_entry, False, False, 0) # make the tree model for holding blocks and a temporary one for search results - self.treestore = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING) - self.treestore_search = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING) + self.treestore = Gtk.TreeStore(GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING) + self.treestore_search = Gtk.TreeStore(GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING) - self.treeview = gtk.TreeView(self.treestore) + self.treeview = Gtk.TreeView(model=self.treestore) self.treeview.set_enable_search(False) # disable pop up search box self.treeview.set_search_column(-1) # really disable search self.treeview.set_headers_visible(False) - self.treeview.add_events(gtk.gdk.BUTTON_PRESS_MASK) + self.treeview.add_events(Gdk.EventMask.BUTTON_PRESS_MASK) self.treeview.connect('button-press-event', self._handle_mouse_button_press) self.treeview.connect('key-press-event', self._handle_search_key_press) - self.treeview.get_selection().set_mode('single') - renderer = gtk.CellRendererText() - column = gtk.TreeViewColumn('Blocks', renderer, text=NAME_INDEX) + self.treeview.get_selection().set_mode(Gtk.SelectionMode.SINGLE) + renderer = Gtk.CellRendererText() + column = Gtk.TreeViewColumn('Blocks', renderer, text=NAME_INDEX) self.treeview.append_column(column) - # try to enable the tooltips (available in pygtk 2.12 and above) - try: - self.treeview.set_tooltip_column(DOC_INDEX) - except: - pass + self.treeview.set_tooltip_column(DOC_INDEX) # setup sort order column.set_sort_column_id(0) - self.treestore.set_sort_column_id(0, gtk.SORT_ASCENDING) + self.treestore.set_sort_column_id(0, Gtk.SortType.ASCENDING) # setup drag and drop - self.treeview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK, Constants.DND_TARGETS, gtk.gdk.ACTION_COPY) + self.treeview.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, Constants.DND_TARGETS, Gdk.DragAction.COPY) self.treeview.connect('drag-data-get', self._handle_drag_get_data) # make the scrolled window to hold the tree view - scrolled_window = gtk.ScrolledWindow() - scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - scrolled_window.add_with_viewport(self.treeview) + scrolled_window = Gtk.ScrolledWindow() + scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + scrolled_window.add(self.treeview) scrolled_window.set_size_request(Constants.DEFAULT_BLOCKS_WINDOW_WIDTH, -1) - self.pack_start(scrolled_window) + self.pack_start(scrolled_window, True, True, 0) # map categories to iters, automatic mapping for root self._categories = {tuple(): None} self._categories_search = {tuple(): None} @@ -154,7 +133,7 @@ class BlockTreeWindow(gtk.VBox): def repopulate(self): self.clear() - for block in self.platform.blocks.itervalues(): + for block in six.itervalues(self.platform.blocks): if block.category: self.add_block(block) self.expand_module_in_tree() @@ -188,15 +167,13 @@ class BlockTreeWindow(gtk.VBox): iter_ = treestore.insert_before(categories[parent_category[:-1]], None) treestore.set_value(iter_, NAME_INDEX, parent_cat_name) treestore.set_value(iter_, KEY_INDEX, '') - treestore.set_value(iter_, DOC_INDEX, Utils.parse_template( - CAT_MARKUP_TMPL, cat=parent_category, default_module=Constants.DEFAULT_BLOCK_MODULE_NAME)) + treestore.set_value(iter_, DOC_INDEX, _format_cat_tooltip(parent_cat_name)) categories[parent_category] = iter_ - # add block iter_ = treestore.insert_before(categories[category], None) - treestore.set_value(iter_, NAME_INDEX, block.get_name()) - treestore.set_value(iter_, KEY_INDEX, block.get_key()) - treestore.set_value(iter_, DOC_INDEX, Utils.parse_template(DOC_MARKUP_TMPL, doc=block.get_doc())) + treestore.set_value(iter_, KEY_INDEX, block.key) + treestore.set_value(iter_, NAME_INDEX, block.name) + treestore.set_value(iter_, DOC_INDEX, _format_doc(block.documentation)) def update_docs(self): """Update the documentation column of every block""" @@ -206,8 +183,7 @@ class BlockTreeWindow(gtk.VBox): if not key: return # category node, no doc string block = self.platform.blocks[key] - doc = Utils.parse_template(DOC_MARKUP_TMPL, doc=block.get_doc()) - model.set_value(iter_, DOC_INDEX, doc) + model.set_value(iter_, DOC_INDEX, _format_doc(block.documentation)) self.treestore.foreach(update_doc) self.treestore_search.foreach(update_doc) @@ -226,16 +202,6 @@ class BlockTreeWindow(gtk.VBox): treestore, iter = selection.get_selected() return iter and treestore.get_value(iter, KEY_INDEX) or '' - def _add_selected_block(self): - """ - Add the selected block with the given key to the flow graph. - """ - key = self._get_selected_block_key() - if key: - self.get_flow_graph().add_new_block(key) - return True - return False - def _expand_category(self): treestore, iter = self.treeview.get_selection().get_selected() if iter and treestore.iter_has_child(iter): @@ -246,9 +212,9 @@ class BlockTreeWindow(gtk.VBox): ## Event Handlers ############################################################ def _handle_icon_event(self, widget, icon, event): - if icon == gtk.ENTRY_ICON_PRIMARY: + if icon == Gtk.EntryIconPosition.PRIMARY: pass - elif icon == gtk.ENTRY_ICON_SECONDARY: + elif icon == Gtk.EntryIconPosition.SECONDARY: widget.set_text('') self.search_entry.hide() @@ -258,8 +224,8 @@ class BlockTreeWindow(gtk.VBox): self.treeview.set_model(self.treestore) self.expand_module_in_tree() else: - matching_blocks = filter(lambda b: key in b.get_key().lower() or key in b.get_name().lower(), - self.platform.blocks.values()) + matching_blocks = [b for b in list(self.platform.blocks.values()) + if key in b.key.lower() or key in b.name.lower()] self.treestore_search.clear() self._categories_search = {tuple(): None} @@ -270,7 +236,7 @@ class BlockTreeWindow(gtk.VBox): def _handle_search_key_press(self, widget, event): """Handle Return and Escape key events in search entry and treeview""" - if event.keyval == gtk.keysyms.Return: + if event.keyval == Gdk.KEY_Return: # add block on enter if widget == self.search_entry: @@ -280,24 +246,28 @@ class BlockTreeWindow(gtk.VBox): selected = self.treestore_search.iter_children(selected) if selected is not None: key = self.treestore_search.get_value(selected, KEY_INDEX) - if key: self.get_flow_graph().add_new_block(key) + if key: self.emit('create_new_block', key) elif widget == self.treeview: - self._add_selected_block() or self._expand_category() + key = self._get_selected_block_key() + if key: + self.emit('create_new_block', key) + else: + self._expand_category() else: return False # propagate event - elif event.keyval == gtk.keysyms.Escape: + elif event.keyval == Gdk.KEY_Escape: # reset the search self.search_entry.set_text('') self.search_entry.hide() - elif (event.state & gtk.gdk.CONTROL_MASK and event.keyval == gtk.keysyms.f) \ - or event.keyval == gtk.keysyms.slash: + elif (event.get_state() & Gdk.ModifierType.CONTROL_MASK and event.keyval == Gdk.KEY_f) \ + or event.keyval == Gdk.KEY_slash: # propagation doesn't work although treeview search is disabled =( # manually trigger action... Actions.FIND_BLOCKS.activate() - elif event.state & gtk.gdk.CONTROL_MASK and event.keyval == gtk.keysyms.b: + elif event.get_state() & Gdk.ModifierType.CONTROL_MASK and event.keyval == Gdk.KEY_b: # ugly... Actions.TOGGLE_BLOCKS_WINDOW.activate() @@ -313,12 +283,15 @@ class BlockTreeWindow(gtk.VBox): Only call set when the key is valid to ignore DND from categories. """ key = self._get_selected_block_key() - if key: selection_data.set(selection_data.target, 8, key) + if key: + selection_data.set_text(key, len(key)) def _handle_mouse_button_press(self, widget, event): """ Handle the mouse button press. If a left double click is detected, call add selected block. """ - if event.button == 1 and event.type == gtk.gdk._2BUTTON_PRESS: - self._add_selected_block() + if event.button == 1 and event.type == Gdk.EventType._2BUTTON_PRESS: + key = self._get_selected_block_key() + if key: + self.emit('create_new_block', key) diff --git a/grc/gui/Colors.py b/grc/gui/Colors.py index d322afa410..01c461ada5 100644 --- a/grc/gui/Colors.py +++ b/grc/gui/Colors.py @@ -16,35 +16,106 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -try: - import pygtk - pygtk.require('2.0') - import gtk - - _COLORMAP = gtk.gdk.colormap_get_system() #create all of the colors - def get_color(color_code): return _COLORMAP.alloc_color(color_code, True, True) - - HIGHLIGHT_COLOR = get_color('#00FFFF') - BORDER_COLOR = get_color('#444444') - # missing blocks stuff - MISSING_BLOCK_BACKGROUND_COLOR = get_color('#FFF2F2') - MISSING_BLOCK_BORDER_COLOR = get_color('red') - #param entry boxes - PARAM_ENTRY_TEXT_COLOR = get_color('black') - ENTRYENUM_CUSTOM_COLOR = get_color('#EEEEEE') - #flow graph color constants - FLOWGRAPH_BACKGROUND_COLOR = get_color('#FFFFFF') - COMMENT_BACKGROUND_COLOR = get_color('#F3F3F3') - FLOWGRAPH_EDGE_COLOR = COMMENT_BACKGROUND_COLOR - #block color constants - BLOCK_ENABLED_COLOR = get_color('#F1ECFF') - BLOCK_DISABLED_COLOR = get_color('#CCCCCC') - BLOCK_BYPASSED_COLOR = get_color('#F4FF81') - #connection color constants - CONNECTION_ENABLED_COLOR = get_color('black') - CONNECTION_DISABLED_COLOR = get_color('#BBBBBB') - CONNECTION_ERROR_COLOR = get_color('red') -except: - print 'Unable to import Colors' - -DEFAULT_DOMAIN_COLOR_CODE = '#777777' + +from __future__ import absolute_import + +from gi.repository import Gdk, cairo +# import pycairo + +from . import Constants + + +def _color_parse(color_code): + color = Gdk.RGBA() + color.parse(color_code) + return color + + +def get_color(color_code): + color = _color_parse(color_code) + return color.red, color.green, color.blue, color.alpha + # chars_per_color = 2 if len(color_code) > 4 else 1 + # offsets = range(1, 3 * chars_per_color + 1, chars_per_color) + # return tuple(int(color_code[o:o + 2], 16) / 255.0 for o in offsets) + +################################################################################# +# fg colors +################################################################################# + +HIGHLIGHT_COLOR = get_color('#00FFFF') +BORDER_COLOR = get_color('#616161') +BORDER_COLOR_DISABLED = get_color('#888888') +FONT_COLOR = get_color('#000000') + +# Missing blocks stuff +MISSING_BLOCK_BACKGROUND_COLOR = get_color('#FFF2F2') +MISSING_BLOCK_BORDER_COLOR = get_color('#FF0000') + +# Flow graph color constants +FLOWGRAPH_BACKGROUND_COLOR = get_color('#FFFFFF') +COMMENT_BACKGROUND_COLOR = get_color('#F3F3F3') +FLOWGRAPH_EDGE_COLOR = COMMENT_BACKGROUND_COLOR + +# Block color constants +BLOCK_ENABLED_COLOR = get_color('#F1ECFF') +BLOCK_DISABLED_COLOR = get_color('#CCCCCC') +BLOCK_BYPASSED_COLOR = get_color('#F4FF81') + +# Connection color constants +CONNECTION_ENABLED_COLOR = get_color('#000000') +CONNECTION_DISABLED_COLOR = get_color('#BBBBBB') +CONNECTION_ERROR_COLOR = get_color('#FF0000') + +DEFAULT_DOMAIN_COLOR = get_color('#777777') + + +################################################################################# +# port colors +################################################################################# + +PORT_TYPE_TO_COLOR = {key: get_color(color) for name, key, sizeof, color in Constants.CORE_TYPES} +PORT_TYPE_TO_COLOR.update((key, get_color(color)) for key, (_, color) in Constants.ALIAS_TYPES.items()) + + +################################################################################# +# param box colors +################################################################################# +COMPLEX_COLOR_SPEC = _color_parse('#3399FF') +FLOAT_COLOR_SPEC = _color_parse('#FF8C69') +INT_COLOR_SPEC = _color_parse('#00FF99') +SHORT_COLOR_SPEC = _color_parse('#FFFF66') +BYTE_COLOR_SPEC = _color_parse('#FF66FF') + +ID_COLOR_SPEC = _color_parse('#DDDDDD') +WILDCARD_COLOR_SPEC = _color_parse('#FFFFFF') + +COMPLEX_VECTOR_COLOR_SPEC = _color_parse('#3399AA') +FLOAT_VECTOR_COLOR_SPEC = _color_parse('#CC8C69') +INT_VECTOR_COLOR_SPEC = _color_parse('#00CC99') +SHORT_VECTOR_COLOR_SPEC = _color_parse('#CCCC33') +BYTE_VECTOR_COLOR_SPEC = _color_parse('#CC66CC') + +PARAM_ENTRY_COLORS = { + # Number types + 'complex': COMPLEX_COLOR_SPEC, + 'real': FLOAT_COLOR_SPEC, + 'float': FLOAT_COLOR_SPEC, + 'int': INT_COLOR_SPEC, + + # Vector types + 'complex_vector': COMPLEX_VECTOR_COLOR_SPEC, + 'real_vector': FLOAT_VECTOR_COLOR_SPEC, + 'float_vector': FLOAT_VECTOR_COLOR_SPEC, + 'int_vector': INT_VECTOR_COLOR_SPEC, + + # Special + 'bool': INT_COLOR_SPEC, + 'hex': INT_COLOR_SPEC, + 'string': BYTE_VECTOR_COLOR_SPEC, + 'id': ID_COLOR_SPEC, + 'stream_id': ID_COLOR_SPEC, + 'raw': WILDCARD_COLOR_SPEC, +} + +PARAM_ENTRY_DEFAULT_COLOR = _color_parse('#FFFFFF') +PARAM_ENTRY_ENUM_CUSTOM_COLOR = _color_parse('#EEEEEE') diff --git a/grc/gui/Config.py b/grc/gui/Config.py index 9b0c5d4afe..b6556ad724 100644 --- a/grc/gui/Config.py +++ b/grc/gui/Config.py @@ -17,8 +17,11 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import, print_function + import sys import os + from ..core.Config import Config as _Config from . import Constants @@ -57,7 +60,7 @@ class Config(_Config): raise Exception() return value except: - print >> sys.stderr, "Error: invalid 'canvas_default_size' setting." + print("Error: invalid 'canvas_default_size' setting.", file=sys.stderr) return Constants.DEFAULT_CANVAS_SIZE_DEFAULT @property @@ -69,6 +72,6 @@ class Config(_Config): raise Exception() except: font_size = Constants.DEFAULT_FONT_SIZE - print >> sys.stderr, "Error: invalid 'canvas_font_size' setting." + print("Error: invalid 'canvas_font_size' setting.", file=sys.stderr) return font_size diff --git a/grc/gui/Connection.py b/grc/gui/Connection.py index 50361c19d0..9862328d6a 100644 --- a/grc/gui/Connection.py +++ b/grc/gui/Connection.py @@ -17,18 +17,17 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import gtk +from __future__ import absolute_import -import Colors -import Utils -from Constants import CONNECTOR_ARROW_BASE, CONNECTOR_ARROW_HEIGHT -from Element import Element +from . import Colors, Utils +from .Constants import CONNECTOR_ARROW_BASE, CONNECTOR_ARROW_HEIGHT, GR_MESSAGE_DOMAIN +from .Element import Element -from ..core.Constants import GR_MESSAGE_DOMAIN -from ..core.Connection import Connection as _Connection +from ..core.Element import nop_write +from ..core.Connection import Connection as CoreConnection -class Connection(Element, _Connection): +class Connection(CoreConnection, Element): """ A graphical connection for ports. The connection has 2 parts, the arrow and the wire. @@ -38,23 +37,25 @@ class Connection(Element, _Connection): The arrow coloring exposes the enabled and valid states. """ - def __init__(self, **kwargs): + def __init__(self, *args, **kwargs): + super(self.__class__, self).__init__(*args, **kwargs) Element.__init__(self) - _Connection.__init__(self, **kwargs) - # can't use Colors.CONNECTION_ENABLED_COLOR here, might not be defined (grcc) - self._bg_color = self._arrow_color = self._color = None - def get_coordinate(self): - """ - Get the 0,0 coordinate. - Coordinates are irrelevant in connection. + self._line = [] + self._line_width_factor = 1.0 + self._color = self._color2 = self._arrow_color = None - Returns: - 0, 0 - """ - return 0, 0 + self._sink_rot = self._source_rot = None + self._sink_coor = self._source_coor = None - def get_rotation(self): + @nop_write + @property + def coordinate(self): + return self.source_port.get_connector_coordinate() + + @nop_write + @property + def rotation(self): """ Get the 0 degree rotation. Rotations are irrelevant in connection. @@ -65,117 +66,142 @@ class Connection(Element, _Connection): return 0 def create_shapes(self): - """Precalculate relative coordinates.""" - Element.create_shapes(self) + """Pre-calculate relative coordinates.""" self._sink_rot = None self._source_rot = None self._sink_coor = None self._source_coor = None #get the source coordinate try: - connector_length = self.get_source().get_connector_length() + connector_length = self.source_port.connector_length except: - return - self.x1, self.y1 = Utils.get_rotated_coordinate((connector_length, 0), self.get_source().get_rotation()) + return # todo: why? + self.x1, self.y1 = Utils.get_rotated_coordinate((connector_length, 0), self.source_port.rotation) #get the sink coordinate - connector_length = self.get_sink().get_connector_length() + CONNECTOR_ARROW_HEIGHT - self.x2, self.y2 = Utils.get_rotated_coordinate((-connector_length, 0), self.get_sink().get_rotation()) + connector_length = self.sink_port.connector_length + CONNECTOR_ARROW_HEIGHT + self.x2, self.y2 = Utils.get_rotated_coordinate((-connector_length, 0), self.sink_port.rotation) #build the arrow - self.arrow = [(0, 0), - Utils.get_rotated_coordinate((-CONNECTOR_ARROW_HEIGHT, -CONNECTOR_ARROW_BASE/2), self.get_sink().get_rotation()), - Utils.get_rotated_coordinate((-CONNECTOR_ARROW_HEIGHT, CONNECTOR_ARROW_BASE/2), self.get_sink().get_rotation()), - ] - source_domain = self.get_source().get_domain() - sink_domain = self.get_sink().get_domain() - self.line_attributes[0] = 2 if source_domain != sink_domain else 0 - self.line_attributes[1] = gtk.gdk.LINE_DOUBLE_DASH \ - if not source_domain == sink_domain == GR_MESSAGE_DOMAIN \ - else gtk.gdk.LINE_ON_OFF_DASH - get_domain_color = lambda d: Colors.get_color(( - self.get_parent().get_parent().domains.get(d, {}) - ).get('color') or Colors.DEFAULT_DOMAIN_COLOR_CODE) - self._color = get_domain_color(source_domain) - self._bg_color = get_domain_color(sink_domain) - self._arrow_color = self._bg_color if self.is_valid() else Colors.CONNECTION_ERROR_COLOR + self._arrow_base = [ + (0, 0), + Utils.get_rotated_coordinate((-CONNECTOR_ARROW_HEIGHT, -CONNECTOR_ARROW_BASE/2), self.sink_port.rotation), + Utils.get_rotated_coordinate((-CONNECTOR_ARROW_HEIGHT, CONNECTOR_ARROW_BASE/2), self.sink_port.rotation), + ] if self.sink_block.state != 'bypassed' else [] + source_domain = self.source_port.domain + sink_domain = self.sink_port.domain + + def get_domain_color(domain_name): + domain = self.parent_platform.domains.get(domain_name, {}) + color_spec = domain.get('color') + return Colors.get_color(color_spec) if color_spec else Colors.DEFAULT_DOMAIN_COLOR + + if source_domain == GR_MESSAGE_DOMAIN: + self._line_width_factor = 1.0 + self._color = None + self._color2 = Colors.CONNECTION_ENABLED_COLOR + else: + if source_domain != sink_domain: + self._line_width_factor = 2.0 + self._color = get_domain_color(source_domain) + self._color2 = get_domain_color(sink_domain) + self._arrow_color = self._color2 if self.is_valid() else Colors.CONNECTION_ERROR_COLOR self._update_after_move() def _update_after_move(self): """Calculate coordinates.""" - self.clear() #FIXME do i want this here? - #source connector - source = self.get_source() - X, Y = source.get_connector_coordinate() - x1, y1 = self.x1 + X, self.y1 + Y - self.add_line((x1, y1), (X, Y)) - #sink connector - sink = self.get_sink() - X, Y = sink.get_connector_coordinate() - x2, y2 = self.x2 + X, self.y2 + Y - self.add_line((x2, y2), (X, Y)) - #adjust arrow - self._arrow = [(x+X, y+Y) for x,y in self.arrow] - #add the horizontal and vertical lines in this connection - if abs(source.get_connector_direction() - sink.get_connector_direction()) == 180: - #2 possible point sets to create a 3-line connector - mid_x, mid_y = (x1 + x2)/2.0, (y1 + y2)/2.0 - points = [((mid_x, y1), (mid_x, y2)), ((x1, mid_y), (x2, mid_y))] - #source connector -> points[0][0] should be in the direction of source (if possible) - if Utils.get_angle_from_coordinates((x1, y1), points[0][0]) != source.get_connector_direction(): points.reverse() - #points[0][0] -> sink connector should not be in the direction of sink - if Utils.get_angle_from_coordinates(points[0][0], (x2, y2)) == sink.get_connector_direction(): points.reverse() - #points[0][0] -> source connector should not be in the direction of source - if Utils.get_angle_from_coordinates(points[0][0], (x1, y1)) == source.get_connector_direction(): points.reverse() - #create 3-line connector - p1, p2 = map(int, points[0][0]), map(int, points[0][1]) - self.add_line((x1, y1), p1) - self.add_line(p1, p2) - self.add_line((x2, y2), p2) + source = self.source_port + sink = self.sink_port + source_dir = source.get_connector_direction() + sink_dir = sink.get_connector_direction() + + x_pos, y_pos = self.coordinate + x_start, y_start = source.get_connector_coordinate() + x_end, y_end = sink.get_connector_coordinate() + + p3 = x3, y3 = x_end - x_pos, y_end - y_pos + p2 = x2, y2 = self.x2 + x3, self.y2 + y3 + p1 = x1, y1 = self.x1, self.y1 + p0 = x_start - x_pos, y_start - y_pos + self._arrow = [(x + x3, y + y3) for x, y in self._arrow_base] + + if abs(source_dir - sink.get_connector_direction()) == 180: + # 2 possible point sets to create a 3-line connector + mid_x, mid_y = (x1 + x2) / 2.0, (y1 + y2) / 2.0 + points = ((mid_x, y1), (mid_x, y2)) + alt = ((x1, mid_y), (x2, mid_y)) + # source connector -> points[0][0] should be in the direction of source (if possible) + if Utils.get_angle_from_coordinates(p1, points[0]) != source_dir: + points, alt = alt, points + # points[0] -> sink connector should not be in the direction of sink + if Utils.get_angle_from_coordinates(points[0], p2) == sink_dir: + points, alt = alt, points + # points[0] -> source connector should not be in the direction of source + if Utils.get_angle_from_coordinates(points[0], p1) == source_dir: + points, alt = alt, points + # create 3-line connector + i1, i2 = points + self._line = [p0, p1, i1, i2, p2, p3] else: - #2 possible points to create a right-angled connector - points = [(x1, y2), (x2, y1)] - #source connector -> points[0] should be in the direction of source (if possible) - if Utils.get_angle_from_coordinates((x1, y1), points[0]) != source.get_connector_direction(): points.reverse() - #points[0] -> sink connector should not be in the direction of sink - if Utils.get_angle_from_coordinates(points[0], (x2, y2)) == sink.get_connector_direction(): points.reverse() - #points[0] -> source connector should not be in the direction of source - if Utils.get_angle_from_coordinates(points[0], (x1, y1)) == source.get_connector_direction(): points.reverse() - #create right-angled connector - self.add_line((x1, y1), points[0]) - self.add_line((x2, y2), points[0]) - - def draw(self, gc, window): + # 2 possible points to create a right-angled connector + point, alt = [(x1, y2), (x2, y1)] + # source connector -> point should be in the direction of source (if possible) + if Utils.get_angle_from_coordinates(p1, point) != source_dir: + point, alt = alt, point + # point -> sink connector should not be in the direction of sink + if Utils.get_angle_from_coordinates(point, p2) == sink_dir: + point, alt = alt, point + # point -> source connector should not be in the direction of source + if Utils.get_angle_from_coordinates(point, p1) == source_dir: + point, alt = alt, point + # create right-angled connector + self._line = [p0, p1, point, p2, p3] + self.bounds_from_line(self._line) + + def draw(self, cr): """ Draw the connection. - - Args: - gc: the graphics context - window: the gtk window to draw on """ - sink = self.get_sink() - source = self.get_source() - #check for changes - if self._sink_rot != sink.get_rotation() or self._source_rot != source.get_rotation(): self.create_shapes() - elif self._sink_coor != sink.get_coordinate() or self._source_coor != source.get_coordinate(): - try: - self._update_after_move() - except: - return - #cache values - self._sink_rot = sink.get_rotation() - self._source_rot = source.get_rotation() - self._sink_coor = sink.get_coordinate() - self._source_coor = source.get_coordinate() - #draw - mod_color = lambda color: ( - Colors.HIGHLIGHT_COLOR if self.is_highlighted() else - Colors.CONNECTION_DISABLED_COLOR if not self.get_enabled() else - color + sink = self.sink_port + source = self.source_port + # check for changes + if self._sink_rot != sink.rotation or self._source_rot != source.rotation: + self.create_shapes() + self._sink_rot = sink.rotation + self._source_rot = source.rotation + + elif self._sink_coor != sink.parent_block.coordinate or self._source_coor != source.parent_block.coordinate: + self._update_after_move() + self._sink_coor = sink.parent_block.coordinate + self._source_coor = source.parent_block.coordinate + # draw + color1, color2, arrow_color = ( + None if color is None else + Colors.HIGHLIGHT_COLOR if self.highlighted else + Colors.CONNECTION_DISABLED_COLOR if not self.enabled else color + for color in (self._color, self._color2, self._arrow_color) ) - Element.draw(self, gc, window, mod_color(self._color), mod_color(self._bg_color)) - # draw arrow on sink port - try: - gc.set_foreground(mod_color(self._arrow_color)) - gc.set_line_attributes(0, gtk.gdk.LINE_SOLID, gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER) - window.draw_polygon(gc, True, self._arrow) - except: - pass + + cr.translate(*self.coordinate) + cr.set_line_width(self._line_width_factor * cr.get_line_width()) + for point in self._line: + cr.line_to(*point) + + if color1: # not a message connection + cr.set_source_rgba(*color1) + cr.stroke_preserve() + + if color1 != color2: + cr.save() + cr.set_dash([5.0, 5.0], 5.0 if color1 else 0.0) + cr.set_source_rgba(*color2) + cr.stroke() + cr.restore() + else: + cr.new_path() + + if self._arrow: + cr.set_source_rgba(*arrow_color) + cr.move_to(*self._arrow[0]) + cr.line_to(*self._arrow[1]) + cr.line_to(*self._arrow[2]) + cr.close_path() + cr.fill() diff --git a/grc/gui/Constants.py b/grc/gui/Constants.py index 022564cd77..2f0f7794be 100644 --- a/grc/gui/Constants.py +++ b/grc/gui/Constants.py @@ -17,7 +17,9 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import gtk +from __future__ import absolute_import + +from gi.repository import Gtk from ..core.Constants import * @@ -25,10 +27,6 @@ from ..core.Constants import * # default path for the open/save dialogs DEFAULT_FILE_PATH = os.getcwd() -# file extensions -IMAGE_FILE_EXTENSION = '.png' -TEXT_FILE_EXTENSION = '.txt' - # name for new/unsaved flow graphs NEW_FLOGRAPH_TITLE = 'untitled' @@ -36,7 +34,7 @@ NEW_FLOGRAPH_TITLE = 'untitled' MIN_WINDOW_WIDTH = 600 MIN_WINDOW_HEIGHT = 400 # dialog constraints -MIN_DIALOG_WIDTH = 500 +MIN_DIALOG_WIDTH = 600 MIN_DIALOG_HEIGHT = 500 # default sizes DEFAULT_BLOCKS_WINDOW_WIDTH = 100 @@ -53,7 +51,7 @@ PARAM_FONT = "Sans 7.5" STATE_CACHE_SIZE = 42 # Shared targets for drag and drop of blocks -DND_TARGETS = [('STRING', gtk.TARGET_SAME_APP, 0)] +DND_TARGETS = [('STRING', Gtk.TargetFlags.SAME_APP, 0)] # label constraint dimensions LABEL_SEPARATION = 3 @@ -70,6 +68,7 @@ PORT_SEPARATION = 32 PORT_MIN_WIDTH = 20 PORT_LABEL_HIDDEN_WIDTH = 10 +PORT_EXTRA_BUS_HEIGHT = 40 # minimal length of connector CONNECTOR_EXTENSION_MINIMAL = 11 @@ -84,11 +83,8 @@ CONNECTOR_ARROW_HEIGHT = 17 # possible rotations in degrees POSSIBLE_ROTATIONS = (0, 90, 180, 270) -# How close can the mouse get to the window border before mouse events are ignored. -BORDER_PROXIMITY_SENSITIVITY = 50 - # How close the mouse can get to the edge of the visible window before scrolling is invoked. -SCROLL_PROXIMITY_SENSITIVITY = 30 +SCROLL_PROXIMITY_SENSITIVITY = 50 # When the window has to be scrolled, move it this distance in the required direction. SCROLL_DISTANCE = 15 @@ -96,6 +92,13 @@ SCROLL_DISTANCE = 15 # How close the mouse click can be to a line and register a connection select. LINE_SELECT_SENSITIVITY = 5 +DEFAULT_BLOCK_MODULE_TOOLTIP = """\ +This subtree holds all blocks (from OOT modules) that specify no module name. \ +The module name is the root category enclosed in square brackets. + +Please consider contacting OOT module maintainer for any block in here \ +and kindly ask to update their GRC Block Descriptions or Block Tree to include a module name.""" + def update_font_size(font_size): global PORT_SEPARATION, BLOCK_FONT, PORT_FONT, PARAM_FONT, FONT_SIZE diff --git a/grc/gui/Dialogs.py b/grc/gui/Dialogs.py index 1d114356c8..da4b11a3b6 100644 --- a/grc/gui/Dialogs.py +++ b/grc/gui/Dialogs.py @@ -1,33 +1,36 @@ -""" -Copyright 2008, 2009 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" - -import gtk +# Copyright 2008, 2009, 2016 Free Software Foundation, Inc. +# This file is part of GNU Radio +# +# GNU Radio Companion is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# GNU Radio Companion is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + +from __future__ import absolute_import import sys +import textwrap from distutils.spawn import find_executable -from . import Utils, Actions +from gi.repository import Gtk + +from . import Utils, Actions, Constants from ..core import Messages -class SimpleTextDisplay(gtk.TextView): - """A non editable gtk text view.""" +class SimpleTextDisplay(Gtk.TextView): + """ + A non user-editable gtk text view. + """ def __init__(self, text=''): """ @@ -36,16 +39,18 @@ class SimpleTextDisplay(gtk.TextView): Args: text: the text to display (string) """ - text_buffer = gtk.TextBuffer() - text_buffer.set_text(text) - self.set_text = text_buffer.set_text - gtk.TextView.__init__(self, text_buffer) + Gtk.TextView.__init__(self) + self.set_text = self.get_buffer().set_text + self.set_text(text) self.set_editable(False) self.set_cursor_visible(False) - self.set_wrap_mode(gtk.WRAP_WORD_CHAR) + self.set_wrap_mode(Gtk.WrapMode.WORD_CHAR) class TextDisplay(SimpleTextDisplay): + """ + A non user-editable scrollable text view with popup menu. + """ def __init__(self, text=''): """ @@ -59,220 +64,311 @@ class TextDisplay(SimpleTextDisplay): self.connect("populate-popup", self.populate_popup) def insert(self, line): - # make backspaces work + """ + Append text after handling backspaces and auto-scroll. + + Args: + line: the text to append (string) + """ line = self._consume_backspaces(line) - # add the remaining text to buffer self.get_buffer().insert(self.get_buffer().get_end_iter(), line) - # Automatically scroll on insert self.scroll_to_end() def _consume_backspaces(self, line): - """removes text from the buffer if line starts with \b*""" - if not line: return + """ + Removes text from the buffer if line starts with '\b' + + Args: + line: a string which may contain backspaces + + Returns: + The string that remains from 'line' with leading '\b's removed. + """ + if not line: + return + # for each \b delete one char from the buffer back_count = 0 start_iter = self.get_buffer().get_end_iter() while line[back_count] == '\b': # stop at the beginning of a line - if not start_iter.starts_line(): start_iter.backward_char() + if not start_iter.starts_line(): + start_iter.backward_char() back_count += 1 - # remove chars + # remove chars from buffer self.get_buffer().delete(start_iter, self.get_buffer().get_end_iter()) - # return remaining text return line[back_count:] def scroll_to_end(self): + """ Update view's scroll position. """ if self.scroll_lock: - buffer = self.get_buffer() - buffer.move_mark(buffer.get_insert(), buffer.get_end_iter()) - self.scroll_to_mark(buffer.get_insert(), 0.0) + buf = self.get_buffer() + buf.move_mark(buf.get_insert(), buf.get_end_iter()) + # TODO: Fix later + #self.scroll_to_mark(buffer.get_insert(), 0.0) def clear(self): - buffer = self.get_buffer() - buffer.delete(buffer.get_start_iter(), buffer.get_end_iter()) + """ Clear all text from buffer. """ + buf = self.get_buffer() + buf.delete(buf.get_start_iter(), buf.get_end_iter()) def save(self, file_path): - console_file = open(file_path, 'w') - buffer = self.get_buffer() - console_file.write(buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter(), True)) - console_file.close() + """ + Save context of buffer to the given file. + + Args: + file_path: location to save buffer contents + """ + with open(file_path, 'w') as logfile: + buf = self.get_buffer() + logfile.write(buf.get_text(buf.get_start_iter(), + buf.get_end_iter(), True)) - # Callback functions to handle the scrolling lock and clear context menus options # Action functions are set by the ActionHandler's init function def clear_cb(self, menu_item, web_view): + """ Callback function to clear the text buffer """ Actions.CLEAR_CONSOLE() def scroll_back_cb(self, menu_item, web_view): + """ Callback function to toggle scroll lock """ Actions.TOGGLE_SCROLL_LOCK() def save_cb(self, menu_item, web_view): + """ Callback function to save the buffer """ Actions.SAVE_CONSOLE() def populate_popup(self, view, menu): """Create a popup menu for the scroll lock and clear functions""" - menu.append(gtk.SeparatorMenuItem()) + menu.append(Gtk.SeparatorMenuItem()) - lock = gtk.CheckMenuItem("Scroll Lock") + lock = Gtk.CheckMenuItem("Scroll Lock") menu.append(lock) lock.set_active(self.scroll_lock) lock.connect('activate', self.scroll_back_cb, view) - save = gtk.ImageMenuItem(gtk.STOCK_SAVE) + save = Gtk.ImageMenuItem(Gtk.STOCK_SAVE) menu.append(save) save.connect('activate', self.save_cb, view) - clear = gtk.ImageMenuItem(gtk.STOCK_CLEAR) + clear = Gtk.ImageMenuItem(Gtk.STOCK_CLEAR) menu.append(clear) clear.connect('activate', self.clear_cb, view) menu.show_all() return False -def MessageDialogHelper(type, buttons, title=None, markup=None, default_response=None, extra_buttons=None): - """ - Create a modal message dialog and run it. +class MessageDialogWrapper(Gtk.MessageDialog): + """ Run a message dialog. """ - Args: - type: the type of message: gtk.MESSAGE_INFO, gtk.MESSAGE_WARNING, gtk.MESSAGE_QUESTION or gtk.MESSAGE_ERROR - buttons: the predefined set of buttons to use: - gtk.BUTTONS_NONE, gtk.BUTTONS_OK, gtk.BUTTONS_CLOSE, gtk.BUTTONS_CANCEL, gtk.BUTTONS_YES_NO, gtk.BUTTONS_OK_CANCEL + def __init__(self, parent, message_type, buttons, title=None, markup=None, + default_response=None, extra_buttons=None): + """ + Create a modal message dialog. - Args: - title: the title of the window (string) - markup: the message text with pango markup - default_response: if set, determines which button is highlighted by default - extra_buttons: a tuple containing pairs of values; each value is the button's text and the button's return value + Args: + message_type: the type of message may be one of: + Gtk.MessageType.INFO + Gtk.MessageType.WARNING + Gtk.MessageType.QUESTION or Gtk.MessageType.ERROR + buttons: the predefined set of buttons to use: + Gtk.ButtonsType.NONE + Gtk.ButtonsType.OK + Gtk.ButtonsType.CLOSE + Gtk.ButtonsType.CANCEL + Gtk.ButtonsType.YES_NO + Gtk.ButtonsType.OK_CANCEL + title: the title of the window (string) + markup: the message text with pango markup + default_response: if set, determines which button is highlighted by default + extra_buttons: a tuple containing pairs of values: + each value is the button's text and the button's return value - Returns: - the gtk response from run() - """ - message_dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, type, buttons) - if title: message_dialog.set_title(title) - if markup: message_dialog.set_markup(markup) - if extra_buttons: message_dialog.add_buttons(*extra_buttons) - if default_response: message_dialog.set_default_response(default_response) - response = message_dialog.run() - message_dialog.destroy() - return response - - -ERRORS_MARKUP_TMPL="""\ -#for $i, $err_msg in enumerate($errors) -<b>Error $i:</b> -$encode($err_msg.replace('\t', ' ')) - -#end for""" - - -def ErrorsDialog(flowgraph): MessageDialogHelper( - type=gtk.MESSAGE_ERROR, - buttons=gtk.BUTTONS_CLOSE, - title='Flow Graph Errors', - markup=Utils.parse_template(ERRORS_MARKUP_TMPL, errors=flowgraph.get_error_messages()), -) - - -class AboutDialog(gtk.AboutDialog): - """A cute little about dialog.""" - - def __init__(self, config): - """AboutDialog constructor.""" - gtk.AboutDialog.__init__(self) - self.set_name(config.name) - self.set_version(config.version) - self.set_license(config.license) - self.set_copyright(config.license.splitlines()[0]) - self.set_website(config.website) - self.run() - self.destroy() - - -def HelpDialog(): MessageDialogHelper( - type=gtk.MESSAGE_INFO, - buttons=gtk.BUTTONS_CLOSE, - title='Help', - markup="""\ -<b>Usage Tips</b> - -<u>Add block</u>: drag and drop or double click a block in the block selection window. -<u>Rotate block</u>: Select a block, press left/right on the keyboard. -<u>Change type</u>: Select a block, press up/down on the keyboard. -<u>Edit parameters</u>: double click on a block in the flow graph. -<u>Make connection</u>: click on the source port of one block, then click on the sink port of another block. -<u>Remove connection</u>: select the connection and press delete, or drag the connection. - -* See the menu for other keyboard shortcuts.""") - -COLORS_DIALOG_MARKUP_TMPL = """\ -<b>Color Mapping</b> - -#if $colors - #set $max_len = max([len(color[0]) for color in $colors]) + 10 - #for $title, $color_spec in $colors -<span background="$color_spec"><tt>$($encode($title).center($max_len))</tt></span> - #end for -#end if -""" - - -def TypesDialog(platform): - MessageDialogHelper( - type=gtk.MESSAGE_INFO, - buttons=gtk.BUTTONS_CLOSE, - title='Types', - markup=Utils.parse_template(COLORS_DIALOG_MARKUP_TMPL, - colors=platform.get_colors()) + """ + Gtk.MessageDialog.__init__( + self, transient_for=parent, modal=True, destroy_with_parent=True, + message_type=message_type, buttons=buttons + ) + if title: + self.set_title(title) + if markup: + self.set_markup(markup) + if extra_buttons: + self.add_buttons(*extra_buttons) + if default_response: + self.set_default_response(default_response) + + def run_and_destroy(self): + response = self.run() + self.hide() + return response + + +class ErrorsDialog(Gtk.Dialog): + """ Display flowgraph errors. """ + + def __init__(self, parent, flowgraph): + """Create a listview of errors""" + Gtk.Dialog.__init__( + self, + title='Errors and Warnings', + transient_for=parent, + modal=True, + destroy_with_parent=True, + ) + self.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT) + self.set_size_request(750, Constants.MIN_DIALOG_HEIGHT) + self.set_border_width(10) + + self.store = Gtk.ListStore(str, str, str) + self.update(flowgraph) + + self.treeview = Gtk.TreeView(model=self.store) + for i, column_title in enumerate(["Block", "Aspect", "Message"]): + renderer = Gtk.CellRendererText() + column = Gtk.TreeViewColumn(column_title, renderer, text=i) + column.set_sort_column_id(i) # liststore id matches treeview id + column.set_resizable(True) + self.treeview.append_column(column) + + self.scrollable = Gtk.ScrolledWindow() + self.scrollable.set_vexpand(True) + self.scrollable.add(self.treeview) + + self.vbox.pack_start(self.scrollable, True, True, 0) + self.show_all() + + def update(self, flowgraph): + self.store.clear() + for element, message in flowgraph.iter_error_messages(): + if element.is_block: + src, aspect = element.get_id(), '' + elif element.is_connection: + src = element.source_block.get_id() + aspect = "Connection to '{}'".format(element.sink_block.get_id()) + elif element.is_port: + src = element.parent_block.get_id() + aspect = "{} '{}'".format('Sink' if element.is_sink else 'Source', element.name) + elif element.is_param: + src = element.parent_block.get_id() + aspect = "Param '{}'".format(element.name) + else: + src = aspect = '' + self.store.append([src, aspect, message]) + + def run_and_destroy(self): + response = self.run() + self.hide() + return response + + +def show_about(parent, config): + ad = Gtk.AboutDialog(transient_for=parent) + ad.set_program_name(config.name) + ad.set_name('') + ad.set_version(config.version) + ad.set_license(config.license) + + try: + ad.set_logo(Gtk.IconTheme().load_icon('gnuradio-grc', 64, 0)) + except: + pass + + ad.set_copyright(config.license.splitlines()[0]) + ad.set_website(config.website) + + ad.connect("response", lambda action, param: action.hide()) + ad.show() + + +def show_help(parent): + """ Display basic usage tips. """ + markup = textwrap.dedent("""\ + <b>Usage Tips</b> + \n\ + <u>Add block</u>: drag and drop or double click a block in the block selection window. + <u>Rotate block</u>: Select a block, press left/right on the keyboard. + <u>Change type</u>: Select a block, press up/down on the keyboard. + <u>Edit parameters</u>: double click on a block in the flow graph. + <u>Make connection</u>: click on the source port of one block, then click on the sink port of another block. + <u>Remove connection</u>: select the connection and press delete, or drag the connection. + \n\ + * See the menu for other keyboard shortcuts.\ + """) + + MessageDialogWrapper( + parent, Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE, title='Help', markup=markup + ).run_and_destroy() + + +def show_types(parent): + """ Display information about standard data types. """ + colors = [(name, color) for name, key, sizeof, color in Constants.CORE_TYPES] + max_len = 10 + max(len(name) for name, code in colors) + + message = '\n'.join( + '<span background="{color}"><tt>{name}</tt></span>' + ''.format(color=color, name=Utils.encode(name).center(max_len)) + for name, color in colors ) + MessageDialogWrapper( + parent, Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE, title='Types - Color Mapping', markup=message + ).run_and_destroy() -def MissingXTermDialog(xterm): - MessageDialogHelper( - type=gtk.MESSAGE_WARNING, - buttons=gtk.BUTTONS_OK, - title='Warning: missing xterm executable', - markup=("The xterm executable {0!r} is missing.\n\n" - "You can change this setting in your gnuradio.conf, in " - "section [grc], 'xterm_executable'.\n" - "\n" - "(This message is shown only once)").format(xterm) - ) +def show_missing_xterm(parent, xterm): + markup = textwrap.dedent("""\ + The xterm executable {0!r} is missing. + You can change this setting in your gnurado.conf, in section [grc], 'xterm_executable'. + \n\ + (This message is shown only once)\ + """).format(xterm) + + MessageDialogWrapper( + parent, message_type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK, + title='Warning: missing xterm executable', markup=markup + ).run_and_destroy() + + +def choose_editor(parent, config): + """ + Give the option to either choose an editor or use the default. + """ + if config.editor and find_executable(config.editor): + return config.editor -def ChooseEditorDialog(config): - # Give the option to either choose an editor or use the default - # Always return true/false so the caller knows it was successful buttons = ( - 'Choose Editor', gtk.RESPONSE_YES, - 'Use Default', gtk.RESPONSE_NO, - gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL - ) - response = MessageDialogHelper( - gtk.MESSAGE_QUESTION, gtk.BUTTONS_NONE, 'Choose Editor', - 'Would you like to choose the editor to use?', gtk.RESPONSE_YES, buttons + 'Choose Editor', Gtk.ResponseType.YES, + 'Use Default', Gtk.ResponseType.NO, + Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL ) + response = MessageDialogWrapper( + parent, message_type=Gtk.MessageType.QUESTION, buttons=Gtk.ButtonsType.NONE, + title='Choose Editor', markup='Would you like to choose the editor to use?', + default_response=Gtk.ResponseType.YES, extra_buttons=buttons + ).run_and_destroy() - # Handle the inital default/choose/cancel response + # Handle the initial default/choose/cancel response # User wants to choose the editor to use - if response == gtk.RESPONSE_YES: - file_dialog = gtk.FileChooserDialog( + editor = '' + if response == Gtk.ResponseType.YES: + file_dialog = Gtk.FileChooserDialog( 'Select an Editor...', None, - gtk.FILE_CHOOSER_ACTION_OPEN, - ('gtk-cancel', gtk.RESPONSE_CANCEL, 'gtk-open', gtk.RESPONSE_OK) + Gtk.FileChooserAction.OPEN, + ('gtk-cancel', Gtk.ResponseType.CANCEL, 'gtk-open', Gtk.ResponseType.OK), + transient_for=parent ) file_dialog.set_select_multiple(False) file_dialog.set_local_only(True) file_dialog.set_current_folder('/usr/bin') try: - if file_dialog.run() == gtk.RESPONSE_OK: - config.editor = file_path = file_dialog.get_filename() - file_dialog.destroy() - return file_path + if file_dialog.run() == Gtk.ResponseType.OK: + editor = file_dialog.get_filename() finally: - file_dialog.destroy() + file_dialog.hide() # Go with the default editor - elif response == gtk.RESPONSE_NO: - # Determine the platform + elif response == Gtk.ResponseType.NO: try: process = None if sys.platform.startswith('linux'): @@ -282,13 +378,10 @@ def ChooseEditorDialog(config): if process is None: raise ValueError("Can't find default editor executable") # Save - config.editor = process - return process + editor = config.editor = process except Exception: Messages.send('>>> Unable to load the default editor. Please choose an editor.\n') - # Just reset of the constant and force the user to select an editor the next time - config.editor = '' - return - Messages.send('>>> No editor selected.\n') - return + if editor == '': + Messages.send('>>> No editor selected.\n') + return editor diff --git a/grc/gui/DrawingArea.py b/grc/gui/DrawingArea.py index 6a1df27a8c..f279d50557 100644 --- a/grc/gui/DrawingArea.py +++ b/grc/gui/DrawingArea.py @@ -17,15 +17,14 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk +from __future__ import absolute_import +from gi.repository import Gtk, Gdk, GObject -from Constants import MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT, DND_TARGETS -import Colors +from . import Constants, Colors -class DrawingArea(gtk.DrawingArea): + +class DrawingArea(Gtk.DrawingArea): """ DrawingArea is the gtk pixel map that graphical elements may draw themselves on. The drawing area also responds to mouse and key events. @@ -39,52 +38,47 @@ class DrawingArea(gtk.DrawingArea): Args: main_window: the main_window containing all flow graphs """ + Gtk.DrawingArea.__init__(self) + + self._flow_graph = flow_graph + + self.zoom_factor = 1.0 self.ctrl_mask = False self.mod1_mask = False - self._flow_graph = flow_graph - gtk.DrawingArea.__init__(self) - self.set_size_request(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT) + self.button_state = [False] * 10 + + # self.set_size_request(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT) self.connect('realize', self._handle_window_realize) - self.connect('configure-event', self._handle_window_configure) - self.connect('expose-event', self._handle_window_expose) + self.connect('draw', self.draw) self.connect('motion-notify-event', self._handle_mouse_motion) self.connect('button-press-event', self._handle_mouse_button_press) self.connect('button-release-event', self._handle_mouse_button_release) self.connect('scroll-event', self._handle_mouse_scroll) self.add_events( - gtk.gdk.BUTTON_PRESS_MASK | \ - gtk.gdk.POINTER_MOTION_MASK | \ - gtk.gdk.BUTTON_RELEASE_MASK | \ - gtk.gdk.LEAVE_NOTIFY_MASK | \ - gtk.gdk.ENTER_NOTIFY_MASK | \ - gtk.gdk.FOCUS_CHANGE_MASK + Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.POINTER_MOTION_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK | + Gdk.EventMask.SCROLL_MASK | + Gdk.EventMask.LEAVE_NOTIFY_MASK | + Gdk.EventMask.ENTER_NOTIFY_MASK + #Gdk.EventMask.FOCUS_CHANGE_MASK ) - #setup drag and drop - self.drag_dest_set(gtk.DEST_DEFAULT_ALL, DND_TARGETS, gtk.gdk.ACTION_COPY) + + # setup drag and drop + self.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY) self.connect('drag-data-received', self._handle_drag_data_received) - #setup the focus flag + self.drag_dest_set_target_list(None) + self.drag_dest_add_text_targets() + + # setup the focus flag self._focus_flag = False self.get_focus_flag = lambda: self._focus_flag def _handle_notify_event(widget, event, focus_flag): self._focus_flag = focus_flag self.connect('leave-notify-event', _handle_notify_event, False) self.connect('enter-notify-event', _handle_notify_event, True) - self.set_flags(gtk.CAN_FOCUS) # self.set_can_focus(True) - self.connect('focus-out-event', self._handle_focus_lost_event) - - def new_pixmap(self, width, height): - return gtk.gdk.Pixmap(self.window, width, height, -1) - - def get_screenshot(self, transparent_bg=False): - pixmap = self._pixmap - W, H = pixmap.get_size() - pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, W, H) - pixbuf.fill(0xFF + Colors.FLOWGRAPH_BACKGROUND_COLOR.pixel << 8) - pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, W-1, H-1) - if transparent_bg: - bgc = Colors.FLOWGRAPH_BACKGROUND_COLOR - pixbuf = pixbuf.add_alpha(True, bgc.red, bgc.green, bgc.blue) - return pixbuf - + # todo: fix +# self.set_flags(Gtk.CAN_FOCUS) # self.set_can_focus(True) +# self.connect('focus-out-event', self._handle_focus_lost_event) ########################################################################## ## Handlers @@ -93,83 +87,121 @@ class DrawingArea(gtk.DrawingArea): """ Handle a drag and drop by adding a block at the given coordinate. """ - self._flow_graph.add_new_block(selection_data.data, (x, y)) + self._flow_graph.add_new_block(selection_data.get_text(), (x, y)) def _handle_mouse_scroll(self, widget, event): - if event.state & gtk.gdk.SHIFT_MASK: - if event.direction == gtk.gdk.SCROLL_UP: - event.direction = gtk.gdk.SCROLL_LEFT - else: - event.direction = gtk.gdk.SCROLL_RIGHT + if event.get_state() & Gdk.ModifierType.SHIFT_MASK: + if event.direction == Gdk.ScrollDirection.UP: + event.direction = Gdk.ScrollDirection.LEFT + else: + event.direction = Gdk.ScrollDirection.RIGHT + + elif event.get_state() & Gdk.ModifierType.CONTROL_MASK: + change = 1.2 if event.direction == Gdk.ScrollDirection.UP else 1/1.2 + zoom_factor = min(max(self.zoom_factor * change, 0.1), 5.0) + + if zoom_factor != self.zoom_factor: + self.zoom_factor = zoom_factor + self.queue_draw() + return True + + return False def _handle_mouse_button_press(self, widget, event): """ Forward button click information to the flow graph. """ self.grab_focus() - self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK - self.mod1_mask = event.state & gtk.gdk.MOD1_MASK - if event.button == 1: self._flow_graph.handle_mouse_selector_press( - double_click=(event.type == gtk.gdk._2BUTTON_PRESS), - coordinate=(event.x, event.y), - ) - if event.button == 3: self._flow_graph.handle_mouse_context_press( - coordinate=(event.x, event.y), - event=event, - ) + self.ctrl_mask = event.get_state() & Gdk.ModifierType.CONTROL_MASK + self.mod1_mask = event.get_state() & Gdk.ModifierType.MOD1_MASK + self.button_state[event.button] = True + + if event.button == 1: + double_click = (event.type == Gdk.EventType._2BUTTON_PRESS) + self.button_state[1] = not double_click + self._flow_graph.handle_mouse_selector_press( + double_click=double_click, + coordinate=self._translate_event_coords(event), + ) + elif event.button == 3: + self._flow_graph.handle_mouse_context_press( + coordinate=self._translate_event_coords(event), + event=event, + ) def _handle_mouse_button_release(self, widget, event): """ Forward button release information to the flow graph. """ - self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK - self.mod1_mask = event.state & gtk.gdk.MOD1_MASK - if event.button == 1: self._flow_graph.handle_mouse_selector_release( - coordinate=(event.x, event.y), - ) + self.ctrl_mask = event.get_state() & Gdk.ModifierType.CONTROL_MASK + self.mod1_mask = event.get_state() & Gdk.ModifierType.MOD1_MASK + self.button_state[event.button] = False + if event.button == 1: + self._flow_graph.handle_mouse_selector_release( + coordinate=self._translate_event_coords(event), + ) def _handle_mouse_motion(self, widget, event): """ Forward mouse motion information to the flow graph. """ - self.ctrl_mask = event.state & gtk.gdk.CONTROL_MASK - self.mod1_mask = event.state & gtk.gdk.MOD1_MASK + self.ctrl_mask = event.get_state() & Gdk.ModifierType.CONTROL_MASK + self.mod1_mask = event.get_state() & Gdk.ModifierType.MOD1_MASK + + if self.button_state[1]: + self._auto_scroll(event) + self._flow_graph.handle_mouse_motion( - coordinate=(event.x, event.y), + coordinate=self._translate_event_coords(event), ) + def _auto_scroll(self, event): + x, y = event.x, event.y + scrollbox = self.get_parent().get_parent() + + w, h = self._flow_graph.get_max_coords(initial=(x, y)) + self.set_size_request(w + 100, h + 100) + + def scroll(pos, adj): + """scroll if we moved near the border""" + adj_val = adj.get_value() + adj_len = adj.get_page_size() + if pos - adj_val > adj_len - Constants.SCROLL_PROXIMITY_SENSITIVITY: + adj.set_value(adj_val + Constants.SCROLL_DISTANCE) + adj.emit('changed') + elif pos - adj_val < Constants.SCROLL_PROXIMITY_SENSITIVITY: + adj.set_value(adj_val - Constants.SCROLL_DISTANCE) + adj.emit('changed') + + scroll(x, scrollbox.get_hadjustment()) + scroll(y, scrollbox.get_vadjustment()) + def _handle_window_realize(self, widget): """ Called when the window is realized. Update the flowgraph, which calls new pixmap. """ self._flow_graph.update() + w, h = self._flow_graph.get_max_coords() + self.set_size_request(w + 100, h + 100) - def _handle_window_configure(self, widget, event): - """ - Called when the window is resized. - Create a new pixmap for background buffer. - """ - self._pixmap = self.new_pixmap(*self.get_size_request()) + def draw(self, widget, cr): + width = widget.get_allocated_width() + height = widget.get_allocated_height() + cr.set_source_rgba(*Colors.FLOWGRAPH_BACKGROUND_COLOR) + cr.rectangle(0, 0, width, height) - def _handle_window_expose(self, widget, event): - """ - Called when window is exposed, or queue_draw is called. - Double buffering: draw to pixmap, then draw pixmap to window. - """ - gc = self.window.new_gc() - self._flow_graph.draw(gc, self._pixmap) - self.window.draw_drawable(gc, self._pixmap, 0, 0, 0, 0, -1, -1) - # draw a light grey line on the bottom and right end of the canvas. - # this is useful when the theme uses the same panel bg color as the canvas - W, H = self._pixmap.get_size() - gc.set_foreground(Colors.FLOWGRAPH_EDGE_COLOR) - self.window.draw_line(gc, 0, H-1, W, H-1) - self.window.draw_line(gc, W-1, 0, W-1, H) + cr.scale(self.zoom_factor, self.zoom_factor) + cr.fill() + + self._flow_graph.draw(cr) + + def _translate_event_coords(self, event): + return event.x / self.zoom_factor, event.y / self.zoom_factor def _handle_focus_lost_event(self, widget, event): # don't clear selection while context menu is active - if not self._flow_graph.get_context_menu().flags() & gtk.VISIBLE: + if not self._flow_graph.get_context_menu().flags() & Gtk.VISIBLE: self._flow_graph.unselect() self._flow_graph.update_selected() self._flow_graph.queue_draw() diff --git a/grc/gui/Element.py b/grc/gui/Element.py index 9385424772..17cd6ddd92 100644 --- a/grc/gui/Element.py +++ b/grc/gui/Element.py @@ -1,5 +1,5 @@ """ -Copyright 2007, 2008, 2009 Free Software Foundation, Inc. +Copyright 2007, 2008, 2009, 2016 Free Software Foundation, Inc. This file is part of GNU Radio GNU Radio Companion is free software; you can redistribute it and/or @@ -17,10 +17,10 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -from Constants import LINE_SELECT_SENSITIVITY -from Constants import POSSIBLE_ROTATIONS +from __future__ import absolute_import +from .Constants import LINE_SELECT_SENSITIVITY -import gtk +from six.moves import zip class Element(object): @@ -30,17 +30,23 @@ class Element(object): and methods to detect selection of those areas. """ + @classmethod + def make_cls_with_base(cls, super_cls): + name = super_cls.__name__ + bases = (super_cls,) + cls.__bases__[1:] + namespace = cls.__dict__.copy() + return type(name, bases, namespace) + def __init__(self): """ Make a new list of rectangular areas and lines, and set the coordinate and the rotation. """ - self.set_rotation(POSSIBLE_ROTATIONS[0]) - self.set_coordinate((0, 0)) - self.clear() - self.set_highlighted(False) - self.line_attributes = [ - 0, gtk.gdk.LINE_SOLID, gtk.gdk.CAP_BUTT, gtk.gdk.JOIN_MITER - ] + self.coordinate = (0, 0) + self.rotation = 0 + self.highlighted = False + + self._bounding_rects = [] + self._bounding_points = [] def is_horizontal(self, rotation=None): """ @@ -53,7 +59,7 @@ class Element(object): Returns: true if rotation is horizontal """ - rotation = rotation or self.get_rotation() + rotation = rotation or self.rotation return rotation in (0, 180) def is_vertical(self, rotation=None): @@ -67,48 +73,9 @@ class Element(object): Returns: true if rotation is vertical """ - rotation = rotation or self.get_rotation() + rotation = rotation or self.rotation return rotation in (90, 270) - def create_labels(self): - """ - Create labels (if applicable) and call on all children. - Call this base method before creating labels in the element. - """ - for child in self.get_children():child.create_labels() - - def create_shapes(self): - """ - Create shapes (if applicable) and call on all children. - Call this base method before creating shapes in the element. - """ - self.clear() - for child in self.get_children(): child.create_shapes() - - def draw(self, gc, window, border_color, bg_color): - """ - Draw in the given window. - - Args: - gc: the graphics context - window: the gtk window to draw on - border_color: the color for lines and rectangle borders - bg_color: the color for the inside of the rectangle - """ - X, Y = self.get_coordinate() - gc.set_line_attributes(*self.line_attributes) - for (rX, rY), (W, H) in self._areas_list: - aX = X + rX - aY = Y + rY - gc.set_foreground(bg_color) - window.draw_rectangle(gc, True, aX, aY, W, H) - gc.set_foreground(border_color) - window.draw_rectangle(gc, False, aX, aY, W, H) - for (x1, y1), (x2, y2) in self._lines_list: - gc.set_foreground(border_color) - gc.set_background(bg_color) - window.draw_line(gc, X+x1, Y+y1, X+x2, Y+y2) - def rotate(self, rotation): """ Rotate all of the areas by 90 degrees. @@ -116,56 +83,7 @@ class Element(object): Args: rotation: multiple of 90 degrees """ - self.set_rotation((self.get_rotation() + rotation)%360) - - def clear(self): - """Empty the lines and areas.""" - self._areas_list = list() - self._lines_list = list() - - def set_coordinate(self, coor): - """ - Set the reference coordinate. - - Args: - coor: the coordinate tuple (x,y) - """ - self.coor = coor - - # def get_parent(self): - # """ - # Get the parent of this element. - # - # Returns: - # the parent - # """ - # return self.parent - - def set_highlighted(self, highlighted): - """ - Set the highlight status. - - Args: - highlighted: true to enable highlighting - """ - self.highlighted = highlighted - - def is_highlighted(self): - """ - Get the highlight status. - - Returns: - true if highlighted - """ - return self.highlighted - - def get_coordinate(self): - """Get the coordinate. - - Returns: - the coordinate tuple (x,y) - """ - return self.coor + self.rotation = (self.rotation + rotation) % 360 def move(self, delta_coor): """ @@ -174,37 +92,48 @@ class Element(object): Args: delta_coor: (delta_x,delta_y) tuple """ - deltaX, deltaY = delta_coor - X, Y = self.get_coordinate() - self.set_coordinate((X+deltaX, Y+deltaY)) + x, y = self.coordinate + dx, dy = delta_coor + self.coordinate = (x + dx, y + dy) - def add_area(self, rel_coor, area): + def create_labels(self): """ - Add an area to the area list. - An area is actually a coordinate relative to the main coordinate - with a width/height pair relative to the area coordinate. - A positive width is to the right of the coordinate. - A positive height is above the coordinate. - The area is associated with a rotation. - - Args: - rel_coor: (x,y) offset from this element's coordinate - area: (width,height) tuple + Create labels (if applicable) and call on all children. + Call this base method before creating labels in the element. """ - self._areas_list.append((rel_coor, area)) - def add_line(self, rel_coor1, rel_coor2): + def create_shapes(self): """ - Add a line to the line list. - A line is defined by 2 relative coordinates. - Lines must be horizontal or vertical. - The line is associated with a rotation. - - Args: - rel_coor1: relative (x1,y1) tuple - rel_coor2: relative (x2,y2) tuple + Create shapes (if applicable) and call on all children. + Call this base method before creating shapes in the element. """ - self._lines_list.append((rel_coor1, rel_coor2)) + + def draw(self, cr): + raise NotImplementedError() + + def bounds_from_area(self, area): + x1, y1, w, h = area + x2 = x1 + w + y2 = y1 + h + self._bounding_rects = [(x1, y1, x2, y2)] + self._bounding_points = [(x1, y1), (x2, y1), (x1, y2), (x2, y2)] + + def bounds_from_line(self, line): + self._bounding_rects = rects = [] + self._bounding_points = list(line) + last_point = line[0] + for x2, y2 in line[1:]: + (x1, y1), last_point = last_point, (x2, y2) + if x1 == x2: + x1, x2 = x1 - LINE_SELECT_SENSITIVITY, x2 + LINE_SELECT_SENSITIVITY + if y2 < y1: + y1, y2 = y2, y1 + elif y1 == y2: + y1, y2 = y1 - LINE_SELECT_SENSITIVITY, y2 + LINE_SELECT_SENSITIVITY + if x2 < x1: + x1, x2 = x2, x1 + + rects.append((x1, y1, x2, y2)) def what_is_selected(self, coor, coor_m=None): """ @@ -222,54 +151,31 @@ class Element(object): Returns: self if one of the areas/lines encompasses coor, else None. """ - #function to test if p is between a and b (inclusive) - in_between = lambda p, a, b: p >= min(a, b) and p <= max(a, b) - #relative coordinate - x, y = [a-b for a,b in zip(coor, self.get_coordinate())] - if coor_m: - x_m, y_m = [a-b for a,b in zip(coor_m, self.get_coordinate())] - #handle rectangular areas - for (x1,y1), (w,h) in self._areas_list: - if in_between(x1, x, x_m) and in_between(y1, y, y_m) or \ - in_between(x1+w, x, x_m) and in_between(y1, y, y_m) or \ - in_between(x1, x, x_m) and in_between(y1+h, y, y_m) or \ - in_between(x1+w, x, x_m) and in_between(y1+h, y, y_m): - return self - #handle horizontal or vertical lines - for (x1, y1), (x2, y2) in self._lines_list: - if in_between(x1, x, x_m) and in_between(y1, y, y_m) or \ - in_between(x2, x, x_m) and in_between(y2, y, y_m): + x, y = [a - b for a, b in zip(coor, self.coordinate)] + + if not coor_m: + for x1, y1, x2, y2 in self._bounding_rects: + if x1 <= x <= x2 and y1 <= y <= y2: return self - return None else: - #handle rectangular areas - for (x1,y1), (w,h) in self._areas_list: - if in_between(x, x1, x1+w) and in_between(y, y1, y1+h): return self - #handle horizontal or vertical lines - for (x1, y1), (x2, y2) in self._lines_list: - if x1 == x2: x1, x2 = x1-LINE_SELECT_SENSITIVITY, x2+LINE_SELECT_SENSITIVITY - if y1 == y2: y1, y2 = y1-LINE_SELECT_SENSITIVITY, y2+LINE_SELECT_SENSITIVITY - if in_between(x, x1, x2) and in_between(y, y1, y2): return self - return None - - def get_rotation(self): - """ - Get the rotation in degrees. - - Returns: - the rotation - """ - return self.rotation - - def set_rotation(self, rotation): - """ - Set the rotation in degrees. + x_m, y_m = [a - b for a, b in zip(coor_m, self.coordinate)] + if y_m < y: + y, y_m = y_m, y + if x_m < x: + x, x_m = x_m, x + + for x1, y1 in self._bounding_points: + if x <= x1 <= x_m and y <= y1 <= y_m: + return self - Args: - rotation: the rotation""" - if rotation not in POSSIBLE_ROTATIONS: - raise Exception('"%s" is not one of the possible rotations: (%s)'%(rotation, POSSIBLE_ROTATIONS)) - self.rotation = rotation + @property + def extend(self): + x_min, y_min = x_max, y_max = self.coordinate + x_min += min(x for x, y in self._bounding_points) + y_min += min(y for x, y in self._bounding_points) + x_max += max(x for x, y in self._bounding_points) + y_max += max(y for x, y in self._bounding_points) + return x_min, y_min, x_max, y_max def mouse_over(self): pass diff --git a/grc/gui/Executor.py b/grc/gui/Executor.py index bf9eecb9a8..a8c67986e5 100644 --- a/grc/gui/Executor.py +++ b/grc/gui/Executor.py @@ -15,7 +15,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import gobject +from __future__ import absolute_import import os import threading import shlex @@ -24,6 +24,8 @@ import sys import re from distutils.spawn import find_executable +from gi.repository import GLib + from ..core import Messages @@ -44,8 +46,7 @@ class ExecFlowGraphThread(threading.Thread): self.update_callback = callback try: - self.process = self._popen() - self.page.set_proc(self.process) + self.process = self.page.process = self._popen() self.update_callback() self.start() except Exception as e: @@ -56,7 +57,7 @@ class ExecFlowGraphThread(threading.Thread): """ Execute this python flow graph. """ - run_command = self.page.get_flow_graph().get_option('run_command') + run_command = self.page.flow_graph.get_option('run_command') generator = self.page.get_generator() try: @@ -86,20 +87,20 @@ class ExecFlowGraphThread(threading.Thread): def run(self): """ Wait on the executing process by reading from its stdout. - Use gobject.idle_add when calling functions that modify gtk objects. + Use GObject.idle_add when calling functions that modify gtk objects. """ # handle completion r = "\n" while r: - gobject.idle_add(Messages.send_verbose_exec, r) + GLib.idle_add(Messages.send_verbose_exec, r) r = os.read(self.process.stdout.fileno(), 1024) self.process.poll() - gobject.idle_add(self.done) + GLib.idle_add(self.done) def done(self): """Perform end of execution tasks.""" Messages.send_end_exec(self.process.returncode) - self.page.set_proc(None) + self.page.process = None self.update_callback() diff --git a/grc/gui/FileDialogs.py b/grc/gui/FileDialogs.py index 9d047b1ffd..cbb7bcaa69 100644 --- a/grc/gui/FileDialogs.py +++ b/grc/gui/FileDialogs.py @@ -17,140 +17,99 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk -from Dialogs import MessageDialogHelper -from Constants import \ - DEFAULT_FILE_PATH, IMAGE_FILE_EXTENSION, TEXT_FILE_EXTENSION, \ - NEW_FLOGRAPH_TITLE -import Preferences -from os import path -import Utils - -################################################## -# Constants -################################################## -OPEN_FLOW_GRAPH = 'open flow graph' -SAVE_FLOW_GRAPH = 'save flow graph' -SAVE_CONSOLE = 'save console' -SAVE_IMAGE = 'save image' -OPEN_QSS_THEME = 'open qss theme' - -FILE_OVERWRITE_MARKUP_TMPL="""\ -File <b>$encode($filename)</b> Exists!\nWould you like to overwrite the existing file?""" - -FILE_DNE_MARKUP_TMPL="""\ -File <b>$encode($filename)</b> Does not Exist!""" - - - -# File Filters -def get_flow_graph_files_filter(): - filter = gtk.FileFilter() - filter.set_name('Flow Graph Files') - filter.add_pattern('*'+Preferences.file_extension()) - return filter - - -def get_text_files_filter(): - filter = gtk.FileFilter() - filter.set_name('Text Files') - filter.add_pattern('*'+TEXT_FILE_EXTENSION) - return filter - - -def get_image_files_filter(): - filter = gtk.FileFilter() - filter.set_name('Image Files') - filter.add_pattern('*'+IMAGE_FILE_EXTENSION) - return filter +from __future__ import absolute_import +from os import path -def get_all_files_filter(): - filter = gtk.FileFilter() - filter.set_name('All Files') - filter.add_pattern('*') - return filter - +from gi.repository import Gtk -def get_qss_themes_filter(): - filter = gtk.FileFilter() - filter.set_name('QSS Themes') - filter.add_pattern('*.qss') - return filter +from . import Constants, Preferences, Utils, Dialogs -# File Dialogs -class FileDialogHelper(gtk.FileChooserDialog): +class FileDialogHelper(Gtk.FileChooserDialog, object): """ A wrapper class for the gtk file chooser dialog. Implement a file chooser dialog with only necessary parameters. """ + title = '' + action = Gtk.FileChooserAction.OPEN + filter_label = '' + filter_ext = '' - def __init__(self, action, title): + def __init__(self, parent, current_file_path): """ FileDialogHelper contructor. Create a save or open dialog with cancel and ok buttons. Use standard settings: no multiple selection, local files only, and the * filter. Args: - action: gtk.FILE_CHOOSER_ACTION_OPEN or gtk.FILE_CHOOSER_ACTION_SAVE + action: Gtk.FileChooserAction.OPEN or Gtk.FileChooserAction.SAVE title: the title of the dialog (string) """ - ok_stock = {gtk.FILE_CHOOSER_ACTION_OPEN : 'gtk-open', gtk.FILE_CHOOSER_ACTION_SAVE : 'gtk-save'}[action] - gtk.FileChooserDialog.__init__(self, title, None, action, ('gtk-cancel', gtk.RESPONSE_CANCEL, ok_stock, gtk.RESPONSE_OK)) + ok_stock = { + Gtk.FileChooserAction.OPEN: 'gtk-open', + Gtk.FileChooserAction.SAVE: 'gtk-save' + }[self.action] + + Gtk.FileChooserDialog.__init__(self, title=self.title, action=self.action, + transient_for=parent) + self.add_buttons('gtk-cancel', Gtk.ResponseType.CANCEL, ok_stock, Gtk.ResponseType.OK) self.set_select_multiple(False) self.set_local_only(True) - self.add_filter(get_all_files_filter()) + + self.parent = parent + self.current_file_path = current_file_path or path.join( + Constants.DEFAULT_FILE_PATH, Constants.NEW_FLOGRAPH_TITLE + Preferences.file_extension()) + + self.set_current_folder(path.dirname(current_file_path)) # current directory + self.setup_filters() + + def setup_filters(self, filters=None): + set_default = True + filters = filters or ([(self.filter_label, self.filter_ext)] if self.filter_label else []) + filters.append(('All Files', '')) + for label, ext in filters: + if not label: + continue + f = Gtk.FileFilter() + f.set_name(label) + f.add_pattern('*' + ext) + self.add_filter(f) + if not set_default: + self.set_filter(f) + set_default = True + + def run(self): + """Get the filename and destroy the dialog.""" + response = Gtk.FileChooserDialog.run(self) + filename = self.get_filename() if response == Gtk.ResponseType.OK else None + self.destroy() + return filename -class FileDialog(FileDialogHelper): +class SaveFileDialog(FileDialogHelper): """A dialog box to save or open flow graph files. This is a base class, do not use.""" + action = Gtk.FileChooserAction.SAVE - def __init__(self, current_file_path=''): - """ - FileDialog constructor. + def __init__(self, parent, current_file_path): + super(SaveFileDialog, self).__init__(parent, current_file_path) + self.set_current_name(path.splitext(path.basename(self.current_file_path))[0] + self.filter_ext) + self.set_create_folders(True) + self.set_do_overwrite_confirmation(True) - Args: - current_file_path: the current directory or path to the open flow graph - """ - if not current_file_path: current_file_path = path.join(DEFAULT_FILE_PATH, NEW_FLOGRAPH_TITLE + Preferences.file_extension()) - if self.type == OPEN_FLOW_GRAPH: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_OPEN, 'Open a Flow Graph from a File...') - self.add_and_set_filter(get_flow_graph_files_filter()) - self.set_select_multiple(True) - elif self.type == SAVE_FLOW_GRAPH: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save a Flow Graph to a File...') - self.add_and_set_filter(get_flow_graph_files_filter()) - self.set_current_name(path.basename(current_file_path)) - elif self.type == SAVE_CONSOLE: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save Console to a File...') - self.add_and_set_filter(get_text_files_filter()) - file_path = path.splitext(path.basename(current_file_path))[0] - self.set_current_name(file_path) #show the current filename - elif self.type == SAVE_IMAGE: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save a Flow Graph Screen Shot...') - self.add_and_set_filter(get_image_files_filter()) - current_file_path = current_file_path + IMAGE_FILE_EXTENSION - self.set_current_name(path.basename(current_file_path)) #show the current filename - elif self.type == OPEN_QSS_THEME: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_OPEN, 'Open a QSS theme...') - self.add_and_set_filter(get_qss_themes_filter()) - self.set_select_multiple(False) - self.set_current_folder(path.dirname(current_file_path)) #current directory - - def add_and_set_filter(self, filter): - """ - Add the gtk file filter to the list of filters and set it as the default file filter. - Args: - filter: a gtk file filter. - """ - self.add_filter(filter) - self.set_filter(filter) +class OpenFileDialog(FileDialogHelper): + """A dialog box to save or open flow graph files. This is a base class, do not use.""" + action = Gtk.FileChooserAction.OPEN + + def show_missing_message(self, filename): + Dialogs.MessageDialogWrapper( + self.parent, + Gtk.MessageType.WARNING, Gtk.ButtonsType.CLOSE, 'Cannot Open!', + 'File <b>{filename}</b> Does not Exist!'.format(filename=Utils.encode(filename)), + ).run_and_destroy() - def get_rectified_filename(self): + def get_filename(self): """ Run the dialog and get the filename. If this is a save dialog and the file name is missing the extension, append the file extension. @@ -160,82 +119,80 @@ class FileDialog(FileDialogHelper): Returns: the complete file path """ - if gtk.FileChooserDialog.run(self) != gtk.RESPONSE_OK: return None #response was cancel - ############################################# - # Handle Save Dialogs - ############################################# - if self.type in (SAVE_FLOW_GRAPH, SAVE_CONSOLE, SAVE_IMAGE): - filename = self.get_filename() - extension = { - SAVE_FLOW_GRAPH: Preferences.file_extension(), - SAVE_CONSOLE: TEXT_FILE_EXTENSION, - SAVE_IMAGE: IMAGE_FILE_EXTENSION, - }[self.type] - #append the missing file extension if the filter matches - if path.splitext(filename)[1].lower() != extension: filename += extension - self.set_current_name(path.basename(filename)) #show the filename with extension - if path.exists(filename): #ask the user to confirm overwrite - if MessageDialogHelper( - gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, 'Confirm Overwrite!', - Utils.parse_template(FILE_OVERWRITE_MARKUP_TMPL, filename=filename), - ) == gtk.RESPONSE_NO: return self.get_rectified_filename() - return filename - ############################################# - # Handle Open Dialogs - ############################################# - elif self.type in (OPEN_FLOW_GRAPH, OPEN_QSS_THEME): - filenames = self.get_filenames() - for filename in filenames: - if not path.exists(filename): #show a warning and re-run - MessageDialogHelper( - gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE, 'Cannot Open!', - Utils.parse_template(FILE_DNE_MARKUP_TMPL, filename=filename), - ) - return self.get_rectified_filename() - return filenames + filenames = Gtk.FileChooserDialog.get_filenames(self) + for filename in filenames: + if not path.exists(filename): + self.show_missing_message(filename) + return None # rerun + return filenames - def run(self): - """ - Get the filename and destroy the dialog. - Returns: - the filename or None if a close/cancel occurred. - """ - filename = self.get_rectified_filename() - self.destroy() - return filename - - -class OpenFlowGraphFileDialog(FileDialog): - type = OPEN_FLOW_GRAPH +class OpenFlowGraph(OpenFileDialog): + title = 'Open a Flow Graph from a File...' + filter_label = 'Flow Graph Files' + filter_ext = Preferences.file_extension() + def __init__(self, parent, current_file_path=''): + super(OpenFlowGraph, self).__init__(parent, current_file_path) + self.set_select_multiple(True) -class SaveFlowGraphFileDialog(FileDialog): - type = SAVE_FLOW_GRAPH +class OpenQSS(OpenFileDialog): + title = 'Open a QSS theme...' + filter_label = 'QSS Themes' + filter_ext = '.qss' -class OpenQSSFileDialog(FileDialog): - type = OPEN_QSS_THEME +class SaveFlowGraph(SaveFileDialog): + title = 'Save a Flow Graph to a File...' + filter_label = 'Flow Graph Files' + filter_ext = Preferences.file_extension() -class SaveConsoleFileDialog(FileDialog): - type = SAVE_CONSOLE +class SaveConsole(SaveFileDialog): + title = 'Save Console to a File...' + filter_label = 'Test Files' + filter_ext = '.txt' -class SaveImageFileDialog(FileDialog): - type = SAVE_IMAGE +class SaveScreenShot(SaveFileDialog): + title = 'Save a Flow Graph Screen Shot...' + filters = [('PDF Files', '.pdf'), ('PNG Files', '.png'), ('SVG Files', '.svg')] + filter_ext = '.pdf' # the default -class SaveScreenShotDialog(SaveImageFileDialog): + def __init__(self, parent, current_file_path=''): + super(SaveScreenShot, self).__init__(parent, current_file_path) - def __init__(self, current_file_path=''): - SaveImageFileDialog.__init__(self, current_file_path) - self._button = button = gtk.CheckButton('_Background transparent') + self._button = button = Gtk.CheckButton(label='Background transparent') self._button.set_active(Preferences.screen_shot_background_transparent()) self.set_extra_widget(button) + def setup_filters(self, filters=None): + super(SaveScreenShot, self).setup_filters(self.filters) + + def show_missing_message(self, filename): + Dialogs.MessageDialogWrapper( + self.parent, + Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, 'Can not Save!', + 'File Extention of <b>{filename}</b> not supported!'.format(filename=Utils.encode(filename)), + ).run_and_destroy() + def run(self): - filename = SaveImageFileDialog.run(self) + valid_exts = {ext for label, ext in self.filters} + filename = None + while True: + response = Gtk.FileChooserDialog.run(self) + if response != Gtk.ResponseType.OK: + filename = None + break + + filename = self.get_filename() + if path.splitext(filename)[1] in valid_exts: + break + + self.show_missing_message(filename) + bg_transparent = self._button.get_active() Preferences.screen_shot_background_transparent(bg_transparent) + self.destroy() return filename, bg_transparent diff --git a/grc/gui/FlowGraph.py b/grc/gui/FlowGraph.py index f98aec41d5..f04383f32c 100644 --- a/grc/gui/FlowGraph.py +++ b/grc/gui/FlowGraph.py @@ -1,5 +1,5 @@ """ -Copyright 2007-2011 Free Software Foundation, Inc. +Copyright 2007-2011, 2016q Free Software Foundation, Inc. This file is part of GNU Radio GNU Radio Companion is free software; you can redistribute it and/or @@ -17,53 +17,57 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import + import functools +import ast import random from distutils.spawn import find_executable -from itertools import chain, count -from operator import methodcaller +from itertools import count + +import six +from six.moves import filter -import gobject +from gi.repository import GLib from . import Actions, Colors, Constants, Utils, Bars, Dialogs -from .Constants import SCROLL_PROXIMITY_SENSITIVITY, SCROLL_DISTANCE from .Element import Element from .external_editor import ExternalEditor -from ..core.FlowGraph import FlowGraph as _Flowgraph +from ..core.FlowGraph import FlowGraph as CoreFlowgraph from ..core import Messages -class FlowGraph(Element, _Flowgraph): +class FlowGraph(CoreFlowgraph, Element): """ FlowGraph is the data structure to store graphical signal blocks, graphical inputs and outputs, and the connections between inputs and outputs. """ - def __init__(self, **kwargs): + def __init__(self, parent, **kwargs): """ FlowGraph constructor. Create a list for signal blocks and connections. Connect mouse handlers. """ + super(self.__class__, self).__init__(parent, **kwargs) Element.__init__(self) - _Flowgraph.__init__(self, **kwargs) - #when is the flow graph selected? (used by keyboard event handler) - self.is_selected = lambda: bool(self.get_selected_elements()) - #important vars dealing with mouse event tracking + self.drawing_area = None + # important vars dealing with mouse event tracking self.element_moved = False self.mouse_pressed = False - self._selected_elements = [] self.press_coor = (0, 0) - #selected ports + # selected + self.selected_elements = set() self._old_selected_port = None self._new_selected_port = None # current mouse hover element self.element_under_mouse = None - #context menu + # context menu self._context_menu = Bars.ContextMenu() self.get_context_menu = lambda: self._context_menu + self._elements_to_draw = [] self._external_updaters = {} def _get_unique_id(self, base_id=''): @@ -76,21 +80,22 @@ class FlowGraph(Element, _Flowgraph): Returns: a unique id """ + block_ids = set(b.get_id() for b in self.blocks) for index in count(): block_id = '{}_{}'.format(base_id, index) - if block_id not in (b.get_id() for b in self.blocks): + if block_id not in block_ids: break return block_id def install_external_editor(self, param): - target = (param.get_parent().get_id(), param.get_key()) + target = (param.parent_block.get_id(), param.key) if target in self._external_updaters: editor = self._external_updaters[target] else: - config = self.get_parent().config + config = self.parent_platform.config editor = (find_executable(config.editor) or - Dialogs.ChooseEditorDialog(config)) + Dialogs.choose_editor(None, config)) # todo: pass in parent if not editor: return updater = functools.partial( @@ -98,7 +103,7 @@ class FlowGraph(Element, _Flowgraph): editor = self._external_updaters[target] = ExternalEditor( editor=editor, name=target[0], value=param.get_value(), - callback=functools.partial(gobject.idle_add, updater) + callback=functools.partial(GLib.idle_add, updater) ) editor.start() try: @@ -107,7 +112,7 @@ class FlowGraph(Element, _Flowgraph): # Problem launching the editor. Need to select a new editor. Messages.send('>>> Error opening an external editor. Please select a different editor.\n') # Reset the editor to force the user to select a new one. - self.get_parent().config.editor = '' + self.parent_platform.config.editor = '' def handle_external_editor_change(self, new_value, target): try: @@ -120,19 +125,6 @@ class FlowGraph(Element, _Flowgraph): return Actions.EXTERNAL_UPDATE() - - ########################################################################### - # Access Drawing Area - ########################################################################### - def get_drawing_area(self): return self.drawing_area - def queue_draw(self): self.get_drawing_area().queue_draw() - def get_size(self): return self.get_drawing_area().get_size_request() - def set_size(self, *args): self.get_drawing_area().set_size_request(*args) - def get_scroll_pane(self): return self.drawing_area.get_parent() - def get_ctrl_mask(self): return self.drawing_area.ctrl_mask - def get_mod1_mask(self): return self.drawing_area.mod1_mask - def new_pixmap(self, *args): return self.get_drawing_area().new_pixmap(*args) - def add_new_block(self, key, coor=None): """ Add a block of the given key to this flow graph. @@ -142,24 +134,62 @@ class FlowGraph(Element, _Flowgraph): coor: an optional coordinate or None for random """ id = self._get_unique_id(key) - #calculate the position coordinate - W, H = self.get_size() - h_adj = self.get_scroll_pane().get_hadjustment() - v_adj = self.get_scroll_pane().get_vadjustment() + scroll_pane = self.drawing_area.get_parent().get_parent() + # calculate the position coordinate + h_adj = scroll_pane.get_hadjustment() + v_adj = scroll_pane.get_vadjustment() if coor is None: coor = ( - int(random.uniform(.25, .75) * min(h_adj.page_size, W) + - h_adj.get_value()), - int(random.uniform(.25, .75) * min(v_adj.page_size, H) + - v_adj.get_value()), + int(random.uniform(.25, .75)*h_adj.get_page_size() + h_adj.get_value()), + int(random.uniform(.25, .75)*v_adj.get_page_size() + v_adj.get_value()), ) - #get the new block + # get the new block block = self.new_block(key) - block.set_coordinate(coor) - block.set_rotation(0) + block.coordinate = coor block.get_param('id').set_value(id) Actions.ELEMENT_CREATE() return id + def make_connection(self): + """this selection and the last were ports, try to connect them""" + if self._old_selected_port and self._new_selected_port: + try: + self.connect(self._old_selected_port, self._new_selected_port) + Actions.ELEMENT_CREATE() + except Exception as e: + Messages.send_fail_connection(e) + self._old_selected_port = None + self._new_selected_port = None + return True + return False + + def update(self): + """ + Call the top level rewrite and validate. + Call the top level create labels and shapes. + """ + self.rewrite() + self.validate() + self.update_elements_to_draw() + self.create_labels() + self.create_shapes() + + def reload(self): + """ + Reload flow-graph (with updated blocks) + + Args: + page: the page to reload (None means current) + Returns: + False if some error occurred during import + """ + success = False + data = self.export_data() + if data: + self.unselect() + success = self.import_data(data) + self.update() + return success + ########################################################################### # Copy Paste ########################################################################### @@ -171,19 +201,20 @@ class FlowGraph(Element, _Flowgraph): the clipboard """ #get selected blocks - blocks = self.get_selected_blocks() - if not blocks: return None + blocks = list(self.selected_blocks()) + if not blocks: + return None #calc x and y min - x_min, y_min = blocks[0].get_coordinate() + x_min, y_min = blocks[0].coordinate for block in blocks: - x, y = block.get_coordinate() + x, y = block.coordinate x_min = min(x, x_min) y_min = min(y, y_min) #get connections between selected blocks - connections = filter( - lambda c: c.get_source().get_parent() in blocks and c.get_sink().get_parent() in blocks, + connections = list(filter( + lambda c: c.source_block in blocks and c.sink_block in blocks, self.connections, - ) + )) clipboard = ( (x_min, y_min), [block.export_data() for block in blocks], @@ -198,32 +229,39 @@ class FlowGraph(Element, _Flowgraph): Args: clipboard: the nested data of blocks, connections """ + # todo: rewrite this... selected = set() (x_min, y_min), blocks_n, connections_n = clipboard old_id2block = dict() - #recalc the position - h_adj = self.get_scroll_pane().get_hadjustment() - v_adj = self.get_scroll_pane().get_vadjustment() - x_off = h_adj.get_value() - x_min + h_adj.page_size/4 - y_off = v_adj.get_value() - y_min + v_adj.page_size/4 + # recalc the position + scroll_pane = self.drawing_area.get_parent().get_parent() + h_adj = scroll_pane.get_hadjustment() + v_adj = scroll_pane.get_vadjustment() + x_off = h_adj.get_value() - x_min + h_adj.get_page_size() / 4 + y_off = v_adj.get_value() - y_min + v_adj.get_page_size() / 4 if len(self.get_elements()) <= 1: x_off, y_off = 0, 0 #create blocks for block_n in blocks_n: - block_key = block_n.find('key') - if block_key == 'options': continue + block_key = block_n.get('key') + if block_key == 'options': + continue block = self.new_block(block_key) if not block: continue # unknown block was pasted (e.g. dummy block) selected.add(block) #set params - params = dict((n.find('key'), n.find('value')) - for n in block_n.findall('param')) + param_data = {n['key']: n['value'] for n in block_n.get('param', [])} + for key in block.states: + try: + block.states[key] = ast.literal_eval(param_data.pop(key)) + except (KeyError, SyntaxError, ValueError): + pass if block_key == 'epy_block': - block.get_param('_io_cache').set_value(params.pop('_io_cache')) - block.get_param('_source_code').set_value(params.pop('_source_code')) + block.get_param('_io_cache').set_value(param_data.pop('_io_cache')) + block.get_param('_source_code').set_value(param_data.pop('_source_code')) block.rewrite() # this creates the other params - for param_key, param_value in params.iteritems(): + for param_key, param_value in six.iteritems(param_data): #setup id parameter if param_key == 'id': old_id2block[param_value] = block @@ -238,12 +276,13 @@ class FlowGraph(Element, _Flowgraph): self.update() #create connections for connection_n in connections_n: - source = old_id2block[connection_n.find('source_block_id')].get_source(connection_n.find('source_key')) - sink = old_id2block[connection_n.find('sink_block_id')].get_sink(connection_n.find('sink_key')) + source = old_id2block[connection_n.get('source_block_id')].get_source(connection_n.get('source_key')) + sink = old_id2block[connection_n.get('sink_block_id')].get_sink(connection_n.get('sink_key')) self.connect(source, sink) #set all pasted elements selected - for block in selected: selected = selected.union(set(block.get_connections())) - self._selected_elements = list(selected) + for block in selected: + selected = selected.union(set(block.get_connections())) + self.selected_elements = set(selected) ########################################################################### # Modify Selected @@ -258,7 +297,7 @@ class FlowGraph(Element, _Flowgraph): Returns: true for change """ - return any([sb.type_controller_modify(direction) for sb in self.get_selected_blocks()]) + return any(sb.type_controller_modify(direction) for sb in self.selected_blocks()) def port_controller_modify_selected(self, direction): """ @@ -270,35 +309,22 @@ class FlowGraph(Element, _Flowgraph): Returns: true for changed """ - return any([sb.port_controller_modify(direction) for sb in self.get_selected_blocks()]) + return any(sb.port_controller_modify(direction) for sb in self.selected_blocks()) - def enable_selected(self, enable): + def change_state_selected(self, new_state): """ Enable/disable the selected blocks. Args: - enable: true to enable - - 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. + new_state: a block state - 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): @@ -308,10 +334,7 @@ class FlowGraph(Element, _Flowgraph): Args: delta_coordinate: the change in coordinates """ - for selected_block in self.get_selected_blocks(): - delta_coordinate = selected_block.bound_move_delta(delta_coordinate) - - for selected_block in self.get_selected_blocks(): + for selected_block in self.selected_blocks(): selected_block.move(delta_coordinate) self.element_moved = True @@ -325,17 +348,17 @@ class FlowGraph(Element, _Flowgraph): Returns: True if changed, otherwise False """ - blocks = self.get_selected_blocks() + blocks = list(self.selected_blocks()) if calling_action is None or not blocks: return False # compute common boundary of selected objects - min_x, min_y = max_x, max_y = blocks[0].get_coordinate() + min_x, min_y = max_x, max_y = blocks[0].coordinate for selected_block in blocks: - x, y = selected_block.get_coordinate() + x, y = selected_block.coordinate min_x, min_y = min(min_x, x), min(min_y, y) - x += selected_block.W - y += selected_block.H + x += selected_block.width + y += selected_block.height max_x, max_y = max(max_x, x), max(max_y, y) ctr_x, ctr_y = (max_x + min_x)/2, (max_y + min_y)/2 @@ -350,9 +373,9 @@ class FlowGraph(Element, _Flowgraph): }.get(calling_action, lambda *args: args) for selected_block in blocks: - x, y = selected_block.get_coordinate() - w, h = selected_block.W, selected_block.H - selected_block.set_coordinate(transform(x, y, w, h)) + x, y = selected_block.coordinate + w, h = selected_block.width, selected_block.height + selected_block.coordinate = transform(x, y, w, h) return True @@ -366,25 +389,24 @@ class FlowGraph(Element, _Flowgraph): Returns: true if changed, otherwise false. """ - if not self.get_selected_blocks(): + if not any(self.selected_blocks()): return False #initialize min and max coordinates - min_x, min_y = self.get_selected_block().get_coordinate() - max_x, max_y = self.get_selected_block().get_coordinate() - #rotate each selected block, and find min/max coordinate - for selected_block in self.get_selected_blocks(): + min_x, min_y = max_x, max_y = self.selected_block.coordinate + # rotate each selected block, and find min/max coordinate + for selected_block in self.selected_blocks(): selected_block.rotate(rotation) #update the min/max coordinate - x, y = selected_block.get_coordinate() + x, y = selected_block.coordinate min_x, min_y = min(min_x, x), min(min_y, y) max_x, max_y = max(max_x, x), max(max_y, y) #calculate center point of slected blocks ctr_x, ctr_y = (max_x + min_x)/2, (max_y + min_y)/2 #rotate the blocks around the center point - for selected_block in self.get_selected_blocks(): - x, y = selected_block.get_coordinate() + for selected_block in self.selected_blocks(): + x, y = selected_block.coordinate x, y = Utils.get_rotated_coordinate((x - ctr_x, y - ctr_y), rotation) - selected_block.set_coordinate((x + ctr_x, y + ctr_y)) + selected_block.coordinate = (x + ctr_x, y + ctr_y) return True def remove_selected(self): @@ -395,116 +417,140 @@ class FlowGraph(Element, _Flowgraph): true if changed. """ changed = False - for selected_element in self.get_selected_elements(): + for selected_element in self.selected_elements: self.remove_element(selected_element) changed = True return changed - def draw(self, gc, window): - """ - Draw the background and grid if enabled. - Draw all of the elements in this flow graph onto the pixmap. - Draw the pixmap to the drawable window of this flow graph. - """ - - W,H = self.get_size() - hide_disabled_blocks = Actions.TOGGLE_HIDE_DISABLED_BLOCKS.get_active() - hide_variables = Actions.TOGGLE_HIDE_VARIABLES.get_active() - - #draw the background - gc.set_foreground(Colors.FLOWGRAPH_BACKGROUND_COLOR) - window.draw_rectangle(gc, True, 0, 0, W, H) - - # draw comments first - if Actions.TOGGLE_SHOW_BLOCK_COMMENTS.get_active(): - for block in self.blocks: - if hide_variables and (block.is_variable or block.is_import): - continue # skip hidden disabled blocks and connections - if block.get_enabled(): - block.draw_comment(gc, window) - #draw multi select rectangle - if self.mouse_pressed and (not self.get_selected_elements() or self.get_ctrl_mask()): - #coordinates - x1, y1 = self.press_coor - x2, y2 = self.get_coordinate() - #calculate top-left coordinate and width/height - x, y = int(min(x1, x2)), int(min(y1, y2)) - w, h = int(abs(x1 - x2)), int(abs(y1 - y2)) - #draw - gc.set_foreground(Colors.HIGHLIGHT_COLOR) - window.draw_rectangle(gc, True, x, y, w, h) - gc.set_foreground(Colors.BORDER_COLOR) - window.draw_rectangle(gc, False, x, y, w, h) - #draw blocks on top of connections - blocks = sorted(self.blocks, key=methodcaller('get_enabled')) - for element in chain(self.connections, blocks): - if hide_disabled_blocks and not element.get_enabled(): - continue # skip hidden disabled blocks and connections - if hide_variables and (element.is_variable or element.is_import): - continue # skip hidden disabled blocks and connections - element.draw(gc, window) - #draw selected blocks on top of selected connections - for selected_element in self.get_selected_connections() + self.get_selected_blocks(): - selected_element.draw(gc, window) - def update_selected(self): """ Remove deleted elements from the selected elements list. Update highlighting so only the selected are highlighted. """ - selected_elements = self.get_selected_elements() + selected_elements = self.selected_elements elements = self.get_elements() - #remove deleted elements - for selected in selected_elements: - if selected in elements: continue + # remove deleted elements + for selected in list(selected_elements): + if selected in elements: + continue selected_elements.remove(selected) - if self._old_selected_port and self._old_selected_port.get_parent() not in elements: + if self._old_selected_port and self._old_selected_port.parent not in elements: self._old_selected_port = None - if self._new_selected_port and self._new_selected_port.get_parent() not in elements: + if self._new_selected_port and self._new_selected_port.parent not in elements: self._new_selected_port = None - #update highlighting + # update highlighting for element in elements: - element.set_highlighted(element in selected_elements) + element.highlighted = element in selected_elements - def update(self): - """ - Call the top level rewrite and validate. - Call the top level create labels and shapes. - """ - self.rewrite() - self.validate() - self.create_labels() - self.create_shapes() + ########################################################################### + # Draw stuff + ########################################################################### - def reload(self): - """ - Reload flow-graph (with updated blocks) + def update_elements_to_draw(self): + hide_disabled_blocks = Actions.TOGGLE_HIDE_DISABLED_BLOCKS.get_active() + hide_variables = Actions.TOGGLE_HIDE_VARIABLES.get_active() - Args: - page: the page to reload (None means current) - Returns: - False if some error occurred during import - """ - success = False - data = self.export_data() - if data: - self.unselect() - success = self.import_data(data) - self.update() - return success + def draw_order(elem): + return elem.highlighted, elem.is_block, elem.enabled + + elements = sorted(self.get_elements(), key=draw_order) + del self._elements_to_draw[:] + + for element in elements: + if hide_disabled_blocks and not element.enabled: + continue # skip hidden disabled blocks and connections + if hide_variables and (element.is_variable or element.is_import): + continue # skip hidden disabled blocks and connections + self._elements_to_draw.append(element) + + def create_labels(self): + for element in self._elements_to_draw: + element.create_labels() + + def create_shapes(self): + for element in self._elements_to_draw: + element.create_shapes() + + def _drawables(self): + show_comments = Actions.TOGGLE_SHOW_BLOCK_COMMENTS.get_active() + for element in self._elements_to_draw: + if element.is_block and show_comments and element.enabled: + yield element.draw_comment + for element in self._elements_to_draw: + yield element.draw + + def draw(self, cr): + """Draw blocks connections comment and select rectangle""" + for draw_element in self._drawables(): + cr.save() + draw_element(cr) + cr.restore() + + # draw multi select rectangle + if self.mouse_pressed and (not self.selected_elements or self.drawing_area.ctrl_mask): + x1, y1 = self.press_coor + x2, y2 = self.coordinate + x, y = int(min(x1, x2)), int(min(y1, y2)) + w, h = int(abs(x1 - x2)), int(abs(y1 - y2)) + cr.set_source_rgba( + Colors.HIGHLIGHT_COLOR[0], + Colors.HIGHLIGHT_COLOR[1], + Colors.HIGHLIGHT_COLOR[2], + 0.5, + ) + cr.rectangle(x, y, w, h) + cr.fill() + cr.rectangle(x, y, w, h) + cr.stroke() ########################################################################## - ## Get Selected + # selection handling ########################################################################## - def unselect(self): + def update_selected_elements(self): """ - Set selected elements to an empty set. + Update the selected elements. + The update behavior depends on the state of the mouse button. + When the mouse button pressed the selection will change when + the control mask is set or the new selection is not in the current group. + When the mouse button is released the selection will change when + the mouse has moved and the control mask is set or the current group is empty. + Attempt to make a new connection if the old and ports are filled. + If the control mask is set, merge with the current elements. """ - self._selected_elements = [] + selected_elements = None + if self.mouse_pressed: + new_selections = self.what_is_selected(self.coordinate) + # update the selections if the new selection is not in the current selections + # allows us to move entire selected groups of elements + if self.drawing_area.ctrl_mask or new_selections not in self.selected_elements: + selected_elements = new_selections - def select_all(self): - """Select all blocks in the flow graph""" - self._selected_elements = list(self.get_elements()) + if self._old_selected_port: + self._old_selected_port.force_show_label = False + self.create_shapes() + self.drawing_area.queue_draw() + elif self._new_selected_port: + self._new_selected_port.force_show_label = True + + else: # called from a mouse release + if not self.element_moved and (not self.selected_elements or self.drawing_area.ctrl_mask): + selected_elements = self.what_is_selected(self.coordinate, self.press_coor) + + # this selection and the last were ports, try to connect them + if self.make_connection(): + return + + # update selected elements + if selected_elements is None: + return + + # if ctrl, set the selected elements to the union - intersection of old and new + if self.drawing_area.ctrl_mask: + self.selected_elements ^= selected_elements + else: + self.selected_elements.clear() + self.selected_elements.update(selected_elements) + Actions.ELEMENT_SELECT() def what_is_selected(self, coor, coor_m=None): """ @@ -524,68 +570,55 @@ class FlowGraph(Element, _Flowgraph): """ selected_port = None selected = set() - #check the elements - hide_disabled_blocks = Actions.TOGGLE_HIDE_DISABLED_BLOCKS.get_active() - hide_variables = Actions.TOGGLE_HIDE_VARIABLES.get_active() - for element in reversed(self.get_elements()): - if hide_disabled_blocks and not element.get_enabled(): - continue # skip hidden disabled blocks and connections - if hide_variables and (element.is_variable or element.is_import): - continue # skip hidden disabled blocks and connections + # check the elements + for element in reversed(self._elements_to_draw): selected_element = element.what_is_selected(coor, coor_m) if not selected_element: continue - #update the selected port information + # update the selected port information if selected_element.is_port: - if not coor_m: selected_port = selected_element - selected_element = selected_element.get_parent() + if not coor_m: + selected_port = selected_element + selected_element = selected_element.parent_block + 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: break - #update selected ports + if not coor_m: + break + # update selected ports if selected_port is not self._new_selected_port: self._old_selected_port = self._new_selected_port self._new_selected_port = selected_port - return list(selected) + return selected - def get_selected_connections(self): + def unselect(self): """ - Get a group of selected connections. - - Returns: - sub set of connections in this flow graph + Set selected elements to an empty set. """ - selected = set() - for selected_element in self.get_selected_elements(): - if selected_element.is_connection: - selected.add(selected_element) - return list(selected) + self.selected_elements.clear() + + def select_all(self): + """Select all blocks in the flow graph""" + self.selected_elements.clear() + self.selected_elements.update(self._elements_to_draw) - def get_selected_blocks(self): + def selected_blocks(self): """ Get a group of selected blocks. Returns: sub set of blocks in this flow graph """ - selected = set() - for selected_element in self.get_selected_elements(): - if selected_element.is_block: - selected.add(selected_element) - return list(selected) + return (e for e in self.selected_elements if e.is_block) - def get_selected_block(self): + @property + def selected_block(self): """ Get the selected block when a block or port is selected. Returns: a block or None """ - selected_blocks = self.get_selected_blocks() - return selected_blocks[0] if selected_blocks else None + return next(self.selected_blocks(), None) def get_selected_elements(self): """ @@ -594,7 +627,7 @@ class FlowGraph(Element, _Flowgraph): Returns: sub set of elements in this flow graph """ - return self._selected_elements + return self.selected_elements def get_selected_element(self): """ @@ -603,60 +636,10 @@ class FlowGraph(Element, _Flowgraph): Returns: a block, port, or connection or None """ - selected_elements = self.get_selected_elements() - return selected_elements[0] if selected_elements else None - - def update_selected_elements(self): - """ - Update the selected elements. - The update behavior depends on the state of the mouse button. - When the mouse button pressed the selection will change when - the control mask is set or the new selection is not in the current group. - When the mouse button is released the selection will change when - the mouse has moved and the control mask is set or the current group is empty. - Attempt to make a new connection if the old and ports are filled. - If the control mask is set, merge with the current elements. - """ - selected_elements = None - if self.mouse_pressed: - new_selections = self.what_is_selected(self.get_coordinate()) - #update the selections if the new selection is not in the current selections - #allows us to move entire selected groups of elements - if self.get_ctrl_mask() or not ( - new_selections and new_selections[0] in self.get_selected_elements() - ): selected_elements = new_selections - if self._old_selected_port: - self._old_selected_port.force_label_unhidden(False) - self.create_shapes() - self.queue_draw() - elif self._new_selected_port: - self._new_selected_port.force_label_unhidden() - else: # called from a mouse release - if not self.element_moved and (not self.get_selected_elements() or self.get_ctrl_mask()): - selected_elements = self.what_is_selected(self.get_coordinate(), self.press_coor) - #this selection and the last were ports, try to connect them - if self._old_selected_port and self._new_selected_port: - try: - self.connect(self._old_selected_port, self._new_selected_port) - Actions.ELEMENT_CREATE() - except: Messages.send_fail_connection() - self._old_selected_port = None - self._new_selected_port = None - return - #update selected elements - if selected_elements is None: return - old_elements = set(self.get_selected_elements()) - self._selected_elements = list(set(selected_elements)) - new_elements = set(self.get_selected_elements()) - #if ctrl, set the selected elements to the union - intersection of old and new - if self.get_ctrl_mask(): - self._selected_elements = list( - set.union(old_elements, new_elements) - set.intersection(old_elements, new_elements) - ) - Actions.ELEMENT_SELECT() + return next(iter(self.selected_elements), None) ########################################################################## - ## Event Handlers + # Event Handlers ########################################################################## def handle_mouse_context_press(self, coordinate, event): """ @@ -665,12 +648,12 @@ class FlowGraph(Element, _Flowgraph): Then, show the context menu at the mouse click location. """ selections = self.what_is_selected(coordinate) - if not set(selections).intersection(self.get_selected_elements()): - self.set_coordinate(coordinate) + if not selections.intersection(self.selected_elements): + self.coordinate = coordinate self.mouse_pressed = True self.update_selected_elements() self.mouse_pressed = False - self._context_menu.popup(None, None, None, event.button, event.time) + self._context_menu.popup(None, None, None, None, event.button, event.time) def handle_mouse_selector_press(self, double_click, coordinate): """ @@ -680,13 +663,14 @@ class FlowGraph(Element, _Flowgraph): Update the selection state of the flow graph. """ self.press_coor = coordinate - self.set_coordinate(coordinate) - self.time = 0 + self.coordinate = coordinate self.mouse_pressed = True - if double_click: self.unselect() + + if double_click: + self.unselect() self.update_selected_elements() - #double click detected, bring up params dialog if possible - if double_click and self.get_selected_block(): + + if double_click and self.selected_block: self.mouse_pressed = False Actions.BLOCK_PARAM_MODIFY() @@ -696,8 +680,7 @@ class FlowGraph(Element, _Flowgraph): Update the state, handle motion (dragging). And update the selected flowgraph elements. """ - self.set_coordinate(coordinate) - self.time = 0 + self.coordinate = coordinate self.mouse_pressed = False if self.element_moved: Actions.BLOCK_MOVE() @@ -710,56 +693,62 @@ class FlowGraph(Element, _Flowgraph): Move a selected element to the new coordinate. Auto-scroll the scroll bars at the boundaries. """ - #to perform a movement, the mouse must be pressed - # (no longer checking pending events via gtk.events_pending() - always true in Windows) + # to perform a movement, the mouse must be pressed + # (no longer checking pending events via Gtk.events_pending() - always true in Windows) if not self.mouse_pressed: - # only continue if mouse-over stuff is enabled (just the auto-hide port label stuff for now) - if not Actions.TOGGLE_AUTO_HIDE_PORT_LABELS.get_active(): return - redraw = False - for element in reversed(self.get_elements()): - over_element = element.what_is_selected(coordinate) - if not over_element: continue - if over_element != self.element_under_mouse: # over sth new - if self.element_under_mouse: - redraw |= self.element_under_mouse.mouse_out() or False - self.element_under_mouse = over_element - redraw |= over_element.mouse_over() or False - break - else: + self._handle_mouse_motion_move(coordinate) + else: + self._handle_mouse_motion_drag(coordinate) + + def _handle_mouse_motion_move(self, coordinate): + # only continue if mouse-over stuff is enabled (just the auto-hide port label stuff for now) + if not Actions.TOGGLE_AUTO_HIDE_PORT_LABELS.get_active(): + return + redraw = False + for element in self._elements_to_draw: + over_element = element.what_is_selected(coordinate) + if not over_element: + continue + if over_element != self.element_under_mouse: # over sth new if self.element_under_mouse: redraw |= self.element_under_mouse.mouse_out() or False - self.element_under_mouse = None - if redraw: - #self.create_labels() - self.create_shapes() - self.queue_draw() + self.element_under_mouse = over_element + redraw |= over_element.mouse_over() or False + break else: - #perform auto-scrolling - width, height = self.get_size() - x, y = coordinate - h_adj = self.get_scroll_pane().get_hadjustment() - v_adj = self.get_scroll_pane().get_vadjustment() - for pos, length, adj, adj_val, adj_len in ( - (x, width, h_adj, h_adj.get_value(), h_adj.page_size), - (y, height, v_adj, v_adj.get_value(), v_adj.page_size), - ): - #scroll if we moved near the border - if pos-adj_val > adj_len-SCROLL_PROXIMITY_SENSITIVITY and adj_val+SCROLL_DISTANCE < length-adj_len: - adj.set_value(adj_val+SCROLL_DISTANCE) - adj.emit('changed') - elif pos-adj_val < SCROLL_PROXIMITY_SENSITIVITY: - adj.set_value(adj_val-SCROLL_DISTANCE) - adj.emit('changed') - #remove the connection if selected in drag event - if len(self.get_selected_elements()) == 1 and self.get_selected_element().is_connection: - Actions.ELEMENT_DELETE() - #move the selected elements and record the new coordinate - if not self.get_ctrl_mask(): - X, Y = self.get_coordinate() - dX, dY = int(x - X), int(y - Y) - active = Actions.TOGGLE_SNAP_TO_GRID.get_active() or self.get_mod1_mask() - if not active or abs(dX) >= Utils.CANVAS_GRID_SIZE or abs(dY) >= Utils.CANVAS_GRID_SIZE: - self.move_selected((dX, dY)) - self.set_coordinate((x, y)) - #queue draw for animation - self.queue_draw() + if self.element_under_mouse: + redraw |= self.element_under_mouse.mouse_out() or False + self.element_under_mouse = None + if redraw: + # self.create_labels() + self.create_shapes() + self.drawing_area.queue_draw() + + def _handle_mouse_motion_drag(self, coordinate): + # remove the connection if selected in drag event + if len(self.selected_elements) == 1 and self.get_selected_element().is_connection: + Actions.ELEMENT_DELETE() + + # move the selected elements and record the new coordinate + x, y = coordinate + if not self.drawing_area.ctrl_mask: + X, Y = self.coordinate + dX, dY = int(x - X), int(y - Y) + active = Actions.TOGGLE_SNAP_TO_GRID.get_active() or self.drawing_area.mod1_mask + if not active or abs(dX) >= Constants.CANVAS_GRID_SIZE or abs(dY) >= Constants.CANVAS_GRID_SIZE: + self.move_selected((dX, dY)) + self.coordinate = (x, y) + # queue draw for animation + self.drawing_area.queue_draw() + + def get_max_coords(self, initial=(0, 0)): + return tuple(max(i, e) for i, e in zip(initial, self.extend[2:])) + + @property + def extend(self): + extend = 100000, 100000, 0, 0 + for element in self._elements_to_draw: + extend = (min_or_max(xy, e_xy) for min_or_max, xy, e_xy in zip( + (min, min, max, max), extend, element.extend + )) + return tuple(extend) diff --git a/grc/gui/MainWindow.py b/grc/gui/MainWindow.py index 686e73cfa3..3d4bf10308 100644 --- a/grc/gui/MainWindow.py +++ b/grc/gui/MainWindow.py @@ -17,52 +17,31 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import + import os -import gtk +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject + from . import Bars, Actions, Preferences, Utils from .BlockTreeWindow import BlockTreeWindow from .VariableEditor import VariableEditor from .Constants import \ NEW_FLOGRAPH_TITLE, DEFAULT_CONSOLE_WINDOW_WIDTH -from .Dialogs import TextDisplay, MessageDialogHelper +from .Dialogs import TextDisplay, MessageDialogWrapper from .NotebookPage import NotebookPage from ..core import Messages -MAIN_WINDOW_TITLE_TMPL = """\ -#if not $saved -*#slurp -#end if -#if $basename -$basename#slurp -#else -$new_flowgraph_title#slurp -#end if -#if $read_only - (read only)#slurp -#end if -#if $dirname - - $dirname#slurp -#end if - - $platform_name#slurp -""" - -PAGE_TITLE_MARKUP_TMPL = """\ -#set $foreground = $saved and 'black' or 'red' -<span foreground="$foreground">$encode($title or $new_flowgraph_title)</span>#slurp -#if $read_only - (ro)#slurp -#end if -""" - - ############################################################ # Main window ############################################################ - -class MainWindow(gtk.Window): +class MainWindow(Gtk.Window): """The topmost window with menus, the tool bar, and other major windows.""" # Constants the action handler can use to indicate which panel visibility to change. @@ -76,55 +55,51 @@ class MainWindow(gtk.Window): Setup the menu, toolbar, flow graph editor notebook, block selection window... """ self._platform = platform - - gen_opts = platform.blocks['options'].get_param('generate_options') - generate_mode_default = gen_opts.get_value() - generate_modes = [ - (o.get_key(), o.get_name(), o.get_key() == generate_mode_default) - for o in gen_opts.get_options()] - - # Load preferences Preferences.load(platform) # Setup window - gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL) - vbox = gtk.VBox() + GObject.GObject.__init__(self) + vbox = Gtk.VBox() self.add(vbox) # Create the menu bar and toolbar + generate_modes = platform.get_generate_options() self.add_accel_group(Actions.get_accel_group()) self.menu_bar = Bars.MenuBar(generate_modes, action_handler_callback) - vbox.pack_start(self.menu_bar, False) + vbox.pack_start(self.menu_bar, False, False, 0) self.tool_bar = Bars.Toolbar(generate_modes, action_handler_callback) - vbox.pack_start(self.tool_bar, False) + vbox.pack_start(self.tool_bar, False, False, 0) # Main parent container for the different panels - self.container = gtk.HPaned() - vbox.pack_start(self.container) + self.main = Gtk.HPaned() #(orientation=Gtk.Orientation.HORIZONTAL) + vbox.pack_start(self.main, True, True, 0) # Create the notebook - self.notebook = gtk.Notebook() + self.notebook = Gtk.Notebook() self.page_to_be_closed = None - self.current_page = None + self.current_page = None # type: NotebookPage self.notebook.set_show_border(False) self.notebook.set_scrollable(True) # scroll arrows for page tabs self.notebook.connect('switch-page', self._handle_page_change) # Create the console window self.text_display = TextDisplay() - self.console_window = gtk.ScrolledWindow() - self.console_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) + self.console_window = Gtk.ScrolledWindow() + self.console_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) self.console_window.add(self.text_display) self.console_window.set_size_request(-1, DEFAULT_CONSOLE_WINDOW_WIDTH) # Create the block tree and variable panels - self.btwin = BlockTreeWindow(platform, self.get_flow_graph) - self.vars = VariableEditor(platform, self.get_flow_graph) + self.btwin = BlockTreeWindow(platform) + self.btwin.connect('create_new_block', self._add_block_to_current_flow_graph) + self.vars = VariableEditor() + self.vars.connect('create_new_block', self._add_block_to_current_flow_graph) + self.vars.connect('remove_block', self._remove_block_from_current_flow_graph) # Figure out which place to put the variable editor - self.left = gtk.VPaned() - self.right = gtk.VPaned() - self.left_subpanel = gtk.HPaned() + self.left = Gtk.VPaned() #orientation=Gtk.Orientation.VERTICAL) + self.right = Gtk.VPaned() #orientation=Gtk.Orientation.VERTICAL) + self.left_subpanel = Gtk.HPaned() #orientation=Gtk.Orientation.HORIZONTAL) self.variable_panel_sidebar = Preferences.variable_editor_sidebar() if self.variable_panel_sidebar: @@ -142,12 +117,12 @@ class MainWindow(gtk.Window): # Create the right panel self.right.pack1(self.btwin) - self.container.pack1(self.left) - self.container.pack2(self.right, False) + self.main.pack1(self.left) + self.main.pack2(self.right, False) - # load preferences and show the main window + # Load preferences and show the main window self.resize(*Preferences.main_window_size()) - self.container.set_position(Preferences.blocks_window_position()) + self.main.set_position(Preferences.blocks_window_position()) self.left.set_position(Preferences.console_window_position()) if self.variable_panel_sidebar: self.right.set_position(Preferences.variable_editor_position(sidebar=True)) @@ -163,6 +138,13 @@ class MainWindow(gtk.Window): # Event Handlers ############################################################ + def _add_block_to_current_flow_graph(self, widget, key): + self.current_flow_graph.add_new_block(key) + + def _remove_block_from_current_flow_graph(self, widget, key): + block = self.current_flow_graph.get_block(key) + self.current_flow_graph.remove_element(block) + def _quit(self, window, event): """ Handle the delete event from the main window. @@ -272,17 +254,15 @@ class MainWindow(gtk.Window): file_path=file_path, ) if file_path: Messages.send_end_load() - except Exception, e: #return on failure + except Exception as e: #return on failure Messages.send_fail_load(e) if isinstance(e, KeyError) and str(e) == "'options'": # This error is unrecoverable, so crash gracefully exit(-1) return #add this page to the notebook - self.notebook.append_page(page, page.get_tab()) - try: self.notebook.set_tab_reorderable(page, True) - except: pass #gtk too old - self.notebook.set_tab_label_packing(page, False, False, gtk.PACK_START) + self.notebook.append_page(page, page.tab) + self.notebook.set_tab_reorderable(page, True) #only show if blank or manual if not file_path or show: self._set_page(page) @@ -293,10 +273,10 @@ class MainWindow(gtk.Window): Returns: true if all closed """ - open_files = filter(lambda file: file, self._get_files()) #filter blank files - open_file = self.get_page().get_file_path() + open_files = [file for file in self._get_files() if file] #filter blank files + open_file = self.current_page.file_path #close each page - for page in sorted(self.get_pages(), key=lambda p: p.get_saved()): + for page in sorted(self.get_pages(), key=lambda p: p.saved): self.page_to_be_closed = page closed = self.close_page(False) if not closed: @@ -307,7 +287,7 @@ class MainWindow(gtk.Window): Preferences.file_open(open_file) Preferences.main_window_size(self.get_size()) Preferences.console_window_position(self.left.get_position()) - Preferences.blocks_window_position(self.container.get_position()) + Preferences.blocks_window_position(self.main.get_position()) if self.variable_panel_sidebar: Preferences.variable_editor_position(self.right.get_position(), sidebar=True) else: @@ -324,23 +304,24 @@ class MainWindow(gtk.Window): Args: ensure: boolean """ - if not self.page_to_be_closed: self.page_to_be_closed = self.get_page() + if not self.page_to_be_closed: self.page_to_be_closed = self.current_page #show the page if it has an executing flow graph or is unsaved - if self.page_to_be_closed.get_proc() or not self.page_to_be_closed.get_saved(): + if self.page_to_be_closed.process or not self.page_to_be_closed.saved: self._set_page(self.page_to_be_closed) #unsaved? ask the user - if not self.page_to_be_closed.get_saved(): + if not self.page_to_be_closed.saved: response = self._save_changes() # return value is either OK, CLOSE, or CANCEL - if response == gtk.RESPONSE_OK: + if response == Gtk.ResponseType.OK: Actions.FLOW_GRAPH_SAVE() #try to save - if not self.page_to_be_closed.get_saved(): #still unsaved? + if not self.page_to_be_closed.saved: #still unsaved? self.page_to_be_closed = None #set the page to be closed back to None return False - elif response == gtk.RESPONSE_CANCEL: + elif response == Gtk.ResponseType.CANCEL: self.page_to_be_closed = None return False #stop the flow graph if executing - if self.page_to_be_closed.get_proc(): Actions.FLOW_GRAPH_KILL() + if self.page_to_be_closed.process: + Actions.FLOW_GRAPH_KILL() #remove the page self.notebook.remove_page(self.notebook.page_num(self.page_to_be_closed)) if ensure and self.notebook.get_n_pages() == 0: self.new_page() #no pages, make a new one @@ -356,69 +337,49 @@ class MainWindow(gtk.Window): Set the title of the main window. Set the titles on the page tabs. Show/hide the console window. - - Args: - title: the window title """ - gtk.Window.set_title(self, Utils.parse_template(MAIN_WINDOW_TITLE_TMPL, - basename=os.path.basename(self.get_page().get_file_path()), - dirname=os.path.dirname(self.get_page().get_file_path()), - new_flowgraph_title=NEW_FLOGRAPH_TITLE, - read_only=self.get_page().get_read_only(), - saved=self.get_page().get_saved(), - platform_name=self._platform.config.name, - ) - ) - #set tab titles - for page in self.get_pages(): page.set_markup( - Utils.parse_template(PAGE_TITLE_MARKUP_TMPL, - #get filename and strip out file extension - title=os.path.splitext(os.path.basename(page.get_file_path()))[0], - read_only=page.get_read_only(), saved=page.get_saved(), - new_flowgraph_title=NEW_FLOGRAPH_TITLE, - ) - ) - #show/hide notebook tabs + page = self.current_page + + basename = os.path.basename(page.file_path) + dirname = os.path.dirname(page.file_path) + Gtk.Window.set_title(self, ''.join(( + '*' if not page.saved else '', basename if basename else NEW_FLOGRAPH_TITLE, + '(read only)' if page.get_read_only() else '', ' - ', + dirname if dirname else self._platform.config.name, + ))) + # set tab titles + for page in self.get_pages(): + file_name = os.path.splitext(os.path.basename(page.file_path))[0] + page.set_markup('<span foreground="{foreground}">{title}{ro}</span>'.format( + foreground='black' if page.saved else 'red', ro=' (ro)' if page.get_read_only() else '', + title=Utils.encode(file_name or NEW_FLOGRAPH_TITLE), + )) + # show/hide notebook tabs self.notebook.set_show_tabs(len(self.get_pages()) > 1) - # Need to update the variable window when changing - self.vars.update_gui() + # Need to update the variable window when changing + self.vars.update_gui(self.current_flow_graph.blocks) def update_pages(self): """ Forces a reload of all the pages in this notebook. """ for page in self.get_pages(): - success = page.get_flow_graph().reload() + success = page.flow_graph.reload() if success: # Only set saved if errors occurred during import - page.set_saved(False) + page.saved = False - def get_page(self): - """ - Get the selected page. - - Returns: - the selected page - """ - return self.current_page - - def get_flow_graph(self): - """ - Get the selected flow graph. - - Returns: - the selected flow graph - """ - return self.get_page().get_flow_graph() + @property + def current_flow_graph(self): + return self.current_page.flow_graph def get_focus_flag(self): """ Get the focus flag from the current page. - Returns: the focus flag """ - return self.get_page().get_drawing_area().get_focus_flag() + return self.current_page.drawing_area.get_focus_flag() ############################################################ # Helpers @@ -442,14 +403,14 @@ class MainWindow(gtk.Window): the response_id (see buttons variable below) """ buttons = ( - 'Close without saving', gtk.RESPONSE_CLOSE, - gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, - gtk.STOCK_SAVE, gtk.RESPONSE_OK - ) - return MessageDialogHelper( - gtk.MESSAGE_QUESTION, gtk.BUTTONS_NONE, 'Unsaved Changes!', - 'Would you like to save changes before closing?', gtk.RESPONSE_OK, buttons + 'Close without saving', Gtk.ResponseType.CLOSE, + Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_SAVE, Gtk.ResponseType.OK ) + return MessageDialogWrapper( + self, Gtk.MessageType.QUESTION, Gtk.ButtonsType.NONE, 'Unsaved Changes!', + 'Would you like to save changes before closing?', Gtk.ResponseType.OK, buttons + ).run_and_destroy() def _get_files(self): """ @@ -458,7 +419,7 @@ class MainWindow(gtk.Window): Returns: list of file paths """ - return map(lambda page: page.get_file_path(), self.get_pages()) + return [page.file_path for page in self.get_pages()] def get_pages(self): """ @@ -467,4 +428,5 @@ class MainWindow(gtk.Window): Returns: list of pages """ - return [self.notebook.get_nth_page(page_num) for page_num in range(self.notebook.get_n_pages())] + return [self.notebook.get_nth_page(page_num) + for page_num in range(self.notebook.get_n_pages())] diff --git a/grc/gui/NotebookPage.py b/grc/gui/NotebookPage.py index c9e8d0f186..347be8eea0 100644 --- a/grc/gui/NotebookPage.py +++ b/grc/gui/NotebookPage.py @@ -17,17 +17,18 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk -import Actions -from StateCache import StateCache -from Constants import MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT -from DrawingArea import DrawingArea +from __future__ import absolute_import import os +from gi.repository import Gtk, Gdk, GObject -class NotebookPage(gtk.HBox): +from . import Actions +from .StateCache import StateCache +from .Constants import MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT +from .DrawingArea import DrawingArea + + +class NotebookPage(Gtk.HBox): """A page in the notebook.""" def __init__(self, main_window, flow_graph, file_path=''): @@ -38,59 +39,53 @@ class NotebookPage(gtk.HBox): main_window: main window file_path: path to a flow graph file """ - self._flow_graph = flow_graph - self.process = None - #import the file + Gtk.HBox.__init__(self) + self.main_window = main_window + self.flow_graph = flow_graph self.file_path = file_path - initial_state = flow_graph.get_parent().parse_flow_graph(file_path) - self.state_cache = StateCache(initial_state) + + self.process = None self.saved = True - #import the data to the flow graph - self.get_flow_graph().import_data(initial_state) - #initialize page gui - gtk.HBox.__init__(self, False, 0) - self.show() - #tab box to hold label and close button - self.tab = gtk.HBox(False, 0) - #setup tab label - self.label = gtk.Label() - self.tab.pack_start(self.label, False) - #setup button image - image = gtk.Image() - image.set_from_stock('gtk-close', gtk.ICON_SIZE_MENU) - #setup image box - image_box = gtk.HBox(False, 0) + + # import the file + initial_state = flow_graph.parent_platform.parse_flow_graph(file_path) + flow_graph.import_data(initial_state) + self.state_cache = StateCache(initial_state) + + # tab box to hold label and close button + self.label = Gtk.Label() + image = Gtk.Image.new_from_icon_name('window-close', Gtk.IconSize.MENU) + image_box = Gtk.HBox(homogeneous=False, spacing=0) image_box.pack_start(image, True, False, 0) - #setup the button - button = gtk.Button() + button = Gtk.Button() button.connect("clicked", self._handle_button) - button.set_relief(gtk.RELIEF_NONE) + button.set_relief(Gtk.ReliefStyle.NONE) button.add(image_box) - #button size - w, h = gtk.icon_size_lookup_for_settings(button.get_settings(), gtk.ICON_SIZE_MENU) - button.set_size_request(w+6, h+6) - self.tab.pack_start(button, False) - self.tab.show_all() - #setup scroll window and drawing area - self.scrolled_window = gtk.ScrolledWindow() + + tab = self.tab = Gtk.HBox(homogeneous=False, spacing=0) + tab.pack_start(self.label, False, False, 0) + tab.pack_start(button, False, False, 0) + tab.show_all() + + # setup scroll window and drawing area + self.drawing_area = DrawingArea(flow_graph) + flow_graph.drawing_area = self.drawing_area + + self.scrolled_window = Gtk.ScrolledWindow() self.scrolled_window.set_size_request(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT) - self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) + self.scrolled_window.set_policy(Gtk.PolicyType.ALWAYS, Gtk.PolicyType.ALWAYS) self.scrolled_window.connect('key-press-event', self._handle_scroll_window_key_press) - self.drawing_area = DrawingArea(self.get_flow_graph()) - self.scrolled_window.add_with_viewport(self.get_drawing_area()) - self.pack_start(self.scrolled_window) - #inject drawing area into flow graph - self.get_flow_graph().drawing_area = self.get_drawing_area() - self.show_all() + self.scrolled_window.add(self.drawing_area) + self.pack_start(self.scrolled_window, True, True, 0) - def get_drawing_area(self): return self.drawing_area + self.show_all() def _handle_scroll_window_key_press(self, widget, event): """forward Ctrl-PgUp/Down to NotebookPage (switch fg instead of horiz. scroll""" is_ctrl_pg = ( - event.state & gtk.gdk.CONTROL_MASK and - event.keyval in (gtk.keysyms.Page_Up, gtk.keysyms.Page_Down) + event.state & Gdk.ModifierType.CONTROL_MASK and + event.keyval in (Gdk.KEY_Page_Up, Gdk.KEY_Page_Down) ) if is_ctrl_pg: return self.get_parent().event(event) @@ -102,8 +97,8 @@ class NotebookPage(gtk.HBox): Returns: generator """ - platform = self.get_flow_graph().get_parent() - return platform.Generator(self.get_flow_graph(), self.get_file_path()) + platform = self.flow_graph.parent_platform + return platform.Generator(self.flow_graph, self.file_path) def _handle_button(self, button): """ @@ -125,42 +120,6 @@ class NotebookPage(gtk.HBox): """ self.label.set_markup(markup) - def get_tab(self): - """ - Get the gtk widget for this page's tab. - - Returns: - gtk widget - """ - return self.tab - - def get_proc(self): - """ - Get the subprocess for the flow graph. - - Returns: - the subprocess object - """ - return self.process - - def set_proc(self, process): - """ - Set the subprocess object. - - Args: - process: the new subprocess - """ - self.process = process - - def get_flow_graph(self): - """ - Get the flow graph. - - Returns: - the flow graph - """ - return self._flow_graph - def get_read_only(self): """ Get the read-only state of the file. @@ -169,51 +128,7 @@ class NotebookPage(gtk.HBox): Returns: true for read-only """ - if not self.get_file_path(): return False - return os.path.exists(self.get_file_path()) and \ - not os.access(self.get_file_path(), os.W_OK) - - def get_file_path(self): - """ - Get the file path for the flow graph. - - Returns: - the file path or '' - """ - return self.file_path - - def set_file_path(self, file_path=''): - """ - Set the file path, '' for no file path. - - Args: - file_path: file path string - """ - self.file_path = os.path.abspath(file_path) if file_path else '' - - def get_saved(self): - """ - Get the saved status for the flow graph. - - Returns: - true if saved - """ - return self.saved - - def set_saved(self, saved=True): - """ - Set the saved status. - - Args: - saved: boolean status - """ - self.saved = saved - - def get_state_cache(self): - """ - Get the state cache for the flow graph. - - Returns: - the state cache - """ - return self.state_cache + if not self.file_path: + return False + return (os.path.exists(self.file_path) and + not os.access(self.file_path, os.W_OK)) diff --git a/grc/gui/Param.py b/grc/gui/Param.py index 4b5a3c294a..ed5257ae69 100644 --- a/grc/gui/Param.py +++ b/grc/gui/Param.py @@ -1,404 +1,33 @@ -""" -Copyright 2007-2011 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" - -import os - -import pygtk -pygtk.require('2.0') -import gtk - -from . import Colors, Utils, Constants -from .Element import Element - -from ..core.Param import Param as _Param - - -class InputParam(gtk.HBox): - """The base class for an input parameter inside the input parameters dialog.""" - expand = False - - def __init__(self, param, changed_callback=None, editing_callback=None): - gtk.HBox.__init__(self) - self.param = param - self._changed_callback = changed_callback - self._editing_callback = editing_callback - self.label = gtk.Label() #no label, markup is added by set_markup - self.label.set_size_request(150, -1) - self.pack_start(self.label, False) - self.set_markup = lambda m: self.label.set_markup(m) - self.tp = None - self._have_pending_changes = False - #connect events - self.connect('show', self._update_gui) - - def set_color(self, color): - pass - - def set_tooltip_text(self, text): - pass - - def get_text(self): - raise NotImplementedError() - - def _update_gui(self, *args): - """ - Set the markup, color, tooltip, show/hide. - """ - #set the markup - has_cb = \ - hasattr(self.param.get_parent(), 'get_callbacks') and \ - filter(lambda c: self.param.get_key() in c, self.param.get_parent()._callbacks) - self.set_markup(Utils.parse_template(PARAM_LABEL_MARKUP_TMPL, - param=self.param, has_cb=has_cb, - modified=self._have_pending_changes)) - #set the color - self.set_color(self.param.get_color()) - #set the tooltip - self.set_tooltip_text( - Utils.parse_template(TIP_MARKUP_TMPL, param=self.param).strip(), - ) - #show/hide - if self.param.get_hide() == 'all': self.hide_all() - else: self.show_all() - - def _mark_changed(self, *args): - """ - Mark this param as modified on change, but validate only on focus-lost - """ - self._have_pending_changes = True - self._update_gui() - if self._editing_callback: - self._editing_callback(self, None) - - def _apply_change(self, *args): - """ - Handle a gui change by setting the new param value, - calling the callback (if applicable), and updating. - """ - #set the new value - self.param.set_value(self.get_text()) - #call the callback - if self._changed_callback: - self._changed_callback(self, None) - else: - self.param.validate() - #gui update - self._have_pending_changes = False - self._update_gui() - - def _handle_key_press(self, widget, event): - if event.keyval == gtk.keysyms.Return and event.state & gtk.gdk.CONTROL_MASK: - self._apply_change(widget, event) - return True - return False - - def apply_pending_changes(self): - if self._have_pending_changes: - self._apply_change() - - -class EntryParam(InputParam): - """Provide an entry box for strings and numbers.""" - - def __init__(self, *args, **kwargs): - InputParam.__init__(self, *args, **kwargs) - self._input = gtk.Entry() - self._input.set_text(self.param.get_value()) - self._input.connect('changed', self._mark_changed) - self._input.connect('focus-out-event', self._apply_change) - self._input.connect('key-press-event', self._handle_key_press) - self.pack_start(self._input, True) - - def get_text(self): - return self._input.get_text() - - def set_color(self, color): - need_status_color = self.label not in self.get_children() - text_color = ( - Colors.PARAM_ENTRY_TEXT_COLOR if not need_status_color else - gtk.gdk.color_parse('blue') if self._have_pending_changes else - gtk.gdk.color_parse('red') if not self.param.is_valid() else - Colors.PARAM_ENTRY_TEXT_COLOR) - base_color = ( - Colors.BLOCK_DISABLED_COLOR - if need_status_color and not self.param.get_parent().get_enabled() - else gtk.gdk.color_parse(color) - ) - self._input.modify_base(gtk.STATE_NORMAL, base_color) - self._input.modify_text(gtk.STATE_NORMAL, text_color) - - def set_tooltip_text(self, text): - try: - self._input.set_tooltip_text(text) - except AttributeError: - pass # no tooltips for old GTK - - -class MultiLineEntryParam(InputParam): - """Provide an multi-line box for strings.""" - expand = True - - def __init__(self, *args, **kwargs): - InputParam.__init__(self, *args, **kwargs) - self._buffer = gtk.TextBuffer() - self._buffer.set_text(self.param.get_value()) - self._buffer.connect('changed', self._mark_changed) - - self._view = gtk.TextView(self._buffer) - self._view.connect('focus-out-event', self._apply_change) - self._view.connect('key-press-event', self._handle_key_press) - - self._sw = gtk.ScrolledWindow() - self._sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - self._sw.add_with_viewport(self._view) - - self.pack_start(self._sw, True) - - def get_text(self): - buf = self._buffer - return buf.get_text(buf.get_start_iter(), - buf.get_end_iter()).strip() - - def set_color(self, color): - self._view.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color)) - self._view.modify_text(gtk.STATE_NORMAL, Colors.PARAM_ENTRY_TEXT_COLOR) - - def set_tooltip_text(self, text): - try: - self._view.set_tooltip_text(text) - except AttributeError: - pass # no tooltips for old GTK - - -# try: -# import gtksourceview -# lang_manager = gtksourceview.SourceLanguagesManager() -# py_lang = lang_manager.get_language_from_mime_type('text/x-python') -# -# class PythonEditorParam(InputParam): -# expand = True -# -# def __init__(self, *args, **kwargs): -# InputParam.__init__(self, *args, **kwargs) -# -# buf = self._buffer = gtksourceview.SourceBuffer() -# buf.set_language(py_lang) -# buf.set_highlight(True) -# buf.set_text(self.param.get_value()) -# buf.connect('changed', self._mark_changed) -# -# view = self._view = gtksourceview.SourceView(self._buffer) -# view.connect('focus-out-event', self._apply_change) -# view.connect('key-press-event', self._handle_key_press) -# view.set_tabs_width(4) -# view.set_insert_spaces_instead_of_tabs(True) -# view.set_auto_indent(True) -# view.set_border_width(2) +# Copyright 2007-2016 Free Software Foundation, Inc. +# This file is part of GNU Radio # -# scroll = gtk.ScrolledWindow() -# scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) -# scroll.add_with_viewport(view) -# self.pack_start(scroll, True) +# GNU Radio Companion is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. # -# def get_text(self): -# buf = self._buffer -# return buf.get_text(buf.get_start_iter(), -# buf.get_end_iter()).strip() +# GNU Radio Companion is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# except ImportError: -# print "Package 'gtksourceview' not found. No Syntax highlighting." -# PythonEditorParam = MultiLineEntryParam - -class PythonEditorParam(InputParam): - - def __init__(self, *args, **kwargs): - InputParam.__init__(self, *args, **kwargs) - button = self._button = gtk.Button('Open in Editor') - button.connect('clicked', self.open_editor) - self.pack_start(button, True) - - def open_editor(self, widget=None): - flowgraph = self.param.get_parent().get_parent() - flowgraph.install_external_editor(self.param) - - def get_text(self): - pass # we never update the value from here - - def set_color(self, color): - # self._button.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color)) - self._button.modify_text(gtk.STATE_NORMAL, Colors.PARAM_ENTRY_TEXT_COLOR) - - def _apply_change(self, *args): - pass - - -class EnumParam(InputParam): - """Provide an entry box for Enum types with a drop down menu.""" - - def __init__(self, *args, **kwargs): - InputParam.__init__(self, *args, **kwargs) - self._input = gtk.combo_box_new_text() - for option in self.param.get_options(): self._input.append_text(option.get_name()) - self._input.set_active(self.param.get_option_keys().index(self.param.get_value())) - self._input.connect('changed', self._editing_callback) - self._input.connect('changed', self._apply_change) - self.pack_start(self._input, False) - - def get_text(self): - return self.param.get_option_keys()[self._input.get_active()] - - def set_tooltip_text(self, text): - try: - self._input.set_tooltip_text(text) - except AttributeError: - pass # no tooltips for old GTK - - -class EnumEntryParam(InputParam): - """Provide an entry box and drop down menu for Raw Enum types.""" - - def __init__(self, *args, **kwargs): - InputParam.__init__(self, *args, **kwargs) - self._input = gtk.combo_box_entry_new_text() - for option in self.param.get_options(): self._input.append_text(option.get_name()) - try: self._input.set_active(self.param.get_option_keys().index(self.param.get_value())) - except: - self._input.set_active(-1) - self._input.get_child().set_text(self.param.get_value()) - self._input.connect('changed', self._apply_change) - self._input.get_child().connect('changed', self._mark_changed) - self._input.get_child().connect('focus-out-event', self._apply_change) - self._input.get_child().connect('key-press-event', self._handle_key_press) - self.pack_start(self._input, False) - - def get_text(self): - if self._input.get_active() == -1: return self._input.get_child().get_text() - return self.param.get_option_keys()[self._input.get_active()] - - def set_tooltip_text(self, text): - try: - if self._input.get_active() == -1: #custom entry - self._input.get_child().set_tooltip_text(text) - else: - self._input.set_tooltip_text(text) - except AttributeError: - pass # no tooltips for old GTK - - def set_color(self, color): - if self._input.get_active() == -1: #custom entry, use color - self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color)) - self._input.get_child().modify_text(gtk.STATE_NORMAL, Colors.PARAM_ENTRY_TEXT_COLOR) - else: #from enum, make pale background - self._input.get_child().modify_base(gtk.STATE_NORMAL, Colors.ENTRYENUM_CUSTOM_COLOR) - self._input.get_child().modify_text(gtk.STATE_NORMAL, Colors.PARAM_ENTRY_TEXT_COLOR) - - -class FileParam(EntryParam): - """Provide an entry box for filename and a button to browse for a file.""" - - def __init__(self, *args, **kwargs): - EntryParam.__init__(self, *args, **kwargs) - input = gtk.Button('...') - input.connect('clicked', self._handle_clicked) - self.pack_start(input, False) - - def _handle_clicked(self, widget=None): - """ - If the button was clicked, open a file dialog in open/save format. - Replace the text in the entry with the new filename from the file dialog. - """ - #get the paths - file_path = self.param.is_valid() and self.param.get_evaluated() or '' - (dirname, basename) = os.path.isfile(file_path) and os.path.split(file_path) or (file_path, '') - # check for qss theme default directory - if self.param.get_key() == 'qt_qss_theme': - dirname = os.path.dirname(dirname) # trim filename - if not os.path.exists(dirname): - platform = self.param.get_parent().get_parent().get_parent() - dirname = os.path.join(platform.config.install_prefix, - '/share/gnuradio/themes') - if not os.path.exists(dirname): - dirname = os.getcwd() # fix bad paths - - #build the dialog - if self.param.get_type() == 'file_open': - file_dialog = gtk.FileChooserDialog('Open a Data File...', None, - gtk.FILE_CHOOSER_ACTION_OPEN, ('gtk-cancel',gtk.RESPONSE_CANCEL,'gtk-open',gtk.RESPONSE_OK)) - elif self.param.get_type() == 'file_save': - file_dialog = gtk.FileChooserDialog('Save a Data File...', None, - gtk.FILE_CHOOSER_ACTION_SAVE, ('gtk-cancel',gtk.RESPONSE_CANCEL, 'gtk-save',gtk.RESPONSE_OK)) - file_dialog.set_do_overwrite_confirmation(True) - file_dialog.set_current_name(basename) #show the current filename - else: - raise ValueError("Can't open file chooser dialog for type " + repr(self.param.get_type())) - file_dialog.set_current_folder(dirname) #current directory - file_dialog.set_select_multiple(False) - file_dialog.set_local_only(True) - if gtk.RESPONSE_OK == file_dialog.run(): #run the dialog - file_path = file_dialog.get_filename() #get the file path - self._input.set_text(file_path) - self._editing_callback() - self._apply_change() - file_dialog.destroy() #destroy the dialog - +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -PARAM_MARKUP_TMPL="""\ -#set $foreground = $param.is_valid() and 'black' or 'red' -<span foreground="$foreground" font_desc="$font"><b>$encode($param.get_name()): </b>$encode(repr($param).replace('\\n',' '))</span>""" +from __future__ import absolute_import +from . import Utils, Constants -PARAM_LABEL_MARKUP_TMPL="""\ -#set $foreground = $modified and 'blue' or $param.is_valid() and 'black' or 'red' -#set $underline = $has_cb and 'low' or 'none' -<span underline="$underline" foreground="$foreground" font_desc="Sans 9">$encode($param.get_name())</span>""" +from . import ParamWidgets +from .Element import Element -TIP_MARKUP_TMPL="""\ -######################################## -#def truncate(string) - #set $max_len = 100 - #set $string = str($string) - #if len($string) > $max_len -$('%s...%s'%($string[:$max_len/2], $string[-$max_len/2:]))#slurp - #else -$string#slurp - #end if -#end def -######################################## -Key: $param.get_key() -Type: $param.get_type() -#if $param.is_valid() -Value: $truncate($param.get_evaluated()) -#elif len($param.get_error_messages()) == 1 -Error: $(param.get_error_messages()[0]) -#else -Error: - #for $error_msg in $param.get_error_messages() - * $error_msg - #end for -#end if""" +from ..core.Param import Param as _Param -class Param(Element, _Param): +class Param(_Param): """The graphical parameter.""" - def __init__(self, **kwargs): - Element.__init__(self) - _Param.__init__(self, **kwargs) + make_cls_with_base = classmethod(Element.make_cls_with_base.__func__) def get_input(self, *args, **kwargs): """ @@ -410,32 +39,124 @@ class Param(Element, _Param): Returns: gtk input class """ - if self.get_type() in ('file_open', 'file_save'): - input_widget = FileParam(self, *args, **kwargs) + type_ = self.get_type() + if type_ in ('file_open', 'file_save'): + input_widget_cls = ParamWidgets.FileParam elif self.is_enum(): - input_widget = EnumParam(self, *args, **kwargs) + input_widget_cls = ParamWidgets.EnumParam - elif self.get_options(): - input_widget = EnumEntryParam(self, *args, **kwargs) + elif self.options: + input_widget_cls = ParamWidgets.EnumEntryParam - elif self.get_type() == '_multiline': - input_widget = MultiLineEntryParam(self, *args, **kwargs) + elif type_ == '_multiline': + input_widget_cls = ParamWidgets.MultiLineEntryParam - elif self.get_type() == '_multiline_python_external': - input_widget = PythonEditorParam(self, *args, **kwargs) + elif type_ == '_multiline_python_external': + input_widget_cls = ParamWidgets.PythonEditorParam else: - input_widget = EntryParam(self, *args, **kwargs) + input_widget_cls = ParamWidgets.EntryParam + + return input_widget_cls(self, *args, **kwargs) + + def format_label_markup(self, have_pending_changes=False): + block = self.parent + # fixme: using non-public attribute here + has_callback = \ + hasattr(block, 'get_callbacks') and \ + any(self.key in callback for callback in block._callbacks) + + return '<span {underline} {foreground} font_desc="Sans 9">{label}</span>'.format( + underline='underline="low"' if has_callback else '', + foreground='foreground="blue"' if have_pending_changes else + 'foreground="red"' if not self.is_valid() else '', + label=Utils.encode(self.name) + ) - return input_widget + def format_tooltip_text(self): + errors = self.get_error_messages() + tooltip_lines = ['Key: ' + self.key, 'Type: ' + self.get_type()] + if self.is_valid(): + value = str(self.get_evaluated()) + if len(value) > 100: + value = '{}...{}'.format(value[:50], value[-50:]) + tooltip_lines.append('Value: ' + value) + elif len(errors) == 1: + tooltip_lines.append('Error: ' + errors[0]) + elif len(errors) > 1: + tooltip_lines.append('Error:') + tooltip_lines.extend(' * ' + msg for msg in errors) + return '\n'.join(tooltip_lines) + + def pretty_print(self): + """ + Get the repr (nice string format) for this param. - def get_markup(self): + Returns: + the string representation + """ + ################################################## + # Truncate helper method + ################################################## + def _truncate(string, style=0): + max_len = max(27 - len(self.name), 3) + if len(string) > max_len: + if style < 0: # Front truncate + string = '...' + string[3-max_len:] + elif style == 0: # Center truncate + string = string[:max_len/2 - 3] + '...' + string[-max_len/2:] + elif style > 0: # Rear truncate + string = string[:max_len-3] + '...' + return string + + ################################################## + # Simple conditions + ################################################## + value = self.get_value() + if not self.is_valid(): + return _truncate(value) + if value in self.options: + return self.options_names[self.options.index(value)] + + ################################################## + # Split up formatting by type + ################################################## + # Default center truncate + truncate = 0 + e = self.get_evaluated() + t = self.get_type() + if isinstance(e, bool): + return str(e) + elif isinstance(e, Constants.COMPLEX_TYPES): + dt_str = Utils.num_to_str(e) + elif isinstance(e, Constants.VECTOR_TYPES): + # Vector types + if len(e) > 8: + # Large vectors use code + dt_str = self.get_value() + truncate = 1 + else: + # Small vectors use eval + dt_str = ', '.join(map(Utils.num_to_str, e)) + elif t in ('file_open', 'file_save'): + dt_str = self.get_value() + truncate = -1 + else: + # Other types + dt_str = str(e) + + # Done + return _truncate(dt_str, truncate) + + def format_block_surface_markup(self): """ Get the markup for this param. Returns: a pango markup string """ - return Utils.parse_template(PARAM_MARKUP_TMPL, - param=self, font=Constants.PARAM_FONT) + return '<span {foreground} font_desc="{font}"><b>{label}:</b> {value}</span>'.format( + foreground='foreground="red"' if not self.is_valid() else '', font=Constants.PARAM_FONT, + label=Utils.encode(self.name), value=Utils.encode(self.pretty_print().replace('\n', ' ')) + ) diff --git a/grc/gui/ParamWidgets.py b/grc/gui/ParamWidgets.py new file mode 100644 index 0000000000..e5657c288e --- /dev/null +++ b/grc/gui/ParamWidgets.py @@ -0,0 +1,300 @@ +# Copyright 2007-2016 Free Software Foundation, Inc. +# This file is part of GNU Radio +# +# GNU Radio Companion is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# GNU Radio Companion is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + +from __future__ import absolute_import +import os + +from gi.repository import Gtk, Gdk + +from . import Colors + + +class InputParam(Gtk.HBox): + """The base class for an input parameter inside the input parameters dialog.""" + expand = False + + def __init__(self, param, changed_callback=None, editing_callback=None): + Gtk.HBox.__init__(self) + + self.param = param + self._changed_callback = changed_callback + self._editing_callback = editing_callback + + self.label = Gtk.Label() + self.label.set_size_request(150, -1) + self.label.show() + self.pack_start(self.label, False, False, 0) + + self.tp = None + self._have_pending_changes = False + + self.connect('show', self._update_gui) + + def set_color(self, color): + pass + + def set_tooltip_text(self, text): + pass + + def get_text(self): + raise NotImplementedError() + + def _update_gui(self, *args): + """ + Set the markup, color, tooltip, show/hide. + """ + self.label.set_markup(self.param.format_label_markup(self._have_pending_changes)) + + # fixme: find a non-deprecated way to change colors + # self.set_color(Colors.PARAM_ENTRY_COLORS.get( + # self.param.get_type(), Colors.PARAM_ENTRY_DEFAULT_COLOR) + # ) + + self.set_tooltip_text(self.param.format_tooltip_text()) + + if self.param.get_hide() == 'all': + self.hide() + else: + self.show_all() + + def _mark_changed(self, *args): + """ + Mark this param as modified on change, but validate only on focus-lost + """ + self._have_pending_changes = True + self._update_gui() + if self._editing_callback: + self._editing_callback(self, None) + + def _apply_change(self, *args): + """ + Handle a gui change by setting the new param value, + calling the callback (if applicable), and updating. + """ + #set the new value + self.param.set_value(self.get_text()) + #call the callback + if self._changed_callback: + self._changed_callback(self, None) + else: + self.param.validate() + #gui update + self._have_pending_changes = False + self._update_gui() + + def _handle_key_press(self, widget, event): + if event.keyval == Gdk.KEY_Return and event.get_state() & Gdk.ModifierType.CONTROL_MASK: + self._apply_change(widget, event) + return True + return False + + def apply_pending_changes(self): + if self._have_pending_changes: + self._apply_change() + + +class EntryParam(InputParam): + """Provide an entry box for strings and numbers.""" + + def __init__(self, *args, **kwargs): + InputParam.__init__(self, *args, **kwargs) + self._input = Gtk.Entry() + self._input.set_text(self.param.get_value()) + self._input.connect('changed', self._mark_changed) + self._input.connect('focus-out-event', self._apply_change) + self._input.connect('key-press-event', self._handle_key_press) + self.pack_start(self._input, True, True, 0) + + def get_text(self): + return self._input.get_text() + + def set_color(self, color): + self._input.override_background_color(Gtk.StateType.NORMAL, color) + + def set_tooltip_text(self, text): + self._input.set_tooltip_text(text) + + +class MultiLineEntryParam(InputParam): + """Provide an multi-line box for strings.""" + expand = True + + def __init__(self, *args, **kwargs): + InputParam.__init__(self, *args, **kwargs) + self._buffer = Gtk.TextBuffer() + self._buffer.set_text(self.param.get_value()) + self._buffer.connect('changed', self._mark_changed) + + self._view = Gtk.TextView() + self._view.set_buffer(self._buffer) + self._view.connect('focus-out-event', self._apply_change) + self._view.connect('key-press-event', self._handle_key_press) + # fixme: add border to TextView + + self._sw = Gtk.ScrolledWindow() + self._sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + self._sw.add(self._view) + + self.pack_start(self._sw, True, True, True) + + def get_text(self): + buf = self._buffer + text = buf.get_text(buf.get_start_iter(), buf.get_end_iter(), + include_hidden_chars=False) + return text.strip() + + def set_color(self, color): + self._view.override_background_color(Gtk.StateType.NORMAL, color) + + def set_tooltip_text(self, text): + self._view.set_tooltip_text(text) + + +class PythonEditorParam(InputParam): + + def __init__(self, *args, **kwargs): + InputParam.__init__(self, *args, **kwargs) + button = self._button = Gtk.Button(label='Open in Editor') + button.connect('clicked', self.open_editor) + self.pack_start(button, True, True, True) + + def open_editor(self, widget=None): + self.param.parent_flowgraph.install_external_editor(self.param) + + def get_text(self): + pass # we never update the value from here + + def _apply_change(self, *args): + pass + + +class EnumParam(InputParam): + """Provide an entry box for Enum types with a drop down menu.""" + + def __init__(self, *args, **kwargs): + InputParam.__init__(self, *args, **kwargs) + self._input = Gtk.ComboBoxText() + for option_name in self.param.options_names: + self._input.append_text(option_name) + + value = self.param.get_value() + active_index = self.param.options.index(value) + self._input.set_active(active_index) + + self._input.connect('changed', self._editing_callback) + self._input.connect('changed', self._apply_change) + self.pack_start(self._input, False, False, 0) + + def get_text(self): + return self.param.options[self._input.get_active()] + + def set_tooltip_text(self, text): + self._input.set_tooltip_text(text) + + +class EnumEntryParam(InputParam): + """Provide an entry box and drop down menu for Raw Enum types.""" + + def __init__(self, *args, **kwargs): + InputParam.__init__(self, *args, **kwargs) + self._input = Gtk.ComboBoxText.new_with_entry() + for option_name in self.param.options_names: + self._input.append_text(option_name) + + value = self.param.get_value() + try: + active_index = self.param.options.index(value) + self._input.set_active(active_index) + except ValueError: + self._input.set_active(-1) + self._input.get_child().set_text(value) + + self._input.connect('changed', self._apply_change) + self._input.get_child().connect('changed', self._mark_changed) + self._input.get_child().connect('focus-out-event', self._apply_change) + self._input.get_child().connect('key-press-event', self._handle_key_press) + self.pack_start(self._input, False, False, 0) + + @property + def has_custom_value(self): + return self._input.get_active() == -1 + + def get_text(self): + if self.has_custom_value: + return self._input.get_child().get_text() + else: + return self.param.options[self._input.get_active()] + + def set_tooltip_text(self, text): + if self.has_custom_value: # custom entry + self._input.get_child().set_tooltip_text(text) + else: + self._input.set_tooltip_text(text) + + def set_color(self, color): + self._input.get_child().modify_base( + Gtk.StateType.NORMAL, + color if not self.has_custom_value else Colors.PARAM_ENTRY_ENUM_CUSTOM_COLOR + ) + + +class FileParam(EntryParam): + """Provide an entry box for filename and a button to browse for a file.""" + + def __init__(self, *args, **kwargs): + EntryParam.__init__(self, *args, **kwargs) + self._open_button = Gtk.Button(label='...') + self._open_button.connect('clicked', self._handle_clicked) + self.pack_start(self._open_button, False, False, 0) + + def _handle_clicked(self, widget=None): + """ + If the button was clicked, open a file dialog in open/save format. + Replace the text in the entry with the new filename from the file dialog. + """ + #get the paths + file_path = self.param.is_valid() and self.param.get_evaluated() or '' + (dirname, basename) = os.path.isfile(file_path) and os.path.split(file_path) or (file_path, '') + # check for qss theme default directory + if self.param.key == 'qt_qss_theme': + dirname = os.path.dirname(dirname) # trim filename + if not os.path.exists(dirname): + config = self.param.parent_platform.config + dirname = os.path.join(config.install_prefix, '/share/gnuradio/themes') + if not os.path.exists(dirname): + dirname = os.getcwd() # fix bad paths + + #build the dialog + if self.param.get_type() == 'file_open': + file_dialog = Gtk.FileChooserDialog('Open a Data File...', None, + Gtk.FileChooserAction.OPEN, ('gtk-cancel',Gtk.ResponseType.CANCEL,'gtk-open',Gtk.ResponseType.OK)) + elif self.param.get_type() == 'file_save': + file_dialog = Gtk.FileChooserDialog('Save a Data File...', None, + Gtk.FileChooserAction.SAVE, ('gtk-cancel',Gtk.ResponseType.CANCEL, 'gtk-save',Gtk.ResponseType.OK)) + file_dialog.set_do_overwrite_confirmation(True) + file_dialog.set_current_name(basename) #show the current filename + else: + raise ValueError("Can't open file chooser dialog for type " + repr(self.param.get_type())) + file_dialog.set_current_folder(dirname) #current directory + file_dialog.set_select_multiple(False) + file_dialog.set_local_only(True) + if Gtk.ResponseType.OK == file_dialog.run(): #run the dialog + file_path = file_dialog.get_filename() #get the file path + self._input.set_text(file_path) + self._editing_callback() + self._apply_change() + file_dialog.destroy() # destroy the dialog diff --git a/grc/gui/ParserErrorsDialog.py b/grc/gui/ParserErrorsDialog.py index 68ee459414..93a09287e8 100644 --- a/grc/gui/ParserErrorsDialog.py +++ b/grc/gui/ParserErrorsDialog.py @@ -17,14 +17,19 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk +from __future__ import absolute_import -from Constants import MIN_DIALOG_WIDTH, MIN_DIALOG_HEIGHT +import six +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +from gi.repository import GObject -class ParserErrorsDialog(gtk.Dialog): +from .Constants import MIN_DIALOG_WIDTH, MIN_DIALOG_HEIGHT + + +class ParserErrorsDialog(Gtk.Dialog): """ A dialog for viewing parser errors """ @@ -36,32 +41,32 @@ class ParserErrorsDialog(gtk.Dialog): Args: block: a block instance """ - gtk.Dialog.__init__(self, title='Parser Errors', buttons=(gtk.STOCK_CLOSE, gtk.RESPONSE_ACCEPT)) + GObject.GObject.__init__(self, title='Parser Errors', buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.ACCEPT)) self._error_logs = None - self.tree_store = gtk.TreeStore(str) + self.tree_store = Gtk.TreeStore(str) self.update_tree_store(error_logs) - column = gtk.TreeViewColumn('XML Parser Errors by Filename') - renderer = gtk.CellRendererText() + column = Gtk.TreeViewColumn('XML Parser Errors by Filename') + renderer = Gtk.CellRendererText() column.pack_start(renderer, True) column.add_attribute(renderer, 'text', 0) column.set_sort_column_id(0) - self.tree_view = tree_view = gtk.TreeView(self.tree_store) + self.tree_view = tree_view = Gtk.TreeView(self.tree_store) tree_view.set_enable_search(False) # disable pop up search box tree_view.set_search_column(-1) # really disable search tree_view.set_reorderable(False) tree_view.set_headers_visible(False) - tree_view.get_selection().set_mode(gtk.SELECTION_NONE) + tree_view.get_selection().set_mode(Gtk.SelectionMode.NONE) tree_view.append_column(column) for row in self.tree_store: tree_view.expand_row(row.path, False) - scrolled_window = gtk.ScrolledWindow() - scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - scrolled_window.add_with_viewport(tree_view) + scrolled_window = Gtk.ScrolledWindow() + scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + scrolled_window.add(tree_view) self.vbox.pack_start(scrolled_window, True) self.set_size_request(2*MIN_DIALOG_WIDTH, MIN_DIALOG_HEIGHT) @@ -71,7 +76,7 @@ class ParserErrorsDialog(gtk.Dialog): """set up data model""" self.tree_store.clear() self._error_logs = error_logs - for filename, errors in error_logs.iteritems(): + for filename, errors in six.iteritems(error_logs): parent = self.tree_store.append(None, [str(filename)]) try: with open(filename, 'r') as fp: @@ -95,6 +100,6 @@ class ParserErrorsDialog(gtk.Dialog): Returns: true if the response was accept """ - response = gtk.Dialog.run(self) + response = Gtk.Dialog.run(self) self.destroy() - return response == gtk.RESPONSE_ACCEPT + return response == Gtk.ResponseType.ACCEPT diff --git a/grc/gui/Platform.py b/grc/gui/Platform.py index 500df1cce4..6a2a13f644 100644 --- a/grc/gui/Platform.py +++ b/grc/gui/Platform.py @@ -17,25 +17,25 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import, print_function + import os import sys -from ..core.Platform import Platform as _Platform +from ..core.Platform import Platform as CorePlatform -from .Config import Config as _Config -from .Block import Block as _Block -from .Connection import Connection as _Connection -from .Element import Element -from .FlowGraph import FlowGraph as _FlowGraph -from .Param import Param as _Param -from .Port import Port as _Port +from .Config import Config +from .Block import Block +from .Connection import Connection +from .FlowGraph import FlowGraph +from .Param import Param +from .Port import Port -class Platform(Element, _Platform): +class Platform(CorePlatform): def __init__(self, *args, **kwargs): - Element.__init__(self) - _Platform.__init__(self, *args, **kwargs) + CorePlatform.__init__(self, *args, **kwargs) # Ensure conf directories gui_prefs_file = self.config.gui_prefs_file @@ -58,14 +58,17 @@ class Platform(Element, _Platform): import shutil shutil.move(old_gui_prefs_file, gui_prefs_file) except Exception as e: - print >> sys.stderr, e + print(e, file=sys.stderr) ############################################## - # Constructors + # Factories ############################################## - FlowGraph = _FlowGraph - Connection = _Connection - Block = _Block - Port = _Port - Param = _Param - Config = _Config + Config = Config + FlowGraph = FlowGraph + Connection = Connection + block_classes = {key: Block.make_cls_with_base(cls) + for key, cls in CorePlatform.block_classes.items()} + port_classes = {key: Port.make_cls_with_base(cls) + for key, cls in CorePlatform.port_classes.items()} + param_classes = {key: Param.make_cls_with_base(cls) + for key, cls in CorePlatform.param_classes.items()} diff --git a/grc/gui/Port.py b/grc/gui/Port.py index 6314b7ede8..8ac32dc25f 100644 --- a/grc/gui/Port.py +++ b/grc/gui/Port.py @@ -17,145 +17,139 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk - -from . import Actions, Colors, Utils -from .Constants import ( - PORT_SEPARATION, PORT_SPACING, CONNECTOR_EXTENSION_MINIMAL, - CONNECTOR_EXTENSION_INCREMENT, PORT_LABEL_PADDING, PORT_MIN_WIDTH, PORT_LABEL_HIDDEN_WIDTH, PORT_FONT -) +from __future__ import absolute_import +import math + +from gi.repository import Gtk, PangoCairo, Pango + +from . import Actions, Colors, Utils, Constants from .Element import Element -from ..core.Constants import DEFAULT_DOMAIN, GR_MESSAGE_DOMAIN +from ..core.Element import nop_write from ..core.Port import Port as _Port -PORT_MARKUP_TMPL="""\ -<span foreground="black" font_desc="$font">$encode($port.get_name())</span>""" - class Port(_Port, Element): """The graphical port.""" - def __init__(self, block, n, dir): + def __init__(self, parent, direction, **n): """ - Port contructor. + Port constructor. Create list of connector coordinates. """ - _Port.__init__(self, block, n, dir) + super(self.__class__, self).__init__(parent, direction, **n) Element.__init__(self) - self.W = self.H = self.w = self.h = 0 self._connector_coordinate = (0, 0) - self._connector_length = 0 - self._hovering = True - self._force_label_unhidden = False + self._hovering = False + self.force_show_label = False + + self._area = [] + self._bg_color = self._border_color = 0, 0, 0, 0 + self._font_color = list(Colors.FONT_COLOR) + + self._line_width_factor = 1.0 + self._label_layout_offsets = 0, 0 + + self.width_with_label = self.height = 0 + self.connector_length = 0 + + self.label_layout = Gtk.DrawingArea().create_pango_layout('') + self.label_layout.set_alignment(Pango.Alignment.CENTER) + + @property + def width(self): + return self.width_with_label if self._show_label else Constants.PORT_LABEL_HIDDEN_WIDTH + + @width.setter + def width(self, value): + self.width_with_label = value + self.label_layout.set_width(value * Pango.SCALE) + + def _update_colors(self): + """ + Get the color that represents this port's type. + Codes differ for ports where the vec length is 1 or greater than 1. + + Returns: + a hex color code. + """ + if not self.parent_block.enabled: + self._font_color[-1] = 0.4 + color = Colors.BLOCK_DISABLED_COLOR + else: + self._font_color[-1] = 1.0 + color = Colors.PORT_TYPE_TO_COLOR.get(self.get_type()) or Colors.PORT_TYPE_TO_COLOR.get('') + vlen = self.get_vlen() + if vlen > 1: + dark = (0, 0, 30 / 255.0, 50 / 255.0, 70 / 255.0)[min(4, vlen)] + color = tuple(max(c - dark, 0) for c in color) + self._bg_color = color + self._border_color = tuple(max(c - 0.3, 0) for c in color) def create_shapes(self): """Create new areas and labels for the port.""" - Element.create_shapes(self) - if self.get_hide(): - return # this port is hidden, no need to create shapes - if self.get_domain() == GR_MESSAGE_DOMAIN: - pass - elif self.get_domain() != DEFAULT_DOMAIN: - self.line_attributes[0] = 2 - #get current rotation - rotation = self.get_rotation() - #get all sibling ports - ports = self.get_parent().get_sources_gui() \ - if self.is_source else self.get_parent().get_sinks_gui() - ports = filter(lambda p: not p.get_hide(), ports) - #get the max width - self.W = max([port.W for port in ports] + [PORT_MIN_WIDTH]) - W = self.W if not self._label_hidden() else PORT_LABEL_HIDDEN_WIDTH - #get a numeric index for this port relative to its sibling ports - try: - index = ports.index(self) - except: - if hasattr(self, '_connector_length'): - del self._connector_length - return - length = len(filter(lambda p: not p.get_hide(), ports)) - #reverse the order of ports for these rotations - if rotation in (180, 270): - index = length-index-1 - - port_separation = PORT_SEPARATION \ - if not self.get_parent().has_busses[self.is_source] \ - else max([port.H for port in ports]) + PORT_SPACING - - offset = (self.get_parent().H - (length-1)*port_separation - self.H)/2 - #create areas and connector coordinates - if (self.is_sink and rotation == 0) or (self.is_source and rotation == 180): - x = -W - y = port_separation*index+offset - self.add_area((x, y), (W, self.H)) - self._connector_coordinate = (x-1, y+self.H/2) - elif (self.is_source and rotation == 0) or (self.is_sink and rotation == 180): - x = self.get_parent().W - y = port_separation*index+offset - self.add_area((x, y), (W, self.H)) - self._connector_coordinate = (x+1+W, y+self.H/2) - elif (self.is_source and rotation == 90) or (self.is_sink and rotation == 270): - y = -W - x = port_separation*index+offset - self.add_area((x, y), (self.H, W)) - self._connector_coordinate = (x+self.H/2, y-1) - elif (self.is_sink and rotation == 90) or (self.is_source and rotation == 270): - y = self.get_parent().W - x = port_separation*index+offset - self.add_area((x, y), (self.H, W)) - self._connector_coordinate = (x+self.H/2, y+1+W) - #the connector length - self._connector_length = CONNECTOR_EXTENSION_MINIMAL + CONNECTOR_EXTENSION_INCREMENT*index + if self.is_horizontal(): + self._area = (0, 0, self.width, self.height) + elif self.is_vertical(): + self._area = (0, 0, self.height, self.width) + self.bounds_from_area(self._area) + + self._connector_coordinate = { + 0: (self.width, self.height / 2), + 90: (self.height / 2, 0), + 180: (0, self.height / 2), + 270: (self.height / 2, self.width) + }[self.get_connector_direction()] def create_labels(self): """Create the labels for the socket.""" - Element.create_labels(self) - self._bg_color = Colors.get_color(self.get_color()) - # create the layout - layout = gtk.DrawingArea().create_pango_layout('') - layout.set_markup(Utils.parse_template(PORT_MARKUP_TMPL, port=self, font=PORT_FONT)) - self.w, self.h = layout.get_pixel_size() - self.W = 2 * PORT_LABEL_PADDING + self.w - self.H = 2 * PORT_LABEL_PADDING + self.h * ( - 3 if self.get_type() == 'bus' else 1) - self.H += self.H % 2 - # create the pixmap - pixmap = self.get_parent().get_parent().new_pixmap(self.w, self.h) - gc = pixmap.new_gc() - gc.set_foreground(self._bg_color) - pixmap.draw_rectangle(gc, True, 0, 0, self.w, self.h) - pixmap.draw_layout(gc, 0, 0, layout) - # create vertical and horizontal pixmaps - self.horizontal_label = pixmap - if self.is_vertical(): - self.vertical_label = self.get_parent().get_parent().new_pixmap(self.h, self.w) - Utils.rotate_pixmap(gc, self.horizontal_label, self.vertical_label) - def draw(self, gc, window): + if self.domain in (Constants.GR_MESSAGE_DOMAIN, Constants.DEFAULT_DOMAIN): + self._line_width_factor = 1.0 + else: + self._line_width_factor = 2.0 + + self._update_colors() + + layout = self.label_layout + layout.set_markup('<span font_desc="{font}">{name}</span>'.format( + name=Utils.encode(self.name), font=Constants.PORT_FONT + )) + label_width, label_height = self.label_layout.get_pixel_size() + + self.width = 2 * Constants.PORT_LABEL_PADDING + label_width + self.height = 2 * Constants.PORT_LABEL_PADDING + label_height + self._label_layout_offsets = [0, Constants.PORT_LABEL_PADDING] + if self.get_type() == 'bus': + self.height += Constants.PORT_EXTRA_BUS_HEIGHT + self._label_layout_offsets[1] += Constants.PORT_EXTRA_BUS_HEIGHT / 2 + self.height += self.height % 2 # uneven height + + def draw(self, cr): """ Draw the socket with a label. - - Args: - gc: the graphics context - window: the gtk window to draw on """ - Element.draw( - self, gc, window, bg_color=self._bg_color, - border_color=self.is_highlighted() and Colors.HIGHLIGHT_COLOR or - self.get_parent().is_dummy_block and Colors.MISSING_BLOCK_BORDER_COLOR or - Colors.BORDER_COLOR, - ) - if not self._areas_list or self._label_hidden(): - return # this port is either hidden (no areas) or folded (no label) - X, Y = self.get_coordinate() - (x, y), (w, h) = self._areas_list[0] # use the first area's sizes to place the labels - if self.is_horizontal(): - window.draw_drawable(gc, self.horizontal_label, 0, 0, x+X+(self.W-self.w)/2, y+Y+(self.H-self.h)/2, -1, -1) - elif self.is_vertical(): - window.draw_drawable(gc, self.vertical_label, 0, 0, x+X+(self.H-self.h)/2, y+Y+(self.W-self.w)/2, -1, -1) + border_color = self._border_color + cr.set_line_width(self._line_width_factor * cr.get_line_width()) + cr.translate(*self.coordinate) + + cr.rectangle(*self._area) + cr.set_source_rgba(*self._bg_color) + cr.fill_preserve() + cr.set_source_rgba(*border_color) + cr.stroke() + + if not self._show_label: + return # this port is folded (no label) + + if self.is_vertical(): + cr.rotate(-math.pi / 2) + cr.translate(-self.width, 0) + cr.translate(*self._label_layout_offsets) + + cr.set_source_rgba(*self._font_color) + PangoCairo.update_layout(cr, self.label_layout) + PangoCairo.show_layout(cr, self.label_layout) def get_connector_coordinate(self): """ @@ -164,9 +158,11 @@ class Port(_Port, Element): Returns: the connector coordinate (x, y) tuple """ - x, y = self._connector_coordinate - X, Y = self.get_coordinate() - return (x + X, y + Y) + return [sum(c) for c in zip( + self._connector_coordinate, # relative to port + self.coordinate, # relative to block + self.parent_block.coordinate # abs + )] def get_connector_direction(self): """ @@ -177,36 +173,15 @@ class Port(_Port, Element): Returns: the direction in degrees """ - if self.is_source: return self.get_rotation() - elif self.is_sink: return (self.get_rotation() + 180)%360 + if self.is_source: + return self.rotation + elif self.is_sink: + return (self.rotation + 180) % 360 - def get_connector_length(self): - """ - Get the length of the connector. - The connector length increases as the port index changes. - - Returns: - the length in pixels - """ - return self._connector_length - - def get_rotation(self): - """ - Get the parent's rotation rather than self. - - Returns: - the parent's rotation - """ - return self.get_parent().get_rotation() - - def move(self, delta_coor): - """ - Move the parent rather than self. - - Args: - delta_corr: the (delta_x, delta_y) tuple - """ - self.get_parent().move(delta_coor) + @nop_write + @property + def rotation(self): + return self.parent_block.rotation def rotate(self, direction): """ @@ -215,63 +190,47 @@ class Port(_Port, Element): Args: direction: degrees to rotate """ - self.get_parent().rotate(direction) - - def get_coordinate(self): - """ - Get the parent's coordinate rather than self. - - Returns: - the parents coordinate - """ - return self.get_parent().get_coordinate() + self.parent_block.rotate(direction) - def set_highlighted(self, highlight): + def move(self, delta_coor): """ - Set the parent highlight rather than self. + Move the parent rather than self. Args: - highlight: true to enable highlighting + delta_corr: the (delta_x, delta_y) tuple """ - self.get_parent().set_highlighted(highlight) + self.parent_block.move(delta_coor) - def is_highlighted(self): - """ - Get the parent's is highlight rather than self. + @property + def highlighted(self): + return self.parent_block.highlighted - Returns: - the parent's highlighting status - """ - return self.get_parent().is_highlighted() + @highlighted.setter + def highlighted(self, value): + self.parent_block.highlighted = value - def _label_hidden(self): + @property + def _show_label(self): """ Figure out if the label should be hidden Returns: true if the label should not be shown """ - return self._hovering and not self._force_label_unhidden and Actions.TOGGLE_AUTO_HIDE_PORT_LABELS.get_active() - - def force_label_unhidden(self, enable=True): - """ - Disable showing the label on mouse-over for this port - - Args: - enable: true to override the mouse-over behaviour - """ - self._force_label_unhidden = enable + return self._hovering or self.force_show_label or not Actions.TOGGLE_AUTO_HIDE_PORT_LABELS.get_active() def mouse_over(self): """ Called from flow graph on mouse-over """ - self._hovering = False - return Actions.TOGGLE_AUTO_HIDE_PORT_LABELS.get_active() # only redraw if necessary + changed = not self._show_label + self._hovering = True + return changed def mouse_out(self): """ Called from flow graph on mouse-out """ - self._hovering = True - return Actions.TOGGLE_AUTO_HIDE_PORT_LABELS.get_active() # only redraw if necessary + label_was_shown = self._show_label + self._hovering = False + return label_was_shown != self._show_label diff --git a/grc/gui/Preferences.py b/grc/gui/Preferences.py index d377018eb4..d917537971 100644 --- a/grc/gui/Preferences.py +++ b/grc/gui/Preferences.py @@ -17,9 +17,12 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import, print_function + import os import sys -import ConfigParser + +from six.moves import configparser HEADER = """\ @@ -31,7 +34,7 @@ HEADER = """\ """ _platform = None -_config_parser = ConfigParser.SafeConfigParser() +_config_parser = configparser.SafeConfigParser() def file_extension(): @@ -45,12 +48,12 @@ def load(platform): for section in ['main', 'files_open', 'files_recent']: try: _config_parser.add_section(section) - except Exception, e: - print e + except Exception as e: + print(e) try: _config_parser.read(_platform.get_prefs_file()) except Exception as err: - print >> sys.stderr, err + print(err, file=sys.stderr) def save(): @@ -59,7 +62,7 @@ def save(): fp.write(HEADER) _config_parser.write(fp) except Exception as err: - print >> sys.stderr, err + print(err, file=sys.stderr) def entry(key, value=None, default=None): @@ -74,7 +77,7 @@ def entry(key, value=None, default=None): }.get(_type, _config_parser.get) try: result = getter('main', key) - except (AttributeError, ConfigParser.Error): + except (AttributeError, configparser.Error): result = _type() if default is None else default return result @@ -106,7 +109,7 @@ def get_file_list(key): try: files = [value for name, value in _config_parser.items(key) if name.startswith('%s_' % key)] - except (AttributeError, ConfigParser.Error): + except (AttributeError, configparser.Error): files = [] return files @@ -121,7 +124,7 @@ def set_open_files(files): def get_recent_files(): """ Gets recent files, removes any that do not exist and re-saves it """ - files = filter(os.path.exists, get_file_list('files_recent')) + files = list(filter(os.path.exists, get_file_list('files_recent'))) set_recent_files(files) return files diff --git a/grc/gui/PropsDialog.py b/grc/gui/PropsDialog.py index 7c66a77a54..3a0f6ae6de 100644 --- a/grc/gui/PropsDialog.py +++ b/grc/gui/PropsDialog.py @@ -17,114 +17,89 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk +from __future__ import absolute_import +from gi.repository import Gtk, Gdk, GObject, Pango -import Actions -from Dialogs import SimpleTextDisplay -from Constants import MIN_DIALOG_WIDTH, MIN_DIALOG_HEIGHT, FONT_SIZE -import Utils -import pango +from . import Actions, Utils, Constants +from .Dialogs import SimpleTextDisplay +import six -TAB_LABEL_MARKUP_TMPL="""\ -#set $foreground = $valid and 'black' or 'red' -<span foreground="$foreground">$encode($tab)</span>""" - -def get_title_label(title): - """ - Get a title label for the params window. - The title will be bold, underlined, and left justified. - - Args: - title: the text of the title - - Returns: - a gtk object - """ - label = gtk.Label() - label.set_markup('\n<b><span underline="low">%s</span>:</b>\n'%title) - hbox = gtk.HBox() - hbox.pack_start(label, False, False, padding=11) - return hbox - - -class PropsDialog(gtk.Dialog): +class PropsDialog(Gtk.Dialog): """ A dialog to set block parameters, view errors, and view documentation. """ - def __init__(self, block): + def __init__(self, parent, block): """ Properties dialog constructor. - Args: + Args:% block: a block instance """ - self._hash = 0 - gtk.Dialog.__init__( + Gtk.Dialog.__init__( self, - title='Properties: %s' % block.get_name(), - buttons=(gtk.STOCK_OK, gtk.RESPONSE_ACCEPT, - gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, - gtk.STOCK_APPLY, gtk.RESPONSE_APPLY) + title='Properties: ' + block.name, + transient_for=parent, + modal=True, + destroy_with_parent=True, + ) + self.add_buttons( + Gtk.STOCK_OK, Gtk.ResponseType.ACCEPT, + Gtk.STOCK_CANCEL, Gtk.ResponseType.REJECT, + Gtk.STOCK_APPLY, Gtk.ResponseType.APPLY, ) - self.set_response_sensitive(gtk.RESPONSE_APPLY, False) - self.set_size_request(MIN_DIALOG_WIDTH, MIN_DIALOG_HEIGHT) + self.set_response_sensitive(Gtk.ResponseType.APPLY, False) + self.set_size_request(Constants.MIN_DIALOG_WIDTH, Constants.MIN_DIALOG_HEIGHT) + self._block = block + self._hash = 0 - vpaned = gtk.VPaned() - self.vbox.pack_start(vpaned) + vpaned = Gtk.VPaned() + self.vbox.pack_start(vpaned, True, True, 0) # Notebook to hold param boxes - notebook = gtk.Notebook() + notebook = self.notebook = Gtk.Notebook() notebook.set_show_border(False) notebook.set_scrollable(True) # scroll arrows for page tabs - notebook.set_tab_pos(gtk.POS_TOP) + notebook.set_tab_pos(Gtk.PositionType.TOP) vpaned.pack1(notebook, True) # Params boxes for block parameters - self._params_boxes = list() - for tab in block.get_param_tab_labels(): - label = gtk.Label() - vbox = gtk.VBox() - scroll_box = gtk.ScrolledWindow() - scroll_box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - scroll_box.add_with_viewport(vbox) - notebook.append_page(scroll_box, label) - self._params_boxes.append((tab, label, vbox)) + self._params_boxes = [] + self._build_param_tab_boxes(block.params) # Docs for the block self._docs_text_display = doc_view = SimpleTextDisplay() - doc_view.get_buffer().create_tag('b', weight=pango.WEIGHT_BOLD) - self._docs_box = gtk.ScrolledWindow() - self._docs_box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - self._docs_box.add_with_viewport(self._docs_text_display) - notebook.append_page(self._docs_box, gtk.Label("Documentation")) + doc_view.get_buffer().create_tag('b', weight=Pango.Weight.BOLD) + self._docs_box = Gtk.ScrolledWindow() + self._docs_box.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + self._docs_box.add(self._docs_text_display) + notebook.append_page(self._docs_box, Gtk.Label(label="Documentation")) # Generated code for the block if Actions.TOGGLE_SHOW_CODE_PREVIEW_TAB.get_active(): self._code_text_display = code_view = SimpleTextDisplay() - code_view.set_wrap_mode(gtk.WRAP_NONE) - code_view.get_buffer().create_tag('b', weight=pango.WEIGHT_BOLD) - code_view.modify_font(pango.FontDescription( - 'monospace %d' % FONT_SIZE)) - code_box = gtk.ScrolledWindow() - code_box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - code_box.add_with_viewport(self._code_text_display) - notebook.append_page(code_box, gtk.Label("Generated Code")) + code_view.set_wrap_mode(Gtk.WrapMode.NONE) + code_view.get_buffer().create_tag('b', weight=Pango.Weight.BOLD) + code_view.set_monospace(True) + # todo: set font size in non-deprecated way + # code_view.override_font(Pango.FontDescription('monospace %d' % Constants.FONT_SIZE)) + code_box = Gtk.ScrolledWindow() + code_box.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + code_box.add(self._code_text_display) + notebook.append_page(code_box, Gtk.Label(label="Generated Code")) else: self._code_text_display = None # Error Messages for the block self._error_messages_text_display = SimpleTextDisplay() - self._error_box = gtk.ScrolledWindow() - self._error_box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - self._error_box.add_with_viewport(self._error_messages_text_display) + self._error_box = Gtk.ScrolledWindow() + self._error_box.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + self._error_box.add(self._error_messages_text_display) vpaned.pack2(self._error_box) - vpaned.set_position(int(0.65 * MIN_DIALOG_HEIGHT)) + vpaned.set_position(int(0.65 * Constants.MIN_DIALOG_HEIGHT)) # Connect events self.connect('key-press-event', self._handle_key_press) @@ -132,6 +107,27 @@ class PropsDialog(gtk.Dialog): self.connect('response', self._handle_response) self.show_all() # show all (performs initial gui update) + def _build_param_tab_boxes(self, params): + tab_labels = (p.tab_label for p in self._block.params.values()) + + def unique_tab_labels(): + seen = {Constants.DEFAULT_PARAM_TAB} + yield Constants.DEFAULT_PARAM_TAB + for tab_label in tab_labels: + if tab_label in seen: + continue + yield tab_label + seen.add(tab_label) + + for tab in unique_tab_labels(): + label = Gtk.Label() + vbox = Gtk.VBox() + scroll_box = Gtk.ScrolledWindow() + scroll_box.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + scroll_box.add(vbox) + self.notebook.append_page(scroll_box, label) + self._params_boxes.append((tab, label, vbox)) + def _params_changed(self): """ Have the params in this dialog changed? @@ -144,25 +140,23 @@ class PropsDialog(gtk.Dialog): true if changed """ old_hash = self._hash - # create a tuple of things from each param that affects the params box - self._hash = hash(tuple([( - hash(param), param.get_name(), param.get_type(), - param.get_hide() == 'all', - ) for param in self._block.get_params()])) - return self._hash != old_hash + new_hash = self._hash = hash(tuple( + (hash(param), param.name, param.get_type(), param.get_hide() == 'all',) + for param in self._block.params.values() + )) + return new_hash != old_hash def _handle_changed(self, *args): """ A change occurred within a param: Rewrite/validate the block and update the gui. """ - # update for the block self._block.rewrite() self._block.validate() self.update_gui() def _activate_apply(self, *args): - self.set_response_sensitive(gtk.RESPONSE_APPLY, True) + self.set_response_sensitive(Gtk.ResponseType.APPLY, True) def update_gui(self, widget=None, force=False): """ @@ -173,45 +167,48 @@ class PropsDialog(gtk.Dialog): Update the documentation block. Hide the box if there are no docs. """ - # update the params box if force or self._params_changed(): # hide params box before changing for tab, label, vbox in self._params_boxes: - vbox.hide_all() + vbox.hide() # empty the params box for child in vbox.get_children(): vbox.remove(child) - child.destroy() + # child.destroy() # disabled because it throws errors... # repopulate the params box box_all_valid = True - for param in filter(lambda p: p.get_tab_label() == tab, self._block.get_params()): - if param.get_hide() == 'all': + for param in self._block.params.values(): + # fixme: why do we even rebuild instead of really hiding params? + if param.get_tab_label() != tab or param.get_hide() == 'all': continue box_all_valid = box_all_valid and param.is_valid() + input_widget = param.get_input(self._handle_changed, self._activate_apply) - vbox.pack_start(input_widget, input_widget.expand) - label.set_markup(Utils.parse_template(TAB_LABEL_MARKUP_TMPL, valid=box_all_valid, tab=tab)) - # show params box with new params - vbox.show_all() - # update the errors box + input_widget.show_all() + vbox.pack_start(input_widget, input_widget.expand, True, 1) + + label.set_markup('<span foreground="{color}">{name}</span>'.format( + color='black' if box_all_valid else 'red', name=Utils.encode(tab) + )) + vbox.show() # show params box with new params + if self._block.is_valid(): self._error_box.hide() else: self._error_box.show() messages = '\n\n'.join(self._block.get_error_messages()) self._error_messages_text_display.set_text(messages) - # update the docs box + self._update_docs_page() - # update the generated code self._update_generated_code_page() def _update_docs_page(self): """Show documentation from XML and try to display best matching docstring""" - buffer = self._docs_text_display.get_buffer() - buffer.delete(buffer.get_start_iter(), buffer.get_end_iter()) - pos = buffer.get_end_iter() + buf = self._docs_text_display.get_buffer() + buf.delete(buf.get_start_iter(), buf.get_end_iter()) + pos = buf.get_end_iter() - docstrings = self._block.get_doc() + docstrings = self._block.documentation if not docstrings: return @@ -219,11 +216,11 @@ class PropsDialog(gtk.Dialog): from_xml = docstrings.pop('', '') for line in from_xml.splitlines(): if line.lstrip() == line and line.endswith(':'): - buffer.insert_with_tags_by_name(pos, line + '\n', 'b') + buf.insert_with_tags_by_name(pos, line + '\n', 'b') else: - buffer.insert(pos, line + '\n') + buf.insert(pos, line + '\n') if from_xml: - buffer.insert(pos, '\n') + buf.insert(pos, '\n') # if given the current parameters an exact match can be made block_constructor = self._block.get_make().rsplit('.', 2)[-1] @@ -232,19 +229,19 @@ class PropsDialog(gtk.Dialog): docstrings = {block_class: docstrings[block_class]} # show docstring(s) extracted from python sources - for cls_name, docstring in docstrings.iteritems(): - buffer.insert_with_tags_by_name(pos, cls_name + '\n', 'b') - buffer.insert(pos, docstring + '\n\n') + for cls_name, docstring in six.iteritems(docstrings): + buf.insert_with_tags_by_name(pos, cls_name + '\n', 'b') + buf.insert(pos, docstring + '\n\n') pos.backward_chars(2) - buffer.delete(pos, buffer.get_end_iter()) + buf.delete(pos, buf.get_end_iter()) def _update_generated_code_page(self): if not self._code_text_display: return # user disabled code preview - buffer = self._code_text_display.get_buffer() + buf = self._code_text_display.get_buffer() block = self._block - key = block.get_key() + key = block.key if key == 'epy_block': src = block.get_param('_source_code').get_value() @@ -256,40 +253,34 @@ class PropsDialog(gtk.Dialog): def insert(header, text): if not text: return - buffer.insert_with_tags_by_name(buffer.get_end_iter(), header, 'b') - buffer.insert(buffer.get_end_iter(), text) + buf.insert_with_tags_by_name(buf.get_end_iter(), header, 'b') + buf.insert(buf.get_end_iter(), text) - buffer.delete(buffer.get_start_iter(), buffer.get_end_iter()) + buf.delete(buf.get_start_iter(), buf.get_end_iter()) insert('# Imports\n', '\n'.join(block.get_imports())) - if key.startswith('variable'): + if block.is_variable: insert('\n\n# Variables\n', block.get_var_make()) insert('\n\n# Blocks\n', block.get_make()) if src: insert('\n\n# External Code ({}.py)\n'.format(block.get_id()), src) def _handle_key_press(self, widget, event): - """ - Handle key presses from the keyboard. - Call the ok response when enter is pressed. - - Returns: - false to forward the keypress - """ - if (event.keyval == gtk.keysyms.Return and - event.state & gtk.gdk.CONTROL_MASK == 0 and - not isinstance(widget.get_focus(), gtk.TextView) - ): - self.response(gtk.RESPONSE_ACCEPT) + close_dialog = ( + event.keyval == Gdk.KEY_Return and + event.get_state() & Gdk.ModifierType.CONTROL_MASK == 0 and + not isinstance(widget.get_focus(), Gtk.TextView) + ) + if close_dialog: + self.response(Gtk.ResponseType.ACCEPT) return True # handled here + return False # forward the keypress def _handle_response(self, widget, response): - if response in (gtk.RESPONSE_APPLY, gtk.RESPONSE_ACCEPT): + if response in (Gtk.ResponseType.APPLY, Gtk.ResponseType.ACCEPT): for tab, label, vbox in self._params_boxes: for child in vbox.get_children(): child.apply_pending_changes() - self.set_response_sensitive(gtk.RESPONSE_APPLY, False) + self.set_response_sensitive(Gtk.ResponseType.APPLY, False) return True return False - - diff --git a/grc/gui/StateCache.py b/grc/gui/StateCache.py index 3cdb5f30ce..b109a1281b 100644 --- a/grc/gui/StateCache.py +++ b/grc/gui/StateCache.py @@ -17,8 +17,9 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import Actions -from Constants import STATE_CACHE_SIZE +from __future__ import absolute_import +from . import Actions +from .Constants import STATE_CACHE_SIZE class StateCache(object): """ diff --git a/grc/gui/Utils.py b/grc/gui/Utils.py index 51b9b19e9f..782a7e3a01 100644 --- a/grc/gui/Utils.py +++ b/grc/gui/Utils.py @@ -17,37 +17,12 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk -import gobject +from __future__ import absolute_import -from Cheetah.Template import Template +from gi.repository import GLib +import cairo -from Constants import POSSIBLE_ROTATIONS, CANVAS_GRID_SIZE - - -def rotate_pixmap(gc, src_pixmap, dst_pixmap, angle=gtk.gdk.PIXBUF_ROTATE_COUNTERCLOCKWISE): - """ - Load the destination pixmap with a rotated version of the source pixmap. - The source pixmap will be loaded into a pixbuf, rotated, and drawn to the destination pixmap. - The pixbuf is a client-side drawable, where a pixmap is a server-side drawable. - - Args: - gc: the graphics context - src_pixmap: the source pixmap - dst_pixmap: the destination pixmap - angle: the angle to rotate by - """ - width, height = src_pixmap.get_size() - pixbuf = gtk.gdk.Pixbuf( - colorspace=gtk.gdk.COLORSPACE_RGB, - has_alpha=False, bits_per_sample=8, - width=width, height=height, - ) - pixbuf.get_from_drawable(src_pixmap, src_pixmap.get_colormap(), 0, 0, 0, 0, -1, -1) - pixbuf = pixbuf.rotate_simple(angle) - dst_pixmap.draw_pixbuf(gc, pixbuf, 0, 0, 0, 0) +from . import Colors, Constants def get_rotated_coordinate(coor, rotation): @@ -62,8 +37,8 @@ def get_rotated_coordinate(coor, rotation): the rotated coordinates """ # handles negative angles - rotation = (rotation + 360)%360 - if rotation not in POSSIBLE_ROTATIONS: + rotation = (rotation + 360) % 360 + if rotation not in Constants.POSSIBLE_ROTATIONS: raise ValueError('unusable rotation angle "%s"'%str(rotation)) # determine the number of degrees to rotate cos_r, sin_r = { @@ -73,7 +48,7 @@ def get_rotated_coordinate(coor, rotation): return x * cos_r + y * sin_r, -x * sin_r + y * cos_r -def get_angle_from_coordinates((x1, y1), (x2, y2)): +def get_angle_from_coordinates(p1, p2): """ Given two points, calculate the vector direction from point1 to point2, directions are multiples of 90 degrees. @@ -84,50 +59,87 @@ def get_angle_from_coordinates((x1, y1), (x2, y2)): Returns: the direction in degrees """ + (x1, y1) = p1 + (x2, y2) = p2 if y1 == y2: # 0 or 180 return 0 if x2 > x1 else 180 else: # 90 or 270 return 270 if y2 > y1 else 90 +def align_to_grid(coor, mode=round): + def align(value): + return int(mode(value / (1.0 * Constants.CANVAS_GRID_SIZE)) * Constants.CANVAS_GRID_SIZE) + try: + return [align(c) for c in coor] + except TypeError: + x = coor + return align(coor) + + +def num_to_str(num): + """ Display logic for numbers """ + def eng_notation(value, fmt='g'): + """Convert a number to a string in engineering notation. E.g., 5e-9 -> 5n""" + template = '{:' + fmt + '}{}' + magnitude = abs(value) + for exp, symbol in zip(range(9, -15-1, -3), 'GMk munpf'): + factor = 10 ** exp + if magnitude >= factor: + return template.format(value / factor, symbol.strip()) + return template.format(value, '') + + if isinstance(num, Constants.COMPLEX_TYPES): + num = complex(num) # Cast to python complex + if num == 0: + return '0' + output = eng_notation(num.real) if num.real else '' + output += eng_notation(num.imag, '+g' if output else 'g') + 'j' if num.imag else '' + return output + else: + return str(num) + + def encode(value): """Make sure that we pass only valid utf-8 strings into markup_escape_text. Older versions of glib seg fault if the last byte starts a multi-byte character. """ - valid_utf8 = value.decode('utf-8', errors='replace').encode('utf-8') - return gobject.markup_escape_text(valid_utf8) - - -class TemplateParser(object): - def __init__(self): - self.cache = {} - - def __call__(self, tmpl_str, **kwargs): - """ - Parse the template string with the given args. - Pass in the xml encode method for pango escape chars. - - Args: - tmpl_str: the template as a string - - Returns: - a string of the parsed template - """ - kwargs['encode'] = encode - template = self.cache.setdefault(tmpl_str, Template.compile(tmpl_str)) - return str(template(namespaces=kwargs)) - -parse_template = TemplateParser() - - -def align_to_grid(coor, mode=round): - def align(value): - return int(mode(value / (1.0 * CANVAS_GRID_SIZE)) * CANVAS_GRID_SIZE) - try: - return map(align, coor) - except TypeError: - x = coor - return align(coor) + return GLib.markup_escape_text(valid_utf8) + + +def make_screenshot(flow_graph, file_path, transparent_bg=False): + if not file_path: + return + + x_min, y_min, x_max, y_max = flow_graph.extend + padding = Constants.CANVAS_GRID_SIZE + width = x_max - x_min + 2 * padding + height = y_max - y_min + 2 * padding + + if file_path.endswith('.png'): + psurf = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + elif file_path.endswith('.pdf'): + psurf = cairo.PDFSurface(file_path, width, height) + elif file_path.endswith('.svg'): + psurf = cairo.SVGSurface(file_path, width, height) + else: + raise ValueError('Unknown file format') + + cr = cairo.Context(psurf) + + if not transparent_bg: + cr.set_source_rgba(*Colors.FLOWGRAPH_BACKGROUND_COLOR) + cr.rectangle(0, 0, width, height) + cr.fill() + + cr.translate(padding - x_min, padding - y_min) + flow_graph.draw(cr) + + if file_path.endswith('.png'): + psurf.write_to_png(file_path) + if file_path.endswith('.pdf') or file_path.endswith('.svg'): + cr.show_page() + psurf.finish() diff --git a/grc/gui/VariableEditor.py b/grc/gui/VariableEditor.py index 7721f3bda6..d97b9e9f24 100644 --- a/grc/gui/VariableEditor.py +++ b/grc/gui/VariableEditor.py @@ -1,5 +1,5 @@ """ -Copyright 2015 Free Software Foundation, Inc. +Copyright 2015, 2016 Free Software Foundation, Inc. This file is part of GNU Radio GNU Radio Companion is free software; you can redistribute it and/or @@ -17,49 +17,45 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -from operator import attrgetter +from __future__ import absolute_import -import pygtk -pygtk.require('2.0') -import gtk -import gobject +from gi.repository import Gtk, Gdk, GObject -from . import Actions -from . import Preferences -from .Constants import DEFAULT_BLOCKS_WINDOW_WIDTH +from . import Actions, Preferences, Constants BLOCK_INDEX = 0 ID_INDEX = 1 -class VariableEditorContextMenu(gtk.Menu): +class VariableEditorContextMenu(Gtk.Menu): """ A simple context menu for our variable editor """ + def __init__(self, var_edit): - gtk.Menu.__init__(self) + Gtk.Menu.__init__(self) - self.imports = gtk.MenuItem("Add _Import") + self.imports = Gtk.MenuItem(label="Add _Import") self.imports.connect('activate', var_edit.handle_action, var_edit.ADD_IMPORT) self.add(self.imports) - self.variables = gtk.MenuItem("Add _Variable") + self.variables = Gtk.MenuItem(label="Add _Variable") self.variables.connect('activate', var_edit.handle_action, var_edit.ADD_VARIABLE) self.add(self.variables) - self.add(gtk.SeparatorMenuItem()) + self.add(Gtk.SeparatorMenuItem()) - self.enable = gtk.MenuItem("_Enable") + self.enable = Gtk.MenuItem(label="_Enable") self.enable.connect('activate', var_edit.handle_action, var_edit.ENABLE_BLOCK) - self.disable = gtk.MenuItem("_Disable") + self.disable = Gtk.MenuItem(label="_Disable") self.disable.connect('activate', var_edit.handle_action, var_edit.DISABLE_BLOCK) self.add(self.enable) self.add(self.disable) - self.add(gtk.SeparatorMenuItem()) + self.add(Gtk.SeparatorMenuItem()) - self.delete = gtk.MenuItem("_Delete") + self.delete = Gtk.MenuItem(label="_Delete") self.delete.connect('activate', var_edit.handle_action, var_edit.DELETE_BLOCK) self.add(self.delete) - self.add(gtk.SeparatorMenuItem()) + self.add(Gtk.SeparatorMenuItem()) - self.properties = gtk.MenuItem("_Properties...") + self.properties = Gtk.MenuItem(label="_Properties...") self.properties.connect('activate', var_edit.handle_action, var_edit.OPEN_PROPERTIES) self.add(self.properties) self.show_all() @@ -71,7 +67,7 @@ class VariableEditorContextMenu(gtk.Menu): self.disable.set_sensitive(selected and enabled) -class VariableEditor(gtk.VBox): +class VariableEditor(Gtk.VBox): # Actions that are handled by the editor ADD_IMPORT = 0 @@ -82,23 +78,28 @@ class VariableEditor(gtk.VBox): ENABLE_BLOCK = 5 DISABLE_BLOCK = 6 - def __init__(self, platform, get_flow_graph): - gtk.VBox.__init__(self) - self.platform = platform - self.get_flow_graph = get_flow_graph + __gsignals__ = { + 'create_new_block': (GObject.SignalFlags.RUN_FIRST, None, (str,)), + 'remove_block': (GObject.SignalFlags.RUN_FIRST, None, (str,)) + } + + def __init__(self): + Gtk.VBox.__init__(self) self._block = None self._mouse_button_pressed = False + self._imports = [] + self._variables = [] # Only use the model to store the block reference and name. # Generate everything else dynamically - self.treestore = gtk.TreeStore(gobject.TYPE_PYOBJECT, # Block reference - gobject.TYPE_STRING) # Category and block name - self.treeview = gtk.TreeView(self.treestore) + self.treestore = Gtk.TreeStore(GObject.TYPE_PYOBJECT, # Block reference + GObject.TYPE_STRING) # Category and block name + self.treeview = Gtk.TreeView(model=self.treestore) self.treeview.set_enable_search(False) self.treeview.set_search_column(-1) #self.treeview.set_enable_search(True) #self.treeview.set_search_column(ID_INDEX) - self.treeview.get_selection().set_mode('single') + self.treeview.get_selection().set_mode(Gtk.SelectionMode.SINGLE) self.treeview.set_headers_visible(True) self.treeview.connect('button-press-event', self._handle_mouse_button_press) self.treeview.connect('button-release-event', self._handle_mouse_button_release) @@ -106,67 +107,63 @@ class VariableEditor(gtk.VBox): self.treeview.connect('key-press-event', self._handle_key_button_press) # Block Name or Category - self.id_cell = gtk.CellRendererText() + self.id_cell = Gtk.CellRendererText() self.id_cell.connect('edited', self._handle_name_edited_cb) - id_column = gtk.TreeViewColumn("Id", self.id_cell, text=ID_INDEX) + id_column = Gtk.TreeViewColumn("Id", self.id_cell, text=ID_INDEX) id_column.set_name("id") id_column.set_resizable(True) id_column.set_max_width(300) id_column.set_min_width(80) id_column.set_fixed_width(100) - id_column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) + id_column.set_sizing(Gtk.TreeViewColumnSizing.FIXED) id_column.set_cell_data_func(self.id_cell, self.set_properties) self.id_column = id_column self.treeview.append_column(id_column) - self.treestore.set_sort_column_id(ID_INDEX, gtk.SORT_ASCENDING) + self.treestore.set_sort_column_id(ID_INDEX, Gtk.SortType.ASCENDING) # For forcing resize self._col_width = 0 # Block Value - self.value_cell = gtk.CellRendererText() + self.value_cell = Gtk.CellRendererText() self.value_cell.connect('edited', self._handle_value_edited_cb) - value_column = gtk.TreeViewColumn("Value", self.value_cell) + value_column = Gtk.TreeViewColumn("Value", self.value_cell) value_column.set_name("value") value_column.set_resizable(False) value_column.set_expand(True) value_column.set_min_width(100) - value_column.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE) + value_column.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) value_column.set_cell_data_func(self.value_cell, self.set_value) self.value_column = value_column self.treeview.append_column(value_column) # Block Actions (Add, Remove) - self.action_cell = gtk.CellRendererPixbuf() + self.action_cell = Gtk.CellRendererPixbuf() value_column.pack_start(self.action_cell, False) value_column.set_cell_data_func(self.action_cell, self.set_icon) # Make the scrolled window to hold the tree view - scrolled_window = gtk.ScrolledWindow() - scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - scrolled_window.add_with_viewport(self.treeview) - scrolled_window.set_size_request(DEFAULT_BLOCKS_WINDOW_WIDTH, -1) - self.pack_start(scrolled_window) + scrolled_window = Gtk.ScrolledWindow() + scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) + scrolled_window.add(self.treeview) + scrolled_window.set_size_request(Constants.DEFAULT_BLOCKS_WINDOW_WIDTH, -1) + self.pack_start(scrolled_window, True, True, 0) # Context menus self._context_menu = VariableEditorContextMenu(self) self._confirm_delete = Preferences.variable_editor_confirm_delete() # Sets cell contents - def set_icon(self, col, cell, model, iter): + def set_icon(self, col, cell, model, iter, data): block = model.get_value(iter, BLOCK_INDEX) - if block: - pb = self.treeview.render_icon(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU, None) - else: - pb = self.treeview.render_icon(gtk.STOCK_ADD, gtk.ICON_SIZE_MENU, None) - cell.set_property('pixbuf', pb) + cell.set_property('icon-name', 'window-close' if block else 'list-add') - def set_value(self, col, cell, model, iter): + def set_value(self, col, cell, model, iter, data): sp = cell.set_property block = model.get_value(iter, BLOCK_INDEX) # Set the default properties for this column first. # Some set in set_properties() may be overridden (editable for advanced variable blocks) - self.set_properties(col, cell, model, iter) + self.set_properties(col, cell, model, iter, data) # Set defaults value = None @@ -174,9 +171,9 @@ class VariableEditor(gtk.VBox): # Block specific values if block: - if block.get_key() == 'import': + if block.key == 'import': value = block.get_param('import').get_value() - elif block.get_key() != "variable": + elif block.key != "variable": value = "<Open Properties>" sp('editable', False) sp('foreground', '#0D47A1') @@ -192,13 +189,13 @@ class VariableEditor(gtk.VBox): self.set_tooltip_text(error_message[-1]) else: # Evaluate and show the value (if it is a variable) - if block.get_key() == "variable": + if block.key == "variable": evaluated = str(block.get_param('value').evaluate()) self.set_tooltip_text(evaluated) # Always set the text value. sp('text', value) - def set_properties(self, col, cell, model, iter): + def set_properties(self, col, cell, model, iter, data): sp = cell.set_property block = model.get_value(iter, BLOCK_INDEX) # Set defaults @@ -208,7 +205,7 @@ class VariableEditor(gtk.VBox): # Block specific changes if block: - if not block.get_enabled(): + if not block.enabled: # Disabled block. But, this should still be editable sp('editable', True) sp('foreground', 'gray') @@ -217,19 +214,12 @@ class VariableEditor(gtk.VBox): if block.get_error_messages(): sp('foreground', 'red') - def update_gui(self): - if not self.get_flow_graph(): - return - self._update_blocks() + def update_gui(self, blocks): + self._imports = [block for block in blocks if block.is_import] + self._variables = [block for block in blocks if block.is_variable] self._rebuild() self.treeview.expand_all() - def _update_blocks(self): - self._imports = filter(attrgetter('is_import'), - self.get_flow_graph().blocks) - self._variables = filter(attrgetter('is_variable'), - self.get_flow_graph().blocks) - def _rebuild(self, *args): self.treestore.clear() imports = self.treestore.append(None, [None, 'Imports']) @@ -258,29 +248,29 @@ class VariableEditor(gtk.VBox): key presses or mouse clicks. Also triggers an update of the flow graph and editor. """ if key == self.ADD_IMPORT: - self.get_flow_graph().add_new_block('import') + self.emit('create_new_block', 'import') elif key == self.ADD_VARIABLE: - self.get_flow_graph().add_new_block('variable') + self.emit('create_new_block', 'variable') elif key == self.OPEN_PROPERTIES: Actions.BLOCK_PARAM_MODIFY(self._block) elif key == self.DELETE_BLOCK: - self.get_flow_graph().remove_element(self._block) + self.emit('remove_block', self._block.get_id()) elif key == self.DELETE_CONFIRM: if self._confirm_delete: # Create a context menu to confirm the delete operation - confirmation_menu = gtk.Menu() + confirmation_menu = Gtk.Menu() block_id = self._block.get_param('id').get_value().replace("_", "__") - confirm = gtk.MenuItem("Delete {}".format(block_id)) + confirm = Gtk.MenuItem(label="Delete {}".format(block_id)) confirm.connect('activate', self.handle_action, self.DELETE_BLOCK) confirmation_menu.add(confirm) confirmation_menu.show_all() - confirmation_menu.popup(None, None, None, event.button, event.time) + confirmation_menu.popup(None, None, None, None, event.button, event.time) else: self.handle_action(None, self.DELETE_BLOCK, None) elif key == self.ENABLE_BLOCK: - self._block.set_enabled(True) + self._block.state = 'enabled' elif key == self.DISABLE_BLOCK: - self._block.set_enabled(False) + self._block.state = 'disabled' Actions.VARIABLE_EDITOR_UPDATE() def _handle_mouse_button_press(self, widget, event): @@ -302,12 +292,12 @@ class VariableEditor(gtk.VBox): if event.button == 1 and col.get_name() == "value": # Make sure this has a block (not the import/variable rows) - if self._block and event.type == gtk.gdk._2BUTTON_PRESS: + if self._block and event.type == Gdk.EventType._2BUTTON_PRESS: # Open the advanced dialog if it is a gui variable - if self._block.get_key() not in ("variable", "import"): + if self._block.key not in ("variable", "import"): self.handle_action(None, self.OPEN_PROPERTIES, event=event) return True - if event.type == gtk.gdk.BUTTON_PRESS: + if event.type == Gdk.EventType.BUTTON_PRESS: # User is adding/removing blocks # Make sure this is the action cell (Add/Remove Icons) if path[2] > col.cell_get_position(self.action_cell)[0]: @@ -320,15 +310,15 @@ class VariableEditor(gtk.VBox): else: self.handle_action(None, self.DELETE_CONFIRM, event=event) return True - elif event.button == 3 and event.type == gtk.gdk.BUTTON_PRESS: + elif event.button == 3 and event.type == Gdk.EventType.BUTTON_PRESS: if self._block: - self._context_menu.update_sensitive(True, enabled=self._block.get_enabled()) + self._context_menu.update_sensitive(True, enabled=self._block.enabled) else: self._context_menu.update_sensitive(False) - self._context_menu.popup(None, None, None, event.button, event.time) + self._context_menu.popup(None, None, None, None, event.button, event.time) # Null handler. Stops the treeview from handling double click events. - if event.type == gtk.gdk._2BUTTON_PRESS: + if event.type == Gdk.EventType._2BUTTON_PRESS: return True return False @@ -345,10 +335,10 @@ class VariableEditor(gtk.VBox): def _handle_key_button_press(self, widget, event): model, path = self.treeview.get_selection().get_selected_rows() if path and self._block: - if self._block.get_enabled() and event.string == "d": + if self._block.enabled and event.string == "d": self.handle_action(None, self.DISABLE_BLOCK, None) return True - elif not self._block.get_enabled() and event.string == "e": + elif not self._block.enabled and event.string == "e": self.handle_action(None, self.ENABLE_BLOCK, None) return True return False diff --git a/grc/gui/external_editor.py b/grc/gui/external_editor.py index 76f21412b0..11d6fd7ebb 100644 --- a/grc/gui/external_editor.py +++ b/grc/gui/external_editor.py @@ -17,6 +17,8 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +from __future__ import absolute_import, print_function + import os import sys import time @@ -71,7 +73,7 @@ class ExternalEditor(threading.Thread): time.sleep(1) except Exception as e: - print >> sys.stderr, "file monitor crashed:", str(e) + print("file monitor crashed:", str(e), file=sys.stderr) else: # print "file monitor: done with", filename pass @@ -79,7 +81,7 @@ class ExternalEditor(threading.Thread): if __name__ == '__main__': def p(data): - print data + print(data) e = ExternalEditor('/usr/bin/gedit', "test", "content", p) e.open_editor() diff --git a/grc/main.py b/grc/main.py index 0edab40769..dc84e166d5 100755 --- a/grc/main.py +++ b/grc/main.py @@ -17,7 +17,12 @@ import argparse -import gtk +import gi +gi.require_version('Gtk', '3.0') +gi.require_version('PangoCairo', '1.0') + +from gi.repository import Gtk + from gnuradio import gr from .gui.Platform import Platform @@ -40,7 +45,7 @@ def main(): args = parser.parse_args() try: - gtk.window_set_default_icon(gtk.IconTheme().load_icon('gnuradio-grc', 256, 0)) + Gtk.window_set_default_icon(Gtk.IconTheme().load_icon('gnuradio-grc', 256, 0)) except: pass @@ -51,5 +56,5 @@ def main(): install_prefix=gr.prefix() ) ActionHandler(args.flow_graphs, platform) - gtk.main() + Gtk.main() diff --git a/grc/scripts/gnuradio-companion b/grc/scripts/gnuradio-companion index 34bb0bf110..8f267c8198 100755 --- a/grc/scripts/gnuradio-companion +++ b/grc/scripts/gnuradio-companion @@ -39,11 +39,11 @@ Is the library path environment variable set correctly? def die(error, message): msg = "{0}\n\n({1})".format(message, error) try: - import gtk - d = gtk.MessageDialog( - type=gtk.MESSAGE_ERROR, - buttons=gtk.BUTTONS_CLOSE, - message_format=msg, + from gi.repository import Gtk + d = Gtk.MessageDialog( + message_type=Gtk.MessageType.ERROR, + buttons=Gtk.ButtonsType.CLOSE, + text=msg, ) d.set_title(type(error).__name__) d.run() @@ -55,10 +55,12 @@ def die(error, message): def check_gtk(): try: warnings.filterwarnings("error") - import pygtk - pygtk.require('2.0') - import gtk - gtk.init_check() + import gi + gi.require_version('Gtk', '3.0') + gi.require_version('PangoCairo', '1.0') + + from gi.repository import Gtk + Gtk.init_check() warnings.filterwarnings("always") except Exception as err: die(err, "Failed to initialize GTK. If you are running over ssh, " |