def boundingRect(self): # This refreshes the bounding rect self.setLine(QLineF(self._start.scenePos(), self._end.scenePos())) return QGraphicsLineItem.boundingRect(self)
class RefsansView(QGraphicsView): pivotdist = 20 def __init__(self, parent=None): scene = QGraphicsScene() QGraphicsView.__init__(self, scene) self.setRenderHints(QPainter.Antialiasing) self._tube = Tube(scene=scene) self._tube.setRotation(1) self._yoke = Yoke(scene=scene) t1 = QTransform() t1.translate(500, 0) self._yoke.setTransform(t1) self._pivotline = QGraphicsLineItem(-200, 0, 700, 0) scene.addItem(self._pivotline) self._pivot = QGraphicsRectItem(0, 0, 6, 10) t2 = QTransform() t2.translate(-3, -10) self._pivot.setTransform(t2) self._pivot.setBrush(QBrush(QColor('#1F1F1F'))) scene.addItem(self._pivot) for i in range(13): txt = '%d' % (i + 1) self.t = QGraphicsTextItem(txt) bw = self.t.boundingRect().size().width() chrwidth = bw / (len(txt) + 1) x = (i - 9) * self.pivotdist - chrwidth / 2 + bw / len(txt) self.t.setX(x) scene.addItem(self.t) self._det = Detector(parent=self._tube, scene=scene) self.values = { 'tubeangle': -1.0, 'pivot': 1, 'detpos': 620, } self.targets = self.values.copy() self.status = { 'tubeangle': status.OK, 'pivot': status.OK, 'detpos': status.OK, } def resizeEvent(self, rsevent): s = rsevent.size() w, h = s.width(), s.height() scale = min( w / self._pivotline.boundingRect().width(), h / (self._yoke.boundingRect().height() + 10 + self.t.boundingRect().y() + self.t.boundingRect().height())) transform = self.transform() transform.reset() transform.scale(scale, scale) self.setTransform(transform) QGraphicsView.resizeEvent(self, rsevent) def update(self): self._tube.setState(self.status['tubeangle']) self._tube.setRotation(-self.values['tubeangle']) p = (self.values['pivot'] - 9) * self.pivotdist self._pivot.setX(p) self._tube.setX(p) self._yoke.setX(p) self._det.setState(self.status['detpos']) # 1 / 20 is the scaling of the tube length self._det.setX(self.values['detpos'] / 20) QGraphicsView.update(self)