Example #1
0
    def _adjust_html_node_size(self, node):
        if node.get_name() in ('graph', 'node', 'empty'):
            return node

        label = node.get_label()

        if label is None:
            return node

        label = label.strip('\"')

        if not label.startswith('<') and not label.endswith('>'):
            return node

        text = QGraphicsTextItem()
        text.setHtml(label)

        bounding_box = text.boundingRect()
        width = bounding_box.width() / POINTS_PER_INCH
        height = bounding_box.height() / POINTS_PER_INCH

        url = node.get_attributes().get('URL', None)

        return self._create_fixed_size_node(node.get_name(), label, str(width),
                                            str(height), url)
Example #2
0
    def __init__(self,
                 bounding_box,
                 shape,
                 label,
                 label_pos=None,
                 url=None,
                 parent=None,
                 **kwargs):
        super(DmgHtmlNodeItem, self).__init__(parent, **kwargs)
        self.url = url
        self._incoming_edges = set()
        self._outgoing_edges = set()
        self._brush = QBrush(self._color)

        self._label_pen = QPen()
        self._label_pen.setColor(self._color)
        self._label_pen.setJoinStyle(Qt.RoundJoin)
        self._label_pen.setWidthF(self._label_pen_width)

        self._graphics_item_pen = QPen(self._label_pen)
        self._graphics_item_pen.setWidthF(self._pen_width)

        self._label = QGraphicsTextItem()
        self._label.setHtml(label)

        label_rectangle = self._label.boundingRect()
        if label_pos is None:
            label_rectangle.moveCenter(bounding_box.center())
        else:
            label_rectangle.moveCenter(label_pos)
        self._label.setPos(label_rectangle.x(), label_rectangle.y())

        self.addToGroup(self._label)

        self._graphics_item = ShapeFactory.create(shape, bounding_box)
        if ShapeFactory.message is not None:
            print ShapeFactory.message
        self.addToGroup(self._graphics_item)

        self._brush.setColor(self._color)
        self._graphics_item_pen.setColor(self._color)
        self._label_pen.setColor(self._color)

        self._graphics_item.setPen(self._graphics_item_pen)

        self._highlight_level = kwargs.get('highlight_level',
                                           self.HIGHLIGHT_LEVEL)
        self._hovered_color = kwargs.get('hovered_color', self.HOVERED_COLOR)
        self._highlighted_color = kwargs.get('highlighted_color',
                                             self.HIGHLIGHTED_COLOR)
        self._highlighted_pen_width = kwargs.get('highlighted_pen_width',
                                                 self.HIGHLIGHTED_PEN_WIDTH)
        self._highlighted_label_pen_width = kwargs.get(
            'highlighted_label_pen_width', self.HIGHLIGHTED_LABEL_PEN_WIDTH)

        self.hover_shape = None
        self.setAcceptHoverEvents(True)
