diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2016-06-15 11:11:37 -0700 |
---|---|---|
committer | Sebastian Koslowski <koslowski@kit.edu> | 2016-07-13 16:33:28 +0200 |
commit | c39aa154ff7a7787b91e9c09862016afb9874254 (patch) | |
tree | 5f136d1dd921adf183ad67ca0c6d41c1c7b56df6 | |
parent | 893b74c770b81f2c09094577e2de720155f84b61 (diff) |
grc-refactor: move more port type color stuff to gui
-rw-r--r-- | grc/core/Constants.py | 56 | ||||
-rw-r--r-- | grc/core/Platform.py | 3 | ||||
-rw-r--r-- | grc/core/Port.py | 26 | ||||
-rw-r--r-- | grc/gui/Colors.py | 39 | ||||
-rw-r--r-- | grc/gui/Connection.py | 20 | ||||
-rw-r--r-- | grc/gui/Dialogs.py | 4 | ||||
-rw-r--r-- | grc/gui/Port.py | 65 |
7 files changed, 104 insertions, 109 deletions
diff --git a/grc/core/Constants.py b/grc/core/Constants.py index 992d5e7d83..99531487fd 100644 --- a/grc/core/Constants.py +++ b/grc/core/Constants.py @@ -99,41 +99,33 @@ 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), - ('Message Queue', 'msg', 0, GRC_COLOR_DARK_GREY), - ('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), + ('Message Queue', 'msg', 0, GRC_COLOR_DARK_GREY), + ('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 six.iteritems(ALIAS_TYPES): - TYPE_TO_COLOR[key] = color - TYPE_TO_SIZEOF[key] = sizeof +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/Platform.py b/grc/core/Platform.py index be7b60ca59..844693d14b 100644 --- a/grc/core/Platform.py +++ b/grc/core/Platform.py @@ -310,6 +310,3 @@ class Platform(Element): def get_new_block(self, flow_graph, key): return self.Block(flow_graph, n=self._blocks_n[key]) - - def get_colors(self): - return [(name, color) for name, key, sizeof, color in Constants.CORE_TYPES] diff --git a/grc/core/Port.py b/grc/core/Port.py index 9a33c5c506..4ba516d5fe 100644 --- a/grc/core/Port.py +++ b/grc/core/Port.py @@ -263,32 +263,6 @@ class Port(Element): 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) diff --git a/grc/gui/Colors.py b/grc/gui/Colors.py index 6ee31e5e18..157c07ea35 100644 --- a/grc/gui/Colors.py +++ b/grc/gui/Colors.py @@ -19,12 +19,37 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 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) + # print(dir(cairo.SolidPattern)) + # cairo_pattern = cairo.SolidPattern( + # red=color.red, + # green=color.green, + # blue=color.blue, + # alpha=color.alpha + # ) + # return cairo_pattern + 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('#444444') @@ -49,18 +74,18 @@ CONNECTION_ERROR_COLOR = get_color('#FF0000') DEFAULT_DOMAIN_COLOR = get_color('#777777') + ################################################################################# -# param box colors +# port colors ################################################################################# -from gi.repository import Gdk +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()) -def _color_parse(color_code): - color = Gdk.RGBA() - color.parse(color_code) - return color - +################################################################################# +# param box colors +################################################################################# COMPLEX_COLOR_SPEC = _color_parse('#3399FF') FLOAT_COLOR_SPEC = _color_parse('#FF8C69') INT_COLOR_SPEC = _color_parse('#00FF99') diff --git a/grc/gui/Connection.py b/grc/gui/Connection.py index d893060aa6..b1a22e3949 100644 --- a/grc/gui/Connection.py +++ b/grc/gui/Connection.py @@ -45,6 +45,9 @@ class Connection(Element, _Connection): # can't use Colors.CONNECTION_ENABLED_COLOR here, might not be defined (grcc) self._bg_color = self._arrow_color = self._color = None + self._sink_rot = self._source_rot = None + self._sink_coor = self._source_coor = None + def get_coordinate(self): """ Get the 0,0 coordinate. @@ -74,12 +77,12 @@ class Connection(Element, _Connection): self._source_coor = None #get the source coordinate try: - connector_length = self.source_port.get_connector_length() + connector_length = self.source_port.connector_length except: - return + return # todo: why? self.x1, self.y1 = Utils.get_rotated_coordinate((connector_length, 0), self.source_port.get_rotation()) #get the sink coordinate - connector_length = self.sink_port.get_connector_length() + CONNECTOR_ARROW_HEIGHT + connector_length = self.sink_port.connector_length + CONNECTOR_ARROW_HEIGHT self.x2, self.y2 = Utils.get_rotated_coordinate((-connector_length, 0), self.sink_port.get_rotation()) #build the arrow self.arrow = [(0, 0), @@ -106,7 +109,7 @@ class Connection(Element, _Connection): def _update_after_move(self): """Calculate coordinates.""" - self.clear() #FIXME do i want this here? + self.clear() #source connector source = self.source_port X, Y = source.get_connector_coordinate() @@ -118,11 +121,11 @@ class Connection(Element, _Connection): 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] + 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 + 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() @@ -155,12 +158,13 @@ class Connection(Element, _Connection): sink = self.sink_port source = self.source_port #check for changes - if self._sink_rot != sink.get_rotation() or self._source_rot != source.get_rotation(): self.create_shapes() + 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 + return # todo: why? #cache values self._sink_rot = sink.get_rotation() self._source_rot = source.get_rotation() diff --git a/grc/gui/Dialogs.py b/grc/gui/Dialogs.py index 8f0f60d764..da271fe46e 100644 --- a/grc/gui/Dialogs.py +++ b/grc/gui/Dialogs.py @@ -27,7 +27,7 @@ from gi.repository import GObject import sys from distutils.spawn import find_executable -from . import Utils, Actions +from . import Utils, Actions, Constants from ..core import Messages @@ -207,7 +207,7 @@ def HelpDialog(): MessageDialogHelper( def TypesDialog(platform): - colors = platform.get_colors() + 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( diff --git a/grc/gui/Port.py b/grc/gui/Port.py index 8c4500f960..62086c76e4 100644 --- a/grc/gui/Port.py +++ b/grc/gui/Port.py @@ -23,15 +23,10 @@ import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, PangoCairo -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 . import Actions, Colors, Utils, Constants from .Element import Element -from ..core.Constants import DEFAULT_DOMAIN, GR_MESSAGE_DOMAIN +from ..core.Constants import DEFAULT_DOMAIN, GR_MESSAGE_DOMAIN from ..core.Port import Port as _Port @@ -45,24 +40,42 @@ class Port(_Port, Element): """ _Port.__init__(self, block, n, dir) Element.__init__(self) - self.W = self.w = self.h = 0 - self.H = 20 # todo: fix self._connector_coordinate = (0, 0) - self._connector_length = 0 self._hovering = True self._force_label_unhidden = False + self._bg_color = (0, 0, 0) + + self.W = self.w = self.h = 0 + self.H = 20 # todo: fix + self.connector_length = 0 + self.layout = Gtk.DrawingArea().create_pango_layout('') - self._bg_color = Colors.get_color(self.get_color()) + + 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. + """ + color = Colors.PORT_TYPE_TO_COLOR[self.get_type()] + 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) + return color def create_shapes(self): """Create new areas and labels for the port.""" Element.create_shapes(self) - self._bg_color = Colors.get_color(self.get_color()) + self._bg_color = self._get_color() + if self.get_hide(): return # this port is hidden, no need to create shapes - if self.get_domain() == GR_MESSAGE_DOMAIN: + if self.get_domain() == Constants.GR_MESSAGE_DOMAIN: pass - elif self.get_domain() != DEFAULT_DOMAIN: + elif self.get_domain() != Constants.DEFAULT_DOMAIN: self.line_attributes[0] = 2 #get current rotation rotation = self.get_rotation() @@ -71,22 +84,22 @@ class Port(_Port, Element): if self.is_source else self.parent.get_sinks_gui() ports = [p for p in ports if not p.get_hide()] #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 + self.W = max([port.W for port in ports] + [Constants.PORT_MIN_WIDTH]) + W = self.W if not self._label_hidden() else Constants.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 + del self.connector_length return #reverse the order of ports for these rotations if rotation in (180, 270): index = len(ports)-index-1 - port_separation = PORT_SEPARATION \ + port_separation = Constants.PORT_SEPARATION \ if not self.parent.has_busses[self.is_source] \ - else max([port.H for port in ports]) + PORT_SPACING + else max([port.H for port in ports]) + Constants.PORT_SPACING offset = (self.parent.H - (len(ports)-1)*port_separation - self.H)/2 #create areas and connector coordinates @@ -111,12 +124,12 @@ class Port(_Port, Element): 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 + self.connector_length = Constants.CONNECTOR_EXTENSION_MINIMAL + Constants.CONNECTOR_EXTENSION_INCREMENT * index def create_labels(self): """Create the labels for the socket.""" self.layout.set_markup("""<span foreground="black" font_desc="{font}">{name}</span>""".format( - name=Utils.encode(self.get_name()), font=PORT_FONT + name=Utils.encode(self.get_name()), font=Constants.PORT_FONT )) def draw(self, widget, cr): @@ -169,16 +182,6 @@ class Port(_Port, Element): if self.is_source: return self.get_rotation() elif self.is_sink: return (self.get_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. |