feat: finally a working super simple pattern
This commit is contained in:
@@ -3,5 +3,5 @@ from grid.ui.pattern import Pattern
|
|||||||
|
|
||||||
class TestPattern:
|
class TestPattern:
|
||||||
def test_position(self):
|
def test_position(self):
|
||||||
p = Pattern(500, 500)
|
p = Pattern(0, 0)
|
||||||
assert len(p.childItems()) > 0
|
assert len(p.childItems()) > 0
|
||||||
|
|||||||
@@ -9,17 +9,29 @@ from PySide6.QtWidgets import (
|
|||||||
|
|
||||||
|
|
||||||
class SphereGraphicsItem(QGraphicsEllipseItem):
|
class SphereGraphicsItem(QGraphicsEllipseItem):
|
||||||
|
"""Single sphere of a static size."""
|
||||||
|
|
||||||
SPHERE_WIDTH = 30
|
SPHERE_WIDTH = 30
|
||||||
|
|
||||||
def __init__(self, x_pos: int, y_pos: int) -> None:
|
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__(
|
super().__init__(
|
||||||
self.SPHERE_WIDTH / 2,
|
x_pos - self.SPHERE_WIDTH / 2,
|
||||||
self.SPHERE_WIDTH / 2,
|
y_pos - self.SPHERE_WIDTH / 2,
|
||||||
self.SPHERE_WIDTH,
|
self.SPHERE_WIDTH,
|
||||||
self.SPHERE_WIDTH,
|
self.SPHERE_WIDTH,
|
||||||
)
|
)
|
||||||
print(f"sphere at {x_pos}:{y_pos}")
|
self.x_pos = x_pos - self.SPHERE_WIDTH / 2
|
||||||
self.setPos(x_pos, y_pos)
|
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)
|
brush = QBrush(Qt.GlobalColor.darkBlue)
|
||||||
self.setBrush(brush)
|
self.setBrush(brush)
|
||||||
pen = QPen(Qt.GlobalColor.blue)
|
pen = QPen(Qt.GlobalColor.blue)
|
||||||
@@ -27,8 +39,13 @@ class SphereGraphicsItem(QGraphicsEllipseItem):
|
|||||||
self.setPen(pen)
|
self.setPen(pen)
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
|
def boundingRect(self) -> QRectF:
|
||||||
|
return QRectF(self.x_pos, self.y_pos, self.SPHERE_WIDTH, self.SPHERE_WIDTH)
|
||||||
|
|
||||||
|
|
||||||
class ArcGraphicsItem(QGraphicsItem):
|
class ArcGraphicsItem(QGraphicsItem):
|
||||||
|
"""Circle arc."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
x_pos: int,
|
x_pos: int,
|
||||||
@@ -38,13 +55,25 @@ class ArcGraphicsItem(QGraphicsItem):
|
|||||||
angle_end: int,
|
angle_end: int,
|
||||||
parent: QGraphicsItem | None = ...,
|
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)
|
super().__init__(parent)
|
||||||
shift = int(width / 2)
|
shift = int(width / 2)
|
||||||
self.x_pos = x_pos - shift
|
self.x_pos = x_pos - shift
|
||||||
self.y_pos = y_pos - shift
|
self.y_pos = y_pos - shift
|
||||||
self.width = width * 2
|
self.width = width
|
||||||
self.angle_start = angle_start * 16
|
self.angle_start = angle_start
|
||||||
self.angle_end = angle_end * 16
|
self.angle_end = angle_end
|
||||||
|
self.draw_angle_start = int(angle_start / 16)
|
||||||
|
self.draw_angle_end = int(angle_end * 16)
|
||||||
print(
|
print(
|
||||||
f"arc from {self.x_pos}:{self.y_pos} of {self.width} {self.angle_start}° {self.angle_end}°"
|
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.y_pos,
|
||||||
self.width,
|
self.width,
|
||||||
self.width,
|
self.width,
|
||||||
self.angle_start,
|
self.draw_angle_start,
|
||||||
self.angle_end,
|
self.draw_angle_end,
|
||||||
)
|
)
|
||||||
|
|
||||||
def boundingRect(self) -> QRectF:
|
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):
|
class LineGraphicsItem(QGraphicsItem):
|
||||||
|
|||||||
@@ -2,7 +2,15 @@ import math
|
|||||||
|
|
||||||
from typing import List, Tuple
|
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 QGraphicsItemGroup
|
||||||
|
from PySide6.QtWidgets import (
|
||||||
|
QStyleOptionGraphicsItem,
|
||||||
|
QWidget,
|
||||||
|
)
|
||||||
|
|
||||||
from grid.ui.items import SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem
|
from grid.ui.items import SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem
|
||||||
|
|
||||||
@@ -10,7 +18,7 @@ from grid.ui.items import SphereGraphicsItem, ArcGraphicsItem, LineGraphicsItem
|
|||||||
class Pattern(QGraphicsItemGroup):
|
class Pattern(QGraphicsItemGroup):
|
||||||
"""Common pattern on the sphere grid"""
|
"""Common pattern on the sphere grid"""
|
||||||
|
|
||||||
PATTERN_WIDTH = 250
|
PATTERN_WIDTH = 600
|
||||||
SPACE_BETWEEN_SPHERE = 60
|
SPACE_BETWEEN_SPHERE = 60
|
||||||
|
|
||||||
def __init__(self, top_left_corner_x: int, top_left_corner_y: int) -> None:
|
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:
|
for x, y in points:
|
||||||
self.addToGroup(SphereGraphicsItem(x, y))
|
self.addToGroup(SphereGraphicsItem(x, y))
|
||||||
for i in range(sphere_on_circle):
|
for i in range(sphere_on_circle-1):
|
||||||
self.addToGroup(
|
self.addToGroup(
|
||||||
ArcGraphicsItem(
|
ArcGraphicsItem(
|
||||||
center_x,
|
x_pos=center_x,
|
||||||
center_y,
|
y_pos=center_y,
|
||||||
self.SPACE_BETWEEN_SPHERE,
|
width=self.SPACE_BETWEEN_SPHERE * 2,
|
||||||
(i * 360 / sphere_on_circle) % 360,
|
angle_start=(i * 360 / sphere_on_circle) % 360,
|
||||||
((i + 1) * 360 / sphere_on_circle) % 360,
|
angle_end=((i + 1) * 360 / sphere_on_circle) % 360,
|
||||||
self,
|
parent=self,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
line_start_x = int(center_x + self.SPACE_BETWEEN_SPHERE / 2)
|
line_start_x = center_x
|
||||||
line_start_y = int(center_y + self.SPACE_BETWEEN_SPHERE / 2)
|
line_start_y = center_y
|
||||||
line_end_x = int(center_x + self.SPACE_BETWEEN_SPHERE / 2)
|
line_end_x = center_x
|
||||||
line_end_y = int(center_y + self.SPACE_BETWEEN_SPHERE * 1.5)
|
line_end_y = int(center_y + self.SPACE_BETWEEN_SPHERE)
|
||||||
self.addToGroup(
|
self.addToGroup(
|
||||||
LineGraphicsItem(line_start_x, line_start_y, line_end_x, line_end_y, self)
|
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:
|
for x, y in points:
|
||||||
self.addToGroup(SphereGraphicsItem(x, y))
|
self.addToGroup(SphereGraphicsItem(x, y))
|
||||||
for i in range(sphere_on_circle):
|
for i in range(sphere_on_circle-2):
|
||||||
self.addToGroup(
|
self.addToGroup(
|
||||||
ArcGraphicsItem(
|
ArcGraphicsItem(
|
||||||
center_x,
|
x_pos=center_x,
|
||||||
center_y,
|
y_pos=center_y,
|
||||||
self.SPACE_BETWEEN_SPHERE * 2,
|
width=self.SPACE_BETWEEN_SPHERE * 4,
|
||||||
int(i * 360 / sphere_on_circle / 4) % 360,
|
angle_start=int(i * 360 / sphere_on_circle / 4) % 360,
|
||||||
int((i + 1) * 360 / sphere_on_circle) % 360,
|
angle_end=int((i + 1) * 360 / sphere_on_circle) % 360,
|
||||||
self,
|
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
|
# third cercle
|
||||||
sphere_on_circle = 8
|
# sphere_on_circle = 8
|
||||||
points = self.generate_circle_points(
|
# points = self.generate_circle_points(
|
||||||
center_x, center_y, self.SPACE_BETWEEN_SPHERE * 3, sphere_on_circle
|
# center_x, center_y, self.SPACE_BETWEEN_SPHERE * 3, sphere_on_circle
|
||||||
)
|
# )
|
||||||
for x, y in points:
|
# for x, y in points:
|
||||||
self.addToGroup(SphereGraphicsItem(x, y))
|
# 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(
|
def generate_circle_points(
|
||||||
self, center_x: int, center_y: int, radius: int, n_points: int
|
self, center_x: int, center_y: int, radius: int, n_points: int
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class Window(QWidget):
|
|||||||
|
|
||||||
self.scene = QGraphicsScene(0, 0, 600, 600)
|
self.scene = QGraphicsScene(0, 0, 600, 600)
|
||||||
|
|
||||||
pp = Pattern(300, 300)
|
pp = Pattern(0, 0)
|
||||||
self.scene.addItem(pp)
|
self.scene.addItem(pp)
|
||||||
|
|
||||||
view = QGraphicsView(self.scene)
|
view = QGraphicsView(self.scene)
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
pyside6==6.6.1
|
pyside6>=6.6.1
|
||||||
pytest==8.3.3
|
pytest==8.3.3
|
||||||
Reference in New Issue
Block a user