From 2d33ab50a59eace94812fc0e7f023371e493583e Mon Sep 17 00:00:00 2001 From: thomahl <thomahl@stud.ntnu.no> Date: Fri, 28 Feb 2020 10:31:12 +0100 Subject: [PATCH] =?UTF-8?q?#18=20scene=20&=20view=20for=20=C3=A9n=20side?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- soitool/main_window.py | 51 +++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/soitool/main_window.py b/soitool/main_window.py index 2abe6dc..2a50c4d 100644 --- a/soitool/main_window.py +++ b/soitool/main_window.py @@ -1,11 +1,11 @@ """Hovedvinduet.""" import sys import os -from PySide2.QtCore import QRectF, QPoint, QTimer +from PySide2.QtCore import QRectF, QPoint, QTimer, Qt from PySide2.QtWidgets import QTabWidget, QWidget, QMainWindow, QApplication, \ QHBoxLayout, QVBoxLayout, QPushButton, QTreeWidget, QGraphicsScene, \ - QGraphicsView, QAction -from PySide2 import QtGui + QGraphicsView, QAction, QScrollArea, QGraphicsRectItem +from PySide2.QtGui import QBrush, QIcon, QPalette class MainWindow(QMainWindow): @@ -92,7 +92,7 @@ class MainWindow(QMainWindow): filename = 'media/HVlogo.PNG' dirname = os.path.dirname(__file__) filepath = os.path.join(dirname, filename) - self.setWindowIcon(QtGui.QIcon(filepath)) + self.setWindowIcon(QIcon(filepath)) class MainWidget(QWidget): @@ -135,10 +135,43 @@ class ViewArea(QWidget): def __init__(self): super().__init__() - self.layout = QHBoxLayout() + self.layout = QHBoxLayout(self) + self.scroll_area = QScrollArea(self) + # necessary to make the scroll area fill the space it's given + self.scroll_area.setWidgetResizable(True) + + # scene and view widgets self.scene = QGraphicsScene() + self.setup_scene() + self.view = QGraphicsView(self.scene) + self.view.scale(0.50, 0.50) + + self.scroll_area.setWidget(self.view) + self.layout.addWidget(self.scroll_area) + self.setLayout(self.layout) + + self.launch_auto_zoom() + + def setup_scene(self): + """Prepare scene for use. + + Draws borders, background, etc. + """ self.scene.setSceneRect(QRectF(0, 0, self.WIDTH, self.HEIGHT)) + + # sets the background color of the scene to be the appropriate + # system-specific background color + # source: https://stackoverflow.com/a/23880531/3545896 + # source: https://stackoverflow.com/questions/15519749/how-to-get-widget-background-qcolor + self.scene.setBackgroundBrush(app.palette().color(QPalette.Window)) + + # color the page white + page_background = QGraphicsRectItem(0, 0, self.WIDTH, self.HEIGHT) + page_background.setBrush(QBrush(Qt.white)) + self.scene.addItem(page_background) + + # draw borders self.scene.addRect(0, 0, self.WIDTH, self.HEIGHT) self.scene.addRect(self.PADDING, self.PADDING, self.CONTENT_WIDTH, self.CONTENT_HEIGHT) @@ -147,14 +180,6 @@ class ViewArea(QWidget): self.scene.addRect(self.PADDING, self.PADDING, self.CONTENT_WIDTH, self.HEADER_HEIGHT) - self.view = QGraphicsView(self.scene) - self.view.scale(0.50, 0.50) - - self.layout.addWidget(self.view) - self.setLayout(self.layout) - - self.launch_auto_zoom() - def launch_auto_zoom(self): """Zoom in a regular interval. -- GitLab