diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/python/main.py | 5 | ||||
-rw-r--r-- | src/main/python/shapes/shapes.py | 22 | ||||
-rw-r--r-- | src/main/python/utils/app.py | 39 | ||||
-rw-r--r-- | src/main/python/utils/canvas.py | 7 | ||||
-rw-r--r-- | src/main/python/utils/fileWindow.py | 5 | ||||
-rw-r--r-- | src/main/python/utils/graphics.py | 1 | ||||
-rw-r--r-- | src/main/python/utils/undo.py | 2 |
7 files changed, 69 insertions, 12 deletions
diff --git a/src/main/python/main.py b/src/main/python/main.py index 72eb7f8..ff41413 100644 --- a/src/main/python/main.py +++ b/src/main/python/main.py @@ -1,4 +1,3 @@ -import json import sys from fbs_runtime.application_context.PyQt5 import ApplicationContext @@ -13,7 +12,7 @@ from utils.fileWindow import fileWindow from utils.data import ppiList, sheetDimensionList from utils import dialogs from utils.toolbar import toolbar -from utils.app import app, settings +from utils.app import app, settings, load import shapes @@ -105,7 +104,7 @@ class appWindow(QMainWindow): if name: for files in name[0]: with open(files,'r') as file: - projectData = json.load(file) + projectData = load(file) project = fileWindow(self.mdi) self.mdi.addSubWindow(project) project.__setstate__(projectData) diff --git a/src/main/python/shapes/shapes.py b/src/main/python/shapes/shapes.py index 3e5cbb2..a795f6a 100644 --- a/src/main/python/shapes/shapes.py +++ b/src/main/python/shapes/shapes.py @@ -316,9 +316,9 @@ class NodeItem(QGraphicsSvgItem): # set a common renderer for all svg renderer = QSvgRenderer(fileImporter(f'svg/ellipse.svg')) - def __init__(self, unitOperationType, parent=None): + def __init__(self, unitOperationType=None, parent=None): QGraphicsSvgItem.__init__(self, parent) - self.m_type = unitOperationType + self.m_type = str(unitOperationType) self.id = None # self.m_renderer = QSvgRenderer("svg/" + unitOperationType + ".svg") # self.m_renderer = QSvgRenderer(fileImporter(f'svg/{unitOperationType}.svg')) @@ -499,6 +499,24 @@ class NodeItem(QGraphicsSvgItem): if action == addLableAction: self.label = ItemLabel(event.scenePos(), self) + def __getstate__(self): + return { + "_classname_": self.__class__.__name__, + "width": self.width, + "height": self.height, + "pos": (self.pos().x(), self.pos().y()) + } + + def __setstate__(self, dict): + self.prepareGeometryChange() + self.width = dict['width'] + self.height = dict['height'] + self.rect = QRectF(-self.width / 2, -self.height / 2, self.width, self.height) + transform = QTransform() + transform.translate(self.width / 2, self.height / 2) + self.setTransform(transform, True) + self.updateSizeGripItem() + # classes of pfd-symbols class AirBlownCooler(NodeItem): def __init__(self): diff --git a/src/main/python/utils/app.py b/src/main/python/utils/app.py index 4ee11c3..5d85cb7 100644 --- a/src/main/python/utils/app.py +++ b/src/main/python/utils/app.py @@ -4,6 +4,7 @@ Declare fbs application so that it can be imported in other modules. from fbs_runtime.application_context.PyQt5 import ApplicationContext from PyQt5.QtCore import QSettings +from json import JSONEncoder, dumps, loads, dump, load app = ApplicationContext() settings = QSettings(QSettings.IniFormat, QSettings.UserScope ,"FOSSEE", "Chemical-PFD") @@ -11,3 +12,41 @@ settings = QSettings(QSettings.IniFormat, QSettings.UserScope ,"FOSSEE", "Chemic def fileImporter(file): # Helper function to fetch files from src/main/resources return app.get_resource(file) + +class JSON_Encoder: + + def _encode(obj): + if isinstance(obj, dict): + ## We'll need to iterate not just the value that default() usually gets passed + ## But also iterate manually over each key: value pair in order to trap the keys. + + for key, val in list(obj.items()): + if isinstance(val, dict): + val = loads(dumps(val, cls=JSON_Typer)) # This, is a EXTREMELY ugly hack.. + # But it's the only quick way I can think of to + # trigger a encoding of sub-dictionaries. (I'm also very tired, yolo!) + else: + val = JSON_Encoder._encode(val) + del(obj[key]) + obj[JSON_Encoder._encode(key)] = val + return obj + elif hasattr(obj, '__getstate__'): + return obj.__getstate__() + elif isinstance(obj, (list, set, tuple)): + r = [] + for item in obj: + r.append(loads(dumps(item, cls=JSON_Typer))) + return r + else: + return obj + +class JSON_Typer(JSONEncoder): + + def default(self, o): + return o.__getstate__() + + def _encode(self, obj): + return JSON_Encoder._encode(obj) + + def encode(self, obj): + return super(JSON_Typer, self).encode(self._encode(obj))
\ No newline at end of file diff --git a/src/main/python/utils/canvas.py b/src/main/python/utils/canvas.py index dee0745..a83aeb2 100644 --- a/src/main/python/utils/canvas.py +++ b/src/main/python/utils/canvas.py @@ -1,5 +1,3 @@ -import json - from PyQt5.QtCore import Qt, QPointF from PyQt5.QtGui import QBrush, QPalette from PyQt5.QtWidgets import (QFileDialog, QApplication, QHBoxLayout, QMenu, @@ -8,6 +6,7 @@ from PyQt5.QtWidgets import (QFileDialog, QApplication, QHBoxLayout, QMenu, from . import dialogs from .graphics import customView, customScene from .data import paperSizes, ppiList, sheetDimensionList +from .app import dumps, loads, JSON_Typer import shapes @@ -129,8 +128,8 @@ class canvas(QWidget): "ppi": self._ppi, "canvasSize": self._canvasSize, "ObjectName": self.objectName(), - "symbols": [i.__getstate__() for i in self.painter.items() if isinstance(i, shapes.NodeItem)], - "lines": [i.__getstate__() for i in self.painter.items() if isinstance(i, shapes.Line)], + "symbols": [i for i in self.painter.items() if isinstance(i, shapes.NodeItem)], + "lines": [i for i in self.painter.items() if isinstance(i, shapes.Line)], # "lineLabels": [i.__getstate__() for i in self.painter.items() if isinstance(i, shapes.LineLabel)], # "itemLabels": [i.__getstate__() for i in self.painter.items() if isinstance(i, shapes.itemLabel)] } diff --git a/src/main/python/utils/fileWindow.py b/src/main/python/utils/fileWindow.py index 6ad4477..04ed81a 100644 --- a/src/main/python/utils/fileWindow.py +++ b/src/main/python/utils/fileWindow.py @@ -1,5 +1,3 @@ -import json - from PyQt5.QtCore import Qt, pyqtSignal, QPoint from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import (QFileDialog, QHBoxLayout, @@ -11,6 +9,7 @@ from .graphics import customView from .canvas import canvas from .tabs import customTabWidget from .undo import resizeCommand +from .app import dump, loads, JSON_Typer class fileWindow(QMdiSubWindow): @@ -223,7 +222,7 @@ class fileWindow(QMdiSubWindow): name = QFileDialog.getSaveFileName(self, 'Save File', f'New Diagram', 'Process Flow Diagram (*.pfd)') if not name else name if name[0]: with open(name[0],'w') as file: - json.dump(self.__getstate__(), file, indent=4) + dump(self, file, indent=4, cls=JSON_Typer) return True else: return False diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index a997a30..e17fa49 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -114,6 +114,7 @@ class customScene(QGraphicsScene): def itemMoved(self, movedItem, lastPos): #item move event, checks if item is moved self.undoStack.push(moveCommand(movedItem, lastPos)) + self.advance() def addItemPlus(self, item): # extended add item method, so that a corresponding undo action is also pushed diff --git a/src/main/python/utils/undo.py b/src/main/python/utils/undo.py index cf539e7..7979bf3 100644 --- a/src/main/python/utils/undo.py +++ b/src/main/python/utils/undo.py @@ -52,9 +52,11 @@ class deleteCommand(QUndoCommand): def undo(self): self.scene.addItem(self.diagramItem) self.scene.update() + self.scene.advance() def redo(self): self.scene.removeItem(self.diagramItem) + self.scene.advance() class moveCommand(QUndoCommand): """ |