constrain legend rect to be fully inside viewbox; simplest solution to fix #113

This commit is contained in:
Dominik Demuth 2023-07-31 20:17:31 +02:00
parent cf1565f7d3
commit a5216b1eed

View File

@ -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)