feat: finally a working super simple pattern
Some checks failed
CI/CD / build (push) Successful in 10m35s
CI/CD / wol (push) Has been cancelled

This commit is contained in:
2025-08-12 15:47:35 +02:00
parent cf18b24207
commit 9c6239028d
5 changed files with 92 additions and 44 deletions

View File

@@ -3,5 +3,5 @@ from grid.ui.pattern import Pattern
class TestPattern:
def test_position(self):
p = Pattern(500, 500)
p = Pattern(0, 0)
assert len(p.childItems()) > 0

View File

@@ -9,17 +9,29 @@ from PySide6.QtWidgets import (
class SphereGraphicsItem(QGraphicsEllipseItem):
"""Single sphere of a static size."""
SPHERE_WIDTH = 30
def __init__(self, x_pos: int, y_pos: int) -> None:
"""Create a sphere
Args:
x_pos (int): x coordinate of the center
y_pos (int): y coordinate of the center
"""
super().__init__(
self.SPHERE_WIDTH / 2,
self.SPHERE_WIDTH / 2,
x_pos - self.SPHERE_WIDTH / 2,
y_pos - self.SPHERE_WIDTH / 2,
self.SPHERE_WIDTH,
self.SPHERE_WIDTH,
)
print(f"sphere at {x_pos}:{y_pos}")
self.setPos(x_pos, y_pos)
self.x_pos = x_pos - self.SPHERE_WIDTH / 2
self.y_pos = y_pos - self.SPHERE_WIDTH / 2
print(f"sphere at {self.x_pos}:{self.y_pos}")
# self.setPos(self.x_pos - self.SPHERE_WIDTH/2, self.y_pos - self.SPHERE_WIDTH/2)
# super().moveBy(x_pos - center.x(), y_pos - center.y())
brush = QBrush(Qt.GlobalColor.darkBlue)
self.setBrush(brush)
pen = QPen(Qt.GlobalColor.blue)
@@ -27,8 +39,13 @@ class SphereGraphicsItem(QGraphicsEllipseItem):
self.setPen(pen)
self.show()
def boundingRect(self) -> QRectF:
return QRectF(self.x_pos, self.y_pos, self.SPHERE_WIDTH, self.SPHERE_WIDTH)
class ArcGraphicsItem(QGraphicsItem):
"""Circle arc."""
def __init__(
self,
x_pos: int,
@@ -38,13 +55,25 @@ class ArcGraphicsItem(QGraphicsItem):
angle_end: int,
parent: QGraphicsItem | None = ...,
):
"""Construct a circle arc.
Args:
x_pos (int): x center of the circle
y_pos (int): y center of the circle
width (int): width of the circle
angle_start (int): Angle start in human degree
angle_end (int): Angle end in human degree
parent (QGraphicsItem | None, optional): _description_. Defaults to ....
"""
super().__init__(parent)
shift = int(width / 2)
self.x_pos = x_pos - shift
self.y_pos = y_pos - shift
self.width = width * 2
self.angle_start = angle_start * 16
self.angle_end = angle_end * 16
self.width = width
self.angle_start = angle_start
self.angle_end = angle_end
self.draw_angle_start = int(angle_start / 16)
self.draw_angle_end = int(angle_end * 16)
print(
f"arc from {self.x_pos}:{self.y_pos} of {self.width} {self.angle_start}° {self.angle_end}°"
)
@@ -60,12 +89,12 @@ class ArcGraphicsItem(QGraphicsItem):
self.y_pos,
self.width,
self.width,
self.angle_start,
self.angle_end,
self.draw_angle_start,
self.draw_angle_end,
)
def boundingRect(self) -> QRectF:
return QRectF(0, 0, self.x_pos, self.y_pos)
return QRectF(self.x_pos, self.y_pos, self.width, self.width)
class LineGraphicsItem(QGraphicsItem):

View File

