diff options
author | sumit | 2020-05-06 11:22:25 +0530 |
---|---|---|
committer | sumit | 2020-05-06 11:22:25 +0530 |
commit | 2f8a29096069c77470be1a721f916f934d9f86ed (patch) | |
tree | 37f260a39dca02043a4f77cb9335774c57471379 | |
parent | bcceca62804d60c6308c6cefb3a65a9b074ee773 (diff) | |
download | Chemical-PFD-2f8a29096069c77470be1a721f916f934d9f86ed.tar.gz Chemical-PFD-2f8a29096069c77470be1a721f916f934d9f86ed.tar.bz2 Chemical-PFD-2f8a29096069c77470be1a721f916f934d9f86ed.zip |
highlight line on click
-rw-r--r-- | line.py | 37 | ||||
-rw-r--r-- | shapes.py | 80 |
2 files changed, 94 insertions, 23 deletions
@@ -1,6 +1,7 @@ -from PyQt5.QtGui import QFont, QPen, QPainterPath +from PyQt5.QtGui import QFont, QPen, QPainterPath, QPolygon, QBrush from PyQt5.QtWidgets import QGraphicsLineItem, QLineEdit, QGraphicsProxyWidget, QGraphicsItem from PyQt5.QtCore import Qt, QPointF, QRectF +from PyQt5.uic.properties import QtCore class Line(QGraphicsItem): @@ -10,6 +11,7 @@ class Line(QGraphicsItem): self.endPoint = endPoint self.startGripItem = None self.endGripItem = None + self._selected = False self.setFlag(QGraphicsItem.ItemIsSelectable, True) # self.setFlag(QGraphicsItem.ItemIsMovable, True) @@ -25,7 +27,7 @@ class Line(QGraphicsItem): 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 = QPainterPath(QPointF(x0, y0)) path.lineTo((x0 + x1) / 2, y0) path.moveTo((x0 + x1) / 2, y0) path.lineTo((x0 + x1) / 2, y1) @@ -46,12 +48,23 @@ class Line(QGraphicsItem): # painter.drawLine((x0 + x1) / 2, y1, x1, y1) 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()) + elif self._selected: + self.hideGripItem() + self._selected = False + def updateLine(self, startPoint=None, endPoint=None): """This function is used to update connecting line when it add on canvas and when it's grip item moves :return: """ + self.prepareGeometryChange() if self.startGripItem and self.endGripItem: item = self.startGripItem self.startPoint = item.parentItem().mapToScene(item.pos()) @@ -61,7 +74,7 @@ class Line(QGraphicsItem): self.startPoint = startPoint if endPoint: self.endPoint = endPoint - self.prepareGeometryChange() + self.update(self.boundingRect()) def removeFromCanvas(self): @@ -70,3 +83,21 @@ 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): + 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) + + def hideGripItem(self): + 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) + + @@ -3,9 +3,10 @@ import random from PyQt5 import QtCore from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer from PyQt5.QtWidgets import QLineEdit, QGraphicsItem, QGraphicsEllipseItem, QGraphicsProxyWidget, QGraphicsPathItem, \ - QGraphicsSceneHoverEvent -from PyQt5.QtGui import QPen, QColor, QFont, QCursor, QPainterPath, QPainter, QDrag -from PyQt5.QtCore import Qt, QRectF, QPointF, QSizeF, QEvent, QMimeData + 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 +from PyQt5.QtXml import QDomDocument from PyQt5.uic.properties import QtGui, QtWidgets from line import Line @@ -30,10 +31,10 @@ class GripItem(QGraphicsPathItem): # self.m_index = index self.setPath(path) - self.setPen(QPen(QColor(), -1)) self.setAcceptHoverEvents(True) self.setCursor(QCursor(Qt.PointingHandCursor)) + # self.setVisible(False) # def hoverEnterEvent(self, event): # """ @@ -67,7 +68,6 @@ class GripItem(QGraphicsPathItem): # return super(GripItem, self).itemChange(change, value) - class SizeGripItem(GripItem): """ Extends grip items for vertical and horizontal directions, with hover events and directional changes @@ -86,6 +86,7 @@ class SizeGripItem(GripItem): self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setFlag(QGraphicsItem.ItemIsMovable, True) self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) + self.setPen(QPen(QColor("black"), -1)) self.setZValue(1) self._direction = direction self.m_index = index @@ -139,6 +140,7 @@ class SizeGripItem(GripItem): """ self.setPen(QPen(QColor("black"), 2)) self.setBrush(QColor("red")) + # self.setVisible(True) if self._direction == Qt.Horizontal: self.setCursor(QCursor(Qt.SizeHorCursor)) else: @@ -149,6 +151,7 @@ class SizeGripItem(GripItem): """ reverts cursor to default on mouse leave """ + # self.setVisible(False) self.setPen(QPen(Qt.transparent)) self.setBrush(Qt.transparent) self.setCursor(QCursor(Qt.ArrowCursor)) @@ -185,7 +188,10 @@ class LineGripItem(GripItem): self.tempLine = None self.previousHoveredItem = None self.setFlag(QGraphicsItem.ItemIsSelectable, True) - self.setZValue(2) + # self.setZValue(2) + self.setPen(QPen(QColor("black"), -1)) + # self.setBrush(QColor("red")) + # self.setVisible(False) def point(self, index): """ @@ -264,6 +270,15 @@ class LineGripItem(GripItem): for line in self.connectedLines: line.removeFromCanvas() + def show(self): + self.setPen(QPen(QColor("black"), 2)) + self.setBrush(QColor("red")) + + def hide(self): + if (self.parentItem().isSelected() or self.isSelected()) is False: + self.setPen(QPen(Qt.transparent)) + self.setBrush(Qt.transparent) + class NodeItem(QGraphicsSvgItem): @@ -271,8 +286,31 @@ class NodeItem(QGraphicsSvgItem): QGraphicsSvgItem.__init__(self, parent) self.type = unitOpType self.rect = QRectF(0, 0, 100, 100) - self.renderer = QSvgRenderer("svg/" + "Column" + ".svg") - self.setSharedRenderer(self.renderer) + + self.doc = QDomDocument("doc") + self.file = QFile("svg/" + "Column" + ".svg") + if not self.file.open(QIODevice.ReadOnly): + print("Cannot open the file") + exit(-1) + if not self.doc.setContent(self.file): + print("Cannot parse the content") + self.file.close() + exit(-1) + self.file.close() + print(self.doc.documentElement()) + self.m_renderer = QSvgRenderer(self.doc.toByteArray()) + self.setSharedRenderer(self.m_renderer) + docElem = self.doc.documentElement() + print(docElem.tagName()) + nodeTag = docElem.firstChildElement() + print(nodeTag.tagName()) + gList = self.doc.elementsByTagName("g") + print(gList.item(0)) + + # self._effect = QGraphicsColorizeEffect() + # self._effect.setColor(Qt.red) + # self._effect.setStrength(1) + # self.setGraphicsEffect(self._effect) self.setZValue(2) self.setAcceptHoverEvents(True) @@ -284,12 +322,13 @@ class NodeItem(QGraphicsSvgItem): self.lineGripItems = [] self.sizeGripItems = [] - def boundingRect(self): return self.rect - def paint(self, painter, options, widget): - self.renderer.render(painter, self.boundingRect()) + def paint(self, painter, option, widget): + 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 @@ -305,7 +344,7 @@ class NodeItem(QGraphicsSvgItem): width = self.boundingRect().width() height = self.boundingRect().height() p_new = self.sizeGripItems[i].pos() - + self.prepareGeometryChange() if i == 0 or i == 1: self.rect = QRectF(x + p.x() - p_new.x(), y + p.y() - p_new.y(), width - p.x() + p_new.x(), height - p.y() + p_new.y()) @@ -313,7 +352,7 @@ 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.update_rect() + # self.update_rect() self.updateSizeGripItem([i]) self.updateLineGripItem() @@ -337,8 +376,6 @@ class NodeItem(QGraphicsSvgItem): self.scene().addItem(item) self.sizeGripItems.append(item) - - def updateLineGripItem(self, index_no_updates=None): # index_no_updates = index_no_updates or [] for item in self.lineGripItems: @@ -391,6 +428,7 @@ 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) @@ -404,17 +442,19 @@ class NodeItem(QGraphicsSvgItem): def hideGripItem(self): for item in self.lineGripItems: - item.setPen(QPen(Qt.transparent)) - item.setBrush(Qt.transparent) + if item.isSelected() is False: + item.setPen(QPen(Qt.transparent)) + item.setBrush(Qt.transparent) for item in self.sizeGripItems: item.setPen(QPen(Qt.transparent)) item.setBrush(Qt.transparent) # def mousePressEvent(self, event): - # self.setSelected(True) - # self.showGripItem() + # # self.setSelected(True) + # print(self.isSelected()) # super(NodeItem, self).mousePressEvent(event) # def mouseReleaseEvent(self, event): - # self.hideGripItem() + # print(self.isSelected()) + # # self.hideGripItem() # super(NodeItem, self).mouseReleaseEvent(event) |