diff options
Diffstat (limited to 'src/main/python/utils')
-rw-r--r-- | src/main/python/utils/app.py | 35 | ||||
-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 |
5 files changed, 43 insertions, 7 deletions
diff --git a/src/main/python/utils/app.py b/src/main/python/utils/app.py index 4ee11c3..aa1ce28 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 app = ApplicationContext() settings = QSettings(QSettings.IniFormat, QSettings.UserScope ,"FOSSEE", "Chemical-PFD") @@ -11,3 +12,37 @@ 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 _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..10601f2 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": [dumps(i, indent=4, cls=JSON_Typer) for i in self.painter.items() if isinstance(i, shapes.NodeItem)], + "lines": [dumps(i, indent=4, cls=JSON_Typer) 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..215ebfe 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 dumps, 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) + dumps(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): """ |