scanwidget: painting (3237d2b)

This commit is contained in:
Robert Jördens 2016-03-16 21:13:14 +01:00
parent 67db5f77cd
commit 11bfe33dd4
1 changed files with 30 additions and 33 deletions

View File

@ -30,11 +30,6 @@ class ScanWidget(QtWidgets.QSlider):
action.triggered.connect(self.snapRange) action.triggered.connect(self.snapRange)
self.menu.addAction(action) self.menu.addAction(action)
self._startSlider = QtWidgets.QSlider()
self._startSlider.setStyleSheet("QSlider::handle {background:blue}")
self._stopSlider = QtWidgets.QSlider()
self._stopSlider.setStyleSheet("QSlider::handle {background:red}")
self.setRange(0, 4095) self.setRange(0, 4095)
self._start, self._stop, self._num = None, None, None self._start, self._stop, self._num = None, None, None
@ -44,6 +39,14 @@ class ScanWidget(QtWidgets.QSlider):
def contextMenuEvent(self, ev): def contextMenuEvent(self, ev):
self.menu.popup(ev.globalPos()) self.menu.popup(ev.globalPos())
def sizeHint(self):
return self.minimumSizeHint()
def minimumSizeHint(self):
qfm = QtGui.QFontMetrics(self.font())
return QtCore.QSize(5*10*qfm.averageCharWidth(),
4*qfm.lineSpacing())
def _axisToPixel(self, val): def _axisToPixel(self, val):
a, b = self._axisView a, b = self._axisView
return a + val*b return a + val*b
@ -116,6 +119,10 @@ class ScanWidget(QtWidgets.QSlider):
return None return None
opt = QtWidgets.QStyleOptionSlider() opt = QtWidgets.QStyleOptionSlider()
self.initStyleOption(opt) 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.sliderPosition = val
opt.sliderValue = val opt.sliderValue = val
opt.subControls = QtWidgets.QStyle.SC_SliderHandle opt.subControls = QtWidgets.QStyle.SC_SliderHandle
@ -205,52 +212,42 @@ class ScanWidget(QtWidgets.QSlider):
if center: if center:
scale = min(scale, self.dynamicRange/abs(center)) scale = min(scale, self.dynamicRange/abs(center))
left = a*scale/b left = a*scale/b
self.ticker.min_ticks = int(ev.size().width()/100)
self._setView(left, scale) self._setView(left, scale)
def paintEvent(self, ev): def paintEvent(self, ev):
self._paintSliders()
self._paintAxis()
def _paintAxis(self):
painter = QtGui.QPainter(self) painter = QtGui.QPainter(self)
qfm = QtGui.QFontMetrics(painter.font()) qfm = QtGui.QFontMetrics(painter.font())
avgCharWidth = qfm.averageCharWidth() avgCharWidth = qfm.averageCharWidth()
lineSpacing = qfm.lineSpacing() lineSpacing = qfm.lineSpacing()
descent = qfm.descent() descent = qfm.descent()
ascent = qfm.ascent() painter.translate(0, lineSpacing)
height = qfm.height()
# painter.setRenderHint(QtGui.QPainter.Antialiasing)
# TODO: make drawable area big enough and move axis higher
painter.translate(0, ascent - 15)
ticks, prefix, labels = self.ticker(self._pixelToAxis(0), ticks, prefix, labels = self.ticker(self._pixelToAxis(0),
self._pixelToAxis(self.width())) self._pixelToAxis(self.width()))
painter.drawText(0, 0, prefix) painter.drawText(0, 0, prefix)
pen = QtGui.QPen()
pen.setWidth(2)
painter.setPen(pen)
painter.translate(0, lineSpacing) painter.translate(0, lineSpacing)
painter.setPen(QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine))
for t, l in zip(ticks, labels): for t, l in zip(ticks, labels):
t = self._axisToPixel(t) t = self._axisToPixel(t)
painter.drawLine(t, descent, t, height/2)
painter.drawText(t - len(l)/2*avgCharWidth, 0, l) painter.drawText(t - len(l)/2*avgCharWidth, 0, l)
painter.drawLine(0, height/2, self.width(), height/2) painter.drawLine(t, descent, t, (lineSpacing + descent)/2)
painter.translate(0, (lineSpacing + descent)/2)
painter.drawLine(0, 0, self.width(), 0)
painter.translate(0, height)
for p in np.linspace(self._axisToPixel(self._start), for p in np.linspace(self._axisToPixel(self._start),
self._axisToPixel(self._stop), self._axisToPixel(self._stop),
self._num): self._num):
# TODO: is drawing far outside the viewport dangerous? painter.drawLine(p, 0, p, (lineSpacing - descent)/2)
painter.drawLine(p, 0, p, -height/2) painter.translate(0, (lineSpacing - descent)/2)
def _paintSliders(self): for x, c in (self._start, QtCore.Qt.blue), (self._stop, QtCore.Qt.red):
startPainter = QtWidgets.QStylePainter(self, self._startSlider) x = self._axisToPixel(x)
stopPainter = QtWidgets.QStylePainter(self, self._stopSlider) if self.minimum() <= x <= self.maximum():
opt = self._getStyleOptionSlider(self._start) painter.setPen(c)
if opt: painter.setBrush(c)
startPainter.drawComplexControl(QtWidgets.QStyle.CC_Slider, opt) painter.drawPolygon(*(QtCore.QPointF(*i) for i in [
opt = self._getStyleOptionSlider(self._stop) (x, 0), (x - lineSpacing/2, lineSpacing),
if opt: (x + lineSpacing/2, lineSpacing)]))
stopPainter.drawComplexControl(QtWidgets.QStyle.CC_Slider, opt)