def __init__(self, ui, list_item_delegate, *, items=None, selection_style=None, properties=None, selection=None, border_color=None, v_scroll_enabled: bool=True, v_auto_resize: bool=False): super().__init__(ui.create_column_widget()) self.property_changed_event = Event.Event() items = items or list() self.__items: typing.List = list() self.on_selection_changed = None self.on_item_selected = None self.on_cancel = None self.on_item_handle_context_menu = None # used for declarative self.__items_binding = None self.__current_index_binding = None self.__on_current_index_changed = None self.__v_auto_resize = v_auto_resize self.on_escape_pressed : typing.Optional[typing.Callable[[], bool]] = None self.on_return_pressed : typing.Optional[typing.Callable[[], bool]] = None self.__selection = selection if selection else Selection.IndexedSelection(selection_style) def selection_changed(): on_selection_changed = self.on_selection_changed if callable(on_selection_changed): on_selection_changed(self.__selection.indexes) if callable(self.__on_current_index_changed): self.__on_current_index_changed(self.current_index) def handle_delegate_cancel(): if callable(self.on_cancel): self.on_cancel() if callable(self.on_escape_pressed): self.on_escape_pressed() def handle_delegate_item_selected(index): if callable(self.on_item_selected): self.on_item_selected(index) if callable(self.on_return_pressed): self.on_return_pressed() self.__selection_changed_event_listener = self.__selection.changed_event.listen(selection_changed) self.__list_canvas_item_delegate = list_item_delegate self.__list_canvas_item_delegate.on_cancel = handle_delegate_cancel self.__list_canvas_item_delegate.on_item_selected = handle_delegate_item_selected self.__list_canvas_item = ListCanvasItem.ListCanvasItem(self.__list_canvas_item_delegate, self.__selection, 20) scroll_area_canvas_item = CanvasItem.ScrollAreaCanvasItem(self.__list_canvas_item) scroll_area_canvas_item.auto_resize_contents = True scroll_group_canvas_item = CanvasItem.CanvasItemComposition() if border_color is not None: scroll_group_canvas_item.border_color = border_color scroll_group_canvas_item.layout = CanvasItem.CanvasItemRowLayout() scroll_group_canvas_item.add_canvas_item(scroll_area_canvas_item) if v_scroll_enabled: scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(scroll_area_canvas_item) scroll_group_canvas_item.add_canvas_item(scroll_bar_canvas_item) canvas_widget = ui.create_canvas_widget(properties=properties) canvas_widget.canvas_item.add_canvas_item(scroll_group_canvas_item) self.content_widget.add(canvas_widget) self.__canvas_widget = canvas_widget self.items = items
def __init__(self, ui, items, selection_style=None, stringify_item=None, properties=None): super().__init__(ui.create_column_widget()) self.__items = items content_widget = self.content_widget self.on_selection_changed = None self.on_item_selected = None stringify_item = str if stringify_item is None else stringify_item class ListCanvasItemDelegate: def __init__(self, string_list_widget, items, selection): self.__string_list_widget = string_list_widget self.__items = items self.__selection = selection @property def items(self): return self.__items @items.setter def items(self, value): self.__items = value @property def item_count(self): return len(self.__items) def on_context_menu_event(self, index, x, y, gx, gy): return False def on_delete_pressed(self): pass def on_key_pressed(self, key): return False def on_drag_started(self, index, x, y, modifiers): pass def on_item_selected(self, index): if callable(self.__string_list_widget.on_item_selected): return self.__string_list_widget.on_item_selected(index) return False def paint_item(self, drawing_context, display_item, rect, is_selected): item = stringify_item(display_item) with drawing_context.saver(): drawing_context.fill_style = "#000" drawing_context.font = "12px" drawing_context.text_align = 'left' drawing_context.text_baseline = 'bottom' drawing_context.fill_text(item, rect[0][1] + 4, rect[0][0] + 20 - 4) self.__selection = Selection.IndexedSelection(selection_style) def selection_changed(): on_selection_changed = self.on_selection_changed if callable(on_selection_changed): on_selection_changed(self.__selection.indexes) self.__selection_changed_event_listener = self.__selection.changed_event.listen( selection_changed) self.__list_canvas_item_delegate = ListCanvasItemDelegate( self, items, self.__selection) self.__list_canvas_item = ListCanvasItem.ListCanvasItem( self.__list_canvas_item_delegate, self.__selection, 20) scroll_area_canvas_item = CanvasItem.ScrollAreaCanvasItem( self.__list_canvas_item) scroll_area_canvas_item.auto_resize_contents = True scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem( scroll_area_canvas_item) scroll_group_canvas_item = CanvasItem.CanvasItemComposition() scroll_group_canvas_item.border_color = "#888" 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) canvas_widget = ui.create_canvas_widget(properties=properties) canvas_widget.canvas_item.add_canvas_item(scroll_group_canvas_item) content_widget.add(canvas_widget)
def __init__(self, event_loop: asyncio.AbstractEventLoop, ui, display_item_adapters_model, selection, direction=GridCanvasItem.Direction.Row, wrap=True): super().__init__() self.__event_loop = event_loop self.__pending_tasks : typing.List = list() self.ui = ui self.__selection = selection self.on_delete_display_item_adapters : typing.Optional[typing.Callable[[typing.List[DisplayItemAdapter]], None]] = None self.on_key_pressed : typing.Optional[typing.Callable[[UserInterface.Key], bool]] = None self.on_display_item_adapter_double_clicked : typing.Optional[typing.Callable[[DisplayItemAdapter], bool]] = None self.on_display_item_adapter_selection_changed : typing.Optional[typing.Callable[[typing.List[DisplayItemAdapter]], None]] = None self.on_context_menu_event : typing.Optional[typing.Callable[[typing.Optional[DisplayItem.DisplayItem], int, int, int, int], bool]] = None self.on_focus_changed : typing.Optional[typing.Callable[[bool], None]] = None self.on_drag_started : typing.Optional[typing.Callable[[UserInterface.MimeData, numpy.ndarray], None]] = None self.__display_item_adapters : typing.List[DisplayItemAdapter] = list() self.__display_item_adapter_needs_update_listeners : typing.List = list() self.__display_item_adapters_model = display_item_adapters_model self.__display_item_adapter_inserted_event_listener = self.__display_item_adapters_model.item_inserted_event.listen(self.__display_item_adapter_inserted) self.__display_item_adapter_removed_event_listener = self.__display_item_adapters_model.item_removed_event.listen(self.__display_item_adapter_removed) self.__display_item_adapter_end_changes_event_listener = self.__display_item_adapters_model.end_changes_event.listen(self.__display_item_adapter_end_changes) class GridCanvasItemDelegate: def __init__(self, data_grid_controller: DataGridController): self.__data_grid_controller = data_grid_controller @property def item_count(self) -> int: return self.__data_grid_controller.display_item_adapter_count @property def items(self) -> typing.List[DisplayItemAdapter]: return self.__data_grid_controller.display_item_adapters def paint_item(self, drawing_context: DrawingContext.DrawingContext, display_item_adapter: DisplayItemAdapter, rect: Geometry.IntRect, is_selected: bool) -> None: display_item_adapter.draw_grid_item(drawing_context, rect) def on_context_menu_event(self, index: int, x: int, y: int, gx: int, gy: int) -> bool: return self.__data_grid_controller.context_menu_event(index, x, y, gx, gy) def on_delete_pressed(self) -> None: self.__data_grid_controller._delete_pressed() def on_key_pressed(self, key: UserInterface.Key) -> bool: return self.__data_grid_controller._key_pressed(key) def on_mouse_double_clicked(self, mouse_index: int, x: int, y: int, modifiers: UserInterface.KeyboardModifiers) -> bool: return self.__data_grid_controller._double_clicked() def on_drag_started(self, index: int, x: int, y: int, modifiers: UserInterface.KeyboardModifiers) -> None: self.__data_grid_controller.drag_started(index, x, y, modifiers) self.icon_view_canvas_item = GridCanvasItem.GridCanvasItem(GridCanvasItemDelegate(self), self.__selection, direction, wrap) def icon_view_canvas_item_focus_changed(focused: bool) -> None: self.icon_view_canvas_item.update() if self.on_focus_changed: self.on_focus_changed(focused) self.icon_view_canvas_item.on_focus_changed = icon_view_canvas_item_focus_changed self.scroll_area_canvas_item = CanvasItem.ScrollAreaCanvasItem(self.icon_view_canvas_item) self.scroll_area_canvas_item.auto_resize_contents = True self.scroll_group_canvas_item = CanvasItem.CanvasItemComposition() if (wrap and direction == GridCanvasItem.Direction.Row) or (not wrap and direction == GridCanvasItem.Direction.Column): self.scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(self.scroll_area_canvas_item) self.scroll_group_canvas_item.layout = CanvasItem.CanvasItemRowLayout() else: self.scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(self.scroll_area_canvas_item, CanvasItem.Orientation.Horizontal) self.scroll_group_canvas_item.layout = CanvasItem.CanvasItemColumnLayout() self.scroll_group_canvas_item.add_canvas_item(self.scroll_area_canvas_item) self.scroll_group_canvas_item.add_canvas_item(self.scroll_bar_canvas_item) """ # dual scroll bars, leave here for easy testing self.vertical_scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(self.scroll_area_canvas_item) self.horizontal_scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(self.scroll_area_canvas_item, CanvasItem.Orientation.Horizontal) self.scroll_group_canvas_item.layout = CanvasItem.CanvasItemGridLayout(Geometry.IntSize(width=2, height=2)) self.scroll_group_canvas_item.add_canvas_item(self.scroll_area_canvas_item, Geometry.IntPoint(x=0, y=0)) self.scroll_group_canvas_item.add_canvas_item(self.vertical_scroll_bar_canvas_item, Geometry.IntPoint(x=1, y=0)) self.scroll_group_canvas_item.add_canvas_item(self.horizontal_scroll_bar_canvas_item, Geometry.IntPoint(x=0, y=1)) """ self.canvas_item = self.scroll_group_canvas_item def selection_changed() -> None: self.selected_indexes = list(self.__selection.indexes) if callable(self.on_display_item_adapter_selection_changed): self.on_display_item_adapter_selection_changed([self.__display_item_adapters[index] for index in list(self.__selection.indexes)]) self.icon_view_canvas_item.make_selection_visible() self.__selection_changed_listener = self.__selection.changed_event.listen(selection_changed) self.selected_indexes = list() # changed display items keep track of items whose content has changed # the content changed messages may come from a thread so have to be # moved to the main thread via this object. self.__changed_display_item_adapters = False self.__changed_display_item_adapters_mutex = threading.RLock() self.__closed = False for index, display_item_adapter in enumerate(self.__display_item_adapters_model.display_item_adapters): self.__display_item_adapter_inserted("display_item_adapters", display_item_adapter, index)
def __init__(self, event_loop: asyncio.AbstractEventLoop, ui: UserInterface.UserInterface, display_item_adapters_model: ListModel.MappedListModel, selection: Selection.IndexedSelection): super().__init__() self.__event_loop = event_loop self.__pending_tasks : typing.List = list() self.ui = ui self.__selection = selection self.on_delete_display_item_adapters : typing.Optional[typing.Callable[[typing.List[DisplayItemAdapter]], None]] = None self.on_key_pressed : typing.Optional[typing.Callable[[UserInterface.Key], bool]] = None self.__display_item_adapters : typing.List[DisplayItemAdapter] = list() self.__display_item_adapter_needs_update_listeners : typing.List = list() self.__display_item_adapters_model = display_item_adapters_model self.__display_item_adapter_inserted_event_listener = self.__display_item_adapters_model.item_inserted_event.listen(self.__display_item_adapter_inserted) self.__display_item_adapter_removed_event_listener = self.__display_item_adapters_model.item_removed_event.listen(self.__display_item_adapter_removed) self.__display_item_adapter_end_changes_event_listener = self.__display_item_adapters_model.end_changes_event.listen(self.__display_item_adapter_end_changes) class ListCanvasItemDelegate: def __init__(self, data_list_controller: DataListController): self.__data_list_controller = data_list_controller @property def item_count(self) -> int: return self.__data_list_controller.display_item_adapter_count @property def items(self) -> typing.List[DisplayItemAdapter]: return self.__data_list_controller.display_item_adapters def paint_item(self, drawing_context: DrawingContext.DrawingContext, display_item_adapter: DisplayItemAdapter, rect: Geometry.IntRect, is_selected: bool) -> None: display_item_adapter.draw_list_item(drawing_context, rect) def context_menu_event(self, index: int, x: int, y: int, gx: int, gy: int) -> bool: return self.__data_list_controller.context_menu_event(index, x, y, gx, gy) def delete_pressed(self) -> None: self.__data_list_controller._delete_pressed() def key_pressed(self, key: UserInterface.Key) -> bool: return self.__data_list_controller._key_pressed(key) def drag_started(self, index: int, x: int, y: int, modifiers: UserInterface.KeyboardModifiers) -> None: self.__data_list_controller.drag_started(index, x, y, modifiers) self.__list_canvas_item = ListCanvasItem.ListCanvasItem(ListCanvasItemDelegate(self), self.__selection) def focus_changed(focused: bool) -> None: self.__list_canvas_item.update() if self.on_focus_changed: self.on_focus_changed(focused) self.__list_canvas_item.on_focus_changed = focus_changed self.scroll_area_canvas_item = CanvasItem.ScrollAreaCanvasItem(self.__list_canvas_item) self.scroll_area_canvas_item.auto_resize_contents = True self.scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(self.scroll_area_canvas_item) self.scroll_group_canvas_item = CanvasItem.CanvasItemComposition() self.scroll_group_canvas_item.layout = CanvasItem.CanvasItemRowLayout() self.scroll_group_canvas_item.add_canvas_item(self.scroll_area_canvas_item) self.scroll_group_canvas_item.add_canvas_item(self.scroll_bar_canvas_item) self.canvas_item = self.scroll_group_canvas_item def selection_changed() -> None: self.selected_indexes = list(self.__selection.indexes) if callable(self.on_display_item_adapter_selection_changed): self.on_display_item_adapter_selection_changed([self.__display_item_adapters[index] for index in list(self.__selection.indexes)]) self.__list_canvas_item.make_selection_visible() self.__selection_changed_listener = self.__selection.changed_event.listen(selection_changed) self.selected_indexes = list() self.on_display_item_adapter_selection_changed : typing.Optional[typing.Callable[[typing.List[DisplayItemAdapter]], None]] = None self.on_context_menu_event : typing.Optional[typing.Callable[[typing.Optional[DisplayItem.DisplayItem], int, int, int, int], bool]] = None self.on_focus_changed : typing.Optional[typing.Callable[[bool], None]] = None self.on_drag_started : typing.Optional[typing.Callable[[UserInterface.MimeData, numpy.ndarray], None]] = None # changed display items keep track of items whose content has changed # the content changed messages may come from a thread so have to be # moved to the main thread via this object. self.__changed_display_item_adapters = False self.__changed_display_item_adapters_mutex = threading.RLock() for index, display_item_adapter in enumerate(self.__display_item_adapters_model.display_item_adapters): self.__display_item_adapter_inserted("display_item_adapters", display_item_adapter, index)
def __init__(self, ui: UserInterface.UserInterface, list_item_delegate: ListCanvasItem.ListCanvasItemDelegate, *, items: typing.Optional[typing.Sequence[typing.Any]] = None, selection_style: typing.Optional[Selection.Style] = None, properties: typing.Optional[typing.Mapping[ str, typing.Any]] = None, selection: typing.Optional[Selection.IndexedSelection] = None, border_color: typing.Optional[str] = None, v_scroll_enabled: bool = True, v_auto_resize: bool = False) -> None: column_widget = ui.create_column_widget() super().__init__(column_widget) self.property_changed_event = Event.Event() items = items or list() self.__items: typing.List[typing.Any] = list() self.on_selection_changed: typing.Optional[typing.Callable[ [typing.AbstractSet[int]], None]] = None self.on_item_selected: typing.Optional[typing.Callable[[int], bool]] = None self.on_cancel: typing.Optional[typing.Callable[[], None]] = None self.on_item_handle_context_menu: typing.Optional[typing.Callable[ ..., bool]] = None # used for declarative self.__items_binding: typing.Optional[Binding.Binding] = None self.__current_index_binding: typing.Optional[Binding.Binding] = None self.__on_current_index_changed: typing.Optional[typing.Callable[ [int], None]] = None self.__v_auto_resize = v_auto_resize self.on_escape_pressed: typing.Optional[typing.Callable[[], bool]] = None self.on_return_pressed: typing.Optional[typing.Callable[[], bool]] = None self.__selection = selection if selection else Selection.IndexedSelection( selection_style) def selection_changed() -> None: on_selection_changed = self.on_selection_changed if callable(on_selection_changed): on_selection_changed(self.__selection.indexes) if callable(self.__on_current_index_changed): self.__on_current_index_changed(self.current_index) def handle_delegate_cancel() -> None: if callable(self.on_cancel): self.on_cancel() if callable(self.on_escape_pressed): self.on_escape_pressed() def handle_delegate_item_selected(index: int) -> None: if callable(self.on_item_selected): self.on_item_selected(index) if callable(self.on_return_pressed): self.on_return_pressed() self.__selection_changed_event_listener = self.__selection.changed_event.listen( selection_changed) self.__list_canvas_item_delegate = list_item_delegate self.__list_canvas_item_delegate.on_cancel = handle_delegate_cancel self.__list_canvas_item_delegate.on_item_selected = handle_delegate_item_selected self.__list_canvas_item = ListCanvasItem.ListCanvasItem( self.__list_canvas_item_delegate, self.__selection, 20) scroll_area_canvas_item = CanvasItem.ScrollAreaCanvasItem( self.__list_canvas_item) scroll_area_canvas_item.auto_resize_contents = True scroll_group_canvas_item = CanvasItem.CanvasItemComposition() if border_color is not None: scroll_group_canvas_item.border_color = border_color scroll_group_canvas_item.layout = CanvasItem.CanvasItemRowLayout() scroll_group_canvas_item.add_canvas_item(scroll_area_canvas_item) if v_scroll_enabled: scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem( scroll_area_canvas_item) scroll_group_canvas_item.add_canvas_item(scroll_bar_canvas_item) canvas_widget = ui.create_canvas_widget(properties=properties) canvas_widget.canvas_item.add_canvas_item(scroll_group_canvas_item) column_widget.add(canvas_widget) self.__canvas_widget = canvas_widget self.items = list(items)
def __init__( self, event_loop: asyncio.AbstractEventLoop, ui: UserInterface.UserInterface, canvas_item: CanvasItem.AbstractCanvasItem, display_item_adapters_model: ListModel.MappedListModel, selection: Selection.IndexedSelection, direction: GridCanvasItem.Direction = GridCanvasItem.Direction.Row, wrap: bool = True) -> None: self.__event_loop = event_loop self.__pending_tasks: typing.List[asyncio.Task[None]] = list() self.ui = ui self.__selection = selection self.__display_item_adapters: typing.List[DisplayItemAdapter] = list() self.__display_item_adapter_needs_update_listeners: typing.List[ Event.EventListener] = list() self.__display_item_adapters_model = display_item_adapters_model self.__display_item_adapter_inserted_event_listener = self.__display_item_adapters_model.item_inserted_event.listen( self.__display_item_adapter_inserted) self.__display_item_adapter_removed_event_listener = self.__display_item_adapters_model.item_removed_event.listen( self.__display_item_adapter_removed) self.__display_item_adapter_end_changes_event_listener = self.__display_item_adapters_model.end_changes_event.listen( self.__display_item_adapter_end_changes) self.on_delete_display_item_adapters: typing.Optional[typing.Callable[ [typing.List[DisplayItemAdapter]], None]] = None self.on_key_pressed: typing.Optional[typing.Callable[ [UserInterface.Key], bool]] = None self.on_display_item_adapter_double_clicked: typing.Optional[ typing.Callable[[DisplayItemAdapter], bool]] = None self.on_context_menu_event: typing.Optional[typing.Callable[[ typing.Optional[DisplayItem.DisplayItem], typing. List[DisplayItem.DisplayItem], int, int, int, int ], bool]] = None self.on_focus_changed: typing.Optional[typing.Callable[[bool], None]] = None self.on_drag_started: typing.Optional[typing.Callable[ [UserInterface.MimeData, typing.Optional[_NDArray]], None]] = None # changed display items keep track of items whose content has changed # the content changed messages may come from a thread so have to be # moved to the main thread via this object. self.__changed_display_item_adapters = False self.__changed_display_item_adapters_mutex = threading.RLock() self.__list_canvas_item = canvas_item def focus_changed(focused: bool) -> None: self.__list_canvas_item.update() if self.on_focus_changed: self.on_focus_changed(focused) self.__list_canvas_item.on_focus_changed = focus_changed self.scroll_area_canvas_item = CanvasItem.ScrollAreaCanvasItem( self.__list_canvas_item) self.scroll_area_canvas_item.auto_resize_contents = True self.scroll_group_canvas_item = CanvasItem.CanvasItemComposition() if (wrap and direction == GridCanvasItem.Direction.Row) or ( not wrap and direction == GridCanvasItem.Direction.Column): self.scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem( self.scroll_area_canvas_item) self.scroll_group_canvas_item.layout = CanvasItem.CanvasItemRowLayout( ) else: self.scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem( self.scroll_area_canvas_item, CanvasItem.Orientation.Horizontal) self.scroll_group_canvas_item.layout = CanvasItem.CanvasItemColumnLayout( ) self.scroll_group_canvas_item.add_canvas_item( self.scroll_area_canvas_item) self.scroll_group_canvas_item.add_canvas_item( self.scroll_bar_canvas_item) """ # dual scroll bars, leave here for easy testing self.vertical_scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(self.scroll_area_canvas_item) self.horizontal_scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(self.scroll_area_canvas_item, CanvasItem.Orientation.Horizontal) self.scroll_group_canvas_item.layout = CanvasItem.CanvasItemGridLayout(Geometry.IntSize(width=2, height=2)) self.scroll_group_canvas_item.add_canvas_item(self.scroll_area_canvas_item, Geometry.IntPoint(x=0, y=0)) self.scroll_group_canvas_item.add_canvas_item(self.vertical_scroll_bar_canvas_item, Geometry.IntPoint(x=1, y=0)) self.scroll_group_canvas_item.add_canvas_item(self.horizontal_scroll_bar_canvas_item, Geometry.IntPoint(x=0, y=1)) """ self.__canvas_item = self.scroll_group_canvas_item def selection_changed() -> None: self.selected_indexes = list(self.__selection.indexes) typing.cast(ItemExplorerCanvasItemLike, self.__list_canvas_item).make_selection_visible() self.__selection_changed_listener: typing.Optional[ Event.EventListener] = self.__selection.changed_event.listen( selection_changed) self.selected_indexes = list() for index, display_item_adapter in enumerate( self.__display_item_adapters_model.display_item_adapters): self.__display_item_adapter_inserted("display_item_adapters", display_item_adapter, index) self.__closed = False