node_item.py
Go to the documentation of this file.
00001 # Copyright (c) 2011, Dirk Thomas, TU Darmstadt
00002 # All rights reserved.
00003 #
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions
00006 # are met:
00007 #
00008 #   * Redistributions of source code must retain the above copyright
00009 #     notice, this list of conditions and the following disclaimer.
00010 #   * Redistributions in binary form must reproduce the above
00011 #     copyright notice, this list of conditions and the following
00012 #     disclaimer in the documentation and/or other materials provided
00013 #     with the distribution.
00014 #   * Neither the name of the TU Darmstadt nor the names of its
00015 #     contributors may be used to endorse or promote products derived
00016 #     from this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00028 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029 # POSSIBILITY OF SUCH DAMAGE.
00030 
00031 from python_qt_binding.QtCore import Qt
00032 from python_qt_binding.QtGui import QBrush, QGraphicsEllipseItem, QGraphicsRectItem, QGraphicsSimpleTextItem, QPainterPath, QPen
00033 
00034 from .graph_item import GraphItem
00035 
00036 
00037 class NodeItem(GraphItem):
00038 
00039     def __init__(self, highlight_level, bounding_box, label, shape, color=None, parent=None, label_pos=None, tooltip=None):
00040         super(NodeItem, self).__init__(highlight_level, parent)
00041 
00042         self._default_color = self._COLOR_BLACK if color is None else color
00043         self._brush = QBrush(self._default_color)
00044         self._label_pen = QPen()
00045         self._label_pen.setColor(self._default_color)
00046         self._label_pen.setJoinStyle(Qt.RoundJoin)
00047         self._ellipse_pen = QPen(self._label_pen)
00048         self._ellipse_pen.setWidth(1)
00049 
00050         self._incoming_edges = set()
00051         self._outgoing_edges = set()
00052 
00053         if shape == 'box':
00054             self._graphics_item = QGraphicsRectItem(bounding_box)
00055         else:
00056             self._graphics_item = QGraphicsEllipseItem(bounding_box)
00057         self.addToGroup(self._graphics_item)
00058 
00059         self._label = QGraphicsSimpleTextItem(label)
00060         self._label.setFont(GraphItem._LABEL_FONT)
00061         label_rect = self._label.boundingRect()
00062         if label_pos is None:
00063             label_rect.moveCenter(bounding_box.center())
00064         else:
00065             label_rect.moveCenter(label_pos)
00066         self._label.setPos(label_rect.x(), label_rect.y())
00067         self.addToGroup(self._label)
00068         if tooltip is not None:
00069             self.setToolTip(tooltip)
00070 
00071         self.set_node_color()
00072 
00073         self.setAcceptHoverEvents(True)
00074 
00075         self.hovershape = None
00076 
00077     def set_hovershape(self, newhovershape):
00078         self.hovershape = newhovershape
00079 
00080     def shape(self):
00081         if self.hovershape is not None:
00082             path = QPainterPath()
00083             path.addRect(self.hovershape)
00084             return path
00085         else:
00086             return super(self.__class__, self).shape()
00087 
00088     def add_incoming_edge(self, edge):
00089         self._incoming_edges.add(edge)
00090 
00091     def add_outgoing_edge(self, edge):
00092         self._outgoing_edges.add(edge)
00093 
00094     def set_node_color(self, color=None):
00095         if color is None:
00096             color = self._default_color
00097 
00098         self._brush.setColor(color)
00099         self._ellipse_pen.setColor(color)
00100         self._label_pen.setColor(color)
00101 
00102         self._graphics_item.setPen(self._ellipse_pen)
00103         self._label.setBrush(self._brush)
00104         self._label.setPen(self._label_pen)
00105 
00106     def hoverEnterEvent(self, event):
00107         # hovered node item in red
00108         self.set_node_color(self._COLOR_RED)
00109 
00110         if self._highlight_level > 1:
00111             cyclic_edges = self._incoming_edges.intersection(self._outgoing_edges)
00112             # incoming edges in blue
00113             incoming_nodes = set()
00114             for incoming_edge in self._incoming_edges.difference(cyclic_edges):
00115                 incoming_edge.set_node_color(self._COLOR_BLUE)
00116                 if incoming_edge.from_node != self:
00117                     incoming_nodes.add(incoming_edge.from_node)
00118             # outgoing edges in green
00119             outgoing_nodes = set()
00120             for outgoing_edge in self._outgoing_edges.difference(cyclic_edges):
00121                 outgoing_edge.set_node_color(self._COLOR_GREEN)
00122                 if outgoing_edge.to_node != self:
00123                     outgoing_nodes.add(outgoing_edge.to_node)
00124             # incoming/outgoing edges in teal
00125             for edge in cyclic_edges:
00126                 edge.set_node_color(self._COLOR_TEAL)
00127 
00128             if self._highlight_level > 2:
00129                 cyclic_nodes = incoming_nodes.intersection(outgoing_nodes)
00130                 # incoming nodes in blue
00131                 for incoming_node in incoming_nodes.difference(cyclic_nodes):
00132                     incoming_node.set_node_color(self._COLOR_BLUE)
00133                 # outgoing nodes in green
00134                 for outgoing_node in outgoing_nodes.difference(cyclic_nodes):
00135                     outgoing_node.set_node_color(self._COLOR_GREEN)
00136                 # incoming/outgoing nodes in teal
00137                 for node in cyclic_nodes:
00138                     node.set_node_color(self._COLOR_TEAL)
00139 
00140     def hoverLeaveEvent(self, event):
00141         self.set_node_color()
00142         if self._highlight_level > 1:
00143             for incoming_edge in self._incoming_edges:
00144                 incoming_edge.set_node_color()
00145                 if self._highlight_level > 2 and incoming_edge.from_node != self:
00146                     incoming_edge.from_node.set_node_color()
00147             for outgoing_edge in self._outgoing_edges:
00148                 outgoing_edge.set_node_color()
00149                 if self._highlight_level > 2 and outgoing_edge.to_node != self:
00150                     outgoing_edge.to_node.set_node_color()


qt_dotgraph
Author(s): Thibault Kruse
autogenerated on Fri Feb 3 2017 03:42:09