Change how port locations are described
Previously, Switch.get_port_location() would return the co-ordinates
of the middle of an edge of a port box, ready for (only) passing into
Graphics.draw_trunk(). We now have more users who will want to find
where a port is, so update what's passed here. Now we will pass the
co-ordinates of the UL and LR corners of the port box (along with
whether it's upper or lower row).
This will enable other users to pick up on the location of a port box
properly, e.g. for image maps.
Change-Id: I82a7e7f63381a64f32188e21af46f72ee3ba7915
diff --git a/visualisation/graphics.py b/visualisation/graphics.py
index 334fc6f..55067d8 100644
--- a/visualisation/graphics.py
+++ b/visualisation/graphics.py
@@ -161,16 +161,23 @@
# Draw a trunk connection between two ports
#
- # Ports are defined as (x, y, top): x, y co-ordinates and whether
- # the port is on the top or bottom row of a switch, i.e. does the
- # wire come up or down when it leaves the port.
+ # Ports are defined as (ulx,uly),(lrx,lry), top): x, y
+ # co-ordinates of UL and LR corners, and whether the port is on
+ # the top or bottom row of a switch, i.e. does the wire come up or
+ # down when it leaves the port.
def draw_trunk(self, trunknum, node1, node2, colour):
for node in (node1, node2):
- (x1,y1,top) = node
+ ((ulx,uly),(lrx,lry),top) = node
+
+ # Work out the co-ordinates for a line vertically up or
+ # down from the edge of the port
+ x1 = int((ulx + lrx) / 2)
x2 = x1
if (top):
+ y1 = uly
y2 = y1 - (self.trunk_gap * (trunknum + 1))
else:
+ y1 = lry
y2 = y1 + (self.trunk_gap * (trunknum + 1))
# Quick hack - use 2-pixel wide rectangles as thick lines :-)
# First line, vertically up/down from the port
@@ -178,17 +185,18 @@
# Now draw horizontally across to the left margin space
x3 = self.trunk_gap * (trunknum + 1)
self.im.rectangle((x3, y2), (x2,y2+1), self.im.colorExact(self.colour_defs[colour]))
+
# Now join up the trunks vertically
- (x1,y1,top1) = node1
+ ((ulx1,uly1),(lrx1,lry1),top1) = node1
if (top1):
- y1 -= self.trunk_gap * (trunknum + 1)
+ y1 = uly1 - self.trunk_gap * (trunknum + 1)
else:
- y1 += self.trunk_gap * (trunknum + 1)
- (x2,y2,top2) = node2
+ y1 = lry1 + self.trunk_gap * (trunknum + 1)
+ ((ulx2,uly2),(lrx2,lry2),top2) = node2
if (top2):
- y2 -= self.trunk_gap * (trunknum + 1)
+ y2 = uly2 - self.trunk_gap * (trunknum + 1)
else:
- y2 += self.trunk_gap * (trunknum + 1)
+ y2 = lry2 + self.trunk_gap * (trunknum + 1)
x1 = self.trunk_gap * (trunknum + 1)
self.im.rectangle((x1, y1), (x1+1,y2), self.im.colorExact(self.colour_defs[colour]))
@@ -360,24 +368,25 @@
for portnum in range(1, self.num_ports + 1):
self.draw_port(g, portnum, 'normal')
- # Get the (x,y) co-ordinates of the middle of the external edge of
- # the port box (i.e. top of a top-row port, bottom of a bottom-row
- # port) so that we can draw a connection to that point
+ # Get the (x,y) co-ordinates of the UL and LR edges of the port
+ # box, and if it's upper row. This lets us so useful things such
+ # as draw a connection to that point for a trunk.
def get_port_location(self, portnum):
if portnum > self.num_ports:
raise InputError('port number out of range')
if (portnum & 1): # odd port number, so top row
ulx = self.left + 3 + ((portnum-1) * self.port_width / 2)
- uly = self.top + 3
- mid_edge = ulx + int((self.port_width / 2))
- return (mid_edge, uly, True)
+ uly = self.top
+ lrx = ulx + self.port_width
+ lry = uly + self.port_height
+ return ((ulx,uly), (lrx,lry), True)
else: # bottom row
ulx = self.left + 3 + ((portnum-2) * self.port_width / 2)
uly = self.top + 3 + self.port_height
+ lrx = ulx + self.port_width
lry = uly + self.port_height
- mid_edge = ulx + int((self.port_width / 2))
- return (mid_edge, lry, False)
+ return ((ulx,uly), (lrx,lry), False)
# Debug: print some of the state of the switch object
def dump_state(self):