diff options
author | Blaine | 2020-05-25 03:50:27 +0530 |
---|---|---|
committer | Blaine | 2020-05-25 03:50:27 +0530 |
commit | 26f3fee786c7e64fea85f93458350ca69dc28acb (patch) | |
tree | a15135490e35b3d95b80fe6f8196d212aa080a8b /src/main/python | |
parent | 7417af719f678f3b00d513e8e21b4759bf19e960 (diff) | |
download | Chemical-PFD-26f3fee786c7e64fea85f93458350ca69dc28acb.tar.gz Chemical-PFD-26f3fee786c7e64fea85f93458350ca69dc28acb.tar.bz2 Chemical-PFD-26f3fee786c7e64fea85f93458350ca69dc28acb.zip |
week 5 refactor
Diffstat (limited to 'src/main/python')
-rw-r--r-- | src/main/python/main.py | 2 | ||||
-rw-r--r-- | src/main/python/utils/app.py | 5 | ||||
-rw-r--r-- | src/main/python/utils/data.py | 4 | ||||
-rw-r--r-- | src/main/python/utils/graphics.py | 35 | ||||
-rw-r--r-- | src/main/python/utils/undo.py | 16 |
5 files changed, 43 insertions, 19 deletions
diff --git a/src/main/python/main.py b/src/main/python/main.py index dfbb8c8..a2aaf8b 100644 --- a/src/main/python/main.py +++ b/src/main/python/main.py @@ -62,7 +62,7 @@ class appWindow(QMainWindow): self.mdi.subWindowActivated.connect(self.tabSwitched) def updateMenuBar(self): - # self.undo.setAction(self.activeScene.painter.undoAction) + # used to update menu bar undo-redo buttons to current scene self.undo.triggered.connect(self.activeScene.painter.undoAction.trigger()) self.redo.triggered.connect(self.activeScene.painter.redoAction.trigger()) diff --git a/src/main/python/utils/app.py b/src/main/python/utils/app.py index 6339ca3..4540a43 100644 --- a/src/main/python/utils/app.py +++ b/src/main/python/utils/app.py @@ -1,5 +1,10 @@ +""" +Declare fbs application so that it can be imported in other modules. +""" + from fbs_runtime.application_context.PyQt5 import ApplicationContext app = ApplicationContext() def fileImporter(file): + # Helper function to fetch files from src/main/resources return app.get_resource(file) diff --git a/src/main/python/utils/data.py b/src/main/python/utils/data.py index a96554e..3b32008 100644 --- a/src/main/python/utils/data.py +++ b/src/main/python/utils/data.py @@ -1,3 +1,7 @@ +""" +Imports data from json configs, so that they can be imported from this module. +""" + from json import load from .app import fileImporter diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index 6b074ce..fdc0a40 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -20,6 +20,7 @@ class customView(QGraphicsView): self.setDragMode(True) #sets pannable using mouse self.setAcceptDrops(True) #sets ability to accept drops if scene: + #create necessary undo redo actions to accept keyboard shortcuts self.addAction(scene.undoAction) self.addAction(scene.redoAction) self.addAction(scene.deleteAction) @@ -78,17 +79,16 @@ class customView(QGraphicsView): class customScene(QGraphicsScene): """ - re-implement QGraphicsScene for future functionality - hint: QUndoFramework + Extends QGraphicsScene with undo-redo functionality """ def __init__(self, *args, parent=None): super(customScene, self).__init__(*args, parent=parent) - self.undoStack = QUndoStack(self) - self.createActions() - + self.undoStack = QUndoStack(self) #Used to store undo-redo moves + self.createActions() #creates necessary actions that need to be called for undo-redo def createActions(self): + # helper function to create delete, undo and redo shortcuts self.deleteAction = QAction("Delete Item", self) self.deleteAction.setShortcut(Qt.Key_Delete) self.deleteAction.triggered.connect(self.deleteItem) @@ -99,35 +99,40 @@ class customScene(QGraphicsScene): self.redoAction.setShortcut(QKeySequence.Redo) def createUndoView(self, parent): + # creates an undo stack view for current QGraphicsScene undoView = QUndoView(self.undoStack, parent) showUndoDialog(undoView, parent) def deleteItem(self): + # (slot) used to delete all selected items, and add undo action for each of them if self.selectedItems(): for item in self.selectedItems(): self.undoStack.push(deleteCommand(item, self)) def itemMoved(self, movedItem, lastPos): + #item move event, checks if item is moved self.undoStack.push(moveCommand(movedItem, lastPos)) def addItemPlus(self, item): - # returnVal = self.addItem(item) + # extended add item method, so that a corresponding undo action is also pushed self.undoStack.push(addCommand(item, self)) - # return returnVal - + def mousePressEvent(self, event): - bdsp = event.buttonDownScenePos(Qt.LeftButton) - point = QPointF(bdsp.x(), bdsp.y()) - itemList = self.items(point) - self.movingItem = itemList[0] if itemList else None + # overloaded mouse press event to check if an item was moved + bdsp = event.buttonDownScenePos(Qt.LeftButton) #get click pos + point = QPointF(bdsp.x(), bdsp.y()) #create a Qpoint from click pos + itemList = self.items(point) #get items at said point + self.movingItem = itemList[0] if itemList else None #set first item in list as moving item if self.movingItem and event.button() == Qt.LeftButton: - self.oldPos = self.movingItem.pos() - self.clearSelection() + self.oldPos = self.movingItem.pos() #if left click is held, then store old pos + self.clearSelection() #clears selected items return super(customScene, self).mousePressEvent(event) def mouseReleaseEvent(self, event): + # overloaded mouse release event to check if an item was moved if self.movingItem and event.button() == Qt.LeftButton: if self.oldPos != self.movingItem.pos(): + #if item pos had changed, when mouse was realeased, emit itemMoved signal self.itemMoved(self.movingItem, self.oldPos) - self.movingItem = None + self.movingItem = None #clear movingitem reference return super(customScene, self).mouseReleaseEvent(event) diff --git a/src/main/python/utils/undo.py b/src/main/python/utils/undo.py index 7832483..4dbcc46 100644 --- a/src/main/python/utils/undo.py +++ b/src/main/python/utils/undo.py @@ -1,7 +1,12 @@ +""" +Contains custom undo commands that can be pushed to undo stack +""" from PyQt5.QtWidgets import QUndoCommand class addCommand(QUndoCommand): - + """ + QUndoCommand for add item event + """ def __init__(self, addItem, scene, parent = None): super(addCommand, self).__init__(parent) self.scene = scene @@ -20,7 +25,9 @@ class addCommand(QUndoCommand): self.scene.update() class deleteCommand(QUndoCommand): - + """ + QUndoCommand for delete item event + """ def __init__(self, item, scene, parent = None): super(deleteCommand, self).__init__(parent) self.scene = scene @@ -36,7 +43,9 @@ class deleteCommand(QUndoCommand): self.scene.removeItem(self.diagramItem) class moveCommand(QUndoCommand): - + """ + QUndoCommand for move item event + """ def __init__(self, item, lastPos, parent = None): super(moveCommand, self).__init__(parent) self.diagramItem = item @@ -53,6 +62,7 @@ class moveCommand(QUndoCommand): self.setText(f"Move {self.diagramItem} {self.newPos}") def mergeWith(self, move): + #merges multiple move commands so that a move event is not added twice. item = move.diagramItem if self.diagramItem != item: |