Attached is a patch to draw connections more pretty. Screenshot here:
<http://unununium.org/~indigo/fancy_nova_connections.png>

I realize some people may prefer straight or right-angled lines; I think
those can be implemented after there is some sort of preferences system
in place (something I'll probably tackle in the future)
=== modified file 'source/pyqt_gui/pyqt_patcher_xlets.py'
--- source/pyqt_gui/pyqt_patcher_xlets.py       2007-09-12 10:19:01 +0000
+++ source/pyqt_gui/pyqt_patcher_xlets.py       2007-09-24 01:04:36 +0000
@@ -24,27 +24,85 @@
 # $LastChangedDate$
 # $LastChangedBy$
 
+from __future__ import division
+
+from math import log
 import py_nova
 import PyQt4.QtCore as QtCore
 import PyQt4.QtGui as QtGui
 
-class connection_line(QtGui.QGraphicsLineItem):
+class connection_line(QtGui.QGraphicsPathItem):
+    width = 4
+    inner_color = QtGui.QColor(0x9a89c2)
+    outer_color = QtGui.QColor(0x38128d)
+
     def __init__(self, connection):
         self.connection = connection
-        if connection:
-            QtGui.QGraphicsLineItem.__init__(self, None, 
connection.canvas.scene)
+        assert connection, 'can not create a connection_line without a 
connection'
+        QtGui.QGraphicsPathItem.__init__(self, None, connection.canvas.scene)
 
-        pen = QtGui.QPen((QtCore.Qt.white))
-        pen.setWidth(2)
+        pen = QtGui.QPen(self.outer_color)
+        pen.setWidth(self.width)
+        pen.setCapStyle(QtCore.Qt.RoundCap)
         self.setPen(pen)
 
         self.setAcceptedMouseButtons(QtCore.Qt.RightButton)
 
+        self.child_line = QtGui.QGraphicsPathItem(self)
+        pen.setWidth(self.width-2)
+        pen.setColor(self.inner_color)
+        self.child_line.setPen(pen)
+
     def mousePressEvent (self, event):
         if self.connection.canvas.edit:
             if event.button() == QtCore.Qt.RightButton and event.modifiers() 
== QtCore.Qt.ShiftModifier:
                 self.connection.schedule_removal()
 
+    def update_endpoints (self, start, end):
+        path = QtGui.QPainterPath(start)
+
+        # how much connections want to point away from their xlets. When the
+        # vertical distance between outlet and inlet are less than this, the
+        # connection will begin to "double back" on itself; ie, bend such that
+        # it can be intersected twice with a horizonal line.
+
+        # there are two factors that affect stiffness; when the boxes are not
+        # very far apart horizontally, it makes sense to reduce the stiffness.
+        # This prevents the connection from making a sharp Z when two xlets are
+        # aligned vertically, and very near each other.
+
+        # the other factor involves connections that go from a low outlet to a
+        # higher inlet. As the vertical difference becomes greater, we need a
+        # higher stiffness to compensate for the greater "pull" on the
+        # connection.
+        stiffness = max(
+            min(abs(start.x() - end.x()) / 2, 100),
+            log(max(start.y() - end.y(), 0.001)) * 30
+            )
+
+        if end.y() - start.y() < stiffness:
+            c1_y = start.y() + stiffness / 2
+            c2_y = end.y() - stiffness / 2
+        else:
+            c1_y = c2_y = start.y() + (end.y() - start.y()) / 2
+
+        # x_offset moves the two control points to the left. This is done when
+        # going from a low outlet to high inlet and the horizontal difference
+        # between them is not very large, to avoid a straight vertical line.
+        if abs(start.x() - end.x()) < 20 and end.y() < start.y():
+            x_offset = 20 - abs(start.x() - end.x())
+        else:
+            x_offset = 0
+
+        path.cubicTo(
+            start.x()-x_offset, c1_y,
+            end.x()-x_offset, c2_y,
+            end.x(), end.y())
+
+        for l in [self, self.child_line]:
+            l.setPath(path)
+        self.update()
+
 class Connection(py_nova.connection_wrapper):
 
     def __init__(self, canvas):
@@ -87,8 +145,7 @@
         mapped_outlet_coord = outlet.mapToScene(outlet_coord)
         mapped_inlet_coord = inlet.mapToScene(inlet_coord)
 
-        self.line.setLine(QtCore.QLineF(mapped_outlet_coord, 
mapped_inlet_coord))
-        self.line.update()
+        self.line.update_endpoints(mapped_outlet_coord, mapped_inlet_coord)
 
     def hide(self):
         self.line = None

_______________________________________________
nova-dev mailing list
[email protected]
http://klingt.org/cgi-bin/mailman/listinfo/nova-dev
http://tim.klingt.org/nova

Reply via email to