def update_screen(self) -> None: use_live_screen = ( self.live_screen_checkbox.checkState() == Qt.CheckState.Checked) is_powered_on = self.power_checkbox.checkState( ) == Qt.CheckState.Checked image_display = (self.image_display_on if is_powered_on else self.image_display_off) result = QPixmap(image_display.size()) result.fill(Qt.transparent) painter = QPainter(result) painter.drawPixmap(QPoint(0, 0), image_display) if is_powered_on: screenshot = get_screenshot( ) if use_live_screen else self.screenshot screenshot = screenshot.scaled(800, 800, Qt.KeepAspectRatio, Qt.SmoothTransformation) screenshot = screenshot.copy(QRect(0, 0, 800, 480 * 0.94)) offset_x = (image_display.size().width() - screenshot.size().width()) / 2 offset_y = (image_display.size().height() - screenshot.size().height()) / 2 painter.setOpacity(self.backlight.brightness / 100) painter.drawPixmap(QPoint(offset_x, offset_y), screenshot) painter.end() result = result.scaled(600, 600, Qt.KeepAspectRatio, Qt.SmoothTransformation) self.screen_image.setPixmap(result)
def merge_with_layer_immediately_below(self): """ Merges a layer with the next lower visible layer. Does nothing if mode is preview or the target layer is an adjustment layer. """ if not hasattr(self, 'inputImg'): return ind = self.getLowerVisibleStackIndex() if ind < 0: # no visible layer found return target = self.parentImage.layersStack[ind] if hasattr(target, 'inputImg') or self.parentImage.useThumb: info = "Uncheck Preview first" if self.parentImage.useThumb else "Target layer must be background or image" dlgWarn("Cannot Merge layers", info=info) return # update stack self.parentImage.layersStack[0].applyToStack() # merge # target.setImage(self) qp = QPainter(target) qp.setCompositionMode(self.compositionMode) qp.setOpacity(self.opacity) qp.drawImage(QRect(0, 0, self.width(), self.height()), self) target.updatePixmap() self.parentImage.layerView.clear(delete=False) currentIndex = self.getStackIndex() self.parentImage.activeLayerIndex = ind self.parentImage.layersStack.pop(currentIndex) self.parentImage.layerView.setLayers(self.parentImage)
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget = None): painter.setRenderHint(QPainter.SmoothPixmapTransform) for layer_view in self.layer_views: if layer_view.visible and layer_view.image_view is not None: painter.setOpacity(layer_view.opacity) image_view_origin = layer_view.image_view.spatial.origin painter.drawImage(QPointF(image_view_origin[1], image_view_origin[0]), layer_view.displayed_image)
def paint_with_opacity(pixmap: QPixmap, opacity: float): transparent_image = QImage(QSize(36, 36), QImage.Format_ARGB32_Premultiplied) transparent_image.fill(Qt.transparent) painter = QPainter(transparent_image) painter.setOpacity(opacity) painter.drawPixmap(18 - pixmap.width() / 2, 18 - pixmap.height() / 2, pixmap) painter.end() return QPixmap.fromImage(transparent_image)
def getCurrentMaskedImage(self): """ Reduces the layer stack up to self (included), taking into account the masks. if self.isClipping is True self.mask applies to all lower layers and to self only otherwise. The method uses the non color managed rPixmaps to build the masked image. For convenience, mainly to be able to use its color space buffers, the built image is of type QLayer. It is drawn on a container image, created only once. @return: masked image @rtype: QLayer """ # init containers if needed if self.parentImage.useHald: return self.getHald() if self.maskedThumbContainer is None: self.maskedThumbContainer = QLayer.fromImage( self.getThumb(), parentImage=self.parentImage) if self.maskedImageContainer is None: self.maskedImageContainer = QLayer.fromImage( self, parentImage=self.parentImage) if self.parentImage.useThumb: img = self.maskedThumbContainer else: img = self.maskedImageContainer # no thumbnails for containers img.getThumb = lambda: img # draw lower stack qp = QPainter(img) top = self.parentImage.getStackIndex(self) bottom = 0 for i, layer in enumerate(self.parentImage.layersStack[bottom:top + 1]): if layer.visible: if i == 0: qp.setCompositionMode(QPainter.CompositionMode_Source) else: qp.setOpacity(layer.opacity) qp.setCompositionMode(layer.compositionMode) if layer.rPixmap is not None: qp.drawPixmap(QRect(0, 0, img.width(), img.height()), layer.rPixmap) else: qp.drawImage(QRect(0, 0, img.width(), img.height()), layer.getCurrentImage()) # clipping if layer.isClipping and layer.maskIsEnabled: #TODO modified 23/06/18 # draw mask as opacity mask # mode DestinationIn (set dest opacity to source opacity) qp.setCompositionMode( QPainter.CompositionMode_DestinationIn) omask = vImage.color2OpacityMask(layer.mask) qp.drawImage(QRect(0, 0, img.width(), img.height()), omask) qp.end() return img
def getCurrentMaskedImage(self): """ Blend the layer stack up to self (included), taking into account the masks. The method uses the non color managed rPixmap to build the masked image. For convenience, mainly to be able to use its color space buffers, the built image is of type bImage. It is drawn on a container image, instantiated only once. @return: masked image @rtype: bImage """ # init containers if needed. They are instantiated only # once and updated by drawing. if self.parentImage.useHald: return self.getHald() if self.maskedThumbContainer is None: self.maskedThumbContainer = bImage.fromImage( self.getThumb(), parentImage=self.parentImage) if self.maskedImageContainer is None: self.maskedImageContainer = bImage.fromImage( self, parentImage=self.parentImage) if self.parentImage.useThumb: img = self.maskedThumbContainer else: img = self.maskedImageContainer # draw lower stack qp = QPainter(img) top = self.parentImage.getStackIndex(self) bottom = 0 for i, layer in enumerate(self.parentImage.layersStack[bottom:top + 1]): if layer.visible: if i == 0: qp.setCompositionMode(QPainter.CompositionMode_Source) else: qp.setOpacity(layer.opacity) qp.setCompositionMode(layer.compositionMode) if layer.rPixmap is None: layer.rPixmap = QPixmap.fromImage(layer.getCurrentImage( )) # TODO modified 9/12/18 validate qp.drawPixmap(QRect(0, 0, img.width(), img.height()), layer.rPixmap) # clipping if layer.isClipping and layer.maskIsEnabled: # draw mask as opacity mask # mode DestinationIn (set dest opacity to source opacity) qp.setCompositionMode( QPainter.CompositionMode_DestinationIn) omask = vImage.color2OpacityMask(layer.mask) qp.drawImage(QRect(0, 0, img.width(), img.height()), omask) qp.end() return img
def brushStrokePoly(pixmap, poly, brush): """ Draws the brush stroke defined by a QPolygon @param layer: @type layer: @param x: @type x: @param y: @type y: @param r: @type r: """ # draw the stroke if brush['name'] == 'eraser': return # drawing into stroke intermediate layer pxmp_temp = pixmap.copy() qp = QPainter() qp.begin(pxmp_temp) qp.setCompositionMode(qp.CompositionMode_SourceOver) # draw lines x_last, y_last = poly.first().x(), poly.first().y() for i in range(poly.length() - 1): x0, y0 = poly.at(i).x(), poly.at(i).y() x, y = poly.at(i + 1).x(), poly.at(i + 1).y() x_last, y_last = brushFamily.brushStrokeSeg( qp, x_last, y_last, x, y, brush) qp.end() # draw texture aligned with image strokeTex = pxmp_temp p = brush['pattern'] if p is not None: if p.pxmp is not None: strokeTex = pxmp_temp.copy() qp1 = QPainter(strokeTex) qp1.setCompositionMode(qp.CompositionMode_DestinationIn) qp1.setBrush(QBrush(p.pxmp)) qp1.fillRect( QRect(0, 0, strokeTex.width(), strokeTex.height()), QBrush(p.pxmp)) qp1.end() # restore source image and paint # the whole stroke with current brush opacity qp.begin(pixmap) # qp.setCompositionMode(qp.CompositionMode_Source) # qp.drawImage(QPointF(), layer.strokeDest) qp.setOpacity(brush['opacity']) qp.setCompositionMode(qp.CompositionMode_SourceOver) qp.drawPixmap(QPointF(), strokeTex) # pxmp_temp) qp.end()
def paintEvent(self, e): painter = QPainter(self) painter.setOpacity(0.5) canvas = None for scribble in self.scribbles: if scribble.rect: if scribble.blend_with_next: if canvas is None: canvas = Overlay() canvas.add(scribble) else: if canvas: canvas.add(scribble) canvas.paint(painter) canvas = None else: scribble.paint(painter) if canvas: canvas.paint(painter) painter.setOpacity(1) if self.current_wet: self.current_wet.paint(painter) painter.end()
def paintEvent(self, event): # pylint: disable=invalid-name, unused-argument p = QPainter(self) p.setRenderHint(QPainter.Antialiasing, True) p.setPen(Qt.NoPen) track_opacity = self._track_opacity thumb_opacity = 1.0 text_opacity = 1.0 if self.isEnabled(): track_brush = self._track_color[self.isChecked()] thumb_brush = self._thumb_color[self.isChecked()] text_color = self._text_color[self.isChecked()] else: track_opacity *= 0.8 track_brush = self.palette().shadow() thumb_brush = self.palette().mid() text_color = self.palette().shadow().color() p.setBrush(track_brush) p.setOpacity(track_opacity) p.drawRoundedRect( self._margin, self._margin, self.width() - 2 * self._margin, self.height() - 2 * self._margin, self._track_radius, self._track_radius, ) p.setBrush(thumb_brush) p.setOpacity(thumb_opacity) p.drawEllipse( self.offset - self._thumb_radius, self._base_offset - self._thumb_radius, 2 * self._thumb_radius, 2 * self._thumb_radius, ) p.setPen(text_color) p.setOpacity(text_opacity) font = p.font() font.setPixelSize(1.5 * self._thumb_radius) p.setFont(font) p.drawText( QRectF( self.offset - self._thumb_radius, self._base_offset - self._thumb_radius, 2 * self._thumb_radius, 2 * self._thumb_radius, ), Qt.AlignCenter, self._thumb_text[self.isChecked()], )
def paintEvent(self, event): super(RetroDrawWidget, self).paintEvent(event) painter = QPainter(self) rectTarget = self.rect() rectSource = QRect(QPoint(0, 0), self.canvasSize) painter.drawPixmap(rectTarget, self.drawable.qpixmap, rectSource) if self._guide and self._guideEnabled: painter.setOpacity(self._guideOpacity) self._paintZoomedGuide(painter) if self._gridEnabled: painter.setOpacity(self._gridOpacity) painter.drawImage(rectTarget, self.grid, rectTarget) painter.setOpacity(1.0) painter.drawImage(rectTarget, self._scratch, rectTarget) painter.end()
def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setOpacity(self.pixmap_opacity) painter.drawPixmap(0, 0, self.old_pixmap) painter.end()
class IconButton(QWidget): clicked = Signal(bool) def __init__(self, image_path, size=40, checkbox=False): super(IconButton, self).__init__() self.setMinimumSize(size, size) self.setMaximumSize(size, size) self.hover = False self.checkbox = checkbox self.checked = False pixmap_image = "" if os.path.exists(image_path): pixmap_image = image_path pixmap = QPixmap(pixmap_image) self.pixmap = pixmap.scaled(QSize(size, size), Qt.KeepAspectRatio, Qt.SmoothTransformation) self.checked_brush = QBrush(QColor("red")) self.painter = QPainter() def enterEvent(self, event): QApplication.setOverrideCursor(Qt.PointingHandCursor) self.hover = True self.repaint() super(IconButton, self).enterEvent(event) def leaveEvent(self, event): QApplication.restoreOverrideCursor() self.hover = False self.repaint() super(IconButton, self).leaveEvent(event) def mousePressEvent(self, event): if self.checkbox: self.checked = not self.checked self.clicked.emit(self.checked) self.repaint() super(IconButton, self).mousePressEvent(event) def paintEvent(self, event): self.painter.begin(self) self.draw() self.painter.end() def draw(self): rect = self.rect() self.painter.setOpacity(0.5) if self.hover: self.painter.setOpacity(1) if self.checkbox and self.checked: self.painter.setOpacity(1) self.painter.setBrush(self.checked_brush) self.painter.setPen(Qt.NoPen) self.painter.drawRect(rect) self.painter.drawPixmap(rect, self.pixmap)
def draw(self, painter: QPainter, block_length: int): self.pixel_length = block_length / Block.WIDTH self.scroll_brush = QBrush(Qt.blue) self.scroll_pen = QPen(self.scroll_brush, 2 * self.pixel_length) self.acceleration_brush = QBrush(Qt.red) self.acceleration_pen = QPen(self.acceleration_brush, 2 * self.pixel_length) painter.setPen(self.scroll_pen) painter.setBrush(self.scroll_brush) auto_scroll_type_index = self.auto_scroll_row >> 4 auto_scroll_routine_index = self.auto_scroll_row & 0b0001_1111 if auto_scroll_type_index in [ SPIKE_CEILING_SCROLL, UP_TIL_DOOR_SCROLL, WATER_LEVEL_SCROLL, UP_RIGHT_DIAG_SCROLL, ]: # not visualized return elif auto_scroll_type_index not in [ HORIZONTAL_SCROLL_0, HORIZONTAL_SCROLL_1 ]: # illegal value, those appear in the vanilla ROM, though; so error out return first_movement_command_index = (self.rom.int( AScroll_HorizontalInitMove + auto_scroll_routine_index) + 1) % 256 last_movement_command_index = (self.rom.int( AScroll_HorizontalInitMove + auto_scroll_routine_index + 1)) % 256 self.horizontal_speed = 0 self.vertical_speed = 0 self.current_pos = self._determine_auto_scroll_start(block_length) for movement_command_index in range(first_movement_command_index, last_movement_command_index + 1): movement_command = self.rom.int(AScroll_Movement + movement_command_index) movement_repeat = self.rom.int(AScroll_MovementRepeat + movement_command_index) self._execute_movement_command(painter, movement_command, movement_repeat) stop_marker = QRectF(QPoint(0, 0), QSizeF(10, 10) * self.pixel_length) stop_marker.moveCenter(self.current_pos) painter.setPen(Qt.NoPen) painter.drawRect(stop_marker) painter.setPen(self.scroll_pen) painter.setBrush(self.scroll_brush) painter.setOpacity(0.2) painter.drawPolygon(self.screen_polygon)
def draw_mini_map(self, painter, is_full=False): _pixmap_minimap = QPixmap( QSize(min([self.width(), self.height()]), min([self.width(), self.height()]))) if is_full else QPixmap( QSize(350, 350)) if is_full: _p_origin = QPoint((self.width() - _pixmap_minimap.width()) // 2, (self.height() - _pixmap_minimap.height()) // 2) else: _p_origin = QPoint(self.width() - _pixmap_minimap.width(), self.height() - _pixmap_minimap.height()) _pixmap_minimap.fill(QColor('#00284d')) # DRAW DEAD ZONE _dz_radius = self.controller.get_dead_zone_radius() _xy = QPoint(*self.controller.get_dead_zone_center().tolist()) # GET MOBILE OBJECTS _model_ships = self.controller.get_data_from_players(enemy_only=False) _model_bolts = self.controller.get_data_from_bolts() _model_asteroids = self.controller.get_data_from_asteroids() # SET MINI MAP _w_ratio = _pixmap_minimap.width() / (PartyConst.WIDTH * 1.25) _h_ratio = _pixmap_minimap.height() / (PartyConst.HEIGHT * 1.25) _minimap_painter = QPainter(_pixmap_minimap) _minimap_painter.setOpacity(1) for _ship in _model_ships: _w = _ship.radius * _w_ratio if _w < 3: _w = 3 _x, _y = int( _ship.x * _w_ratio) + _pixmap_minimap.width() // 2, int( _ship.y * _h_ratio) + _pixmap_minimap.height() // 2 if _ship.is_alive: _factor_1 = (time.time() * 2) % 2 _factor_2 = (time.time() * 2 + .5) % 2 _minimap_painter.setPen( QPen(QColor('#ffffff'), 1, Qt.SolidLine)) _minimap_painter.setOpacity(1 - _factor_1) _minimap_painter.drawEllipse(QPoint(_x, _y), int(20 * _factor_1), int(20 * _factor_1)) _minimap_painter.setOpacity(1 - _factor_2) _minimap_painter.drawEllipse(QPoint(_x, _y), int(20 * _factor_2), int(20 * _factor_2)) _minimap_painter.setPen( QPen(QColor(*_ship.color), _w, Qt.SolidLine)) _minimap_painter.setOpacity(1) _minimap_painter.drawPoint(_x, _y) for _astroid in _model_asteroids: _w = _astroid.radius * _w_ratio if _w < 5: _w = 5 _pen = QPen(QColor('#ffb86c'), _w, Qt.SolidLine) _pen.setCapStyle(Qt.RoundCap) _minimap_painter.setPen(_pen) _x, _y = int( _astroid.x * _w_ratio) + _pixmap_minimap.width() // 2, int( _astroid.y * _h_ratio) + _pixmap_minimap.height() // 2 _minimap_painter.drawPoint(_x, _y) for _bolt in _model_bolts: _w = _bolt.radius * _w_ratio if _w < 1: _w = 1 _minimap_painter.setPen(QPen(QColor('#ff5555'), _w, Qt.SolidLine)) _x, _y = int( _bolt.x * _w_ratio) + _pixmap_minimap.width() // 2, int( _bolt.y * _h_ratio) + _pixmap_minimap.height() // 2 _minimap_painter.drawPoint(_x, _y) _xy.setX(_xy.x() * _w_ratio + _pixmap_minimap.width() // 2) _xy.setY(_xy.y() * _h_ratio + _pixmap_minimap.height() // 2) _minimap_painter.setPen(QPen(QColor('#8be9fd'), 3, Qt.SolidLine)) _minimap_painter.drawEllipse(_xy, _dz_radius * _w_ratio, _dz_radius * _h_ratio) if not is_full: _minimap_painter.setPen(QPen(QColor('#ffffff'), 1, Qt.SolidLine)) _x = -self.camera_pst.x() * _w_ratio + _pixmap_minimap.width() // 2 _y = self.camera_pst.y() * _h_ratio + _pixmap_minimap.height() // 2 _w = self.width() * _w_ratio _h = self.height() * _h_ratio _minimap_painter.drawRect(_x, _y - _h, _w, _h) _minimap_painter.end() _pixmap_minimap = _pixmap_minimap.transformed(QTransform().scale( 1, -1)) painter.setOpacity(1 if is_full else .75) painter.drawPixmap(_p_origin, _pixmap_minimap)
def paintEvent(self, event): p = QPainter(self) p.setOpacity(0)