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

feat: graceful offline fallback for qiconify #251

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/superqt/iconify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

from qtpy.QtCore import QSize
from qtpy.QtGui import QIcon
from qtpy.QtCore import QSize, Qt
from qtpy.QtGui import QIcon, QPainter, QPixmap
from qtpy.QtWidgets import QApplication

if TYPE_CHECKING:
from typing import Literal
Expand Down Expand Up @@ -122,5 +124,25 @@
state : QIcon.State, optional
State specified for the icon, passed to `QIcon.addFile`.
"""
path = svg_path(*key, color=color, flip=flip, rotate=rotate, dir=dir)
self.addFile(str(path), size or QSize(), mode, state)
try:
path = svg_path(*key, color=color, flip=flip, rotate=rotate, dir=dir)
except OSError:
warnings.warn(

Check warning on line 130 in src/superqt/iconify/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/superqt/iconify/__init__.py#L129-L130

Added lines #L129 - L130 were not covered by tests
f"Unable to connect to internet, and icon {key} not cached.",
stacklevel=2,
)
self._draw_text_fallback(key)

Check warning on line 134 in src/superqt/iconify/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/superqt/iconify/__init__.py#L134

Added line #L134 was not covered by tests
else:
self.addFile(str(path), size or QSize(), mode, state)

def _draw_text_fallback(self, key: tuple[str, ...]) -> None:
if style := QApplication.style():
pixmap = style.standardPixmap(style.StandardPixmap.SP_MessageBoxQuestion)
else:
pixmap = QPixmap(18, 18)
pixmap.fill(Qt.GlobalColor.transparent)
painter = QPainter(pixmap)
painter.drawText(pixmap.rect(), Qt.AlignmentFlag.AlignCenter, "?")
painter.end()

self.addPixmap(pixmap)