diff --git a/birdvisu/visualisation/annotators.py b/birdvisu/visualisation/annotators.py index dc6d92b..74ff73b 100644 --- a/birdvisu/visualisation/annotators.py +++ b/birdvisu/visualisation/annotators.py @@ -9,6 +9,7 @@ from dataclasses import dataclass import re import random from PySide6 import QtCore, QtGui, QtWidgets +from .qt_widgets import MyGraphicsRectItem # Classification @@ -140,7 +141,7 @@ def create_qgritems(at): x = pos.x y = pos.y - shape = QtWidgets.QGraphicsRectItem(-size/2, -size/2, size, size) + shape = MyGraphicsRectItem(-size/2, -size/2, size, size) shape.setBrush(brush) shape.setPos(x, y) shape.setToolTip(rk) @@ -149,6 +150,7 @@ def create_qgritems(at): # Centering: text_width = label.boundingRect().width() label.setX(-text_width/2) + shape.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable | QtWidgets.QGraphicsItem.ItemIsSelectable) at.router_annotations[rk].append(shape) for nk, n in topo.networks.items(): @@ -166,7 +168,7 @@ def create_qgritems(at): x = pos.x y = pos.y - shape = QtWidgets.QGraphicsRectItem(-size/2, -size/2, size, size) + shape = MyGraphicsRectItem(-size/2, -size/2, size, size) shape.setBrush(brush) shape.setPos(x, y) shape.setToolTip(nk) @@ -175,6 +177,7 @@ def create_qgritems(at): # Centering: text_width = label.boundingRect().width() label.setX(-text_width/2) + shape.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable | QtWidgets.QGraphicsItem.ItemIsSelectable) at.network_annotations[nk].append(shape) for lk, l in topo.links.items(): @@ -183,16 +186,24 @@ def create_qgritems(at): rpos = None for tag in at.router_annotations[rid][-1::-1]: + if isinstance(tag, QtWidgets.QGraphicsRectItem): + ritem = tag if isinstance(tag, Position): rpos = tag break npos = None for tag in at.network_annotations[nid][-1::-1]: + if isinstance(tag, QtWidgets.QGraphicsRectItem): + nitem = tag if isinstance(tag, Position): npos = tag break line = QtWidgets.QGraphicsLineItem(rpos.x, rpos.y, npos.x, npos.y) + ritem.setData(0, line) + nitem.setData(0, line) + line.setData(0, nitem) + line.setData(1, ritem) at.link_annotations[lk].append(line) return at diff --git a/birdvisu/visualisation/qt_widgets.py b/birdvisu/visualisation/qt_widgets.py new file mode 100644 index 0000000..486a274 --- /dev/null +++ b/birdvisu/visualisation/qt_widgets.py @@ -0,0 +1,23 @@ +from PySide6.QtWidgets import QGraphicsRectItem +from PySide6.QtCore import QLineF + +class MyGraphicsRectItem(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): + print(f'Moving: {self}') + line = self.data(0) + other_end = line.data(0) + if other_end == self: + other_end = line.data(1) + new_qlinef = QLineF( + self.x(), + self.y(), + other_end.x(), + other_end.y(), + ) + line.setLine(new_qlinef) + return super().mouseMoveEvent(evt) +