def selectpoly_copy(self): """ Copy a polygon region from the current image, returning it. Create a mask for the selected area, and use it to blank out non-selected regions. Then get the bounding rect of the selection and crop to produce the smallest possible image. :return: QPixmap of the copied region. """ self.timer_cleanup() pixmap = self.pixmap().copy() bitmap = QBitmap(*CANVAS_DIMENSIONS) bitmap.clear() # Starts with random data visible. p = QPainter(bitmap) # Construct a mask where the user selected area will be kept, # the rest removed from the image is transparent. userpoly = QPolygon(self.history_pos + [self.current_pos]) p.setPen(QPen(Qt.color1)) p.setBrush(QBrush(Qt.color1)) # Solid color, Qt.color1 == bit on. p.drawPolygon(userpoly) p.end() # Set our created mask on the image. pixmap.setMask(bitmap) # Calculate the bounding rect and return a copy of that region. return pixmap.copy(userpoly.boundingRect())
def _draw_flags_icon(mutable: bool, persistent: bool, icon_size: int) -> QPixmap: """ Combines icons into a single large icon and renders it into a pixmap of specified size. This operation is quite resource-consuming (we're drawing pictures after all, ask Picasso), so we cache the results in a LRU cache. """ icon_name = "edit" if mutable else "lock" mutability = get_icon_pixmap(icon_name, icon_size) # https://youtu.be/AX2uz2XYkbo?t=21s icon_name = "save" if persistent else "random-access-memory" persistence = get_icon_pixmap(icon_name, icon_size) icon_size_rect = QRect(0, 0, icon_size, icon_size) pixmap = QPixmap(icon_size * 2, icon_size) mask = QBitmap(pixmap.width(), pixmap.height()) mask.clear() pixmap.setMask(mask) painter = QPainter(pixmap) painter.drawPixmap(icon_size_rect, mutability, icon_size_rect) painter.drawPixmap( QRect(icon_size, 0, icon_size, icon_size), persistence, icon_size_rect ) return pixmap