@@ -2,7 +2,15 @@ import math
from typing import List, Tuple
from PySide6.QtCore import Qt
from PySide6.QtGui import QBrush, QPainter
from PySide6.QtWidgets import QGraphicsItemGroup
from PySide6.QtWidgets import (
QStyleOptionGraphicsItem,
QWidget,
)
from grid.ui.items import SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem
@@ -10,7 +18,7 @@ from grid.ui.items import SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem
class Pattern(QGraphicsItemGroup):
"""Common pattern on the sphere grid"""
PATTERN_WIDTH = 250
PATTERN_WIDTH = 600
SPACE_BETWEEN_SPHERE = 60
def __init__(self, top_left_corner_x: int, top_left_corner_y: int) -> None:
@@ -30,22 +38,22 @@ class Pattern(QGraphicsItemGroup):
)
for x, y in points:
self.addToGroup(SphereGraphicsItem(x, y))
for i in range(sphere_on_circle):
for i in range(sphere_on_circle-1):
self.addToGroup(
ArcGraphicsItem(
center_x,
center_y,
self.SPACE_BETWEEN_SPHERE,
(i * 360 / sphere_on_circle) % 360,
((i + 1) * 360 / sphere_on_circle) % 360,
self,
x_pos=center_x,
y_pos=center_y,
width=self.SPACE_BETWEEN_SPHERE * 2,
angle_start=(i * 360 / sphere_on_circle) % 360,
angle_end=((i + 1) * 360 / sphere_on_circle) % 360,
parent=self,
)
)
line_start_x = int(center_x + self.SPACE_BETWEEN_SPHERE / 2)
line_start_y = int(center_y + self.SPACE_BETWEEN_SPHERE / 2)
line_end_x = int(center_x + self.SPACE_BETWEEN_SPHERE / 2)
line_end_y = int(center_y + self.SPACE_BETWEEN_SPHERE * 1.5)
line_start_x = center_x
line_start_y = center_y
line_end_x = center_x
line_end_y = int(center_y + self.SPACE_BETWEEN_SPHERE)
self.addToGroup(
LineGraphicsItem(line_start_x, line_start_y, line_end_x, line_end_y, self)
)
@@ -57,30 +65,41 @@ class Pattern(QGraphicsItemGroup):
)
for x, y in points:
self.addToGroup(SphereGraphicsItem(x, y))
for i in range(sphere_on_circle):
for i in range(sphere_on_circle-2):
self.addToGroup(
ArcGraphicsItem(
center_x,
center_y,
self.SPACE_BETWEEN_SPHERE * 2,
int(i * 360 / sphere_on_circle / 4) % 360,
int((i + 1) * 360 / sphere_on_circle) % 360,
self,
x_pos=center_x,
y_pos=center_y,
width=self.SPACE_BETWEEN_SPHERE * 4,
angle_start=int(i * 360 / sphere_on_circle / 4) % 360,
angle_end=int((i + 1) * 360 / sphere_on_circle) % 360,
parent=self,
)
)
self.addToGroup(
ArcGraphicsItem(
center_x, center_y, self.SPACE_BETWEEN_SPHERE * 2, 0, 90, self
)
)
# self.addToGroup(
# ArcGraphicsItem(
# center_x, center_y, self.SPACE_BETWEEN_SPHERE * 2, 0, 90, self
# )
# )
# third cercle
sphere_on_circle = 8
points = self.generate_circle_points(
center_x, center_y, self.SPACE_BETWEEN_SPHERE * 3, sphere_on_circle
)
for x, y in points:
self.addToGroup(SphereGraphicsItem(x, y))
# sphere_on_circle = 8
# points = self.generate_circle_points(
# center_x, center_y, self.SPACE_BETWEEN_SPHERE * 3, sphere_on_circle
# )
# for x, y in points:
# self.addToGroup(SphereGraphicsItem(x, y))
def paint(
self,
painter: QPainter,
option: QStyleOptionGraphicsItem,
widget: QWidget | None = ...,
):
super().paint(painter, option, widget)
brush = QBrush(Qt.GlobalColor.gray)
painter.setBrush(brush)
painter.drawRect(0, 0, self.PATTERN_WIDTH, self.PATTERN_WIDTH)
def generate_circle_points(
self, center_x: int, center_y: int, radius: int, n_points: int

View File

@@ -20,7 +20,7 @@ class Window(QWidget):
self.scene = QGraphicsScene(0, 0, 600, 600)
pp = Pattern(300, 300)
pp = Pattern(0, 0)
self.scene.addItem(pp)
view = QGraphicsView(self.scene)

View File

@@ -1,2 +1,2 @@
pyside6==6.6.1
pytest==8.3.3
pyside6>=6.6.1
pytest==8.3.3