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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
import pickle
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush
from PyQt5.QtWidgets import (QComboBox, QDialog, QFileDialog, QFormLayout,
QGraphicsScene, QGraphicsView, QHBoxLayout,
QLabel, QMainWindow, QMdiSubWindow, QMenu,
QMessageBox, QTabWidget, QWidget)
from . import graphics
from .sizes import paperSizes, ppiList, sheetDimensionList
from .tabs import customTabWidget
class canvas(QWidget):
def __init__(self, parent=None, size= 'A4', ppi= '72'):
super(canvas, self).__init__(parent)
self._ppi = ppi
self._canvasSize = size
self.painter = QGraphicsScene()
self.painter.setSceneRect(0, 0, *paperSizes[self.canvasSize][self.ppi])
self.painter.setBackgroundBrush(QBrush(Qt.white))
self.view = QGraphicsView(self.painter)
self.view.setMinimumSize(595, 842)
self.view.setSceneRect(0, 0, *paperSizes[self.canvasSize][self.ppi])
self.layout = QHBoxLayout(self)
self.layout.addWidget(self.view)
self.setLayout(self.layout)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.contextMenu)
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.view.setSceneRect(0, 0, *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.view.setSceneRect(0, 0, *paperSizes[self.canvasSize][self.ppi])
@property
def dimensions(self):
return self.painter.sceneRect().width(), self.painter.sceneRect().height()
def contextMenu(self, point):
menu = QMenu("Context Menu", self)
menu.addAction("Adjust Canvas", self.adjustCanvasDialog)
menu.exec_(self.mapToGlobal(point))
def adjustCanvasDialog(self):
dialogBox = QDialog(self)
dialogBox.setWindowTitle(self.objectName()+":Canvas Size")
dialogBoxLayout = QFormLayout(dialogBox)
sizeComboBox = QComboBox()
sizeComboBox.addItems(sheetDimensionList)
sizeComboBox.setCurrentIndex(4)
sizeComboBox.activated[str].connect(self.setCanvasSize)
sizeLabel = QLabel("Canvas Size")
sizeLabel.setBuddy(sizeComboBox)
sizeComboBox.setCurrentIndex(sheetDimensionList.index(self.canvasSize))
dialogBoxLayout.setWidget(0, QFormLayout.LabelRole, sizeLabel)
dialogBoxLayout.setWidget(0, QFormLayout.FieldRole, sizeComboBox)
ppiComboBox = QComboBox()
ppiComboBox.addItems(ppiList)
ppiComboBox.activated[str].connect(self.setCanvasPPI)
ppiLabel = QLabel("Canvas ppi")
ppiLabel.setBuddy(ppiComboBox)
ppiComboBox.setCurrentIndex(ppiList.index(self.ppi))
dialogBoxLayout.setWidget(1, QFormLayout.LabelRole, ppiLabel)
dialogBoxLayout.setWidget(1, QFormLayout.FieldRole, ppiComboBox)
dialogBox.setLayout(dialogBoxLayout)
dialogBox.exec_()
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)
class fileWindow(QMdiSubWindow):
def __init__(self, parent = None, title = 'New Project', size = 'A4', ppi = '72'):
super(fileWindow, self).__init__(parent)
self._ppi = ppi
self._canvasSize = size
self.tabber = customTabWidget(self)
self.tabber.setObjectName(title)
self.tabber.tabCloseRequested.connect(self.closeTab)
self.tabber.currentChanged.connect(self.changeTab)
self.tabber.plusClicked.connect(self.newDiagram)
self.setWidget(self.tabber)
self.setWindowTitle(title)
@property
def canvasSize(self):
return self._canvasSize
@property
def ppi(self):
return self._ppi
@canvasSize.setter
def canvasSize(self, size):
self._canvasSize = sheetDimensionList.index(size)
if self.tabCount:
activeTab = self.tabber.currentWidget()
activeTab.canvasSize = size
@ppi.setter
def ppi(self, ppi):
self._ppi = ppiList.index(ppi)
if self.tabCount:
activeTab = self.tabber.currentWidget()
activeTab.ppi = ppi
def changeTab(self, currentIndex):
pass
def closeTab(self, currentIndex):
if self.saveEvent():
self.tabber.widget(currentIndex).deleteLater()
self.tabber.removeTab(currentIndex)
def newDiagram(self):
diagram = canvas(self.tabber, size = self._canvasSize, ppi = self._ppi)
diagram.setObjectName("New")
self.tabber.addTab(diagram, "New")
def resizeHandler(self, parent = None):
parentRect = parent.rect() if parent else self.parent().rect()
self.resize(parentRect.width(), parentRect.height())
self.setMaximumHeight(parentRect.height())
self.tabber.setMaximumHeight(parentRect.height())
for i in self.tabList:
i.setMaximumHeight(parentRect.height())
@property
def tabList(self):
return [self.tabber.widget(i) for i in range(self.tabCount)]
@property
def tabCount(self):
return self.tabber.count()
def __getstate__(self) -> dict:
return {
"_classname_": self.__class__.__name__,
"ObjectName": self.objectName(),
"ppi": self._ppi,
"canvasSize": self._canvasSize,
"tabs": [i.__getstate__() for i in self.tabList]
}
def __setstate__(self, dict):
self.__init__(title = dict['ObjectName'])
for i in dict['tabs']:
diagram = canvas(self.tabber, size = dict['canvasSize'], ppi = dict['ppi'])
diagram.__setstate__(i)
self.tabber.addTab(diagram, i['ObjectName'])
def saveProject(self, name = None):
name = QFileDialog.getSaveFileName(self, 'Save File', f'New Diagram', 'Process Flow Diagram (*.pfd)') if not name else name
if name:
with open(name[0],'wb') as file:
pickle.dump(self, file)
return True
else:
return False
def closeEvent(self, event):
if self.tabCount or self.saveEvent():
event.accept()
else:
event.ignore()
def saveEvent(self):
alert = QMessageBox.question(self, self.objectName(), "All unsaved progress will be LOST!",
QMessageBox.StandardButtons(QMessageBox.Save|QMessageBox.Ignore|QMessageBox.Cancel), QMessageBox.Save)
if alert == QMessageBox.Cancel:
return False
else:
if alert == QMessageBox.Save:
if not self.saveProject():
return False
return True
|