summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorBlaine2020-04-28 15:32:21 +0530
committerBlaine2020-04-28 15:32:21 +0530
commit19f5df0a0dff91ca775f7a05aec8d5199b7ca513 (patch)
tree4e59871fdfc34e498aaf3a71628ae0dc33ad98bf /src/main
parent2e8523900f9bb0a7ced4ed967c4ccc63b682f8ca (diff)
downloadChemical-PFD-19f5df0a0dff91ca775f7a05aec8d5199b7ca513.tar.gz
Chemical-PFD-19f5df0a0dff91ca775f7a05aec8d5199b7ca513.tar.bz2
Chemical-PFD-19f5df0a0dff91ca775f7a05aec8d5199b7ca513.zip
weird
Diffstat (limited to 'src/main')
-rw-r--r--src/main/python/main.py6
-rw-r--r--src/main/python/utils/canvas.py173
-rw-r--r--src/main/python/utils/fileWindow.py122
-rw-r--r--src/main/python/utils/tabs.py6
4 files changed, 174 insertions, 133 deletions
diff --git a/src/main/python/main.py b/src/main/python/main.py
index eb1805a..207701e 100644
--- a/src/main/python/main.py
+++ b/src/main/python/main.py
@@ -9,7 +9,8 @@ from PyQt5.QtWidgets import (QComboBox, QFileDialog, QFormLayout,
QHBoxLayout, QLabel, QMainWindow, QMenu, QMenuBar,
QPushButton, QTabWidget, QWidget, QMdiArea, QMessageBox)
-from utils.canvas import canvas, fileWindow
+from utils.canvas import canvas
+from utils.fileWindow import fileWindow
from utils.sizes import ppiList, sheetDimensionList
from utils import dialogs
@@ -79,8 +80,7 @@ class appWindow(QMainWindow):
with open(files,'rb') as file:
project = pickle.load(file)
self.mdi.addSubWindow(project)
- project.show()
- project.resizeHandler(self.mdi)
+ project.show()
def saveProject(self):
#pickle all files in mdi area
diff --git a/src/main/python/utils/canvas.py b/src/main/python/utils/canvas.py
index 696c736..ade0d03 100644
--- a/src/main/python/utils/canvas.py
+++ b/src/main/python/utils/canvas.py
@@ -2,14 +2,12 @@ import pickle
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush, QPalette
-from PyQt5.QtWidgets import (QFileDialog, QFormLayout,
- QGraphicsScene, QGraphicsView, QHBoxLayout,
- QMdiSubWindow, QMenu,
- QTabWidget, QWidget, QSpacerItem)
+from PyQt5.QtWidgets import (QFileDialog, QApplication,
+ QGraphicsScene, QGraphicsView, QHBoxLayout, QMenu,
+ QTabWidget, QWidget, QSpacerItem, QStyle)
from . import graphics, dialogs
from .sizes import paperSizes, ppiList, sheetDimensionList
-from .tabs import customTabWidget
class canvas(QWidget):
"""
@@ -33,18 +31,47 @@ class canvas(QWidget):
self.view = QGraphicsView(self.painter) #create a viewport for the canvas board
self.layout = QHBoxLayout(self) #create the layout of the canvas, the canvas could just subclass QGView instead
- self.layout.addWidget(self.view, stretch = 1, alignment= Qt.AlignLeft)
- self.spacer = QSpacerItem(1, self.height()) #Horizonatal spacer to force view to not expand to fill widget
- self.layout.addSpacerItem(self.spacer)
-
+ self.layout.addWidget(self.view, alignment=Qt.AlignCenter)
+ # self.spacer = QSpacerItem(1, self.height()) #Horizonatal spacer to force view to not expand to fill widget
+ # self.layout.addSpacerItem(self.spacer)
+ # self.layout.addSpacing(0)
#set layout and background color
- self.setPalette(self.palette)
- self.setLayout(self.layout)
-
- #This is done so that a right click menu is shown on right click
- self.setContextMenuPolicy(Qt.CustomContextMenu)
- self.customContextMenuRequested.connect(self.contextMenu)
+ self.setLayout(self.layout)
+ 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)
+
+ 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)
+
+ def adjustView(self):
+ #update view size
+ width, height = self.dimensions
+ # give the view some time to adjust itself
+ QApplication.processEvents()
+
+ self.view.setSceneRect(0, 0, width, height)
+ prect = self.parent().parentWidget().size()
+ frameWidth = self.view.frameWidth()
+ width = min(prect.width() - frameWidth*2, width + frameWidth * 2)
+ height = min(prect.height() - frameWidth*2, height + frameWidth * 2)
+
+ if self.view.verticalScrollBar().isVisible():
+ width += self.style().pixelMetric(QStyle.PM_ScrollBarExtent)
+ if self.view.horizontalScrollBar().isVisible():
+ height += self.style().pixelMetric(QStyle.PM_ScrollBarExtent)
+ 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):
"""
extended setter for dialog box
@@ -80,28 +107,7 @@ class canvas(QWidget):
def dimensions(self):
#returns the dimension of the current scene
return self.painter.sceneRect().width(), self.painter.sceneRect().height()
-
- def resizeView(self, w=None, h=None):
- #resize canvas to appropriate size.
- if w is None and h is None:
- w, h = paperSizes[self.canvasSize][self.ppi]
- self.painter.setSceneRect(0, 0, w, h)
- self.view.setSceneRect(0, 0, w, h)
- w = min(self.width() - 5, w)
- h = self.height() - 5
- self.view.setFixedWidth(w)
- self.view.setFixedHeight(h)
-
- 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.exec_(self.mapToGlobal(point))
- def adjustCanvasDialog(self):
- #helper function to the context menu dialog box
- self.canvasSize, self.ppi = dialogs.paperDims(self, self._canvasSize, self._ppi, self.objectName()).exec_()
-
#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.
def __getstate__(self) -> dict:
@@ -123,97 +129,4 @@ class canvas(QWidget):
graphic.__setstate__(item)
self.painter.addItem(graphic)
-class fileWindow(QMdiSubWindow):
- """
- This defines a single file, inside the application, consisting of multiple tabs that contain
- canvases. Pre-Defined so that a file can be instantly created without defining the structure again.
- """
- def __init__(self, parent = None, title = 'New Project', size = 'A4', ppi = '72'):
- super(fileWindow, self).__init__(parent)
-
- #Uses a custom QTabWidget that houses a custom new Tab Button, used to house the seperate
- # diagrams inside a file
- self.tabber = customTabWidget(self)
- self.tabber.setObjectName(title) #store title as object name for pickling
- self.tabber.tabCloseRequested.connect(self.closeTab) # Show save alert on tab close
- self.tabber.currentChanged.connect(self.changeTab) # placeholder just to detect tab change
- self.tabber.plusClicked.connect(self.newDiagram) #connect the new tab button to add a new tab
-
- #assign layout to widget
- self.setWidget(self.tabber)
- self.setWindowTitle(title)
-
- def changeTab(self, currentIndex):
- #placeholder function to detect tab change
- pass
-
- 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")
- self.tabber.addTab(diagram, "New")
- diagram.resizeView()
-
- def resizeHandler(self, parent = None):
- # experimental resize Handler to handle resize on parent resize.
- 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())
- self.tabber.currentWidget().resizeView()
-
- @property
- def tabList(self):
- #returns a list of tabs in the given window
- return [self.tabber.widget(i) for i in range(self.tabCount)]
-
- @property
- def tabCount(self):
- #returns the number of tabs in the given window only
- return self.tabber.count()
-
- 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
- if name:
- with open(name[0],'wb') as file:
- pickle.dump(self, file)
- return True
- else:
- return False
-
- def closeEvent(self, event):
- # handle save alert on file close, check if current file has no tabs aswell.
- if self.tabCount==0 or dialogs.saveEvent(self):
- event.accept()
- self.deleteLater()
- else:
- event.ignore()
-
- #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.
- 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'])
-
- \ No newline at end of file
+ \ No newline at end of file
diff --git a/src/main/python/utils/fileWindow.py b/src/main/python/utils/fileWindow.py
new file mode 100644
index 0000000..9a679e9
--- /dev/null
+++ b/src/main/python/utils/fileWindow.py
@@ -0,0 +1,122 @@
+import pickle
+
+from PyQt5.QtCore import Qt
+from PyQt5.QtWidgets import QMdiSubWindow, QFileDialog, QMenu
+
+from . import dialogs
+from .canvas import canvas
+from .tabs import customTabWidget
+
+
+class fileWindow(QMdiSubWindow):
+ """
+ This defines a single file, inside the application, consisting of multiple tabs that contain
+ canvases. Pre-Defined so that a file can be instantly created without defining the structure again.
+ """
+ def __init__(self, parent = None, title = 'New Project', size = 'A4', ppi = '72'):
+ super(fileWindow, self).__init__(parent)
+
+ #Uses a custom QTabWidget that houses a custom new Tab Button, used to house the seperate
+ # diagrams inside a file
+ self.tabber = customTabWidget(self)
+ self.tabber.setObjectName(title) #store title as object name for pickling
+ self.tabber.tabCloseRequested.connect(self.closeTab) # Show save alert on tab close
+ self.tabber.currentChanged.connect(self.changeTab) # placeholder just to detect tab change
+ self.tabber.plusClicked.connect(self.newDiagram) #connect the new tab button to add a new tab
+ # self.tabber.setContentsMargins(0, 1, 1, 1)
+ #assign layout to widget
+ self.setWidget(self.tabber)
+ self.setWindowTitle(title)
+
+ #This is done so that a right click menu is shown on right click
+ self.setContextMenuPolicy(Qt.CustomContextMenu)
+ self.customContextMenuRequested.connect(self.contextMenu)
+
+ 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")
+ self.tabber.addTab(diagram, "New")
+
+ def resizeHandler(self, parent = None):
+ # experimental resize Handler to handle resize on parent resize.
+ parentRect = parent.rect() if parent else self.parentWidget().rect()
+ width, height = self.tabber.currentWidget().dimensions
+ frameWidth = self.tabber.currentWidget().view.frameWidth()
+ width = min(parentRect.width(), width + frameWidth*2)
+ height = min(parentRect.height(), height + frameWidth*2)
+ self.setFixedSize(width, height)
+ # self.tabber.resize(width, height)
+ self.tabber.currentWidget().adjustView()
+
+ 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.exec_(self.mapToGlobal(point))
+
+ def adjustCanvasDialog(self):
+ #helper function to the context menu dialog box
+ currentTab = self.tabber.currentWidget()
+ currentTab.canvasSize, currentTab.ppi = dialogs.paperDims(self, currentTab._canvasSize, currentTab._ppi, currentTab.objectName()).exec_()
+ self.resizeHandler()
+
+ def resizeEvent(self, event):
+ print(self.size())
+ super(fileWindow, self).resizeEvent(event)
+
+ @property
+ def tabList(self):
+ #returns a list of tabs in the given window
+ return [self.tabber.widget(i) for i in range(self.tabCount)]
+
+ @property
+ def tabCount(self):
+ #returns the number of tabs in the given window only
+ return self.tabber.count()
+
+ 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
+ if name:
+ with open(name[0],'wb') as file:
+ pickle.dump(self, file)
+ return True
+ else:
+ return False
+
+ def closeEvent(self, event):
+ # handle save alert on file close, check if current file has no tabs aswell.
+ if self.tabCount==0 or dialogs.saveEvent(self):
+ event.accept()
+ self.deleteLater()
+ else:
+ event.ignore()
+
+ #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.
+ 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'])
diff --git a/src/main/python/utils/tabs.py b/src/main/python/utils/tabs.py
index 7d8bb1d..7c6319c 100644
--- a/src/main/python/utils/tabs.py
+++ b/src/main/python/utils/tabs.py
@@ -37,6 +37,12 @@ class customTabWidget(QTabWidget):
self.tab.layoutChanged.connect(self.movePlusButton) #connect layout change
# to dynamically move the button.
+ self.setStyleSheet("""QTabWidget::pane {
+ margin: 0px,1px,1px,1px;
+ border: 2px solid #E6E6E3;
+ border-radius: 7px;
+ padding: 1px;
+ background-color: #E6E6E3;}""")
def movePlusButton(self):
#move the new tab button to correct location