Skip to content

Icon

Base classes

Name Children Inherits
SerializeMixin
prettyqt.utils.serializemixin
QIcon
PySide6.QtGui
QIcon(self, /) -> None

â‹” Inheritance diagram

graph TD
  3907960597["gui.Icon"]
  69336483["utils.SerializeMixin"]
  2751680898["builtins.object"]
  83249570["QtGui.QIcon"]
  37984305["Shiboken.Object"]
  69336483 --> 3907960597
  2751680898 --> 69336483
  83249570 --> 3907960597
  37984305 --> 83249570
  2751680898 --> 37984305

🛈 DocStrings

Bases: SerializeMixin, QIcon

Scalable icons in different modes and states.

Source code in prettyqt/gui/icon.py
class Icon(serializemixin.SerializeMixin, gui.QIcon):
    """Scalable icons in different modes and states."""

    def __repr__(self):
        return get_repr(self)

    def __bool__(self):
        return not self.isNull()

    def __getstate__(self):
        pixmap = self.pixmap(256, 256)
        return bytes(gui.Pixmap(pixmap))

    def __setstate__(self, ba):
        px = gui.Pixmap()
        px.__setstate__(ba)
        self.add_pixmap(px)

    @classmethod
    def for_color(cls, color_str: str) -> Self:
        color = gui.Color(color_str)
        bitmap = gui.Pixmap(16, 16)
        bitmap.fill(color)
        return cls(bitmap)

    @classmethod
    def from_char(cls, char: str, background="black", color="white") -> Self:
        """Create a QIcon with a given character."""
        icon = cls()
        for size in (16, 32, 64):
            px = gui.Pixmap.create_char(
                char, background=background, color=color, size=size
            )
            icon.addPixmap(px)
        return icon

    @classmethod
    def for_mimetype(cls, path: os.PathLike[str] | str) -> Self | None:
        """Try to create an icon from theme using the file mimetype.

        Returns None if there is no theme icon for the mimetype.
        """
        path = os.fspath(path)
        if mime := mimetypes.guess_type(path)[0]:
            name = mime.replace("/", "-")
            if cls.hasThemeIcon(name) and not (icon := cls(cls.fromTheme(name))).isNull():
                return icon
        return None

    @classmethod
    def from_theme(
        cls,
        name: ThemeIconStr | gui.QIcon.ThemeIcon | str,
        fallback: gui.QIcon | None = None,
    ) -> Self:
        """Create an icon from a theme name or Qt 6.7+ ThemeIcon."""
        if isinstance(name, str) and name in THEME_ICON:
            icon_arg: gui.QIcon.ThemeIcon | str = THEME_ICON[name]
        else:
            icon_arg = name
        if fallback is None:
            return cls(cls.fromTheme(icon_arg))
        return cls(cls.fromTheme(icon_arg, fallback))

    @classmethod
    def from_image(cls, image: gui.QImage) -> Self:
        return cls(gui.Pixmap.fromImage(image))

    def get_available_sizes(
        self,
        mode: ModeStr | gui.QIcon.Mode = "normal",
        state: StateStr | gui.QIcon.State = "off",
    ) -> list[core.Size]:
        m = MODE.get_enum_value(mode)
        s = STATE.get_enum_value(state)
        return [core.Size(i) for i in self.availableSizes(m, s)]

    def add_pixmap(
        self,
        data: core.QByteArray | gui.QPixmap | bytes,
        mode: ModeStr | gui.QIcon.Mode = "normal",
        state: StateStr | gui.QIcon.State = "off",
    ):
        if isinstance(data, bytes):
            data = core.QByteArray(data)
        if isinstance(data, core.QByteArray):
            pixmap = gui.QPixmap()
            pixmap.loadFromData(data)
        else:
            pixmap = data
        self.addPixmap(pixmap, MODE.get_enum_value(mode), STATE.get_enum_value(state))

    def get_pixmap(
        self,
        size: datatypes.SizeType | int,
        mode: ModeStr | gui.QIcon.Mode = "normal",
        state: StateStr | gui.QIcon.State = "off",
    ) -> gui.Pixmap:
        sz = datatypes.to_size(size)
        return gui.Pixmap(
            self.pixmap(sz, MODE.get_enum_value(mode), STATE.get_enum_value(state))
        )

    def get_actual_size(
        self,
        size: datatypes.SizeType,
        mode: ModeStr | gui.QIcon.Mode = "normal",
        state: StateStr | gui.QIcon.State = "off",
    ) -> core.Size:
        sz = datatypes.to_size(size)
        m = MODE.get_enum_value(mode)
        s = STATE.get_enum_value(state)
        actual_size = self.actualSize(sz, m, s)
        return core.Size(actual_size)

for_mimetype(path: os.PathLike[str] | str) -> Self | None classmethod

Try to create an icon from theme using the file mimetype.

Returns None if there is no theme icon for the mimetype.

Source code in prettyqt/gui/icon.py
@classmethod
def for_mimetype(cls, path: os.PathLike[str] | str) -> Self | None:
    """Try to create an icon from theme using the file mimetype.

    Returns None if there is no theme icon for the mimetype.
    """
    path = os.fspath(path)
    if mime := mimetypes.guess_type(path)[0]:
        name = mime.replace("/", "-")
        if cls.hasThemeIcon(name) and not (icon := cls(cls.fromTheme(name))).isNull():
            return icon
    return None

from_char(char: str, background='black', color='white') -> Self classmethod

Create a QIcon with a given character.

Source code in prettyqt/gui/icon.py
@classmethod
def from_char(cls, char: str, background="black", color="white") -> Self:
    """Create a QIcon with a given character."""
    icon = cls()
    for size in (16, 32, 64):
        px = gui.Pixmap.create_char(
            char, background=background, color=color, size=size
        )
        icon.addPixmap(px)
    return icon

from_theme(name: ThemeIconStr | gui.QIcon.ThemeIcon | str, fallback: gui.QIcon | None = None) -> Self classmethod

Create an icon from a theme name or Qt 6.7+ ThemeIcon.

Source code in prettyqt/gui/icon.py
@classmethod
def from_theme(
    cls,
    name: ThemeIconStr | gui.QIcon.ThemeIcon | str,
    fallback: gui.QIcon | None = None,
) -> Self:
    """Create an icon from a theme name or Qt 6.7+ ThemeIcon."""
    if isinstance(name, str) and name in THEME_ICON:
        icon_arg: gui.QIcon.ThemeIcon | str = THEME_ICON[name]
    else:
        icon_arg = name
    if fallback is None:
        return cls(cls.fromTheme(icon_arg))
    return cls(cls.fromTheme(icon_arg, fallback))

:fa-brands-github: Show source on GitHub