From 3effce358c8f1155a3b51cc60b7b699cf7bcbd09 Mon Sep 17 00:00:00 2001 From: rsxri Date: Fri, 11 Oct 2024 01:58:08 +0100 Subject: [PATCH] add base class for grid, update main and participant --- main.py | 22 ++++++++++++++-- modules/grid.py | 58 ++++++++++++++++++++++++++++++++++++++++++ modules/participant.py | 15 ++++++++--- 3 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 modules/grid.py diff --git a/main.py b/main.py index c1a745b..5866bc6 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,30 @@ import sys -from PyQt5.QtWidgets import QApplication +from PyQt5.QtWidgets import QApplication, QStackedWidget + from modules.participant import Participant +from modules.grid import GridDots + + +class MainWindow(QStackedWidget): + def __init__(self): + super().__init__() + self.participant_widget = Participant() + self.grid_widget = GridDots() + self.addWidget(self.participant_widget) + self.addWidget(self.grid_widget) + + self.participant_widget.reg_success.connect(self.show_line_widget) + + def show_line_widget(self): + """Method to show widget with index 1 in the stack""" + self.setCurrentIndex(1) # change to lines not grid later + if __name__ == '__main__': app = QApplication(sys.argv) - window = Participant() + window = MainWindow() window.setWindowTitle("3Dots") window.show() sys.exit(app.exec_()) diff --git a/modules/grid.py b/modules/grid.py new file mode 100644 index 0000000..0fbf5c2 --- /dev/null +++ b/modules/grid.py @@ -0,0 +1,58 @@ +from PyQt5.QtGui import QPen, QBrush +from PyQt5.QtCore import Qt, QPointF +from PyQt5.QtWidgets import ( + QWidget, + QGraphicsView, + QGraphicsScene, + QGraphicsEllipseItem, + QGraphicsLineItem, + QVBoxLayout +) + + +class GridDots(QWidget): + def __init__(self): + super().__init__() + self.view = QGraphicsView() + self.scene = QGraphicsScene() + self.scene.setSceneRect(0, 0, 800, 800) + self.view.setScene(self.scene) + + self.dots = [] + self.lines = set() + self.setup_ui() + self.create_grid() + + def setup_ui(self): + layout = QVBoxLayout(self) + layout.addWidget(self.view) + + def create_grid(self): + """Draw 3x3 grid of dots""" + dot_radius = 30 + spacing = 240 + offset = 150 + + for row in range(3): + for col in range(3): + x, y, = offset + col * spacing, offset + row * spacing + dot = QGraphicsEllipseItem( + x - dot_radius, y - dot_radius, + 2 * dot_radius, 2 * dot_radius) + dot.setBrush(QBrush(Qt.black)) + self.scene.addItem(dot) + self.dots.append((x, y)) + + def draw_line(self, start, end): + line_id = tuple(sorted((start, end))) + if line_id not in self.lines: + start_pos = QPointF(*self.dots[start]) + end_pos = QPointF(*self.dots[end]) + + line = QGraphicsLineItem( + start_pos.x(), start_pos.y(), + end_pos.x(), end_pos.y()) + + line.setPen(QPen(Qt.black, 2)) + self.scene.addItem(line) + self.lines.add(line_id) diff --git a/modules/participant.py b/modules/participant.py index 6afc300..0195b7d 100644 --- a/modules/participant.py +++ b/modules/participant.py @@ -1,3 +1,4 @@ +from PyQt5.QtCore import Qt, pyqtSignal from PyQt5.QtWidgets import ( QWidget, QVBoxLayout, @@ -13,10 +14,10 @@ from PyQt5.QtWidgets import ( QSpinBox ) -from PyQt5.QtCore import Qt - class Participant(QWidget): + reg_success = pyqtSignal() + def __init__(self): super().__init__() @@ -79,5 +80,11 @@ class Participant(QWidget): QMessageBox.warning( self, 'Input Error', 'Please select your sex.') else: - QMessageBox.information( - self, 'Success', f'Participant Registered: {identifier}, Age: {age}, Sex: {sex}') + result = QMessageBox.information( + self, + 'Success', + f'Participant Registered: {identifier}, Age: {age}, Sex: {sex}' + ) + + if result == QMessageBox.Ok: + self.reg_success.emit()