superqt icon indicating copy to clipboard operation
superqt copied to clipboard

Fonticon color matching theme

Open Czaki opened this issue 2 years ago • 4 comments

I try to use superqt.fonticon in napari widget. I observe that icons are dark when enabled. It does not look best when selecting a dark theme.

Is there a way to such an icon automatically match the text color for the current qss? Or does the QSS for the theme need to be updated to handle icons?

Czaki avatar Apr 17 '23 09:04 Czaki

I think you'd want to use the setTextIcon function in that case (rather than using the icon() -> Qicon function).

tlambert03 avatar Apr 18 '23 16:04 tlambert03

Color works... obraz

The problem is caused because the widget has not yet been added to the layout. Of course, I could workaround it by using a hardcode size of the font, but there should be a better way.

Czaki avatar Apr 18 '23 16:04 Czaki

hey @Czaki, I'm thinking about this a bit more, for both fonticon and iconify QIcons. One thing we can do here is to install an event filter on the object that listens for PaletteChange events (and, optionally, parent change events) and then recolors the icon. Fortunately, if you set the stylesheet on a widget to change the background color (like napari's theme for example) it will emit a palette change event and change the palette background in most cases. So this is likely compatible with napari's stylesheet theming approach too.

Stripped of all the error catching conditionals it would look something like this:

class PaletteEventFilter(QObject):
    def eventFilter(self, obj: QObject, event: QEvent) -> bool:
        """Change icon color when palette changes."""
        if event.type() == QEvent.Type.PaletteChange:
            pal = (
                obj.palette() if hasattr(obj, "palette") else QGuiApplication.palette()
            )
            new_color = pal.color(QPalette.ColorRole.ButtonText)
            new_icon = self.getNewIcon(obj, new_color)
            obj.setIcon(new_icon)
        return False

    def getNewIcon(self, obj: QObject, color: QColor) -> QIcon | None:
        """Return an instance of QIcon suitable for obj using `color`."""

That event filter could be installed on any QObject that supports icon(), setIcon(). The getNewIcon method would depend on the object and exactly how the icon should be recolored.

Want me to put together a more complete proposal like that?

tlambert03 avatar Nov 07 '23 13:11 tlambert03

It looks promising. I do not know about this event.

Czaki avatar Nov 07 '23 15:11 Czaki