def colored_icon(name, color): pix = QPixmap(name) mask = pix.createMaskFromColor(QColor(Qt.black), Qt.MaskOutColor) if isinstance(color, str): color = QColor(color) pix.fill(color) pix.setMask(mask) return QIcon(pix)
class Icon(): def __init__(self, filename, color='#FFF'): super().__init__() self.pixmap = QPixmap('./icons/{}'.format(filename)) mask = self.pixmap.createMaskFromColor(QColor('black'), Qt.MaskOutColor) self.pixmap.fill((QColor(color))) self.pixmap.setMask(mask) def getIcon(self): return QIcon(self.pixmap)
def __init__(self, filename="", tint_color=None): palette = QApplication.instance().palette() px = QPixmap(filename) pxr = QPixmap(px.size()) if not tint_color: pxr.fill(palette.color(QPalette.ButtonText)) else: pxr.fill(tint_color) pxr.setMask(px.createMaskFromColor((Qt.transparent))) super().__init__(pxr)
def __init__(self, mask_path: Path): mask_pix = QPixmap() if not mask_pix.load(str(mask_path)): raise ValueError(f"Failed to load mask from {mask_path}") self.size = mask_pix.size() logger.debug("size: %s", self.size) self.clip_region = QRegion( mask_pix.createMaskFromColor(QColor("white"), Qt.MaskInColor) ) logger.debug("clip_region: %s", self.clip_region) logger.debug("clip_region bounding rect: %s", self.clip_region.boundingRect())
def chromecast_device_connected(self, device): px = QPixmap('icons/cast.svg') pxr = QPixmap(px.size()) pxr.fill(Qt.lightGray) pxr.setMask(px.createMaskFromColor((Qt.transparent))) self.window().cast_label_pixmap.setPixmap(pxr) self.window().cast_label_pixmap.setScaledContents(True) self.window().cast_label.setText(self.tr("Connected to: {0}".format(device.device.friendly_name))) self.window().cast_label_pixmap.setHidden(False) self.window().cast_label.setHidden(False) self.chromecast_connected.emit(device) if self.player.path: self.chromecast_manager.play_url(self.player.path)
def load_themed_icon(file_path: str, color: QColor) -> QIcon: """ Loads an icon, paints it the given color, and builds an icon :param file_path: Path where the icon is stored, to be loaded as a pixmap :param color: Color to paint pixmap :return: a new QIcon containing the photo at file_path """ photo_pixmap = QPixmap(file_path) themed_pixmap = QPixmap(photo_pixmap.size()) themed_pixmap.fill(color) themed_pixmap.setMask(photo_pixmap.createMaskFromColor(Qt.transparent)) return QIcon(themed_pixmap)
def __init__(self): super().__init__() pixmap = QPixmap(imageDir + 'home.png') mask = pixmap.createMaskFromColor(QColor('transparent'), Qt.MaskInColor) pixmap.fill(QColor(150, 150, 150)) pixmap.setMask(mask) icon = QIcon(pixmap) self.setIcon(icon) self.setFixedSize(SIZE) self.setIconSize(SIZE) self.setStyleSheet("border: 0px solid black; background: transparent")
def load_icon(path): """Returns a QIcon with the image at path as the Normal state and the image recoloured as the Disabled state. """ pixmap = QPixmap(path) icon = QIcon() icon.addPixmap(pixmap) # Create disabled mask = pixmap.createMaskFromColor(QColor(0xff, 0xff, 0xff), Qt.MaskOutColor) p = QPainter(pixmap) p.setPen(QColor(0xaa, 0xaa, 0xaa)) p.drawPixmap(pixmap.rect(), mask, mask.rect()) p.end() icon.addPixmap(pixmap, QIcon.Disabled) return icon
def add_label(self, attr_name: str, width: int, height: int, x: int = 0, y: int = 0, set_pixmap: bool = False, parent: Widget = None, ignore_geometry: bool = False): label = Label() if parent is not None: label.setParent(parent) if not ignore_geometry: if parent is not None: label_geo = *self.relative_center(parent, tr_x=x, tr_y=y, width=width, height=height), width, height else: label_geo = self.center_x(width, x), self.center_y(height, y), width, height label.setGeometry(*label_geo) if set_pixmap: pixmap = QPixmap(width, height) mask = pixmap.createMaskFromColor(QColor(0, 0, 0), Qt.MaskInColor) pixmap.setMask(mask) label.setPixmap(pixmap) if not hasattr(self, attr_name): setattr(self, attr_name, label) else: raise ValueError('Label Attribute: attr_name = ' + attr_name + ' | is Already Exists')
def set_table_icons(self, table_icon_list): """The list 'table_icon_list' contains tuples (file_name_short, marker_name, face, color) [description] Arguments: - table_icon_list {[type]} -- [description] """ self.DataSettreeWidget.blockSignals( True ) #avoid triggering 'itemChanged' signal that causes a call to do_plot() for fname, marker_name, face, color in table_icon_list: item = self.DataSettreeWidget.findItems( fname, Qt.MatchCaseSensitive, column=0) #returns list of items matching file name if item: #paint icon folder = ':/Markers/Images/Matplotlib_markers/' if face == 'none': #empty symbol marker_path = folder + 'marker_%s' % marker_name else: #filled symbol marker_path = folder + 'marker_filled_%s' % marker_name qp = QPixmap(marker_path) mask = qp.createMaskFromColor(QColor(0, 0, 0), Qt.MaskOutColor) qpainter = QPainter() qpainter.begin(qp) qpainter.setPen( QColor( int(255 * color[0]), int(255 * color[1]), int(255 * color[2]), 255)) qpainter.drawPixmap(qp.rect(), mask, qp.rect()) qpainter.end() item[0].setIcon(0, QIcon(qp)) self.DataSettreeWidget.blockSignals(False)
def _color_icon(icon, color_code): pix_map = QPixmap(icon) mask = pix_map.createMaskFromColor(QColor('black'), Qt.MaskOutColor) pix_map.fill(QColor(color_code)) pix_map.setMask(mask) return QIcon(pix_map)
class Bullet(QGraphicsPixmapItem): # 继承Qt的QGraphicsPixmapItem类 # 默认初始化,参数为火力大小、子弹颜色、所属机器人 def __init__(self, power, color, bot): QGraphicsPixmapItem.__init__(self) # graphics self.maskColor = QColor(255, 128, 0) # 用Qt的QColor方法设置颜色 self.pixmap = QPixmap( os.getcwd() + "/robotImages/blast.png") # 用Qt的QPixmap方法。设置炮弹的图片 self.setPixmap(self.pixmap) # QGraphicsPixmapItem中的方法 self.setColour(color) # 下边的自定义函数 self.isfired = False # physics self.width = self.boundingRect().width() self.height = self.boundingRect().height() if power <= 0.5: power = 0.5 elif power >= 10: power = 10 self.power = power bsize = power if power < 3: bsize = 4 self.pixmap = self.pixmap.scaled(bsize, bsize) self.setPixmap(self.pixmap) # QGraphicsPixmapItem中的方法 self.robot = bot #子弹的“手动”初始化方法 def init(self, pos, angle, scene): self.angle = angle # self.setPos(pos) # setPos为qt中QGraphicsPixmapItem的方法,pos为一个二维坐标。x,y self.scene = scene self.isfired = True def setColour(self, color): mask = self.pixmap.createMaskFromColor(self.maskColor, 1) p = QPainter(self.pixmap) p.setPen(color) p.drawPixmap(self.pixmap.rect(), mask, mask.rect()) p.end() self.setPixmap(self.pixmap) # QGraphicsPixmapItem中的方法 self.maskColor = color # 子弹的前进逻辑,当本类的isfired这个tag为真的时候,执行。 def advance(self, i): if self.isfired: pos = self.pos() x = pos.x() y = pos.y() dx = -math.sin(math.radians(self.angle)) * 10.0 dy = math.cos(math.radians(self.angle)) * 10.0 self.setPos(x + dx, y + dy) if x < 0 or y < 0 or x > self.scene.width or y > self.scene.height: self.robot.onBulletMiss(id(self)) self.scene.removeItem(self) self.robot.removeMyProtectedItem(self)
def data(self, index=QModelIndex(), role=Qt.DisplayRole): if role not in [Qt.DisplayRole, Qt.DecorationRole, Qt.EditRole]: return QVariant() style = self._styles[index.row()] fore = QColor(*style.fore) back = QColor(*style.back) border_color = QColor(*style.color) if role == Qt.EditRole: if index.column() == 0: if style.isFilled(): return 0 # solid special case return style.pattern elif index.column() == 1: return fore elif index.column() == 2: return back elif index.column() == 3: if style.style == -4105: return 0 # Auto special case elif style.style == -4142: return 9 # None special case return style.dash # see also MsoLineDashStyle elif index.column() == 4: return border_color elif index.column() == 5: if style.weight == -4138: return 3 # Medium special case return style.weight # see also XlBorderWeight else: return QVariant() elif role == Qt.DecorationRole: # Pattern if index.column() == 0: img = None if style.isFilled(): img = QPixmap(48, 48) img.fill(Qt.black) else: img = self.patternImage.copy(48 * (style.pattern - 1), 0, 48, 48) foreMask = img.createMaskFromColor(QColor(Qt.white)) backMask = img.createMaskFromColor(QColor(Qt.black)) p = QPainter(img) p.setPen(fore) p.drawPixmap(img.rect(), foreMask, foreMask.rect()) p.setPen(back) p.drawPixmap(img.rect(), backMask, backMask.rect()) p.end() return img elif role == Qt.DisplayRole: # Line style if index.column() == 3: value = self.data(index, role=Qt.EditRole) if value not in range(10): return self.tr("Unknown") return { 0: self.tr("Auto"), 1: self.tr("Solid"), 2: self.tr("SquareDot"), 3: self.tr("RoundDot"), 4: self.tr("LineDash"), 5: self.tr("DashDot"), 6: self.tr("DashDotDot"), 7: self.tr("LongDash"), 8: self.tr("LongDashDot"), 9: self.tr("None"), }[value] elif index.column() == 5: return { 1: self.tr("Hairline"), 2: self.tr("Thin"), 3: self.tr("Medium"), 4: self.tr("Thick"), }[self.data(index, role=Qt.EditRole)] # Fore if index.column() == 1: return fore # Back elif index.column() == 2: return back # border color elif index.column() == 4: return border_color return QVariant()