Moving items in topo-v3

topo-mov
LEdoian 1 year ago
parent 7b2ff09168
commit 0226ba8eb2

@ -68,12 +68,32 @@ app = QtWidgets.QApplication([])
# )
from random import randint
from collections import defaultdict
shapes = dict()
nei = defaultdict(lambda: [])
class MyGraphicsRectItem(QtWidgets.QGraphicsRectItem):
#def __init__(self, *a, **kwa):
# return super().__init__(*a, **kwa)
#def itemChange(self, change, val):
# return super().itemChange(change, val)
def mouseMoveEvent(self, evt):
vtxid = self.data(0)
for e in nei[vtxid]:
x1 = self.x()
y1 = self.y()
other = e.source if e.source != vtxid else e.target
x2 = shapes[other].x()
y2 = shapes[other].y()
qlinef = QtCore.QLineF(x1, y1, x2, y2)
shapes[e].setLine(qlinef)
return super().mouseMoveEvent(evt)
for k, v in annot_topo.topology.vertices.items():
size = 30 if k.is_router else 10
x, y = randint(0, 1920), randint(0, 1080)
shape = QtWidgets.QGraphicsRectItem(-size/2, -size/2, size, size)
shape = MyGraphicsRectItem(-size/2, -size/2, size, size)
shape.setPos(x,y)
# TODO:brush
label_text = str(IPv4Address(k.router_id)) if k.is_router else str(k.address) # Surprisingly works for all the possible addresses.
@ -81,6 +101,8 @@ for k, v in annot_topo.topology.vertices.items():
label.setY(size*0.8)
text_width = label.boundingRect().width()
label.setX(-text_width/2)
shape.setData(0, k)
shape.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable | QtWidgets.QGraphicsItem.ItemIsSelectable)
shapes[k] = shape
for e in annot_topo.topology.edges:
@ -88,6 +110,9 @@ for e in annot_topo.topology.edges:
end = shapes[e.target].pos()
qlinef = QtCore.QLineF(start, end)
line = QtWidgets.QGraphicsLineItem(qlinef)
line.setData(0, e)
nei[e.source].append(e)
nei[e.target].append(e)
shapes[e] = line
# Render the widget

Loading…
Cancel
Save