Example #3
0
    def _handle_add_clicked(self):
        name = self.name_edit.text()
        if len(name) != 0:
            # remove and re-draw it
            if name in self.places_dict.keys():
                self.places_dict.pop(name)
                for item in self._sceneItems[name].keys():
                    self._scene.removeItem(self._sceneItems[name][item])
            try:
                # storing the values in the dict
                x = self.x_spin.value()
                y = self.y_spin.value()
                theta = self.theta_spin.value()
                q = quaternion_from_euler(0.0, 0.0, theta)
                self.places_dict[str(name)] = [
                    x, y, 0.0,
                    float(q[0]),
                    float(q[1]),
                    float(q[2]),
                    float(q[3])
                ]

                # drawing the items
                self._sceneItems[name] = {"text": QGraphicsTextItem()}
                self._sceneItems[name]["text"].setDefaultTextColor(
                    QColor(0, 0, 255))
                self._sceneItems[name]["text"].setFont(QFont("Times", 10))
                self._sceneItems[name]["text"].setPlainText(name)
                scene_pos = self._gridToScene(x, y, q)
                x_c = scene_pos[0] - self._sceneItems[name][
                    "text"].boundingRect().width() / 2.0
                self._sceneItems[name]["text"].setPos(x_c, scene_pos[1])
                self._scene.addItem(self._sceneItems[name]["text"])
                self._sceneItems[name]["rec"] = self._scene.addRect(
                    QRectF((scene_pos[0] - 2), (scene_pos[1] - 2), 4, 4))
                line = QLineF(
                    scene_pos[0], scene_pos[1],
                    (scene_pos[0] - self._mag * cos(radians(scene_pos[3]))),
                    (scene_pos[1] + self._mag * sin(radians(scene_pos[3]))))
                self._sceneItems[name]["line"] = self._scene.addLine(
                    line, pen=QPen(Qt.red, 2))

            except ValueError:
                QMessageBox.critical(self, "Error!",
                                     "You must insert a valid value.")
        else:
            QMessageBox.critical(
                self, "Error!",
                "You have to insert a name and a valid position.")
    def _adjust_html_node_size(self, node):
        if node.get_name() in ('graph', 'node', 'empty'):
            return node

        label = node.get_label()

        if not label:
            return node

        label = label.strip('\"')

        if not label.startswith('<') and not label.endswith('>'):
            return node

        text = QGraphicsTextItem()
        text.setHtml(label)

        bounding_box = text.boundingRect()
        width = bounding_box.width() / POINTS_PER_INCH
        height = bounding_box.height() / POINTS_PER_INCH

        url = node.get_attributes().get('URL', None)

        return self._create_fixed_size_node(node.get_name(), label, str(width), str(height), url)
    def __init__(self, bounding_box, shape, label, label_pos=None, url=None, parent=None, **kwargs):
        super(DmgHtmlNodeItem, self).__init__(parent, **kwargs)
        self.url = url
        self._incoming_edges = set()
        self._outgoing_edges = set()
        self._brush = QBrush(self._color)

        self._label_pen = QPen()
        self._label_pen.setColor(self._color)
        self._label_pen.setJoinStyle(Qt.RoundJoin)
        self._label_pen.setWidthF(self._label_pen_width)

        self._graphics_item_pen = QPen(self._label_pen)
        self._graphics_item_pen.setWidthF(self._pen_width)

        self._label = QGraphicsTextItem()
        self._label.setHtml(label)

        label_rectangle = self._label.boundingRect()
        if label_pos is None:
            label_rectangle.moveCenter(bounding_box.center())
        else:
            label_rectangle.moveCenter(label_pos)
        self._label.setPos(label_rectangle.x(), label_rectangle.y())

        self.addToGroup(self._label)

        self._graphics_item = ShapeFactory.create(shape, bounding_box)
        if ShapeFactory.message is not None:
            print ShapeFactory.message
        self.addToGroup(self._graphics_item)

        self._brush.setColor(self._color)
        self._graphics_item_pen.setColor(self._color)
        self._label_pen.setColor(self._color)

        self._graphics_item.setPen(self._graphics_item_pen)

        self._highlight_level = kwargs.get('highlight_level', self.HIGHLIGHT_LEVEL)
        self._hovered_color = kwargs.get('hovered_color', self.HOVERED_COLOR)
        self._highlighted_color = kwargs.get('highlighted_color', self.HIGHLIGHTED_COLOR)
        self._highlighted_pen_width = kwargs.get('highlighted_pen_width', self.HIGHLIGHTED_PEN_WIDTH)
        self._highlighted_label_pen_width = kwargs.get('highlighted_label_pen_width', self.HIGHLIGHTED_LABEL_PEN_WIDTH)

        self.hover_shape = None
        self.setAcceptHoverEvents(True)
