diff options
author | Blaine | 2020-05-02 14:25:03 +0530 |
---|---|---|
committer | Blaine | 2020-05-02 14:25:03 +0530 |
commit | 06816626c9ef691303012008dfae09b0210f78a1 (patch) | |
tree | c1b6887a65f416bcc6c616a4f4741f2002036d1c | |
parent | d2dd2117a260f188cc3d0bacc2a6b863bcd973c0 (diff) | |
download | Chemical-PFD-06816626c9ef691303012008dfae09b0210f78a1.tar.gz Chemical-PFD-06816626c9ef691303012008dfae09b0210f78a1.tar.bz2 Chemical-PFD-06816626c9ef691303012008dfae09b0210f78a1.zip |
refactor and cleanup
-rw-r--r-- | src/main/python/main.py | 11 | ||||
-rw-r--r-- | src/main/python/utils/canvas.py | 33 | ||||
-rw-r--r-- | src/main/python/utils/dialogs.py | 17 | ||||
-rw-r--r-- | src/main/python/utils/fileWindow.py | 83 | ||||
-rw-r--r-- | src/main/python/utils/tabs.py | 3 |
5 files changed, 70 insertions, 77 deletions
diff --git a/src/main/python/main.py b/src/main/python/main.py index abdb822..83cbffd 100644 --- a/src/main/python/main.py +++ b/src/main/python/main.py @@ -41,12 +41,14 @@ class appWindow(QMainWindow): self.mdi = QMdiArea(self) #create area for files to be displayed self.mdi.setObjectName('mdi area') + #create toolbar and add the toolbar plus mdi to layout self.createToolbar() mainLayout.addWidget(self.toolbar) mainLayout.addWidget(self.mdi) - self.mdi.setOption(QMdiArea.DontMaximizeSubWindowOnActivation, True) #set flag so that window doesnt look weird + #set flag so that window doesnt look weird + self.mdi.setOption(QMdiArea.DontMaximizeSubWindowOnActivation, True) self.mdi.setTabsClosable(True) self.mdi.setTabsMovable(True) self.mdi.setDocumentMode(True) @@ -55,7 +57,6 @@ class appWindow(QMainWindow): self.mainWidget.setLayout(mainLayout) self.setCentralWidget(self.mainWidget) self.resize(1280, 720) #set collapse dim - # self.setWindowState(Qt.WindowMaximized) #launch maximized def createToolbar(self): #place holder for toolbar with fixed width, layout may change @@ -74,7 +75,6 @@ class appWindow(QMainWindow): project.newDiagram() #create a new tab in the new file project.resizeHandler() project.fileCloseEvent.connect(self.fileClosed) #closed file signal to switch to sub window view - project.fileMinimized.connect(self.fileMinimized) if self.count > 1: #switch to tab view if needed self.mdi.setViewMode(QMdiArea.TabbedView) project.show() @@ -126,12 +126,9 @@ class appWindow(QMainWindow): event.accept() def fileClosed(self, index): + #checks if the file tab menu needs to be removed if self.count <= 2 : self.mdi.setViewMode(QMdiArea.SubWindowView) - - def fileMinimized(self, file): - if self.count > 1: - self.mdi.activateNextSubWindow() @property def activeFiles(self): diff --git a/src/main/python/utils/canvas.py b/src/main/python/utils/canvas.py index 2ddcc01..8f5f7b4 100644 --- a/src/main/python/utils/canvas.py +++ b/src/main/python/utils/canvas.py @@ -36,48 +36,47 @@ class canvas(QWidget): self.setLayout(self.layout) #set layout and background color + #set initial paper size for the scene self.painter.setSceneRect(0, 0, *paperSizes[self.canvasSize][self.ppi]) - # ensure that the scene is always aligned on the left, instead of being - # centered (the default) - # self.view.setAlignment(Qt.AlignLeft|Qt.AlignTop) + #set pointers to necessary parents for ease of reference self.parentMdiArea = self.parent().parentWidget().parentWidget().parentWidget().parentWidget() self.parentFileWindow = self.parent().parentWidget().parentWidget() def resizeView(self, w, h): #helper function to resize canvas self.painter.setSceneRect(0, 0, w, h) - # self.view.setSceneRect(0, 0, w - self.view.frameWidth() * 2, h) - # self.adjustView() def adjustView(self): - #update view size + #utitily to adjust current diagram view width, height = self.dimensions frameWidth = self.view.frameWidth() - self.view.setSceneRect(0, 0, width - frameWidth*2, height) - # give the view some time to adjust itself - + #update view size + self.view.setSceneRect(0, 0, width - frameWidth*2, height) + + # use the available mdi area, also add padding prect = self.parentMdiArea.rect() - # prect = self.parent().parent().size() width = width + 20 height = height + 60 + # add scrollbar size to width and height if they are visible, avoids clipping if self.view.verticalScrollBar().isVisible(): width += self.style().pixelMetric(QStyle.PM_ScrollBarExtent) if self.view.horizontalScrollBar().isVisible(): height += self.style().pixelMetric(QStyle.PM_ScrollBarExtent) + #if view is visible use half of available width factor = 2 if self.parentFileWindow.sideViewTab is not None else 1 + #use minimum width required to fit the view width = min((prect.width() - 20)//factor, width) height = min(prect.height() - 80, height) + #set view dims self.view.setFixedWidth(width) self.view.setFixedHeight(height) - # self.resize(width + frameWidth * 2, height + frameWidth * 2) def resizeEvent(self, event): #overloaded function to also view size on window update self.adjustView() - # pass def setCanvasSize(self, size): """ @@ -92,6 +91,11 @@ class canvas(QWidget): self.ppi = ppi @property + def dimensions(self): + #returns the dimension of the current scene + return self.painter.sceneRect().width(), self.painter.sceneRect().height() + + @property def canvasSize(self): return self._canvasSize @property @@ -109,11 +113,6 @@ class canvas(QWidget): self._ppi = ppi if self.painter: self.resizeView(*paperSizes[self.canvasSize][self.ppi]) - - @property - def dimensions(self): - #returns the dimension of the current scene - return self.painter.sceneRect().width(), self.painter.sceneRect().height() #following 2 methods are defined for correct pickling of the scene. may be changed to json or xml later so as # to not have a binary file. diff --git a/src/main/python/utils/dialogs.py b/src/main/python/utils/dialogs.py index d5522cc..4970468 100644 --- a/src/main/python/utils/dialogs.py +++ b/src/main/python/utils/dialogs.py @@ -36,6 +36,7 @@ class paperDims(QDialog): dialogBoxLayout.setWidget(1, QFormLayout.LabelRole, ppiLabel) dialogBoxLayout.setWidget(1, QFormLayout.FieldRole, ppiComboBox) + # add ok and cancel buttons buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self) buttonBox.accepted.connect(self.accept) buttonBox.rejected.connect(self.reject) @@ -43,8 +44,6 @@ class paperDims(QDialog): dialogBoxLayout.addWidget(buttonBox) self.setLayout(dialogBoxLayout) self.resize(300,100) #resize to a certain size - - #todo add ok or cancel buttons def setCanvasSize(self, size): #for standard combo box behaviour @@ -57,20 +56,10 @@ class paperDims(QDialog): def exec_(self): #overload exec_ to add return values and delete itself(currently being tested) super(paperDims, self).exec_() - self.deleteLater() + self.deleteLater() #remove from memory + #if ok was pressed return value else return None return (self._canvasSize, self._ppi) if self.result() else None - - def accept(self): - self.setResult(1) - self.accepted.emit() - return super(paperDims, self).accept() - - def reject(self): - self.setResult(0) - self.rejected.emit() - return super(paperDims, self).reject() - def saveEvent(parent = None): #utility function to generate a Qt alert window requesting the user to save the file, returns user intention on window close alert = QMessageBox.question(parent, parent.objectName(), "All unsaved progress will be LOST!", diff --git a/src/main/python/utils/fileWindow.py b/src/main/python/utils/fileWindow.py index f9b3ac5..c850b27 100644 --- a/src/main/python/utils/fileWindow.py +++ b/src/main/python/utils/fileWindow.py @@ -13,7 +13,7 @@ class fileWindow(QMdiSubWindow): canvases. Pre-Defined so that a file can be instantly created without defining the structure again. """ fileCloseEvent = pyqtSignal(int) - fileMinimized = pyqtSignal(QMdiSubWindow) + def __init__(self, parent = None, title = 'New Project', size = 'A4', ppi = '72'): super(fileWindow, self).__init__(parent) self._sideViewTab = None @@ -53,61 +53,49 @@ class fileWindow(QMdiSubWindow): self.splitter.setVisible(False) self.sideView.setVisible(False) - def changeTab(self, currentIndex): - #placeholder function to detect tab change - self.resizeHandler() - - def closeTab(self, currentIndex): - #show save alert on tab close - if dialogs.saveEvent(self): - self.tabber.widget(currentIndex).deleteLater() - self.tabber.removeTab(currentIndex) - - def newDiagram(self): - # helper function to add a new tab on pressing new tab button, using the add tab method on QTabWidget - diagram = canvas(self.tabber) - diagram.setObjectName("New") - index = self.tabber.addTab(diagram, "New") - self.tabber.setCurrentIndex(index) - def resizeHandler(self): - # experimental resize Handler to handle resize on parent resize. + # resize Handler to handle resize cases. parentRect = self.mdiArea().size() current = self.tabber.currentWidget() width, height = current.dimensions + + # if side view is visible, set width to maximum possible, else use minimum requirement if self.sideViewTab: width = parentRect.width() height = parentRect.height() else: width = min(parentRect.width(), width + 100) height = min(parentRect.height(), height + 200) - # width = parentRect.width() - # height = parentRect.height() + + # set element dimensions self.setFixedSize(width, height) self.tabber.resize(width, height) self.tabber.currentWidget().adjustView() - - def adjustCanvasDialog(self): - #helper function to the context menu dialog box - currentTab = self.tabber.currentWidget() - result = dialogs.paperDims(self, currentTab._canvasSize, currentTab._ppi, currentTab.objectName()).exec_() - if result is not None: - currentTab.canvasSize, currentTab.ppi = result - return self.resizeHandler() - else: - return None def contextMenu(self, point): #function to display the right click menu at point of right click menu = QMenu("Context Menu", self) menu.addAction("Adjust Canvas", self.adjustCanvasDialog) - menu.addAction("View Side-By-Side", self.sideViewMode) + menu.addAction("Remove Side View" if self.sideViewTab == self.tabber.currentWidget() else "View Side-By-Side", + self.sideViewMode) menu.exec_(self.mapToGlobal(point)) - def sideViewMode(self): + def sideViewMode(self): + #helper context menu function to toggle side view self.sideViewTab = self.tabber.currentWidget() + def adjustCanvasDialog(self): + #helper context menu function to the context menu dialog box + currentTab = self.tabber.currentWidget() + result = dialogs.paperDims(self, currentTab._canvasSize, currentTab._ppi, currentTab.objectName()).exec_() + if result is not None: + currentTab.canvasSize, currentTab.ppi = result + return self.resizeHandler() + else: + return None + def sideViewToggle(self): + #Function checks if current side view tab is set, and toggles view as required if self.sideViewTab: self.splitter.setVisible(True) self.sideView.setVisible(True) @@ -122,13 +110,9 @@ class fileWindow(QMdiSubWindow): @property def sideViewTab(self): + #returns current active if sideViewTab otherwise None return self._sideViewTab - @sideViewTab.setter - def sideViewTab(self, tab): - self._sideViewTab = None if tab == self.sideViewTab else tab - return self.sideViewToggle() - @property def tabList(self): #returns a list of tabs in the given window @@ -139,6 +123,29 @@ class fileWindow(QMdiSubWindow): #returns the number of tabs in the given window only return self.tabber.count() + @sideViewTab.setter + def sideViewTab(self, tab): + #setter for side view. Also toggles view as necessary + self._sideViewTab = None if tab == self.sideViewTab else tab + return self.sideViewToggle() + + def changeTab(self, currentIndex): + #placeholder function to detect tab change + self.resizeHandler() + + def closeTab(self, currentIndex): + #show save alert on tab close + if dialogs.saveEvent(self): + self.tabber.widget(currentIndex).deleteLater() + self.tabber.removeTab(currentIndex) + + def newDiagram(self): + # helper function to add a new tab on pressing new tab button, using the add tab method on QTabWidget + diagram = canvas(self.tabber) + diagram.setObjectName("New") + index = self.tabber.addTab(diagram, "New") + self.tabber.setCurrentIndex(index) + def saveProject(self, name = None): # called by dialog.saveEvent, saves the current file name = QFileDialog.getSaveFileName(self, 'Save File', f'New Diagram', 'Process Flow Diagram (*.pfd)') if not name else name diff --git a/src/main/python/utils/tabs.py b/src/main/python/utils/tabs.py index 037710c..1049ce5 100644 --- a/src/main/python/utils/tabs.py +++ b/src/main/python/utils/tabs.py @@ -37,13 +37,14 @@ class customTabWidget(QTabWidget): self.tab.layoutChanged.connect(self.movePlusButton) #connect layout change # to dynamically move the button. + + #set custom stylesheet for the widget area self.setStyleSheet("""QTabWidget::pane { margin: 0px,1px,1px,1px; border: 2px solid #E6E6E3; border-radius: 7px; padding: 1px; background-color: #E6E6E3;}""") - self.setContentsMargins(0, 0, 0, 0) def movePlusButton(self): #move the new tab button to correct location |