def resizeEvent(self, event): """Resize the view range to match the widget size""" d = self.declaration widget = self.widget if d.auto_range: size = self.widget.size() view_range = QRectF(0, 0, size.width(), size.height()) # view_range = self.scene.itemsBoundingRect() else: # Return the boundaries of the view in scene coordinates r = QRectF(widget.rect()) view_range = widget.viewportTransform().inverted()[0].mapRect(r) self.set_view_range(view_range)
def update_scenerect(self, bbox=None): if bbox is not None: bb = QRectF(*bbox) else: bb = self.boundingRect() if self.widget is not None: self.widget.setSceneRect(bb)
def scale_view(self, x, y): """Scale the zoom but keep in in the min and max zoom bounds.""" d = self.declaration sx, sy = float(x), float(y) if d.lock_aspect_ratio: sy = sx view_range = self.view_range center = view_range.center() w, h = view_range.width() / sx, view_range.height() / sy cx, cy = center.x(), center.y() x = cx - (cx - view_range.left()) / sx y = cy - (cy - view_range.top()) / sy view_range = QRectF(x, y, w, h) self.set_view_range(view_range) return view_range
def __init__(self, e, ids=None, parent=False): """ Creates a QtPainterPath from an SVG document applying all transforms. Does NOT include any styling. Parameters ---------- e: Element or string An lxml etree.Element or an argument to pass to etree.parse() ids: List List of node ids to include. If not given all will be used. """ is_etree = isinstance(e, EtreeElement) self.isParentSvg = parent or not is_etree if self.isParentSvg: if not is_etree: self._doc = etree.parse(e) e = self._svg = self._doc.getroot() if ids: nodes = set() xpath = self._svg.xpath for node_id in ids: nodes.update(set(xpath('//*[@id="%s"]' % node_id))) # Find all nodes and their parents valid_nodes = set() for node in nodes: valid_nodes.add(node) parent = node.getparent() while parent: valid_nodes.add(parent) parent = parent.getparent() self._nodes = valid_nodes self.viewBox = QRectF(0, 0, -1, -1) super(QtSvgDoc, self).__init__(e, self._nodes)
def get_items_in(self, top_left, bottom_right): qrect = QRectF(QPointF(top_left.x, top_left.y), QPointF(bottom_right.x, bottom_right.y)) items = self.scene.items(qrect) return [item.ref().declaration for item in items if item.ref()]
def boundingRect(self): return QRectF(-self.width // 2, -self.height // 2, self.width, self.height)
def request_relayout(self): # y = 0.0 # for child in self.children(): # if not isinstance(child, QtContainer): # continue # scene_proxy = self._proxies[child] # width, height = child._layout_manager.best_size() # scene_proxy.setPos(0.0, y) # y += height + 25.0 for p in self._edge_paths: self.scene.removeItem(p) self._edge_paths = [] g = pygraphviz.AGraph(directed=True) g.graph_attr['nodesep'] = 100 g.graph_attr['ranksep'] = 50 g.node_attr['shape'] = 'rect' children_names = {child.declaration.name for child in self.children() if isinstance(child, QtContainer)} if any(from_ not in children_names or to not in children_names for (from_, to) in self.declaration.edges): # hasn't finished being set up yet return for child in self.children(): if not isinstance(child, QtContainer): continue scene_proxy = self._proxy(child) width, height = child._layout_manager.best_size() scene_proxy.setGeometry(QRectF(0.0, 0.0, width, height)) g.add_node(child.declaration.name, width=width, height=height) for from_, to in self.declaration.edges: g.add_edge(from_, to) before = time.time() g.layout(prog='dot') after = time.time() for child in self.children(): if not isinstance(child, QtContainer): continue scene_proxy = self._proxies[child] node = g.get_node(child.declaration.name) center_x, center_y = (-float(v)/72.0 for v in node.attr['pos'].split(',')) width, height = child._layout_manager.best_size() x = center_x - (width / 2.0) y = center_y - (height / 2.0) scene_proxy.setPos(x, y) for from_, to in self.declaration.edges: if from_ not in children_names or to not in children_names: continue edge = g.get_edge(from_, to) # TODO: look at below code all_points = [tuple(-float(v)/72.0 for v in t.strip('e,').split(',')) for t in edge.attr['pos'].split(' ')] arrow = all_points[0] start_point = all_points[1] painter = QPainterPath(QPointF(*start_point)) for c1, c2, end in grouper(all_points[2:], 3): painter.cubicTo(QPointF(*c1), QPointF(*c2), QPointF(*end)) self._edge_paths.append(self.scene.addPath(painter)) self.show_selected()