2016-03-12 01:29:21 +08:00
|
|
|
import logging
|
|
|
|
|
2016-02-26 03:34:04 +08:00
|
|
|
from PyQt5 import QtGui, QtCore, QtWidgets
|
2016-03-16 22:13:31 +08:00
|
|
|
import numpy as np
|
2016-02-26 03:34:04 +08:00
|
|
|
|
2016-03-11 22:38:22 +08:00
|
|
|
from .ticker import Ticker
|
|
|
|
|
2016-02-26 03:34:04 +08:00
|
|
|
|
2016-03-12 01:29:21 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-03-17 05:08:32 +08:00
|
|
|
class ScanWidget(QtWidgets.QWidget):
|
2016-03-16 22:13:31 +08:00
|
|
|
startChanged = QtCore.pyqtSignal(float)
|
|
|
|
stopChanged = QtCore.pyqtSignal(float)
|
|
|
|
numChanged = QtCore.pyqtSignal(int)
|
|
|
|
|
2016-03-28 22:59:49 +08:00
|
|
|
def __init__(self):
|
2016-03-17 05:08:32 +08:00
|
|
|
QtWidgets.QWidget.__init__(self)
|
2016-03-28 22:59:49 +08:00
|
|
|
self.zoomMargin = .1
|
|
|
|
self.zoomFactor = 1.05
|
|
|
|
self.dynamicRange = 1e9
|
|
|
|
self.suffix = ""
|
2016-03-16 22:13:31 +08:00
|
|
|
|
2016-02-26 03:34:04 +08:00
|
|
|
self.ticker = Ticker()
|
|
|
|
|
2016-03-17 06:30:50 +08:00
|
|
|
self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
|
|
|
|
action = QtWidgets.QAction("V&iew range", self)
|
|
|
|
action.setShortcut(QtGui.QKeySequence("CTRL+i"))
|
|
|
|
action.setShortcutContext(QtCore.Qt.WidgetShortcut)
|
2016-03-16 22:13:31 +08:00
|
|
|
action.triggered.connect(self.viewRange)
|
2016-03-17 06:30:50 +08:00
|
|
|
self.addAction(action)
|
|
|
|
action = QtWidgets.QAction("Sna&p range", self)
|
|
|
|
action.setShortcut(QtGui.QKeySequence("CTRL+p"))
|
|
|
|
action.setShortcutContext(QtCore.Qt.WidgetShortcut)
|
2016-03-16 22:13:31 +08:00
|
|
|
action.triggered.connect(self.snapRange)
|
2016-03-17 06:30:50 +08:00
|
|
|
self.addAction(action)
|
2016-03-11 22:38:22 +08:00
|
|
|
|
2016-03-17 04:25:45 +08:00
|
|
|
qfm = QtGui.QFontMetrics(self.font())
|
|
|
|
self._labelSize = QtCore.QSize(
|
|
|
|
(self.ticker.precision + 5)*qfm.averageCharWidth(),
|
|
|
|
qfm.lineSpacing())
|
|
|
|
|
2016-03-16 22:13:31 +08:00
|
|
|
self._start, self._stop, self._num = None, None, None
|
2016-06-12 06:52:48 +08:00
|
|
|
self._min, self._max = float("-inf"), float("inf")
|
2016-03-17 06:05:23 +08:00
|
|
|
self._axisView = None
|
2016-03-18 07:07:28 +08:00
|
|
|
self._offset, self._drag, self._rubber = None, None, None
|
2016-02-26 03:34:04 +08:00
|
|
|
|
2016-03-17 04:13:14 +08:00
|
|
|
def sizeHint(self):
|
|
|
|
return self.minimumSizeHint()
|
|
|
|
|
|
|
|
def minimumSizeHint(self):
|
2016-03-17 04:25:45 +08:00
|
|
|
return QtCore.QSize(2.5*3*self._labelSize.width(),
|
|
|
|
4*self._labelSize.height())
|
2016-03-17 04:13:14 +08:00
|
|
|
|
2016-03-16 22:13:31 +08:00
|
|
|
def _axisToPixel(self, val):
|
|
|
|
a, b = self._axisView
|
|
|
|
return a + val*b
|
|
|
|
|
|
|
|
def _pixelToAxis(self, val):
|
|
|
|
a, b = self._axisView
|
|
|
|
return (val - a)/b
|
|
|
|
|
2016-03-19 04:53:06 +08:00
|
|
|
def _setView(self, left, scale):
|
|
|
|
self._axisView = left, scale
|
2016-03-16 22:13:31 +08:00
|
|
|
self.update()
|
|
|
|
|
2016-03-19 04:53:06 +08:00
|
|
|
def _setViewAxis(self, center, scale):
|
|
|
|
if center:
|
|
|
|
scale = min(scale, self.dynamicRange/abs(center))
|
|
|
|
left = self.width()/2 - center*scale
|
|
|
|
self._setView(left, scale)
|
|
|
|
|
2016-03-28 22:59:49 +08:00
|
|
|
def _clamp(self, v):
|
2016-06-12 06:48:58 +08:00
|
|
|
if v is None:
|
|
|
|
return None
|
2016-06-12 06:52:48 +08:00
|
|
|
v = max(self._min, v)
|
|
|
|
v = min(self._max, v)
|
2016-03-28 22:59:49 +08:00
|
|
|
return v
|
|
|
|
|
2016-03-16 22:13:31 +08:00
|
|
|
def setStart(self, val):
|
2016-03-28 22:59:49 +08:00
|
|
|
val = self._clamp(val)
|
2016-03-16 22:13:31 +08:00
|
|
|
if self._start == val:
|
|
|
|
return
|
|
|
|
self._start = val
|
|
|
|
self.update()
|
|
|
|
self.startChanged.emit(val)
|
|
|
|
|
|
|
|
def setStop(self, val):
|
2016-03-28 22:59:49 +08:00
|
|
|
val = self._clamp(val)
|
2016-03-16 22:13:31 +08:00
|
|
|
if self._stop == val:
|
|
|
|
return
|
|
|
|
self._stop = val
|
|
|
|
self.update()
|
|
|
|
self.stopChanged.emit(val)
|
|
|
|
|
|
|
|
def setNum(self, val):
|
|
|
|
if self._num == val:
|
|
|
|
return
|
|
|
|
self._num = val
|
|
|
|
self.update()
|
|
|
|
self.numChanged.emit(val)
|
|
|
|
|
2016-03-28 22:59:49 +08:00
|
|
|
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
|
2016-04-20 22:29:39 +08:00
|
|
|
# the axis should always use the most compact notation
|
2016-03-28 22:59:49 +08:00
|
|
|
# do not:
|
|
|
|
# self.ticker.precision = n
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setSingleStep(self, v):
|
|
|
|
# TODO
|
2016-04-20 22:29:39 +08:00
|
|
|
# use this (and/or maybe decimals) to snap to "nice" values when
|
|
|
|
# dragging
|
2016-03-28 22:59:49 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
def setSuffix(self, v):
|
|
|
|
self.suffix = v
|
|
|
|
|
2016-03-16 22:13:31 +08:00
|
|
|
def viewRange(self):
|
|
|
|
center = (self._stop + self._start)/2
|
|
|
|
scale = self.width()*(1 - 2*self.zoomMargin)
|
|
|
|
if self._stop != self._start:
|
|
|
|
scale /= abs(self._stop - self._start)
|
|
|
|
else:
|
|
|
|
scale = self.dynamicRange
|
2016-03-19 04:53:06 +08:00
|
|
|
self._setViewAxis(center, scale)
|
2016-03-16 22:13:31 +08:00
|
|
|
|
|
|
|
def snapRange(self):
|
|
|
|
self.setStart(self._pixelToAxis(self.zoomMargin*self.width()))
|
|
|
|
self.setStop(self._pixelToAxis((1 - self.zoomMargin)*self.width()))
|
2016-02-26 03:34:04 +08:00
|
|
|
|
|
|
|
def mousePressEvent(self, ev):
|
2016-03-17 06:05:23 +08:00
|
|
|
if ev.buttons() ^ ev.button(): # buttons changed
|
2016-02-26 03:34:04 +08:00
|
|
|
ev.ignore()
|
|
|
|
return
|
2016-03-18 07:07:28 +08:00
|
|
|
if ev.modifiers() & QtCore.Qt.ShiftModifier:
|
|
|
|
self._drag = "select"
|
|
|
|
self.setStart(self._pixelToAxis(ev.x()))
|
|
|
|
self.setStop(self._start)
|
|
|
|
elif ev.modifiers() & QtCore.Qt.ControlModifier:
|
|
|
|
self._drag = "zoom"
|
2016-03-19 04:53:06 +08:00
|
|
|
self._offset = QtCore.QPoint(ev.x(), 0)
|
2016-03-18 07:07:28 +08:00
|
|
|
self._rubber = QtWidgets.QRubberBand(
|
|
|
|
QtWidgets.QRubberBand.Rectangle, self)
|
2016-03-19 04:53:06 +08:00
|
|
|
self._rubber.setGeometry(QtCore.QRect(
|
|
|
|
self._offset, QtCore.QPoint(ev.x(), self.height() - 1)))
|
2016-03-18 07:07:28 +08:00
|
|
|
self._rubber.show()
|
2016-03-14 18:45:26 +08:00
|
|
|
else:
|
2016-03-18 07:07:28 +08:00
|
|
|
qfm = QtGui.QFontMetrics(self.font())
|
|
|
|
if ev.y() <= 2.5*qfm.lineSpacing():
|
|
|
|
self._drag = "axis"
|
|
|
|
self._offset = ev.x() - self._axisView[0]
|
|
|
|
# testing should match inverse drawing order for start/stop
|
|
|
|
elif abs(self._axisToPixel(self._stop) -
|
|
|
|
ev.x()) < qfm.lineSpacing()/2:
|
|
|
|
self._drag = "stop"
|
|
|
|
self._offset = ev.x() - self._axisToPixel(self._stop)
|
|
|
|
elif abs(self._axisToPixel(self._start) -
|
|
|
|
ev.x()) < qfm.lineSpacing()/2:
|
|
|
|
self._drag = "start"
|
|
|
|
self._offset = ev.x() - self._axisToPixel(self._start)
|
|
|
|
else:
|
|
|
|
self._drag = "both"
|
|
|
|
self._offset = (ev.x() - self._axisToPixel(self._start),
|
|
|
|
ev.x() - self._axisToPixel(self._stop))
|
2016-02-26 03:34:04 +08:00
|
|
|
|
|
|
|
def mouseMoveEvent(self, ev):
|
2016-03-17 06:05:23 +08:00
|
|
|
if not self._drag:
|
2016-02-26 03:34:04 +08:00
|
|
|
ev.ignore()
|
|
|
|
return
|
2016-03-18 07:07:28 +08:00
|
|
|
if self._drag == "select":
|
|
|
|
self.setStop(self._pixelToAxis(ev.x()))
|
|
|
|
elif self._drag == "zoom":
|
|
|
|
self._rubber.setGeometry(QtCore.QRect(
|
2016-03-19 04:53:06 +08:00
|
|
|
self._offset, QtCore.QPoint(ev.x(), self.height() - 1)
|
|
|
|
).normalized())
|
2016-03-18 07:07:28 +08:00
|
|
|
elif self._drag == "axis":
|
2016-03-17 06:05:23 +08:00
|
|
|
self._setView(ev.x() - self._offset, self._axisView[1])
|
|
|
|
elif self._drag == "start":
|
2016-03-18 07:07:28 +08:00
|
|
|
self.setStart(self._pixelToAxis(ev.x() - self._offset))
|
|
|
|
elif self._drag == "stop":
|
|
|
|
self.setStop(self._pixelToAxis(ev.x() - self._offset))
|
2016-03-17 06:05:23 +08:00
|
|
|
elif self._drag == "both":
|
2016-03-18 07:07:28 +08:00
|
|
|
self.setStart(self._pixelToAxis(ev.x() - self._offset[0]))
|
|
|
|
self.setStop(self._pixelToAxis(ev.x() - self._offset[1]))
|
2016-03-17 06:05:23 +08:00
|
|
|
|
|
|
|
def mouseReleaseEvent(self, ev):
|
2016-03-18 07:07:28 +08:00
|
|
|
if self._drag == "zoom":
|
|
|
|
self._rubber.hide()
|
2016-03-18 16:04:35 +08:00
|
|
|
if self._rubber.geometry().width():
|
2016-03-19 04:53:06 +08:00
|
|
|
scale = self._axisView[1]
|
2016-03-18 16:04:35 +08:00
|
|
|
scale *= self.width()/self._rubber.geometry().width()
|
2016-03-19 04:53:06 +08:00
|
|
|
center = self._pixelToAxis(
|
|
|
|
self._rubber.geometry().center().x())
|
|
|
|
self._setViewAxis(center, scale)
|
2016-03-17 06:05:23 +08:00
|
|
|
self._drag = None
|
2016-03-16 22:13:31 +08:00
|
|
|
|
|
|
|
def _zoom(self, z, x):
|
|
|
|
a, b = self._axisView
|
|
|
|
scale = z*b
|
2016-03-19 04:53:06 +08:00
|
|
|
left = x + z*(a - x)
|
2016-03-16 22:13:31 +08:00
|
|
|
if z > 1 and abs(left - self.width()/2) > self.dynamicRange:
|
2016-03-14 18:45:26 +08:00
|
|
|
return
|
2016-03-16 22:13:31 +08:00
|
|
|
self._setView(left, scale)
|
2016-03-14 18:45:26 +08:00
|
|
|
|
2016-03-15 01:06:18 +08:00
|
|
|
def wheelEvent(self, ev):
|
2016-03-16 22:13:31 +08:00
|
|
|
y = ev.angleDelta().y()/120.
|
2016-03-17 06:47:28 +08:00
|
|
|
if not y:
|
|
|
|
return
|
2016-03-14 18:45:26 +08:00
|
|
|
if ev.modifiers() & QtCore.Qt.ShiftModifier:
|
2016-03-17 06:47:28 +08:00
|
|
|
self.setNum(max(1, self._num + y))
|
2016-03-14 18:45:26 +08:00
|
|
|
else:
|
2016-03-17 06:47:28 +08:00
|
|
|
self._zoom(self.zoomFactor**y, ev.x())
|
2016-02-26 03:34:04 +08:00
|
|
|
|
2016-03-14 23:53:06 +08:00
|
|
|
def resizeEvent(self, ev):
|
2017-10-12 04:25:14 +08:00
|
|
|
if not ev.oldSize().isValid() or not ev.oldSize().width():
|
2016-03-14 18:45:26 +08:00
|
|
|
self.viewRange()
|
2016-03-16 22:13:31 +08:00
|
|
|
return
|
2016-03-17 07:30:13 +08:00
|
|
|
self.ticker.min_ticks = max(
|
|
|
|
3, int(ev.size().width()/(2.5*self._labelSize.width())))
|
2016-03-19 04:53:06 +08:00
|
|
|
scale = self._axisView[1]
|
|
|
|
scale *= ev.size().width()/ev.oldSize().width()
|
|
|
|
center = self._pixelToAxis(ev.oldSize().width()/2)
|
|
|
|
self._setViewAxis(center, scale)
|
2016-03-14 23:53:06 +08:00
|
|
|
|
2016-03-16 22:13:31 +08:00
|
|
|
def paintEvent(self, ev):
|
|
|
|
painter = QtGui.QPainter(self)
|
|
|
|
qfm = QtGui.QFontMetrics(painter.font())
|
|
|
|
avgCharWidth = qfm.averageCharWidth()
|
|
|
|
lineSpacing = qfm.lineSpacing()
|
|
|
|
descent = qfm.descent()
|
2016-03-17 06:05:23 +08:00
|
|
|
ascent = qfm.ascent()
|
|
|
|
painter.translate(0, ascent)
|
2016-03-16 22:13:31 +08:00
|
|
|
|
|
|
|
ticks, prefix, labels = self.ticker(self._pixelToAxis(0),
|
|
|
|
self._pixelToAxis(self.width()))
|
2016-03-28 22:59:49 +08:00
|
|
|
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)
|
2016-03-17 04:13:14 +08:00
|
|
|
|
2016-03-16 22:13:31 +08:00
|
|
|
for t, l in zip(ticks, labels):
|
|
|
|
t = self._axisToPixel(t)
|
|
|
|
painter.drawText(t - len(l)/2*avgCharWidth, 0, l)
|
2016-03-17 06:05:23 +08:00
|
|
|
painter.drawLine(t, descent, t, lineSpacing/2)
|
|
|
|
painter.translate(0, lineSpacing/2)
|
2016-03-17 04:13:14 +08:00
|
|
|
|
|
|
|
painter.drawLine(0, 0, self.width(), 0)
|
2016-03-16 22:13:31 +08:00
|
|
|
|
|
|
|
for p in np.linspace(self._axisToPixel(self._start),
|
|
|
|
self._axisToPixel(self._stop),
|
|
|
|
self._num):
|
2016-03-17 06:05:23 +08:00
|
|
|
painter.drawLine(p, 0, p, lineSpacing/2)
|
|
|
|
painter.translate(0, lineSpacing/2)
|
2016-03-17 04:13:14 +08:00
|
|
|
|
|
|
|
for x, c in (self._start, QtCore.Qt.blue), (self._stop, QtCore.Qt.red):
|
|
|
|
x = self._axisToPixel(x)
|
2016-03-17 05:08:32 +08:00
|
|
|
painter.setPen(c)
|
|
|
|
painter.setBrush(c)
|
|
|
|
painter.drawPolygon(*(QtCore.QPointF(*i) for i in [
|
|
|
|
(x, 0), (x - lineSpacing/2, lineSpacing),
|
|
|
|
(x + lineSpacing/2, lineSpacing)]))
|