Skip to content

Commit

Permalink
fix wheel based zooming in the plot
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhrisca committed Apr 15, 2024
1 parent 5da4da8 commit 37db460
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/asammdf/gui/dialogs/error_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, title, message, trace, *args, **kwargs):

self.trace = QtWidgets.QTextEdit()

families = QtGui.QFontDatabase().families()
families = QtGui.QFontDatabase.families()
for family in (
"Consolas",
"Liberation Mono",
Expand Down
21 changes: 8 additions & 13 deletions src/asammdf/gui/dialogs/messagebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ def __init__(self, *args, **kwargs):

layout = QtWidgets.QVBoxLayout()

scroll_contents = QtWidgets.QWidget()
self.scroll_contents = scroll_contents = QtWidgets.QWidget()
scroll_contents.setLayout(layout)

self.scroll = scroll = QtWidgets.QScrollArea()
scroll.setMinimumWidth(400)
scroll.setMinimumHeight(300)
scroll.setWidgetResizable(True)
scroll.setWidget(scroll_contents)
scroll.setFrameShape(QtWidgets.QFrame.Shape.NoFrame)
Expand All @@ -36,6 +34,9 @@ def __init__(self, *args, **kwargs):

self.layout().addWidget(scroll, *position)

self.scroll.setMinimumWidth(400)
self.scroll.setMinimumHeight(200)

self.original_text = self.text()
if markdown:
self.setTextFormat(QtCore.Qt.TextFormat.MarkdownText)
Expand Down Expand Up @@ -90,6 +91,10 @@ def __init__(self, *args, **kwargs):
self.timer.timeout.connect(self.tick)
self.timer.start(1000)

self.show()
self.scroll.setMinimumWidth(min(800, self.scroll_contents.width()))
self.scroll.setMinimumHeight(min(800, self.scroll_contents.height()))

def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key.Key_F1:
self.timer.stop()
Expand All @@ -98,16 +103,6 @@ def keyPressEvent(self, event):
else:
super().keyPressEvent(event)

def setText(self, text):
super().setText(text)
pscreen = QtWidgets.QApplication.primaryScreen()

geometry = pscreen.availableGeometry()
self.label.adjustSize()

self.scroll.setMinimumWidth(min(self.label.width() + 20, int(geometry.width() * 0.8)))
self.scroll.setMinimumHeight(min(self.label.height() + 20, int(geometry.height() * 0.8)))

def tick(self):
self.timeout -= 1
default = self.defaultButton()
Expand Down
2 changes: 1 addition & 1 deletion src/asammdf/gui/widgets/channel_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, xunit="s", precision=6, *args, **kwargs):
global MONOSPACE_FONT

if MONOSPACE_FONT is None:
families = QtGui.QFontDatabase().families()
families = QtGui.QFontDatabase.families()
for family in (
"Consolas",
"Liberation Mono",
Expand Down
2 changes: 1 addition & 1 deletion src/asammdf/gui/widgets/tabular_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,7 @@ def __init__(self, pgdf):
global MONOSPACE_FONT

if MONOSPACE_FONT is None:
families = QtGui.QFontDatabase().families()
families = QtGui.QFontDatabase.families()
for family in (
"Consolas",
"Liberation Mono",
Expand Down
47 changes: 33 additions & 14 deletions src/asammdf/gui/widgets/viewbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,6 @@ def wheelEvent(self, ev, axis=None):

pos = ev.pos()

if self._settings.value("zoom_y_center_on_cursor", True, type=bool):
y_pos_val, sig_y_top, sig_y_bottom = self.plot.value_at_cursor()

if isinstance(y_pos_val, (int, float)):
ratio = (sig_y_top - y_pos_val) / (sig_y_top - sig_y_bottom)

rect = self.boundingRect()

y_coord = (rect.height() - rect.y()) * ratio
pos.setY(y_coord)

s = 1.02 ** (ev.delta() * self.state["wheelScaleFactor"]) # actual scaling factor

s = [(None if m is False else s) for m in mask]
Expand All @@ -337,18 +326,48 @@ def wheelEvent(self, ev, axis=None):
center = pg.Point(fn.invertQTransform(self.childGroup.transform()).map(pos))

self._resetTarget()
self.scaleBy(s, center)

if s is not None:
x, y = s[0], s[1]

affect = [x is not None, y is not None]
if not any(affect):
return

scale = pg.Point([1.0 if x is None else x, 1.0 if y is None else y])

if self.state["aspectLocked"] is not False:
scale[0] = scale[1]

vr = self.targetRect()
if center is None:
center = pg.Point(vr.center())
else:
center = pg.Point(center)

tl = center + (vr.topLeft() - center) * scale
br = center + (vr.bottomRight() - center) * scale

x_range = tl.x(), br.x()
y_range = br.y(), tl.y()

if (
self._settings.value("zoom_x_center_on_cursor", True, type=bool)
and self.cursor is not None
and self.cursor.isVisible()
):
x_range, _ = self.viewRange()
delta = x_range[1] - x_range[0]

pos = self.cursor.value()
self.setXRange(pos - delta / 2, pos + delta / 2, padding=0)
x_range = pos - delta / 2, pos + delta / 2

if self._settings.value("zoom_y_center_on_cursor", True, type=bool):
y_pos_val, sig_y_top, sig_y_bottom = self.plot.value_at_cursor()
if isinstance(y_pos_val, (int, float)):
delta = y_range[1] - y_range[0]
y_range = y_pos_val - delta / 2, y_pos_val + delta / 2

self.setRange(xRange=x_range, yRange=y_range, padding=0)

ev.accept()
self.sigRangeChangedManually.emit(mask)

0 comments on commit 37db460

Please sign in to comment.