def __load_element_visual(self, diagram, node): position = Point(int(node.attrib["x"]), int(node.attrib["y"])) size = Size(int(node.attrib["width"]), int(node.attrib["height"])) element = self.__element_map[UUID(node.attrib["id"])] visual = diagram.show(element) visual.move(self.__ruler, position) visual.resize(self.__ruler, size)
def draw_for(self, canvas, visual): if visual not in self.__selected: return if isinstance(visual, ElementVisual): bounds = visual.get_bounds(canvas.get_ruler()) if len(self.__selected) == 1: for pos_x, pos_y in self.__get_selection_points_positions( visual): self.__draw_selection_point(canvas, bounds, pos_x, pos_y) connection_icon_position = bounds.top_right + self.CONNECTION_ICON_SHIFT connection_icon = self.CONNECTION_ICON.transform( Transformation.make_translate(connection_icon_position)) canvas.draw_path(connection_icon, fg=self.ICON_COLOR, bg=self.ICON_COLOR_BACKGROUND, line_width=2) canvas.draw_rectangle(bounds, fg=self.SELECTION_COLOR, line_width=self.SELECTION_SIZE) elif isinstance(visual, ConnectionVisual): if len(self.__selected) == 1: fg_color = None bg_color = self.SELECTION_POINT_COLOR else: fg_color = self.SELECTION_POINT_COLOR bg_color = None for point in visual.get_points(canvas.get_ruler()): canvas.draw_rectangle(Rectangle.from_point_size( point - Vector(self.SELECTION_POINT_SIZE // 2, self.SELECTION_POINT_SIZE // 2), Size(self.SELECTION_POINT_SIZE, self.SELECTION_POINT_SIZE)), fg=fg_color, bg=bg_color) for label in visual.get_labels(): bounds = label.get_bounds(canvas.get_ruler()) if bounds.width and bounds.height: canvas.draw_rectangle(bounds, fg=self.SELECTION_COLOR, line_width=self.SELECTION_SIZE) line_to_connection = Line.from_point_point( label.get_nearest_point(canvas.get_ruler()), bounds.center) intersect = list(bounds.intersect(line_to_connection)) if intersect and ( line_to_connection.first - intersect[0] ).length > self.LABEL_LINE_MINIMAL_DISTANCE: canvas.draw_line(line_to_connection.first, intersect[0], fg=self.SELECTION_COLOR, line_width=self.SELECTION_SIZE, line_style=LineStyle.dot)
def __init__(self, child, fill, border): self.__child = child self.__fill = fill self.__border = border self.__rectangle = None if child is None: self.__child_size = Size(0, 0) else: self.__child_size = child.get_minimal_size()
def measure_text(self, font, text): qfont = QFont(font.family) qfont.setPixelSize(font.size) qfont.setBold(FontStyle.bold in font.style) qfont.setItalic(FontStyle.italic in font.style) qfont.setStrikeOut(FontStyle.strike in font.style) qfont.setUnderline(FontStyle.underline in font.style) metrics = QFontMetrics(qfont) size = metrics.size(0, text) return Size(size.width(), size.height())
def _create_object(self, context, ruler): width = self.__width(context) height = self.__height(context) size = Size(width, height) children = [ child.create_graphical_object(local, ruler, size) for local, child in self._get_children(context) ] return GraphicsObject(children)
def create_graphical_object(self, context, ruler, size): builder = PathBuilder() for local, part in self._get_children(context): part.add_to_path(local, builder) path = builder.build().transform( Transformation.make_scale2(Size(1 / size.width, 1 / size.height))) fill = self.__fill(context) border = self.__border(context) return PathObject(path, fill, border)
def __init__(self, child, fill, border, corners, sides): self.__child = child self.__fill = fill self.__border = border self.__path = None self.__ornaments_path = None self.__corners = corners self.__sides = sides if child is None: self.__child_size = Size(0, 0) else: self.__child_size = child.get_minimal_size()
def __init__(self, child, minwidth, maxwidth, minheight, maxheight, width, height): self.__child = child self.__minwidth = minwidth self.__maxwidth = maxwidth self.__minheight = minheight self.__maxheight = maxheight self.__width = width self.__height = height if child is None: self.__child_size = Size(0, 0) else: self.__child_size = child.get_minimal_size()
def __create_appearance_object(self, ruler): self.__cached_appearance = self.__object.create_appearance_object( ruler) self.__cached_appearance.move(self.__position) min_size = self.__cached_appearance.get_minimal_size() if self.__size is None: self.__size = min_size else: undersize_width = self.__size.width < min_size.width undersize_height = self.__size.height < min_size.height resizable_x, resizable_y = self.__cached_appearance.is_resizable() if (undersize_width and undersize_height) or (not resizable_x and not resizable_y): self.__size = min_size else: if undersize_width or not resizable_x: self.__size = Size(min_size.width, self.__size.height) elif undersize_height or not resizable_y: self.__size = Size(self.__size.width, min_size.height) self.__cached_appearance.resize(self.__size)
def load(self): definitions = { "ArrowDefinition": {}, "CornerDefinition": {}, "SideDefinition": {}, } for child in self.__xmlroot: if child.tag == "{{{0}}}ArrowDefinition".format(ADDON_NAMESPACE): definition = ArrowDefinition( child.attrib["id"], PathBuilder().from_string(child.attrib["path"]).build(), Point.parse(child.attrib["center"]), self.__parse_rotation(child.attrib["rotation"]) ) definitions["ArrowDefinition"][definition.id] = definition elif child.tag == "{{{0}}}CornerDefinition".format(ADDON_NAMESPACE): ornament = None if "ornament" in child.attrib: ornament = PathBuilder().from_string(child.attrib["ornament"]).build() definition = CornerDefinition( child.attrib["id"], PathBuilder().from_string(child.attrib["path"]).build(), ornament, Point.parse(child.attrib["center"]), child.attrib["corner"] ) definitions["CornerDefinition"][definition.id] = definition elif child.tag == "{{{0}}}SideDefinition".format(ADDON_NAMESPACE): ornament = None if "ornament" in child.attrib: ornament = PathBuilder().from_string(child.attrib["ornament"]).build() definition = SideDefinition( child.attrib["id"], PathBuilder().from_string(child.attrib["path"]).build(), ornament, Point.parse(child.attrib["center"]), Size(int(child.attrib["width"]), int(child.attrib["height"])), child.attrib["side"] ) definitions["SideDefinition"][definition.id] = definition return definitions
def assign_bounds(self, bounds): corner_and_side_positions = [ (bounds.top_left, bounds.left_center, bounds.height), (bounds.top_right, bounds.top_center, bounds.width), (bounds.bottom_right, bounds.right_center, bounds.height), (bounds.bottom_left, bounds.bottom_center, bounds.width) ] path = PathBuilder() ornaments = PathBuilder() for no, (corner_point, side_point, side_size) in enumerate(corner_and_side_positions): rotation = -math.pi * no / 2 corner = self.__corners[no] side = self.__sides[no] next_side = self.__sides[(no + 1) % 4] if corner is not None: transformation = Transformation.make_translate( corner_point) * Transformation.make_rotation(-rotation) path.from_path(corner.path.transform(transformation), True) if corner.ornament is not None: ornaments.from_path( corner.ornament.transform(transformation)) elif side is not None: transformation = Transformation.make_translate(side_point) * Transformation.make_rotation(-rotation) *\ Transformation.make_scale2(Size(1, side_size / side.size.height)) path.from_path(side.path.transform(transformation), True) if side.ornament is not None: ornaments.from_path( side.ornament.transform(transformation)) elif next_side is None: path.move_or_line_to(corner_point) path.close() self.__path = path.build() self.__ornaments_path = ornaments.build() if self.__child is not None: self.__child.assign_bounds(bounds)
def get_minimal_size(self): w = self.__child_size.width h = self.__child_size.height if self.__width is not None: w = self.__width else: if self.__minwidth is not None and w < self.__minwidth: w = self.__minwidth if self.__maxwidth is not None and w > self.__maxwidth: w = self.__maxwidth if self.__height is not None: h = self.__height else: if self.__minheight is not None and h < self.__minheight: h = self.__minheight if self.__maxheight is not None and w > self.__maxheight: h = self.__maxheight return Size(w, h)
def __get_size(self, node): return Size(int(node.attrib.get("width", 0)), int(node.attrib.get("height", 0)))
def _compute_size(self, all_widths, all_heights): return Size(sum(all_widths), max(all_heights))
def get_minimal_size(self): if self.__orientation == LineOrientation.vertical: return Size(0, 1) else: return Size(1, 0)
def get_minimal_size(self): if self.__children_sizes: return self._compute_size((s.width for s in self.__children_sizes), (s.height for s in self.__children_sizes)) else: return Size(0, 0)
def __show_element(self, ruler, diagram, element, data): visual = diagram.show(element) visual.move(ruler, Point(data['x'], data['y'])) visual.resize(ruler, Size(data['width'], data['height'])) return visual
def get_minimal_size(self): return Size(0, 0)
def get_minimal_size(self): return Size(self.__child_size.width + self.__left + self.__right, self.__child_size.height + self.__top + self.__bottom)
def _new_size(self, size, whole_size, delta): return Size(size.width + delta, whole_size.height)
def get_minimal_size(self): return Size(sum(self.__columns), sum(self.__rows))
def measure_image(self, image): pixmap = image_loader.load(image) size = pixmap.size() return Size(size.width(), size.height())