Skip to content

Commit

Permalink
Fix icon loading
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Sep 30, 2024
1 parent 173e336 commit 9b2d5a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/napari_imagej/widgets/result_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
SearchResults are grouped by the SciJava Searcher that created them.
"""

from __future__ import annotations

from typing import Dict, List

from qtpy.QtCore import Qt, Signal
Expand All @@ -13,7 +15,7 @@

from napari_imagej.java import ij, jc
from napari_imagej.utilities.logging import log_debug
from napari_imagej.widgets.widget_utils import _getIcon, python_actions_for
from napari_imagej.widgets.widget_utils import _get_icon, python_actions_for


class SearcherTreeView(QTreeView):
Expand Down Expand Up @@ -106,7 +108,7 @@ def __init__(self, result: "jc.SearchResult"):

# Set QtPy properties
self.setEditable(False)
if icon := _getIcon(result.iconPath()):
if icon := _get_icon(str(result.iconPath()), result.getClass()):
self.setIcon(icon)


Expand Down
46 changes: 26 additions & 20 deletions src/napari_imagej/widgets/widget_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,26 +295,32 @@ def pass_to_ij():


@lru_cache
def _getIcon(icon_path):
def _get_icon(path: str, cls: "jc.Class" = None):
# Ignore falsy paths
if not icon_path:
if not path:
return
stream = jc.File.class_.getResourceAsStream(icon_path)
# Ignore falsy streams
if not stream:
# Web URLs
if path.startswith("https"):
# TODO: Add icons from web
return
# Create a buffer to create the byte[]
buffer = jc.ByteArrayOutputStream()
foo = JArray(JByte)(1024)
while True:
length = stream.read(foo, 0, foo.length)
if length == -1:
break
buffer.write(foo, 0, length)
# Convert the byte[] into a bytearray
bytes_array = bytearray()
bytes_array.extend(buffer.toByteArray())
# Convert thte bytearray into a QIcon
pixmap = QPixmap()
pixmap.loadFromData(QByteArray(bytes_array))
return QIcon(pixmap)
# Java Resources
elif isinstance(cls, jc.Class):
stream = cls.getResourceAsStream(path)
# Ignore falsy streams
if not stream:
return
# Create a buffer to create the byte[]
buffer = jc.ByteArrayOutputStream()
foo = JArray(JByte)(1024)
while True:
length = stream.read(foo, 0, foo.length)
if length == -1:
break
buffer.write(foo, 0, length)
# Convert the byte[] into a bytearray
bytes_array = bytearray()
bytes_array.extend(buffer.toByteArray())
# Convert thte bytearray into a QIcon
pixmap = QPixmap()
pixmap.loadFromData(QByteArray(bytes_array))
return QIcon(pixmap)
3 changes: 3 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ def info(self):
def iconPath(self):
return None

def getClass(self):
return None


class DummyModuleInfo:
"""
Expand Down

0 comments on commit 9b2d5a1

Please sign in to comment.