def on_actItem_Polygon_triggered(self): # 添加一个梯形 item = QGraphicsPolygonItem() points = QPolygonF() points.append(QPointF(-40, -40)) points.append(QPointF(40, -40)) points.append(QPointF(100, 40)) points.append(QPointF(-100, 40)) item.setPolygon(points) item.setPos(-50 + (QtCore.qrand() % 100), -50 + (QtCore.qrand() % 100)) item.setFlags(QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsFocusable) item.setBrush(QBrush(Qt.green)) self.view.frontZ = self.view.frontZ + 1 item.setZValue(self.view.frontZ) self.view.seqNum = self.view.seqNum + 1 item.setData(self.view.ItemId, self.view.seqNum) # //自定义数据,ItemId键 item.setData(self.view.ItemDesciption, "梯形") self.scene.addItem(item) self.scene.clearSelection() item.setSelected(True)
def draw_polygons(self): sf = shapefile.Reader(self.shapefile) polygons = sf.shapes() for polygon in polygons: # convert shapefile geometries into shapely geometries # to extract the polygons of a multipolygon polygon = shapely.geometry.shape(polygon) # if it is a polygon, we use a list to make it iterable if polygon.geom_type == 'Polygon': polygon = [polygon] for land in polygon: qt_polygon = QPolygonF() for lon, lat in land.exterior.coords: px, py = self.to_canvas_coordinates(lon, lat) if px > 1e+10: continue qt_polygon.append(QPointF(px, py)) polygon_item = QGraphicsPolygonItem(qt_polygon) polygon_item.setBrush(self.land_brush) polygon_item.setPen(self.land_pen) polygon_item.setZValue(1) yield polygon_item
def draw_polygons(self): sf = shapefile.Reader(self.shapefile) polygons = sf.shapes() for polygon in polygons: # convert shapefile geometries into shapely geometries # to extract the polygons of a multipolygon polygon = shapely.geometry.shape(polygon) # if it is a polygon, we use a list to make it iterable if polygon.geom_type == 'Polygon': polygon = [polygon] for land in polygon: qt_polygon = QPolygonF() land = str(land)[10:-2].replace(', ', ',').replace(' ', ',') coords = land.replace('(', '').replace(')', '').split(',') for lon, lat in zip(coords[0::2], coords[1::2]): px, py = self.to_canvas_coordinates(lon, lat) if px > 1e+10: continue qt_polygon.append(QPointF(px, py)) polygon_item = QGraphicsPolygonItem(qt_polygon) polygon_item.setBrush(self.land_brush) polygon_item.setPen(self.land_pen) polygon_item.setZValue(1) yield polygon_item
class GraphicLine(QGraphicsLineItem): """ This class is a graphic line with an arrow which connects two blocks in the scene. Attributes ---------- origin : QGraphicsRectItem Origin rect of the line. destination : QGraphicsRectItem Destination rect of the line. scene : QGraphicsScene Current drawing scene. brush : QBrush Brush to draw the arrow. pen : QPen Pen to draw the arrow. arrow_head : QGraphicsPolygonItem Final arrow of the line. arrow_size : int Size of the head of the arrow. dim_label : QGraphicsTextItem Text showing the dimensions of the edge. is_valid : bool Flag monitoring whether the connection is consistent. Methods ---------- gen_endpoints(QRectF, QRectF) Returns the shortest connection between the two rects. draw_arrow() Draws the polygon for the arrow. set_valid(bool) Assign validity for this line. update_dims(tuple) Update the line dimensions. update_pos(QRectF) Update the line position given the new rect position. remove_self() Delete this line. """ def __init__(self, origin: QGraphicsRectItem, destination: QGraphicsRectItem, scene): super(GraphicLine, self).__init__() self.origin = origin self.destination = destination self.scene = scene # This flag confirms a legal connection self.is_valid = True # Get the four sides of the rects destination_lines = u.get_sides_of( self.destination.sceneBoundingRect()) origin_lines = u.get_sides_of(self.origin.sceneBoundingRect()) # Get the shortest edge between the two blocks self.setLine(self.gen_endpoints(origin_lines, destination_lines)) self.brush = QBrush(QColor(style.GREY_0)) self.pen = QPen(QColor(style.GREY_0)) self.pen.setWidth(4) self.pen.setCapStyle(Qt.RoundCap) self.pen.setJoinStyle(Qt.RoundJoin) self.setPen(self.pen) # Dimensions labels self.dim_label = QGraphicsTextItem() self.dim_label.setZValue(6) self.scene.addItem(self.dim_label) # Arrow head self.arrow_head = QGraphicsPolygonItem() self.arrow_head.setPen(self.pen) self.arrow_head.setBrush(self.brush) self.arrow_size = 15.0 self.draw_arrow() @staticmethod def gen_endpoints(origin_sides: dict, destination_sides: dict) -> QLineF: """ This method finds the shortest path between two rectangles. Parameters ---------- origin_sides : dict The dictionary {side_label: side_size} of the starting rect. destination_sides : dict The dictionary {side_label: side_size} of the ending rect. Returns ---------- QLineF The shortest line. """ # Init the line with the maximum possible value shortest_line = QLineF(-sys.maxsize / 2, -sys.maxsize / 2, sys.maxsize / 2, sys.maxsize / 2) for o_side, origin_side in origin_sides.items(): o_mid_x, o_mid_y = u.get_midpoint(o_side, origin_side) for d_side, destination_side in destination_sides.items(): d_mid_x, d_mid_y = u.get_midpoint(d_side, destination_side) # Update line line = QLineF(o_mid_x, o_mid_y, d_mid_x, d_mid_y) if line.length() < shortest_line.length(): shortest_line = line return shortest_line def draw_arrow(self) -> None: """ This method draws an arrow at the end of the line. """ polygon_arrow_head = QPolygonF() # Compute the arrow angle angle = math.acos(self.line().dx() / self.line().length()) angle = ((math.pi * 2) - angle) # Compute the direction where the arrow points (1 up, -1 down) arrow_direction = 1 if math.asin(self.line().dy() / self.line().length()) < 0: arrow_direction = -1 # First point of the arrow tail arrow_p1 = self.line().p2() - arrow_direction * QPointF( arrow_direction * math.sin(angle + math.pi / 2.5) * self.arrow_size, math.cos(angle + math.pi / 2.5) * self.arrow_size) # Second point of the arrow tail arrow_p2 = self.line().p2() - arrow_direction * QPointF( arrow_direction * math.sin(angle + math.pi - math.pi / 2.5) * self.arrow_size, math.cos(angle + math.pi - math.pi / 2.5) * self.arrow_size) # Third point is the line end polygon_arrow_head.append(self.line().p2()) polygon_arrow_head.append(arrow_p2) polygon_arrow_head.append(arrow_p1) # Add the arrow to the scene self.arrow_head.setZValue(1) self.arrow_head.setParentItem(self) self.arrow_head.setPolygon(polygon_arrow_head) def set_valid(self, valid: bool) -> None: """ This method changes the arrow style: if the connection is not valid the arrow becomes red, otherwise it remains grey with dimensions displayed. Parameters ---------- valid : bool New value for the legality flag. """ if valid: self.is_valid = True self.pen.setColor(QColor(style.GREY_0)) self.brush.setColor(QColor(style.GREY_0)) self.dim_label.setVisible(False) else: self.is_valid = False self.pen.setColor(QColor(style.RED_2)) self.brush.setColor(QColor(style.RED_2)) if self.scene.is_dim_visible: self.dim_label.setVisible(True) def update_dims(self, dims: tuple) -> None: """ This method updates the input & output dimensions. Parameters ---------- dims : tuple The new dimensions to update. """ self.dim_label.setHtml("<div style = 'background-color: " + style.RED_2 + "; color: white; font-family: consolas;'>" + str(dims) + "</div>") self.dim_label.setPos(self.line().center()) def update_pos(self, new_target: QRectF): """ This method updates the line as it origin or its destination has changed location. Parameters ---------- new_target : QRectF """ if new_target == self.destination: self.destination = new_target elif new_target == self.origin: self.origin = new_target # Get the four sides of the rects destination_lines = u.get_sides_of( self.destination.sceneBoundingRect()) origin_lines = u.get_sides_of(self.origin.sceneBoundingRect()) # Get the shortest edge between the two blocks self.setLine(self.gen_endpoints(origin_lines, destination_lines)) self.draw_arrow() self.dim_label.setPos(self.line().center()) def remove_self(self) -> None: """ The line is removed from the scene along with origin and destination pointers. """ self.scene.removeItem(self) self.scene.edges.remove(self) self.scene.removeItem(self.dim_label) self.origin = None self.destination = None
class RelationItem(): ''' This represents one relationship on the diagram. This class creates the arc graphics item and the text graphics item and draws them on the scene. The RelationInstance class manages reading and writing the relationship to Neo4j ''' def __init__(self, scene, relationInstance=None): self.scene = scene self.model = self.scene.parent.model self.diagramType = "Instance Relationship" self.logMsg = None # self.relationInstance should have been called self.itemInstance to be consistent # with NodeItem. so we'll set it to none and someday fix this. self.itemInstance = None self.relationInstance = relationInstance self.startNZID = self.relationInstance.startNZID self.endNZID = self.relationInstance.endNZID # get the NodeItem objects for the start and end nodes self.startNode = self.scene.parent.itemDict[self.startNZID] self.endNode = self.scene.parent.itemDict[self.endNZID] self.numRels = self.scene.parent.numRels(self.relationInstance.startNZID, self.relationInstance.endNZID) # print("numRels:{}".format(self.numRels)) self.lineType = None # print("num rels{}".format(str(self.numRels))) # initialize the two qgraphicsitems needed to draw a relationship to None self.IRel = None self.IRtext = None self.bunnyEar = None self.endDot = None self.arrowHead = None self.TAline1 = None self.TAline2 = None self.debugTriangle = None self.drawRelationship() def name(self, ): return self.relationInstance.NZID def NZID(self, ): return self.relationInstance.NZID def getFormat(self, ): ''' determine if the rel instance has a template format or should use the project default format ''' # get the default self.relFormat = IRelFormat(formatDict=self.model.modelData["IRformat"]) # get a custom template format if there is one if not self.relationInstance.relTemplate is None: index, relTemplateDict = self.model.getDictByName(topLevel="Relationship Template",objectName=self.relationInstance.relTemplate) if not relTemplateDict is None: self.instanceRelFormatDict = relTemplateDict.get("IRformat", None) if not self.instanceRelFormatDict is None: self.relFormat = IRelFormat(formatDict=self.instanceRelFormatDict) def getNodeId(self): '''return the rel id for the relationship from the IREL or bunnyear graphic item - which ever it is ''' if not self.IRel is None: nodeID = self.IRel.data(NODEID) elif not self.bunnyEar is None: nodeID = self.bunnyEar.data(NODEID) else: nodeID = None return nodeID def clearItem(self, ): if (not self.IRel is None and not self.IRel.scene() is None): self.IRel.scene().removeItem(self.IRel) if (not self.IRtext is None and not self.IRtext.scene() is None): self.IRtext.scene().removeItem(self.IRtext) if (not self.bunnyEar is None and not self.bunnyEar.scene() is None): self.bunnyEar.scene().removeItem(self.bunnyEar) if (not self.endDot is None and not self.endDot.scene() is None): self.endDot.scene().removeItem(self.endDot) if (not self.arrowHead is None and not self.arrowHead.scene() is None): self.arrowHead.scene().removeItem(self.arrowHead) if (not self.TAline2 is None and not self.TAline2.scene() is None): self.TAline2.scene().removeItem(self.TAline2) if (not self.TAline1 is None and not self.TAline1.scene() is None): self.TAline1.scene().removeItem(self.TAline1) # if (not self.debugTriangle is None and not self.debugTriangle.scene() is None): # self.debugTriangle.scene().removeItem(self.debugTriangle) def drawIt(self, ): ''' for consistency, drawIt should be used instead of drawRelationship ''' self.drawRelationship() def drawRelationship(self, ): # relationship is between two different nodes if self.startNZID != self.endNZID: # set the line type if it hasn't been determined yet. This happens on first draw if self.lineType is None: if self.scene.parent.anyRels(self.relationInstance.startNZID, self.relationInstance.endNZID): self.lineType = CURVE else: self.lineType = STRAIGHT # now draw the line or arc if self.lineType == CURVE: self.drawRel() else: self.drawStraightRel() # relationship is between the same node if self.startNZID == self.endNZID: self.drawBunnyEars() def drawBunnyEars(self, ): # force the rel instance to update its values in case it has been updated from another diagram or the tree view self.relationInstance.reloadDictValues() # get the format in case it changed self.getFormat() # if the arc and text graphics items already exist on the scene then delete them self.clearItem() # draw the relationship arc pen = self.relFormat.pen() brush = self.relFormat.brush() brush.setColor(QColor(Qt.white)) # create an ellipse like the startNode x = self.startNode.INode.sceneBoundingRect().center().x() y = self.startNode.INode.sceneBoundingRect().center().y() w = self.startNode.INode.sceneBoundingRect().width()/2.0 h = self.startNode.INode.sceneBoundingRect().height()/2.0 myEllipse = Ellipse(x, y, w, h) # move the bunny ears around the ellipse until we run out of room startDegree = 360 - (self.numRels * 40) if startDegree < 40: startDegree = 40 startPoint = myEllipse.pointFromAngle(radians(startDegree)) endPoint = myEllipse.pointFromAngle(radians(startDegree-35)) lineMidPoint = centerLine (startPoint[0], startPoint[1], endPoint[0], endPoint[1]) lineDist = distanceLine (startPoint[0], startPoint[1], endPoint[0], endPoint[1]) circleRad = lineDist/2.0 self.bunnyEar = QGraphicsEllipseItem(QRectF(lineMidPoint[0]-circleRad,lineMidPoint[1]-circleRad,circleRad*2,circleRad*2), parent=None) self.bunnyEar.setZValue(RELATIONLAYER) self.bunnyEar.setBrush(brush) self.bunnyEar.setPen(pen) self.bunnyEar.setFlag(QGraphicsItem.ItemIsMovable, True) self.bunnyEar.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.bunnyEar.setFlag(QGraphicsItem.ItemIsSelectable, False) self.bunnyEar.setSelected(False) # create arrowhead self.circlex = lineMidPoint[0] self.circley = lineMidPoint[1] # self.circleDot = QGraphicsEllipseItem(QRectF(self.circlex-1.5,self.circley-1.5,3, 3), parent=None) # self.circleDot.setZValue(NODELAYER) # self.scene.addItem(self.circleDot) # self.pointDot = QGraphicsEllipseItem(QRectF(endPoint[0]-1.5,endPoint[1]-1.5,3, 3), parent=None) # self.pointDot.setZValue(NODELAYER) # self.scene.addItem(self.pointDot) # compute the arrow base center point arrowLen = 5 # this is half the distance of the arrowhead base which determines how wide the arrow head is # compute a point on the arc that is a short distance away from the edge of the ellipse. this is the base of the arrow head # get the slope of the line from the start point to the end point on the node ellipse if endPoint[0] == startPoint[0]: startPoint[0] = startPoint[0] + .001 # slope1 = (startPoint[1] - endPoint[1])/(startPoint[0] - endPoint[0]) slope1 = (endPoint[1] - startPoint[1] )/(endPoint[0] - startPoint[0]) if slope1 == 1.0: slope1 = 1.001 slope1Squared = slope1 * slope1 perpSlope1 = -1.0 / slope1 perpSlope1Squared = perpSlope1 * perpSlope1 # print("numrels:{} startDegree:{} radius:{} slope:{} perpslope:{}".format(self.numRels, startDegree, circleRad, slope1, perpSlope1)) # now find a point on the line tangent to the point on the bunny ear circle. This is the base of the arrow # ax = (arrowLen / sqrt(1 + (perpSlope1Squared))) # ay = ax * perpSlope1 if lineMidPoint[0] < x: baseX = endPoint[0] - (arrowLen / sqrt(1 + (perpSlope1Squared))) baseY = endPoint[1] - ((arrowLen / sqrt(1 + (perpSlope1Squared))) * perpSlope1 ) else: baseX = endPoint[0] + (arrowLen / sqrt(1 + (perpSlope1Squared))) baseY = endPoint[1] + ((arrowLen / sqrt(1 + (perpSlope1Squared))) * perpSlope1 ) # print("ax:{} ay:{} slope1:{} perslope1:{}".format(ax, ay, slope1, perpSlope1)) # self.anchorDot = QGraphicsEllipseItem(QRectF(baseX-1.5,baseY-1.5,3, 3), parent=None) # self.anchorDot.setZValue(NODELAYER) # self.scene.addItem(self.anchorDot) # get first arrowhead base corner ab1dx = (arrowLen / sqrt(1 + (slope1Squared))) ab1dy = ab1dx * slope1 self.b1x = baseX + ab1dx self.b1y = baseY + ab1dy # get 2nd arrowhead base corner ab2dx = (arrowLen / sqrt(1 + (slope1Squared))) ab2dy = ab2dx * slope1 self.b2x = baseX - ab2dx self.b2y = baseY - ab2dy # # calculate the arrowhead points # cx = self.endNode.INode.sceneBoundingRect().center().x() # cy = self.endNode.INode.sceneBoundingRect().center().y() # self.calcArrowHead(QPointF(cx, cy), QPointF( endPoint[0], endPoint[1]), bunnyEars=True) #create an empty polygon arrowPolygon = QPolygonF() # # add arrowhead points # arrowPolygon.append(QPointF(self.ah1x, self.ah1y)) # arrowPolygon.append(QPointF(endPoint[0], endPoint[1])) # arrowPolygon.append(QPointF(self.ah2x, self.ah2y)) # add arrowhead points arrowPolygon.append(QPointF(self.b1x, self.b1y)) arrowPolygon.append(QPointF(endPoint[0], endPoint[1])) arrowPolygon.append(QPointF(self.b2x, self.b2y)) self.arrowHead = QGraphicsPolygonItem(arrowPolygon, parent=None, ) self.arrowHead.setZValue(RELATIONLAYER) self.arrowHead.setBrush(brush) self.arrowHead.setPen(pen) self.arrowHead.setFlag(QGraphicsItem.ItemIsMovable, True) self.arrowHead.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.arrowHead.setFlag(QGraphicsItem.ItemIsSelectable, False) self.arrowHead.setSelected(False) # set data in the bunnyEar object self.bunnyEar.setData(NODEID, self.relationInstance.NZID) self.bunnyEar.setData(ITEMTYPE, RELINSTANCEARC) self.scene.addItem(self.bunnyEar) self.scene.addItem(self.arrowHead) # calculate position of text bunnyEllipse = Ellipse(lineMidPoint[0],lineMidPoint[1],circleRad,circleRad) # use the same midpoint angle as the bunny ear start and end point bunnyMidPoint = bunnyEllipse.pointFromAngle(radians(startDegree-17.5)) # draw the text self.drawText(bunnyMidPoint[0], bunnyMidPoint[1], startPoint[0], startPoint[1], endPoint[0], endPoint[1]) # self.anchorDot = QGraphicsEllipseItem(QRectF(bunnyMidPoint[0]-1.5,bunnyMidPoint[1]-1.5,3, 3), parent=None) # self.anchorDot.setZValue(NODELAYER) # self.scene.addItem(self.anchorDot) def drawStraightRel(self): # print("draw straight rel") # force the rel instance to update its values in case it has been updated from another diagram or the tree view self.relationInstance.reloadDictValues() # get the format in case it changed self.getFormat() # if the arc and text graphics items already exist on the scene then delete them self.clearItem() # draw the relationship arc pen = self.relFormat.pen() brush = self.relFormat.brush() # get the centerpoint of the end node cx = self.endNode.INode.sceneBoundingRect().center().x() cy = self.endNode.INode.sceneBoundingRect().center().y() # get the start and end points of the line esx, esy, eex, eey = self.calcLine3() # create the arc qgraphicsitem self.IRel = QGraphicsLineItem(esx, esy, eex, eey, parent=None) arrowLine = self.IRel # text location tx = self.IRel.line().pointAt(.5).x() ty = self.IRel.line().pointAt(.5).y() # draw the relationship line pen = self.relFormat.pen() # configure the lines and add them to the scene self.IRel.setPen(pen) self.IRel.setZValue(RELATIONLAYER) self.IRel.setData(NODEID, self.relationInstance.NZID) self.IRel.setData(ITEMTYPE, RELINSTANCEARC) self.IRel.setFlag(QGraphicsItem.ItemIsSelectable, True) self.scene.addItem(self.IRel) # line arrowhead # compute the arrow base center point arrowLen = 5 # this is half the distance of the arrowhead base which determines how wide the arrow head is # compute a point on the arc that is a short distance away from the edge of the ellipse. this is the base of the arrow head ax = self.IRel.line().pointAt(1-(10.0/self.IRel.line().length())).x() ay = self.IRel.line().pointAt(1-(10.0/self.IRel.line().length())).y() # compute the arrow base corners if eex == ax: ax = ax + .001 # get the slope of the line from the ellipse to the arrowhead base slope = (eey - ay)/(eex - ax) if slope == 1.0: slope = 1.001 # get the perpindicular slope perpSlope = -1.0 / slope perpSlopeSquared = perpSlope * perpSlope # get first arrowhead base corner ab1dx = (arrowLen / sqrt(1 + (perpSlopeSquared))) ab1dy = ab1dx * perpSlope self.b1x = ax + ab1dx self.b1y = ay + ab1dy # get 2nd arrowhead base corner ab2dx = (arrowLen / sqrt(1 + (perpSlopeSquared))) ab2dy = ab2dx * perpSlope self.b2x = ax - ab2dx self.b2y = ay - ab2dy #create an empty polygon arrowPolygon = QPolygonF() # add arrowhead points arrowPolygon.append(QPointF(self.b1x, self.b1y)) arrowPolygon.append(QPointF(eex, eey)) arrowPolygon.append(QPointF(self.b2x, self.b2y)) self.arrowHead = QGraphicsPolygonItem(arrowPolygon, parent=None, ) self.arrowHead.setZValue(RELATIONLAYER) self.arrowHead.setBrush(brush) self.arrowHead.setPen(pen) self.arrowHead.setFlag(QGraphicsItem.ItemIsMovable, True) self.arrowHead.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.arrowHead.setFlag(QGraphicsItem.ItemIsSelectable, False) self.arrowHead.setSelected(False) self.scene.addItem(self.arrowHead) # draw the text self.drawText(tx, ty, esx, esy, eex, eey) def drawRel(self, ): # draw the curved relationship line # print("draw curve rel") # force the rel instance to update its values in case it has been updated from another diagram or the tree view self.relationInstance.reloadDictValues() # get the format in case it changed self.getFormat() # if the arc and text graphics items already exist on the scene then delete them self.clearItem() # draw the relationship arc pen = self.relFormat.pen() brush = self.relFormat.brush() # METHOD 1 use ellipse center points # # get the center points of the qgraphicellipses # sx = self.startNode.INode.sceneBoundingRect().center().x() # sy = self.startNode.INode.sceneBoundingRect().center().y() # ex = self.endNode.INode.sceneBoundingRect().center().x() # ey = self.endNode.INode.sceneBoundingRect().center().y() # # if the x's or y's are equal offset one slightly to avoid divide by zero errors # if sx == ex: # ex = ex + .001 # if sy == ey: # ey = ey + .001 # self.IRel = RelArc(parent=None, sx=sx, sy=sy, ex=ex, ey=ey, pen=pen, brush=brush, numRels=self.numRels) # METHOD 2 use points on the ellipse # esx, esy, eex, eey = self.calcLine() # self.IRel = RelArc(parent=None, sx=esx, sy=esy, ex=eex, ey=eey, pen=pen, brush=brush, numRels=self.numRels) # METHOD 3 use regularly spaced endpoints on the ellipse # get the centerpoint of the end node cx = self.endNode.INode.sceneBoundingRect().center().x() cy = self.endNode.INode.sceneBoundingRect().center().y() # get the start and end points of the arc esx, esy, eex, eey = self.calcLine2() # create the arc qgraphicsitem self.IRel = RelArc(parent=None, sx=esx, sy=esy, ex=eex, ey=eey, cx=cx, cy=cy, pen=pen, brush=brush, numRels=self.numRels) #create an empty polygon arrowPolygon = QPolygonF() # add arrowhead points arrowPolygon.append(QPointF(self.IRel.b1x, self.IRel.b1y)) arrowPolygon.append(QPointF(eex, eey)) arrowPolygon.append(QPointF(self.IRel.b2x, self.IRel.b2y)) self.arrowHead = QGraphicsPolygonItem(arrowPolygon, parent=None, ) self.arrowHead.setZValue(RELATIONLAYER) self.arrowHead.setBrush(brush) self.arrowHead.setPen(pen) self.arrowHead.setFlag(QGraphicsItem.ItemIsMovable, True) self.arrowHead.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.arrowHead.setFlag(QGraphicsItem.ItemIsSelectable, False) self.arrowHead.setSelected(False) # set data in the RelLine object self.IRel.setData(NODEID, self.relationInstance.NZID) self.IRel.setData(ITEMTYPE, RELINSTANCEARC) # add the RelLine object to the scene self.scene.addItem(self.IRel) # add arrowhead to the scene self.scene.addItem(self.arrowHead) # draw the text self.drawText(self.IRel.mx, self.IRel.my, esx, esy, eex, eey) def drawText(self, mx, my, esx, esy, eex, eey): # mx,my is the center point of the arc # esx,esy is the start point of the arc # eex,eey is the end point of the arc # get the center points of the node qgraphicellipses sx = self.startNode.INode.sceneBoundingRect().center().x() sy = self.startNode.INode.sceneBoundingRect().center().y() sh = self.startNode.INode.sceneBoundingRect().height() ex = self.endNode.INode.sceneBoundingRect().center().x() ey = self.endNode.INode.sceneBoundingRect().center().y() eh = self.endNode.INode.sceneBoundingRect().height() # if the x's or y's are equal offset one slightly to avoid divide by zero errors if sx == ex: ex = ex + .001 if sy == ey: ey = ey + .001 # draw the text # self.IRtext = QGraphicsTextItem(self.relationInstance.relName, parent=None) self.IRtext = RelText(parent=None) # self.IRtext.setZValue(RELATIONLAYER) # self.IRtext.setFlag(QGraphicsItem.ItemIsMovable, True) # self.IRtext.setFlag(QGraphicsItem.ItemIsSelectable, True) # self.IRtext.setSelected(True) # self.IRtext.setAcceptHoverEvents(True) self.IRtext.setData(NODEID, self.relationInstance.NZID) self.IRtext.setData(ITEMTYPE, RELINSTANCETEXT) self.IRtext.setHtml(self.genRelHTML()) #calculate rotation angle adj = abs(esx-eex) opp = abs(esy-eey) OOA = opp/adj rotateAnglerad = (atan(OOA)) rotateAngledeg = degrees(atan(OOA)) # get the height and width of the text graphics item th = self.IRtext.boundingRect().height() tw = self.IRtext.boundingRect().width() # position the relation name text centered on the arc centerpoint if self.startNZID != self.endNZID: # esx, esy, eex, eey if esy >= eey and esx <= eex: # end node is above and to the right self.IRtext.setRotation((360 - rotateAngledeg)) xoffset = (tw/2) * cos(rotateAnglerad) yoffset = (tw/2) * sin(rotateAnglerad) self.IRtext.setPos(QPointF(mx - xoffset, my + yoffset )) elif esy <= eey and esx <= eex: # end node is below and to the right self.IRtext.setRotation(rotateAngledeg) xoffset = (tw/2) * (cos(rotateAnglerad)) yoffset = (tw/2) * (sin(rotateAnglerad)) self.IRtext.setPos(QPointF(mx - xoffset, my - yoffset )) elif esy >= eey and esx >= eex: # end node is above and to the left self.IRtext.setRotation(rotateAngledeg) xoffset = (tw/2) * (cos(rotateAnglerad)) yoffset = (tw/2) * (sin(rotateAnglerad)) self.IRtext.setPos(QPointF(mx - xoffset, my - yoffset )) elif esy <= eey and esx >= eex: # end node is below and to the left self.IRtext.setRotation((360 - rotateAngledeg)) xoffset = (tw/2) * cos(rotateAnglerad) yoffset = (tw/2) * sin(rotateAnglerad) self.IRtext.setPos(QPointF(mx - xoffset, my + yoffset )) # this shouldn't happen else: xoffset = (tw/2) yoffset = (tw/2) self.IRtext.setPos(QPointF(mx, my )) # print("tw {} xoffset {} yoffset {} angle {}".format(tw, xoffset, yoffset, rotateAngledeg)) elif self.startNZID == self.endNZID: if mx > sx: self.IRtext.setPos(QPointF(mx, my - (th) )) if mx < sx: self.IRtext.setPos(QPointF(mx - tw, my - (th) )) else: # this shouldn't happen self.IRtext.setPos(QPointF(mx, my )) self.scene.addItem(self.IRtext) def updateText(self, ): # force the node instance to update its values in case it has been updated from another diagram or the tree view self.relationInstance.reloadDictValues() # self.IRtext.setPlainText(self.relationInstance.relName) self.IRtext.setHtml(self.genRelHTML()) def genRelHTML(self): '''generate html to display the relationship type ''' prefix = '<html><body>' suffix = "</body></html>" myHTML = ('{}<p><font size="1"> [{}]</font></p>{}'.format(prefix, self.relationInstance.relName, suffix)) return myHTML # def calcLine(self, ): # '''calculate the points on the two ellipses to draw a straight line through their centers''' # self.startNodeX = self.startNode.INode.sceneBoundingRect().center().x() # self.startNodeY = self.startNode.INode.sceneBoundingRect().center().y() # self.endNodeX = self.endNode.INode.sceneBoundingRect().center().x() # self.endNodeY = self.endNode.INode.sceneBoundingRect().center().y() # # self.distanceX = abs(self.startNodeX - self.endNodeX) + .001 # self.distanceY = abs(self.startNodeY - self.endNodeY) + .001 # temp = (pow(self.distanceX,2) + pow(self.distanceY,2)) - (2 * self.distanceX * self.distanceY * cos(radians(90))) # self.distanceXY = sqrt(temp) # a = self.distanceX # b = self.distanceY # c = self.distanceXY ## angA = angle(a,b,c) ## angB = angle(b,c,a) # angC = angleDegrees(c,a,b) # # if self.endNodeX == self.startNodeX: # self.endNodeX = self.endNodeX + .001 # if self.endNodeY == self.startNodeY: # self.endNodeY = self.endNodeY + .001 # # if self.endNodeX > self.startNodeX and self.endNodeY > self.startNodeY: # startangle = angC # endangle = 180 + angC # elif self.endNodeX < self.startNodeX and self.endNodeY > self.startNodeY: # startangle = 180 - angC # endangle = 360 - angC # elif self.endNodeX < self.startNodeX and self.endNodeY < self.startNodeY: # startangle = 180 + angC # endangle = angC # elif self.endNodeX > self.startNodeX and self.endNodeY < self.startNodeY: # startangle = 360 - angC # endangle = 180 - angC # # #print("angA: {} angleB: {} angleC: {} startangle: {}".format(angA, angB, angC, startangle)) # aa = self.startNode.INode.boundingRect().width()/2.0 # bb = self.startNode.INode.boundingRect().height()/2.0 # sn = Ellipse(self.startNodeX, self.startNodeY, aa, bb) # sx, sy = sn.pointFromAngle(radians(startangle)) # cc = self.endNode.INode.boundingRect().width()/2.0 # dd = self.endNode.INode.boundingRect().height()/2.0 # en = Ellipse(self.endNodeX, self.endNodeY, cc, dd) #need to calc width/height of end node. # ex, ey = en.pointFromAngle(radians(endangle)) # #print("sx {} - sy {} - ex {} - ey {}".format(sx,sy,ex,sy)) # return sx, sy, ex, ey def calcLine2(self, ): '''calculate the point on the start and end node ellipse for an arc ''' # get centerpoints of the two nodes self.startNodeX = self.startNode.INode.sceneBoundingRect().center().x() self.startNodeY = self.startNode.INode.sceneBoundingRect().center().y() self.endNodeX = self.endNode.INode.sceneBoundingRect().center().x() self.endNodeY = self.endNode.INode.sceneBoundingRect().center().y() self.distanceX = abs(self.startNodeX - self.endNodeX) + .001 self.distanceY = abs(self.startNodeY - self.endNodeY) + .001 temp = (pow(self.distanceX,2) + pow(self.distanceY,2)) - (2 * self.distanceX * self.distanceY * cos(radians(90))) self.distanceXY = sqrt(temp) a = self.distanceX b = self.distanceY c = self.distanceXY # angA = angle(a,b,c) # angB = angle(b,c,a) angC = angleDegrees(c,a,b) # print("angC {}".format(angC)) if self.endNodeX == self.startNodeX: self.endNodeX = self.endNodeX + .001 if self.endNodeY == self.startNodeY: self.endNodeY = self.endNodeY + .001 # adjust starting and ending points based on the number of rels between the node, if more than 7 in the same direction just start stacking them one on top of the other. if self.numRels > 6: adjustDegree = (7) * 10 else: adjustDegree = (self.numRels+1) * 10 if self.endNodeX > self.startNodeX and self.endNodeY > self.startNodeY: startangle = angC - adjustDegree endangle = 180 + angC + adjustDegree elif self.endNodeX < self.startNodeX and self.endNodeY > self.startNodeY: startangle = 180 - angC - adjustDegree endangle = 360 - angC + adjustDegree elif self.endNodeX < self.startNodeX and self.endNodeY < self.startNodeY: startangle = 180 + angC - adjustDegree endangle = angC + adjustDegree elif self.endNodeX > self.startNodeX and self.endNodeY < self.startNodeY: startangle = 360 - angC - adjustDegree endangle = 180 - angC + adjustDegree # print("adjust: {}startangle: {} endangle: {}".format(adjustDegree, startangle, endangle)) aa = self.startNode.INode.boundingRect().width()/2.0 bb = self.startNode.INode.boundingRect().height()/2.0 sn = Ellipse(self.startNodeX, self.startNodeY, aa, bb) sx, sy = sn.pointFromAngle(radians(startangle)) cc = self.endNode.INode.boundingRect().width()/2.0 dd = self.endNode.INode.boundingRect().height()/2.0 en = Ellipse(self.endNodeX, self.endNodeY, cc, dd) #need to calc width/height of end node. ex, ey = en.pointFromAngle(radians(endangle)) #print("sx {} - sy {} - ex {} - ey {}".format(sx,sy,ex,sy)) return sx, sy, ex, ey def calcLine3(self, ): '''calculate the point on the start and end node ellipse for a straight line ''' # get centerpoints of the two nodes self.startNodeX = self.startNode.INode.sceneBoundingRect().center().x() self.startNodeY = self.startNode.INode.sceneBoundingRect().center().y() self.endNodeX = self.endNode.INode.sceneBoundingRect().center().x() self.endNodeY = self.endNode.INode.sceneBoundingRect().center().y() self.distanceX = abs(self.startNodeX - self.endNodeX) + .001 self.distanceY = abs(self.startNodeY - self.endNodeY) + .001 temp = (pow(self.distanceX,2) + pow(self.distanceY,2)) - (2 * self.distanceX * self.distanceY * cos(radians(90))) self.distanceXY = sqrt(temp) a = self.distanceX b = self.distanceY c = self.distanceXY # angA = angle(a,b,c) # angB = angle(b,c,a) angC = angleDegrees(c,a,b) # print("angC {}".format(angC)) if self.endNodeX == self.startNodeX: self.endNodeX = self.endNodeX + .001 if self.endNodeY == self.startNodeY: self.endNodeY = self.endNodeY + .001 # # adjust starting and ending points based on the number of rels between the node, if more than 7 in the same direction just start stacking them one on top of the other. # if self.numRels > 6: # adjustDegree = (7) * 10 # else: # adjustDegree = (self.numRels+1) * 10 adjustDegree = 0 if self.endNodeX > self.startNodeX and self.endNodeY > self.startNodeY: startangle = angC - adjustDegree endangle = 180 + angC + adjustDegree elif self.endNodeX < self.startNodeX and self.endNodeY > self.startNodeY: startangle = 180 - angC - adjustDegree endangle = 360 - angC + adjustDegree elif self.endNodeX < self.startNodeX and self.endNodeY < self.startNodeY: startangle = 180 + angC - adjustDegree endangle = angC + adjustDegree elif self.endNodeX > self.startNodeX and self.endNodeY < self.startNodeY: startangle = 360 - angC - adjustDegree endangle = 180 - angC + adjustDegree # print("adjust: {}startangle: {} endangle: {}".format(adjustDegree, startangle, endangle)) aa = self.startNode.INode.boundingRect().width()/2.0 bb = self.startNode.INode.boundingRect().height()/2.0 sn = Ellipse(self.startNodeX, self.startNodeY, aa, bb) sx, sy = sn.pointFromAngle(radians(startangle)) cc = self.endNode.INode.boundingRect().width()/2.0 dd = self.endNode.INode.boundingRect().height()/2.0 en = Ellipse(self.endNodeX, self.endNodeY, cc, dd) #need to calc width/height of end node. ex, ey = en.pointFromAngle(radians(endangle)) #print("sx {} - sy {} - ex {} - ey {}".format(sx,sy,ex,sy)) return sx, sy, ex, ey # def moveRelationshipLine(self, ): # self.drawRelationship() def getObjectDict(self, ): objectDict = {} objectDict["NZID"] = self.relationInstance.NZID objectDict["diagramType"] = self.diagramType return objectDict
class ASCGraphicsView(QGraphicsView): scale_signal = pyqtSignal('float', 'float') set_focus_point_signal = pyqtSignal('int', 'int', 'int') set_focus_point_percent_signal = pyqtSignal('float', 'float', 'float') move_focus_point_signal = pyqtSignal('int', 'int', 'int') # x, y, z, BRUSH_TYPE, BRUSH_SIZE, ERASE paint_anno_on_point_signal = pyqtSignal('int', 'int', 'int', 'int', 'int', 'bool', 'bool') def __init__(self, parent=None): super(ASCGraphicsView, self).__init__(parent) self.scene = QGraphicsScene(self) self.raw_img_item = QGraphicsPixmapItem() self.raw_img_item.setZValue(0) self.anno_img_item = QGraphicsPixmapItem() self.anno_img_item.setZValue(1) self.cross_bar_v_line_item = QGraphicsLineItem() self.cross_bar_h_line_item = QGraphicsLineItem() self.cross_bar_v_line_item.setZValue(100) self.cross_bar_h_line_item.setZValue(100) self.cross_bar_v_line_item.setPen( QPen(Qt.blue, 0, Qt.DotLine, Qt.FlatCap, Qt.RoundJoin)) self.cross_bar_h_line_item.setPen( QPen(Qt.blue, 0, Qt.DotLine, Qt.FlatCap, Qt.RoundJoin)) self.paint_brush_circle_item = QGraphicsEllipseItem() self.paint_brush_rect_item = QGraphicsPolygonItem() self.paint_brush_circle_item.setZValue(10) self.paint_brush_rect_item.setZValue(11) self.paint_brush_circle_item.setVisible(False) self.paint_brush_rect_item.setVisible(False) self.paint_brush_circle_item.setPen( QPen(Qt.red, 0, Qt.DotLine, Qt.FlatCap, Qt.RoundJoin)) self.paint_brush_rect_item.setPen( QPen(Qt.red, 0, Qt.DotLine, Qt.FlatCap, Qt.RoundJoin)) self.scene.addItem(self.raw_img_item) self.scene.addItem(self.anno_img_item) self.scene.addItem(self.cross_bar_v_line_item) self.scene.addItem(self.cross_bar_h_line_item) self.scene.addItem(self.paint_brush_circle_item) self.scene.addItem(self.paint_brush_rect_item) self.setScene(self.scene) self.setViewport(QOpenGLWidget()) self._last_button_press = Qt.NoButton self._last_pos_middle_button = None self._last_pos_right_button = None self._brush_stats = {'type': BRUSH_TYPE_NO_BRUSH, 'size': 5} self.setResizeAnchor(QGraphicsView.AnchorViewCenter) self.slice_scroll_bar = None self.image_size = None self.is_valid = False def clear(self): """before loading new image""" self._last_pos_middle_button = None self._last_pos_right_button = None self._last_button_press = Qt.NoButton self.raw_img_item.setPixmap(QPixmap()) self.anno_img_item.setPixmap(QPixmap()) self.paint_brush_circle_item.setVisible(False) self.paint_brush_rect_item.setVisible(False) self.image_size = None self.is_valid = False def init_view(self, image_size): """after loading new image""" self.is_valid = True self.image_size = image_size self.slice_scroll_bar = self.parent().findChild( QScrollBar, self.objectName()[0] + 'SliceScrollBar') trans_mat = item2scene_transform[self.objectName()[0]] self.raw_img_item.setTransform(trans_mat) self.anno_img_item.setTransform(trans_mat) self.cross_bar_v_line_item.setTransform(trans_mat) self.cross_bar_h_line_item.setTransform(trans_mat) self.paint_brush_rect_item.setTransform(trans_mat) self.paint_brush_circle_item.setTransform(trans_mat) self.fitInView(self.raw_img_item, Qt.KeepAspectRatio) self.paint_brush_circle_item.setVisible(False) self.paint_brush_rect_item.setVisible(False) @property def brush_stats(self): return self._brush_stats @brush_stats.setter def brush_stats(self, stats_tuple): b_type, size = stats_tuple if b_type in [ BRUSH_TYPE_NO_BRUSH, BRUSH_TYPE_CIRCLE_BRUSH, BRUSH_TYPE_RECT_BRUSH ]: self._brush_stats['type'] = b_type self._brush_stats['size'] = size if b_type != BRUSH_TYPE_NO_BRUSH: self.setMouseTracking(True) else: self.setMouseTracking(False) def update_brush_preview(self, x, y, out_of_sight=False): if not self.is_valid or self.brush_stats[ 'type'] == BRUSH_TYPE_NO_BRUSH or out_of_sight: self.paint_brush_rect_item.setVisible(False) self.paint_brush_circle_item.setVisible(False) return center = self.anno_img_item.mapFromScene(self.mapToScene(x, y)) start_x = self.raw_img_item.boundingRect().topLeft().x() start_y = self.raw_img_item.boundingRect().topLeft().y() end_x = self.raw_img_item.boundingRect().bottomRight().x() end_y = self.raw_img_item.boundingRect().bottomRight().y() center.setX(min(max(start_x, center.x()), end_x) + 0.5) center.setY(min(max(start_y, center.y()), end_y) + 0.5) top_left_x = int(center.x() - self.brush_stats['size'] / 2) top_left_y = int(center.y() - self.brush_stats['size'] / 2) rect = QRectF(top_left_x, top_left_y, self.brush_stats['size'], self.brush_stats['size']) if self.brush_stats['type'] == BRUSH_TYPE_CIRCLE_BRUSH: self.paint_brush_rect_item.setVisible(False) self.paint_brush_circle_item.setVisible(True) self.paint_brush_circle_item.setRect(rect) if self.brush_stats['type'] == BRUSH_TYPE_RECT_BRUSH: self.paint_brush_rect_item.setVisible(True) self.paint_brush_circle_item.setVisible(False) self.paint_brush_rect_item.setPolygon(QPolygonF(rect)) def anno_paint(self, x, y, erase=False, new_step=False): pos_on_item = self.raw_img_item.mapFromScene(self.mapToScene(x, y)) if self.objectName() == 'aGraphicsView': paint_point = [pos_on_item.y(), pos_on_item.x(), 999999] if self.objectName() == 'sGraphicsView': paint_point = [999999, pos_on_item.y(), pos_on_item.x()] if self.objectName() == 'cGraphicsView': paint_point = [pos_on_item.y(), 999999, pos_on_item.x()] self.paint_anno_on_point_signal.emit(math.floor(paint_point[0]), math.floor(paint_point[1]), math.floor(paint_point[2]), self.brush_stats['type'], self.brush_stats['size'], erase, new_step) @pyqtSlot('int') def on_slice_scroll_bar_changed(self, value): if not self.is_valid: return ratios = [-1, -1, -1] ratio = (value - self.slice_scroll_bar.minimum()) / \ (self.slice_scroll_bar.maximum() - self.slice_scroll_bar.minimum()) if self.objectName() == 'aGraphicsView': ratios[2] = ratio if self.objectName() == 'sGraphicsView': ratios[0] = ratio if self.objectName() == 'cGraphicsView': ratios[1] = ratio self.set_focus_point_percent_signal.emit(ratios[0], ratios[1], ratios[2]) @pyqtSlot('int', 'int') def set_brush_stats(self, b_type, size): self.brush_stats = [b_type, size] @pyqtSlot('int', 'int', 'int') def set_cross_bar(self, x, y, z): if self.objectName() == 'aGraphicsView': cross_bar_x = y cross_bar_y = x slice_bar_ratio = z / self.image_size[2] if self.objectName() == 'sGraphicsView': cross_bar_x = z cross_bar_y = y slice_bar_ratio = x / self.image_size[0] if self.objectName() == 'cGraphicsView': cross_bar_x = z cross_bar_y = x slice_bar_ratio = y / self.image_size[1] # cross line in voxel center cross_bar_x = cross_bar_x + 0.5 cross_bar_y = cross_bar_y + 0.5 start_x = self.raw_img_item.boundingRect().topLeft().x() start_y = self.raw_img_item.boundingRect().topLeft().y() end_x = self.raw_img_item.boundingRect().bottomRight().x() end_y = self.raw_img_item.boundingRect().bottomRight().y() self.cross_bar_v_line_item.setLine(cross_bar_x, start_y, cross_bar_x, end_y) self.cross_bar_h_line_item.setLine(start_x, cross_bar_y, end_x, cross_bar_y) slice_bar_value = round(slice_bar_ratio * (self.slice_scroll_bar.maximum() - self.slice_scroll_bar.minimum())) \ + self.slice_scroll_bar.minimum() self.slice_scroll_bar.setValue(slice_bar_value) def mousePressEvent(self, event: QtGui.QMouseEvent): self._last_button_press = event.button() if self.brush_stats['type'] == BRUSH_TYPE_NO_BRUSH: if event.button() == Qt.LeftButton: item_coord_pos = self.raw_img_item.mapFromScene( self.mapToScene(event.pos())) if self.objectName() == 'aGraphicsView': new_focus_point = [ item_coord_pos.y(), item_coord_pos.x(), 999999 ] if self.objectName() == 'sGraphicsView': new_focus_point = [ 999999, item_coord_pos.y(), item_coord_pos.x() ] if self.objectName() == 'cGraphicsView': new_focus_point = [ item_coord_pos.y(), 999999, item_coord_pos.x() ] self.set_focus_point_signal.emit( math.floor(new_focus_point[0]), math.floor(new_focus_point[1]), math.floor(new_focus_point[2])) elif event.button() == Qt.MiddleButton: self._last_pos_middle_button = event.pos() self.setCursor(Qt.ClosedHandCursor) elif event.button() == Qt.RightButton: self._last_pos_right_button = event.pos() else: super(ASCGraphicsView, self).mousePressEvent(event) if self.brush_stats['type'] in [ BRUSH_TYPE_CIRCLE_BRUSH, BRUSH_TYPE_RECT_BRUSH ]: if event.button() == Qt.LeftButton: self.anno_paint(event.x(), event.y(), erase=False, new_step=True) elif event.button() == Qt.MiddleButton: self._last_pos_middle_button = event.pos() self.setCursor(Qt.ClosedHandCursor) elif event.button() == Qt.RightButton: self.anno_paint(event.x(), event.y(), erase=True, new_step=True) def mouseMoveEvent(self, event: QtGui.QMouseEvent): if self.brush_stats['type'] == BRUSH_TYPE_NO_BRUSH: if self._last_button_press == Qt.LeftButton: item_coord_pos = self.raw_img_item.mapFromScene( self.mapToScene(event.pos())) if self.objectName() == 'aGraphicsView': new_focus_point = [ item_coord_pos.y(), item_coord_pos.x(), 999999 ] if self.objectName() == 'sGraphicsView': new_focus_point = [ 999999, item_coord_pos.y(), item_coord_pos.x() ] if self.objectName() == 'cGraphicsView': new_focus_point = [ item_coord_pos.y(), 999999, item_coord_pos.x() ] self.set_focus_point_signal.emit( math.floor(new_focus_point[0]), math.floor(new_focus_point[1]), math.floor(new_focus_point[2])) elif self._last_button_press == Qt.MiddleButton: delta_x = event.x() - self._last_pos_middle_button.x() delta_y = event.y() - self._last_pos_middle_button.y() self.horizontalScrollBar().setValue( self.horizontalScrollBar().value() - delta_x) self.verticalScrollBar().setValue( self.verticalScrollBar().value() - delta_y) self._last_pos_middle_button = event.pos() elif self._last_button_press == Qt.RightButton: delta = event.pos().y() - self._last_pos_right_button.y() scale = 1 - float(delta) / float(self.size().height()) self.scale_signal.emit(scale, scale) self._last_pos_right_button = event.pos() else: super(ASCGraphicsView, self).mouseMoveEvent(event) if self.brush_stats['type'] in [ BRUSH_TYPE_CIRCLE_BRUSH, BRUSH_TYPE_RECT_BRUSH ]: self.update_brush_preview(event.x(), event.y()) if self._last_button_press == Qt.LeftButton: self.anno_paint(event.x(), event.y(), erase=False, new_step=False) elif self._last_button_press == Qt.MiddleButton: delta_x = event.x() - self._last_pos_middle_button.x() delta_y = event.y() - self._last_pos_middle_button.y() self.horizontalScrollBar().setValue( self.horizontalScrollBar().value() - delta_x) self.verticalScrollBar().setValue( self.verticalScrollBar().value() - delta_y) self._last_pos_middle_button = event.pos() elif self._last_button_press == Qt.RightButton: self.anno_paint(event.x(), event.y(), erase=True, new_step=False) def mouseReleaseEvent(self, event: QtGui.QMouseEvent): self._last_button_press = Qt.NoButton if self.brush_stats['type'] == BRUSH_TYPE_NO_BRUSH: if event.button() == Qt.MiddleButton: self.setCursor(Qt.ArrowCursor) if self.brush_stats['type'] in [ BRUSH_TYPE_CIRCLE_BRUSH, BRUSH_TYPE_RECT_BRUSH ]: if event.button() == Qt.LeftButton: pass elif event.button() == Qt.RightButton: pass else: super(ASCGraphicsView, self).mouseReleaseEvent(event) def wheelEvent(self, event: QtGui.QWheelEvent): # super(ASCGraphicsView, self).wheelEvent(event) if self.objectName() == 'aGraphicsView': if event.angleDelta().y() > 0: self.move_focus_point_signal.emit(0, 0, -1) elif event.angleDelta().y() < 0: self.move_focus_point_signal.emit(0, 0, 1) if self.objectName() == 'sGraphicsView': if event.angleDelta().y() > 0: self.move_focus_point_signal.emit(-1, 0, 0) elif event.angleDelta().y() < 0: self.move_focus_point_signal.emit(1, 0, 0) if self.objectName() == 'cGraphicsView': if event.angleDelta().y() > 0: self.move_focus_point_signal.emit(0, -1, 0) elif event.angleDelta().y() < 0: self.move_focus_point_signal.emit(0, 1, 0) def leaveEvent(self, event: QtCore.QEvent): self._last_button_press = Qt.NoButton if self.brush_stats['type'] == BRUSH_TYPE_NO_BRUSH: super(ASCGraphicsView, self).leaveEvent(event) else: self.update_brush_preview(0, 0, out_of_sight=True)
class CallOut(): ''' This represents a callouton the diagram This class creates a text item and then surrounds it with a polygon that is a rectangle with a pointer to another object on the diagram. This can be used by hover over functions or be displayed as a part of a node or relationship ''' def __init__(self, scene, text, anchorPoint, diagramType, format): self.scene = scene self.model = self.scene.parent.model self.text = text self.anchorPoint = anchorPoint self.diagramType = diagramType self.format = format # initialize the two qgraphicsitems needed to draw a relationship to None self.itemText = None self.itemPolygon = None self.drawIt def name(self, ): return "no name" def NZID(self, ): return None def clearItem(self, ): if (not self.itemText is None and not self.itemText.scene() is None): self.itemText.scene().removeItem(self.itemText) if (not self.itemPolygon is None and not self.itemPolygon.scene() is None): self.itemPolygon.scene().removeItem(self.itemPolygon) def drawIt(self, ): ''' draw the callout ''' # if the polygon and text graphics items already exist on the scene then delete them self.clearItem() # draw the relationship arc pen = self.format.pen() brush = self.format.brush() # create text box # draw the text self.itemText = QGraphicsTextItem(self.relationInstance.relName, parent=None) self.itemText.setZValue(CALLOUTLAYER) self.itemText.setFlag(QGraphicsItem.ItemIsMovable, True) self.itemText.setFlag(QGraphicsItem.ItemIsSelectable, True) self.itemText.setSelected(False) self.itemText.setData(NODEID, self.relationInstance.NZID) self.itemText.setData(ITEMTYPE, CALLOUT) self.itemText.setHtml(self.genTextHTML()) # set the position of the text self.itemText.setPos(self.anchorPoint) # get the height and width of the text graphics item th = self.IRtext.boundingRect().height() tw = self.IRtext.boundingRect().width() #create an empty polygon arrowPolygon = QPolygonF() # add callout points arrowPolygon.append(self.anchorPoint) arrowPolygon.append( QPointF(self.anchorPoint.x() + tw, self.anchorPoint.y())) arrowPolygon.append( QPointF(self.anchorPoint.x() + tw, self.anchorPoint.y())) arrowPolygon.append( QPointF(self.anchorPoint.x() + tw, self.anchorPoint.y() + th)) arrowPolygon.append( QPointF(self.anchorPoint.x(), self.anchorPoint.y() + th)) self.itemPolygon = QGraphicsPolygonItem( arrowPolygon, parent=None, ) self.itemPolygon.setZValue(CALLOUTLAYER) self.itemPolygon.setBrush(brush) self.itemPolygon.setPen(pen) self.itemPolygon.setFlag(QGraphicsItem.ItemIsMovable, True) self.itemPolygon.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True) self.itemPolygon.setFlag(QGraphicsItem.ItemIsSelectable, False) self.itemPolygon.setSelected(False) # set data in the RelLine object self.IRel.setData(NODEID, self.relationInstance.NZID) self.IRel.setData(ITEMTYPE, RELINSTANCEARC) # add the polygon object to the scene self.scene.addItem(self.itemPolygon) # add text to the scene self.scene.addItem(self.itemText) def updateText(self, ): # # force the node instance to update its values in case it has been updated from another diagram or the tree view # self.relationInstance.reloadDictValues() # self.IRtext.setPlainText(self.relationInstance.relName) self.itemText.setHtml(self.genTextHTML()) def genTextHTML(self): '''generate html to display the text ''' prefix = '<html><body>' suffix = "</body></html>" myHTML = ('{}<p><font size="1"> [{}]</font></p>{}'.format( prefix, self.text, suffix)) return myHTML def moveIt(self, ): self.drawIt()