Skip to content
Snippets Groups Projects
Commit e29c068c authored by Thomas Holene Løkkeborg's avatar Thomas Holene Løkkeborg
Browse files

#18 ViewArea nå GraphicsScene + zoom

WIP
parent a66272b6
No related branches found
No related tags found
1 merge request!3#18 MVP brukergrensesnitt - inline edit view
Pipeline #70959 passed
"""Hovedvinduet.""" """Hovedvinduet."""
import sys import sys
import os import os
from PySide2.QtWidgets import QTabWidget, QWidget, QMainWindow, \ from PySide2.QtCore import QRectF, QPoint, QTimer
QApplication, QHBoxLayout, QVBoxLayout, QPushButton, QTreeWidget, QLabel from PySide2.QtWidgets import QTabWidget, QWidget, QMainWindow, QApplication, \
QHBoxLayout, QVBoxLayout, QPushButton, QTreeWidget, QGraphicsScene, \
QGraphicsView
from PySide2 import QtGui from PySide2 import QtGui
...@@ -72,12 +74,66 @@ class MainWidget(QWidget): ...@@ -72,12 +74,66 @@ class MainWidget(QWidget):
class ViewArea(QWidget): class ViewArea(QWidget):
"""Widget som kan byttes ut med view, edit etc.""" """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): def __init__(self):
super().__init__() super().__init__()
test = QLabel("Test")
layout = QHBoxLayout() self.layout = QHBoxLayout()
layout.addWidget(test)
self.setLayout(layout) 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([]) app = QApplication([])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment