summaryrefslogtreecommitdiff
path: root/src/main/python/utils/custom.py
diff options
context:
space:
mode:
authorBlaine2020-06-20 14:43:57 +0530
committerBlaine2020-06-20 14:43:57 +0530
commitf8519766d7f7fc70180411f912ed8ca15abb6a95 (patch)
tree97eb7e5570b43f3fc10628d37360351330e16bf3 /src/main/python/utils/custom.py
parent7ff2d30a1c16216f3bfaebc8f71924d6a6f8ec8d (diff)
downloadChemical-PFD-f8519766d7f7fc70180411f912ed8ca15abb6a95.tar.gz
Chemical-PFD-f8519766d7f7fc70180411f912ed8ca15abb6a95.tar.bz2
Chemical-PFD-f8519766d7f7fc70180411f912ed8ca15abb6a95.zip
refactor and comment code
Diffstat (limited to 'src/main/python/utils/custom.py')
-rw-r--r--src/main/python/utils/custom.py64
1 files changed, 51 insertions, 13 deletions
diff --git a/src/main/python/utils/custom.py b/src/main/python/utils/custom.py
index 7e6b197..fedecdb 100644
--- a/src/main/python/utils/custom.py
+++ b/src/main/python/utils/custom.py
@@ -1,3 +1,6 @@
+"""
+Holds the custom window to generate new symbols, can be called while running or throught the build interface.
+"""
from PyQt5.QtCore import QRectF, Qt, QSize
from PyQt5.QtGui import (QBrush, QIcon, QImage, QPainter, QPainterPath, QPen,
QPixmap, QTransform)
@@ -12,17 +15,19 @@ from shapes import SizeGripItem, directionsEnum
from .app import fileImporter
-
class ShapeDialog(QDialog):
-
+ """
+ The main dialog box for the custom symbol window.
+ """
def __init__(self, parent=None):
super(ShapeDialog, self).__init__(parent)
- self.resize(500, 300)
+ self.resize(500, 300) # resize to a fixed dim
self.setWindowTitle("Add New Shapes")
self.createLayout()
self.graphic = None
def createLayout(self):
+ #build layout for the dialog box
importButton = QPushButton("Import", self)
importButton.clicked.connect(self.importSVG)
@@ -80,6 +85,7 @@ class ShapeDialog(QDialog):
self.setLayout(layout)
def importSVG(self):
+ # Imports svg file through user input, adds it to the scene and stores it as a reference
self.name = QFileDialog.getOpenFileName(self, 'Open SVG File', '', 'Scalable Vector Graphics (*svg)')
if self.name:
self.graphic = QGraphicsSvgItem(self.name[0])
@@ -87,6 +93,9 @@ class ShapeDialog(QDialog):
self.painter.addItem(self.graphic)
def saveEvent(self):
+ # executes the build procedure
+
+ #check if all necessary values are there, each is seperate to show qalerts later on
if self.graphic is None:
return
@@ -102,10 +111,9 @@ class ShapeDialog(QDialog):
if category == "":
category = "misc"
+ # get rect for calculating grip positions
graphicRect = self.graphic.boundingRect()
-
- QIcon
-
+
#save file
name = QFileDialog.getSaveFileName(self, 'Save Icon', className, 'PNG (*.png)')
if name:
@@ -113,6 +121,7 @@ class ShapeDialog(QDialog):
else:
return
+ #calculate grip positions and build a list
gripList = []
x, y, w, h = graphicRect.getRect()
for i in self.grips:
@@ -125,10 +134,12 @@ class ShapeDialog(QDialog):
entry.append(i.width)
gripList.append(entry)
-
+ # format list in class definition flavor
grips = ",\n ".join([str(i) for i in gripList]) if gripList else ""
if grips:
grips = "self.grips = [" + grips + "]\n"
+
+ # build output dialog box
temp = QDialog(self)
tempLayout = QBoxLayout(QBoxLayout.TopToBottom)
output = OutputBox(temp, f"""
@@ -159,15 +170,19 @@ class {className}(NodeItem):
return [i for i in self.painter.items() if isinstance(i, gripAbstract)]
def addGrip(self):
+ #adds a grip dot to the scene
grip = gripDot()
self.painter.addItem(grip)
def addLineGrip(self):
+ #adds a line grip item
rect = gripRect()
self.painter.addItem(rect)
class gripAbstract(QGraphicsItem):
-
+ """
+ Abstract class for mouse click behaviour
+ """
def __init__(self):
super(gripAbstract, self).__init__()
self.location = "top"
@@ -178,6 +193,10 @@ class gripAbstract(QGraphicsItem):
directionsEnum.index(self.location), False)
class gripRect(gripAbstract):
+ """
+ simulates line grip item with resizeablity
+ (Haha grip items on grip items. Progress)
+ """
def __init__(self, x=0, y=0, w=80, h=10 ):
super(gripRect, self).__init__()
self.rotation = 0
@@ -185,7 +204,8 @@ class gripRect(gripAbstract):
self.width = w
self.height = h
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
-
+
+ #bounding rect and paint need to be implemented
def boundingRect(self):
return QRectF(-self.width / 2, -self.height / 2, self.width, self.height)
@@ -193,8 +213,9 @@ class gripRect(gripAbstract):
painter.setPen(QPen(Qt.black, 1, Qt.SolidLine))
painter.setBrush(QBrush(Qt.red))
painter.drawRect(self.boundingRect())
-
+
def addGripItems(self):
+ # adds the resizeable line grip items from the shape file :D
for i, (direction) in enumerate((Qt.Vertical,
Qt.Horizontal,
Qt.Vertical,
@@ -202,12 +223,14 @@ class gripRect(gripAbstract):
self.sizeGripItems.append(SizeGripItem(i, direction, parent=self))
def updateSizeGripItem(self, index_no_updates=None):
+ #update positions for existing grip items
index_no_updates = index_no_updates or []
for i, item in enumerate(self.sizeGripItems):
if i not in index_no_updates:
item.updatePosition()
def itemChange(self, change, value):
+ #do the needful when item is updated
if change == QGraphicsItem.ItemPositionHasChanged:
# update grips
self.updateSizeGripItem()
@@ -221,6 +244,7 @@ class gripRect(gripAbstract):
return super(gripRect, self).itemChange(change, value)
def resize(self, index, movement):
+ #resize method
self.prepareGeometryChange()
if index in [0, 1]:
self.width -= movement.x()
@@ -233,7 +257,10 @@ class gripRect(gripAbstract):
self.setTransform(transform, True)
self.updateSizeGripItem([index])
-class gripDot(gripAbstract):
+class gripDot(gripAbstract):
+ """
+ class for circular grips
+ """
def boundingRect(self):
return QRectF(0, 0, 10, 10)
@@ -243,9 +270,20 @@ class gripDot(gripAbstract):
painter.drawEllipse(self.boundingRect())
class OutputBox(QTextEdit):
-
+ """
+ Defines a read only text box for class output
+ """
def __init__(self, parent, text):
super(OutputBox, self).__init__(parent)
self.setReadOnly(True)
self.setFontWeight(10)
- self.setHtml(text) \ No newline at end of file
+ self.setHtml(text)
+
+if __name__ == '__main__': # 1. Instantiate ApplicationContext
+ #if app is launched directly
+ from .app import app
+ import sys
+ main = ShapeDialog()
+ main.show()
+ exit_code = app.app.exec_() # 2. Invoke app.app.exec_()
+ sys.exit(exit_code) \ No newline at end of file