Skip to content

Commit 00355ca

Browse files
committed
#187 - Refactor new canvas ready for easy extension to do the actual drawing per analysis type
1 parent cda6f60 commit 00355ca

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/prototype_ui/canvas.py

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsRectItem, QApplication, QGraphicsPixmapItem, \
3-
QMainWindow, QLabel
3+
QMainWindow, QLabel, QAbstractGraphicsShapeItem
44
from PyQt6.QtGui import QBrush, QPen, QPixmap, QPainter
55
from PyQt6.QtCore import Qt
66

@@ -9,41 +9,49 @@ class Canvas(QGraphicsView):
99
def __init__(self, width: int, height: int, background_colour: Qt.GlobalColor):
1010
super().__init__(None)
1111
self._background_colour = background_colour
12-
self._recreate_scene(width, height)
13-
self._last_drawn_width = self.geometry().width()
14-
self._last_drawn_height = self.geometry().height()
12+
self._width = self.geometry().width()
13+
self._height = self.geometry().height()
14+
self._recreate_scene()
1515

1616
def paintEvent(self, event):
17-
width = self.geometry().width()
18-
height = self.geometry().height()
19-
if self._last_drawn_width != width or self._last_drawn_height != height:
20-
self._recreate_scene(width, height)
21-
self._last_drawn_width = width
22-
self._last_drawn_height = height
17+
new_width = self.geometry().width()
18+
new_height = self.geometry().height()
19+
if self._width != new_width or self._height != new_height:
20+
self._width = new_width
21+
self._height = new_height
22+
self._recreate_scene()
2323

2424
super().paintEvent(event)
2525

26-
def _recreate_scene(self, width: int, height: int):
27-
print("REDRAW***", width, height)
28-
pixmap = QPixmap(width, height)
29-
pixmap.fill(self._background_colour)
26+
def _recreate_scene(self):
27+
# print("REDRAW***", self._width, self._height)
3028

29+
pixmap = QPixmap(self._width, self._height)
30+
pixmap.fill(self._background_colour)
3131
painter = QPainter(pixmap)
32-
painter.fillRect(0, 0, round(width / 2), round(height / 2), Qt.GlobalColor.green)
32+
self._paint(painter)
3333
painter.end()
3434

35-
scene = QGraphicsScene(0, 0, width, height)
36-
rect = QGraphicsRectItem(0, 0, width/3, height/3)
37-
rect.setPos(width/10, height/10)
35+
scene = QGraphicsScene(0, 0, self._width, self._height)
36+
scene.addItem(QGraphicsPixmapItem(pixmap))
37+
for i in self._get_scene_items():
38+
scene.addItem(i)
39+
self.setScene(scene)
40+
41+
# Hardcoded examples for now - these will be abstract shortly
42+
def _paint(self, painter: QPainter):
43+
painter.fillRect(0, 0, round(self._width / 2), round(self._height / 2), Qt.GlobalColor.darkYellow)
44+
45+
# Hardcoded examples for now - these will be abstract shortly
46+
def _get_scene_items(self) -> list[QAbstractGraphicsShapeItem]:
47+
rect = QGraphicsRectItem(0, 0, self._width / 3, self._height / 3)
48+
rect.setPos(self._width / 10, self._height / 10)
3849
brush = QBrush(Qt.GlobalColor.red)
3950
rect.setBrush(brush)
4051
pen = QPen(Qt.GlobalColor.cyan)
4152
pen.setWidth(10)
4253
rect.setPen(pen)
4354

44-
scene.addItem(QGraphicsPixmapItem(pixmap))
45-
scene.addItem(rect)
46-
47-
self.setScene(scene)
55+
return [rect]
4856

4957

0 commit comments

Comments
 (0)