diff options
Diffstat (limited to 'grc/gui/Port.py')
-rw-r--r-- | grc/gui/Port.py | 105 |
1 files changed, 47 insertions, 58 deletions
diff --git a/grc/gui/Port.py b/grc/gui/Port.py index 6314b7ede8..8c4500f960 100644 --- a/grc/gui/Port.py +++ b/grc/gui/Port.py @@ -17,23 +17,23 @@ 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 math +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 + CONNECTOR_EXTENSION_INCREMENT, PORT_LABEL_PADDING, PORT_MIN_WIDTH, + PORT_LABEL_HIDDEN_WIDTH, PORT_FONT ) from .Element import Element from ..core.Constants import DEFAULT_DOMAIN, GR_MESSAGE_DOMAIN 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.""" @@ -45,15 +45,19 @@ class Port(_Port, Element): """ _Port.__init__(self, block, n, dir) Element.__init__(self) - self.W = self.H = self.w = self.h = 0 + 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.layout = Gtk.DrawingArea().create_pango_layout('') + self._bg_color = Colors.get_color(self.get_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()) if self.get_hide(): return # this port is hidden, no need to create shapes if self.get_domain() == GR_MESSAGE_DOMAIN: @@ -63,9 +67,9 @@ class Port(_Port, Element): #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) + ports = self.parent.get_sources_gui() \ + 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 @@ -76,16 +80,15 @@ class Port(_Port, Element): 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 + index = len(ports)-index-1 port_separation = PORT_SEPARATION \ - if not self.get_parent().has_busses[self.is_source] \ + if not self.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 + offset = (self.parent.H - (len(ports)-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 @@ -93,7 +96,7 @@ class Port(_Port, Element): 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 + x = self.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) @@ -103,7 +106,7 @@ class Port(_Port, Element): 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 + y = self.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) @@ -112,50 +115,36 @@ class Port(_Port, Element): 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): + self.layout.set_markup("""<span foreground="black" font_desc="{font}">{name}</span>""".format( + name=Utils.encode(self.get_name()), font=PORT_FONT + )) + + def draw(self, widget, 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, + border_color = ( + Colors.HIGHLIGHT_COLOR if self.is_highlighted() else + Colors.MISSING_BLOCK_BORDER_COLOR if self.parent.is_dummy_block else + Colors.BORDER_COLOR ) + Element.draw(self, widget, cr, border_color, self._bg_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 + (x, y), _ = self._areas_list[0] + cr.set_source_rgb(*self._bg_color) + cr.save() 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) + cr.translate(x + X + (self.W - self.w) / 2, y + Y + (self.H - self.h) / 2) 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) + cr.translate(x + X + (self.H - self.h) / 2, y + Y + (self.W - self.w) / 2) + cr.rotate(-90 * math.pi / 180.) + cr.translate(-self.w, 0) + PangoCairo.update_layout(cr, self.layout) + PangoCairo.show_layout(cr, self.layout) + cr.restore() def get_connector_coordinate(self): """ @@ -197,7 +186,7 @@ class Port(_Port, Element): Returns: the parent's rotation """ - return self.get_parent().get_rotation() + return self.parent.get_rotation() def move(self, delta_coor): """ @@ -206,7 +195,7 @@ class Port(_Port, Element): Args: delta_corr: the (delta_x, delta_y) tuple """ - self.get_parent().move(delta_coor) + self.parent.move(delta_coor) def rotate(self, direction): """ @@ -215,7 +204,7 @@ class Port(_Port, Element): Args: direction: degrees to rotate """ - self.get_parent().rotate(direction) + self.parent.rotate(direction) def get_coordinate(self): """ @@ -224,7 +213,7 @@ class Port(_Port, Element): Returns: the parents coordinate """ - return self.get_parent().get_coordinate() + return self.parent.get_coordinate() def set_highlighted(self, highlight): """ @@ -233,7 +222,7 @@ class Port(_Port, Element): Args: highlight: true to enable highlighting """ - self.get_parent().set_highlighted(highlight) + self.parent.set_highlighted(highlight) def is_highlighted(self): """ @@ -242,7 +231,7 @@ class Port(_Port, Element): Returns: the parent's highlighting status """ - return self.get_parent().is_highlighted() + return self.parent.is_highlighted() def _label_hidden(self): """ |