def _draw_grid(self, painter, rect, pen, grid_size): """ draws the grid lines in the scene. Args: painter (QtGui.QPainter): painter object. rect (QtCore.QRectF): rect object. pen (QtGui.QPen): pen object. grid_size (int): grid size. """ left = int(rect.left()) right = int(rect.right()) top = int(rect.top()) bottom = int(rect.bottom()) first_left = left - (left % grid_size) first_top = top - (top % grid_size) lines = [] lines.extend([ QtCore.QLineF(x, top, x, bottom) for x in range(first_left, right, grid_size) ]) lines.extend([ QtCore.QLineF(left, y, right, y) for y in range(first_top, bottom, grid_size) ]) painter.setPen(pen) painter.drawLines(lines)
def graphicsMouseMoveEvent(self, event, itemId=0): """This function is called by the owned :class:`~ts2.scenery.helper.TrackGraphicsItem` to handle its :meth:`~ts2.scenery.helper.TrackGraphicsItem.mouseMoveEvent`. Reimplemented in the ResizableItem class to begin a drag operation on corners.""" if event.buttons() == Qt.LeftButton and \ self.simulation.context == utils.Context.EDITOR_SCENERY: if QtCore.QLineF(event.scenePos(), event.buttonDownScenePos( Qt.LeftButton)).length() < 3.0: return drag = QtGui.QDrag(event.widget()) mime = QtCore.QMimeData() pos = event.buttonDownScenePos(Qt.LeftButton) - self.origin if QtCore.QRectF(-5, -5, 9, 9).contains(pos): movedEnd = "start" elif QtCore.QRectF(self.end.x() - self.origin.x() - 5, self.end.y() - self.origin.y() - 5, 9, 9).contains(pos): movedEnd = "end" pos -= self.end - self.origin # elif self._gi[itemId].shape().contains(pos): else: movedEnd = "origin" if movedEnd is not None: mime.setText( type(self).__name__ + "#" + str(self.tiId) + "#" + str(pos.x()) + "#" + str(pos.y()) + "#" + movedEnd) drag.setMimeData(mime) drag.exec_()
def _update_line(self): """Resolve start and end point from current source and target position :returns: A Qt line object :rtype: :class:`QtCore.QLineF` """ start = QtCore.QPointF(0, 0) end = self._target_slot.center - self._source_slot.center self._line = QtCore.QLineF(start, end)
def _update_line(self): """Re-implement function that updates edge line definition """ start = QtCore.QPoint(0, 0) if self._source_slot.family & NodeSlot.OUTPUT: end = self._mouse_pos - self._source_slot.center else: end = self._source_slot.center - self._mouse_pos self._line = QtCore.QLineF(start, end)
def __init__(self, parameters): """Constructor for the LineItem class""" self._placeCode = "" self._trackCode = "" self._realLength = "" super().__init__(parameters) self.defaultZValue = 1 self._line = QtCore.QLineF() self._boundingRect = QtCore.QRectF() self.updateGeometry() gli = helper.TrackGraphicsItem(self) gli.setPos(self._origin) gli.setZValue(self.defaultZValue) self._gi[0] = gli self._tli = []
def drawTrain(self): """Draws the train(s) on the line, if any""" tlines = [] if self.simulation.context == utils.Context.GAME: for i in range(len(self._trainHeads)): tlines.append( QtCore.QLineF( self.sceneLine.pointAt(self._trainHeads[i] / self._realLength), self.sceneLine.pointAt(self._trainTails[i] / self._realLength))) if tlines[i].length() < 5.0 and self._trainTails[i] != 0: # Make sure that the train representation is always # at least 5 pixel long. tlines[i].setLength( min(5.0, (1 - self._trainTails[i] / self._realLength) * self.sceneLine.length())) self.showTrainLineItem(tlines)
def graphicsMouseMoveEvent(self, event, itemId=0): """This function is called by the owned TrackGraphicsItem to handle its mouseMoveEvent. The implementation in the base class TrackItem begins a drag operation.""" if itemId == 0: if (event.buttons() == Qt.LeftButton and self.simulation.context == utils.Context.EDITOR_SCENERY): if QtCore.QLineF(event.scenePos(), event.buttonDownScenePos( Qt.LeftButton)).length() < 3.0: return drag = QtGui.QDrag(event.widget()) mime = QtCore.QMimeData() pos = event.buttonDownScenePos(Qt.LeftButton) - self.origin mime.setText( type(self).__name__ + "#" + str(self.tiId) + "#" + str(pos.x()) + "#" + str(pos.y()) + "#" + "origin") drag.setMimeData(mime) drag.exec_()
def updateGeometry(self): """Updates the internal representation of the line and boundingRect when it has been modified""" orig = QtCore.QPointF(0, 0) end = orig + self.end - self.origin self._line = QtCore.QLineF(orig, end) x1 = self._line.p1().x() x2 = self._line.p2().x() y1 = self._line.p1().y() y2 = self._line.p2().y() lx = min(x1, x2) - 5.0 rx = max(x1, x2) + 5.0 ty = min(y1, y2) - 5.0 by = max(y1, y2) + 5.0 if self.tiId.startswith("__EDITOR__"): # Library item in editor lx -= 15 rx += 15 ty -= 20 by += 20 self._boundingRect = QtCore.QRectF(lx, ty, rx - lx, by - ty)
def rebuild_line(self): src_node = self.view.get_node_graphic(self.src_node_path) tgt_node = self.view.get_node_graphic(self.tgt_node_path) if src_node: self.src_pos = src_node.get_attr_out_pos(self.src_attr_name, scene=True) if not self.src_pos: exec_attr = nxt_node.INTERNAL_ATTRS.EXECUTE_IN self.src_pos = src_node.get_attr_out_pos(exec_attr, scene=True) else: self.src_pos = self.view.mouse_scene_pos if tgt_node: self.tgt_pos = tgt_node.get_attr_in_pos(self.tgt_attr_name, scene=True) if not self.tgt_pos: exec_attr = nxt_node.INTERNAL_ATTRS.EXECUTE_IN self.tgt_pos = tgt_node.get_attr_in_pos(exec_attr, scene=True) else: self.tgt_pos = self.view.mouse_scene_pos self.setLine(QtCore.QLineF(self.src_pos, self.tgt_pos)) # I don't want this self.update()
def draw_path(self, start_port, end_port=None, cursor_pos=None): """ Draws the path between ports. Args: start_port (PortItem): port used to draw the starting point. end_port (PortItem): port used to draw the end point. cursor_pos (QtCore.QPointF): cursor position if specified this will be the draw end point. """ if not start_port: return pos1 = start_port.scenePos() pos1.setX(pos1.x() + (start_port.boundingRect().width() / 2)) pos1.setY(pos1.y() + (start_port.boundingRect().height() / 2)) if cursor_pos: pos2 = cursor_pos elif end_port: pos2 = end_port.scenePos() pos2.setX(pos2.x() + (start_port.boundingRect().width() / 2)) pos2.setY(pos2.y() + (start_port.boundingRect().height() / 2)) else: return line = QtCore.QLineF(pos1, pos2) path = QtGui.QPainterPath() path.moveTo(line.x1(), line.y1()) if self.viewer_pipe_layout() == PIPE_LAYOUT_STRAIGHT: path.lineTo(pos2) self.setPath(path) return else: if NODE_LAYOUT_DIRECTION is NODE_LAYOUT_VERTICAL: self.__draw_path_vertical(start_port, pos1, pos2, path) elif NODE_LAYOUT_DIRECTION is NODE_LAYOUT_HORIZONTAL: self.__draw_path_horizontal(start_port, pos1, pos2, path)
def sceneLine(self): """Returns the line as a QLineF in the scene's coordinates.""" return QtCore.QLineF(self.origin, self.end)
def drawBackground(self, painter, rect): super(CanvasBase, self).drawBackground(painter, rect) lod = self.getCanvasLodValueFromCurrentScale() self.boundingRect = rect polygon = self.mapToScene(self.viewport().rect()) painter.fillRect(rect, QtGui.QBrush(editableStyleSheet().CanvasBgColor)) left = int(rect.left()) - (int(rect.left()) % editableStyleSheet().GridSizeFine[0]) top = int(rect.top()) - (int(rect.top()) % editableStyleSheet().GridSizeFine[0]) if editableStyleSheet().DrawGrid[0] >= 1: if lod < editableStyleSheet().CanvasSwitch[0]: # Draw horizontal fine lines gridLines = [] y = float(top) while y < float(rect.bottom()): gridLines.append( QtCore.QLineF(rect.left(), y, rect.right(), y)) y += editableStyleSheet().GridSizeFine[0] painter.setPen( QtGui.QPen(editableStyleSheet().CanvasGridColor, 1)) painter.drawLines(gridLines) # Draw vertical fine lines gridLines = [] x = float(left) while x < float(rect.right()): gridLines.append( QtCore.QLineF(x, rect.top(), x, rect.bottom())) x += editableStyleSheet().GridSizeFine[0] painter.setPen( QtGui.QPen(editableStyleSheet().CanvasGridColor, 1)) painter.drawLines(gridLines) # Draw thick grid left = int(rect.left()) - (int(rect.left()) % editableStyleSheet().GridSizeHuge[0]) top = int(rect.top()) - (int(rect.top()) % editableStyleSheet().GridSizeHuge[0]) # Draw vertical thick lines gridLines = [] painter.setPen( QtGui.QPen(editableStyleSheet().CanvasGridColorDarker, 1.5)) x = left while x < rect.right(): gridLines.append(QtCore.QLineF(x, rect.top(), x, rect.bottom())) x += editableStyleSheet().GridSizeHuge[0] painter.drawLines(gridLines) # Draw horizontal thick lines gridLines = [] painter.setPen( QtGui.QPen(editableStyleSheet().CanvasGridColorDarker, 1.5)) y = top while y < rect.bottom(): gridLines.append(QtCore.QLineF(rect.left(), y, rect.right(), y)) y += editableStyleSheet().GridSizeHuge[0] painter.drawLines(gridLines) if editableStyleSheet().DrawNumbers[0] >= 1: # draw numbers scale = self.currentViewScale() f = painter.font() f.setPointSize(6 / min(scale, 1)) f.setFamily("Consolas") painter.setFont(f) y = float(top) while y < float(rect.bottom()): y += editableStyleSheet().GridSizeHuge[0] inty = int(y) if y > top + 30: painter.setPen( QtGui.QPen( editableStyleSheet().CanvasGridColorDarker.lighter( 300))) painter.drawText(rect.left(), y - 1.0, str(inty)) x = float(left) while x < rect.right(): x += editableStyleSheet().GridSizeHuge[0] intx = int(x) if x > left + 30: painter.setPen( QtGui.QPen( editableStyleSheet().CanvasGridColorDarker.lighter( 300))) painter.drawText(x, rect.top() + painter.font().pointSize(), str(intx))
def paint(self, p: QtGui.QPainter, option: QtWidgets.QGraphicsItem.ItemIsSelectable, widget: QtWidgets.QWidget): # pylint: disable=unused-argument # Set color when selected if option.state & QtWidgets.QStyle.State_Selected: fillColor = self.color.darker(150) else: fillColor = self.color # Lighten if mouse over if option.state & QtWidgets.QStyle.State_MouseOver: fillColor = fillColor.lighter(125) # Calculate level of detail lod = option.levelOfDetailFromTransform(p.worldTransform()) if lod < 0.125: p.fillRect(QtCore.QRectF(0, 0, 110, 70), fillColor) return if lod < 0.2: oldBrush = p.brush() p.setBrush(fillColor) p.drawRect(13, 13, 97, 57) p.setBrush(oldBrush) return oldPen = p.pen() oldBrush = p.brush() # Use a copy of the old pen pen = QtGui.QPen(oldPen) if option.state & QtWidgets.QStyle.State_Selected: pen.setWidth(2) else: pen.setWidth(0) p.setPen(pen) # Draw the inside of the chip if option.state & QtWidgets.QStyle.State_Sunken: p.setBrush(QtGui.QBrush(fillColor.darker(120))) else: p.setBrush(QtGui.QBrush(fillColor.darker(100))) p.drawRect(QtCore.QRect(14, 14, 79, 39)) p.setBrush(oldBrush) if lod >= 1: p.setPen(QtGui.QPen(QtCore.Qt.gray, 1)) p.drawLine(15, 54, 94, 54) p.drawLine(94, 53, 94, 15) p.setPen(QtGui.QPen(QtCore.Qt.black, 0)) if lod >= 2: font = QtGui.QFont("Times", 10) font.setStyleStrategy(QtGui.QFont.ForceOutline) p.setFont(font) p.save() p.scale(0.1, 0.1) p.drawText(170, 180, f"Model: VSC-2000 (Very Small Chip) at {self.x}x{self.y}") p.drawText(170, 200, "Serial number: DLWR-WEER-123L-ZZ33-SDSJ") p.drawText(170, 220, "Manufacturer: Chip Manufacturer") p.restore() lines = [] if lod >= 0.5: for i in range(0, 11, 1 if lod > 0.5 else 2): lines.append(QtCore.QLineF(18 + 7*i, 13, 18 + 7*i, 5)) lines.append(QtCore.QLineF(18 + 7*i, 54, 18 + 7*i, 62)) for i in range(0, 7, 1 if lod > 0.5 else 2): lines.append(QtCore.QLineF(5, 18 + i*5, 13, 18 + i*5)) lines.append(QtCore.QLineF(94, 18 + i*5, 102, 18 + i*5)) if lod >= 0.4: lines.append(QtCore.QLineF(25, 35, 35, 35)) lines.append(QtCore.QLineF(35, 30, 35, 40)) lines.append(QtCore.QLineF(35, 30, 45, 35)) lines.append(QtCore.QLineF(35, 40, 45, 35)) lines.append(QtCore.QLineF(45, 30, 45, 40)) lines.append(QtCore.QLineF(45, 35, 55, 35)) p.drawLines(lines) # Draw red ink if self.stuff: p.setPen(QtGui.QPen(QtCore.Qt.red, 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)) p.setBrush(QtCore.Qt.NoBrush) path = QtGui.QPainterPath() path.moveTo(self.stuff[0]) for point in self.stuff[1:]: path.lineTo(point) p.drawPath(path) p.setPen(oldPen) p.setBrush(oldBrush)