def paint(self, painter, index): widget = self.parent() if index != widget.activeIndex(): return scale = widget.inverseScale() # metrics if self._rulerObject is not None: line, a = self._rulerObject origin = line.p1() cursor = line.p2() size = 8 * scale halfSize = 4 * scale color = QColor(255, 85, 127, 170) # line painter.save() painter.setPen(color) drawing.drawLine(painter, origin.x(), origin.y(), cursor.x(), cursor.y(), scale) # ellipses ellipses = [ (origin.x(), origin.y()), (cursor.x(), cursor.y()), ] path = QPainterPath() path.setFillRule(Qt.WindingFill) for x, y in itertools.chain(self._rulerPts.values(), ellipses): x -= halfSize y -= halfSize path.addEllipse(x, y, size, size) painter.fillPath(path, color) painter.restore() # text line = QLineF(line) xAlign = yAlign = "center" ellipses.pop(0) rp = self._rulerPts # XXX: sort shouldn't be performed in paintEvent for pt in itertools.chain((rp[k] for k in sorted(rp)), ellipses): p = QPointF(*pt) line.setP2(p) if line.length(): d = str(round(line.length(), 1)) pos = (line.p1() + line.p2()) / 2 drawing.drawTextAtPoint(painter, d, pos.x(), pos.y(), scale, xAlign, yAlign) line.setP1(p) xAlign, yAlign = "left", "top" dx = cursor.x() - origin.x() px = size if dx < 0: xAlign = "right" px = -px drawing.drawTextAtPoint(painter, a, cursor.x() + px, cursor.y() + size, scale, xAlign, yAlign)
def _draw_arrow(self, event, painter): if self.info["mouth_is_opened"][0] == False or self.prediction == 4: return angles = [135, 90, 45, 180, None, 0, 225, 270, 0] angleline = QLineF() angleline.setP1(QPointF(*_mouse_controller.position)) angleline.setAngle(angles[self.prediction]) angleline.setLength(100) painter.setPen(QPen(Qt.red, 5, Qt.SolidLine)) painter.drawLine(angleline)
def __activeControlMoved(self, pos): line = QLineF(self.__line) control = self.__activeControl if control.anchor() == ControlPoint.TopLeft: line.setP1(pos) elif control.anchor() == ControlPoint.BottomRight: line.setP2(pos) if self.__line != line: self.blockSignals(True) self.setLine(line) self.blockSignals(False) self.lineEdited.emit(line)
def rotateUIPointAroundRefLine(x1, y1, x2, y2, pt): """ Given three points p1, p2, pt this rotates pt around p2 such that p1,p2 and p1,pt are collinear. """ line = QLineF(pt.x, pt.y, x2, y2) p2p_l = line.length() line.setP1(QPointF(x1, y1)) p1p2_l = line.length() if not p1p2_l: return line.setLength(p1p2_l + p2p_l) pt.x = line.x2() pt.y = line.y2()
def __init__(self,src:DeviceItem,des:DeviceItem): super().__init__() self.setZValue(10) self.setOpacity(0.6) self.linkPen.setWidth(3) self.linkPen.setColor(Qt.darkBlue) self.setPen(self.linkPen) self.src = src self.des = des line_t = QLineF() line_t.setP1(src.pos() + src.boundingRect().center()) line_t.setP2(des.pos() +des.boundingRect().center()) self.setLine(line_t) #print("P1X : {0:3} P1Y :{1:3}".format(line.p1().x(),line.p1().y())) #print("P2X : {0:3} P2Y :{1:3}".format(line.p2().x(),line.p2().y())) self.setLine(line_t)
def addline(self, event): ''' 获取触摸点 :param event: 触摸事件对象 :return: ''' touchPoints = event.touchPoints() for point in touchPoints: # logger.debug('触摸点的直径:', point.ellipseDiameters()) print(point.ellipseDiameters().width()) self.penWidth = point.ellipseDiameters().width() self.lastPoint_t = point.lastPos() self.endPoint_t = point.pos() print('lastPoint', self.lastPoint_t) print('endPoint', self.endPoint_t) line = QLineF() line.setP1(self.lastPoint_t) line.setP2(self.lastPoint_t) self.lines.append(line) self.paintTouchEvent() # 手动调绘图事件
def drawWidget(self, qp): c = self.rect().center() c_coords = c.x(), c.y() background_color = self.palette().color(QPalette.Background) # paint inner trackpad qp.setPen(QPen(self.fishbowl_color, self.fishbowl_border_size, Qt.SolidLine)) # draw fishbowl qp.setBrush(QBrush(Qt.gray, Qt.SolidPattern)) qp.drawEllipse(c, *([self.fishbowl_size] * 2)) # draw axis lines qp.setPen(QPen(self.fishbowl_color, self.fishbowl_thin_border_size, Qt.DashDotDotLine)) for angle in range(0, 420, 45): line = QLineF(); line.setP1(c); line.setAngle(angle); line.setLength(self.fishbowl_size) qp.drawLine(line) # draw wheel separators line = QLineF(); line.setP1(c + QPoint(self.wheel_size, 0)); line.setAngle(0); line.setLength(self.wheel_width) qp.drawLine(line) line = QLineF(); line.setP1(c + QPoint(0, -self.wheel_size)); line.setAngle(90); line.setLength(self.wheel_width) qp.drawLine(line) line = QLineF(); line.setP1(c + QPoint(-self.wheel_size, 0)); line.setAngle(180); line.setLength(self.wheel_width) qp.drawLine(line) line = QLineF(); line.setP1(c + QPoint(0, self.wheel_size)); line.setAngle(270); line.setLength(self.wheel_width) qp.drawLine(line) qp.setPen(QPen(self.fishbowl_color, self.fishbowl_border_size, Qt.SolidLine)) # draw dead enemies for i, enemy in enumerate([x for x in self.enemies if x.dead]): qp.setBrush(QBrush(enemy.color, Qt.SolidPattern)) qp.drawEllipse(c + QPoint(*self.scale_point(enemy.coords)), *([self.npc_size] * 2)) # draw alive enemies for i, enemy in enumerate([x for x in self.enemies if not x.dead]): qp.setBrush(QBrush(enemy.color, Qt.SolidPattern)) qp.drawEllipse(c + QPoint(*self.scale_point(enemy.coords)), *([self.npc_size] * 2)) # draw player qp.setBrush(QBrush(self.player.color, Qt.SolidPattern)) qp.drawEllipse(c + QPoint(*self.scale_point(self.player.coords)), *([self.npc_size] * 2))
def __init__(self, position, angle, arrow_size, value, unit): super(StressRepresentation, self).__init__() arrow_line = QLineF() arrow_line.setP1(position) arrow_line.setLength(arrow_size) arrow_line.setAngle(angle) arrow = QGraphicsLineItem() arrow.setLine(arrow_line) self.addToGroup(arrow) arrow_head1_line = QLineF() arrow_head1_line.setP1(arrow_line.p1()) arrow_head1_line.setLength(arrow_size / 4) arrow_head1_line.setAngle(arrow_line.angle() + 45) arrow_head1 = QGraphicsLineItem() arrow_head1.setLine(arrow_head1_line) self.addToGroup(arrow_head1) arrow_head2_line = QLineF() arrow_head2_line.setP1(arrow_line.p1()) arrow_head2_line.setLength(arrow_size / 4) arrow_head2_line.setAngle(arrow_line.angle() - 45) arrow_head2 = QGraphicsLineItem() arrow_head2.setLine(arrow_head2_line) self.addToGroup(arrow_head2) text = QGraphicsTextItem() text.setPlainText(f'{str(value)}{unit}') text.setPos(arrow_line.p2()) self.addToGroup(text) self._set_color()
def mouseMoveEvent(self, event): path, text = self._rulerObject baseElem = path.elementAt(0) canvasPos = event.localPos() if event.modifiers() & Qt.ShiftModifier: basePos = QPointF(baseElem.x, baseElem.y) canvasPos = self.clampToOrigin(canvasPos, basePos) canvasPos = self.magnetPos(canvasPos) x, y = canvasPos.x(), canvasPos.y() path.setElementPositionAt(1, x, baseElem.y) path.setElementPositionAt(2, x, y) path.setElementPositionAt(3, baseElem.x, baseElem.y) line = QLineF(baseElem.x, baseElem.y, x, y) l = line.length() # angle() doesnt go by trigonometric direction. Weird. # TODO: maybe split in positive/negative 180s (ff) a = 360 - line.angle() line.setP2(QPointF(x, baseElem.y)) h = line.length() line.setP1(QPointF(x, y)) v = line.length() text = "%d\n↔ %d\n↕ %d\nα %dº" % (l, h, v, a) self._rulerObject = (path, text) self.parent().update()
class StickWidget(QGraphicsObject): font: QFont = QFont("monospace", 32) delete_clicked = pyqtSignal(Stick) link_initiated = pyqtSignal('PyQt_PyObject') # Actually StickWidget link_accepted = pyqtSignal('PyQt_PyObject') hovered = pyqtSignal(['PyQt_PyObject', 'PyQt_PyObject']) stick_changed = pyqtSignal('PyQt_PyObject') sibling_changed = pyqtSignal(bool) right_clicked = pyqtSignal('PyQt_PyObject') handle_idle_brush = QBrush(QColor(0, 125, 125, 50)) handle_hover_brush = QBrush(QColor(125, 125, 0, 50)) handle_press_brush = QBrush(QColor(200, 200, 0, 0)) handle_idle_pen = QPen(QColor(0, 0, 0, 255)) handle_press_pen = QPen(QColor(200, 200, 0, 255)) handle_size = 20 normal_color = QColor(0, 200, 120) negative_color = QColor(200, 0, 0) positive_color = QColor(0, 200, 0) mismatched = pyqtSignal('PyQt_PyObject') misplaced = pyqtSignal('PyQt_PyObject') measurement_corrected = pyqtSignal('PyQt_PyObject') clearly_visible = pyqtSignal('PyQt_PyObject') zero_clicked = pyqtSignal('PyQt_PyObject') def __init__(self, stick: Stick, camera: Camera, parent: Optional[QGraphicsItem] = None): QGraphicsObject.__init__(self, parent) self.camera = camera self.stick = stick self.line = QLineF() self.gline = QGraphicsLineItem(self.line) self.stick_label_text = QGraphicsSimpleTextItem("0", self) self.stick_label_text.setFont(StickWidget.font) self.stick_label_text.setPos(self.line.p1() - QPoint(0, 24)) self.stick_label_text.setBrush(QBrush(QColor(0, 255, 0))) self.stick_label_text.hide() self.setZValue(10) self.mode = StickMode.Display self.btn_delete = Button("delete", "x", parent=self) self.btn_delete.setFlag(QGraphicsItem.ItemIgnoresTransformations, True) self.btn_delete.set_base_color([ButtonColor.RED]) self.btn_delete.setVisible(False) btn_size = max(int(np.linalg.norm(self.stick.top - self.stick.bottom) / 5.0), 15) self.btn_delete.set_height(12) self.btn_delete.clicked.connect(self.handle_btn_delete_clicked) self.btn_delete.setPos(self.line.p1() - QPointF(0.5 * self.btn_delete.boundingRect().width(), 1.1 * self.btn_delete.boundingRect().height())) self.btn_delete.set_opacity(0.7) self.top_handle = QGraphicsEllipseItem(0, 0, self.handle_size, self.handle_size, self) self.mid_handle = QGraphicsEllipseItem(0, 0, self.handle_size, self.handle_size, self) self.bottom_handle = QGraphicsEllipseItem(0, 0, self.handle_size, self.handle_size, self) self.top_handle.setAcceptedMouseButtons(Qt.NoButton) self.mid_handle.setAcceptedMouseButtons(Qt.NoButton) self.bottom_handle.setAcceptedMouseButtons(Qt.NoButton) self.top_handle.setBrush(self.handle_idle_brush) self.top_handle.setPen(self.handle_idle_pen) self.mid_handle.setBrush(self.handle_idle_brush) self.mid_handle.setPen(self.handle_idle_pen) self.bottom_handle.setBrush(self.handle_idle_brush) self.bottom_handle.setPen(self.handle_idle_pen) self.hovered_handle: Optional[QGraphicsRectItem] = None self.handles = [self.top_handle, self.mid_handle, self.bottom_handle] self.link_button = Button("link", "Link to...", parent=self) self.link_button.set_base_color([ButtonColor.GREEN]) self.link_button.set_height(12) self.link_button.set_label("Link", direction="vertical") self.link_button.fit_to_contents() self.link_button.clicked.connect(lambda: self.link_initiated.emit(self)) self.link_button.setVisible(False) self.link_button.setFlag(QGraphicsObject.ItemIgnoresTransformations, False) self.adjust_line() self.setAcceptHoverEvents(True) self.top_handle.setZValue(4) self.bottom_handle.setZValue(4) self.mid_handle.setZValue(4) self.top_handle.hide() self.mid_handle.hide() self.bottom_handle.hide() self.handle_mouse_offset = QPointF(0, 0) self.available_for_linking = False self.link_source = False self.current_highlight_color: QColor = StickWidget.normal_color self.highlighted = False self.frame_color: Optional[None] = self.normal_color self.is_linked = False self.is_master = True self.selected = False self.measured_height: int = -1 self.current_color = self.normal_color self.show_label = False self.highlight_animation = QPropertyAnimation(self, b"highlight_color") self.highlight_animation.valueChanged.connect(self.handle_highlight_animation_value_changed) self.deleting = False self.update_tooltip() self.show_measurements: bool = False self.proposed_snow_height: int = -1 self.zero_btn = Button("zero_btn", "0", parent=self) self.zero_btn.setFlag(QGraphicsItem.ItemIgnoresTransformations, True) self.zero_btn.setVisible(False) self.zero_btn.setPos(self.boundingRect().center() + QPointF(self.zero_btn.boundingRect().width() * -0.5, self.boundingRect().height() * 0.5)) self.zero_btn.clicked.connect(self.handle_zero) @pyqtSlot() def handle_btn_delete_clicked(self): self.delete_clicked.emit(self.stick) def prepare_for_deleting(self): self.deleting = True self.highlight_animation.stop() self.btn_delete.setParentItem(None) self.scene().removeItem(self.btn_delete) self.btn_delete.deleteLater() def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Optional[PyQt5.QtWidgets.QWidget] = ...): painter.setPen(QPen(self.current_color, 1.0)) brush = QBrush(self.current_highlight_color) pen = QPen(brush, 4) painter.setPen(pen) if self.highlighted: painter.fillRect(self.boundingRect(), QBrush(self.current_highlight_color)) if self.frame_color is not None and self.mode != StickMode.Edit and self.mode != StickMode.EditDelete: painter.setPen(QPen(self.frame_color, 4)) painter.drawRect(self.boundingRect()) pen = QPen(QColor(0, 255, 0, 255)) pen.setWidth(1.0) pen.setColor(QColor(255, 0, 255, 255)) pen.setStyle(Qt.DotLine) painter.setPen(pen) off = 10 painter.drawLine(self.line.p1() - QPointF(0, off), self.line.p1() + QPointF(0, off)) painter.drawLine(self.line.p1() - QPointF(off, 0), self.line.p1() + QPointF(off, 0)) painter.drawLine(self.line.p2() - QPointF(0, off), self.line.p2() + QPointF(0, off)) painter.drawLine(self.line.p2() - QPointF(off, 0), self.line.p2() + QPointF(off, 0)) pen.setStyle(Qt.SolidLine) pen.setColor(QColor(0, 255, 0, 255)) painter.setPen(pen) if self.mode != StickMode.EditDelete: pen.setWidth(2.0) br = painter.brush() painter.setPen(pen) painter.drawEllipse(self.line.p1(), 10, 10) painter.drawEllipse(self.line.p2(), 10, 10) painter.setBrush(br) if self.mode == StickMode.Measurement and self.proposed_snow_height >= 0: point = QPointF(self.boundingRect().x(), -self.proposed_snow_height + self.line.p2().y()) pen = QPen(QColor(200, 100, 0, 255), 3.0) painter.setPen(pen) painter.drawLine(point, point + QPointF(self.boundingRect().width(), 0.0)) if self.measured_height >= 0: vec = (self.stick.top - self.stick.bottom) / np.linalg.norm(self.stick.top - self.stick.bottom) dist_along_stick = self.measured_height / np.dot(np.array([0.0, -1.0]), vec) point = self.line.p2() + dist_along_stick * QPointF(vec[0], vec[1]) point = QPointF(self.boundingRect().x(), point.y()) pen = QPen(QColor(0, 100, 200, 255), 3.0) painter.setPen(pen) painter.drawLine(point, point + QPointF(self.boundingRect().width(), 0.0)) else: painter.drawLine(self.line.p1(), self.line.p2()) if self.selected: pen.setColor(QColor(255, 125, 0, 255)) pen.setStyle(Qt.DashLine) painter.setPen(pen) painter.drawRect(self.boundingRect().marginsAdded(QMarginsF(5, 5, 5, 5))) if self.show_measurements: painter.fillRect(self.stick_label_text.boundingRect().translated(self.stick_label_text.pos()), QBrush(QColor(0, 0, 0, 120))) def boundingRect(self) -> PyQt5.QtCore.QRectF: return self.gline.boundingRect().united(self.top_handle.boundingRect()).\ united(self.mid_handle.boundingRect()).united(self.bottom_handle.boundingRect()) def set_edit_mode(self, value: bool): if value: self.set_mode(StickMode.EditDelete) else: self.set_mode(StickMode.Display) def set_mode(self, mode: StickMode): if mode == StickMode.Display: self.btn_delete.setVisible(False) self.top_handle.setVisible(False) self.mid_handle.setVisible(False) self.bottom_handle.setVisible(False) self.link_button.setVisible(False) self.available_for_linking = False self.link_source = False self.zero_btn.setVisible(False) self.setVisible(self.stick.is_visible) elif mode == StickMode.EditDelete: self.set_mode(StickMode.Display) self.top_handle.setVisible(True) self.mid_handle.setVisible(True) self.bottom_handle.setVisible(True) self.available_for_linking = False self.link_source = False self.btn_delete.setVisible(True) elif mode == StickMode.LinkSource: self.set_mode(StickMode.Display) self.link_source = True self.available_for_linking = False self.link_button.setPos(self.boundingRect().topLeft()) self.link_button.set_width(int(self.boundingRect().width())) self.link_button.set_button_height(int(self.boundingRect().height())) self.link_button.adjust_text_to_button() elif mode == StickMode.LinkTarget: self.set_mode(StickMode.Display) self.link_source = False self.available_for_linking = True elif mode == StickMode.Edit: self.set_mode(StickMode.EditDelete) self.btn_delete.setVisible(False) elif mode == StickMode.Measurement: self.zero_btn.setVisible(True) self.setVisible(True) self.mode = mode self.update_tooltip() self.update() def mousePressEvent(self, event: QGraphicsSceneMouseEvent): if self.mode != StickMode.EditDelete: return if self.hovered_handle is None: return self.hovered_handle.setBrush(self.handle_press_brush) if self.hovered_handle == self.mid_handle: self.bottom_handle.setBrush(self.handle_press_brush) self.bottom_handle.setPen(self.handle_press_pen) self.bottom_handle.setOpacity(0.5) self.top_handle.setBrush(self.handle_press_brush) self.top_handle.setPen(self.handle_press_pen) self.top_handle.setOpacity(0.5) self.hovered_handle.setPen(self.handle_press_pen) self.hovered_handle.setOpacity(0.5) self.handle_mouse_offset = self.hovered_handle.rect().center() - event.pos() self.btn_delete.setVisible(False) def mouseReleaseEvent(self, event: QGraphicsSceneMouseEvent): if self.available_for_linking: self.link_accepted.emit(self) return if self.mode == StickMode.Measurement: old_snow = self.stick.snow_height_px self.measured_height = self.proposed_snow_height self.stick.set_snow_height_px(self.proposed_snow_height) if abs(old_snow - self.proposed_snow_height) > 0: self.measurement_corrected.emit(self) self.proposed_snow_height = -1 if self.mode != StickMode.EditDelete and self.mode != StickMode.Edit: return if self.hovered_handle is not None: self.hovered_handle.setBrush(self.handle_hover_brush) self.hovered_handle.setPen(self.handle_idle_pen) self.hovered_handle.setOpacity(1.0) if self.hovered_handle == self.mid_handle: self.bottom_handle.setBrush(self.handle_idle_brush) self.bottom_handle.setPen(self.handle_idle_pen) self.bottom_handle.setOpacity(1.0) self.top_handle.setBrush(self.handle_idle_brush) self.top_handle.setPen(self.handle_idle_pen) self.top_handle.setOpacity(1.0) self.stick_changed.emit(self) self.hovered_handle = None if self.mode == StickMode.EditDelete: self.btn_delete.setVisible(True) def mouseMoveEvent(self, event: QGraphicsSceneMouseEvent): if self.hovered_handle is None: return if self.hovered_handle == self.top_handle: self.line.setP1((event.pos() + self.handle_mouse_offset).toPoint()) elif self.hovered_handle == self.bottom_handle: self.line.setP2((event.pos() + self.handle_mouse_offset).toPoint()) else: displacement = event.pos() - event.lastPos() self.setPos(self.pos() + displacement) self.adjust_handles() self.adjust_stick() self.scene().update() def set_top(self, pos: QPoint): self.line.setP1(pos) self.adjust_handles() self.adjust_stick() self.scene().update() def set_bottom(self, pos: QPoint): self.line.setP2(pos) self.adjust_handles() self.adjust_stick() self.scene().update() def hoverEnterEvent(self, event: QGraphicsSceneHoverEvent): if self.available_for_linking: self.hovered.emit(True, self) elif self.link_source: self.link_button.setVisible(True) self.scene().update() def hoverLeaveEvent(self, event: QGraphicsSceneHoverEvent): for h in self.handles: h.setBrush(self.handle_idle_brush) self.hovered_handle = None if self.available_for_linking: self.hovered.emit(False, self) self.link_button.setVisible(False) self.proposed_snow_height = -1 self.scene().update() def hoverMoveEvent(self, event: QGraphicsSceneHoverEvent): if self.mode != StickMode.EditDelete and self.mode != StickMode.Edit and self.mode != StickMode.Measurement: return if self.mode == StickMode.Measurement: self.proposed_snow_height = max(self.line.p2().y() - event.pos().y(), 0) self.update() return hovered_handle = list(filter(lambda h: h.rect().contains(event.pos()), self.handles)) if len(hovered_handle) == 0: if self.hovered_handle is not None: self.hovered_handle.setBrush(self.handle_idle_brush) self.hovered_handle = None return if self.hovered_handle is not None and self.hovered_handle != hovered_handle[0]: self.hovered_handle.setBrush(self.handle_idle_brush) self.hovered_handle = hovered_handle[0] if self.hovered_handle == self.top_handle: self.top_handle.setBrush(self.handle_hover_brush) elif self.hovered_handle == self.bottom_handle: self.bottom_handle.setBrush(self.handle_hover_brush) else: self.mid_handle.setBrush(self.handle_hover_brush) self.scene().update() def adjust_stick(self): self.stick.top[0] = self.pos().x() + self.line.p1().x() self.stick.top[1] = self.pos().y() + self.line.p1().y() self.stick.bottom[0] = self.pos().x() + self.line.p2().x() self.stick.bottom[1] = self.pos().y() + self.line.p2().y() def adjust_handles(self): if self.line.p1().y() > self.line.p2().y(): p1, p2 = self.line.p1(), self.line.p2() self.line.setP1(p2) self.line.setP2(p1) if self.hovered_handle is not None: self.hovered_handle.setBrush(self.handle_idle_brush) self.hovered_handle.setPen(self.handle_idle_pen) self.hovered_handle = self.top_handle if self.hovered_handle == self.bottom_handle else self.bottom_handle self.hovered_handle.setBrush(self.handle_press_brush) self.hovered_handle.setPen(self.handle_press_pen) rect = self.top_handle.rect() rect.moveCenter(self.line.p1()) self.top_handle.setRect(rect) rect = self.bottom_handle.rect() rect.moveCenter(self.line.p2()) self.bottom_handle.setRect(rect) rect = self.mid_handle.rect() rect.moveCenter(self.line.center()) self.mid_handle.setRect(rect) self.btn_delete.setPos(self.top_handle.rect().center() - QPointF(self.btn_delete.boundingRect().width() / 2, self.btn_delete.boundingRect().height() + self.top_handle.boundingRect().height() / 2)) def set_available_for_linking(self, available: bool): self.available_for_linking = available def set_is_link_source(self, is_source: bool): self.link_source = is_source self.link_button.setPos(self.boundingRect().topLeft()) self.link_button.set_width(int(self.boundingRect().width())) self.link_button.set_button_height(int(self.boundingRect().height())) self.link_button.adjust_text_to_button() def set_frame_color(self, color: Optional[QColor]): self.frame_color = color if color is not None else self.normal_color self.update() def set_is_linked(self, value: bool): self.is_linked = value if not self.is_linked: self.set_frame_color(None) if self.available_for_linking: self.highlight(QColor(0, 255, 0, 100)) else: self.highlight(None) self.update_tooltip() def adjust_line(self): self.setPos(QPointF(0.5 * (self.stick.top[0] + self.stick.bottom[0]), 0.5 * (self.stick.top[1] + self.stick.bottom[1]))) vec = 0.5 * (self.stick.top - self.stick.bottom) self.line.setP1(QPointF(vec[0], vec[1])) self.line.setP2(-self.line.p1()) self.gline.setLine(self.line) self.adjust_handles() self.stick_label_text.setPos(self.line.p1() - QPointF(0.5 * self.stick_label_text.boundingRect().width(), 1.3 * self.stick_label_text.boundingRect().height())) self.update() def set_selected(self, selected: bool): self.selected = selected self.update() def is_selected(self) -> bool: return self.selected def set_snow_height(self, height: int): self.measured_height = height self.update() def border_normal(self): self.current_color = self.normal_color self.update() def border_positive(self): self.current_color = self.positive_color self.update() def border_negative(self): self.current_color = self.negative_color self.update() @pyqtProperty(QColor) def highlight_color(self) -> QColor: return self.current_highlight_color @highlight_color.setter def highlight_color(self, color: QColor): self.current_highlight_color = color def highlight(self, color: Optional[QColor], animated: bool = False): self.highlighted = color is not None if not animated or color is None: self.highlight_animation.stop() self.current_highlight_color = self.normal_color if color is None else color self.update() return self.highlight_animation.setStartValue(color) self.highlight_animation.setEndValue(color) self.highlight_animation.setKeyValueAt(0.5, color.darker()) self.highlight_animation.setDuration(2000) self.highlight_animation.setLoopCount(-1) self.highlight_animation.start() def handle_link_button_hovered(self, btn: Dict[str, Any]): self.link_button.setVisible(btn['hovered']) def handle_highlight_animation_value_changed(self, new: QColor): if not self.deleting: self.update(self.boundingRect().marginsAdded(QMarginsF(10, 10, 10, 10))) def contextMenuEvent(self, event: QGraphicsSceneContextMenuEvent) -> None: self.right_clicked.emit({'stick_widget': self}) def set_stick_label(self, label: str): self.stick.label = label self.stick_label_text.setText(label) self.update_tooltip() self.update() def get_stick_label(self) -> str: return self.stick.label def get_stick_length_cm(self) -> int: return self.stick.length_cm def set_stick_length_cm(self, length: int): self.stick.length_cm = length self.update_tooltip() self.update() def update_tooltip(self): if self.mode != StickMode.Display or self.mode == StickMode.Measurement: self.setToolTip("") return snow_txt = "Snow height: " if self.stick.snow_height_px >= 0: snow_txt += str(self.stick.snow_height_cm) + " cm" self.stick_label_text.setText(str(self.stick.snow_height_cm)) else: snow_txt = "not measured" self.stick_label_text.setVisible(False) self.stick_label_text.setText(self.stick.label) self.stick_label_text.setVisible(True) stick_view_text = '' role = '' if self.stick.alternative_view is not None: alt_view = self.stick.alternative_view role = " - primary" alt = "Secondary" if not self.stick.primary: role = " - secondary" alt = "Primary" stick_view_text = f'\n{alt} view: {alt_view.label} in {alt_view.camera_folder.name}\n' mark = '*' if self.stick.determines_quality else '' self.setToolTip(f'{mark}{self.stick.label}{role}{stick_view_text}\nLength: {self.stick.length_cm} cm\n{snow_txt}') def set_stick(self, stick: Stick): self.reset_d_btns() self.stick = stick self.adjust_line() self.adjust_handles() self.set_snow_height(stick.snow_height_px) self.update_tooltip() self.set_show_measurements(self.show_measurements) if self.mode == StickMode.Measurement: self.set_frame_color(QColor(200, 100, 0, 100) if not self.stick.is_visible else None) self.setVisible(True) self.clearly_visible_btn.setVisible(not self.stick.is_visible) else: self.setVisible(self.stick.is_visible) def set_show_measurements(self, show: bool): self.show_measurements = show if self.show_measurements: self.stick_label_text.setText(str(self.stick.snow_height_cm) if self.stick.snow_height_cm >= 0 else "n/a") else: self.stick_label_text.setText(self.stick.label) self.update() def handle_zero(self): self.measured_height = 0 self.stick.set_snow_height_px(0) self.measurement_corrected.emit(self) def reset_d_btns(self): self.zero_btn.set_default_state()
def drawArrows(self, painter): """Draws arrows on the board.""" for highlight in self.highlights: if highlight.Type == self.Arrow.Type: lineWidth = 10 sqSize = self.squareSize painter.setPen( QPen(highlight.color, lineWidth, Qt.SolidLine, Qt.RoundCap)) origin = highlight.origin target = highlight.target dx = target.x() - origin.x() dy = target.y() - origin.y() # Knight jumps if dx == sqSize.width() and dy == -2 * sqSize.height(): corner = QPoint(origin.x(), origin.y() - 2 * sqSize.height()) line = QLineF(origin, corner) painter.drawLine(line) origin = corner elif dx == 2 * sqSize.width() and dy == -sqSize.height(): corner = QPoint(origin.x() + 2 * sqSize.width(), origin.y()) line = QLineF(origin, corner) painter.drawLine(line) origin = corner elif dx == 2 * sqSize.width() and dy == sqSize.height(): corner = QPoint(origin.x() + 2 * sqSize.width(), origin.y()) line = QLineF(origin, corner) painter.drawLine(line) origin = corner elif dx == sqSize.width() and dy == 2 * sqSize.height(): corner = QPoint(origin.x(), origin.y() + 2 * sqSize.height()) line = QLineF(origin, corner) painter.drawLine(line) origin = corner elif dx == -sqSize.width() and dy == 2 * sqSize.height(): corner = QPoint(origin.x(), origin.y() + 2 * sqSize.height()) line = QLineF(origin, corner) painter.drawLine(line) origin = corner elif dx == -2 * sqSize.width() and dy == sqSize.height(): corner = QPoint(origin.x() - 2 * sqSize.width(), origin.y()) line = QLineF(origin, corner) painter.drawLine(line) origin = corner elif dx == -2 * sqSize.width() and dy == -sqSize.height(): corner = QPoint(origin.x() - 2 * sqSize.width(), origin.y()) line = QLineF(origin, corner) painter.drawLine(line) origin = corner elif dx == -sqSize.width() and dy == -2 * sqSize.height(): corner = QPoint(origin.x(), origin.y() - 2 * sqSize.height()) line = QLineF(origin, corner) painter.drawLine(line) origin = corner # Other moves (and second part of knight jump) line = QLineF(origin, target) angle = line.angle() tip = line.p2() line.setLength(line.length() - 2 * lineWidth) painter.drawLine(line) tipOffset = line.p2() leftBase = QLineF() leftBase.setP1(tipOffset) leftBase.setLength(lineWidth) leftBase.setAngle(angle - 90) left = leftBase.p2() rightBase = QLineF() rightBase.setP1(tipOffset) rightBase.setLength(lineWidth) rightBase.setAngle(angle + 90) right = rightBase.p2() arrowHead = QPolygonF([left, right, tip]) path = QPainterPath() path.addPolygon(arrowHead) painter.fillPath(path, QBrush(highlight.color))
def updateLine(self): line_t = QLineF() line_t.setP1(self.src.pos() + self.src.boundingRect().center()) line_t.setP2(self.des.pos() +self.des.boundingRect().center()) self.setLine(line_t) self.update()
def drawRadar(selfself, qp): #set frame: qp.setBrush(QBrush(QColor(0, 0, 0), Qt.SolidPattern)) qp.drawRect(20, 10, 700, 700) pen_bold = QPen(QColor(102, 204, 255), 0.8, Qt.SolidLine) pen = QPen(QColor(102, 204, 255), 0.8, Qt.DashDotLine) pen_hide = QPen(QColor(0, 0, 0), 1.5, Qt.SolidLine) #add circle: qp.setPen(pen) qp.drawEllipse(QPointF(370, 360), 347, 347) #add scales: for i in range(1, 360): if i % 30 == 0: pass else: if i % 10 == 0: scaleLine = QLineF() scaleLine.setP1(QPointF(370, 360)) scaleLine.setAngle(i) scaleLine.setLength(347) qp.setPen(pen_bold) qp.drawLine(scaleLine) hideLine = QLineF() hideLine.setP1(QPointF(370, 360)) hideLine.setAngle(i) hideLine.setLength(337) qp.setPen(pen_hide) qp.drawLine(hideLine) else: scaleLine = QLineF() scaleLine.setP1(QPointF(370, 360)) scaleLine.setAngle(i) scaleLine.setLength(347) qp.setPen(pen_bold) qp.drawLine(scaleLine) hideLine = QLineF() hideLine.setP1(QPointF(370, 360)) hideLine.setAngle(i) hideLine.setLength(342) qp.setPen(pen_hide) qp.drawLine(hideLine) #add other circles: qp.setPen(pen) qp.drawEllipse(QPointF(370, 360), 277, 277) qp.setPen(pen) qp.drawEllipse(QPointF(370, 360), 207, 207) qp.setPen(pen) qp.drawEllipse(QPointF(370, 360), 137, 137) qp.setPen(pen) qp.drawEllipse(QPointF(370, 360), 67, 67) #add lines: qp.setPen(pen_bold) qp.drawLine(20, 360, 720, 360) qp.setPen(pen_bold) qp.drawLine(370, 10, 370, 710) angleLine1 = QLineF() angleLine1.setP1(QPointF(370, 360)) angleLine1.setAngle(30) angleLine1.setLength(347) qp.setPen(pen) qp.drawLine(angleLine1) angleLine2 = QLineF() angleLine2.setP1(QPointF(370, 360)) angleLine2.setAngle(60) angleLine2.setLength(347) qp.setPen(pen) qp.drawLine(angleLine2) angleLine3 = QLineF() angleLine3.setP1(QPointF(370, 360)) angleLine3.setAngle(120) angleLine3.setLength(347) qp.setPen(pen) qp.drawLine(angleLine3) angleLine4 = QLineF() angleLine4.setP1(QPointF(370, 360)) angleLine4.setAngle(150) angleLine4.setLength(347) qp.setPen(pen) qp.drawLine(angleLine4) angleLine5 = QLineF() angleLine5.setP1(QPointF(370, 360)) angleLine5.setAngle(210) angleLine5.setLength(347) qp.setPen(pen) qp.drawLine(angleLine5) angleLine6 = QLineF() angleLine6.setP1(QPointF(370, 360)) angleLine6.setAngle(240) angleLine6.setLength(347) qp.setPen(pen) qp.drawLine(angleLine6) angleLine7 = QLineF() angleLine7.setP1(QPointF(370, 360)) angleLine7.setAngle(300) angleLine7.setLength(347) qp.setPen(pen) qp.drawLine(angleLine7) angleLine8 = QLineF() angleLine8.setP1(QPointF(370, 360)) angleLine8.setAngle(330) angleLine8.setLength(347) qp.setPen(pen) qp.drawLine(angleLine8)