def add_gateway(self, gateway): basename = os.path.basename(os.path.normpath(gateway.nodedir)) icon = QIcon(os.path.join(gateway.nodedir, "icon")) if not icon.availableSizes(): icon = QIcon(resource("tahoe-lafs.png")) self.insertItem(0, icon, basename, gateway) self.setCurrentIndex(0) self.current_index = 0
def setIcon(self, icon): super(RecordButton, self).setIcon(icon) on_icon = QIcon(icon) off_icon = QIcon(icon) for size in off_icon.availableSizes(QIcon.Normal, QIcon.On): pixmap = off_icon.pixmap(size, QIcon.Normal, QIcon.Off) off_icon.addPixmap(pixmap, QIcon.Normal, QIcon.On) self.animation_icons = [on_icon, off_icon]
def setIcon(self, icon): super(RecordButton, self).setIcon(icon) on_icon = QIcon(icon) off_icon = QIcon(icon) for size in off_icon.availableSizes(QIcon.Normal, QIcon.On): pixmap = off_icon.pixmap(size, QIcon.Normal, QIcon.Off) off_icon.addPixmap(pixmap, QIcon.Normal, QIcon.On) self.animation_icons = [on_icon, off_icon]
def populate(self, gateways): self.clear() for gateway in gateways: basename = os.path.basename(os.path.normpath(gateway.nodedir)) icon = QIcon(os.path.join(gateway.nodedir, 'icon')) if not icon.availableSizes(): icon = QIcon(resource('tahoe-lafs.png')) self.addItem(icon, basename, gateway) self.insertSeparator(self.count()) self.addItem(" Add new...")
def color_icon2gray_icon(cls, icon: QIcon): # TODO: implement for RGB icons pixmap = icon.pixmap(icon.availableSizes()[0]) img_array = cls.Qpixmap2array(pixmap) *_, alpha = cv2.split(img_array) gray_layer = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY) gray_img = cv2.merge((gray_layer, gray_layer, gray_layer, alpha)) height, width, channel = gray_img.shape bytesPerLine = 4 * width qImg = QImage(gray_img.data, width, height, bytesPerLine, QImage.Format_RGBA8888) pixmap = QtGui.QPixmap.fromImage(qImg) return QIcon(pixmap)
def load_icon(name: AutoKeyIcon) -> QIcon: file_path = ICON_PATH_PREFIX + "/" + name.value icon = QIcon(file_path) if not icon.availableSizes() and file_path.endswith(".svg"): # FIXME: Work around Qt Bug: https://bugreports.qt.io/browse/QTBUG-63187 # Manually render the SVG to some common icon sizes. icon = QIcon() # Discard the bugged QIcon renderer = QSvgRenderer(file_path) for size in (16, 22, 24, 32, 64, 128): pixmap = QPixmap(QSize(size, size)) pixmap.fill(QColor(255, 255, 255, 0)) renderer.render(QPainter(pixmap)) icon.addPixmap(pixmap) return icon
def load_icon(name: AutoKeyIcon) -> QIcon: file_path = ICON_PATH_PREFIX + "/" + name.value icon = QIcon(file_path) if not icon.availableSizes() and file_path.endswith(".svg"): # FIXME: Work around Qt Bug: https://bugreports.qt.io/browse/QTBUG-63187 # Manually render the SVG to some common icon sizes. icon = QIcon() # Discard the bugged QIcon renderer = QSvgRenderer(file_path) for size in (16, 22, 24, 32, 64, 128): pixmap = QPixmap(QSize(size, size)) pixmap.fill(QColor(255, 255, 255, 0)) renderer.render(QPainter(pixmap)) icon.addPixmap(pixmap) return icon
def load_icon(name: str) -> QIcon: """ Load a QIcon with the given file name. Files are loaded from the ICON_PATH_PREFIX, which depends on the installation style. """ file_path = ICON_PATH_PREFIX + "/" + name icon = QIcon(file_path) if not icon.availableSizes() and file_path.endswith(".svg"): # FIXME: Work around Qt Bug: https://bugreports.qt.io/browse/QTBUG-63187 # Manually render the SVG to some common icon sizes. icon = QIcon() # Discard the bugged QIcon renderer = QSvgRenderer(file_path) for size in (16, 22, 24, 32, 64, 128): pixmap = QPixmap(QSize(size, size)) pixmap.fill(QColor(255, 255, 255, 0)) renderer.render(QPainter(pixmap)) icon.addPixmap(pixmap) return icon
def drawIconWithShadow(icon: QIcon, rect: QRect, p: QPainter, iconMode: QIcon.Mode = None, dipRadius: int = None, color: QColor = None, dipOffset: QPoint = None): if iconMode is None: iconMode = QIcon.Normal if color is None: color = QColor(0, 0, 0, 130) if dipRadius is None: dipRadius = 3 if dipOffset is None: dipOffset = QPoint(1, -2) devicePixelRatio: int = p.device().devicePixelRatio() pixmapName = "icon %s %s %s %s" % (icon.cacheKey(), iconMode, rect.height(), devicePixelRatio) cache = QPixmapCache.find(pixmapName) if cache is None: # High-dpi support: The in parameters (rect, radius, offset) are in # device-independent pixels. The call to QIcon::pixmap() below might # return a high-dpi pixmap, which will in that case have a devicePixelRatio # different than 1. The shadow drawing caluculations are done in device # pixels. window = p.device().window().windowHandle() px = icon.pixmap(window, rect.size(), iconMode) radius = dipRadius * devicePixelRatio offset = dipOffset * devicePixelRatio cache = QPixmap(px.size() + QSize(radius * 2, radius * 2)) cache.fill(Qt.transparent) cachePainter = QPainter(cache) if iconMode == QIcon.Disabled: hasDisabledState = len(icon.availableSizes()) == len( icon.availableSizes(QIcon.Disabled)) if not hasDisabledState: px = disabledSideBarIcon(icon.pixmap(window, rect.size())) elif TOOLBAR_ICON_SHADOW: # Draw shadow tmp = QImage(px.size() + QSize(radius * 2, radius * 2 + 1), QImage.Format_ARGB32_Premultiplied) tmp.fill(Qt.transparent) tmpPainter = QPainter(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_Source) tmpPainter.drawPixmap( QRect(radius, radius, px.width(), px.height()), px) tmpPainter.end() # blur the alpha channel blurred = QImage(tmp.size(), QImage.Format_ARGB32_Premultiplied) blurred.fill(Qt.transparent) blurPainter = QPainter(blurred) #qt_blurImage(blurPainter, tmp, radius, False, True) # implement qt_blurImage via QLabel with QGraphicsBlurEffect # FIXME: alignment is broken scene = QGraphicsScene() item = QGraphicsPixmapItem(QPixmap.fromImage(tmp)) effect = QGraphicsBlurEffect() effect.setBlurRadius(radius) item.setGraphicsEffect(effect) scene.addItem(item) scene.render(blurPainter) blurPainter.end() tmp = blurred # blacken the image... tmpPainter.begin(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_SourceIn) tmpPainter.fillRect(tmp.rect(), color) tmpPainter.end() tmpPainter.begin(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_SourceIn) tmpPainter.fillRect(tmp.rect(), color) tmpPainter.end() # draw the blurred drop shadow... cachePainter.drawImage( QRect(0, 0, cache.rect().width(), cache.rect().height()), tmp) # Draw the actual pixmap... cachePainter.drawPixmap( QRect( QPoint(radius, radius) + offset, QSize(px.width(), px.height())), px) cachePainter.end() cache.setDevicePixelRatio(devicePixelRatio) QPixmapCache.insert(pixmapName, cache) targetRect = cache.rect() targetRect.setSize(targetRect.size() / cache.devicePixelRatio()) targetRect.moveCenter(rect.center() - dipOffset) p.drawPixmap(targetRect, cache)
def _get_icon_size(self, icon: QIcon) -> QSize: sizes = icon.availableSizes() return sizes[-1] if sizes else self.DEFAULT_ICON_SIZE