Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fonticon color matching theme #159

Open
Czaki opened this issue Apr 17, 2023 · 4 comments
Open

Fonticon color matching theme #159

Czaki opened this issue Apr 17, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@Czaki
Copy link
Contributor

Czaki commented Apr 17, 2023

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?

@tlambert03
Copy link
Member

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

@Czaki
Copy link
Contributor Author

Czaki commented Apr 18, 2023

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.

@tlambert03 tlambert03 added the enhancement New feature or request label May 10, 2023
@tlambert03
Copy link
Member

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?

@Czaki
Copy link
Contributor Author

Czaki commented Nov 7, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants