summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsumit2020-05-19 20:21:05 +0530
committersumit2020-05-19 20:21:05 +0530
commit0e8a9866a7101233820273e9d97598af31382ef0 (patch)
treef7fbb46123f3879c4904dea2b0156e8fa7259e98 /src
parent2917912f54c26ccfca010f6fc09f75425fa3a2df (diff)
downloadChemical-PFD-0e8a9866a7101233820273e9d97598af31382ef0.tar.gz
Chemical-PFD-0e8a9866a7101233820273e9d97598af31382ef0.tar.bz2
Chemical-PFD-0e8a9866a7101233820273e9d97598af31382ef0.zip
clean up and add comments
Diffstat (limited to 'src')
-rw-r--r--src/main/python/shapes/line.py299
-rw-r--r--src/main/python/shapes/shapes.py238
2 files changed, 353 insertions, 184 deletions
diff --git a/src/main/python/shapes/line.py b/src/main/python/shapes/line.py
index d83cf71..310b496 100644
--- a/src/main/python/shapes/line.py
+++ b/src/main/python/shapes/line.py
@@ -1,63 +1,254 @@
-from PyQt5.QtGui import QFont, QPen, QPainterPath, QPolygon, QBrush
-from PyQt5.QtWidgets import QGraphicsLineItem, QLineEdit, QGraphicsProxyWidget, QGraphicsItem
+from PyQt5.QtGui import QPen, QPainterPath, QBrush, QPainterPathStroker, QPainter, QCursor
+from PyQt5.QtWidgets import QGraphicsItem, QGraphicsPathItem
from PyQt5.QtCore import Qt, QPointF, QRectF
-from PyQt5.uic.properties import QtCore
+class Grabber(QGraphicsPathItem):
+ """
+ Extends QGraphicsPathItem to create grabber for line for moving a particular segment
+ """
+ circle = QPainterPath()
+ circle.addEllipse(QRectF(-5, -5, 10, 10))
+
+ def __init__(self, annotation_line, index, direction):
+ super(Grabber, self).__init__()
+ self.m_index = index
+ self.m_annotation_item = annotation_line
+ self._direction = direction
+ self.setPath(Grabber.circle)
+ # set graphical settings for this item
+ self.setFlag(QGraphicsItem.ItemIsSelectable, True)
+ self.setFlag(QGraphicsItem.ItemIsMovable, True)
+ self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
+ self.setAcceptHoverEvents(True)
+
+ def itemChange(self, change, value):
+ """ move position of grabber after resize"""
+ if change == QGraphicsItem.ItemPositionChange and self.isEnabled():
+ p = QPointF(self.pos())
+ if self._direction == Qt.Horizontal:
+ p.setX(value.x())
+ elif self._direction == Qt.Vertical:
+ p.setY(value.y())
+ movement = p - self.pos()
+ self.m_annotation_item.movePoints(self.m_index, movement)
+ return p
+ return super(Grabber, self).itemChange(change, value)
+
+ def paint(self, painter, option, widget):
+ """paints the path of grabber only if it is selected
+ """
+ if self.isSelected():
+ # show line of grabber
+ self.m_annotation_item.setSelected(True)
+ painter.setBrush(QBrush(Qt.cyan))
+ color = Qt.black if self.isSelected() else Qt.white
+ width = 2 if self.isSelected() else -1
+ painter.setPen(QPen(color, width, Qt.SolidLine))
+ painter.drawPath(self.path())
+
+ # To paint path of shape
+ # color = Qt.red if self.isSelected() else Qt.black
+ # painter.setPen(QPen(Qt.blue, 1, Qt.SolidLine))
+ # painter.drawPath(self.shape())
+
+ def shape(self):
+ """Overrides shape method and set shape to segment on which grabber is located"""
+ index = self.m_index
+ startPoint = QPointF(self.m_annotation_item.path().elementAt(index))
+ endPoint = QPointF(self.m_annotation_item.path().elementAt(index + 1))
+ startPoint = self.mapFromParent(startPoint)
+ endPoint = self.mapFromParent(endPoint)
+ path = QPainterPath(startPoint)
+ path.lineTo(endPoint)
+ # generate outlines for path
+ stroke = QPainterPathStroker()
+ stroke.setWidth(8)
+ return stroke.createStroke(path)
+
+ def boundingRect(self):
+ return self.shape().boundingRect()
+
+ def mousePressEvent(self, event):
+ print('grabber clicked', self)
+ super(Grabber, self).mousePressEvent(event)
+
+ def hoverEnterEvent(self, event):
+ """
+ Changes cursor to horizontal movement or vertical movement
+ depending on the direction of the grabber on mouse enter
+ """
+ if self._direction == Qt.Horizontal:
+ self.setCursor(QCursor(Qt.SplitHCursor))
+ else:
+ self.setCursor(QCursor(Qt.SplitVCursor))
+ super(Grabber, self).hoverEnterEvent(event)
+
+ def hoverLeaveEvent(self, event):
+ """
+ reverts cursor to default on mouse leave
+ """
+ self.setCursor(QCursor(Qt.ArrowCursor))
+ super(Grabber, self).hoverLeaveEvent(event)
+
+
+class Line(QGraphicsPathItem):
+ """
+ Extends QGraphicsPathItem to draw zig-zag line consisting of multiple points
+ """
+ penStyle = Qt.SolidLine
-class Line(QGraphicsItem):
def __init__(self, startPoint, endPoint, **args):
QGraphicsItem.__init__(self, **args)
self.startPoint = startPoint
self.endPoint = endPoint
+ #stores all points of line
+ self.points = []
+ self.points.extend([startPoint, endPoint])
self.startGripItem = None
self.endGripItem = None
self._selected = False
-
+ self.m_grabbers = []
+ # stores current pen style of line
+ self.penStyle = Line.penStyle
+ # set graphical settings for this item
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
- # self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
self.setAcceptHoverEvents(True)
+ # initiates path
+ self.createPath()
- def setStartGripItem(self, item):
- self.startGripItem = item
+ def createPath(self):
+ """
+ creates initial path and stores it's points
+ :return:
+ """
+ offset = 30
+ x0, y0 = self.startPoint.x(), self.startPoint.y()
+ x1, y1 = self.endPoint.x(), self.endPoint.y()
+ self.points = [self.startPoint, QPointF((x0 + x1) / 2, y0), QPointF((x0 + x1) / 2, y1), self.endPoint]
+ if self.startGripItem and self.startGripItem.m_location in ["left", "right"]:
+ if self.endGripItem and self.endGripItem.m_location in ["top", "bottom"]:
+ if self.endGripItem.m_location == "top": offset = -offset
+ self.points = [self.startPoint, QPointF((x0 + x1) / 2, y0), QPointF((x0 + x1) / 2, y1 + offset),
+ QPointF(self.endPoint.x(), y1 + offset), self.endPoint]
- def setEndGripItem(self, item):
- self.endGripItem = item
+ if self.startGripItem and self.startGripItem.m_location in ["top", "bottom"]:
+ self.points = [self.startPoint, QPointF(x0, (y0 + y1) / 2), QPointF(x1, (y0 + y1) / 2), self.endPoint]
+ if self.endGripItem and self.endGripItem.m_location in ["left", "right"]:
+ self.points = [self.startPoint, QPointF(x0, (y0 + y1) / 2), QPointF(x1 - offset, (y0 + y1) / 2),
+ QPointF(x1 - offset, self.endPoint.y()), self.endPoint]
+ # draw line
+ path = QPainterPath(self.startPoint)
+ for i in range(1, len(self.points)):
+ path.lineTo(self.points[i])
+ self.setPath(path)
+ if self.endGripItem:
+ self.addGrabber()
+
+ def updatePath(self):
+ """ update path when svg item moves
+ """
+ path = QPainterPath(self.startPoint)
+ self.updatePoints()
+
+ for i in range(1, len(self.points) - 1):
+ path.lineTo(self.points[i])
+ path.lineTo(self.endPoint)
+ self.setPath(path)
+
+ def updatePoints(self):
+ """
+ updates points of line when grabber is moved
+ :return:
+ """
+ if self.startGripItem.m_location in ["left", "right"]:
+ point = self.points[1]
+ self.points[1] = QPointF(point.x(), self.startPoint.y())
+ if self.endGripItem.m_location in ["left", "right"]:
+ point = self.points[len(self.points) - 2]
+ self.points[len(self.points) - 2] = QPointF(point.x(), self.endPoint.y())
+ else:
+ point = self.points[len(self.points) - 2]
+ self.points[len(self.points) - 2] = QPointF(self.endPoint.x(), point.y())
+
+ else:
+ point = self.points[1]
+ self.points[1] = QPointF(self.startPoint.x(), point.y())
+ if self.endGripItem.m_location in ["left", "right"]:
+ point = self.points[len(self.points) - 2]
+ self.points[len(self.points) - 2] = QPointF(point.x(), self.endPoint.y())
+ else:
+ point = self.points[len(self.points) - 2]
+ self.points[len(self.points) - 2] = QPointF(self.endPoint.x(), point.y())
def shape(self):
- x0, y0 = self.startPoint.x(), self.startPoint.y()
- x1, y1 = self.endPoint.x(), self.endPoint.y()
- path = QPainterPath(QPointF(x0, y0))
- path.lineTo((x0 + x1) / 2, y0)
- path.moveTo((x0 + x1) / 2, y0)
- path.lineTo((x0 + x1) / 2, y1)
- path.moveTo((x0 + x1) / 2, y1)
- path.lineTo(x1, y1)
+ """generates outline for path
+ """
+ qp = QPainterPathStroker()
+ qp.setWidth(8)
+ path = qp.createStroke(self.path())
return path
- def boundingRect(self):
- x0, y0 = self.startPoint.x(), self.startPoint.y()
- x1, y1 = self.endPoint.x(), self.endPoint.y()
- return QRectF(min(x0, x1), min(y0, y1), abs(x1 - x0), abs(y1 - y0))
-
- def paint(self, painter, style, widget=None):
- # x0, y0 = self.startPoint.x(), self.startPoint.y()
- # x1, y1 = self.endPoint.x(), self.endPoint.y()
- # painter.drawLine(x0, y0, (x0 + x1) / 2, y0)
- # painter.drawLine((x0 + x1) / 2, y0, (x0 + x1) / 2, y1)
- # painter.drawLine((x0 + x1) / 2, y1, x1, y1)
- painter.drawPath(self.shape())
+ def paint(self, painter, option, widget):
+ color = Qt.red if self.isSelected() else Qt.black
+ painter.setPen(QPen(color, 2, self.penStyle))
+ painter.drawPath(self.path())
+ # To paint path of shape
+ # painter.setPen(QPen(Qt.blue, 1, Qt.SolidLine))
+ # painter.drawPath(self.shape())
if self.isSelected():
self.showGripItem()
- self._selected= True
- pen = QPen(QBrush(Qt.red), 5)
- painter.setPen(pen)
- painter.drawPath(self.shape())
+ self._selected = True
elif self._selected:
self.hideGripItem()
self._selected = False
+ def movePoints(self, index, movement):
+ """move points of line
+ """
+ for i in [index, index + 1]:
+ point = self.points[i]
+ point += movement
+ self.points[i] = point
+ self.updatePath()
+ self.updateGrabber([index])
+
+ def addGrabber(self):
+ """adds grabber when line is moved
+ """
+ if self.startGripItem.m_location in ["left", "right"]:
+ direction = [Qt.Horizontal, Qt.Vertical]
+ else:
+ direction = [Qt.Vertical, Qt.Horizontal]
+ for i in range(1, len(self.points) - 2):
+ item = Grabber(self, i, direction[i - 1])
+ item.setParentItem(self)
+ item.setPos(self.pos())
+ self.scene().addItem(item)
+ self.m_grabbers.append(item)
+
+ def updateGrabber(self, index_no_updates=None):
+ """updates all grabber of line when it is moved
+ """
+ index_no_updates = index_no_updates or []
+ for grabber in self.m_grabbers:
+ if grabber.m_index in index_no_updates: continue
+ index = grabber.m_index
+ startPoint = self.points[index]
+ endPoint = self.points[index + 1]
+ pos = (startPoint + endPoint) / 2
+ grabber.setEnabled(False)
+ grabber.setPos(pos)
+ grabber.setEnabled(True)
+
+ def itemChange(self, change, value):
+ if change == QGraphicsItem.ItemSceneHasChanged and self.scene():
+ # self.addGrabber()
+ # self.updateGrabber()
+ return
+ return super(Line, self).itemChange(change, value)
+
def updateLine(self, startPoint=None, endPoint=None):
"""This function is used to update connecting line when it add on
@@ -65,17 +256,21 @@ class Line(QGraphicsItem):
:return:
"""
self.prepareGeometryChange()
- if self.startGripItem and self.endGripItem:
- item = self.startGripItem
- self.startPoint = item.parentItem().mapToScene(item.pos())
- item = self.endGripItem
- self.endPoint = item.parentItem().mapToScene(item.pos())
if startPoint:
self.startPoint = startPoint
if endPoint:
self.endPoint = endPoint
+ self.createPath()
+ self.updateGrabber()
+ return
- self.update(self.boundingRect())
+ if self.startGripItem and self.endGripItem:
+ item = self.startGripItem
+ self.startPoint = item.parentItem().mapToScene(item.pos())
+ item = self.endGripItem
+ self.endPoint = item.parentItem().mapToScene(item.pos())
+ self.updatePath()
+ self.updateGrabber()
def removeFromCanvas(self):
"""This function is used to remove connecting line from canvas
@@ -84,20 +279,26 @@ class Line(QGraphicsItem):
if self.scene():
self.scene().removeItem(self)
- def mousePressEvent(self, event):
- print('line clicked', self)
- super(Line, self).mousePressEvent(event)
-
def showGripItem(self):
+ """hides grip items which contains line
+ """
if self.startGripItem: self.startGripItem.show()
if self.endGripItem: self.endGripItem.show()
- # if self.startGripItem: self.startGripItem.setVisible(True)
- # if self.endGripItem: self.endGripItem.setVisible(True)
+ # for grabber in self.m_grabber:
+ # grabber.setSelected(True)
def hideGripItem(self):
- if self.startGripItem : self.startGripItem.hide()
+ """hides grip items which contains line
+ """
+ if self.startGripItem: self.startGripItem.hide()
if self.endGripItem: self.endGripItem.hide()
- # if self.startGripItem: self.startGripItem.setVisible(False)
- # if self.endGripItem: self.endGripItem.setVisible(False)
+ def setStartGripItem(self, item):
+ self.startGripItem = item
+
+ def setEndGripItem(self, item):
+ self.endGripItem = item
+ def setPenStyle(self, style):
+ """change current pen style for line"""
+ self.penStyle = style
diff --git a/src/main/python/shapes/shapes.py b/src/main/python/shapes/shapes.py
index 482088f..982e882 100644
--- a/src/main/python/shapes/shapes.py
+++ b/src/main/python/shapes/shapes.py
@@ -1,38 +1,29 @@
-import random
-
-from PyQt5 import QtCore
+from PyQt5 import QtCore, QtWidgets
from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer
from PyQt5.QtWidgets import QLineEdit, QGraphicsItem, QGraphicsEllipseItem, QGraphicsProxyWidget, QGraphicsPathItem, \
QGraphicsSceneHoverEvent, QGraphicsColorizeEffect
from PyQt5.QtGui import QPen, QColor, QFont, QCursor, QPainterPath, QPainter, QDrag, QBrush, QImage
from PyQt5.QtCore import Qt, QRectF, QPointF, QSizeF, QEvent, QMimeData, QFile, QIODevice, QRect
-from PyQt5.QtXml import QDomDocument
-from PyQt5.uic.properties import QtGui, QtWidgets
-from xml.dom import minidom
-import re
from line import Line
-from svghandler import SvgHandler
class GripItem(QGraphicsPathItem):
"""
- Extends PyQt5's QGraphicsPathItem to create the general structure of the Grabbable points for resizing shapes.
- Takes two parameters, reference item (On which the grip items are to appear) and the grip index
+ Extends QGraphicsPathItem to create the structure of the Grabbable points for resizing shapes and connecting lines.
+ Takes two parameters, reference item (On which the grip items are to appear) and the path of the item
"""
+
def __init__(self, annotation_item, path, parent=None):
"""
Extends PyQt5's QGraphicsPathItem to create the general structure of the Grabbable points for resizing shapes.
"""
QGraphicsPathItem.__init__(self, parent)
self.m_annotation_item = annotation_item
- # self.m_index = index
-
+ # set path of item
self.setPath(path)
-
self.setAcceptHoverEvents(True)
self.setCursor(QCursor(Qt.PointingHandCursor))
- # self.setVisible(False)
# def hoverEnterEvent(self, event):
# """
@@ -57,15 +48,6 @@ class GripItem(QGraphicsPathItem):
self.setSelected(False)
super(GripItem, self).mouseReleaseEvent(event)
- # def itemChange(self, change, value):
- # """
- # Calls movepoint from reference item, with the index of this grip item
- # """
- # if change == QGraphicsItem.ItemPositionChange and self.isEnabled():
- # self.m_annotation_item.movePoint(self.m_index, value)
- # return super(GripItem, self).itemChange(change, value)
-
-
class SizeGripItem(GripItem):
"""
Extends grip items for vertical and horizontal directions, with hover events and directional changes
@@ -96,7 +78,9 @@ class SizeGripItem(GripItem):
"""
return self._direction
- def update_path(self):
+ def updatePath(self):
+ """updates path of size grip item
+ """
if self._direction is Qt.Horizontal:
self.height = self.m_annotation_item.boundingRect().height()
else:
@@ -105,10 +89,10 @@ class SizeGripItem(GripItem):
path.addRect(QRectF(0, 0, self.width, self.height))
self.setPath(path)
- def update_position(self):
- """updates grip items
+ def updatePosition(self):
+ """updates position of grip items
"""
- self.update_path()
+ self.updatePath()
pos = self.m_annotation_item.mapToScene(self.point(self.m_index))
x = self.m_annotation_item.boundingRect().x()
y = self.m_annotation_item.boundingRect().y()
@@ -136,9 +120,8 @@ class SizeGripItem(GripItem):
"""
Changes cursor to horizontal resize or vertical resize depending on the direction of the grip item on mouse enter
"""
- self.setPen(QPen(QColor("black"), 2))
- self.setBrush(QColor("red"))
- # self.setVisible(True)
+ # self.setPen(QPen(QColor("black"), 2))
+ # self.setBrush(QColor("red"))
if self._direction == Qt.Horizontal:
self.setCursor(QCursor(Qt.SizeHorCursor))
else:
@@ -149,17 +132,15 @@ class SizeGripItem(GripItem):
"""
reverts cursor to default on mouse leave
"""
- # self.setVisible(False)
- self.setPen(QPen(Qt.transparent))
- self.setBrush(Qt.transparent)
+ # self.setPen(QPen(Qt.transparent))
+ # self.setBrush(Qt.transparent)
self.setCursor(QCursor(Qt.ArrowCursor))
super(SizeGripItem, self).hoverLeaveEvent(event)
def itemChange(self, change, value):
"""
- Moves position of grip item on resize or reference circle's position change
+ Moves position of grip item on resize
"""
-
if change == QGraphicsItem.ItemPositionChange and self.isEnabled():
p = QPointF(self.pos())
if self.direction == Qt.Horizontal:
@@ -171,34 +152,34 @@ class SizeGripItem(GripItem):
return super(SizeGripItem, self).itemChange(change, value)
def removeFromCanvas(self):
+ # used to remove grip item from scene
if self.scene():
self.scene().removeItem(self)
class LineGripItem(GripItem):
+ """Extends grip items for connecting lines , with hover events and mouse events
+ """
circle = QPainterPath()
circle.addEllipse(QRectF(-10, -10, 20, 20))
- def __init__(self, annotation_item, index, parent=None):
- """
- Extends grip items for connecting lines , with hover events and mouse events
- """
+
+ def __init__(self, annotation_item, index, location, parent=None):
self.path = LineGripItem.circle
- super(LineGripItem, self).__init__(annotation_item,path=self.path, parent=parent)
+ super(LineGripItem, self).__init__(annotation_item, path=self.path, parent=parent)
self.m_index = index
+ self.m_location = location
self.connectedLines = []
+ # stores current line which is in process
self.tempLine = None
+ # keep previous hovered item when line drawing in process
self.previousHoveredItem = None
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
- # self.setZValue(2)
self.setPen(QPen(QColor("black"), -1))
- # self.setBrush(QColor("red"))
- # self.setVisible(False)
def point(self, index):
"""
yields a list of positions of grip items in a node item
"""
- radiusOfGripItem = self.boundingRect().width() / 2
x = self.m_annotation_item.boundingRect().width()
y = self.m_annotation_item.boundingRect().height()
if 0 <= index < 4:
@@ -209,8 +190,7 @@ class LineGripItem(GripItem):
QPointF(x, y / 2)
][index]
- def update_position(self):
- # print('updating grip item ', self.m_index)
+ def updatePosition(self):
pos = self.point(self.m_index)
x = self.m_annotation_item.boundingRect().x()
y = self.m_annotation_item.boundingRect().y()
@@ -223,133 +203,122 @@ class LineGripItem(GripItem):
line.updateLine()
def mousePressEvent(self, mouseEvent):
+ """Handle all mouse press for this item
+ """
if mouseEvent.button() != Qt.LeftButton:
return
- radiusOfGripItem = self.boundingRect().width() / 2
+ # initialize a line and add on scene
startPoint = endPoint = self.parentItem().mapToScene(self.pos())
-
self.tempLine = Line(startPoint, endPoint)
+ self.tempLine.setStartGripItem(self)
self.scene().addItem(self.tempLine)
super().mousePressEvent(mouseEvent)
def mouseMoveEvent(self, mouseEvent):
+ """Handle all mouse move for this item
+ """
+ #if line get started then update it's end point
if self.tempLine:
endPoint = mouseEvent.scenePos()
self.tempLine.updateLine(endPoint=endPoint)
- super().mouseMoveEvent(mouseEvent)
+
item = self.scene().itemAt(mouseEvent.scenePos().x(), mouseEvent.scenePos().y(),
self.m_annotation_item.transform())
- # print(self.m_annotation_item.transform())
if self.previousHoveredItem and item != self.previousHoveredItem and \
item not in self.previousHoveredItem.lineGripItems:
self.previousHoveredItem.hideGripItem()
+ super().mouseMoveEvent(mouseEvent)
if type(item) == NodeItem:
self.previousHoveredItem = item
item.showGripItem()
def mouseReleaseEvent(self, mouseEvent):
+ """Handle all mouse release for this item"""
+ super().mouseReleaseEvent(mouseEvent)
+ # set final position of line
if self.tempLine:
item = self.scene().itemAt(mouseEvent.scenePos().x(), mouseEvent.scenePos().y(),
self.transform())
- if type(item) == LineGripItem:
+
+ if type(item) == LineGripItem and item != self:
+ self.tempLine.setEndGripItem(item)
endPoint = item.parentItem().mapToScene(item.pos())
self.tempLine.updateLine(endPoint=endPoint)
self.connectedLines.append(self.tempLine)
item.connectedLines.append(self.tempLine)
- self.tempLine.setStartGripItem(self)
- self.tempLine.setEndGripItem(item)
- else:
+
+
+ elif self.tempLine and self.scene():
self.scene().removeItem(self.tempLine)
- super().mouseReleaseEvent(mouseEvent)
self.tempLine = None
self.previousHoveredItem = None
def removeConnectedLines(self):
+ """removes all connected line to grip"""
for line in self.connectedLines:
line.removeFromCanvas()
def show(self):
+ """ shows line grip item
+ """
self.setPen(QPen(QColor("black"), 2))
self.setBrush(QColor("red"))
def hide(self):
+ """ hides line grip item
+ """
if (self.parentItem().isSelected() or self.isSelected()) is False:
self.setPen(QPen(Qt.transparent))
self.setBrush(Qt.transparent)
class NodeItem(QGraphicsSvgItem):
+ """
+ Extends PyQt5's QGraphicsSvgItem to create the basic structure of shapes with given unit operation type
+ """
- def __init__(self, unitOpType, parent=None):
+ def __init__(self, unitOperationType, parent=None):
QGraphicsSvgItem.__init__(self, parent)
- self.m_renderer = QSvgRenderer()
- self.type = unitOpType
-
-
- self.file = QFile("svg/" + "Boiler" + ".svg")
- if not self.file.open(QIODevice.ReadOnly):
- print("Cannot open the file")
- exit(-1)
- self.svghandler = SvgHandler(self.file)
- self.updateRenderer()
- self.rect = QRectF(0,0,300,400)
- # self.rect=QRectF(0,0,self.m_renderer.defaultSize().width(),self.m_renderer.defaultSize().height())
- # self.rect = self.m_renderer.viewBoxF()
- # self.changeColour("red")
- # self.changeStrokeWidth(4)
-
- self.setZValue(2)
- self.setAcceptHoverEvents(True)
- self.setAcceptDrops(True)
+ self.m_type = unitOperationType
+ self.m_renderer = QSvgRenderer("svg/" + unitOperationType + ".svg")
+ self.setSharedRenderer(self.m_renderer)
+ # set initial size of item
+ self.width = 100
+ self.height = 150
+ self.rect = QRectF(-self.width / 2, -self.height / 2, self.width, self.height)
+ # set graphical settings for this item
self.setFlags(QGraphicsSvgItem.ItemIsMovable |
QGraphicsSvgItem.ItemIsSelectable |
QGraphicsSvgItem.ItemSendsGeometryChanges)
-
+ self.setAcceptHoverEvents(True)
+ self.setZValue(2)
+ # grip items connected to this item
self.lineGripItems = []
self.sizeGripItems = []
- self.ds= 0.70875001
- self.cs = 0.70875001
- self.dw=self.m_renderer.defaultSize().width()
- self.dh=self.m_renderer.defaultSize().height()
- self.vdw = self.m_renderer.viewBoxF().width()
- self.vdh=self.m_renderer.viewBoxF().height()
-
-
- def changeStrokeWidth(self,value):
- self.svghandler.setStrokeWidth(value)
- self.updateRenderer()
-
- def changeColour(self,value):
- self.svghandler.setColor(value)
- self.updateRenderer()
- """graphics effect can also used to change colour of an svg image
- """
- # self._effect = QGraphicsColorizeEffect()
- # self._effect.setColor(Qt.red)
- # self._effect.setStrength(1)
- # self.setGraphicsEffect(self._effect)
-
- def updateRenderer(self):
- byteArray = self.svghandler.doc.toByteArray()
- self.m_renderer.load(byteArray)
- self.setSharedRenderer(self.m_renderer)
def boundingRect(self):
+ """Overrides QGraphicsSvgItem's boundingRect() virtual public function and
+ returns a valid bounding
+ """
return self.rect
def paint(self, painter, option, widget):
+ """
+ Paints the contents of an item in local coordinates.
+ :param painter: QPainter instance
+ :param option: QStyleOptionGraphicsItem instance
+ :param widget: QWidget instance
+ """
if not self.m_renderer:
QGraphicsSvgItem.paint(self, painter, option, widget)
self.m_renderer.render(painter, self.boundingRect())
-
- def update_rect(self):
- """Update rect of node item
- """
- self.prepareGeometryChange()
- self.update(self.rect)
+ if self.isSelected():
+ self.showGripItem()
+ else:
+ self.hideGripItem()
def resize(self, i, p):
"""Move grip item with changing rect of node item
@@ -368,22 +337,26 @@ class NodeItem(QGraphicsSvgItem):
if i == 2 or i == 3:
self.rect = QRectF(x, y, width + p.x() - p_new.x(), height + p.y() - p_new.y())
- self.cs = self.ds*(self.boundingRect().width()/self.vdw)
- # self.cs =self.s
- print(self.boundingRect())
- print(self.cs)
- offset = (self.cs-self.ds)
- offset = offset*(self.vdw/self.boundingRect().width())
- # self.m_renderer.setViewBox(QRectF(offset/2.0,0,self.vdw-offset,self.vdh))
-
- # self.update_rect()
self.updateSizeGripItem([i])
self.updateLineGripItem()
def addGripItem(self):
"""adds grip items
"""
- if self.scene() and not self.lineGripItems:
+ if self.scene():
+ # add grip items for connecting lines
+ for i, (location) in enumerate(
+ (
+ "top",
+ "left",
+ "bottom",
+ "right"
+ )
+ ):
+ item = LineGripItem(self, i, location, parent=self)
+ self.scene().addItem(item)
+ self.lineGripItems.append(item)
+ # add grip for resize it
for i, (direction) in enumerate(
(
Qt.Vertical,
@@ -392,29 +365,29 @@ class NodeItem(QGraphicsSvgItem):
Qt.Horizontal,
)
):
- item = LineGripItem(self, i)
- item.setParentItem(self)
- self.scene().addItem(item)
- self.lineGripItems.append(item)
item = SizeGripItem(self, i, direction)
self.scene().addItem(item)
self.sizeGripItems.append(item)
def updateLineGripItem(self, index_no_updates=None):
+ """
+ updates line grip items
+ """
# index_no_updates = index_no_updates or []
for item in self.lineGripItems:
- item.update_position()
+ item.updatePosition()
def updateSizeGripItem(self, index_no_updates=None):
- """updates grip items
+ """
+ updates size grip items
"""
index_no_updates = index_no_updates or []
for i, item in zip(range(len(self.sizeGripItems)), self.sizeGripItems):
if i not in index_no_updates:
- item.update_position()
+ item.updatePosition()
def itemChange(self, change, value):
- """Overloads and extends QGraphicsSvgItem to also update gripitem
+ """Overloads and extends QGraphicsSvgItem to also update grip items
"""
if change == QGraphicsItem.ItemPositionHasChanged:
self.updateLineGripItem()
@@ -427,12 +400,6 @@ class NodeItem(QGraphicsSvgItem):
return
return super(NodeItem, self).itemChange(change, value)
- def addOnCanvas(self, scene):
- """This function is used to add Node Item on canvas
- :return:
- """
- scene.addItem(self)
-
def removeFromCanvas(self):
"""This function is used to remove item from canvas
:return:
@@ -452,19 +419,21 @@ class NodeItem(QGraphicsSvgItem):
def hoverLeaveEvent(self, event):
"""defines shape highlighting on Mouse Leave
"""
- # if self.isSelected() is False:
self.hideGripItem()
super(NodeItem, self).hoverLeaveEvent(event)
def showGripItem(self):
+ """shows grip items of svg item
+ """
for item in self.lineGripItems:
item.setPen(QPen(QColor("black"), 2))
item.setBrush(QColor("red"))
for item in self.sizeGripItems:
item.setPen(QPen(QColor("black"), 2))
- # item.setBrush(QColor("red"))
def hideGripItem(self):
+ """hide grip items of svg item
+ """
for item in self.lineGripItems:
if item.isSelected() is False:
item.setPen(QPen(Qt.transparent))
@@ -472,4 +441,3 @@ class NodeItem(QGraphicsSvgItem):
for item in self.sizeGripItems:
item.setPen(QPen(Qt.transparent))
item.setBrush(Qt.transparent)
-