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

PR: Make QIcon.ThemeIcon enum for Qt < 6.7 #503

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

StSav012
Copy link
Contributor

In Qt 6.7, they finally added icons from https://specifications.freedesktop.org/icon-naming-spec/latest/ as a collection of constants. Years of looking the names up are over.

I haven't implemented NThemeIcons because I have no idea how to do it for string constants.

Overview of the icons
import sys

from qtpy import PYQT4, PYQT5, PYQT6
from qtpy.QtCore import Qt
from qtpy.QtGui import QIcon
from qtpy.QtWidgets import (
    QApplication,
    QGridLayout,
    QScrollArea,
    QToolButton,
    QWidget,
)


def kebab_case(name: str) -> str:
    """Convert a PascalCase name or a camelCase one to a kebab-case synonym."""
    return "".join((("-" + c.casefold()) if c.isupper() else c) for c in name).lstrip("-")


# A hotfix to be PRed later, see https://t.me/qtforpython/24462
if PYQT4 or PYQT5 or PYQT6:
    # Make the app instance last
    class QApplication(QApplication):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            QApplication._instance = QApplication.instance()


QApplication(sys.argv)
w: QWidget = QWidget()
layout: QGridLayout = QGridLayout()
w.setLayout(layout)

icon_button: QToolButton
for index, (key, value) in enumerate(QIcon.ThemeIcon.__members__.items()):
    icon_button = QToolButton(w)
    icon_button.setIcon(QIcon.fromTheme(value))
    icon_button.setText(key)
    icon_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
    icon_button.clicked.connect(lambda is_pressed, _key=key: print(_key) or QApplication.clipboard().setText(_key))
    layout.addWidget(icon_button, index, 0)

    free_desktop_org_key: str = kebab_case(key)
    icon_button = QToolButton(w)
    icon_button.setIcon(QIcon.fromTheme(free_desktop_org_key))
    icon_button.setText(free_desktop_org_key)
    icon_button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
    icon_button.clicked.connect(
        lambda is_pressed, _key=free_desktop_org_key: print(_key) or QApplication.clipboard().setText(_key),
    )
    layout.addWidget(icon_button, index, 1)

    # Test that it's the same icon
    assert QIcon.fromTheme(value).cacheKey() == QIcon.fromTheme(free_desktop_org_key).cacheKey()

s: QScrollArea = QScrollArea()
s.setWidget(w)
s.show()
QApplication.exec()

@coveralls
Copy link

coveralls commented Nov 29, 2024

Coverage Status

coverage: 91.311% (+1.1%) from 90.18%
when pulling 12d1f31 on StSav012:theme-icon-enum
into cd0b49b on spyder-ide:master.

@dalthviz dalthviz added this to the v2.5.0 milestone Dec 13, 2024
@dalthviz dalthviz changed the title Make QIcon.ThemeIcon enum for Qt < 6.7 PR: Make QIcon.ThemeIcon enum for Qt < 6.7 Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants