From a5216b1eed044507459b1c469fdc97dc60a97f66 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Mon, 31 Jul 2023 20:17:31 +0200 Subject: [PATCH] constrain legend rect to be fully inside viewbox; simplest solution to fix #113 --- src/gui_qt/lib/pg_objects.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/gui_qt/lib/pg_objects.py b/src/gui_qt/lib/pg_objects.py index 6612848..fe706c9 100644 --- a/src/gui_qt/lib/pg_objects.py +++ b/src/gui_qt/lib/pg_objects.py @@ -5,7 +5,7 @@ from pyqtgraph import ( LinearRegionItem, mkBrush, mkColor, mkPen, PlotDataItem, - LegendItem, + LegendItem, ViewBox, ) from nmreval.lib.colors import BaseColor, Colors @@ -472,11 +472,18 @@ class LegendItemBlock(LegendItem): def mouseDragEvent(self, ev): if ev.button() == QtCore.Qt.LeftButton: ev.accept() + dpos = ev.pos() - ev.lastPos() + + upper_left = self.pos() + lower_right = self.pos() + lower_right.setX(lower_right.x() + self.width()) + lower_right.setY(lower_right.y() + self.height()) + vb_rect = self.parentItem().rect() - pos = self.pos() - # upper left corner and a point a little more to the bottom right must be inside - if vb_rect.contains(pos+dpos) and vb_rect.contains(pos+dpos+QtCore.QPointF(20., 20.)): - self.autoAnchor(pos + dpos) + + # upper left and lower right corner must be inside viewbox + if vb_rect.contains(upper_left + dpos) and vb_rect.contains(lower_right + dpos): + self.autoAnchor(upper_left + dpos) else: - self.autoAnchor(pos) + self.autoAnchor(upper_left)