Example #6
0
    def _handle_load_clicked(self):
        filename = QFileDialog.getOpenFileName(
            self, self.tr('Load from File'), '.',
            self.tr('YAML files {.yaml} (*.yaml)'))
        if filename[0] != '':
            with open(filename[0], 'r') as infile:
                try:
                    self.places_dict = yaml.load(infile)
                    for k in self._sceneItems.keys():
                        for item in self._sceneItems[k].keys():
                            self._scene.removeItem(self._sceneItems[k][item])
                    for name in self.places_dict.keys():
                        #
                        q = numpy.asarray(self.places_dict[name][3:7])
                        scene_pos = self._gridToScene(
                            self.places_dict[name][0],
                            self.places_dict[name][1], q)

                        # drawing the items
                        self._sceneItems[name] = {"text": QGraphicsTextItem()}
                        self._sceneItems[name]["text"].setDefaultTextColor(
                            QColor(0, 0, 255))
                        self._sceneItems[name]["text"].setFont(
                            QFont("Times", 10))
                        self._sceneItems[name]["text"].setPlainText(name)
                        x_c = scene_pos[0] - self._sceneItems[name][
                            "text"].boundingRect().width() / 2.0
                        self._sceneItems[name]["text"].setPos(
                            x_c, scene_pos[1])
                        self._scene.addItem(self._sceneItems[name]["text"])
                        self._sceneItems[name]["rec"] = self._scene.addRect(
                            QRectF((scene_pos[0] - 2), (scene_pos[1] - 2), 4,
                                   4))
                        line = QLineF(scene_pos[0], scene_pos[1],
                                      (scene_pos[0] -
                                       self._mag * cos(radians(scene_pos[3]))),
                                      (scene_pos[1] +
                                       self._mag * sin(radians(scene_pos[3]))))
                        self._sceneItems[name]["line"] = self._scene.addLine(
                            line, pen=QPen(Qt.red, 2))
                except yaml.scanner.ScannerError:
                    QMessageBox.critical(self, "Error!", "Invalid YAML file.")
Example #7
0
class DmgHtmlNodeItem(GraphItem):
    HIGHLIGHT_LEVEL = 1
    HOVERED_COLOR = QColor(250, 0, 0)
    HIGHLIGHTED_COLOR = QColor(100, 100, 100)
    HIGHLIGHTED_PEN_WIDTH = 2.0
    HIGHLIGHTED_LABEL_PEN_WIDTH = 1.0

    def __init__(self,
                 bounding_box,
                 shape,
                 label,
                 label_pos=None,
                 url=None,
                 parent=None,
                 **kwargs):
        super(DmgHtmlNodeItem, self).__init__(parent, **kwargs)
        self.url = url
        self._incoming_edges = set()
        self._outgoing_edges = set()
        self._brush = QBrush(self._color)

        self._label_pen = QPen()
        self._label_pen.setColor(self._color)
        self._label_pen.setJoinStyle(Qt.RoundJoin)
        self._label_pen.setWidthF(self._label_pen_width)

        self._graphics_item_pen = QPen(self._label_pen)
        self._graphics_item_pen.setWidthF(self._pen_width)

        self._label = QGraphicsTextItem()
        self._label.setHtml(label)

        label_rectangle = self._label.boundingRect()
        if label_pos is None:
            label_rectangle.moveCenter(bounding_box.center())
        else:
            label_rectangle.moveCenter(label_pos)
        self._label.setPos(label_rectangle.x(), label_rectangle.y())

        self.addToGroup(self._label)

        self._graphics_item = ShapeFactory.create(shape, bounding_box)
        if ShapeFactory.message is not None:
            print ShapeFactory.message
        self.addToGroup(self._graphics_item)

        self._brush.setColor(self._color)
        self._graphics_item_pen.setColor(self._color)
        self._label_pen.setColor(self._color)

        self._graphics_item.setPen(self._graphics_item_pen)

        self._highlight_level = kwargs.get('highlight_level',
                                           self.HIGHLIGHT_LEVEL)
        self._hovered_color = kwargs.get('hovered_color', self.HOVERED_COLOR)
        self._highlighted_color = kwargs.get('highlighted_color',
                                             self.HIGHLIGHTED_COLOR)
        self._highlighted_pen_width = kwargs.get('highlighted_pen_width',
                                                 self.HIGHLIGHTED_PEN_WIDTH)
        self._highlighted_label_pen_width = kwargs.get(
            'highlighted_label_pen_width', self.HIGHLIGHTED_LABEL_PEN_WIDTH)

        self.hover_shape = None
        self.setAcceptHoverEvents(True)

    def add_incoming_edge(self, edge):
        self._incoming_edges.add(edge)

    def add_outgoing_edge(self, edge):
        self._outgoing_edges.add(edge)

    def set_color(self, color=None):
        if color is None:
            color = self._color

        self._brush.setColor(color)
        self._graphics_item_pen.setColor(color)
        self._label.setDefaultTextColor(color)

        self._graphics_item.setPen(self._graphics_item_pen)

    def set_hover_shape(self, shape):
        self.hover_shape = shape

    def shape(self):
        if self.hover_shape is not None:
            path = QPainterPath()
            path.addRect(self.hover_shape)
            return path

        return super(GraphItem, self).shape()

    def hoverEnterEvent(self, event):
        self.set_color(self._highlighted_color)
        self._highlight_connections()

    def hoverLeaveEvent(self, event):
        self.set_color()
        self._highlight_connections(False)

    def _highlight_connections(self, highlighted=True):
        if highlighted:
            if self._highlight_level > 1:
                cyclic_edges = self._incoming_edges.intersection(
                    self._outgoing_edges)
                # incoming edges in blue
                incoming_nodes = set()
                for incoming_edge in self._incoming_edges.difference(
                        cyclic_edges):
                    incoming_edge.set_color(self.COLOR_BLUE)
                    if incoming_edge.from_node != self:
                        incoming_nodes.add(incoming_edge.from_node)
                # outgoing edges in green
                outgoing_nodes = set()
                for outgoing_edge in self._outgoing_edges.difference(
                        cyclic_edges):
                    outgoing_edge.set_color(self.COLOR_GREEN)
                    if outgoing_edge.to_node != self:
                        outgoing_nodes.add(outgoing_edge.to_node)
                # incoming/outgoing edges in teal
                for edge in cyclic_edges:
                    edge.set_color(self.COLOR_TEAL)

                if self._highlight_level > 2:
                    cyclic_nodes = incoming_nodes.intersection(outgoing_nodes)
                    # incoming nodes in blue
                    for incoming_node in incoming_nodes.difference(
                            cyclic_nodes):
                        incoming_node.set_color(self.COLOR_BLUE)
                    # outgoing nodes in green
                    for outgoing_node in outgoing_nodes.difference(
                            cyclic_nodes):
                        outgoing_node.set_color(self.COLOR_GREEN)
                    # incoming/outgoing nodes in teal
                    for node in cyclic_nodes:
                        node.set_color(self.COLOR_TEAL)
            else:
                if self._highlight_level > 1:
                    for incoming_edge in self._incoming_edges:
                        incoming_edge.set_color()
                        if self.highlight_level > 2 and incoming_edge.from_node != self:
                            incoming_edge.from_node.set_color()
                    for outgoing_edge in self._outgoing_edges:
                        outgoing_edge.set_color()
                        if self.highlight_level > 2 and outgoing_edge.to_node != self:
                            outgoing_edge.to_node.set_color()

    def highlight(self, highlighted=True):
        if highlighted:
            self._graphics_item_pen.setWidthF(self._highlighted_pen_width)
        else:
            self._graphics_item_pen.setWidthF(self._pen_width)

        self._graphics_item.setPen(self._graphics_item_pen)
