summaryrefslogtreecommitdiff
path: root/src/main/python
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/python')
-rw-r--r--src/main/python/shapes/line.py463
-rw-r--r--src/main/python/shapes/shapes.py21
-rw-r--r--src/main/python/utils/graphics.py7
-rw-r--r--src/main/python/utils/undo.py1
4 files changed, 439 insertions, 53 deletions
diff --git a/src/main/python/shapes/line.py b/src/main/python/shapes/line.py
index 310b496..1a54284 100644
--- a/src/main/python/shapes/line.py
+++ b/src/main/python/shapes/line.py
@@ -1,7 +1,9 @@
+import math
from PyQt5.QtGui import QPen, QPainterPath, QBrush, QPainterPathStroker, QPainter, QCursor
-from PyQt5.QtWidgets import QGraphicsItem, QGraphicsPathItem
+from PyQt5.QtWidgets import QGraphicsItem, QGraphicsPathItem, QGraphicsTextItem
from PyQt5.QtCore import Qt, QPointF, QRectF
+
class Grabber(QGraphicsPathItem):
"""
Extends QGraphicsPathItem to create grabber for line for moving a particular segment
@@ -20,6 +22,8 @@ class Grabber(QGraphicsPathItem):
self.setFlag(QGraphicsItem.ItemIsMovable, True)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
self.setAcceptHoverEvents(True)
+ self.pen = QPen(Qt.white, -1, Qt.SolidLine)
+ self.brush = QBrush(Qt.transparent)
def itemChange(self, change, value):
""" move position of grabber after resize"""
@@ -37,13 +41,11 @@ class Grabber(QGraphicsPathItem):
def paint(self, painter, option, widget):
"""paints the path of grabber only if it is selected
"""
- if self.isSelected():
- # show line of grabber
+ if self.isSelected() and not self.m_annotation_item.isSelected() :
+ # show parent 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.setBrush(self.brush)
+ painter.setPen(self.pen)
painter.drawPath(self.path())
# To paint path of shape
@@ -69,7 +71,6 @@ class Grabber(QGraphicsPathItem):
return self.shape().boundingRect()
def mousePressEvent(self, event):
- print('grabber clicked', self)
super(Grabber, self).mousePressEvent(event)
def hoverEnterEvent(self, event):
@@ -90,6 +91,57 @@ class Grabber(QGraphicsPathItem):
self.setCursor(QCursor(Qt.ArrowCursor))
super(Grabber, self).hoverLeaveEvent(event)
+ def show(self):
+ self.pen = QPen(Qt.black, 2, Qt.SolidLine)
+ self.brush = QBrush(Qt.cyan)
+
+ def hide(self):
+ self.pen = QPen(Qt.white, -1, Qt.SolidLine)
+ self.brush = QBrush(Qt.transparent)
+
+class LineLabel(QGraphicsTextItem):
+ def __init__(self, parent=None):
+ super(LineLabel, self).__init__(parent=parent)
+ self.setTextInteractionFlags(Qt.TextEditorInteraction)
+ self.setFlags(QGraphicsItem.ItemIsMovable |
+ QGraphicsItem.ItemIsSelectable |
+ QGraphicsItem.ItemIsFocusable)
+ self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
+ self.setPlainText("abc")
+
+ def paint(self, painter, option, widget):
+ super(LineLabel, self).paint(painter,option,widget)
+ painter.drawEllipse(self.boundingRect())
+
+ def updateLabel(self):
+ points = self.parentItem().points
+ # min_A = QPointF()
+ # min_B =QPointF()
+ # min_dis =math.inf
+ # for i in range(1, len(points)):
+ # A = points[i - 1]
+ # B = points[i]
+ # C = self.pos()
+ # BAx = B.x() - A.x()
+ # BAy = B.y() - A.y()
+ # CAx = C.x() - A.x()
+ # CAy = C.y() - A.y()
+ # length = math.sqrt(BAx * BAx + BAy * BAy)
+ # if length >0:
+ # dis = (BAx*CAy - CAx*BAy)/length
+ # if abs(dis) < abs(min_dis):
+ # min_dis=dis
+ # min_A=A
+ # min_B=B
+ #
+ # self.setPos(self.parentItem().mapFromScene(QPointF(min_A)))
+ # print(self.pos())
+
+ def itemChange(self, change, value):
+ # if change == QGraphicsItem.ItemPositionChange:
+ # print("label change", change, value)
+ return super(LineLabel, self).itemChange(change,value)
+
class Line(QGraphicsPathItem):
"""
@@ -116,6 +168,73 @@ class Line(QGraphicsPathItem):
self.setAcceptHoverEvents(True)
# initiates path
self.createPath()
+ self.commonPaths=[]
+ self.label = LineLabel(self)
+
+ def advance(self, phase):
+ # items = self.collidingItems(Qt.IntersectsItemShape)
+ items = self.scene().items(self.shape(),Qt.IntersectsItemShape,Qt.AscendingOrder)
+ self.commonPaths = []
+ for item in items:
+ if type(item) in [type(self)]:
+ if item == self:
+ break
+ shape = item.shape()
+ shape = self.mapFromItem(item, item.shape())
+ commonPath = self.shape().intersected(shape)
+ polygons = commonPath.toSubpathPolygons()
+ for polygon in polygons:
+ center = polygon.boundingRect().center()
+ self.commonPaths.append(center)
+
+ # self.commonPaths[commonPath] = item
+ print(self.commonPaths)
+ self.update()
+
+ def paint(self, painter, option, widget):
+ color = Qt.red if self.isSelected() else Qt.black
+ painter.setPen(QPen(color, 2, self.penStyle))
+ # path = self.path()
+ # painter.drawPath(path)
+ path = QPainterPath(self.startPoint)
+ # iterating over all points of line
+ for i in range(len(self.points) - 1):
+ # for point in self.commonPaths:
+ # # point is center of common path
+ # pass
+ for point in self.commonPaths:
+ x1, y1 = self.points[i].x(), self.points[i].y()
+ x2, y2 = self.points[i+1].x(), self.points[i+1].y()
+ x, y = point.x(), point.y()
+ # if not x1 * (y - y2) + x * (y2 - y1) + x2 * (y1 - y) :
+ if x == x1 == x2:
+ #vertical
+ if min(y1, y2) <= y < max(y1, y2):
+ if y2>y1:
+ path.lineTo(point - QPointF(0, 8))
+ path.arcTo(QRectF(x-8, y-8, 16, 16), 90, -180)
+ path.moveTo(point + QPointF(0, 8))
+ else:
+ path.lineTo(point + QPointF(0, 8))
+ path.arcTo(QRectF(x-8, y-8, 16, 16), -90, 180)
+ path.moveTo(point - QPointF(0, 8))
+ elif y == y1 == y2:
+ #horizontal
+ if min(x1, x2) <= x < max(x1, x2):
+ if x2>x1:
+ path.lineTo(point - QPointF(8, 0))
+ path.arcTo(QRectF(x-8, y-8, 16, 16), 180, 180)
+ path.moveTo(point + QPointF(8, 0))
+ else:
+ path.lineTo(point + QPointF(8, 0))
+ path.arcTo(QRectF(x-8, y-8, 16, 16), 0, -180)
+ path.lineTo(point - QPointF(8, 0))
+ path.lineTo(self.points[i+1])
+
+ painter.drawPath(path)
+
+
+
def createPath(self):
"""
@@ -125,19 +244,285 @@ class Line(QGraphicsPathItem):
offset = 30
x0, y0 = self.startPoint.x(), self.startPoint.y()
x1, y1 = self.endPoint.x(), self.endPoint.y()
+ # create line is in process
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]
-
- 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
+ # final path of line
+ if self.startGripItem and self.endGripItem:
+ # determine ns (point next to start)
+ item = self.startGripItem
+ self.startPoint = item.parentItem().mapToScene(item.pos())
+ if item.m_location == "top":
+ ns = QPointF(self.startPoint.x(), self.startPoint.y() - offset)
+ elif item.m_location == "left":
+ ns = QPointF(self.startPoint.x() - offset, self.startPoint.y())
+ elif item.m_location == "bottom":
+ ns = QPointF(self.startPoint.x(), self.startPoint.y() + offset)
+ else:
+ ns = QPointF(self.startPoint.x() + offset, self.startPoint.y())
+ # pe (point previous to end)
+ item = self.endGripItem
+ self.endPoint = item.parentItem().mapToScene(item.pos())
+ if item.m_location == "top":
+ pe = QPointF(self.endPoint.x(), self.endPoint.y() - offset)
+ elif item.m_location == "left":
+ pe = QPointF(self.endPoint.x() - offset, self.endPoint.y())
+ elif item.m_location == "bottom":
+ pe = QPointF(self.endPoint.x(), self.endPoint.y() + offset)
+ else:
+ pe = QPointF(self.endPoint.x() + offset, self.endPoint.y())
+
+ start = self.startPoint
+ end = self.endPoint
+ sheight = self.startGripItem.m_annotation_item.boundingRect().height() / 2
+ swidth = self.startGripItem.m_annotation_item.boundingRect().width() / 2
+ eheight = self.endGripItem.m_annotation_item.boundingRect().height() / 2
+ ewidth = self.endGripItem.m_annotation_item.boundingRect().width() / 2
+
+ if self.startGripItem.m_location in ["right"]:
+ if self.endGripItem.m_location in ["top"]:
+ if start.x() + offset < end.x() - ewidth:
+ if start.y() + offset < end.y():
+ self.points = [start, QPointF(end.x(), start.y()), end]
+ else:
+ self.points = [start, ns, QPointF(ns.x(), pe.y()), pe, end]
+ elif start.x() - 2 * swidth > end.x():
+ if start.y() + sheight + offset < end.y():
+ self.points = [start, ns, QPointF(ns.x(), pe.y()), pe, end]
+ elif start.y() - sheight - offset < end.y():
+ self.points = [start, ns, QPointF(ns.x(), ns.y() - sheight - offset),
+ QPointF(pe.x(), ns.y() - sheight - offset), end]
+ else:
+ self.points = [start, ns, QPointF(ns.x(), pe.y()), pe, end]
+ else:
+ self.points = [start, ns, QPointF(ns.x(), pe.y()), pe, end]
+ if start.y() > end.y():
+ x = max(end.x() + ewidth + offset, ns.x())
+ self.points = [start, QPointF(x, start.y()), QPointF(x, pe.y()), pe, end]
+
+ elif self.endGripItem.m_location in ["bottom"]:
+ if start.x() + offset < end.x() - ewidth:
+ if start.y() + offset < end.y():
+ self.points = [start, ns, QPointF(ns.x(), pe.y()), pe, end]
+ else:
+ self.points = [start, QPointF(end.x(), start.y()), end]
+
+ elif start.x() - 2 * swidth > end.x():
+ if start.y() + sheight + offset < end.y():
+ self.points = [start, ns, QPointF(ns.x(), pe.y()), pe, end]
+ elif start.y() - sheight - offset < end.y():
+ y = max(pe.y(), start.y() + sheight + offset)
+ self.points = [start, ns, QPointF(ns.x(), y),
+ QPointF(pe.x(), y), end]
+ else:
+ self.points = [start, ns, QPointF(ns.x(), pe.y()), pe, end]
+ else:
+ self.points = [start, ns, QPointF(ns.x(), pe.y()), pe, end]
+ if start.y() < end.y():
+ x = max(end.x() + ewidth + offset, ns.x())
+ self.points = [start, QPointF(x, start.y()), QPointF(x, pe.y()), pe, end]
+
+ elif self.endGripItem.m_location in ["right"]:
+ x = max(start.x() + offset, pe.x())
+ self.points = [start, QPointF(x, start.y()), QPointF(x, end.y()), end]
+ if start.x() + offset < end.x() - ewidth:
+ if start.y() + offset > end.y() - eheight and end.y() >= start.y():
+ self.points = [start, ns, QPointF(ns.x(), pe.y() - eheight),
+ QPointF(pe.x(), pe.y() - eheight), pe, end]
+ elif start.y() - offset < end.y() + eheight and end.y() <= start.y():
+ self.points = [start, ns, QPointF(ns.x(), pe.y() + eheight),
+ QPointF(pe.x(), pe.y() + eheight), pe, end]
+ elif start.y() - sheight - offset < end.y() < start.y() + sheight + offset:
+ if end.y() < start.y():
+ self.points = [start, ns, QPointF(ns.x(), ns.y() - sheight),
+ QPointF(pe.x(), ns.y() - sheight), pe, end]
+ else:
+ self.points = [start, ns, QPointF(ns.x(), ns.y() + sheight),
+ QPointF(pe.x(), ns.y() + sheight), pe, end]
+
+ elif self.endGripItem.m_location in ["left"]:
+ self.points = [start, QPointF((start.x() + end.x()) / 2, start.y()),
+ QPointF((start.x() + end.x()) / 2, end.y()), end]
+ if end.x() < start.x() + offset:
+ if end.y() + eheight <= start.y() - sheight - offset:
+ self.points = [start, ns, QPointF(ns.x(), ns.y() - sheight),
+ QPointF(pe.x(), ns.y() - sheight), pe, end]
+ elif end.y() - eheight >= start.y() + sheight + offset:
+ self.points = [start, ns, QPointF(ns.x(), ns.y() + sheight),
+ QPointF(pe.x(), ns.y() + sheight), pe, end]
+ elif end.y() <= start.y():
+ y = min(end.y() - eheight, start.y() - sheight)
+ self.points = [start, ns, QPointF(ns.x(), y),
+ QPointF(pe.x(), y), pe, end]
+ else:
+ y = max(end.y() + eheight, start.y() + sheight)
+ self.points = [start, ns, QPointF(ns.x(), y),
+ QPointF(pe.x(), y), pe, end]
+
+
+ elif self.startGripItem.m_location in ["left"]:
+ if self.endGripItem.m_location in ["top"]:
+ if start.x() + offset < end.x() - ewidth:
+ if end.y() > start.y() + sheight + offset:
+ self.points = [start, ns, QPointF(ns.x(), ns.y() + sheight),
+ QPointF(pe.x(), ns.y() + sheight), end]
+ else:
+ y = min(start.y() - sheight, pe.y())
+ self.points = [start, ns, QPointF(ns.x(), y),
+ QPointF(pe.x(), y), end]
+ elif end.x() + ewidth >= start.x() - offset:
+ x = min(ns.x(), end.x() - ewidth)
+ self.points = [start, QPointF(x, ns.y()),
+ QPointF(x, pe.y()), pe, end]
+ else:
+ if end.y() >= start.y() + offset:
+ self.points = [start, QPointF(end.x(), start.y()), end]
+ else:
+ x = (start.x() + end.x()) / 2
+ self.points = [start, QPointF(x, start.y()),
+ QPointF(x, pe.y()), pe, end]
+
+ elif self.endGripItem.m_location in ["bottom"]:
+ if start.x() + offset < end.x() - ewidth:
+ if end.y() < start.y() - sheight - offset:
+ self.points = [start, ns, QPointF(ns.x(), ns.y() - sheight),
+ QPointF(pe.x(), ns.y() - sheight), end]
+ else:
+ y = max(start.y() + sheight, pe.y())
+ self.points = [start, ns, QPointF(ns.x(), y),
+ QPointF(pe.x(), y), end]
+ elif end.x() + ewidth >= start.x() - offset:
+ x = min(ns.x(), end.x() - ewidth)
+ self.points = [start, QPointF(x, ns.y()),
+ QPointF(x, pe.y()), pe, end]
+ else:
+ if end.y() <= start.y() - offset:
+ self.points = [start, QPointF(end.x(), start.y()), end]
+ else:
+ x = (start.x() + end.x()) / 2
+ self.points = [start, QPointF(x, start.y()),
+ QPointF(x, pe.y()), pe, end]
+
+ elif self.endGripItem.m_location in ["right"]:
+ self.points = [start, QPointF((start.x() + end.x()) / 2, start.y()),
+ QPointF((start.x() + end.x()) / 2, end.y()), end]
+ if end.x() > start.x() + offset:
+ if end.y() + eheight <= start.y() - sheight - offset:
+ self.points = [start, ns, QPointF(ns.x(), ns.y() - sheight),
+ QPointF(pe.x(), ns.y() - sheight), pe, end]
+ elif end.y() - eheight >= start.y() + sheight + offset:
+ self.points = [start, ns, QPointF(ns.x(), ns.y() + sheight),
+ QPointF(pe.x(), ns.y() + sheight), pe, end]
+ elif end.y() <= start.y():
+ y = min(end.y() - eheight, start.y() - sheight)
+ self.points = [start, ns, QPointF(ns.x(), y),
+ QPointF(pe.x(), y), pe, end]
+ else:
+ y = max(end.y() + eheight, start.y() + sheight)
+ self.points = [start, ns, QPointF(ns.x(), y),
+ QPointF(pe.x(), y), pe, end]
+
+ elif self.endGripItem.m_location in ["left"]:
+ self.points = [start, QPointF(pe.x(), start.y()), pe, end]
+ if start.x() + offset < end.x():
+ self.points = [start, ns, QPointF(ns.x(), end.y()), end]
+ if start.y() + sheight + offset > end.y() > start.y() - sheight - offset:
+ self.points = [start, ns, QPointF(ns.x(), ns.y() - sheight),
+ QPointF(pe.x(), ns.y() - sheight), pe, end]
+ elif end.y() - eheight - offset < start.y() < end.y() + eheight + offset:
+ if end.y() > start.y():
+ self.points = [start, ns, QPointF(ns.x(), pe.y() - eheight),
+ QPointF(pe.x(), pe.y() - eheight), pe, end]
+ else:
+ self.points = [start, ns, QPointF(ns.x(), pe.y() + eheight),
+ QPointF(pe.x(), pe.y() + eheight), pe, end]
+
+
+ elif self.startGripItem.m_location in ["top"]:
+ if self.endGripItem.m_location in ["top"]:
+ self.points = [self.startPoint, QPointF(start.x(), pe.y()),
+ pe, self.endPoint]
+ if start.y() < end.y():
+ self.points = [self.startPoint, ns, QPointF(pe.x(), ns.y()), self.endPoint]
+ if start.x() + swidth > end.x() > start.x() - swidth or end.x() + ewidth > start.x() > end.x() - ewidth:
+ x = max(start.x() + swidth, end.x() + ewidth)
+ x += offset
+ if start.x() > end.x():
+ x = min(start.x() - swidth, end.x() - ewidth)
+ x -= offset
+ self.points = [start, ns, QPointF(x, ns.y()),
+ QPointF(x, pe.y()), pe, end]
+
+ elif self.endGripItem.m_location in ["bottom"]:
+ self.points = [self.startPoint, ns, QPointF((x0 + x1) / 2, ns.y()), QPointF((x0 + x1) / 2, pe.y()),
+ pe, self.endPoint]
+ if start.y() - offset > end.y():
+ self.points = [start, QPointF(start.x(), (y0 + y1) / 2), QPointF(end.x(), (y0 + y1) / 2),
+ self.endPoint]
+ elif start.x() + swidth > end.x() > start.x() - swidth or end.x() + ewidth > start.x() > end.x() - ewidth:
+ x = max(start.x() + swidth, end.x() + ewidth)
+ x += offset
+ if start.x() > end.x():
+ x = min(start.x() - swidth, end.x() - ewidth)
+ x -= offset
+ self.points = [start, ns, QPointF(x, ns.y()),
+ QPointF(x, pe.y()), pe, end]
+
+ elif self.endGripItem.m_location in ["right"]:
+ y = min(ns.y(), end.y() + eheight + offset)
+ self.points = [start, QPointF(ns.x(), y), QPointF(pe.x(), y), pe, end]
+ if start.x() - swidth - offset < end.x() < start.x() + swidth + offset and end.y() > start.y() + offset:
+ self.points = [start, ns, QPointF(ns.x() + swidth + offset, ns.y()),
+ QPointF(ns.x() + swidth + offset, pe.y()), end]
+ elif end.y() - eheight < start.y() - offset < end.y() + eheight:
+ self.points = [start, ns, QPointF(start.x(), end.y() - eheight),
+ QPointF(pe.x(), end.y() - eheight), pe, end]
+
+ elif self.endGripItem.m_location in ["left"]:
+ y = min(ns.y(), end.y() + eheight + offset)
+ self.points = [start, QPointF(ns.x(), y), QPointF(pe.x(), y), pe, end]
+ if start.x() - swidth - offset < end.x() < start.x() + swidth + offset and end.y() > start.y() + offset:
+ self.points = [start, ns, QPointF(ns.x() - swidth - offset, ns.y()),
+ QPointF(ns.x() - swidth - offset, pe.y()), end]
+ elif end.y() - eheight < start.y() - offset < end.y() + eheight:
+ self.points = [start, QPointF(start.x(), end.y() - eheight),
+ QPointF(pe.x(), end.y() - eheight), pe, end]
+
+ elif self.startGripItem.m_location in ["bottom"]:
+ if self.endGripItem.m_location in ["top"]:
+ self.points = [self.startPoint, ns, QPointF((x0 + x1) / 2, ns.y()), QPointF((x0 + x1) / 2, pe.y()),
+ pe, self.endPoint]
+ if start.y() < end.y():
+ self.points = [self.startPoint, ns, QPointF(pe.x(), ns.y()), self.endPoint]
+ if start.x() + swidth > end.x() > start.x() - swidth or end.x() + ewidth > start.x() > end.x() - ewidth:
+ x = max(start.x() + swidth, end.x() + ewidth)
+ x += offset
+ self.points = [start, ns, QPointF(x, ns.y()),
+ QPointF(x, pe.y()), pe, end]
+
+ elif self.endGripItem.m_location in ["bottom"]:
+ self.points = [self.startPoint, ns, QPointF((x0 + x1) / 2, ns.y()), QPointF((x0 + x1) / 2, pe.y()),
+ pe, self.endPoint]
+ if start.x() + swidth > end.x() > start.x() - swidth or end.x() + ewidth > start.x() > end.x() - ewidth:
+ x = max(start.x() + swidth, end.x() + ewidth)
+ x += offset
+ self.points = [start, ns, QPointF(x, ns.y()),
+ QPointF(x, pe.y()), pe, end]
+
+ elif self.endGripItem.m_location in ["right"]:
+ y = max(ns.y(), end.y() + eheight + offset)
+ self.points = [start, QPointF(ns.x(), y), QPointF(pe.x(), y), pe, end]
+ if start.x() - swidth - offset < end.x() < start.x() + swidth + offset:
+ self.points = [start, ns, QPointF(ns.x() + swidth + offset, ns.y()),
+ QPointF(ns.x() + swidth + offset, pe.y()), end]
+
+ elif self.endGripItem.m_location in ["left"]:
+ y = max(ns.y(), end.y() + eheight + offset)
+ self.points = [start, QPointF(ns.x(), y), QPointF(pe.x(), y), pe, end]
+ if start.x() - swidth - offset < end.x() < start.x() + swidth + offset:
+ self.points = [start, ns, QPointF(ns.x() - swidth - offset, ns.y()),
+ QPointF(ns.x() - swidth - offset, pe.y()), end]
+
+ # path of line
path = QPainterPath(self.startPoint)
for i in range(1, len(self.points)):
path.lineTo(self.points[i])
@@ -161,6 +546,10 @@ class Line(QGraphicsPathItem):
updates points of line when grabber is moved
:return:
"""
+ if self.startGripItem:
+ self.points[0]= self.startPoint
+ if self.endGripItem:
+ self.points[len(self.points) - 1] = self.endPoint
if self.startGripItem.m_location in ["left", "right"]:
point = self.points[1]
self.points[1] = QPointF(point.x(), self.startPoint.y())
@@ -170,7 +559,6 @@ class Line(QGraphicsPathItem):
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())
@@ -181,6 +569,7 @@ class Line(QGraphicsPathItem):
point = self.points[len(self.points) - 2]
self.points[len(self.points) - 2] = QPointF(self.endPoint.x(), point.y())
+
def shape(self):
"""generates outline for path
"""
@@ -189,21 +578,6 @@ class Line(QGraphicsPathItem):
path = qp.createStroke(self.path())
return path
- 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
- elif self._selected:
- self.hideGripItem()
- self._selected = False
-
def movePoints(self, index, movement):
"""move points of line
"""
@@ -222,7 +596,7 @@ class Line(QGraphicsPathItem):
else:
direction = [Qt.Vertical, Qt.Horizontal]
for i in range(1, len(self.points) - 2):
- item = Grabber(self, i, direction[i - 1])
+ item = Grabber(self, i, direction[(i - 1)%2])
item.setParentItem(self)
item.setPos(self.pos())
self.scene().addItem(item)
@@ -243,6 +617,12 @@ class Line(QGraphicsPathItem):
grabber.setEnabled(True)
def itemChange(self, change, value):
+ if change == QGraphicsItem.ItemSelectedHasChanged:
+ if value == 1:
+ self.showGripItem()
+ else:
+ self.hideGripItem()
+ return
if change == QGraphicsItem.ItemSceneHasChanged and self.scene():
# self.addGrabber()
# self.updateGrabber()
@@ -262,6 +642,7 @@ class Line(QGraphicsPathItem):
self.endPoint = endPoint
self.createPath()
self.updateGrabber()
+ self.scene().update()
return
if self.startGripItem and self.endGripItem:
@@ -280,18 +661,20 @@ class Line(QGraphicsPathItem):
self.scene().removeItem(self)
def showGripItem(self):
- """hides grip items which contains line
+ """shows grip items which contains line
"""
if self.startGripItem: self.startGripItem.show()
if self.endGripItem: self.endGripItem.show()
- # for grabber in self.m_grabber:
- # grabber.setSelected(True)
+ for grabber in self.m_grabbers:
+ grabber.show()
def hideGripItem(self):
"""hides grip items which contains line
"""
if self.startGripItem: self.startGripItem.hide()
if self.endGripItem: self.endGripItem.hide()
+ for grabber in self.m_grabbers:
+ grabber.hide()
def setStartGripItem(self, item):
self.startGripItem = item
diff --git a/src/main/python/shapes/shapes.py b/src/main/python/shapes/shapes.py
index e79ddb7..c2d79e4 100644
--- a/src/main/python/shapes/shapes.py
+++ b/src/main/python/shapes/shapes.py
@@ -12,7 +12,12 @@ from PyQt5.QtWidgets import (QGraphicsColorizeEffect, QGraphicsEllipseItem,
from utils.app import fileImporter
from .line import Line
+from utils.app import fileImporter
+<<<<<<< HEAD
+
+=======
+>>>>>>> upstream/master
class GripItem(QGraphicsPathItem):
"""
@@ -53,7 +58,7 @@ class SizeGripItem(GripItem):
self.width = annotation_item.boundingRect().width()
path = QPainterPath()
- path.addRect(QRectF(-self.width/2, -self.height/2, self.width, self.height))
+ path.addRect(QRectF(-self.width / 2, -self.height / 2, self.width, self.height))
super(SizeGripItem, self).__init__(annotation_item, path=path, parent=parent)
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QGraphicsItem.ItemIsMovable, True)
@@ -84,7 +89,7 @@ class SizeGripItem(GripItem):
else:
self.width = self.parentItem().boundingRect().width()
path = QPainterPath()
- path.addRect(QRectF(-self.width/2, -self.height/2, self.width, self.height))
+ path.addRect(QRectF(-self.width / 2, -self.height / 2, self.width, self.height))
self.setPath(path)
def updatePosition(self):
@@ -245,6 +250,7 @@ class LineGripItem(GripItem):
self.transform())
if type(item) == LineGripItem and item != self:
+ self.tempLine.setStartGripItem(self)
self.tempLine.setEndGripItem(item)
endPoint = item.parentItem().mapToScene(item.pos())
self.tempLine.updateLine(endPoint=endPoint)
@@ -281,6 +287,8 @@ class NodeItem(QGraphicsSvgItem):
"""
Extends PyQt5's QGraphicsSvgItem to create the basic structure of shapes with given unit operation type
"""
+ # set a common renderer for all svg
+ renderer = QSvgRenderer(fileImporter(f'svg/ellipse.svg'))
def __init__(self, unitOperationType, parent=None):
QGraphicsSvgItem.__init__(self, parent)
@@ -319,7 +327,10 @@ class NodeItem(QGraphicsSvgItem):
"""
if not self.m_renderer:
QGraphicsSvgItem.paint(self, painter, option, widget)
- self.m_renderer.render(painter, self.boundingRect())
+ elif self.id:
+ self.m_renderer.render(painter,self.id, self.boundingRect())
+ else:
+ self.m_renderer.render(painter, self.boundingRect())
if self.isSelected():
self.showGripItem()
@@ -601,9 +612,9 @@ class InducedDraftCooling(NodeItem):
super(InducedDraftCooling, self).__init__("InducedDraftCooling", parent=None)
-class jacketedMixingVessel(NodeItem):
+class JacketedMixingVessel(NodeItem):
def __init__(self):
- super(jacketedMixingVessel, self).__init__("jacketedMixingVessel", parent=None)
+ super(JacketedMixingVessel, self).__init__("JacketedMixingVessel", parent=None)
class LiquidRingCompressor(NodeItem):
diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py
index 27e02c4..a997a30 100644
--- a/src/main/python/utils/graphics.py
+++ b/src/main/python/utils/graphics.py
@@ -86,15 +86,9 @@ class customScene(QGraphicsScene):
def __init__(self, *args, parent=None):
super(customScene, self).__init__(*args, parent=parent)
- self.setItemIndexMethod(QGraphicsScene.NoIndex)
-
self.undoStack = QUndoStack(self) #Used to store undo-redo moves
self.createActions() #creates necessary actions that need to be called for undo-redo
- def update(self, *args):
- self.advance()
- return super(customScene, self).update(*args)
-
def createActions(self):
# helper function to create delete, undo and redo shortcuts
self.deleteAction = QAction("Delete Item", self)
@@ -120,7 +114,6 @@ class customScene(QGraphicsScene):
def itemMoved(self, movedItem, lastPos):
#item move event, checks if item is moved
self.undoStack.push(moveCommand(movedItem, lastPos))
- self.advance()
def addItemPlus(self, item):
# extended add item method, so that a corresponding undo action is also pushed
diff --git a/src/main/python/utils/undo.py b/src/main/python/utils/undo.py
index 4a9f3dd..cf539e7 100644
--- a/src/main/python/utils/undo.py
+++ b/src/main/python/utils/undo.py
@@ -36,7 +36,6 @@ class addCommand(QUndoCommand):
self.scene.addItem(self.diagramItem)
self.diagramItem.setPos(self.itemPos)
self.scene.clearSelection()
- # print(self.diagramItem)
self.scene.update()
class deleteCommand(QUndoCommand):