Skip to content

Commit

Permalink
#187 - Add resizable Scene to the UI prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
dmh23 committed Feb 26, 2023
1 parent 00ffd81 commit cda6f60
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/personalize/configuration/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Copyright (c) 2021 dmh23
#

from src.personalize.reward_functions.private.feb_2023_fumiaki_loop_2 import reward_function
from src.personalize.reward_functions.private.feb_2023_fumiaki_loop_3 import reward_function

NEW_REWARD_FUNCTION = reward_function

Expand Down
49 changes: 49 additions & 0 deletions src/prototype_ui/canvas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sys
from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsRectItem, QApplication, QGraphicsPixmapItem, \
QMainWindow, QLabel
from PyQt6.QtGui import QBrush, QPen, QPixmap, QPainter
from PyQt6.QtCore import Qt


class Canvas(QGraphicsView):
def __init__(self, width: int, height: int, background_colour: Qt.GlobalColor):
super().__init__(None)
self._background_colour = background_colour
self._recreate_scene(width, height)
self._last_drawn_width = self.geometry().width()
self._last_drawn_height = self.geometry().height()

def paintEvent(self, event):
width = self.geometry().width()
height = self.geometry().height()
if self._last_drawn_width != width or self._last_drawn_height != height:
self._recreate_scene(width, height)
self._last_drawn_width = width
self._last_drawn_height = height

super().paintEvent(event)

def _recreate_scene(self, width: int, height: int):
print("REDRAW***", width, height)
pixmap = QPixmap(width, height)
pixmap.fill(self._background_colour)

painter = QPainter(pixmap)
painter.fillRect(0, 0, round(width / 2), round(height / 2), Qt.GlobalColor.green)
painter.end()

scene = QGraphicsScene(0, 0, width, height)
rect = QGraphicsRectItem(0, 0, width/3, height/3)
rect.setPos(width/10, height/10)
brush = QBrush(Qt.GlobalColor.red)
rect.setBrush(brush)
pen = QPen(Qt.GlobalColor.cyan)
pen.setWidth(10)
rect.setPen(pen)

scene.addItem(QGraphicsPixmapItem(pixmap))
scene.addItem(rect)

self.setScene(scene)


5 changes: 3 additions & 2 deletions src/prototype_ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QProgressBar

from prototype_ui.actions import Actions
from prototype_ui.canvas import Canvas
from prototype_ui.menubar import MenuBarManager
from prototype_ui.toolbar import ToolBarManager

Expand All @@ -14,7 +15,7 @@ def __init__(self):
super().__init__()

self.setMinimumSize(200, 200)
self.resize(500, 400)
# self.resize(500, 400)
self.setWindowTitle("Example")

# Status Bar
Expand All @@ -36,7 +37,7 @@ def __init__(self):
self._actions.file_new.triggered.connect(self._new_file)
self._actions.file_open.triggered.connect(self._open_file)

self.canvas = QLabel("Hello")
self.canvas = Canvas(400, 300, Qt.GlobalColor.gray)
self.setCentralWidget(self.canvas)

self.show()
Expand Down

0 comments on commit cda6f60

Please sign in to comment.