1
1
import sys
2
2
from PyQt6 .QtWidgets import QGraphicsScene , QGraphicsView , QGraphicsRectItem , QApplication , QGraphicsPixmapItem , \
3
- QMainWindow , QLabel
3
+ QMainWindow , QLabel , QAbstractGraphicsShapeItem
4
4
from PyQt6 .QtGui import QBrush , QPen , QPixmap , QPainter
5
5
from PyQt6 .QtCore import Qt
6
6
@@ -9,41 +9,49 @@ class Canvas(QGraphicsView):
9
9
def __init__ (self , width : int , height : int , background_colour : Qt .GlobalColor ):
10
10
super ().__init__ (None )
11
11
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 ()
15
15
16
16
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 ()
23
23
24
24
super ().paintEvent (event )
25
25
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)
30
28
29
+ pixmap = QPixmap (self ._width , self ._height )
30
+ pixmap .fill (self ._background_colour )
31
31
painter = QPainter (pixmap )
32
- painter . fillRect ( 0 , 0 , round ( width / 2 ), round ( height / 2 ), Qt . GlobalColor . green )
32
+ self . _paint ( painter )
33
33
painter .end ()
34
34
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 )
38
49
brush = QBrush (Qt .GlobalColor .red )
39
50
rect .setBrush (brush )
40
51
pen = QPen (Qt .GlobalColor .cyan )
41
52
pen .setWidth (10 )
42
53
rect .setPen (pen )
43
54
44
- scene .addItem (QGraphicsPixmapItem (pixmap ))
45
- scene .addItem (rect )
46
-
47
- self .setScene (scene )
55
+ return [rect ]
48
56
49
57
0 commit comments