first circle

This commit is contained in:
2024-09-04 21:26:42 +02:00
parent 787d4e776a
commit 054384ca85
4 changed files with 109 additions and 45 deletions

View File

@@ -1,15 +1,53 @@
from PySide6.QtCore import Qt
from PySide6.QtGui import QPaintEvent, QPainter, QPalette
from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget
from PySide6.QtCore import QRectF, Qt
from PySide6.QtGui import QBrush, QPainter, QPen
from PySide6.QtWidgets import QGraphicsEllipseItem, QGraphicsItem, QStyleOptionGraphicsItem, QWidget
class SphereWidget(QWidget):
class SphereGraphicsItem(QGraphicsEllipseItem):
def __init__(self) -> None:
super().__init__()
SPHERE_WIDTH = 30
def paintEvent(self, event: QPaintEvent) -> None:
with QPainter(self) as painter:
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(Qt.red)
painter.drawEllipse(0, 0, self.width(), self.height())
return super().paintEvent(event)
def __init__(self, x_pos: int, y_pos: int) -> None:
super().__init__(self.SPHERE_WIDTH/2, self.SPHERE_WIDTH/2, self.SPHERE_WIDTH, self.SPHERE_WIDTH)
print(f"sphere at {x_pos}:{y_pos}")
self.setPos(x_pos, y_pos)
brush = QBrush(Qt.GlobalColor.blue)
self.setBrush(brush)
pen = QPen(Qt.GlobalColor.green)
pen.setWidth(3)
self.setPen(pen)
self.show()
class ArcGraphicsItem(QGraphicsItem):
def __init__(self, x_pos: int, y_pos: int, width: int, angle_start: int, angle_end: int, parent: QGraphicsItem | None = ...):
super().__init__(parent)
print(f"arc from {x_pos}:{y_pos} of {width} {angle_start}° {angle_end}°")
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.angle_start = angle_start
self.angle_end = angle_end
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget | None = ...) -> None:
shift = int(self.width/2)
painter.drawArc(self.x_pos-shift, self.y_pos-shift, self.width*2,
self.width*2, self.angle_start*16, self.angle_end*16)
def boundingRect(self) -> QRectF:
return QRectF(0, 0, self.x_pos, self.y_pos)
class LineGraphicsItem(QGraphicsItem):
def __init__(self, begin_x: int, begin_y: int, end_x: int, end_y: int, parent: QGraphicsItem | None = ...) -> None:
super().__init__(parent)
print(f"line from {begin_x}:{begin_y} to {end_x}:{end_y}")
self.begin_x = begin_x
self.begin_y = begin_y
self.end_x = end_x
self.end_y = end_y
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget | None = ...) -> None:
painter.drawLine(self.begin_x, self.begin_y, self.end_x, self.end_y)
def boundingRect(self) -> QRectF:
return QRectF(self.begin_x, self.begin_y, self.end_x, self.end_y)