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):
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         label_rect = self._label.boundingRect()
00061         if label_pos is None:
00062             label_rect.moveCenter(bounding_box.center())
00063         else:
00064             label_rect.moveCenter(label_pos)
00065         self._label.setPos(label_rect.x(), label_rect.y())
00066         self.addToGroup(self._label)
00067 
00068         self.set_node_color()
00069 
00070         self.setAcceptHoverEvents(True)
00071 
00072         self.hovershape = None
00073 
00074     def set_hovershape(self, newhovershape):
00075         self.hovershape = newhovershape
00076 
00077     def shape(self):
00078         if self.hovershape is not None:
00079             path = QPainterPath()
00080             path.addRect(self.hovershape)
00081             return path
00082         else:
00083             return super(self.__class__, self).shape()
00084 
00085     def add_incoming_edge(self, edge):
00086         self._incoming_edges.add(edge)
00087 
00088     def add_outgoing_edge(self, edge):
00089         self._outgoing_edges.add(edge)
00090 
00091     def set_node_color(self, color=None):
00092         if color is None:
00093             color = self._default_color
00094 
00095         self._brush.setColor(color)
00096         self._ellipse_pen.setColor(color)
00097         self._label_pen.setColor(color)
00098 
00099         self._graphics_item.setPen(self._ellipse_pen)
00100         self._label.setBrush(self._brush)
00101         self._label.setPen(self._label_pen)
00102 
00103     def hoverEnterEvent(self, event):
00104         # hovered node item in red
00105         self.set_node_color(self._COLOR_RED)
00106 
00107         if self._highlight_level > 1:
00108             cyclic_edges = self._incoming_edges.intersection(self._outgoing_edges)
00109             # incoming edges in blue
00110             incoming_nodes = set()
00111             for incoming_edge in self._incoming_edges.difference(cyclic_edges):
00112                 incoming_edge.set_node_color(self._COLOR_BLUE)
00113                 if incoming_edge.from_node != self:
00114                     incoming_nodes.add(incoming_edge.from_node)
00115             # outgoing edges in green
00116             outgoing_nodes = set()
00117             for outgoing_edge in self._outgoing_edges.difference(cyclic_edges):
00118                 outgoing_edge.set_node_color(self._COLOR_GREEN)
00119                 if outgoing_edge.to_node != self:
00120                     outgoing_nodes.add(outgoing_edge.to_node)
00121             # incoming/outgoing edges in teal
00122             for edge in cyclic_edges:
00123                 edge.set_node_color(self._COLOR_TEAL)
00124 
00125             if self._highlight_level > 2:
00126                 cyclic_nodes = incoming_nodes.intersection(outgoing_nodes)
00127                 # incoming nodes in blue
00128                 for incoming_node in incoming_nodes.difference(cyclic_nodes):
00129                     incoming_node.set_node_color(self._COLOR_BLUE)
00130                 # outgoing nodes in green
00131                 for outgoing_node in outgoing_nodes.difference(cyclic_nodes):
00132                     outgoing_node.set_node_color(self._COLOR_GREEN)
00133                 # incoming/outgoing nodes in teal
00134                 for node in cyclic_nodes:
00135                     node.set_node_color(self._COLOR_TEAL)
00136 
00137     def hoverLeaveEvent(self, event):
00138         self.set_node_color()
00139         if self._highlight_level > 1:
00140             for incoming_edge in self._incoming_edges:
00141                 incoming_edge.set_node_color()
00142                 if self._highlight_level > 2 and incoming_edge.from_node != self:
00143                     incoming_edge.from_node.set_node_color()
00144             for outgoing_edge in self._outgoing_edges:
00145                 outgoing_edge.set_node_color()
00146                 if self._highlight_level > 2 and outgoing_edge.to_node != self:
00147                     outgoing_edge.to_node.set_node_color()


qt_dotgraph
Author(s): Thibault Kruse
autogenerated on Mon Oct 6 2014 03:57:59