def drawSizeInfo(self): sizeInfoAreaWidth = 200 sizeInfoAreaHeight = 30 spacing = 5 rect = self.selectedArea.normalized() sizeInfoArea = QRect(rect.left(), rect.top() - spacing - sizeInfoAreaHeight, sizeInfoAreaWidth, sizeInfoAreaHeight) if sizeInfoArea.top() < 0: sizeInfoArea.moveTopLeft(rect.topLeft() + QPoint(spacing, spacing)) if sizeInfoArea.right() >= self.screenPixel.width(): sizeInfoArea.moveTopLeft(rect.topLeft() - QPoint(spacing, spacing) - QPoint(sizeInfoAreaWidth, 0)) if sizeInfoArea.left() < spacing: sizeInfoArea.moveLeft(spacing) if sizeInfoArea.top() < spacing: sizeInfoArea.moveTop(spacing) self.itemsToRemove.append( self.graphicsScene.addRect(QRectF(sizeInfoArea), QPen(Qt.white), QBrush(Qt.black))) sizeInfo = self.graphicsScene.addSimpleText(' {0} x {1}'.format( rect.width() * self.scale, rect.height() * self.scale)) sizeInfo.setPos(sizeInfoArea.topLeft() + QPoint(0, 2)) sizeInfo.setPen(QPen(QColor(255, 255, 255), 2)) self.itemsToRemove.append(sizeInfo)
def __init__(self, rect=None, parent=None): self.signals = CurveNodeItemSignals() gradient = QRadialGradient( self.WIDTH * 0.75, self.WIDTH * 0.75, self.WIDTH * 0.75, self.WIDTH * 0.75, self.WIDTH * 0.75) gradient.setColorAt(0, self.theme().accent_color_6 if self.theme() else QColor.fromRgbF(1, 0.5, 0.01, 1)) gradient.setColorAt(1, self.theme().accent_color_8 if self.theme() else QColor.fromRgbF(1, 0.6, 0.06, 1)) self._brush = QBrush(gradient) self._brush.setStyle(Qt.RadialGradientPattern) self._pen = QPen() self._pen.setStyle(Qt.SolidLine) self._pen.setWidth(2) self._pen.setColor(self.theme().accent_color if self.theme() else QColor(104, 104, 104, 255)) self._selected_pen = QPen() self._selected_pen.setStyle(Qt.SolidLine) self._selected_pen.setWidth(3) self._selected_pen.setColor(self.theme().accent_color_4 if self.theme() else QColor(67, 255, 163, 255)) super(CurveNodeItem, self).__init__(parent) self._lock_x_pos = False self._snap = False self._current_pos = None self._new_pos = None self._line = None self._is_point1 = False self.set_rect(rect if rect else QRect(0, 0, 10, 10)) self.setFlags( QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemSendsScenePositionChanges)
class BezierCurveItem(QGraphicsPathItem, object): def __init__(self, parent=None): super(BezierCurveItem, self).__init__(parent) self._pen = QPen(Qt.green, 2, Qt.SolidLine) self._rect = QRectF() self._bounding_rect = None self._points = list() # self._theme = None self.setZValue(-1) @property def points(self): return tuple(self._points) def boundingRect(self): return QRectF(self._bounding_rect or self._rect) def paint(self, painter, option, widget): self._pen.setColor(self.theme().accent_color if self.theme() else Qt.blue) painter.setPen(self._pen) painter.drawPath(self.path()) def update_path(self, points): self._points = points bezier_path = QPainterPath() bezier_path.moveTo(points[0]) bezier_path.cubicTo(*points[1:]) self._rect = bezier_path.boundingRect() self.setPath(bezier_path)
def paint_blend_slider(self, painter, option, index): if not self.PAINT_SLIDER or not self.viewer().is_icon_view(): return painter.setPen(QPen(Qt.NoPen)) rect = self.visual_rect(option) color = self.viewer().background_color().toRgb() color.setAlpha(75) painter.setBrush(QBrush(color)) height = rect.height() ratio = self.blend_value() if ratio < 0: width = 0 elif ratio > 100: width = rect.width() else: width = rect.width() * (float(ratio) / 100) rect.setWidth(width) rect.setHeight(height) rect = self.visual_rect(option) rect.setY(rect.y() + (4 * self.dpi())) color = self.viewer().text_color().toRgb() color.setAlpha(220) pen = QPen(color) align = Qt.AlignTop | Qt.AlignHCenter painter.setPen(pen) painter.drawText(rect, align, str(self.blend_value()) + "%")
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 __init__(self, parent=None): super(BezierCurveItem, self).__init__(parent) self._pen = QPen(Qt.green, 2, Qt.SolidLine) self._rect = QRectF() self._bounding_rect = None self._points = list() # self._theme = None self.setZValue(-1)
def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.HighQualityAntialiasing) gradient = QRadialGradient(self._bounds.center(), self._bounds.width() * 0.5, self._bounds.center()) gradient.setFocalRadius(self._bounds.width() * 0.3) gradient.setCenterRadius(self._bounds.width() * 0.7) gradient.setColorAt(0, Qt.white) gradient.setColorAt(1, Qt.lightGray) painter.setPen(QPen(QBrush(Qt.gray), self._bounds.width() * 0.005)) painter.setBrush(QBrush(gradient)) painter.drawEllipse(self._bounds) painter.setPen(QPen(QBrush(Qt.gray), self._bounds.width() * 0.005)) painter.drawLine( QPointF(self._bounds.left(), self._bounds.center().y()), QPointF(self._bounds.center().x() - self._bounds.width() * 0.35, self._bounds.center().y())) painter.drawLine( QPointF(self._bounds.center().x() + self._bounds.width() * 0.35, self._bounds.center().y()), QPointF(self._bounds.right(), self._bounds.center().y())) painter.drawLine( QPointF(self._bounds.center().x(), self._bounds.top()), QPointF(self._bounds.center().x(), self._bounds.center().y() - self._bounds.width() * 0.35)) painter.drawLine( QPointF(self._bounds.center().x(), self._bounds.center().y() + self._bounds.width() * 0.35), QPointF(self._bounds.center().x(), self._bounds.bottom())) if not self.isEnabled(): return gradient = QRadialGradient(self._knop_bounds.center(), self._knop_bounds.width() * 0.5, self._knop_bounds.center()) gradient.setFocalRadius(self._knop_bounds.width() * 0.2) gradient.setCenterRadius(self._knop_bounds.width() * 0.5) gradient.setColorAt(0, Qt.gray) gradient.setColorAt(1, Qt.darkGray) painter.setPen(QPen(QBrush(Qt.darkGray), self._bounds.width() * 0.005)) painter.setBrush(QBrush(gradient)) painter.drawEllipse(self._knop_bounds)
def paint_background(self, painter, option, index): """ Draw the background for the item :param painter: QPainter :param option: QStyleOptionViewItem :param index:QModelIndex """ is_selected = option.state & QStyle.State_Selected is_mouse_over = option.state & QStyle.State_MouseOver painter.setPen(QPen(Qt.NoPen)) visual_rect = self.visual_rect(option) if is_selected: color = self.background_selected_color() elif is_mouse_over: color = self.background_hover_color() else: color = self.backgroundColor() painter.setBrush(QBrush(color)) if not self.viewer().is_icon_view(): spacing = 1 * self.dpi() height = visual_rect.height() - spacing visual_rect.setHeight(height) painter.drawRect(visual_rect)
def paint_background(self, painter, option, index): """ Overrides base paint_background icon function Draw the background for the item :param painter: QPainter :param option: QStyleOptionViewItem :param index: QModelIndex """ super(GroupDataItemView, self).paint_background(painter, option, index) painter.setPen(QPen(Qt.NoPen)) visual_rect = self.visualRect(option) text = self.name() metrics = QFontMetrics(self._font) text_width = metrics.width(text) padding = (25 * self.dpi()) visual_rect.setX(text_width + padding) visual_rect.setY(visual_rect.y() + (visual_rect.height() / 2)) visual_rect.setHeight(2 * self.dpi()) visual_rect.setWidth(visual_rect.width() - padding) color = QColor(self.text_color().red(), self.text_color().green(), self.text_color().blue(), 10) painter.setBrush(QBrush(color)) painter.drawRect(visual_rect)
def paintEvent(self, event): QMainWindow.paintEvent(self, event) if not self.line.isNull(): painter = QPainter(self) pen = QPen(Qt.red, 3) painter.setPen(pen) painter.drawLine(self.line)
def paint(self, painter, option, index): painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) model = index.model() view = self.parent() if view.hasFocus() and option.state & QStyle.State_MouseOver: painter.setPen(Qt.NoPen) painter.setBrush(Qt.gray) painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), self._ICON_MARGIN, self._ICON_MARGIN) pixmap = model.data(index, Qt.DecorationRole).pixmap(view.iconSize()) pm_rect = QRect( option.rect.topLeft() + QPoint(self._ICON_MARGIN + 1, self._ICON_MARGIN + 1), view.iconSize() - QSize(self._ICON_MARGIN * 2, self._ICON_MARGIN * 2)) painter.drawPixmap(pm_rect, pixmap) if option.state & QStyle.State_Selected: painter.setPen( QPen(Qt.red, 1.0, Qt.SolidLine, Qt.SquareCap, Qt.RoundJoin)) painter.setBrush(Qt.NoBrush) painter.drawRect(option.rect.adjusted(2, 2, -2, -2)) font = view.font() fm = QFontMetrics(font) text = os.path.splitext( os.path.basename(model.data(index, Qt.DisplayRole)))[0] text = fm.elidedText(text, Qt.ElideRight, view.iconSize().width() - 4) text_opt = QTextOption() text_opt.setAlignment(Qt.AlignHCenter) txt_rect = QRectF(QPointF(pm_rect.bottomLeft() + QPoint(0, 1)), QPointF(option.rect.bottomRight() - QPoint(4, 3))) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(QColor(22, 22, 22, 220)) painter.drawRoundedRect(txt_rect.adjusted(-2, -2, 2, 2), 2, 2) painter.restore() painter.setPen(self.parent().palette().color(QPalette.WindowText)) painter.drawText(txt_rect, text, text_opt) font.setPointSize(8) fm = QFontMetrics(font) item = model.itemFromIndex(index) size_text = '%d x %d' % (item.size.width(), item.size.height()) size_rect = fm.boundingRect(option.rect, Qt.AlignLeft | Qt.AlignTop, size_text) size_rect.translate(4, 4) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(QColor(22, 22, 22, 220)) painter.drawRoundedRect(size_rect.adjusted(-2, -2, 2, 2), 2, 2) painter.restore() painter.setFont(font) painter.drawText(size_rect, size_text)
def drawFreeLine(self, pointPath, result): tmp = [ ACTION_FREEPEN, QPainterPath(pointPath), QPen(QColor(self.penColorNow), int(self.penSizeNow)) ] if result: self.drawListResult.append(tmp) else: self.drawListProcess = tmp
def _create_grid(self, divider=4, color=Qt.darkGray, line=Qt.DashLine): pos = float(self._base_size) / divider for div in range(divider): if div == 0: continue line_item = QGraphicsLineItem(0, pos * div, self._base_size, pos * div) line_item.setZValue(-1) line_item.setPen(QPen(color, 1, line)) self.addItem(line_item) line_item = QGraphicsLineItem(pos * div, 0, pos * div, self._base_size) line_item.setZValue(-1) line_item.setPen(QPen(color, 1, line)) self.addItem(line_item) pen = QPen(Qt.black, 1, Qt.SolidLine) rect = QGraphicsRectItem(self._base_rect) rect.setZValue(-1) self.addItem(rect)
def drawBackground(self, painter, rect): """ Override draw background method to write out image as background """ painter.setRenderHint(QPainter.Antialiasing) painter.fillRect(rect, self._color) painter.setPen(QPen(Qt.black, 0.5, Qt.DashLine)) painter.setBrush(QColor(67, 255, 163)) if self._use_bg_image and not self._pixmap.isNull(): painter.drawPixmap(QPointF(0, 0), self._pixmap)
def main(): import sys app = QApplication(sys.argv) series0 = QLineSeries() series1 = QLineSeries() series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF( 9, 7) << QPointF(12, 6) << QPointF(16, 7) << QPointF(18, 5) series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF( 8, 2) << QPointF(12, 3) << QPointF(16, 4) << QPointF(18, 3) series = QAreaSeries(series0, series1) series.setName("Batman") pen = QPen(0x059605) pen.setWidth(3) series.setPen(pen) gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 1)) gradient.setColorAt(0.0, QColor(0x3CC63C)) gradient.setColorAt(1.0, QColor(0x26F626)) gradient.setCoordinateMode(QGradient.ObjectBoundingMode) series.setBrush(gradient) chart = QChart() chart.addSeries(series) chart.setTitle("Simple areachart example") chart.createDefaultAxes() chart.axes(Qt.Horizontal)[0].setRange(0, 20) chart.axes(Qt.Vertical)[0].setRange(0, 10) chartView = QChartView(chart) chartView.setRenderHint(QPainter.Antialiasing) window = QMainWindow() window.setCentralWidget(chartView) window.resize(400, 300) window.show() sys.exit(app.exec_())
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 set_badge(self, x, y, w, h, color=None): """ Set badge for the icon :param x: int :param y: int :param w: int :param h: int :param color: QColor or None """ color = color or QColor(240, 100, 100) size = self.actualSize(QSize(256, 256)) pixmap = self.pixmap(size) painter = QPainter(pixmap) pen = QPen(color) pen.setWidth(0) painter.setPen(pen) painter.setBrush(color) painter.setRenderHint(QPainter.Antialiasing) painter.drawEllipse(x, y, w, h) painter.end() icon = Icon(pixmap) self.swap(icon)
def paintEvent(self, event): contents_y = self.editor.verticalScrollBar().value() page_bottom = contents_y + self.editor.viewport().height() font_metrics = self.fontMetrics() current_block = self.editor.document().findBlock( self.editor.textCursor().position()) painter = QPainter(self) line_count = 0 # Iterate over all text blocks in the document block = self.editor.document().begin() font_size = self.editor.font().pointSize() font = painter.font() font.setPixelSize(font_size) offset = font_metrics.ascent() + font_metrics.descent() color = painter.pen().color() painter.setFont(font) align = Qt.AlignRight while block.isValid(): line_count += 1 # Get top left position of the block in the document and check if the position of the block is # outside of the visible area position = self.editor.document().documentLayout( ).blockBoundingRect(block).topLeft() if position.y() == page_bottom: break rect = QRect(0, round(position.y()) - contents_y, self.width() - 5, font_size + offset) # Draw line rect if block == current_block: painter.setPen(Qt.NoPen) painter.setBrush(QBrush(self.bg)) painter.drawRect( QRect(0, round(position.y()) - contents_y, self.width(), font_size + (offset / 2))) painter.setPen(QPen(color)) # Draw text painter.drawText(rect, align, str(line_count)) block = block.next() self.highest_line = line_count painter.end() super(ScriptEditorNumberBar, self).paintEvent(event)
def __init__(self, grid_main_spacing=8, grid_secondary_spacing=8, grid_main_pen_color=GridColors.BaseColor, grid_secondary_pen_color=GridColors.DarkerColor, grid_main_width=1, grid_secondary_width=1, grid_main_style=Qt.SolidLine, grid_secondary_style=Qt.SolidLine, draw_main_grid=True, draw_secondary_grid=True, fit_grid=False, fit_grid_main_divisions=10, fit_grid_secondary_divisions=4, parent=None): super(GridScene, self).__init__(parent=parent) self._grid_main_pen = QPen() self._grid_secondary_pen = QPen() self._grid_main_spacing = grid_main_spacing self._grid_secondary_spacing = grid_secondary_spacing self._grid_main_pen.setColor(grid_main_pen_color) self._grid_secondary_pen.setColor(grid_secondary_pen_color) self._grid_main_pen.setWidth(grid_main_width) self._grid_secondary_pen.setWidth(grid_secondary_width) self._grid_main_style = grid_main_style self._grid_secondary_style = grid_secondary_style self._draw_main_grid = draw_main_grid self._draw_secondary_grid = draw_secondary_grid self._fit_grid = fit_grid self._fit_grid_main_divisions = fit_grid_main_divisions self._fit_grid_secondary_divisions = fit_grid_secondary_divisions self._grid_main_pen.setStyle(self._grid_main_style) self._grid_secondary_pen.setStyle(self._grid_secondary_style) self._fit_grid_draw = False
def drawEllipse(self, x1, x2, y1, y2, result): rect = self.selectedArea.normalized() tmpRect = QRect(QPoint(x1, x2), QPoint(y1, y2)).normalized() resultRect = rect & tmpRect tmp = [ ACTION_ELLIPSE, resultRect.topLeft().x(), resultRect.topLeft().y(), resultRect.bottomRight().x(), resultRect.bottomRight().y(), QPen(QColor(self.penColorNow), int(self.penSizeNow)) ] if result: self.drawListResult.append(tmp) else: self.drawListProcess = tmp
def drawLine(self, x1, x2, y1, y2, result): rect = self.selectedArea.normalized() if y1 <= rect.left(): y1 = rect.left() elif y1 >= rect.right(): y1 = rect.right() if y2 <= rect.top(): y2 = rect.top() elif y2 >= rect.bottom(): y2 = rect.bottom() tmp = [ ACTION_LINE, x1, x2, y1, y2, QPen(QColor(self.penColorNow), int(self.penSizeNow)) ] if result: self.drawListResult.append(tmp) else: self.drawListProcess = tmp
def _paint_drop_indicator(self, painter): """ Internal function used to paint the drop indicator manually :param painter: QPainter """ if self.state() == QAbstractItemView.DraggingState: opt = QStyleOption() opt.initFrom(self) opt.rect = self._drop_indicator_rect rect = opt.rect color = Qt.black if dcc.is_maya(): color = Qt.white brush = QBrush(QColor(color)) pen = QPen(brush, 1, Qt.DotLine) painter.setPen(pen) if rect.height() == 0: painter.drawLine(rect.topLeft(), rect.topRight()) else: painter.drawRect(rect)
def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(painter.Antialiasing) font = painter.font() font.setBold(True) painter.setFont(font) x = self.rect().x() y = self.rect().y() w = self.rect().width() - 1 h = self.rect().height() - 1 _rect = 8 if self._rolloutStyle == ExpanderStyles.Rounded: painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) self.__drawTriangle(painter, x, y) pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRoundedRect(x + 1, y + 1, w - 1, h - 1, _rect, _rect) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRoundedRect(x, y, w - 1, h - 1, _rect, _rect) if self._rolloutStyle == ExpanderStyles.Square: painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) self.__drawTriangle(painter, x, y) pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.2) painter.setPen(pen) painter.drawRect(x + 1, y + 1, w - 1, h - 1) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRect(x, y, w - 1, h - 1) if self._rolloutStyle == ExpanderStyles.Maya: painter.drawText( x + (45 if self.dragDropMode() == ExpanderDragDropModes.InternalMove else 25), y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) painter.setRenderHint(QPainter.Antialiasing, False) self.__drawTriangle(painter, x, y) header_height = 20 header_rect = QRect(x + 1, y + 1, w - 1, header_height) header_rect_shadow = QRect(x - 1, y - 1, w + 1, header_height + 2) pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.4) # painter.setPen(pen) painter.setPen(Qt.NoPen) painter.drawRect(header_rect) painter.fillRect(header_rect, QColor(255, 255, 255, 18)) pen.setColor(self.palette().color(QPalette.Dark)) painter.setPen(pen) painter.drawRect(header_rect_shadow) if not self.isCollapsed(): # pen = QPen(self.palette().color(QPalette.Background)) # painter.setPen(pen) offset = header_height + 3 body_rect = QRect(x, y + offset, w, h - offset) # body_rect_shadow = QRect(x + 1, y + offSet, w + 1, h - offSet + 1) painter.drawRect(body_rect) # pen.setColor(self.palette().color(QPalette.Foreground)) # pen.setWidthF(0.4) # painter.setPen(pen) # painter.drawRect(body_rect_shadow) elif self._rolloutStyle == ExpanderStyles.Boxed: if self.isCollapsed(): arect = QRect(x + 1, y + 9, w - 1, 4) brect = QRect(x, y + 8, w - 1, 4) text = '+' else: arect = QRect(x + 1, y + 9, w - 1, h - 9) brect = QRect(x, y + 8, w - 1, h - 9) text = '-' pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRect(arect) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRect(brect) painter.setRenderHint(painter.Antialiasing, False) painter.setBrush(self.palette().color(QPalette.Window).darker(120)) painter.drawRect(x + 10, y + 1, w - 20, 16) painter.drawText(x + 16, y + 1, w - 32, 16, Qt.AlignLeft | Qt.AlignVCenter, text) painter.drawText(x + 10, y + 1, w - 20, 16, Qt.AlignCenter, self.title()) if self.dragDropMode(): rect = self.dragDropRect() _layout = rect.left() _rect = rect.right() center_y = rect.center().y() pen = QPen(self.palette().color(self.isCollapsed() and QPalette.Shadow or QPalette.Mid)) painter.setPen(pen) for y in (center_y - 3, center_y, center_y + 3): painter.drawLine(_layout, y, _rect, y) painter.end()
class BaseAnimObject(object): _glow_pens = {} for index in range(1, 11): _glow_pens[index] = [QPen(QColor(0, 255, 0, 12 * index), 1, Qt.SolidLine), QPen(QColor(0, 255, 0, 5 * index), 3, Qt.SolidLine), QPen(QColor(0, 255, 0, 2 * index), 5, Qt.SolidLine), QPen(QColor(0, 255, 0, 25.5 * index), 1, Qt.SolidLine)] _pens_text = QPen(QColor(202, 207, 210), 1, Qt.SolidLine) _pens_shadow = QPen(QColor(9, 10, 12), 1, Qt.SolidLine) _pens_border = QPen(QColor(9, 10, 12), 2, Qt.SolidLine) _pens_clear = QPen(QColor(0, 0, 0, 0), 1, Qt.SolidLine) _pens_text_disabled = QPen(QColor(102, 107, 110), 1, Qt.SolidLine) _pens_shadow_disabled = QPen(QColor(0, 0, 0), 1, Qt.SolidLine) _brush_clear = QBrush(QColor(0, 0, 0, 0)) _brush_border = QBrush(QColor(9, 10, 12)) def __init__(self): font = QFont() font.setPointSize(8) font.setFamily("Calibri") self.setFont(font) self._hover = False self._glow_index = 0 self._anim_timer = QTimer() self._anim_timer.timeout.connect(self._animate_glow) def enterEvent(self, event): super(self.__class__, self).enterEvent(event) if not self.isEnabled(): return self._hover = True self._start_anim() def leaveEvent(self, event): super(self.__class__, self).leaveEvent(event) if not self.isEnabled(): return self._hover = False self._start_anim() def _animate_glow(self): if self._hover: if self._glow_index >= 10: self._glow_index = 10 self._anim_timer.stop() else: self._glow_index += 1 else: if self._glow_index <= 0: self._glow_index = 0 self._anim_timer.stop() else: self._glow_index -= 1 dcc.execute_deferred(self.update) def _start_anim(self): if self._anim_timer.isActive(): return self._anim_timer.start(20)
def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(painter.Antialiasing) font = painter.font() font.setBold(True) painter.setFont(font) x = self.rect().x() y = self.rect().y() w = self.rect().width() - 1 h = self.rect().height() - 1 _rect = 8 if self.rollout_style == AccordionStyle.ROUNDED: painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) self._draw_triangle(painter, x, y) self._draw_icon(painter, x + 22, y + 3) pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRoundedRect(x + 1, y + 1, w - 1, h - 1, _rect, _rect) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRoundedRect(x, y, w - 1, h - 1, _rect, _rect) elif self.rollout_style == AccordionStyle.SQUARE: painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) self._draw_triangle(painter, x, y) self._draw_icon(painter, x + 22, y + 3) pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.3) painter.setPen(pen) painter.drawRect(x + 1, y + 1, w - 1, h - 1) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) elif self.rollout_style == AccordionStyle.MAYA: painter.drawText(x + 33 if not self._icon else 40, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) painter.setRenderHint(QPainter.Antialiasing, False) self._draw_triangle(painter, x, y) self._draw_icon(painter, x + 22, y + 3) header_height = 20 header_rect = QRect(x + 1, y + 1, w - 1, header_height) header_rect_shadow = QRect(x - 1, y - 1, w + 1, header_height + 2) pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.4) painter.setPen(pen) painter.drawRect(header_rect) painter.fillRect(header_rect, QColor(255, 255, 255, 18)) pen.setColor(self.palette().color(QPalette.Dark)) painter.setPen(pen) painter.drawRect(header_rect_shadow) if not self.is_collapsed(): pen = QPen(self.palette().color(QPalette.Dark)) pen.setWidthF(0.8) painter.setPen(pen) offset = header_height + 3 body_rect = QRect(x, y + offset, w, h - offset) body_rect_shadow = QRect(x + 1, y + offset, w + 1, h - offset + 1) painter.drawRect(body_rect) pen.setColor(self.palette().color(QPalette.Light)) pen.setWidth(0.4) painter.setPen(pen) painter.drawRect(body_rect_shadow) elif self.rollout_style == AccordionStyle.BOXED: if self.is_collapsed(): a_rect = QRect(x + 1, y + 9, w - 1, 4) b_rect = QRect(x, y + 8, w - 1, 4) text = '+' else: a_rect = QRect(x + 1, y + 9, w - 1, h - 9) b_rect = QRect(x, y + 8, w - 1, h - 9) text = '-' pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRect(a_rect) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRect(b_rect) painter.setRenderHint(painter.Antialiasing, False) painter.setBrush(self.palette().color(QPalette.Window).darker(120)) painter.drawRect(x + 10, y + 1, w - 20, 16) painter.drawText(x + 16, y + 1, w - 32, 16, Qt.AlignLeft | Qt.AlignVCenter, text) painter.drawText(x + 10, y + 1, w - 20, 16, Qt.AlignCenter, self.title()) if self.drag_drop_mode: rect = self.get_drag_drop_rect() _layout = rect.left() _rect = rect.right() center_y = rect.center().y() for y in (center_y - 3, center_y, center_y + 3): painter.drawLine(_layout, y, _rect, y) painter.end()
def __init__(self, name): super(RubberRect, self).__init__() self._name = name self.setZValue(2) self.setPen(QPen(self.DEFAULT_RUBBER_RECT_COLOR, 0.5, Qt.SolidLine)) self.setBrush(QBrush(self.DEFAULT_RUBBER_RECT_COLOR))
def paintEvent( self, event ): painter = QPainter() painter.begin(self) painter.setRenderHint(painter.Antialiasing) font = painter.font() font.setBold(True) painter.setFont(font) x = self.rect().x() y = self.rect().y() w = self.rect().width() - 1 h = self.rect().height() - 1 r = 8 # draw a rounded style if self._rolloutStyle == 2: # draw the text painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) # draw the triangle self.__drawTriangle(painter, x, y) # draw the borders pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRoundedRect(x + 1, y + 1, w - 1, h - 1, r, r) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRoundedRect(x, y, w - 1, h - 1, r, r) # draw a square style if self._rolloutStyle == 3: # draw the text painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) self.__drawTriangle(painter, x, y) # draw the borders pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRect(x + 1, y + 1, w - 1, h - 1) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRect(x, y, w - 1, h - 1) # draw a Maya style if self._rolloutStyle == 4: # draw the text painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) painter.setRenderHint(QPainter.Antialiasing, False) self.__drawTriangle(painter, x, y) # draw the borders - top headerHeight = 20 headerRect = QRect(x + 1, y + 1, w - 1, headerHeight) headerRectShadow = QRect(x - 1, y - 1, w + 1, headerHeight + 2) # Highlight pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.4) painter.setPen(pen) painter.drawRect(headerRect) painter.fillRect(headerRect, QColor(255, 255, 255, 18)) # Shadow pen.setColor(self.palette().color(QPalette.Dark)) painter.setPen(pen) painter.drawRect(headerRectShadow) if not self.isCollapsed(): # draw the lover border pen = QPen(self.palette().color(QPalette.Dark)) pen.setWidthF(0.8) painter.setPen(pen) offSet = headerHeight + 3 bodyRect = QRect(x, y + offSet, w, h - offSet) bodyRectShadow = QRect(x + 1, y + offSet, w + 1, h - offSet + 1) painter.drawRect(bodyRect) pen.setColor(self.palette().color(QPalette.Light)) pen.setWidthF(0.4) painter.setPen(pen) painter.drawRect(bodyRectShadow) # draw a boxed style elif self._rolloutStyle == 1: if self.isCollapsed(): arect = QRect(x + 1, y + 9, w - 1, 4) brect = QRect(x, y + 8, w - 1, 4) text = '+' else: arect = QRect(x + 1, y + 9, w - 1, h - 9) brect = QRect(x, y + 8, w - 1, h - 9) text = '-' # draw the borders pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.6) painter.setPen(pen) painter.drawRect(arect) pen.setColor(self.palette().color(QPalette.Shadow)) painter.setPen(pen) painter.drawRect(brect) painter.setRenderHint(painter.Antialiasing, False) painter.setBrush(self.palette().color(QPalette.Window).darker(120)) painter.drawRect(x + 10, y + 1, w - 20, 16) painter.drawText(x + 16, y + 1, w - 32, 16, Qt.AlignLeft | Qt.AlignVCenter, text) painter.drawText(x + 10, y + 1, w - 20, 16, Qt.AlignCenter, self.title()) if self.dragDropMode(): rect = self.dragDropRect() # draw the lines l = rect.left() r = rect.right() cy = rect.center().y() for y in (cy - 3, cy, cy + 3): painter.drawLine(l, y, r, y) painter.end()
def drawBackground(self, painter, rect): """ Draw grid background for the graph scene """ scene_rect = self.sceneRect() gradient = QColor(65, 65, 65) painter.fillRect(rect.intersected(scene_rect), QBrush(gradient)) painter.setPen(QPen()) painter.drawRect(scene_rect) if len(self.views()) <= 0: LOGGER.error('Scene has not view associated to it!') return if not self.views()[0]: LOGGER.error('View {0} is not valid!'.format(self.views()[0])) return if hasattr(self.views()[0], 'is_grid_visible'): if self.views()[0].is_grid_visible: return if self._fit_grid: if not self._fit_grid_draw: if self._draw_main_grid: pos = float(self.width() / self._fit_grid_main_divisions) for div in range(self._fit_grid_main_divisions): if div == 0: continue line = QGraphicsLineItem(0, pos * div, self.width(), pos * div) line.setZValue(-1) line.setPen(self._grid_main_pen) self.addItem(line) line = QGraphicsLineItem(pos * div, 0, pos * div, self.width()) line.setZValue(-1) line.setPen(self._grid_main_pen) self.addItem(line) rect = QGraphicsRectItem(self.sceneRect()) rect.setZValue(-1) self.addItem(rect) if self._draw_secondary_grid: pos = float(self.width() / self._fit_grid_secondary_divisions) for div in range(self._fit_grid_secondary_divisions): if div == 0: continue line = QGraphicsLineItem(0, pos * div, self.width(), pos * div) line.setZValue(-1) line.setPen(self._grid_secondary_pen) self.addItem(line) line = QGraphicsLineItem(pos * div, 0, pos * div, self.width()) line.setZValue(-1) line.setPen(self._grid_secondary_pen) self.addItem(line) rect = QGraphicsRectItem(self.sceneRect()) rect.setZValue(-1) self.addItem(rect) self._fit_grid_draw = True else: if self._draw_main_grid: left = int(self.sceneRect().left()) - (int(self.sceneRect().left()) % self._grid_main_spacing) top = int(self.sceneRect().top()) - (int(self.sceneRect().top()) % self._grid_main_spacing) painter.setPen(self._grid_main_pen) # draw grid vertical lines for x in range(left, int(self.sceneRect().right()), self.grid_main_spacing): painter.drawLine(x, self.sceneRect().top(), x, self.sceneRect().bottom()) # draw grid horizontal lines for y in range(top, int(self.sceneRect().bottom()), self.grid_main_spacing): painter.drawLine(self.sceneRect().left(), y, self.sceneRect().right(), y) if self._draw_secondary_grid: left = int(self.sceneRect().left()) - (int(self.sceneRect().left()) % self._grid_secondary_spacing) top = int(self.sceneRect().top()) - (int(self.sceneRect().top()) % self._grid_secondary_spacing) painter.setPen(self._grid_secondary_pen) # draw grid vertical lines for x in range(left, int(self.sceneRect().right()), self.grid_secondary_spacing * 10): painter.drawLine(x, self.sceneRect().top(), x, self.sceneRect().bottom()) # draw grid horizontal lines for y in range(top, int(self.sceneRect().bottom()), self.grid_secondary_spacing * 10): painter.drawLine(self.sceneRect().left(), y, self.sceneRect().right(), y)
class CurveNodeItem(QGraphicsItem, object): curveUpdated = Signal() WIDTH = 10 def __init__(self, rect=None, parent=None): self.signals = CurveNodeItemSignals() gradient = QRadialGradient( self.WIDTH * 0.75, self.WIDTH * 0.75, self.WIDTH * 0.75, self.WIDTH * 0.75, self.WIDTH * 0.75) gradient.setColorAt(0, self.theme().accent_color_6 if self.theme() else QColor.fromRgbF(1, 0.5, 0.01, 1)) gradient.setColorAt(1, self.theme().accent_color_8 if self.theme() else QColor.fromRgbF(1, 0.6, 0.06, 1)) self._brush = QBrush(gradient) self._brush.setStyle(Qt.RadialGradientPattern) self._pen = QPen() self._pen.setStyle(Qt.SolidLine) self._pen.setWidth(2) self._pen.setColor(self.theme().accent_color if self.theme() else QColor(104, 104, 104, 255)) self._selected_pen = QPen() self._selected_pen.setStyle(Qt.SolidLine) self._selected_pen.setWidth(3) self._selected_pen.setColor(self.theme().accent_color_4 if self.theme() else QColor(67, 255, 163, 255)) super(CurveNodeItem, self).__init__(parent) self._lock_x_pos = False self._snap = False self._current_pos = None self._new_pos = None self._line = None self._is_point1 = False self.set_rect(rect if rect else QRect(0, 0, 10, 10)) self.setFlags( QGraphicsItem.ItemIsMovable | QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemSendsScenePositionChanges) @property def snap(self): return self._snap @snap.setter def snap(self, flag): self._snap = bool(flag) @property def lock_x_pos(self): return self._lock_x_pos @lock_x_pos.setter def lock_x_pos(self, value): self._lock_x_pos = value def boundingRect(self): return QRectF(0, 0, 20, 20) def paint(self, painter, option, widget): painter.setBrush(self._brush) painter.setPen(self._selected_pen if self.isSelected() else self._pen) painter.drawEllipse(self._rect) def mousePressEvent(self, event): self._current_pos = self.pos() super(CurveNodeItem, self).mousePressEvent(event) def mouseMoveEvent(self, event): super(CurveNodeItem, self).mouseMoveEvent(event) curve_offset = -self.WIDTH * 0.5 scale_x = min(max(event.scenePos().x(), curve_offset), self.scene().base_size + curve_offset) scale_y = min(max(event.scenePos().y(), curve_offset), self.scene().base_size + curve_offset) if self._lock_x_pos: scale_x = self._lock_x_pos if self._snap is not False: scale_x = round((float(scale_x) / self._snap)) * self._snap scale_y = round((float(scale_y) / self._snap)) * self._snap self._new_pos = QPointF(scale_x, scale_y) self.setPos(self._new_pos) self.scene().update_curve() self.signals.curveUpdated.emit() def mouseReleaseEvent(self, event): super(CurveNodeItem, self).mouseReleaseEvent(event) if not self._new_pos: return self.scene().undo_stack.push(CurveNodeMoveUndoCommand(self.scene(), self, self._current_pos, self._new_pos)) self._new_pos = None def itemChange(self, change, value): if change == self.ItemPositionChange and self.scene(): new_pos = value self._move_line_to_center(new_pos) return super(CurveNodeItem, self).itemChange(change, value) def set_rect(self, rect): self._rect = rect self.update() def add_line(self, line, is_point1): self._line = line self._is_point1 = is_point1 self._move_line_to_center(self.pos()) 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 redraw(self): self.graphicsScene.clear() # draw screenshot self.graphicsScene.addPixmap(self.screenPixel) # prepare for drawing selected area rect = QRectF(self.selectedArea) rect = rect.normalized() topLeftPoint = rect.topLeft() topRightPoint = rect.topRight() bottomLeftPoint = rect.bottomLeft() bottomRightPoint = rect.bottomRight() topMiddlePoint = (topLeftPoint + topRightPoint) / 2 leftMiddlePoint = (topLeftPoint + bottomLeftPoint) / 2 bottomMiddlePoint = (bottomLeftPoint + bottomRightPoint) / 2 rightMiddlePoint = (topRightPoint + bottomRightPoint) / 2 # draw the picture mask mask = QColor(0, 0, 0, 155) if self.selectedArea == QRect(): self.graphicsScene.addRect(0, 0, self.screenPixel.width(), self.screenPixel.height(), QPen(Qt.NoPen), mask) else: self.graphicsScene.addRect(0, 0, self.screenPixel.width(), topRightPoint.y(), QPen(Qt.NoPen), mask) self.graphicsScene.addRect(0, topLeftPoint.y(), topLeftPoint.x(), rect.height(), QPen(Qt.NoPen), mask) self.graphicsScene.addRect( topRightPoint.x(), topRightPoint.y(), self.screenPixel.width() - topRightPoint.x(), rect.height(), QPen(Qt.NoPen), mask) self.graphicsScene.addRect( 0, bottomLeftPoint.y(), self.screenPixel.width(), self.screenPixel.height() - bottomLeftPoint.y(), QPen(Qt.NoPen), mask) # draw the toolBar if self.action != ACTION_SELECT: spacing = 5 # show the toolbar first, then move it to the correct position # because the width of it may be wrong if this is the first time it shows self.tooBar.show() dest = QPointF(rect.bottomRight() - QPointF(self.tooBar.width(), 0) - QPointF(spacing, -spacing)) if dest.x() < spacing: dest.setX(spacing) pen_set_bar_height = self.penSetBar.height( ) if self.penSetBar is not None else 0 if dest.y() + self.tooBar.height( ) + pen_set_bar_height >= self.height(): if rect.top() - self.tooBar.height( ) - pen_set_bar_height < spacing: dest.setY(rect.top() + spacing) else: dest.setY(rect.top() - self.tooBar.height() - pen_set_bar_height - spacing) self.tooBar.move(self.mapToGlobal(dest.toPoint())) if self.penSetBar is not None: self.penSetBar.show() _pen_point = QPoint(dest.toPoint() + QPoint(0, self.tooBar.height() + spacing)) self.penSetBar.move(self.mapToGlobal(_pen_point)) if self.action == ACTION_TEXT: self.penSetBar.showFontWidget() else: self.penSetBar.showPenWidget() else: self.tooBar.hide() if self.penSetBar is not None: self.penSetBar.hide() # draw the list for step in self.drawListResult: self.drawOneStep(step) if self.drawListProcess is not None: self.drawOneStep(self.drawListProcess) if self.action != ACTION_TEXT: self.drawListProcess = None # if self.selectedArea != QRect(): # self.itemsToRemove = [] # # draw the selected rectangle # pen = QPen(QColor(0, 255, 255), 2) # self.itemsToRemove.append(self.graphicsScene.addRect(rect, pen)) # # draw the drag point # radius = QPoint(3, 3) # brush = QBrush(QColor(0, 255, 255)) # self.itemsToRemove.append( # self.graphicsScene.addEllipse(QRectF(topLeftPoint - radius, topLeftPoint + radius), pen, brush)) # self.itemsToRemove.append( # self.graphicsScene.addEllipse(QRectF(topMiddlePoint - radius, topMiddlePoint + radius), pen, brush)) # self.itemsToRemove.append( # self.graphicsScene.addEllipse(QRectF(topRightPoint - radius, topRightPoint + radius), pen, brush)) # self.itemsToRemove.append( # self.graphicsScene.addEllipse(QRectF(leftMiddlePoint - radius, leftMiddlePoint + radius), pen, brush)) # self.itemsToRemove.append( # self.graphicsScene.addEllipse(QRectF(rightMiddlePoint - radius, rightMiddlePoint + radius), pen, brush)) # self.itemsToRemove.append( # self.graphicsScene.addEllipse(QRectF(bottomLeftPoint - radius, bottomLeftPoint + radius), pen, brush)) # self.itemsToRemove.append( # self.graphicsScene.addEllipse(QRectF(bottomMiddlePoint - radius, bottomMiddlePoint + radius), pen, brush)) # self.itemsToRemove.append( # self.graphicsScene.addEllipse(QRectF(bottomRightPoint - radius, bottomRightPoint + radius), pen, brush)) # draw the textedit if self.textPosition is not None: textSpacing = 50 position = QPoint() if self.textPosition.x() + self.textInput.width( ) >= self.screenPixel.width(): position.setX(self.textPosition.x() - self.textInput.width()) else: position.setX(self.textPosition.x()) if self.textRect is not None: if self.textPosition.y() + self.textInput.height( ) + self.textRect.height() >= self.screenPixel.height(): position.setY(self.textPosition.y() - self.textInput.height() - self.textRect.height()) else: position.setY(self.textPosition.y() + self.textRect.height()) else: if self.textPosition.y() + self.textInput.height( ) >= self.screenPixel.height(): position.setY(self.textPosition.y() - self.textInput.height()) else: position.setY(self.textPosition.y()) self.textInput.move(self.mapToGlobal(position)) self.textInput.show() # self.textInput.getFocus() # draw the magnifier if self.action == ACTION_SELECT: self.drawMagnifier() if self.mousePressed: self.drawSizeInfo() if self.action == ACTION_MOVE_SELECTED: self.drawSizeInfo()
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))