def paintEvent(self, event): painter = QPainter(self) rect = painter.window() dia = min(rect.width(), rect.height()) - 40 if dia <= 0: return dia_in = int(dia * 4/9.) painter.setPen(self.p_black) painter.setWindow(-dia/2, -dia/2, dia, dia) v = painter.viewport() painter.setViewport(v.left() + (v.width() - dia)/2, v.top() + (v.height() - dia)/2, dia, dia) # outer circle painter.setBrush(self.br_back) painter.drawEllipse(-dia/2, -dia/2, dia, dia) # incoming beam painter.setBrush(self.br_empty) painter.drawPie(-dia/2, -dia/2, dia, dia, 16*(90 - IN_WIDTH/2), 16*IN_WIDTH) painter.setBrush(self.br_seg) start = IN_WIDTH / 2 + 90 for _ in range(MAX_SEGS - self.values[4]): painter.drawPie(-dia/2, -dia/2, dia, dia, 16*start, 16*SEG_ANGLE) start += SEG_ANGLE painter.setBrush(self.br_empty) painter.drawPie(-dia/2, -dia/2, dia, dia, 16*start, 16*OUT_WIDTH) painter.setBrush(self.br_seg) start = -IN_WIDTH / 2 + 90 for _ in range(self.values[4]): painter.drawPie(-dia/2, -dia/2, dia, dia, 16*start, -16*SEG_ANGLE) start -= SEG_ANGLE # inner circle painter.drawEllipse(-dia_in/2, -dia_in/2, dia_in, dia_in) # outgoing beam (mtt angle) painter.drawLine(0, -dia/2, 0, 0) painter.rotate(self.values[3]) painter.drawLine(0, 0, 0, dia/2) painter.rotate(-self.values[3]) # mobil arm angle painter.setPen(self.p_red) painter.rotate(self.values[2]) painter.drawLine(0, 0, 0, -(dia + dia_in)/4) painter.rotate(-self.values[2]) painter.setPen(self.p_black) # switches swvals = self.values[0] | self.values[1] << 16 for (bit, phi, r, talign, text) in SWITCHES: on = swvals & (1 << bit) != 0 r = (r / 1800) * dia posx = r * cos(radians(phi - 90)) posy = r * sin(radians(phi - 90)) painter.setBrush(self.br_led[on]) painter.drawEllipse(posx - 5, posy - 5, 10, 10) if talign == 1: painter.drawText(posx - 210, posy - 5, 200, 20, Qt.AlignRight | Qt.AlignTop, text) elif talign == 2: painter.drawText(posx + 10, posy - 5, 200, 20, Qt.AlignLeft | Qt.AlignTop, text) else: painter.drawText(posx - 100, posy - 20, 200, 20, Qt.AlignHCenter | Qt.AlignTop, text)
def paintEvent(self, event): s = self.size() w, h = s.width(), s.height() # calculate the maximum length if all elements in a line maxL = self.values['Lms'] + self.values['Lsa'] + self.values['Lad'] # add the size of the Monochromator and detector scale = min((w / 2 - anaradius) / float(maxL), (h - monoradius - anaradius) / float(maxL)) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(QColor('black')) painter.setBrush(_white) painter.drawRect(0, 0, w, h) # determine positions # incoming beam if self.values['mth'] < 0: bx, by = 3, h - (2 + monoradius) else: bx, by = 3, 2 + monoradius # monochromator mx, my = w / 2., by # sample L = self.values['Lms'] * scale # length is in mm -- scale down a bit mttangle = self.values['mtt'] * deg2rad mttangle_t = self.targets['mtt'] * deg2rad if self.values['mth'] < 0: mttangle = -mttangle mttangle_t = -mttangle_t sx, sy = mx + L * cos(mttangle), my - L * sin(mttangle) sx_t, sy_t = mx + L * cos(mttangle_t), my - L * sin(mttangle_t) # analyzer L = self.values['Lsa'] * scale # length is in mm -- scale down a bit sttangle = self.values['stt'] * deg2rad sttangle_t = self.targets['stt'] * deg2rad if self.values['sth'] < 0: sttangle = mttangle - sttangle sttangle_t = mttangle_t - sttangle_t else: sttangle = mttangle + sttangle sttangle_t = mttangle_t + sttangle_t ax, ay = sx + L * cos(sttangle), sy - L * sin(sttangle) ax_t, ay_t = sx_t + L * cos(sttangle_t), sy_t - L * sin(sttangle_t) # detector L = self.values['Lad'] * scale # length is in mm -- scale down a bit attangle = self.values['att'] * deg2rad attangle_t = self.targets['att'] * deg2rad if self.values['ath'] < 0: attangle = sttangle - attangle attangle_t = sttangle_t - attangle_t else: attangle = sttangle + attangle attangle_t = sttangle_t + attangle_t dx, dy = ax + L * cos(attangle), ay - L * sin(attangle) dx_t, dy_t = ax_t + L * cos(attangle_t), ay_t - L * sin(attangle_t) # draw table "halos" painter.setPen(nopen) if self.status['mth'] != OK: painter.setBrush(statusbrush[self.status['mth']]) painter.drawEllipse(QPoint(mx, my), monoradius + halowidth, monoradius + halowidth) elif self.status['mtt'] != OK: painter.setBrush(statusbrush[self.status['mtt']]) painter.drawEllipse(QPoint(mx, my), monoradius + halowidth, monoradius + halowidth) if self.status['sth'] != OK: painter.setBrush(statusbrush[self.status['sth']]) painter.drawEllipse(QPoint(sx, sy), sampleradius + halowidth, sampleradius + halowidth) elif self.status['stt'] != OK: painter.setBrush(statusbrush[self.status['stt']]) painter.drawEllipse(QPoint(sx, sy), sampleradius + halowidth, sampleradius + halowidth) if self.status['ath'] != OK: painter.setBrush(statusbrush[self.status['ath']]) painter.drawEllipse(QPoint(ax, ay), anaradius + halowidth, anaradius + halowidth) elif self.status['att'] != OK: painter.setBrush(statusbrush[self.status['att']]) painter.drawEllipse(QPoint(ax, ay), anaradius + halowidth, anaradius + halowidth) # draw table targets painter.setPen(targetpen) painter.setBrush(_nobrush) painter.drawEllipse(QPoint(sx_t, sy_t), sampleradius - .5, sampleradius - .5) painter.drawEllipse(QPoint(ax_t, ay_t), anaradius - .5, anaradius - .5) painter.drawEllipse(QPoint(dx_t, dy_t), detradius - .5, detradius - .5) # draw the tables painter.setPen(defaultpen) painter.setBrush(monotablebrush) painter.drawEllipse(QPoint(mx, my), monoradius, monoradius) painter.setBrush(sampletablebrush) painter.drawEllipse(QPoint(sx, sy), sampleradius, sampleradius) painter.setBrush(anatablebrush) painter.drawEllipse(QPoint(ax, ay), anaradius, anaradius) painter.setBrush(dettablebrush) painter.drawEllipse(QPoint(dx, dy), detradius, detradius) painter.setBrush(_white) painter.setPen(nopen) painter.drawEllipse(QPoint(mx, my), monoradius / 2, monoradius / 2) # painter.drawEllipse(QPoint(sx, sy), 20, 20) painter.drawEllipse(QPoint(ax, ay), anaradius / 2, anaradius / 2) # painter.drawEllipse(QPoint(dx, dy), 20, 20) beam = QPolygonF([ QPointF(bx, by), QPointF(mx, my), QPointF(sx, sy), QPointF(ax, ay), QPointF(dx, dy) ]) painter.setPen(beambackgroundpen) painter.drawPolyline(beam) # draw mono crystals painter.setPen(monopen) mthangle = -self.values['mth'] * deg2rad painter.drawLine(mx + 10 * cos(mthangle), my - 10 * sin(mthangle), mx - 10 * cos(mthangle), my + 10 * sin(mthangle)) # draw ana crystals athangle = -self.values['ath'] * deg2rad alpha = athangle + sttangle # TODO if the angle is too small then it could be that the ath value # must be turned by 90 deg (PANDA: chair setup) ?? if attangle < 0 and alpha < attangle: alpha += pi_2 painter.drawLine(ax + 10 * cos(alpha), ay - 10 * sin(alpha), ax - 10 * cos(alpha), ay + 10 * sin(alpha)) # draw sample painter.setPen(samplepen) painter.setBrush(samplebrush) sthangle = self.values['sth'] * deg2rad alpha = sthangle + mttangle + pi_4 # painter.drawRect(sx - 5, sy - 5, 10, 10) sz = 10 painter.drawPolygon( QPolygonF([ QPointF(sx + sz * cos(alpha), sy - sz * sin(alpha)), QPointF(sx + sz * cos(alpha + pi_2), sy - sz * sin(alpha + pi_2)), QPointF(sx - sz * cos(alpha), sy + sz * sin(alpha)), QPointF(sx - sz * cos(alpha + pi_2), sy + sz * sin(alpha + pi_2)), QPointF(sx + sz * cos(alpha), sy - sz * sin(alpha)) ])) painter.setPen(samplecoordpen) sr = sampleradius for angle in [alpha - pi_4, alpha - 3 * pi_4]: painter.drawLine(sx - sr * cos(angle), sy + sr * sin(angle), sx + sr * cos(angle), sy - sr * sin(angle)) # draw detector painter.setPen(monopen) painter.setBrush(_white) painter.drawEllipse(QPoint(dx, dy), 4, 4) # draw beam painter.setPen(beampen) painter.drawPolyline(beam)