def __make_selection_visible(self, style: int) -> None: if self.__delegate: selected_indexes = list(self.__selection.indexes) if len(selected_indexes) > 0 and self.canvas_bounds is not None: min_index = min(selected_indexes) max_index = max(selected_indexes) min_rect = self.__rect_for_index(min_index) max_rect = self.__rect_for_index(max_index) visible_rect = getattr(self.container, "visible_rect", None) canvas_rect = self.canvas_rect if visible_rect and canvas_rect: canvas_origin = canvas_rect.origin canvas_size = canvas_rect.size if style < 0: if min_rect.top < visible_rect.top: self.update_layout(Geometry.IntPoint(y=-min_rect.top, x=canvas_origin.x), canvas_size) elif min_rect.bottom > visible_rect.bottom: self.update_layout(Geometry.IntPoint(y=-min_rect.bottom + visible_rect.height, x=canvas_origin.x), canvas_size) elif style > 0: if max_rect.bottom > visible_rect.bottom: self.update_layout(Geometry.IntPoint(y=-max_rect.bottom + visible_rect.height, x=canvas_origin.x), canvas_size) elif max_rect.top < visible_rect.top: self.update_layout(Geometry.IntPoint(y=-max_rect.top, x=canvas_origin.x), canvas_size) else: pass # do nothing. maybe a use case will pop up where this should do something?
def start_mouse_tracker( ui, event_loop: asyncio.AbstractEventLoop, canvas_item: CanvasItem.AbstractCanvasItem, mouse_position_changed_by_fn: typing.Callable[[Geometry.IntPoint], None], global_pos: Geometry.IntPoint, size: Geometry.IntSize): tracking_canvas_item = TrackingCanvasItem() tracking_canvas_item.on_mouse_position_changed_by = mouse_position_changed_by_fn tracking_canvas_item.add_canvas_item(canvas_item) async def close_window(document_window): document_window.request_close() def handle_close(document_window): tracking_canvas_item.release_mouse() event_loop.create_task(close_window(document_window)) def activation_changed(document_window, activated): if not activated: handle_close(document_window) # create the popup window document_window = ui.create_document_window() document_window.window_style = "mousegrab" document_window.on_activation_changed = functools.partial( activation_changed, document_window) tracking_canvas_item.on_close = functools.partial(handle_close, document_window) # configure canvas widget, attach to document window mousegrab_window_pos = global_pos - Geometry.IntPoint(x=size.width, y=size.height / 2) document_window.show(size=size, position=mousegrab_window_pos) if sys.platform == "win32": relative_pos = Geometry.IntPoint() else: relative_pos = mousegrab_window_pos document_window.fill_screen() canvas_widget = ui.create_canvas_widget() tracking_canvas_item.sizing.set_fixed_size(size) content_row_canvas_item = CanvasItem.CanvasItemComposition() content_row_canvas_item.layout = CanvasItem.CanvasItemRowLayout() content_row_canvas_item.add_spacing(relative_pos.x) content_row_canvas_item.add_canvas_item(tracking_canvas_item) content_row_canvas_item.add_stretch() content_canvas_item = CanvasItem.CanvasItemComposition() content_canvas_item.layout = CanvasItem.CanvasItemColumnLayout() content_canvas_item.add_spacing(relative_pos.y) content_canvas_item.add_canvas_item(content_row_canvas_item) content_canvas_item.add_stretch() canvas_widget.canvas_item.add_canvas_item(content_canvas_item) document_window.attach(canvas_widget) tracking_canvas_item.request_focus() tracking_canvas_item.cursor_shape = "blank" canvas_widget.set_cursor_shape("blank") tracking_canvas_item.grab_mouse(relative_pos.x + size.width // 2, relative_pos.y + size.height // 2)
def test_start_drag_does_not_change_selection(self) -> None: selection = Selection.IndexedSelection() delegate = GridCanvasItemDelegate() canvas_item = GridCanvasItem.GridCanvasItem(delegate, selection) canvas_item.update_layout(Geometry.IntPoint(), Geometry.IntSize.make((320, 100))) self.assertEqual(selection.indexes, set()) canvas_item.simulate_drag(Geometry.IntPoint(y=120, x=50), Geometry.IntPoint(y=120, x=500)) self.assertEqual(selection.indexes, set())
def test_start_drag_does_not_change_selection(self): selection = Selection.IndexedSelection() delegate = ListCanvasItemDelegate() canvas_item = ListCanvasItem.ListCanvasItem(delegate, selection) canvas_item.update_layout((0, 0), (320, 100)) self.assertEqual(selection.indexes, set()) canvas_item.simulate_drag(Geometry.IntPoint(y=120, x=50), Geometry.IntPoint(y=120, x=500)) self.assertEqual(selection.indexes, set())
def test_shift_click_extends_selection(self): selection = Selection.IndexedSelection() delegate = ListCanvasItemDelegate() canvas_item = ListCanvasItem.ListCanvasItem(delegate, selection) canvas_item.update_layout((0, 0), (320, 100)) self.assertEqual(selection.indexes, set()) canvas_item.simulate_click(Geometry.IntPoint(y=120, x=50)) self.assertEqual(selection.indexes, {1}) modifiers = CanvasItem.KeyboardModifiers(shift=True) canvas_item.simulate_click(Geometry.IntPoint(y=200, x=50), modifiers) self.assertEqual(selection.indexes, {1, 2})
def __make_selection_visible(self, top: bool) -> None: if self.__delegate: selected_indexes = list(self.__selection.indexes) if len(selected_indexes) > 0 and self.canvas_bounds is not None: min_index = min(selected_indexes) max_index = max(selected_indexes) min_rect = self.__rect_for_index(min_index) max_rect = self.__rect_for_index(max_index) container = self.container # TODO: typing here is incorrect since container has no "visible_rect" property. assuming scroll area. visible_rect = getattr(container, "visible_rect", Geometry.IntRect.empty_rect()) canvas_origin = self.canvas_origin assert canvas_origin if (self.direction == Direction.Row and self.wrap) or (self.direction == Direction.Column and not self.wrap): if top: if min_rect.top < visible_rect.top: self.update_layout(Geometry.IntPoint(y=-min_rect.top, x=canvas_origin.x), self.canvas_size) elif min_rect.bottom > visible_rect.bottom: self.update_layout(Geometry.IntPoint(y=-min_rect.bottom + visible_rect.height, x=canvas_origin.x), self.canvas_size) else: if max_rect.bottom > visible_rect.bottom: self.update_layout(Geometry.IntPoint(y=-max_rect.bottom + visible_rect.height, x=canvas_origin.x), self.canvas_size) elif max_rect.top < visible_rect.top: self.update_layout(Geometry.IntPoint(y=-max_rect.top, x=canvas_origin.x), self.canvas_size) else: if top: if min_rect.left < visible_rect.left: self.update_layout(Geometry.IntPoint(y=canvas_origin.y, x=-min_rect.left), self.canvas_size) elif min_rect.right > visible_rect.right: self.update_layout(Geometry.IntPoint(y=canvas_origin.y, x=-min_rect.right + visible_rect.width), self.canvas_size) else: if max_rect.right > visible_rect.right: self.update_layout(Geometry.IntPoint(y=canvas_origin.y, x=-max_rect.right + visible_rect.width), self.canvas_size) elif max_rect.left < visible_rect.left: self.update_layout(Geometry.IntPoint(y=canvas_origin.y, x=-max_rect.left), self.canvas_size)
def __make_selection_visible(self, top): if self.__delegate: selected_indexes = list(self.__selection.indexes) if len(selected_indexes) > 0 and self.canvas_bounds is not None: min_index = min(selected_indexes) max_index = max(selected_indexes) min_rect = self.__rect_for_index(min_index) max_rect = self.__rect_for_index(max_index) visible_rect = self.container.visible_rect if (self.direction == Direction.Row and self.wrap) or (self.direction == Direction.Column and not self.wrap): if top: if min_rect.top < visible_rect.top: self.update_layout( Geometry.IntPoint(y=-min_rect.top, x=self.canvas_origin.x), self.canvas_size) elif min_rect.bottom > visible_rect.bottom: self.update_layout( Geometry.IntPoint( y=-min_rect.bottom + visible_rect.height, x=self.canvas_origin.x), self.canvas_size) else: if max_rect.bottom > visible_rect.bottom: self.update_layout( Geometry.IntPoint( y=-max_rect.bottom + visible_rect.height, x=self.canvas_origin.x), self.canvas_size) elif max_rect.top < visible_rect.top: self.update_layout( Geometry.IntPoint(y=-max_rect.top, x=self.canvas_origin.x), self.canvas_size) else: if top: if min_rect.left < visible_rect.left: self.update_layout( Geometry.IntPoint(y=self.canvas_origin.y, x=-min_rect.left), self.canvas_size) elif min_rect.right > visible_rect.right: self.update_layout( Geometry.IntPoint(y=self.canvas_origin.y, x=-min_rect.right + visible_rect.width), self.canvas_size) else: if max_rect.right > visible_rect.right: self.update_layout( Geometry.IntPoint(y=self.canvas_origin.y, x=-max_rect.right + visible_rect.width), self.canvas_size) elif max_rect.left < visible_rect.left: self.update_layout( Geometry.IntPoint(y=self.canvas_origin.y, x=-max_rect.left), self.canvas_size)
def mouse_pressed(self, x, y, modifiers): p = Geometry.IntPoint(y=y, x=x) value_path = self.__value_path_at_point(p) if value_path: if modifiers.shift: self.__extend_selection(value_path) elif modifiers.control: self.__toggle_selection(value_path) else: self.__set_selection(value_path) self.__mouse_pressed = True self.__mouse_position = Geometry.IntPoint(y=y, x=x) self.__mouse_item = value_path return True return super().mouse_pressed(x, y, modifiers)
def _repaint_visible(self, drawing_context: DrawingContext.DrawingContext, visible_rect: Geometry.IntRect) -> None: canvas_bounds = self.canvas_bounds if self.__delegate and canvas_bounds: item_width = canvas_bounds.width item_height = self.__item_height with drawing_context.saver(): items = self.__delegate.items max_index = len(items) top_visible_row = visible_rect.top // item_height bottom_visible_row = visible_rect.bottom // item_height for index in range(top_visible_row, bottom_visible_row + 1): if 0 <= index < max_index: rect = Geometry.IntRect(origin=Geometry.IntPoint(y=index * item_height, x=0), size=Geometry.IntSize(width=item_width, height=item_height)) if rect.intersects_rect(visible_rect): is_selected = self.__selection.contains(index) if is_selected: with drawing_context.saver(): drawing_context.begin_path() drawing_context.rect(rect.left, rect.top, rect.width, rect.height) drawing_context.fill_style = "#3875D6" if self.focused else "#DDD" drawing_context.fill() self.__delegate.paint_item(drawing_context, items[index], rect, is_selected) if index == self.__drop_index: with drawing_context.saver(): drop_border_width = 2.5 rect_in = rect.to_float_rect().inset(drop_border_width / 2, drop_border_width / 2).to_int_rect() drawing_context.begin_path() drawing_context.rect(rect_in.left, rect_in.top, rect_in.width, rect_in.height) drawing_context.line_width = drop_border_width drawing_context.stroke_style = "rgba(56, 117, 214, 0.8)" drawing_context.stroke()
def mouse_position_changed(self, x, y, modifiers): p = Geometry.IntPoint(x=x, y=y) if self.__drag_start is not None and Geometry.distance(p, self.__drag_start) > 2: self.__drag_start = None on_drag_pressed = self.on_drag_pressed if on_drag_pressed: on_drag_pressed(x, y, modifiers)
def test_layout_size_maintains_height_with_no_items_when_not_wrapped(self) -> None: selection = Selection.IndexedSelection() delegate = GridCanvasItemDelegate(0) canvas_item = GridCanvasItem.GridCanvasItem(delegate, selection, wrap=False) canvas_item.update_layout(Geometry.IntPoint(), Geometry.IntSize.make((40, 500))) canvas_bounds = canvas_item.canvas_bounds or Geometry.IntRect.empty_rect() self.assertEqual(canvas_bounds.height, 40)
def test_add_item_to_string_list_widget_causes_container_to_relayout(self): from nion.ui import Widgets ui = TestUI.UserInterface() widget = Widgets.StringListWidget(ui) with contextlib.closing(widget): canvas_item = widget.content_widget.children[0].canvas_item canvas_item.update_layout(Geometry.IntPoint(x=0, y=0), Geometry.IntSize(width=300, height=200), immediate=True) scroll_area_canvas_item = canvas_item.canvas_items[0].canvas_items[ 0] canvas_item.layout_immediate( Geometry.IntSize(width=300, height=200)) # check assumptions self.assertEqual(scroll_area_canvas_item.canvas_rect.height, 200) self.assertEqual( scroll_area_canvas_item.content.canvas_rect.height, 0) # add item self.assertFalse(canvas_item._needs_layout_for_testing) widget.items = ["abc"] # self.assertTrue(canvas_item._needs_layout_for_testing) # check that column was laid out again canvas_item.layout_immediate(Geometry.IntSize(width=300, height=200), force=False) self.assertEqual(scroll_area_canvas_item.canvas_rect.height, 200) self.assertEqual( scroll_area_canvas_item.content.canvas_rect.height, 20)
def _repaint_visible(self, drawing_context: DrawingContext.DrawingContext, visible_rect: Geometry.IntRect) -> None: canvas_size = self.canvas_size if self.__delegate and canvas_size and canvas_size.height > 0 and canvas_size.width > 0: item_size = self.__calculate_item_size(canvas_size) items = self.__delegate.items if self.__delegate else list() item_count = len(items) items_per_row = max(1, int(canvas_size.width / item_size.width) if self.wrap else item_count) items_per_column = max(1, int(canvas_size.height / item_size.height) if self.wrap else item_count) with drawing_context.saver(): top_visible_row = visible_rect.top // item_size.height bottom_visible_row = visible_rect.bottom // item_size.height left_visible_column = visible_rect.left // item_size.width right_visible_column = visible_rect.right // item_size.width for row in range(top_visible_row, bottom_visible_row + 1): for column in range(left_visible_column, right_visible_column + 1): if self.direction == Direction.Row: index = row * items_per_row + column else: index = row + column * items_per_column if 0 <= index < item_count: rect = Geometry.IntRect(origin=Geometry.IntPoint(y=row * item_size.height, x=column * item_size.width), size=Geometry.IntSize(width=item_size.width, height=item_size.height)) if rect.intersects_rect(visible_rect): is_selected = self.__selection.contains(index) if is_selected: with drawing_context.saver(): drawing_context.begin_path() drawing_context.rect(rect.left, rect.top, rect.width, rect.height) drawing_context.fill_style = "#3875D6" if self.focused else "#BBB" drawing_context.fill() self.__delegate.paint_item(drawing_context, items[index], rect, is_selected)
def draw_list_item(self, drawing_context: DrawingContext.DrawingContext, rect: Geometry.IntRect) -> None: with drawing_context.saver(): draw_rect = Geometry.IntRect(origin=rect.top_left + Geometry.IntPoint(y=4, x=4), size=Geometry.IntSize(h=72, w=72)) drawing_context.add(self.__create_thumbnail(draw_rect)) drawing_context.fill_style = "#000" drawing_context.font = "11px serif" drawing_context.fill_text(self.title_str, rect.left + 4 + 72 + 4, rect.top + 4 + 12) drawing_context.fill_text(self.format_str, rect.left + 4 + 72 + 4, rect.top + 4 + 12 + 15) drawing_context.fill_text(self.datetime_str, rect.left + 4 + 72 + 4, rect.top + 4 + 12 + 15 + 15) if self.status_str: drawing_context.fill_text(self.status_str, rect.left + 4 + 72 + 4, rect.top + 4 + 12 + 15 + 15 + 15) else: drawing_context.fill_style = "#888" drawing_context.fill_text(self.project_str, rect.left + 4 + 72 + 4, rect.top + 4 + 12 + 15 + 15 + 15)
def mouse_position_changed(self, x, y, modifiers): if self.__mouse_pressed_for_dragging: if not self.__mouse_dragging and Geometry.distance( self.__mouse_position, Geometry.IntPoint(y=y, x=x)) > 8: self.__mouse_dragging = True drag_started = getattr(self.__delegate, "drag_started", None) if self.__delegate else None if callable(drag_started): root_container = self.root_container if root_container: root_container.bypass_request_focus() drag_started(self.__mouse_index, x, y, modifiers) # once a drag starts, mouse release will not be called; call it here instead self.__mouse_released(x, y, modifiers, False) # TODO: delete soon. only here for backwards compatibility. on_drag_started = getattr(self.__delegate, "on_drag_started", None) if self.__delegate else None if callable(on_drag_started): root_container = self.root_container if root_container: root_container.bypass_request_focus() on_drag_started(self.__mouse_index, x, y, modifiers) # once a drag starts, mouse release will not be called; call it here instead self.__mouse_released(x, y, modifiers, False) return True return super().mouse_position_changed(x, y, modifiers)
def test_eels_data_camera_current_is_consistent(self): instrument = InstrumentDevice.Instrument("usim_stem_controller") # set up the scan context; these are here temporarily until the scan context architecture is fully implemented instrument._update_scan_context(Geometry.IntSize(256, 256), Geometry.FloatPoint(), 10, 0.0) instrument._set_scan_context_probe_position( instrument.scan_context, Geometry.FloatPoint(0.5, 0.5)) # grab scan data instrument.get_scan_data( scan_base.ScanFrameParameters({ "size": (256, 256), "pixel_time_us": 1, "fov_nm": 10 }), 0) instrument.validate_probe_position() camera = instrument._get_camera_simulator("eels") camera_size = camera._camera_shape camera.noise.enabled = False readout_area = Geometry.IntRect(origin=Geometry.IntPoint(), size=camera_size) binning_shape = Geometry.IntSize(1, 1) # get the value at 200eV and ZLP offset of 0 instrument.ZLPoffset = -20 exposure_s = 0.01 d = xd.sum(instrument.get_camera_data("eels", readout_area, binning_shape, exposure_s), axis=0).data # confirm it is a reasonable value camera_current_pA = numpy.sum( d) / exposure_s / instrument.counts_per_electron / 6.242e18 * 1e12 # print(f"current {camera_current_pA :#.2f}pA") self.assertTrue(190 < camera_current_pA < 210)
def _repaint_visible(self, drawing_context, visible_rect): if self.__delegate: canvas_bounds = self.canvas_bounds item_width = int(canvas_bounds.width) item_height = self.__item_height with drawing_context.saver(): items = self.__delegate.items max_index = len(items) top_visible_row = visible_rect.top // item_height bottom_visible_row = visible_rect.bottom // item_height for index in range(top_visible_row, bottom_visible_row + 1): if 0 <= index < max_index: rect = Geometry.IntRect(origin=Geometry.IntPoint(y=index * item_height, x=0), size=Geometry.IntSize(width=item_width, height=item_height)) if rect.intersects_rect(visible_rect): is_selected = self.__selection.contains(index) if is_selected: drawing_context.save() drawing_context.begin_path() drawing_context.rect(rect.left, rect.top, rect.width, rect.height) drawing_context.fill_style = "#3875D6" if self.focused else "#DDD" drawing_context.fill() drawing_context.restore() self.__delegate.paint_item(drawing_context, items[index], rect, is_selected)
def test_eels_data_is_consistent_when_energy_offset_changes(self): instrument = InstrumentDevice.Instrument("usim_stem_controller") instrument.get_scan_data( scan_base.ScanFrameParameters({ "size": (256, 256), "pixel_time_us": 1, "fov_nm": 10 }), 0) instrument.validate_probe_position() camera = instrument._get_camera_simulator("eels") camera_size = camera._camera_shape camera.noise.enabled = False readout_area = Geometry.IntRect(origin=Geometry.IntPoint(), size=camera_size) binning_shape = Geometry.IntSize(1, 1) # get the value at 200eV and ZLP offset of 0 instrument.ZLPoffset = 0 d = xd.sum(instrument.get_camera_data("eels", readout_area, binning_shape, 0.01), axis=0) index200_0 = int( d.dimensional_calibrations[-1].convert_from_calibrated_value(200)) value200_0 = d.data[index200_0] # get the value at 200eV and ZLP offset of 100 instrument.ZLPoffset = 100 d = xd.sum(instrument.get_camera_data("eels", readout_area, binning_shape, 0.01), axis=0) index200_100 = int( d.dimensional_calibrations[-1].convert_from_calibrated_value(200)) value200_100 = d.data[index200_100] self.assertEqual(int(value200_0 / 100), int(value200_100 / 100))
def mouse_position_changed(self, x, y, modifiers): pt = Geometry.IntPoint(y=y, x=x) if self.__mouse_pressed_position and Geometry.distance(self.__mouse_pressed_position, pt) > 12: on_drag_pressed = self.on_drag_pressed if callable(on_drag_pressed): self.__mouse_pressed_position = None on_drag_pressed()
def test_eels_data_thickness_is_consistent(self): instrument = InstrumentDevice.Instrument("usim_stem_controller") # use the flake sample instrument.sample_index = 0 # set up the scan context; these are here temporarily until the scan context architecture is fully implemented instrument._update_scan_context(Geometry.IntSize(256, 256), Geometry.FloatPoint(), 10, 0.0) instrument._set_scan_context_probe_position( instrument.scan_context, Geometry.FloatPoint(0.5, 0.5)) # grab scan data instrument.get_scan_data( scan_base.ScanFrameParameters({ "size": (256, 256), "pixel_time_us": 1, "fov_nm": 10 }), 0) instrument.validate_probe_position() camera = instrument._get_camera_simulator("eels") camera_size = camera._camera_shape camera.noise.enabled = False readout_area = Geometry.IntRect(origin=Geometry.IntPoint(), size=camera_size) binning_shape = Geometry.IntSize(1, 1) # get the value at 200eV and ZLP offset of 0 instrument.ZLPoffset = -20 d = xd.sum(instrument.get_camera_data("eels", readout_area, binning_shape, 0.01), axis=0).data # confirm it is a reasonable value # print(measure_thickness(d)) self.assertTrue(0.40 < measure_thickness(d) < 1.00)
def plot(self, data: numpy.ndarray, offset_m: Geometry.FloatPoint, fov_nm: Geometry.FloatSize, center_nm: Geometry.FloatPoint, shape: Geometry.IntSize) -> int: # TODO: how does center_nm interact with stage position? # TODO: take into account feature angle # TODO: take into account frame parameters angle # TODO: expand features to other shapes than rectangle scan_rect_m = self.get_scan_rect_m(offset_m, fov_nm, center_nm) feature_rect_m = self.get_feature_rect_m() sum = 0 if scan_rect_m.intersects_rect(feature_rect_m): feature_rect_top_px = int(shape[0] * (feature_rect_m.top - scan_rect_m.top) / scan_rect_m.height) feature_rect_left_px = int(shape[1] * (feature_rect_m.left - scan_rect_m.left) / scan_rect_m.width) feature_rect_height_px = int(shape[0] * feature_rect_m.height / scan_rect_m.height) feature_rect_width_px = int(shape[1] * feature_rect_m.width / scan_rect_m.width) if feature_rect_top_px < 0: feature_rect_height_px += feature_rect_top_px feature_rect_top_px = 0 if feature_rect_left_px < 0: feature_rect_width_px += feature_rect_left_px feature_rect_left_px = 0 if feature_rect_top_px + feature_rect_height_px > shape[0]: feature_rect_height_px = shape[0] - feature_rect_top_px if feature_rect_left_px + feature_rect_width_px > shape[1]: feature_rect_width_px = shape[1] - feature_rect_left_px feature_rect_origin_px = Geometry.IntPoint(y=feature_rect_top_px, x=feature_rect_left_px) feature_rect_size_px = Geometry.IntSize(height=feature_rect_height_px, width=feature_rect_width_px) feature_rect_px = Geometry.IntRect(feature_rect_origin_px, feature_rect_size_px) data[feature_rect_px.top:feature_rect_px.bottom, feature_rect_px.left:feature_rect_px.right] += 1.0 sum += (feature_rect_px.bottom - feature_rect_px.top) * (feature_rect_px.right - feature_rect_px.left) return sum
def wheel_changed(self, x, y, dx, dy, is_horizontal): dy = dy if not is_horizontal else 0.0 new_canvas_origin = Geometry.IntPoint.make( self.canvas_origin) + Geometry.IntPoint(x=0, y=dy) self.update_layout(new_canvas_origin, self.canvas_size) self.update() return True
def content_height_changed(content_height): desired_height = content_height + 12 metadata_editor_canvas_item.sizing.set_fixed_height(desired_height) metadata_editor_widget.canvas_item.update_layout( Geometry.IntPoint(), scroll_area.size) if metadata_editor_canvas_item._has_layout: column.size = Geometry.IntSize(height=desired_height, width=column.size.width)
def grabbed_mouse_position_changed( self, dx: int, dy: int, modifiers: UserInterface.KeyboardModifiers) -> bool: if not self.__discard_first and callable( self.on_mouse_position_changed_by): self.on_mouse_position_changed_by(Geometry.IntPoint(x=dx, y=dy)) self.__discard_first = False return True
def wheel_changed(self, x: int, y: int, dx: int, dy: int, is_horizontal: bool) -> bool: dy = dy if not is_horizontal else 0 canvas_rect = self.canvas_rect if canvas_rect: new_canvas_origin = canvas_rect.origin + Geometry.IntPoint(x=0, y=dy) self.update_layout(new_canvas_origin, canvas_rect.size) self.update() return True
def context_menu_event(self, x, y, gx, gy): p = Geometry.IntPoint(y=y, x=x) value_path = self.__value_path_at_point(p) if value_path: if not self.__is_selected(value_path): self.__set_selection(value_path) return self.__context_menu_event(value_path, x, y, gx, gy) return self.__context_menu_event(None, x, y, gx, gy)
def mouse_position_changed(self, x, y, modifiers): if super().mouse_position_changed(x, y, modifiers): return True if self.delegate.tool_mode == "pointer": self.cursor_shape = "arrow" self.__last_mouse = Geometry.IntPoint(x=x, y=y) self.__update_cursor_info() return True
def mouse_pressed(self, x, y, modifiers): if self.__delegate: mouse_index = y // self.__item_height max_index = self.__delegate.item_count if mouse_index >= 0 and mouse_index < max_index: self.__mouse_index = mouse_index self.__mouse_pressed = True handled = False if self.__delegate and hasattr(self.__delegate, "mouse_pressed_in_item") and self.__delegate.mouse_pressed_in_item: handled = self.__delegate.mouse_pressed_in_item(mouse_index, Geometry.IntPoint(y=y - mouse_index * self.__item_height, x=x), modifiers) if handled: self.__mouse_index = None # prevent selection handling if not handled and not modifiers.shift and not modifiers.control: self.__mouse_pressed_for_dragging = True self.__mouse_position = Geometry.IntPoint(y=y, x=x) return True return super().mouse_pressed(x, y, modifiers)
def __rect_for_index(self, index: int) -> Geometry.IntRect: canvas_bounds = self.canvas_bounds if canvas_bounds: item_width = canvas_bounds.width item_height = self.__item_height return Geometry.IntRect(origin=Geometry.IntPoint(y=index * item_height, x=0), size=Geometry.IntSize(width=item_width, height=item_height)) return Geometry.IntRect.empty_rect()
def mouse_position_changed(self, x, y, modifiers): if self.__mouse_pressed: if not self.__mouse_dragging and Geometry.distance( self.__mouse_position, Geometry.IntPoint(y=y, x=x)) > 8: self.__mouse_dragging = True self.__drag_started(self.__mouse_item, x, y, modifiers) # once a drag starts, mouse release will not be called; call it here instead self.mouse_released(x, y, modifiers) return True return super().mouse_position_changed(x, y, modifiers)