def __init__(self, x, y, scaleFactor, text, obj, v, parent): """ Default class constructor. :param `x`: TOWRITE :type `x`: qreal :param `y`: TOWRITE :type `y`: qreal :param `scaleFactor`: TOWRITE :type `scaleFactor`: qreal :param `text`: TOWRITE :type `text`: QString :param `obj`: TOWRITE :type `obj`: `BaseObject` :param `v`: TOWRITE :type `v`: `View` :param `parent`: TOWRITE :type `parent`: `QUndoCommand`_ """ super(UndoableScaleCommand, self).__init__(parent) self.gview = v self.object = obj self.setText(text) # Prevent division by zero and other wacky behavior if scaleFactor <= 0.0: self.dx = 0.0 self.dy = 0.0 self.factor = 1.0 QMessageBox.critical( 0, QObject.tr("ScaleFactor Error"), QObject. tr("Hi there. If you are not a developer, report this as a bug. " "If you are a developer, your code needs examined, and possibly your head too." )) else: # Calculate the offset oldX = self.object.x() # qreal oldY = self.object.y() # qreal scaleLine = QLineF(x, y, oldX, oldY) scaleLine.setLength(scaleLine.length() * scaleFactor) newX = scaleLine.x2() # qreal newY = scaleLine.y2() # qreal self.dx = newX - oldX self.dy = newY - oldY self.factor = scaleFactor
def __init__(self, x, y, scaleFactor, text, obj, v, parent=None): """ Default class constructor. :param `x`: TOWRITE :type `x`: qreal :param `y`: TOWRITE :type `y`: qreal :param `scaleFactor`: TOWRITE :type `scaleFactor`: qreal :param `text`: TOWRITE :type `text`: QString :param `obj`: TOWRITE :type `obj`: `BaseObject` :param `v`: TOWRITE :type `v`: `View` :param `parent`: TOWRITE :type `parent`: `QUndoCommand`_ """ super(UndoableScaleCommand, self).__init__(parent) self.gview = v self.object = obj self.setText(text) # Prevent division by zero and other wacky behavior if scaleFactor <= 0.0: self.dx = 0.0 self.dy = 0.0 self.factor = 1.0 QMessageBox.critical(0, QObject.tr("ScaleFactor Error"), QObject.tr("Hi there. If you are not a developer, report this as a bug. " "If you are a developer, your code needs examined, and possibly your head too.")) else: # Calculate the offset oldX = self.object.x() # qreal oldY = self.object.y() # qreal scaleLine = QLineF(x, y, oldX, oldY) scaleLine.setLength(scaleLine.length() * scaleFactor) newX = scaleLine.x2() # qreal newY = scaleLine.y2() # qreal self.dx = newX - oldX self.dy = newY - oldY self.factor = scaleFactor
def renewplot(self): """ Do not layout anything, but redraw all lines""" scene = self.graphicsView.scene() self.roots = set() # scene.changed.disconnect(self.renewplot) for i in self.qLines: scene.removeItem(i) self.qLines = [] for edge in self.gv.edges_iter(): qnode1 = self.nodesToQNodes[edge[0]] qnode2 = self.nodesToQNodes[edge[1]] line = QLineF(qnode1.pos(), qnode2.pos()) line.setLength(line.length() - 40) end = line.p2() arrowLine1 = QLineF() arrowLine1.setP1(end) arrowLine1.setLength(10) arrowLine1.setAngle(line.angle() + 210) arrowLine2 = QLineF() arrowLine2.setP1(end) arrowLine2.setLength(10) arrowLine2.setAngle(line.angle() - 210) if edge.attr['color'] not in self.qpens: self.qpens[edge.attr['color']] = QPen( QColor(edge.attr['color'])) item = scene.addLine(line, self.qpens[edge.attr['color']]) item.setZValue(-1) item.setFlag(QGraphicsItem.ItemIsSelectable, True) self.qLines.append(item) item = scene.addLine(arrowLine1, self.qpens[edge.attr['color']]) self.qLines.append(item) item = scene.addLine(arrowLine2, self.qpens[edge.attr['color']]) self.qLines.append(item) self.roots.add(edge[0])
def angle_arrow(self, path, origin='head'): ''' Compute the two points of the arrow head with the right angle ''' if origin == 'tail': path = path.toReversed() length = path.length() percent = path.percentAtLength(length - 10.0) src = path.pointAtPercent(percent) #path.moveTo(path.pointAtPercent(1)) end_point = path.pointAtPercent(1) #end_point = path.currentPosition() line = QLineF(src, end_point) angle = math.acos(line.dx() / (line.length() or 1)) if line.dy() >= 0: angle = math.pi * 2 - angle arrow_size = 10.0 arrow_p1 = end_point + QPointF( math.sin(angle - math.pi / 3) * arrow_size, math.cos(angle - math.pi / 3) * arrow_size) arrow_p2 = end_point + QPointF( math.sin(angle - math.pi + math.pi / 3) * arrow_size, math.cos(angle - math.pi + math.pi / 3) * arrow_size) return (arrow_p1, arrow_p2)
def updateRubber(self, painter=None): """ TOWRITE :param `painter`: TOWRITE :type `painter`: `QPainter`_ """ rubberMode = self.objectRubberMode() # int if rubberMode == OBJ_RUBBER_CIRCLE_1P_RAD: sceneCenterPoint = self.objectRubberPoint( "CIRCLE_CENTER") # QPointF sceneQSnapPoint = self.objectRubberPoint( "CIRCLE_RADIUS") # QPointF itemCenterPoint = self.mapFromScene(sceneCenterPoint) # QPointF itemQSnapPoint = self.mapFromScene(sceneQSnapPoint) # QPointF itemLine = QLineF(itemCenterPoint, itemQSnapPoint) self.setObjectCenter(sceneCenterPoint) sceneLine = QLineF(sceneCenterPoint, sceneQSnapPoint) radius = sceneLine.length() # qreal self.setObjectRadius(radius) if painter: self.drawRubberLine(itemLine, painter, "VIEW_COLOR_CROSSHAIR") self.updatePath() elif rubberMode == OBJ_RUBBER_CIRCLE_1P_DIA: sceneCenterPoint = self.objectRubberPoint( "CIRCLE_CENTER") # QPointF sceneQSnapPoint = self.objectRubberPoint( "CIRCLE_DIAMETER") # QPointF itemCenterPoint = self.mapFromScene(sceneCenterPoint) # QPointF itemQSnapPoint = self.mapFromScene(sceneQSnapPoint) # QPointF itemLine = QLineF(itemCenterPoint, itemQSnapPoint) self.setObjectCenter(sceneCenterPoint) sceneLine = QLineF(sceneCenterPoint, sceneQSnapPoint) diameter = sceneLine.length() # qreal self.setObjectDiameter(diameter) if painter: self.drawRubberLine(itemLine, painter, "VIEW_COLOR_CROSSHAIR") self.updatePath() elif rubberMode == OBJ_RUBBER_CIRCLE_2P: sceneTan1Point = self.objectRubberPoint("CIRCLE_TAN1") # QPointF sceneQSnapPoint = self.objectRubberPoint("CIRCLE_TAN2") # QPointF sceneLine = QLineF(sceneTan1Point, sceneQSnapPoint) self.setObjectCenter(sceneLine.pointAt(0.5)) diameter = sceneLine.length() # qreal self.setObjectDiameter(diameter) self.updatePath() elif rubberMode == OBJ_RUBBER_CIRCLE_3P: sceneTan1Point = self.objectRubberPoint("CIRCLE_TAN1") # QPointF sceneTan2Point = self.objectRubberPoint("CIRCLE_TAN2") # QPointF sceneTan3Point = self.objectRubberPoint("CIRCLE_TAN3") # QPointF #TODO/PORT# double sceneCenterX #TODO/PORT# double sceneCenterY #TODO/PORT# getArcCenter(sceneTan1Point.x(), sceneTan1Point.y(), #TODO/PORT# sceneTan2Point.x(), sceneTan2Point.y(), #TODO/PORT# sceneTan3Point.x(), sceneTan3Point.y(), #TODO/PORT# &sceneCenterX, &sceneCenterY) sceneCenterPoint = QPointF(sceneCenterX, sceneCenterY) sceneLine = QLineF(sceneCenterPoint, sceneTan3Point) self.setObjectCenter(sceneCenterPoint) radius = sceneLine.length() # qreal self.setObjectRadius(radius) self.updatePath() elif rubberMode == OBJ_RUBBER_GRIP: if painter: gripPoint = self.objectRubberPoint("GRIP_POINT") # QPointF if gripPoint == self.objectCenter(): painter.drawEllipse(self.rect().translated( self.mapFromScene(self.objectRubberPoint('')) - self.mapFromScene(gripPoint))) else: gripRadius = QLineF( self.objectCenter(), self.objectRubberPoint('')).length() # qreal painter.drawEllipse(QPointF(), gripRadius, gripRadius) rubLine = QLineF(self.mapFromScene(gripPoint), self.mapFromScene(self.objectRubberPoint(''))) self.drawRubberLine(rubLine, painter, "VIEW_COLOR_CROSSHAIR")
def updateRubber(self, painter=None): """ TOWRITE :param `painter`: TOWRITE :type `painter`: `QPainter`_ """ rubberMode = self.objectRubberMode() # int if rubberMode == OBJ_RUBBER_CIRCLE_1P_RAD: sceneCenterPoint = self.objectRubberPoint("CIRCLE_CENTER") # QPointF sceneQSnapPoint = self.objectRubberPoint("CIRCLE_RADIUS") # QPointF itemCenterPoint = self.mapFromScene(sceneCenterPoint) # QPointF itemQSnapPoint = self.mapFromScene(sceneQSnapPoint) # QPointF itemLine = QLineF(itemCenterPoint, itemQSnapPoint) self.setObjectCenter(sceneCenterPoint) sceneLine = QLineF(sceneCenterPoint, sceneQSnapPoint) radius = sceneLine.length() # qreal self.setObjectRadius(radius) if painter: self.drawRubberLine(itemLine, painter, "VIEW_COLOR_CROSSHAIR") self.updatePath() elif rubberMode == OBJ_RUBBER_CIRCLE_1P_DIA: sceneCenterPoint = self.objectRubberPoint("CIRCLE_CENTER") # QPointF sceneQSnapPoint = self.objectRubberPoint("CIRCLE_DIAMETER") # QPointF itemCenterPoint = self.mapFromScene(sceneCenterPoint) # QPointF itemQSnapPoint = self.mapFromScene(sceneQSnapPoint) # QPointF itemLine = QLineF(itemCenterPoint, itemQSnapPoint) self.setObjectCenter(sceneCenterPoint) sceneLine = QLineF(sceneCenterPoint, sceneQSnapPoint) diameter = sceneLine.length() # qreal self.setObjectDiameter(diameter) if painter: self.drawRubberLine(itemLine, painter, "VIEW_COLOR_CROSSHAIR") self.updatePath() elif rubberMode == OBJ_RUBBER_CIRCLE_2P: sceneTan1Point = self.objectRubberPoint("CIRCLE_TAN1") # QPointF sceneQSnapPoint = self.objectRubberPoint("CIRCLE_TAN2") # QPointF sceneLine = QLineF(sceneTan1Point, sceneQSnapPoint) self.setObjectCenter(sceneLine.pointAt(0.5)) diameter = sceneLine.length() # qreal self.setObjectDiameter(diameter) self.updatePath() elif rubberMode == OBJ_RUBBER_CIRCLE_3P: sceneTan1Point = self.objectRubberPoint("CIRCLE_TAN1") # QPointF sceneTan2Point = self.objectRubberPoint("CIRCLE_TAN2") # QPointF sceneTan3Point = self.objectRubberPoint("CIRCLE_TAN3") # QPointF #TODO/PORT# double sceneCenterX #TODO/PORT# double sceneCenterY #TODO/PORT# getArcCenter(sceneTan1Point.x(), sceneTan1Point.y(), #TODO/PORT# sceneTan2Point.x(), sceneTan2Point.y(), #TODO/PORT# sceneTan3Point.x(), sceneTan3Point.y(), #TODO/PORT# &sceneCenterX, &sceneCenterY) sceneCenterPoint = QPointF(sceneCenterX, sceneCenterY) sceneLine = QLineF(sceneCenterPoint, sceneTan3Point) self.setObjectCenter(sceneCenterPoint) radius = sceneLine.length() # qreal self.setObjectRadius(radius) self.updatePath() elif rubberMode == OBJ_RUBBER_GRIP: if painter: gripPoint = self.objectRubberPoint("GRIP_POINT") # QPointF if gripPoint == self.objectCenter(): painter.drawEllipse(self.rect().translated(self.mapFromScene(self.objectRubberPoint('')) - self.mapFromScene(gripPoint))) else: gripRadius = QLineF(self.objectCenter(), self.objectRubberPoint('')).length() # qreal painter.drawEllipse(QPointF(), gripRadius, gripRadius) rubLine = QLineF(self.mapFromScene(gripPoint), self.mapFromScene(self.objectRubberPoint(''))) self.drawRubberLine(rubLine, painter, "VIEW_COLOR_CROSSHAIR")