diff --git a/soitool/main_window.py b/soitool/main_window.py index 762ed255714fda7dcf32a3314c9e418051c69c7f..146e2ef1ce1f75261dd8b94f54212ff7c5558dae 100644 --- a/soitool/main_window.py +++ b/soitool/main_window.py @@ -1,8 +1,10 @@ """Hovedvinduet.""" import sys import os -from PySide2.QtWidgets import QTabWidget, QWidget, QMainWindow, \ - QApplication, QHBoxLayout, QVBoxLayout, QPushButton, QTreeWidget, QLabel +from PySide2.QtCore import QRectF, QPoint, QTimer +from PySide2.QtWidgets import QTabWidget, QWidget, QMainWindow, QApplication, \ + QHBoxLayout, QVBoxLayout, QPushButton, QTreeWidget, QGraphicsScene, \ + QGraphicsView from PySide2 import QtGui @@ -72,12 +74,66 @@ class MainWidget(QWidget): class ViewArea(QWidget): """Widget som kan byttes ut med view, edit etc.""" + A4_RATIO = 1.414 + HEIGHT = 1700 + WIDTH = HEIGHT * A4_RATIO + PADDING = 100 + CONTENT_WIDTH = WIDTH - PADDING * 2 + CONTENT_HEIGHT = HEIGHT - PADDING * 2 + HEADER_HEIGHT = 200 + def __init__(self): super().__init__() - test = QLabel("Test") - layout = QHBoxLayout() - layout.addWidget(test) - self.setLayout(layout) + + self.layout = QHBoxLayout() + + self.scene = QGraphicsScene() + self.scene.setSceneRect(QRectF(0, 0, self.WIDTH, self.HEIGHT)) + self.scene.addRect(0, 0, self.WIDTH, self.HEIGHT) + self.scene.addRect(self.PADDING, self.PADDING, self.CONTENT_WIDTH, self.CONTENT_HEIGHT) + 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. + + Used to demonstrate zooming only, should be removed once the project matures. + """ + def do_on_timeout(): + self.zoom(1 / 1.00005) + + timer = QTimer(self) + timer.timeout.connect(do_on_timeout) + timer.start(10) + + def zoom(self, zoom_factor): + """Zoom GraphicsView by zoom_factor. + + source: https://stackoverflow.com/a/29026916/3545896 + + Parameters + ---------- + zoom_factor : float + > 1 to zoom in and < 1 to zoom out + """ + # Set Anchors + self.view.setTransformationAnchor(QGraphicsView.NoAnchor) + self.view.setResizeAnchor(QGraphicsView.NoAnchor) + + # Zoom + self.view.scale(zoom_factor, zoom_factor) + + pos_old = self.view.mapToScene(QPoint(self.WIDTH / 2, self.HEIGHT / 2)) + pos_new = self.view.mapToScene(self.WIDTH / 2, self.HEIGHT / 2) + delta = pos_new - pos_old + self.view.translate(delta.x(), delta.y()) app = QApplication([])