Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Dec 3, 2024
1 parent c0a4815 commit 78345f1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
15 changes: 9 additions & 6 deletions src/napari_imagej/widgets/result_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from __future__ import annotations

from logging import getLogger
from typing import Dict, List, TYPE_CHECKING
from typing import TYPE_CHECKING

from qtpy.QtCore import QRectF, Qt, Signal, QSize
from qtpy.QtGui import QStandardItem, QStandardItemModel, QTextDocument
Expand All @@ -28,9 +28,10 @@
if TYPE_CHECKING:
from qtpy.QtCore import QModelIndex

from typing import Dict, List

# FIXME: Ideally we'd use something from the palette
# but there's no good options

# Color used for additional information in the QTreeView
HIGHLIGHT = "#8C745E"


Expand Down Expand Up @@ -155,13 +156,13 @@ def paint(self, painter, option, index: QModelIndex):
doc.setHtml(rich_text)

painter.save()
painter.setClipRect(options.rect)

# NB: Unsure why we can't pass subElementRect output to drawContents...
# Translate the painter to the correct item
# NB offset is necessary to account for checkbox, icon
text_offset = style.subElementRect(
QStyle.SE_ItemViewItemText, options, options.widget
).x()
painter.translate(text_offset, options.rect.top())
# Paint the rich text
rect = QRectF(0, 0, options.rect.width(), options.rect.height())
doc.drawContents(painter, rect)

Expand All @@ -171,6 +172,7 @@ def sizeHint(self, option, index):
options = QStyleOptionViewItem(option)
self.initStyleOption(options, index)

# size hint is the size of the rendered HTML
doc = QTextDocument()
doc.setHtml(options.text)
doc.setTextWidth(options.rect.width())
Expand Down Expand Up @@ -251,6 +253,7 @@ def _update_searcher_title(self, parent_idx, first: int, last: int):
if isinstance(item, SearcherItem):
if item.hasChildren():
item.setData(
# Write the number of results in "highlight text"
f'{item.searcher.title()} <span style="color:{HIGHLIGHT};">({item.rowCount()})</span>',
Qt.DisplayRole,
)
Expand Down
4 changes: 3 additions & 1 deletion src/napari_imagej/widgets/widget_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List

from jpype import JArray, JByte
from scyjava import jvm_started
from magicgui import magicgui
from napari import Viewer
from napari.layers import Image, Labels, Layer, Points, Shapes
Expand Down Expand Up @@ -315,7 +316,8 @@ def _get_icon(path: str, cls: "jc.Class" = None):
# TODO: Add icons from web
return
# Java Resources
elif isinstance(cls, jc.Class):
# NB: Use java only if JVM started
elif jvm_started() and isinstance(cls, jc.Class):
stream = cls.getResourceAsStream(path)
# Ignore falsy streams
if not stream:
Expand Down
17 changes: 10 additions & 7 deletions tests/widgets/test_result_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from qtpy.QtCore import QRunnable, Qt, QThreadPool
from qtpy.QtWidgets import QApplication, QMenu

from napari_imagej.java import init_ij
from napari_imagej import nij
from napari_imagej.widgets.result_tree import (
SearcherItem,
SearcherTreeView,
Expand All @@ -23,7 +23,7 @@ def results_tree():


@pytest.fixture
def fixed_tree(ij, asserter):
def fixed_tree(asserter):
"""Creates a "fake" ResultsTree with deterministic results"""
# Create a default SearchResultTree
tree = SearcherTreeView(None)
Expand All @@ -49,7 +49,12 @@ def test_searchers_persist(fixed_tree: SearcherTreeView, asserter):
asserter(lambda: fixed_tree.model().invisibleRootItem().rowCount() == 2)


def test_resultTreeItem_regression(ij):
def test_regression():
"""Tests SearchResultItems, SearcherItems display as expected."""
# SearchResultItems wrap SciJava SearchResults, so they expect a running JVM
nij.ij

# Phase 1: Search Results
dummy = DummySearchResult()
item = SearchResultItem(dummy)
assert item.result == dummy
Expand All @@ -61,9 +66,7 @@ def test_resultTreeItem_regression(ij):
data = f'{dummy.name()} <span style="color:#8C745E;">foo > bar > baz</span>'
assert item.data(0) == data


def test_searcherTreeItem_regression():
init_ij()
# Phase 2: Searchers
dummy = DummySearcher("This is not a Searcher")
item = SearcherItem(dummy)
assert item.searcher == dummy
Expand All @@ -78,7 +81,7 @@ def test_searcherTreeItem_regression():
assert item.data(0) == dummy.title()


def test_key_return_expansion(fixed_tree: SearcherTreeView, qtbot, asserter):
def test_key_return_expansion(fixed_tree: SearcherTreeView, qtbot):
idx = fixed_tree.model().index(0, 0)
fixed_tree.setCurrentIndex(idx)
expanded = fixed_tree.isExpanded(idx)
Expand Down
4 changes: 4 additions & 0 deletions tests/widgets/widget_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from qtpy.QtCore import Qt

from napari_imagej import nij
from napari_imagej.widgets.result_tree import SearcherItem, SearcherTreeView
from tests.utils import DummySearcher, DummySearchEvent, jc

Expand All @@ -19,6 +20,9 @@ def _searcher_tree_named(tree: SearcherTreeView, name: str) -> Optional[Searcher


def _populate_tree(tree: SearcherTreeView, asserter):
# DummySearchers are SciJava Searchers - we need a JVM
nij.ij

root = tree.model().invisibleRootItem()
asserter(lambda: root.rowCount() == 0)
# Add two searchers
Expand Down

0 comments on commit 78345f1

Please sign in to comment.