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

Improve performance of collections panel #1266

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 3 additions & 7 deletions nion/swift/DataItemThumbnailWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,8 @@ def close(self) -> None:
super().close()

def __detach_listeners(self) -> None:
if self.__thumbnail_updated_event_listener:
self.__thumbnail_updated_event_listener.close()
self.__thumbnail_updated_event_listener = None
if self.__thumbnail_source:
self.__thumbnail_source.remove_ref()
self.__thumbnail_source = None
self.__thumbnail_updated_event_listener = None
self.__thumbnail_source = None

def __update_thumbnail(self) -> None:
if self.__display_item:
Expand All @@ -461,7 +457,7 @@ def set_display_item(self, display_item: typing.Optional[DisplayItem.DisplayItem
self.__detach_listeners()
self.__display_item = display_item
if display_item:
self.__thumbnail_source = Thumbnails.ThumbnailManager().thumbnail_source_for_display_item(self.ui, display_item).add_ref()
self.__thumbnail_source = Thumbnails.ThumbnailManager().thumbnail_source_for_display_item(self.ui, display_item)
self.__thumbnail_updated_event_listener = self.__thumbnail_source.thumbnail_updated_event.listen(self.__update_thumbnail)
self.__update_thumbnail()
if self.__display_item_binding:
Expand Down
38 changes: 23 additions & 15 deletions nion/swift/DataPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,8 @@ def display_item_removed() -> None:

def close(self) -> None:
# remove the listener.
if self.__thumbnail_updated_event_listener:
self.__thumbnail_updated_event_listener.close()
self.__thumbnail_updated_event_listener = None
if self.__thumbnail_source:
self.__thumbnail_source.remove_ref()
self.__thumbnail_source = None
self.__thumbnail_updated_event_listener = None
self.__thumbnail_source = None
if self.__display_changed_event_listener:
self.__display_changed_event_listener.close()
self.__display_changed_event_listener = None
Expand Down Expand Up @@ -184,7 +180,7 @@ def drag_started(self, ui: UserInterface.UserInterface, x: int, y: int, modifier
def calculate_thumbnail_data(self) -> typing.Optional[_NDArray]:
# grab the display specifier and if there is a display, handle thumbnail updating.
if self.__display_item and not self.__thumbnail_source:
self.__thumbnail_source = Thumbnails.ThumbnailManager().thumbnail_source_for_display_item(self.ui, self.__display_item).add_ref()
self.__thumbnail_source = Thumbnails.ThumbnailManager().thumbnail_source_for_display_item(self.ui, self.__display_item)

def thumbnail_updated() -> None:
self.__list_item_drawing_context = None
Expand Down Expand Up @@ -611,7 +607,7 @@ def thumbnail_updated() -> None:
self.__thumbnail_canvas_item.bitmap = Bitmap.Bitmap(rgba_bitmap_data=bitmap_data)

self.__thumbnail_canvas_item = thumbnail_canvas_item
self.__thumbnail_source = Thumbnails.ThumbnailManager().thumbnail_source_for_display_item(self.__ui, self.__display_item).add_ref()
self.__thumbnail_source = Thumbnails.ThumbnailManager().thumbnail_source_for_display_item(self.__ui, self.__display_item)
self.__thumbnail_updated_event_listener = self.__thumbnail_source.thumbnail_updated_event.listen(thumbnail_updated)

thumbnail_updated()
Expand All @@ -623,7 +619,8 @@ def thumbnail_updated() -> None:
self.__item_changed_listener = display_item.item_changed_event.listen(ReferenceCounting.weak_partial(DataPanelListItem.__item_changed, self))

def close(self) -> None:
self.__thumbnail_source.remove_ref()
self.__thumbnail_updated_event_listener = typing.cast(typing.Any, None)
self.__thumbnail_source = typing.cast(typing.Any, None)
super().close()

@property
Expand Down Expand Up @@ -686,7 +683,9 @@ def map_display_item_to_display_item_adapter(display_item: DisplayItem.DisplayIt
def unmap_display_item_to_display_item_adapter(display_item_adapter: DisplayItemAdapter) -> None:
display_item_adapter.close()

self.__filtered_display_item_adapters_model = ListModel.MappedListModel(container=document_controller.filtered_display_items_model, master_items_key="display_items", items_key="display_item_adapters", map_fn=map_display_item_to_display_item_adapter, unmap_fn=unmap_display_item_to_display_item_adapter)
display_items_model = document_controller.filtered_display_items_model

self.__filtered_display_item_adapters_model = ListModel.MappedListModel(container=display_items_model, master_items_key="display_items", items_key="display_item_adapters", map_fn=map_display_item_to_display_item_adapter, unmap_fn=unmap_display_item_to_display_item_adapter)

self.__selection = self.document_controller.selection

Expand Down Expand Up @@ -753,8 +752,7 @@ def _get_mime_data_and_thumbnail_data(self, drag_started_event: ListCanvasItem.L
mime_data = document_controller.ui.create_mime_data()
MimeTypes.mime_data_put_display_item(mime_data, display_item)
thumbnail_source = Thumbnails.ThumbnailManager().thumbnail_source_for_display_item(self.__data_panel.ui, display_item)
with thumbnail_source.ref():
thumbnail_data = thumbnail_source.thumbnail_data
thumbnail_data = thumbnail_source.thumbnail_data
if thumbnail_data is not None:
# scaling is very slow
thumbnail_data = Image.get_rgba_data_from_rgba(
Expand All @@ -766,19 +764,29 @@ def _get_mime_data_and_thumbnail_data(self, drag_started_event: ListCanvasItem.L
anchor_index = self.__selection.anchor_index or 0
thumbnail_display_item = display_items[anchor_index]
thumbnail_source = Thumbnails.ThumbnailManager().thumbnail_source_for_display_item(self.__data_panel.ui, thumbnail_display_item)
with thumbnail_source.ref():
thumbnail_data = thumbnail_source.thumbnail_data
thumbnail_data = thumbnail_source.thumbnail_data
return mime_data, thumbnail_data

list_item_delegate = ListItemDelegate(self, self.__selection)
list_canvas_item = ListCanvasItem.ListCanvasItem2(document_controller.filtered_display_items_model, self.__selection, list_item_factory, list_item_delegate, item_height=80, key="display_items")
list_canvas_item = ListCanvasItem.ListCanvasItem2(display_items_model, self.__selection, list_item_factory, list_item_delegate, item_height=80, key="display_items")
scroll_area_canvas_item = CanvasItem.ScrollAreaCanvasItem(list_canvas_item)
scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(scroll_area_canvas_item, CanvasItem.Orientation.Vertical)
scroll_group_canvas_item = CanvasItem.CanvasItemComposition()
scroll_group_canvas_item.layout = CanvasItem.CanvasItemRowLayout()
scroll_group_canvas_item.add_canvas_item(scroll_area_canvas_item)
scroll_group_canvas_item.add_canvas_item(scroll_bar_canvas_item)

def begin_changes(key: str) -> None:
list_canvas_item._begin_batch_update()

def end_changes(key: str) -> None:
list_canvas_item._end_batch_update()

# the display items model can notify us when it is about to change. in order to gang up changes, watch
# for these notification events and tell the list canvas item to only update at the end of the changes.
self.__display_items_begin_changes_listener = display_items_model.begin_changes_event.listen(begin_changes)
self.__display_items_end_changes_listener = display_items_model.end_changes_event.listen(end_changes)

data_list_canvas_item = scroll_group_canvas_item

data_list_widget = ui.create_canvas_widget()
Expand Down
Loading