def _rotate_context(context, direction): """Moves the current position to 'position' and rotates the context according to 'direction' :param context: Cairo context :param direction: Direction enum """ if direction is Direction.UP: pass elif direction is Direction.RIGHT: context.rotate(deg2rad(90)) elif direction is Direction.DOWN: context.rotate(deg2rad(180)) elif direction is Direction.LEFT: context.rotate(deg2rad(-90))
def _draw_rectangle_path(self, context, width, height, only_get_extents=False): """Draws the rectangle path for the port The rectangle is correctly rotated. Height therefore refers to the border thickness and width to the length of the port. :param context: The context to draw on :param float width: The width of the rectangle :param float height: The height of the rectangle """ c = context # Current position is the center of the rectangle c.save() if self.side is SnappedSide.LEFT or self.side is SnappedSide.RIGHT: c.rotate(deg2rad(90)) c.rel_move_to(-width / 2., -height / 2.) c.rel_line_to(width, 0) c.rel_line_to(0, height) c.rel_line_to(-width, 0) c.close_path() c.restore() if only_get_extents: extents = c.path_extents() c.new_path() return extents
def draw_name(self, context, transparency, only_calculate_size=False): """Draws the name of the port Offers the option to only calculate the size of the name. :param context: The context to draw on :param transparency: The transparency of the text :param only_calculate_size: Whether to only calculate the size :return: Size of the name :rtype: float, float """ c = context c.set_antialias(cairo.ANTIALIAS_SUBPIXEL) side_length = self.port_side_size layout = c.create_layout() font_name = constants.INTERFACE_FONT font_size = gap_draw_helper.FONT_SIZE font = FontDescription(font_name + " " + str(font_size)) layout.set_font_description(font) layout.set_text(self.name) ink_extents, logical_extents = layout.get_extents() extents = [extent / float(SCALE) for extent in logical_extents] real_name_size = extents[2], extents[3] desired_height = side_length * 0.75 scale_factor = real_name_size[1] / desired_height # Determine the size of the text, increase the width to have more margin left and right of the text margin = side_length / 4. name_size = real_name_size[0] / scale_factor, desired_height name_size_with_margin = name_size[0] + margin * 2, name_size[ 1] + margin * 2 # Only the size is required, stop here if only_calculate_size: return name_size_with_margin # Current position is the center of the port rectangle c.save() if self.side is SnappedSide.RIGHT or self.side is SnappedSide.LEFT: c.rotate(deg2rad(-90)) c.rel_move_to(-name_size[0] / 2, -name_size[1] / 2) c.scale(1. / scale_factor, 1. / scale_factor) c.rel_move_to(-extents[0], -extents[1]) c.set_source_rgba( *gap_draw_helper.get_col_rgba(self.text_color, transparency)) c.update_layout(layout) c.show_layout(layout) c.restore() return name_size_with_margin
def draw_port_label(context, port, transparency, fill, label_position, show_additional_value=False, additional_value=None, only_extent_calculations=False): """Draws a normal label indicating the port name. :param context: Draw Context :param port: The PortView :param transparency: Transparency of the text :param fill: Whether the label should be filled or not :param label_position: Side on which the label should be drawn :param show_additional_value: Whether to show an additional value (for data ports) :param additional_value: The additional value to be shown :param only_extent_calculations: Calculate only the extends and do not actually draw """ c = context c.set_antialias(ANTIALIAS_SUBPIXEL) text = port.name label_color = get_col_rgba(port.fill_color, transparency) text_color = port.text_color port_height = port.port_size[1] port_position = c.get_current_point() layout = c.create_layout() layout.set_text(text) font_name = constants.INTERFACE_FONT font = FontDescription(font_name + " " + str(FONT_SIZE)) layout.set_font_description(font) ink_extents, logical_extents = layout.get_extents() extents = [extent / float(SCALE) for extent in logical_extents] real_text_size = extents[2], extents[3] desired_height = port_height scale_factor = real_text_size[1] / desired_height # margin is the distance between the text and the border line margin = desired_height / 2.5 arrow_height = desired_height # The real_text_size dimensions are rotated by 90 deg compared to the label, as the label is drawn upright text_size = desired_height, real_text_size[0] / scale_factor, text_size_with_margin = text_size[0] + 2 * margin, text_size[1] + 2 * margin + arrow_height port_distance = desired_height port_offset = desired_height / 2. if label_position is SnappedSide.RIGHT: label_angle = deg2rad(-90) text_angle = 0 elif label_position is SnappedSide.BOTTOM: label_angle = 0 text_angle = deg2rad(-90) elif label_position is SnappedSide.LEFT: label_angle = deg2rad(90) text_angle = 0 else: # label_position is SnappedSide.TOP: label_angle = deg2rad(180) text_angle = deg2rad(90) # Draw (filled) outline of label c.move_to(*port_position) c.save() c.rotate(label_angle) draw_label_path(c, text_size_with_margin[0], text_size_with_margin[1], arrow_height, port_distance, port_offset) c.restore() c.set_line_width(port_height * .03) c.set_source_rgba(*label_color) label_extents = c.stroke_extents() if label_extents[0] == 0: label_extents = c.fill_extents() if only_extent_calculations: c.new_path() else: if fill: c.fill_preserve() c.stroke() # Move to the upper left corner of the desired text position c.save() c.move_to(*port_position) c.rotate(label_angle) c.rel_move_to(0, port_distance + arrow_height + 2 * margin) c.scale(1. / scale_factor, 1. / scale_factor) c.rel_move_to(-real_text_size[1] / 2 - extents[1], real_text_size[0] - extents[0]) c.restore() # Show text in correct orientation c.save() c.rotate(text_angle) c.scale(1. / scale_factor, 1. / scale_factor) # Correction for labels positioned right: as the text is mirrored, the anchor point must be moved if label_position is SnappedSide.RIGHT: c.rel_move_to(-real_text_size[0], -real_text_size[1]) c.set_source_rgba(*get_col_rgba(text_color, transparency)) c.update_layout(layout) c.show_layout(layout) c.restore() if show_additional_value: value_text = limit_value_string_length(additional_value) value_layout = c.create_layout() value_layout.set_text(value_text) value_layout.set_font_description(font) ink_extents, logical_extents = value_layout.get_extents() extents = [extent / float(SCALE) for extent in logical_extents] value_text_size = extents[2], real_text_size[1] # Move to the upper left corner of the additional value box c.save() c.move_to(*port_position) c.rotate(label_angle) c.rel_move_to(-text_size_with_margin[0] / 2., text_size_with_margin[1] + port_distance) # Draw rectangular path c.rel_line_to(text_size_with_margin[0], 0) c.rel_line_to(0, value_text_size[0] / scale_factor + 2 * margin) c.rel_line_to(-text_size_with_margin[0], 0) c.close_path() c.restore() value_extents = c.stroke_extents() if only_extent_calculations: c.new_path() else: # Draw filled outline c.set_source_rgba(*get_col_rgba(gui_config.gtk_colors['DATA_VALUE_BACKGROUND'])) c.fill_preserve() c.set_source_color(gui_config.gtk_colors['BLACK']) c.stroke() # Move to the upper left corner of the desired text position c.save() c.move_to(*port_position) c.rotate(label_angle) c.rel_move_to(0, margin + text_size_with_margin[1] + port_distance) c.scale(1. / scale_factor, 1. / scale_factor) c.rel_move_to(-real_text_size[1] / 2., value_text_size[0]) c.restore() # Show text in correct orientation c.save() c.rotate(text_angle) c.scale(1. / scale_factor, 1. / scale_factor) # Correction for labels positioned right: as the text is mirrored, the anchor point must be moved if label_position is SnappedSide.RIGHT: c.rel_move_to(-value_text_size[0] - margin * scale_factor, -real_text_size[1]) c.set_source_rgba(*get_col_rgba(gui_config.gtk_colors['SCOPED_VARIABLE_TEXT'])) c.update_layout(value_layout) c.show_layout(value_layout) c.restore() label_extents = min(label_extents[0], value_extents[0]), min(label_extents[1], value_extents[1]), \ max(label_extents[2], value_extents[2]), max(label_extents[3], value_extents[3]) return label_extents