class DmgHtmlNodeItem(GraphItem):
    HIGHLIGHT_LEVEL = 1
    HOVERED_COLOR = QColor(250, 0, 0)
    HIGHLIGHTED_COLOR = QColor(100, 100, 100)
    HIGHLIGHTED_PEN_WIDTH = 2.0
    HIGHLIGHTED_LABEL_PEN_WIDTH = 1.0

    def __init__(self, bounding_box, shape, label, label_pos=None, url=None, parent=None, **kwargs):
        super(DmgHtmlNodeItem, self).__init__(parent, **kwargs)
        self.url = url
        self._incoming_edges = set()
        self._outgoing_edges = set()
        self._brush = QBrush(self._color)

        self._label_pen = QPen()
        self._label_pen.setColor(self._color)
        self._label_pen.setJoinStyle(Qt.RoundJoin)
        self._label_pen.setWidthF(self._label_pen_width)

        self._graphics_item_pen = QPen(self._label_pen)
        self._graphics_item_pen.setWidthF(self._pen_width)

        self._label = QGraphicsTextItem()
        self._label.setHtml(label)

        label_rectangle = self._label.boundingRect()
        if label_pos is None:
            label_rectangle.moveCenter(bounding_box.center())
        else:
            label_rectangle.moveCenter(label_pos)
        self._label.setPos(label_rectangle.x(), label_rectangle.y())

        self.addToGroup(self._label)

        self._graphics_item = ShapeFactory.create(shape, bounding_box)
        if ShapeFactory.message is not None:
            print ShapeFactory.message
        self.addToGroup(self._graphics_item)

        self._brush.setColor(self._color)
        self._graphics_item_pen.setColor(self._color)
        self._label_pen.setColor(self._color)

        self._graphics_item.setPen(self._graphics_item_pen)

        self._highlight_level = kwargs.get('highlight_level', self.HIGHLIGHT_LEVEL)
        self._hovered_color = kwargs.get('hovered_color', self.HOVERED_COLOR)
        self._highlighted_color = kwargs.get('highlighted_color', self.HIGHLIGHTED_COLOR)
        self._highlighted_pen_width = kwargs.get('highlighted_pen_width', self.HIGHLIGHTED_PEN_WIDTH)
        self._highlighted_label_pen_width = kwargs.get('highlighted_label_pen_width', self.HIGHLIGHTED_LABEL_PEN_WIDTH)

        self.hover_shape = None
        self.setAcceptHoverEvents(True)

    def add_incoming_edge(self, edge):
        self._incoming_edges.add(edge)

    def add_outgoing_edge(self, edge):
        self._outgoing_edges.add(edge)

    def set_color(self, color=None):
        if color is None:
            color = self._color

        self._brush.setColor(color)
        self._graphics_item_pen.setColor(color)
        self._label.setDefaultTextColor(color)

        self._graphics_item.setPen(self._graphics_item_pen)

    def set_hover_shape(self, shape):
        self.hover_shape = shape

    def shape(self):
        if self.hover_shape is not None:
            path = QPainterPath()
            path.addRect(self.hover_shape)
            return path

        return super(GraphItem, self).shape()

    def hoverEnterEvent(self, event):
        self.set_color(self._highlighted_color)
        self._highlight_connections()

    def hoverLeaveEvent(self, event):
        self.set_color()
        self._highlight_connections(False)

    def _highlight_connections(self, highlighted=True):
        if highlighted:
            if self._highlight_level > 1:
                cyclic_edges = self._incoming_edges.intersection(self._outgoing_edges)
                # incoming edges in blue
                incoming_nodes = set()
                for incoming_edge in self._incoming_edges.difference(cyclic_edges):
                    incoming_edge.set_color(self.COLOR_BLUE)
                    if incoming_edge.from_node != self:
                        incoming_nodes.add(incoming_edge.from_node)
                # outgoing edges in green
                outgoing_nodes = set()
                for outgoing_edge in self._outgoing_edges.difference(cyclic_edges):
                    outgoing_edge.set_color(self.COLOR_GREEN)
                    if outgoing_edge.to_node != self:
                        outgoing_nodes.add(outgoing_edge.to_node)
                # incoming/outgoing edges in teal
                for edge in cyclic_edges:
                    edge.set_color(self.COLOR_TEAL)

                if self._highlight_level > 2:
                    cyclic_nodes = incoming_nodes.intersection(outgoing_nodes)
                    # incoming nodes in blue
                    for incoming_node in incoming_nodes.difference(cyclic_nodes):
                        incoming_node.set_color(self.COLOR_BLUE)
                    # outgoing nodes in green
                    for outgoing_node in outgoing_nodes.difference(cyclic_nodes):
                        outgoing_node.set_color(self.COLOR_GREEN)
                    # incoming/outgoing nodes in teal
                    for node in cyclic_nodes:
                        node.set_color(self.COLOR_TEAL)
            else:
                if self._highlight_level > 1:
                    for incoming_edge in self._incoming_edges:
                        incoming_edge.set_color()
                        if self.highlight_level > 2 and incoming_edge.from_node != self:
                            incoming_edge.from_node.set_color()
                    for outgoing_edge in self._outgoing_edges:
                        outgoing_edge.set_color()
                        if self.highlight_level > 2 and outgoing_edge.to_node != self:
                            outgoing_edge.to_node.set_color()

    def highlight(self, highlighted=True):
        if highlighted:
            self._graphics_item_pen.setWidthF(self._highlighted_pen_width)
        else:
            self._graphics_item_pen.setWidthF(self._pen_width)

        self._graphics_item.setPen(self._graphics_item_pen)