connect patterns with single link
This commit is contained in:
@@ -16,6 +16,7 @@ from grid.ui.items import (
|
||||
HPSphereGraphicsItem,
|
||||
ArcGraphicsItem,
|
||||
LineGraphicsItem,
|
||||
SphereGraphicsItem,
|
||||
random_sphere_factory,
|
||||
)
|
||||
|
||||
@@ -31,6 +32,26 @@ class Pattern(QGraphicsItemGroup):
|
||||
# center coordinates of the pattern
|
||||
self.center_x = top_left_corner_x + int(self.PATTERN_WIDTH / 2)
|
||||
self.center_y = top_left_corner_y + int(self.PATTERN_WIDTH / 2)
|
||||
self.external_connectors: List[SphereGraphicsItem] = []
|
||||
|
||||
def get_external_connectors(self) -> List[SphereGraphicsItem]:
|
||||
"""Candidate spheres to connect with other patterns.
|
||||
|
||||
Returns:
|
||||
List[SphereGraphicsItem]: List of spheres
|
||||
"""
|
||||
return self.external_connectors
|
||||
|
||||
def connect(self, other: "Pattern") -> None:
|
||||
s1 = self.external_connectors[0]
|
||||
s2 = other.external_connectors[1]
|
||||
line_start_x = s1.x_pos + s1.SPHERE_WIDTH / 2
|
||||
line_start_y = s1.y_pos + s1.SPHERE_WIDTH / 2
|
||||
line_end_x = s2.x_pos + s2.SPHERE_WIDTH / 2
|
||||
line_end_y = s2.y_pos + s2.SPHERE_WIDTH / 2
|
||||
self.addToGroup(
|
||||
LineGraphicsItem(line_start_x, line_start_y, line_end_x, line_end_y, self)
|
||||
)
|
||||
|
||||
def paint(
|
||||
self,
|
||||
@@ -39,8 +60,8 @@ class Pattern(QGraphicsItemGroup):
|
||||
widget: QWidget | None = ...,
|
||||
):
|
||||
super().paint(painter, option, widget)
|
||||
brush = QBrush(Qt.GlobalColor.gray)
|
||||
painter.setBrush(brush)
|
||||
# brush = QBrush(Qt.GlobalColor.gray)
|
||||
# painter.setBrush(brush)
|
||||
painter.drawRect(
|
||||
self.top_left_corner_x,
|
||||
self.top_left_corner_y,
|
||||
@@ -143,7 +164,9 @@ class LargeUltimaPattern(Pattern):
|
||||
)
|
||||
)
|
||||
for x, y in points:
|
||||
self.addToGroup(random_sphere_factory(x, y))
|
||||
external_sphere = random_sphere_factory(x, y)
|
||||
self.addToGroup(external_sphere)
|
||||
self.external_connectors.append(external_sphere)
|
||||
# self.addToGroup(
|
||||
# ArcGraphicsItem(
|
||||
# self.center_x, self.center_y, self.SPACE_BETWEEN_SPHERE * 2, 0, 90, self
|
||||
@@ -178,15 +201,21 @@ class SmallZPattern(Pattern):
|
||||
self.center_x, self.center_y, self.SPACE_BETWEEN_SPHERE, sphere_on_circle
|
||||
)
|
||||
self.addToGroup(
|
||||
LineGraphicsItem(self.center_x, self.center_y, points[1][0], points[1][1], self)
|
||||
LineGraphicsItem(
|
||||
self.center_x, self.center_y, points[1][0], points[1][1], self
|
||||
)
|
||||
)
|
||||
self.addToGroup(
|
||||
LineGraphicsItem(self.center_x, self.center_y, points[4][0], points[4][1], self)
|
||||
LineGraphicsItem(
|
||||
self.center_x, self.center_y, points[4][0], points[4][1], self
|
||||
)
|
||||
)
|
||||
# Remove left and right sphere
|
||||
del points[0]
|
||||
del points[2]
|
||||
for x, y in points:
|
||||
self.addToGroup(random_sphere_factory(x, y))
|
||||
external_sphere = random_sphere_factory(x, y)
|
||||
self.addToGroup(external_sphere)
|
||||
self.external_connectors.append(external_sphere)
|
||||
|
||||
self.addToGroup(HPSphereGraphicsItem(self.center_x, self.center_y))
|
||||
self.addToGroup(HPSphereGraphicsItem(self.center_x, self.center_y))
|
||||
|
||||
@@ -18,7 +18,8 @@ class Window(QWidget):
|
||||
self.scene = QGraphicsScene(0, 0, 400, 400)
|
||||
|
||||
pp1 = LargeUltimaPattern(0, 0)
|
||||
pp2 = SmallZPattern(401, 0)
|
||||
pp2 = SmallZPattern(351, 0)
|
||||
pp1.connect(pp2)
|
||||
self.scene.addItem(pp1)
|
||||
self.scene.addItem(pp2)
|
||||
|
||||
|
||||
BIN
screenshot.png
BIN
screenshot.png
Binary file not shown.
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 29 KiB |
Reference in New Issue
Block a user