From b04b5c8239f1cc8c8d691f0f62295d9631dc88ea Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Mon, 28 Mar 2016 16:59:49 +0200 Subject: [PATCH] scanwidget: handle min, max, suffix (closes #352) --- artiq/gui/entries.py | 17 +++++++------- artiq/gui/scanwidget.py | 51 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/artiq/gui/entries.py b/artiq/gui/entries.py index f0d48bd13..34fb8e078 100644 --- a/artiq/gui/entries.py +++ b/artiq/gui/entries.py @@ -139,20 +139,20 @@ class _RangeScan(LayoutWidget): scale = procdesc["scale"] - def apply_properties(spinbox): - spinbox.setDecimals(procdesc["ndecimals"]) + def apply_properties(widget): + widget.setDecimals(procdesc["ndecimals"]) if procdesc["global_min"] is not None: - spinbox.setMinimum(procdesc["global_min"]/scale) + widget.setMinimum(procdesc["global_min"]/scale) else: - spinbox.setMinimum(float("-inf")) + widget.setMinimum(float("-inf")) if procdesc["global_max"] is not None: - spinbox.setMaximum(procdesc["global_max"]/scale) + widget.setMaximum(procdesc["global_max"]/scale) else: - spinbox.setMaximum(float("inf")) + widget.setMaximum(float("inf")) if procdesc["global_step"] is not None: - spinbox.setSingleStep(procdesc["global_step"]/scale) + widget.setSingleStep(procdesc["global_step"]/scale) if procdesc["unit"]: - spinbox.setSuffix(" " + procdesc["unit"]) + widget.setSuffix(" " + procdesc["unit"]) scanner = ScanWidget() disable_scroll_wheel(scanner) @@ -200,6 +200,7 @@ class _RangeScan(LayoutWidget): scanner.setStop(state["stop"]/scale) apply_properties(start) apply_properties(stop) + apply_properties(scanner) class _ExplicitScan(LayoutWidget): diff --git a/artiq/gui/scanwidget.py b/artiq/gui/scanwidget.py index fd756390f..b6f72c94f 100644 --- a/artiq/gui/scanwidget.py +++ b/artiq/gui/scanwidget.py @@ -14,11 +14,12 @@ class ScanWidget(QtWidgets.QWidget): stopChanged = QtCore.pyqtSignal(float) numChanged = QtCore.pyqtSignal(int) - def __init__(self, zoomFactor=1.05, zoomMargin=.1, dynamicRange=1e9): + def __init__(self): QtWidgets.QWidget.__init__(self) - self.zoomMargin = zoomMargin - self.dynamicRange = dynamicRange - self.zoomFactor = zoomFactor + self.zoomMargin = .1 + self.zoomFactor = 1.05 + self.dynamicRange = 1e9 + self.suffix = "" self.ticker = Ticker() @@ -40,6 +41,7 @@ class ScanWidget(QtWidgets.QWidget): qfm.lineSpacing()) self._start, self._stop, self._num = None, None, None + self._min, self._max = None, None self._axisView = None self._offset, self._drag, self._rubber = None, None, None @@ -68,7 +70,15 @@ class ScanWidget(QtWidgets.QWidget): left = self.width()/2 - center*scale self._setView(left, scale) + def _clamp(self, v): + if self._min is not None: + v = max(self._min, v) + if self._max is not None: + v = min(self._max, v) + return v + def setStart(self, val): + val = self._clamp(val) if self._start == val: return self._start = val @@ -76,6 +86,7 @@ class ScanWidget(QtWidgets.QWidget): self.startChanged.emit(val) def setStop(self, val): + val = self._clamp(val) if self._stop == val: return self._stop = val @@ -89,6 +100,31 @@ class ScanWidget(QtWidgets.QWidget): self.update() self.numChanged.emit(val) + def setMinimum(self, v): + self._min = v + self.setStart(self._start) + self.setStop(self._stop) + + def setMaximum(self, v): + self._max = v + self.setStart(self._start) + self.setStop(self._stop) + + def setDecimals(self, n): + # TODO + # the axis should always use compressed notation is useful + # do not: + # self.ticker.precision = n + pass + + def setSingleStep(self, v): + # TODO + # use this (and maybe decimals) to snap to "nice" values when dragging + pass + + def setSuffix(self, v): + self.suffix = v + def viewRange(self): center = (self._stop + self._start)/2 scale = self.width()*(1 - 2*self.zoomMargin) @@ -207,8 +243,11 @@ class ScanWidget(QtWidgets.QWidget): ticks, prefix, labels = self.ticker(self._pixelToAxis(0), self._pixelToAxis(self.width())) - painter.drawText(0, 0, prefix) - painter.translate(0, lineSpacing) + rect = QtCore.QRect(0, 0, self.width(), lineSpacing) + painter.drawText(rect, QtCore.Qt.AlignLeft, prefix) + painter.drawText(rect, QtCore.Qt.AlignRight, self.suffix) + + painter.translate(0, lineSpacing + ascent) for t, l in zip(ticks, labels): t = self._axisToPixel(t)