def drawBackground(self, painter, rect): super(GridView, self).drawBackground(painter, rect) if self._show_grid: scene_rect = self.sceneRect() left = int(scene_rect.left()) - (int(scene_rect.left()) % self._draw_grid_size) top = int(scene_rect.top()) - (int(scene_rect.top()) % self._draw_grid_size) scale_mult = 1.0 lines = list() left_line = rect.left() - rect.left() % self._grid_size top_line = rect.top() - rect.top() % self._grid_size i = int(left_line) while i < int(rect.right()): lines.append(QLineF(i, rect.top(), i, rect.bottom())) i += self._grid_size u = int(top_line) while u < int(rect.bottom()): lines.append(QLineF(rect.left(), u, rect.right(), u)) u += self._grid_size # TODO: Change pen to a class variable (avoid to create a pen each drawing frame) pen = QPen() pen.setWidth(0) pen.setColor(QColor(20, 20, 20)) painter.setPen(pen) painter.drawLines(lines)
def _create_controls(self): self._control_points = list() points = [ QPoint(0, 0), QPoint(self._base_size / 4.0, 0), QPoint(self._base_size - (self._base_size / 4.0), self._base_size), QPoint(self._base_size, self._base_size)] for point in points: control_point = CurveNodeItem() self.addItem(control_point) control_point.setPos(point - QPoint(5, 5)) self._control_points.append(control_point) self._point_objects.append(control_point) control_point.signals.curveUpdated.connect(self.curveUpdated.emit) self._control_points[0].lock_x_pos = -5.0 self._control_points[-1].lock_x_pos = (self._base_size - 5.0) self._bezier_curve = BezierCurveItem() self._bezier_curve.update_path(points) self.addItem(self._bezier_curve) start_connector = self.addLine( QLineF(40, 40, 80, 80), QPen(Qt.white, 1, Qt.DashLine)) start_connector.setZValue(-2) end_connector = self.addLine( QLineF(40, 40, 80, 80), QPen(Qt.white, 1, Qt.DashLine)) end_connector.setZValue(-2) self._control_points[0].add_line(start_connector, is_point1=True) self._control_points[1].add_line(start_connector, is_point1=False) self._control_points[-1].add_line(end_connector, is_point1=False) self._control_points[-2].add_line(end_connector, is_point1=True)
def _move_line_to_center(self, new_pos): if not self._line: return x_offset = self._rect.x() - self._rect.width() / 2 y_offset = self._rect.y() - self._rect.height() / 2 new_center_pos = QPointF(new_pos.x() - x_offset, new_pos.y() - y_offset) p1 = new_center_pos if self._is_point1 else self._line.line().p1() p2 = self._line.line().p2() if self._is_point1 else new_center_pos self._line.setLine(QLineF(p1, p2))
def create_drop_indicator_pixmap(palette, size, drop_area): border_color = palette.color(QPalette.Active, QPalette.Highlight) background_color = palette.color(QPalette.Active, QPalette.Base) area_background_color = palette.color(QPalette.Active, QPalette.Highlight).lighter(150) pm = QPixmap(size.width(), size.height()) pm.fill(QColor(0, 0, 0, 0)) painter = QPainter(pm) pen = painter.pen() base_rect = QRectF(pm.rect()) painter.fillRect(base_rect, background_color) painter.save() area_rect = QRectF() area_line = QLineF() gradient = QLinearGradient() if drop_area == DropArea.TopDropArea: area_rect = QRectF(base_rect.x(), base_rect.y(), base_rect.width(), base_rect.height() * 0.5) area_line = QLineF(area_rect.bottomLeft(), area_rect.bottomRight()) gradient.setStart(area_rect.topLeft()) gradient.setFinalStop(area_rect.bottomLeft()) gradient.setColorAt(0, area_background_color) gradient.setColorAt(1, area_background_color.lighter(120)) elif drop_area == DropArea.RightDropArea: area_rect = QRectF(base_rect.width() * 0.5, base_rect.y(), base_rect.width() * 0.5, base_rect.height()) area_line = QLineF(area_rect.topLeft(), area_rect.bottomLeft()) gradient.setStart(area_rect.topLeft()) gradient.setFinalStop(area_rect.topRight()) gradient.setColorAt(0, area_background_color.lighter(120)) gradient.setColorAt(1, area_background_color) elif drop_area == DropArea.BottomDropArea: area_rect = QRectF(base_rect.x(), base_rect.height() * 0.5, base_rect.width(), base_rect.height() * 0.5) area_line = QLineF(area_rect.topLeft(), area_rect.topRight()) gradient.setStart(area_rect.topLeft()) gradient.setFinalStop(area_rect.bottomLeft()) gradient.setColorAt(0, area_background_color.lighter(120)) gradient.setColorAt(1, area_background_color) elif drop_area == DropArea.LeftDropArea: area_rect = QRectF(base_rect.x(), base_rect.y(), base_rect.width() * 0.5, base_rect.height()) area_line = QLineF(area_rect.topRight(), area_rect.bottomRight()) gradient.setStart(area_rect.topLeft()) gradient.setFinalStop(area_rect.topRight()) gradient.setColorAt(0, area_background_color) gradient.setColorAt(1, area_background_color.lighter(120)) if area_rect.isValid(): painter.fillRect(area_rect, gradient) pen = painter.pen() pen.setColor(border_color) pen.setStyle(Qt.DashLine) painter.setPen(pen) painter.drawLine(area_line) painter.restore() painter.save() pen = painter.pen() pen.setColor(border_color) pen.setWidth(1) painter.setPen(pen) painter.drawRect(base_rect.adjusted(0, 0, -pen.width(), -pen.width())) painter.restore() return pm
def drawOneStep(self, step): """ :type step: tuple """ if step[0] == ACTION_RECT: self.graphicsScene.addRect( QRectF(QPointF(step[1], step[2]), QPointF(step[3], step[4])), step[5]) elif step[0] == ACTION_ELLIPSE: self.graphicsScene.addEllipse( QRectF(QPointF(step[1], step[2]), QPointF(step[3], step[4])), step[5]) elif step[0] == ACTION_ARROW: arrow = QPolygonF() linex = float(step[1] - step[3]) liney = float(step[2] - step[4]) line = sqrt(pow(linex, 2) + pow(liney, 2)) # in case to divided by 0 if line == 0: return sinAngel = liney / line cosAngel = linex / line # sideLength is the length of bottom side of the body of an arrow # arrowSize is the size of the head of an arrow, left and right # sides' size is arrowSize, and the bottom side's size is arrowSize / 2 sideLength = step[5].width() arrowSize = 8 bottomSize = arrowSize / 2 tmpPoint = QPointF(step[3] + arrowSize * sideLength * cosAngel, step[4] + arrowSize * sideLength * sinAngel) point1 = QPointF(step[1] + sideLength * sinAngel, step[2] - sideLength * cosAngel) point2 = QPointF(step[1] - sideLength * sinAngel, step[2] + sideLength * cosAngel) point3 = QPointF(tmpPoint.x() - sideLength * sinAngel, tmpPoint.y() + sideLength * cosAngel) point4 = QPointF(tmpPoint.x() - bottomSize * sideLength * sinAngel, tmpPoint.y() + bottomSize * sideLength * cosAngel) point5 = QPointF(step[3], step[4]) point6 = QPointF(tmpPoint.x() + bottomSize * sideLength * sinAngel, tmpPoint.y() - bottomSize * sideLength * cosAngel) point7 = QPointF(tmpPoint.x() + sideLength * sinAngel, tmpPoint.y() - sideLength * cosAngel) arrow.append(point1) arrow.append(point2) arrow.append(point3) arrow.append(point4) arrow.append(point5) arrow.append(point6) arrow.append(point7) arrow.append(point1) self.graphicsScene.addPolygon(arrow, step[5], step[6]) elif step[0] == ACTION_LINE: self.graphicsScene.addLine( QLineF(QPointF(step[1], step[2]), QPointF(step[3], step[4])), step[5]) elif step[0] == ACTION_FREEPEN: self.graphicsScene.addPath(step[1], step[2]) elif step[0] == ACTION_TEXT: textAdd = self.graphicsScene.addSimpleText(step[1], step[2]) textAdd.setPos(step[3]) textAdd.setBrush(QBrush(step[4])) self.textRect = textAdd.boundingRect()
def drawMagnifier(self): # First, calculate the magnifier position due to the mouse position watchAreaWidth = 16 watchAreaHeight = 16 watchAreaPixmap = QPixmap() cursor_pos = self.mousePoint watchArea = QRect( QPoint(cursor_pos.x() - watchAreaWidth / 2, cursor_pos.y() - watchAreaHeight / 2), QPoint(cursor_pos.x() + watchAreaWidth / 2, cursor_pos.y() + watchAreaHeight / 2)) if watchArea.left() < 0: watchArea.moveLeft(0) watchArea.moveRight(watchAreaWidth) if self.mousePoint.x() + watchAreaWidth / 2 >= self.screenPixel.width( ): watchArea.moveRight(self.screenPixel.width() - 1) watchArea.moveLeft(watchArea.right() - watchAreaWidth) if self.mousePoint.y() - watchAreaHeight / 2 < 0: watchArea.moveTop(0) watchArea.moveBottom(watchAreaHeight) if self.mousePoint.y( ) + watchAreaHeight / 2 >= self.screenPixel.height(): watchArea.moveBottom(self.screenPixel.height() - 1) watchArea.moveTop(watchArea.bottom() - watchAreaHeight) # tricks to solve the hidpi impact on QCursor.pos() watchArea.setTopLeft( QPoint(watchArea.topLeft().x() * self.scale, watchArea.topLeft().y() * self.scale)) watchArea.setBottomRight( QPoint(watchArea.bottomRight().x() * self.scale, watchArea.bottomRight().y() * self.scale)) watchAreaPixmap = self.screenPixel.copy(watchArea) # second, calculate the magnifier area magnifierAreaWidth = watchAreaWidth * 10 magnifierAreaHeight = watchAreaHeight * 10 fontAreaHeight = 40 cursorSize = 24 magnifierArea = QRectF( QPoint(QCursor.pos().x() + cursorSize, QCursor.pos().y() + cursorSize), QPoint(QCursor.pos().x() + cursorSize + magnifierAreaWidth, QCursor.pos().y() + cursorSize + magnifierAreaHeight)) if magnifierArea.right() >= self.screenPixel.width(): magnifierArea.moveLeft(QCursor.pos().x() - magnifierAreaWidth - cursorSize / 2) if magnifierArea.bottom() + fontAreaHeight >= self.screenPixel.height( ): magnifierArea.moveTop(QCursor.pos().y() - magnifierAreaHeight - cursorSize / 2 - fontAreaHeight) # third, draw the watch area to magnifier area watchAreaScaled = watchAreaPixmap.scaled( QSize(magnifierAreaWidth * self.scale, magnifierAreaHeight * self.scale)) magnifierPixmap = self.graphicsScene.addPixmap(watchAreaScaled) magnifierPixmap.setOffset(magnifierArea.topLeft()) # then draw lines and text self.graphicsScene.addRect(QRectF(magnifierArea), QPen(QColor(255, 255, 255), 2)) self.graphicsScene.addLine( QLineF(QPointF(magnifierArea.center().x(), magnifierArea.top()), QPointF(magnifierArea.center().x(), magnifierArea.bottom())), QPen(QColor(0, 255, 255), 2)) self.graphicsScene.addLine( QLineF(QPointF(magnifierArea.left(), magnifierArea.center().y()), QPointF(magnifierArea.right(), magnifierArea.center().y())), QPen(QColor(0, 255, 255), 2)) # get the rgb of mouse point pointRgb = QColor(self.screenPixel.toImage().pixel(self.mousePoint)) # draw information self.graphicsScene.addRect( QRectF( magnifierArea.bottomLeft(), magnifierArea.bottomRight() + QPoint(0, fontAreaHeight + 30)), QPen(Qt.black), QBrush(Qt.black)) rgbInfo = self.graphicsScene.addSimpleText( ' Rgb: ({0}, {1}, {2})'.format(pointRgb.red(), pointRgb.green(), pointRgb.blue())) rgbInfo.setPos(magnifierArea.bottomLeft() + QPoint(0, 5)) rgbInfo.setPen(QPen(QColor(255, 255, 255), 2)) rect = self.selectedArea.normalized() sizeInfo = self.graphicsScene.addSimpleText(' Size: {0} x {1}'.format( rect.width() * self.scale, rect.height() * self.scale)) sizeInfo.setPos(magnifierArea.bottomLeft() + QPoint(0, 15) + QPoint(0, fontAreaHeight / 2)) sizeInfo.setPen(QPen(QColor(255, 255, 255), 2))
def paint(self, painter, option, widget): fillColor = self.color if option.state & QStyle.State_Selected: fillColor = self.color.darker(150) if option.state & QStyle.State_MouseOver: fillColor = fillColor.lighter(125) lod = option.levelOfDetailFromTransform(painter.worldTransform()) if lod < 0.2: if lod < 0.125: painter.fillRect(QRectF(0, 0, 110, 70), fillColor) return b = painter.brush() painter.setBrush(fillColor) painter.drawRect(13, 13, 97, 57) painter.setBrush(b) return oldPen = painter.pen() pen = oldPen width = 0 if option.state & QStyle.State_Selected: width += 2 pen.setWidth(width) b = painter.brush() painter.setBrush( QBrush( fillColor.darker(120 if (option.state & QStyle.State_Sunken) else 100))) painter.drawRect(QRect(14, 14, 79, 39)) painter.setBrush(b) if lod >= 1: painter.setPen(QPen(Qt.gray, 1)) painter.drawLine(15, 54, 94, 54) painter.drawLine(94, 53, 94, 15) painter.setPen(QPen(Qt.black, 0)) # Draw text if lod >= 2: font = QFont("Times", 10) font.setStyleStrategy(QFont.ForceOutline) painter.setFont(font) painter.save() painter.scale(0.1, 0.1) painter.drawText( 170, 180, "Model: VSC-2000 (Very Small Chip) at {}x{}".format( self.x, self.y)) painter.drawText(170, 200, "Serial number: DLWR-WEER-123L-ZZ33-SDSJ") painter.drawText(170, 220, "Manufacturer: Chip Manufacturer") painter.restore() # Draw lines lines = [] # QVarLengthArray < QLineF, 36 > if lod >= 0.5: for i in range( 0, 11, 1 if lod > 0.5 else 2): # (int i = 0 i <= 10 i += (1 if lod > 0.5 else 2)): lines.append(QLineF(18 + 7 * i, 13, 18 + 7 * i, 5)) lines.append(QLineF(18 + 7 * i, 54, 18 + 7 * i, 62)) for i in range(0, 6, 1 if lod > 0.5 else 2): # (int i = 0 i <= 6 i += (lod > 0.5 ? 1: 2)) { lines.append(QLineF(5, 18 + i * 5, 13, 18 + i * 5)) lines.append(QLineF(94, 18 + i * 5, 102, 18 + i * 5)) if lod >= 0.4: lineData = [ QLineF(25, 35, 35, 35), QLineF(35, 30, 35, 40), QLineF(35, 30, 45, 35), QLineF(35, 40, 45, 35), QLineF(45, 30, 45, 40), QLineF(45, 35, 55, 35) ] lines.extend(lineData) painter.drawLines(lines) # Draw red ink if len(self.stuff) > 1: p = painter.pen() painter.setPen( QPen(Qt.red, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) painter.setBrush(Qt.NoBrush) path = QPainterPath path.moveTo(self.stuff[0]) for i in range(1, len(self.stuff)): path.lineTo(self.stuff[i]) painter.drawPath(path) painter.setPen(p) """