summaryrefslogtreecommitdiff
path: root/src/main/python/utils/canvas.py
blob: 6f0fb23be491b38dc1fd79da6e9f2ee31f6de338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QHBoxLayout, QWidget

import graphics
from .sizes import paperSizes, ppiList, sheetDimensionList


class canvas(QWidget):
    def __init__(self, parent=None, size= 0, ppi= 1):
        super(canvas, self).__init__(parent)
        
        self._ppi = ppiList[ppi]
        self._canvasSize = sheetDimensionList[size]        
        self.painter = QGraphicsScene(0, 0, *paperSizes[self.canvasSize][self.ppi])
        self.painter.setSceneRect(0, 0, *paperSizes[self.canvasSize][self.ppi])
        self.setMaximumSize(*paperSizes[self.canvasSize][self.ppi])
        self.resize(*paperSizes[self.canvasSize][self.ppi])
        self.painter.setBackgroundBrush(QBrush(Qt.white))
        self.view = QGraphicsView(self.painter)
        self.view.setMaximumSize(794, 1123)
        self.layout = QHBoxLayout(self)
        self.layout.addWidget(self.view)
        self.setLayout(self.layout)
        
    def setCanvasSize(self, size):
        self.canvasSize = size
    
    def setCanvasPPI(self, ppi):  
        self.ppi = ppi
    
    @property
    def canvasSize(self):
        return self._canvasSize
    @property
    def ppi(self):
        return self._ppi
    
    @canvasSize.setter
    def canvasSize(self, size):
        self._canvasSize = size
        if self.painter:
            self.painter.setSceneRect(0, 0, *paperSizes[self.canvasSize][self.ppi])
            self.setMaximumSize(*paperSizes[self.canvasSize][self.ppi])
            self.resize(*paperSizes[self.canvasSize][self.ppi])


    @ppi.setter
    def ppi(self, ppi):
        self._ppi = ppi
        if self.painter:
            self.painter.setSceneRect(0, 0, *paperSizes[self.canvasSize][self.ppi])
            self.setMaximumSize(*paperSizes[self.canvasSize][self.ppi])
            self.resize(*paperSizes[self.canvasSize][self.ppi])

    @property
    def dimensions(self):
        return self.painter.sceneRect().width(), self.painter.sceneRect().height()
    
    def __getstate__(self) -> dict:
        return {
            "_classname_": self.__class__.__name__,
            "ppi": self._ppi,
            "canvasSize": self._canvasSize,
            "ObjectName": self.objectName(),
            "items": [i.__getState__() for i in self.painter.items()]
        }
    
    def __setstate__(self, dict):
        self.__init__()
        self._ppi = dict['ppi']
        self._canvasSize = dict['canvasSize']
        self.setObjectName(dict['ObjectName'])
        for item in dict['items']:
            graphic = getattr(graphics, dict['_classname_'])
            graphic.setstate(item)
            self.painter.addItem(graphic)