scanwidget: just do QWidget (337af80)

This commit is contained in:
Robert Jördens 2016-03-16 22:08:32 +01:00
parent a75ee76463
commit 5d5ef356f4
1 changed files with 17 additions and 60 deletions

View File

@ -9,13 +9,13 @@ from .ticker import Ticker
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class ScanWidget(QtWidgets.QSlider): class ScanWidget(QtWidgets.QWidget):
startChanged = QtCore.pyqtSignal(float) startChanged = QtCore.pyqtSignal(float)
stopChanged = QtCore.pyqtSignal(float) stopChanged = QtCore.pyqtSignal(float)
numChanged = QtCore.pyqtSignal(int) numChanged = QtCore.pyqtSignal(int)
def __init__(self, zoomFactor=1.05, zoomMargin=.1, dynamicRange=1e9): def __init__(self, zoomFactor=1.05, zoomMargin=.1, dynamicRange=1e9):
QtWidgets.QSlider.__init__(self, QtCore.Qt.Horizontal) QtWidgets.QWidget.__init__(self)
self.zoomMargin = zoomMargin self.zoomMargin = zoomMargin
self.dynamicRange = dynamicRange self.dynamicRange = dynamicRange
self.zoomFactor = zoomFactor self.zoomFactor = zoomFactor
@ -30,15 +30,13 @@ class ScanWidget(QtWidgets.QSlider):
action.triggered.connect(self.snapRange) action.triggered.connect(self.snapRange)
self.menu.addAction(action) self.menu.addAction(action)
self.setRange(0, 4095)
qfm = QtGui.QFontMetrics(self.font()) qfm = QtGui.QFontMetrics(self.font())
self._labelSize = QtCore.QSize( self._labelSize = QtCore.QSize(
(self.ticker.precision + 5)*qfm.averageCharWidth(), (self.ticker.precision + 5)*qfm.averageCharWidth(),
qfm.lineSpacing()) qfm.lineSpacing())
self._start, self._stop, self._num = None, None, None self._start, self._stop, self._num = None, None, None
self._axisView, self._sliderView = None, None self._axisView = None, None
self._offset, self._pressed, self._dragLeft = None, None, None self._offset, self._pressed, self._dragLeft = None, None, None
def contextMenuEvent(self, ev): def contextMenuEvent(self, ev):
@ -60,21 +58,7 @@ class ScanWidget(QtWidgets.QSlider):
return (val - a)/b return (val - a)/b
def _setView(self, axis_left, axis_scale): def _setView(self, axis_left, axis_scale):
opt = QtWidgets.QStyleOptionSlider()
self.initStyleOption(opt)
g = self.style().subControlRect(QtWidgets.QStyle.CC_Slider, opt,
QtWidgets.QStyle.SC_SliderGroove,
self)
h = self.style().subControlRect(QtWidgets.QStyle.CC_Slider, opt,
QtWidgets.QStyle.SC_SliderHandle,
self)
slider_left = g.x() + h.width()/2
slider_scale = (self.maximum() - self.minimum())/(
g.width() - h.width())
self._axisView = axis_left, axis_scale self._axisView = axis_left, axis_scale
self._sliderView = ((axis_left - slider_left)*slider_scale,
axis_scale*slider_scale)
self.update() self.update()
def setStart(self, val): def setStart(self, val):
@ -116,36 +100,12 @@ class ScanWidget(QtWidgets.QSlider):
self.setStart(self._pixelToAxis(self.zoomMargin*self.width())) self.setStart(self._pixelToAxis(self.zoomMargin*self.width()))
self.setStop(self._pixelToAxis((1 - self.zoomMargin)*self.width())) self.setStop(self._pixelToAxis((1 - self.zoomMargin)*self.width()))
def _getStyleOptionSlider(self, val):
a, b = self._sliderView
val = a + val*b
if not (self.minimum() <= val <= self.maximum()):
return None
opt = QtWidgets.QStyleOptionSlider()
self.initStyleOption(opt)
rect = self.rect()
qfm = QtGui.QFontMetrics(self.font())
opt.rect = QtCore.QRect(0, 3*qfm.lineSpacing(), rect.width(),
qfm.lineSpacing())
opt.sliderPosition = val
opt.sliderValue = val
opt.subControls = QtWidgets.QStyle.SC_SliderHandle
return opt
def _hitHandle(self, pos, val): def _hitHandle(self, pos, val):
opt = self._getStyleOptionSlider(val) qfm = QtGui.QFontMetrics(self.font())
if not opt: val = self._axisToPixel(val)
return False rect = QtCore.QRect(val - qfm.lineSpacing()/2, 3*qfm.lineSpacing(),
control = self.style().hitTestComplexControl( qfm.lineSpacing(), qfm.lineSpacing())
QtWidgets.QStyle.CC_Slider, opt, pos, self) return rect.contains(pos)
if control != QtWidgets.QStyle.SC_SliderHandle:
return False
sr = self.style().subControlRect(QtWidgets.QStyle.CC_Slider, opt,
QtWidgets.QStyle.SC_SliderHandle,
self)
self._offset = pos.x() - sr.center().x()
self.setSliderDown(True)
return True
def mousePressEvent(self, ev): def mousePressEvent(self, ev):
if ev.buttons() ^ ev.button(): if ev.buttons() ^ ev.button():
@ -153,8 +113,10 @@ class ScanWidget(QtWidgets.QSlider):
return return
if self._hitHandle(ev.pos(), self._stop): if self._hitHandle(ev.pos(), self._stop):
self._pressed = "stop" self._pressed = "stop"
self._offset = ev.x() - self._axisToPixel(self._stop)
elif self._hitHandle(ev.pos(), self._start): elif self._hitHandle(ev.pos(), self._start):
self._pressed = "start" self._pressed = "start"
self._offset = ev.x() - self._axisToPixel(self._stop)
else: else:
self._pressed = "axis" self._pressed = "axis"
self._offset = ev.x() self._offset = ev.x()
@ -167,20 +129,16 @@ class ScanWidget(QtWidgets.QSlider):
if self._pressed == "stop": if self._pressed == "stop":
self._stop = self._pixelToAxis(ev.x() - self._offset) self._stop = self._pixelToAxis(ev.x() - self._offset)
self.update() self.update()
if self.hasTracking(): self.stopChanged.emit(self._stop)
self.stopChanged.emit(self._stop)
elif self._pressed == "start": elif self._pressed == "start":
self._start = self._pixelToAxis(ev.x() - self._offset) self._start = self._pixelToAxis(ev.x() - self._offset)
self.update() self.update()
if self.hasTracking(): self.startChanged.emit(self._start)
self.startChanged.emit(self._start)
elif self._pressed == "axis": elif self._pressed == "axis":
self._setView(self._dragLeft + ev.x() - self._offset, self._setView(self._dragLeft + ev.x() - self._offset,
self._axisView[1]) self._axisView[1])
def mouseReleaseEvent(self, ev): def mouseReleaseEvent(self, ev):
QtWidgets.QSlider.mouseReleaseEvent(self, ev)
self.setSliderDown(False)
if self._pressed == "start": if self._pressed == "start":
self.startChanged.emit(self._start) self.startChanged.emit(self._start)
elif self._pressed == "stop": elif self._pressed == "stop":
@ -250,9 +208,8 @@ class ScanWidget(QtWidgets.QSlider):
for x, c in (self._start, QtCore.Qt.blue), (self._stop, QtCore.Qt.red): for x, c in (self._start, QtCore.Qt.blue), (self._stop, QtCore.Qt.red):
x = self._axisToPixel(x) x = self._axisToPixel(x)
if self.minimum() <= x <= self.maximum(): painter.setPen(c)
painter.setPen(c) painter.setBrush(c)
painter.setBrush(c) painter.drawPolygon(*(QtCore.QPointF(*i) for i in [
painter.drawPolygon(*(QtCore.QPointF(*i) for i in [ (x, 0), (x - lineSpacing/2, lineSpacing),
(x, 0), (x - lineSpacing/2, lineSpacing), (x + lineSpacing/2, lineSpacing)]))
(x + lineSpacing/2, lineSpacing)]))