Esempio n. 1
0
    def __init__(self, ui: UserInterface.UserInterface, text: str) -> None:
        column_widget = ui.create_column_widget()
        super().__init__(column_widget)
        self.on_button_clicked: typing.Optional[typing.Callable[[],
                                                                None]] = None
        font = "normal 11px serif"
        font_metrics = ui.get_font_metrics(font, text)
        text_button_canvas_item = TextButtonCanvasItem(text)
        text_button_canvas_item.update_sizing(
            text_button_canvas_item.sizing.with_fixed_size(
                Geometry.IntSize(height=font_metrics.height + 6,
                                 width=font_metrics.width + 6)))

        def button_clicked() -> None:
            if callable(self.on_button_clicked):
                self.on_button_clicked()

        text_button_canvas_item.on_button_clicked = button_clicked

        text_button_canvas_widget = ui.create_canvas_widget(properties={
            "height": 20,
            "width": 20
        })
        text_button_canvas_widget.canvas_item.add_canvas_item(
            text_button_canvas_item)
        # ugh. this is a partially working stop-gap when a canvas item is in a widget it will not get mouse exited reliably
        root_container = text_button_canvas_item.root_container
        if root_container:
            text_button_canvas_widget.on_mouse_exited = root_container.canvas_widget.on_mouse_exited

        column_widget.add(text_button_canvas_widget)
Esempio n. 2
0
 def __init__(
     self,
     ui: UserInterface.UserInterface,
     create_list_item_widget: typing.Callable[[typing.Any],
                                              UserInterface.BoxWidget],
     header_widget: typing.Optional[UserInterface.Widget] = None,
     header_for_empty_list_widget: typing.Optional[
         UserInterface.Widget] = None
 ) -> None:
     column_widget = ui.create_column_widget()
     super().__init__(column_widget)
     self.__binding: typing.Optional[Binding.Binding] = None
     self.content_section = ui.create_column_widget()
     self.content_section.widget_id = "content_section"
     header_column = ui.create_column_widget()
     self.header_widget = header_widget
     self.header_for_empty_list_widget = header_for_empty_list_widget
     if self.header_widget:
         header_column.add(self.header_widget)
     if self.header_for_empty_list_widget:
         header_column.add(self.header_for_empty_list_widget)
     column_widget.add(header_column)
     content_column = ui.create_column_widget()
     content_column.add(self.content_section)
     content_column.add_stretch()
     column_widget.add(content_column)
     column_widget.add_stretch()
     self.create_list_item_widget = create_list_item_widget
Esempio n. 3
0
    def __init__(self, ui: UserInterface.UserInterface, data_grid_controller: DataGridController):
        super().__init__(ui.create_column_widget())
        self.data_grid_controller = data_grid_controller
        data_grid_widget = ui.create_canvas_widget()
        data_grid_widget.canvas_item.add_canvas_item(data_grid_controller.canvas_item)
        self.content_widget.add(data_grid_widget)

        def data_list_drag_started(mime_data: UserInterface.MimeData, thumbnail_data: numpy.ndarray) -> None:
            self.drag(mime_data, thumbnail_data)

        data_grid_controller.on_drag_started = data_list_drag_started
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 thumbnail_source: AbstractThumbnailSource,
                 size: typing.Optional[Geometry.IntSize] = None,
                 properties: typing.Optional[
                     Persistence.PersistentDictType] = None,
                 is_expanding: bool = False) -> None:
        content_widget = ui.create_column_widget(
            properties={
                "size-policy-horizontal": "expanding",
                "size-policy-vertical": "expanding"
            } if is_expanding else None)
        super().__init__(content_widget)
        if not is_expanding:
            size = size or Geometry.IntSize(width=80, height=80)
        thumbnail_canvas_item = ThumbnailCanvasItem(ui, thumbnail_source, size)
        properties = properties or ({
            "height": size.height,
            "width": size.width
        } if size else dict())
        bitmap_canvas_widget = ui.create_canvas_widget(properties=properties)
        thumbnail_square = CanvasItem.CanvasItemComposition()
        thumbnail_square.layout = SquareCanvasItemLayout()
        thumbnail_square.add_canvas_item(thumbnail_canvas_item)
        bitmap_canvas_widget.canvas_item.add_canvas_item(thumbnail_square)
        content_widget.add(bitmap_canvas_widget)
        self.on_drop_mime_data: typing.Optional[typing.Callable[
            [UserInterface.MimeData, int, int], str]] = None
        self.on_drag: typing.Optional[typing.Callable[[
            UserInterface.MimeData, typing.Optional[_ImageDataType], int, int
        ], None]] = None
        self.on_delete: typing.Optional[typing.Callable[[], None]] = None

        def drop_mime_data(mime_data: UserInterface.MimeData, x: int,
                           y: int) -> str:
            if callable(self.on_drop_mime_data):
                return self.on_drop_mime_data(mime_data, x, y)
            return "ignore"

        def drag(mime_data: UserInterface.MimeData,
                 thumbnail: typing.Optional[_NDArray], x: int, y: int) -> None:
            on_drag = self.on_drag
            if callable(on_drag):
                on_drag(mime_data, thumbnail, x, y)

        def delete() -> None:
            on_delete = self.on_delete
            if callable(on_delete):
                on_delete()

        thumbnail_canvas_item.on_drop_mime_data = drop_mime_data
        thumbnail_canvas_item.on_drag = drag
        thumbnail_canvas_item.on_delete = delete
Esempio n. 5
0
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 locals=None,
                 properties=None):
        super().__init__(ui.create_column_widget())

        self.prompt = ">>> "
        self.continuation_prompt = "... "

        self.__cursor_position = None

        self.__text_edit_widget = ui.create_text_edit_widget(properties)
        self.__text_edit_widget.set_text_color("white")
        self.__text_edit_widget.set_text_background_color("black")
        self.__text_edit_widget.set_text_font(
            Panel.Panel.get_monospace_text_font())
        self.__text_edit_widget.set_line_height_proportional(
            Panel.Panel.get_monospace_proportional_line_height())
        self.__text_edit_widget.word_wrap_mode = "anywhere"
        self.__text_edit_widget.on_cursor_position_changed = self.__cursor_position_changed
        self.__text_edit_widget.on_selection_changed = self.__selection_changed
        self.__text_edit_widget.on_return_pressed = self.__return_pressed
        self.__text_edit_widget.on_key_pressed = self.__key_pressed
        self.__text_edit_widget.on_insert_mime_data = self.__insert_mime_data

        class StdoutCatcher:
            def __init__(self, text_edit_widget):
                self.__text_edit_widget = text_edit_widget

            def write(self, stuff):
                if self.__text_edit_widget:
                    self.__text_edit_widget.append_text(str(stuff).rstrip())

            def flush(self):
                pass

        stdout = StdoutCatcher(self.__text_edit_widget)

        locals = locals if locals is not None else dict()
        locals.update({
            '__name__': None,
            '__console__': None,
            '__doc__': None,
            '_stdout': stdout
        })

        self.__state_controller = ConsoleWidgetStateController(locals)

        self.__text_edit_widget.append_text(self.prompt)
        self.__text_edit_widget.move_cursor_position("end")
        self.__last_position = copy.deepcopy(self.__cursor_position)

        self.content_widget.add(self.__text_edit_widget)
Esempio n. 6
0
 def construct(self, d_type: str, ui: UserInterface.UserInterface, window: typing.Optional[Window.Window],
               d: Declarative.UIDescription, handler: Declarative.HandlerLike,
               finishes: typing.List[typing.Callable[[], None]]) -> typing.Optional[UserInterface.Widget]:
     if d_type == "notification_char_button":
         font = "normal 13px serif"
         text = d["text"]
         fm = ui.get_font_metrics(font, text)
         canvas_item = CharButtonCanvasItem(text)
         widget = ui.create_canvas_widget(properties={"height": fm.height + 4, "width": fm.width + 4})
         widget.canvas_item.add_canvas_item(canvas_item)
         if handler:
             Declarative.connect_name(widget, d, handler)
             Declarative.connect_attributes(widget, d, handler, finishes)
             Declarative.connect_event(widget, canvas_item, d, handler, "on_clicked", [])
         return widget
     return None
Esempio n. 7
0
    def __init__(
        self,
        ui: UserInterface.UserInterface,
        rgba_bitmap_data: typing.Optional[DrawingContext.RGBA32Type] = None,
        properties: typing.Optional[typing.Mapping[str, typing.Any]] = None
    ) -> None:
        column_widget = ui.create_column_widget(properties=properties)
        super().__init__(column_widget)
        self.ui = ui
        self.on_clicked = None
        self.__image_binding: typing.Optional[Binding.Binding] = None

        def button_clicked() -> None:
            if callable(self.on_clicked):
                self.on_clicked()

        self.__bitmap_canvas_item = CanvasItem.BitmapButtonCanvasItem(
            rgba_bitmap_data)
        self.__bitmap_canvas_item.on_button_clicked = button_clicked
        bitmap_canvas_widget = self.ui.create_canvas_widget()
        bitmap_canvas_widget.canvas_item.add_canvas_item(
            self.__bitmap_canvas_item)
        column_widget.add(bitmap_canvas_widget)

        self.image = rgba_bitmap_data
Esempio n. 8
0
    def __init__(self, ui: UserInterface.UserInterface,
                 item_explorer_controller: ItemExplorerController):
        content_widget = ui.create_column_widget()
        super().__init__(content_widget)
        self.item_explorer_controller = item_explorer_controller
        data_list_widget = ui.create_canvas_widget()
        data_list_widget.canvas_item.add_canvas_item(
            item_explorer_controller.canvas_item)
        content_widget.add(data_list_widget)

        def data_list_drag_started(
                mime_data: UserInterface.MimeData,
                thumbnail_data: typing.Optional[_NDArray]) -> None:
            self.drag(mime_data, thumbnail_data)

        item_explorer_controller.on_drag_started = data_list_drag_started
Esempio n. 9
0
    def __init__(self, ui: UserInterface.UserInterface, statistics_model: Model.PropertyModel[typing.Dict[str, str]]) -> None:
        content_widget = ui.create_column_widget(properties={"min-height": 18 * 3, "max-height": 18 * 3})
        super().__init__(content_widget)

        # create property models for the UI
        self._stats1_property = Model.PropertyModel[str](str())
        self._stats2_property = Model.PropertyModel[str](str())

        self.__statistics_model = statistics_model

        def statistics_changed(key: str) -> None:
            if key == "value":
                statistics_data = self.__statistics_model.value or dict()
                statistic_strings = list()
                for key in sorted(statistics_data.keys()):
                    value = statistics_data[key]
                    if value is not None:
                        statistic_str = "{0} {1}".format(key, value)
                    else:
                        statistic_str = "{0} {1}".format(key, _("N/A"))
                    statistic_strings.append(statistic_str)
                self._stats1_property.value = "\n".join(statistic_strings[:(len(statistic_strings) + 1) // 2])
                self._stats2_property.value = "\n".join(statistic_strings[(len(statistic_strings) + 1) // 2:])

        self.__statistics_property_changed_event_listener = self.__statistics_model.property_changed_event.listen(statistics_changed)

        statistics_changed("value")

        stats_column1 = ui.create_column_widget(properties={"min-width": 140, "max-width": 140})
        stats_column2 = ui.create_column_widget(properties={"min-width": 140, "max-width": 140})
        stats_column1_label = ui.create_label_widget()
        stats_column2_label = ui.create_label_widget()
        stats_column1.add(stats_column1_label)
        stats_column2.add(stats_column2_label)
        stats_section = ui.create_row_widget()
        stats_section.add_spacing(13)
        stats_section.add(stats_column1)
        stats_section.add_stretch()
        stats_section.add(stats_column2)
        stats_section.add_spacing(13)

        stats_column1_label.bind_text(Binding.PropertyBinding(self._stats1_property, "value"))
        stats_column2_label.bind_text(Binding.PropertyBinding(self._stats2_property, "value"))

        content_widget.add(stats_section)
Esempio n. 10
0
 def __init__(self, ui: UserInterface.UserInterface, *, message: str, parent_window: Window.Window):
     super().__init__(ui, parent_window=parent_window)
     self._document_window.set_window_style(["tool", "frameless-hint"])
     self._document_window.set_palette_color("background", 255, 255, 204, 224)
     content_column = ui.create_column_widget()
     content_row = ui.create_row_widget()
     label = ui.create_label_widget(message, properties={"width": NotificationDialog.width})
     label.word_wrap = True
     content_row.add_spacing(8)
     content_row.add(label)
     content_row.add_spacing(8)
     content_column.add_spacing(8)
     content_column.add(content_row)
     content_column.add_stretch()
     content_column.add_spacing(8)
     self.__start_time = time.time()
     self.attach_widget(content_column)
     parent_window.register_dialog(self)
Esempio n. 11
0
 def __get_calculated_data(
     self, ui: UserInterface.UserInterface
 ) -> typing.Optional[DrawingContext.RGBA32Type]:
     drawing_context, shape = DisplayPanel.preview(
         DisplayPanel.DisplayPanelUISettings(ui), self.__display_item, 512,
         512)
     thumbnail_drawing_context = DrawingContext.DrawingContext()
     thumbnail_drawing_context.scale(self.width / 512, self.height / 512)
     thumbnail_drawing_context.translate(0, (shape[1] - shape[0]) * 0.5)
     thumbnail_drawing_context.add(drawing_context)
     return ui.create_rgba_image(thumbnail_drawing_context, self.width,
                                 self.height)
Esempio n. 12
0
    def __init__(self, ui: UserInterface.UserInterface,
                 app: Application.Application) -> None:
        super().__init__(ui,
                         _("Project Manager"),
                         app=app,
                         window_style="window",
                         persistent_id="ProjectsDialog")

        projects_section = ProjectTreeWidget(self, app.profile)

        column = ui.create_column_widget()
        column.add(projects_section)
        column.add_stretch()

        scroll_area = ui.create_scroll_area_widget()
        scroll_area.set_scrollbar_policies("off", "needed")
        scroll_area.content = column

        column = self.content
        column.add_spacing(6)
        column.add(scroll_area)
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 label: str,
                 instrument: InstrumentDevice.Instrument,
                 xy_property: str,
                 unit: str = "nm",
                 multiplier: float = 1E9) -> None:
        row_widget = ui.create_row_widget()
        super().__init__(row_widget)

        stage_x_field = ui.create_line_edit_widget()
        stage_x_field.bind_text(
            Control2DBinding(
                instrument, xy_property, "x",
                Converter.PhysicalValueToStringConverter(unit, multiplier)))

        stage_y_field = ui.create_line_edit_widget()
        stage_y_field.bind_text(
            Control2DBinding(
                instrument, xy_property, "y",
                Converter.PhysicalValueToStringConverter(unit, multiplier)))

        row_widget.add_spacing(8)
        row_widget.add(ui.create_label_widget(label))
        row_widget.add_spacing(8)
        row_widget.add(ui.create_label_widget(_("X")))
        row_widget.add_spacing(8)
        row_widget.add(stage_x_field)
        row_widget.add_spacing(8)
        row_widget.add(ui.create_label_widget(_("Y")))
        row_widget.add_spacing(8)
        row_widget.add(stage_y_field)
        row_widget.add_spacing(8)
Esempio n. 14
0
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 color: typing.Optional[str] = None):
        column_widget = ui.create_column_widget()
        super().__init__(column_widget)

        self.on_color_changed: typing.Optional[typing.Callable[
            [typing.Optional[str]], None]] = None

        color_button_canvas_item = ColorButtonCanvasItem(color)
        color_button_canvas_item.update_sizing(
            color_button_canvas_item.sizing.with_fixed_size(
                Geometry.IntSize(height=30, width=44)))

        def button_clicked() -> None:
            start_color = self.color or "rgba(255, 255, 255, 0.0)"
            color = ui.get_color_dialog(_("Select Color"), start_color, True)
            if color != start_color:
                self.color = color
                if callable(self.on_color_changed):
                    self.on_color_changed(self.color)

        color_button_canvas_item.on_button_clicked = button_clicked

        color_button_canvas_widget = ui.create_canvas_widget(properties={
            "height": 30,
            "width": 44
        })
        color_button_canvas_widget.canvas_item.add_canvas_item(
            color_button_canvas_item)
        # ugh. this is a partially working stop-gap when a canvas item is in a widget it will not get mouse exited reliably
        root_container = color_button_canvas_item.root_container
        if root_container:
            color_button_canvas_widget.on_mouse_exited = root_container.canvas_widget.on_mouse_exited

        self.__color_button_canvas_item = color_button_canvas_item

        column_widget.add(color_button_canvas_widget)

        self.__color_binding: typing.Optional[Binding.Binding] = None
Esempio n. 15
0
def pose_confirmation_message_box(ui: UserInterface.UserInterface, message_column: UserInterface.BoxWidget,
                                  caption: str, accepted_fn: typing.Callable[[], None],
                                  rejected_fn: typing.Optional[typing.Callable[[], None]] = None,
                                  accepted_text: typing.Optional[str] = None,
                                  rejected_text: typing.Optional[str] = None,
                                  display_rejected: bool = True) -> UserInterface.BoxWidget:
    if accepted_text is None:
        accepted_text = _("OK")
    if rejected_text is None:
        rejected_text = _("Cancel")
    message_box_widget = ui.create_column_widget()  # properties={"stylesheet": "background: #FFD"}

    def reject_button_clicked() -> typing.Any:
        if rejected_fn:
            rejected_fn()
        return False

    def accept_button_clicked() -> typing.Any:
        accepted_fn()
        return False

    reject_button = ui.create_push_button_widget(rejected_text)
    reject_button.on_clicked = reject_button_clicked
    accepted_button = ui.create_push_button_widget(accepted_text)
    accepted_button.on_clicked = accept_button_clicked
    caption_row = ui.create_row_widget()
    caption_row.add_spacing(12)
    caption_row.add(ui.create_label_widget(caption))
    if display_rejected:
        caption_row.add_spacing(12)
        caption_row.add(reject_button)
    caption_row.add_spacing(12)
    caption_row.add(accepted_button)
    caption_row.add_stretch()
    message_box_widget.add_spacing(6)
    message_box_widget.add(caption_row)
    message_box_widget.add_spacing(4)
    message_column.add(message_box_widget)
    return message_box_widget
Esempio n. 16
0
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 section_title: str,
                 section: UserInterface.Widget,
                 section_id: typing.Optional[str] = None) -> None:
        section_widget = ui.create_column_widget()
        super().__init__(section_widget)

        section_title_row = ui.create_row_widget()

        twist_down_canvas_item = CanvasItem.TwistDownCanvasItem()

        twist_down_canvas_widget = ui.create_canvas_widget(properties={
            "height": 20,
            "width": 20
        })
        twist_down_canvas_widget.canvas_item.add_canvas_item(
            twist_down_canvas_item)

        section_title_label = ui.create_label_widget(section_title)
        section_title_label.text_font = "bold"

        section_title_row.add(twist_down_canvas_widget)
        section_title_row.add(section_title_label)
        section_title_row.add_stretch()
        section_widget.add(section_title_row)
        section_content_row = ui.create_row_widget()
        section_content_column = ui.create_column_widget()
        section_content_column.add_spacing(4)
        section_content_column.add(section)
        section_content_row.add_spacing(20)
        section_content_row.add(section_content_column)
        section_widget.add(section_content_row)
        section_widget.add_spacing(4)

        def toggle() -> None:
            twist_down_canvas_item.checked = not twist_down_canvas_item.checked
            section_content_column.visible = twist_down_canvas_item.checked
            if section_id:
                ui.set_persistent_string(
                    section_id,
                    "true" if twist_down_canvas_item.checked else "false")

        section_open = ui.get_persistent_string(
            section_id, "true") == "true" if section_id else True
        twist_down_canvas_item.checked = section_open
        section_content_column.visible = section_open
        twist_down_canvas_item.on_button_clicked = toggle

        self.section_title_row = section_title_row
        self.__twist_down_canvas_item = twist_down_canvas_item
Esempio n. 17
0
def pose_get_string_message_box(ui: UserInterface.UserInterface, message_column: UserInterface.BoxWidget, caption: str,
                                text: str, accepted_fn: typing.Callable[[str], None],
                                rejected_fn: typing.Optional[typing.Callable[[], None]] = None,
                                accepted_text: typing.Optional[str] = None,
                                rejected_text: typing.Optional[str] = None) -> UserInterface.BoxWidget:
    if accepted_text is None:
        accepted_text = _("OK")
    if rejected_text is None:
        rejected_text = _("Cancel")
    message_box_widget = ui.create_column_widget()  # properties={"stylesheet": "background: #FFD"}
    caption_row = ui.create_row_widget()
    caption_row.add_spacing(12)
    caption_row.add(ui.create_label_widget(caption))
    caption_row.add_stretch()
    inside_row = ui.create_row_widget()

    def reject_button_clicked() -> typing.Any:
        if callable(rejected_fn):
            rejected_fn()
        return False

    def accept_button_clicked() -> typing.Any:
        accepted_fn(string_edit_widget.text or str())
        return False

    string_edit_widget = ui.create_line_edit_widget()
    string_edit_widget.text = text
    string_edit_widget.on_return_pressed = accept_button_clicked
    string_edit_widget.on_escape_pressed = reject_button_clicked
    reject_button = ui.create_push_button_widget(rejected_text)
    reject_button.on_clicked = reject_button_clicked
    accepted_button = ui.create_push_button_widget(accepted_text)
    accepted_button.on_clicked = accept_button_clicked
    inside_row.add_spacing(12)
    inside_row.add(string_edit_widget)
    inside_row.add_spacing(12)
    inside_row.add(reject_button)
    inside_row.add_spacing(12)
    inside_row.add(accepted_button)
    inside_row.add_stretch()
    message_box_widget.add_spacing(6)
    message_box_widget.add(caption_row)
    message_box_widget.add_spacing(4)
    message_box_widget.add(inside_row)
    message_box_widget.add_spacing(4)
    message_column.add(message_box_widget)
    string_edit_widget.select_all()
    string_edit_widget.focused = True
    return message_box_widget
Esempio n. 18
0
 def __init__(
         self,
         ui: UserInterface.UserInterface,
         properties: typing.Optional[typing.Mapping[str,
                                                    typing.Any]] = None):
     column_widget = ui.create_column_widget(properties=properties)
     super().__init__(column_widget)
     self.ui = ui
     self.on_clicked: typing.Optional[typing.Callable[[], None]] = None
     self.__bitmap_canvas_item = CanvasItem.BitmapButtonCanvasItem(
         None, border_color="#CCC")
     self.__bitmap_canvas_item.on_button_clicked = self.__handle_clicked
     self.__value: typing.Optional[int] = None
     self.__group_value: typing.Optional[int] = None
     self.__on_group_value_changed: typing.Optional[typing.Callable[
         [typing.Optional[int]], None]] = None
     self.__group_value_binding: typing.Optional[Binding.Binding] = None
     self.__icon_binding: typing.Optional[Binding.Binding] = None
     self.__enabled = True
     self.__checked = False
     bitmap_canvas_widget = self.ui.create_canvas_widget()
     bitmap_canvas_widget.canvas_item.add_canvas_item(
         self.__bitmap_canvas_item)
     column_widget.add(bitmap_canvas_widget)
Esempio n. 19
0
 def create_combo_box(self, ui: UserInterface.UserInterface) -> UserInterface.ComboBoxWidget:
     combo_box = ui.create_combo_box_widget(self.hardware_sources_model.value, item_getter=operator.attrgetter("display_name"))
     combo_box.bind_items(Binding.PropertyBinding(self.hardware_sources_model, "value"))
     combo_box.bind_current_index(Binding.PropertyBinding(self.hardware_source_index_model, "value"))
     return combo_box
Esempio n. 20
0
def start_mouse_tracker(ui: UserInterface.UserInterface,
                        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) -> None:
    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 handle_close_later(
            document_window: UserInterface.Window) -> None:
        document_window.request_close()

    def handle_close(document_window: UserInterface.Window) -> None:
        tracking_canvas_item.release_mouse()
        event_loop.create_task(handle_close_later(document_window))

    def activation_changed(document_window: UserInterface.Window,
                           activated: bool) -> None:
        if not activated:
            handle_close(document_window)

    # create the popup window
    document_window = ui.create_document_window()
    document_window.window_style = "mousegrab"

    def close_window(geometry: str, state: str) -> None:
        ui.destroy_document_window(document_window)

    document_window.on_about_to_close = close_window

    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.update_sizing(
        tracking_canvas_item.sizing.with_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)
Esempio n. 21
0
    def __init__(self, ui: UserInterface.UserInterface, app: Application.BaseApplication) -> None:
        super().__init__(ui, _("Preferences"), app=app)

        self.ui = ui
        self.document_controller = self

        properties = dict()
        properties["min-height"] = 400
        properties["min-width"] = 800

        preference_pane_delegates: typing.List[PreferencePaneDelegate] = list()
        preference_pane_delegate_id_ref: typing.List[typing.Optional[str]] = [None]

        content_stack = ui.create_stack_widget()

        def change_selection(indexes: typing.AbstractSet[int]) -> None:
            index = list(indexes)[0]
            assert 0 <= index < len(preference_pane_delegates)
            content_stack.current_index = index
            preference_pane_delegate_id_ref[0] = preference_pane_delegates[index].identifier

        selector_list_widget = Widgets.StringListWidget(ui, selection_style=Selection.Style.single_or_none)
        selector_list_widget.on_selection_changed = change_selection

        row = self.ui.create_row_widget(properties={"min-width": 640, "min-height": 320})
        selector_column = self.ui.create_column_widget(properties={"width": 200})
        selector_row = ui.create_row_widget()
        selector_row.add_spacing(8)
        selector_row.add(selector_list_widget)
        selector_row.add_spacing(8)
        selector_column.add_spacing(8)
        selector_column.add(selector_row)
        selector_column.add_spacing(8)
        content_column = self.ui.create_column_widget()
        content_column.add(content_stack)
        row.add(selector_column)
        row.add(content_column)
        self.content.add(row)

        self.add_button(_("Done"), lambda: True)

        def rebuild() -> None:
            content_stack.remove_all()
            preference_pane_delegates.clear()
            preference_pane_delegate_id = preference_pane_delegate_id_ref[0]
            items = list()
            selected_index = 0
            delegates = PreferencesManager().preference_pane_delegates
            if not delegates:
                delegates.append(EmptyPreferencePanel())
            for index, preference_pane_delegate in enumerate(delegates):
                preference_pane_delegates.append(preference_pane_delegate)
                content_column_widget = ui.create_column_widget()
                content_column_widget.add_spacing(12)
                content_column_widget.add(preference_pane_delegate.build(ui, event_loop=self.event_loop))
                content_column_widget.add_spacing(12)
                content_row_widget = ui.create_row_widget()
                content_row_widget.add_spacing(12)
                content_row_widget.add(content_column_widget)
                content_row_widget.add_spacing(12)
                content_stack.add(content_row_widget)
                items.append(preference_pane_delegate.label)
                if preference_pane_delegate.identifier == preference_pane_delegate_id:
                    selected_index = index
            change_selection({selected_index})
            selector_list_widget.items = items

        self.__preference_pane_delegates_changed_listener = PreferencesManager().preference_pane_delegates_changed_event.listen(rebuild)

        rebuild()
    def __init__(self, ui: UserInterface.UserInterface,
                 instrument: InstrumentDevice.Instrument) -> None:
        column_widget = ui.create_column_widget(properties={
            "margin": 6,
            "spacing": 2
        })
        super().__init__(column_widget)

        sample_combo_box = ui.create_combo_box_widget(instrument.sample_titles)
        sample_combo_box.current_index = instrument.sample_index
        sample_combo_box.bind_current_index(
            Binding.PropertyBinding(instrument, "sample_index"))

        voltage_field = ui.create_line_edit_widget()
        voltage_field.bind_text(
            Binding.PropertyBinding(
                instrument,
                "voltage",
                converter=Converter.PhysicalValueToStringConverter(
                    units="keV", multiplier=1E-3)))

        beam_current_field = ui.create_line_edit_widget()
        beam_current_field.bind_text(
            ControlBinding(instrument,
                           "BeamCurrent",
                           converter=Converter.PhysicalValueToStringConverter(
                               units="pA", multiplier=1E12)))

        stage_position_widget = PositionWidget(ui, _("Stage"), instrument,
                                               "stage_position_m")

        beam_shift_widget = PositionWidget(ui, _("Beam"), instrument,
                                           "beam_shift_m")

        defocus_field = ui.create_line_edit_widget()
        defocus_field.bind_text(
            ControlBinding(instrument,
                           "C10",
                           converter=Converter.PhysicalValueToStringConverter(
                               units="nm", multiplier=1E9)))

        c12_widget = PositionWidget(ui, _("C12"), instrument, "C12")

        c21_widget = PositionWidget(ui, _("C21"), instrument, "C21")

        c23_widget = PositionWidget(ui, _("C23"), instrument, "C23")

        c3_field = ui.create_line_edit_widget()
        c3_field.bind_text(
            ControlBinding(instrument,
                           "C30",
                           converter=Converter.PhysicalValueToStringConverter(
                               units="nm", multiplier=1E9)))

        c32_widget = PositionWidget(ui, _("C32"), instrument, "C32")

        c34_widget = PositionWidget(ui, _("C34"), instrument, "C34")

        blanked_checkbox = ui.create_check_box_widget(_("Beam Blanked"))
        blanked_checkbox.bind_checked(
            Binding.PropertyBinding(instrument, "is_blanked"))

        slit_in_checkbox = ui.create_check_box_widget(_("Slit In"))
        slit_in_checkbox.bind_checked(
            Binding.PropertyBinding(instrument, "is_slit_in"))

        voa_in_checkbox = ui.create_check_box_widget(_("VOA In"))
        voa_in_checkbox.bind_checked(ControlBinding(instrument, "S_VOA"))

        convergenve_angle_field = ui.create_line_edit_widget()
        convergenve_angle_field.bind_text(
            ControlBinding(instrument,
                           "ConvergenceAngle",
                           converter=Converter.PhysicalValueToStringConverter(
                               units="mrad", multiplier=1E3)))

        c_aperture_widget = PositionWidget(ui,
                                           _("CAperture"),
                                           instrument,
                                           "CAperture",
                                           unit="mrad",
                                           multiplier=1E3)
        aperture_round_widget = PositionWidget(ui,
                                               _("ApertureRound"),
                                               instrument,
                                               "ApertureRound",
                                               unit="",
                                               multiplier=1)

        energy_offset_field = ui.create_line_edit_widget()
        energy_offset_field.bind_text(
            Binding.PropertyBinding(
                instrument,
                "energy_offset_eV",
                converter=Converter.FloatToStringConverter()))

        energy_dispersion_field = ui.create_line_edit_widget()
        energy_dispersion_field.bind_text(
            Binding.PropertyBinding(
                instrument,
                "energy_per_channel_eV",
                converter=Converter.FloatToStringConverter()))

        beam_row = ui.create_row_widget()
        beam_row.add_spacing(8)
        beam_row.add(blanked_checkbox)
        beam_row.add_spacing(8)
        beam_row.add(voa_in_checkbox)
        beam_row.add_stretch()

        eels_row = ui.create_row_widget()
        eels_row.add_spacing(8)
        eels_row.add(slit_in_checkbox)
        eels_row.add_spacing(8)
        eels_row.add(ui.create_label_widget("+eV"))
        eels_row.add_spacing(4)
        eels_row.add(energy_offset_field)
        eels_row.add_spacing(8)
        eels_row.add(ui.create_label_widget("eV/ch"))
        eels_row.add_spacing(4)
        eels_row.add(energy_dispersion_field)
        eels_row.add_stretch()

        defocus_row = ui.create_row_widget()
        defocus_row.add_spacing(8)
        defocus_row.add_spacing(8)
        defocus_row.add(ui.create_label_widget("Defocus"))
        defocus_row.add(defocus_field)
        defocus_row.add_stretch()

        c3_row = ui.create_row_widget()
        c3_row.add_spacing(8)
        c3_row.add_spacing(8)
        c3_row.add(ui.create_label_widget("Spherical Aberration"))
        c3_row.add(c3_field)
        c3_row.add_stretch()

        sample_row = ui.create_row_widget()
        sample_row.add_spacing(8)
        sample_row.add_spacing(8)
        sample_row.add(sample_combo_box)
        sample_row.add_stretch()

        voltage_row = ui.create_row_widget()
        voltage_row.add_spacing(8)
        voltage_row.add_spacing(8)
        voltage_row.add(ui.create_label_widget("Voltage"))
        voltage_row.add(voltage_field)
        voltage_row.add_stretch()

        beam_current_row = ui.create_row_widget()
        beam_current_row.add_spacing(8)
        beam_current_row.add_spacing(8)
        beam_current_row.add(ui.create_label_widget("Beam Current"))
        beam_current_row.add(beam_current_field)
        beam_current_row.add_stretch()

        convergence_angle_row = ui.create_row_widget()
        convergence_angle_row.add_spacing(8)
        convergence_angle_row.add_spacing(8)
        convergence_angle_row.add(ui.create_label_widget("Convergence Angle"))
        convergence_angle_row.add(convergenve_angle_field)
        convergence_angle_row.add_stretch()

        column_widget.add(sample_row)
        column_widget.add(voltage_row)
        column_widget.add(beam_current_row)
        column_widget.add(stage_position_widget)
        column_widget.add(beam_shift_widget)
        column_widget.add(defocus_row)
        column_widget.add(c12_widget)
        column_widget.add(c21_widget)
        column_widget.add(c23_widget)
        column_widget.add(c3_row)
        column_widget.add(c32_widget)
        column_widget.add(c34_widget)
        column_widget.add(beam_row)
        column_widget.add(convergence_angle_row)
        column_widget.add(c_aperture_widget)
        column_widget.add(aperture_round_widget)
        column_widget.add(eels_row)
        column_widget.add_stretch()
Esempio n. 23
0
    def __init__(
        self,
        ui: UserInterface.UserInterface,
        locals: typing.Optional[typing.Dict[str, typing.Any]] = None,
        properties: typing.Optional[Persistence.PersistentDictType] = None
    ) -> None:
        content_widget = ui.create_column_widget()
        super().__init__(content_widget)

        self.prompt = ">>> "
        self.continuation_prompt = "... "

        self.__cursor_position: typing.Optional[
            UserInterface.CursorPosition] = None

        self.__text_edit_widget = ui.create_text_edit_widget(properties)
        self.__text_edit_widget.set_text_color("white")
        self.__text_edit_widget.set_text_background_color("black")
        self.__text_edit_widget.set_text_font(
            Panel.Panel.get_monospace_text_font())
        self.__text_edit_widget.set_line_height_proportional(
            Panel.Panel.get_monospace_proportional_line_height())
        self.__text_edit_widget.word_wrap_mode = "anywhere"
        self.__text_edit_widget.on_cursor_position_changed = self.__cursor_position_changed
        self.__text_edit_widget.on_return_pressed = self.__return_pressed
        self.__text_edit_widget.on_key_pressed = self.__key_pressed
        self.__text_edit_widget.on_insert_mime_data = self.__insert_mime_data

        class StdoutCatcher:
            def __init__(
                    self,
                    text_edit_widget: UserInterface.TextEditWidget) -> None:
                self.__text_edit_widget = text_edit_widget

            def write(self, stuff: str) -> int:
                if self.__text_edit_widget:
                    stripped_stuff = str(stuff).rstrip()
                    self.__text_edit_widget.append_text(stripped_stuff)
                    return len(stripped_stuff)
                return 0

            def flush(self) -> None:
                pass

        stdout = StdoutCatcher(self.__text_edit_widget)

        locals = locals if locals is not None else dict()
        locals.update({
            '__name__': None,
            '__console__': None,
            '__doc__': None,
            '_stdout': stdout
        })

        self.__state_controller = ConsoleWidgetStateController(locals)

        self.__text_edit_widget.append_text(self.prompt)
        self.__text_edit_widget.move_cursor_position("end")
        self.__last_cursor_position: typing.Optional[
            UserInterface.CursorPosition] = copy.deepcopy(
                self.__cursor_position)

        content_widget.add(self.__text_edit_widget)
Esempio n. 24
0
    def __init__(
            self, ui: UserInterface.UserInterface,
            document_controller: DocumentController.DocumentController
    ) -> None:
        content_widget = ui.create_column_widget()
        super().__init__(content_widget)

        document_model = document_controller.document_model

        all_items_controller = CollectionDisplayItemCounter(
            _("All"), None, "all", document_controller)
        persistent_items_controller = CollectionDisplayItemCounter(
            _("Persistent"), None, "persistent", document_controller)
        live_items_controller = CollectionDisplayItemCounter(
            _("Live"), None, "temporary", document_controller)
        latest_items_controller = CollectionDisplayItemCounter(
            _("Latest Session"), None, "latest-session", document_controller)

        self.__item_controllers = [
            all_items_controller, persistent_items_controller,
            live_items_controller, latest_items_controller
        ]

        self.__data_group_controllers: typing.List[
            CollectionDisplayItemCounter] = list()

        collection_selection = Selection.IndexedSelection(
            Selection.Style.single_or_none)

        collections_list_widget = Widgets.ListWidget(
            ui,
            CollectionListCanvasItemDelegate(collection_selection),
            selection=collection_selection,
            v_scroll_enabled=False,
            v_auto_resize=True)
        collections_list_widget.wants_drag_events = True

        def filter_changed(data_group: typing.Optional[DataGroup.DataGroup],
                           filter_id: typing.Optional[str]) -> None:
            if data_group:
                for index, controller in enumerate(
                        collections_list_widget.items):
                    if data_group == controller.data_group:
                        collection_selection.set(index)
                        break
            else:
                if filter_id == "latest-session":
                    collection_selection.set(3)
                elif filter_id == "temporary":
                    collection_selection.set(2)
                elif filter_id == "persistent":
                    collection_selection.set(1)
                else:
                    collection_selection.set(0)

        self.__filter_changed_event_listener = document_controller.filter_changed_event.listen(
            filter_changed)

        def collections_changed(t: str) -> None:
            collections_list_widget.items = [
                all_items_controller,
                persistent_items_controller,
                live_items_controller,
                latest_items_controller,
            ] + self.__data_group_controllers

        all_items_controller.on_title_changed = collections_changed
        persistent_items_controller.on_title_changed = collections_changed
        live_items_controller.on_title_changed = collections_changed
        latest_items_controller.on_title_changed = collections_changed

        def document_model_item_inserted(key: str, value: typing.Any,
                                         before_index: int) -> None:
            if key == "data_groups":
                data_group = value
                controller = CollectionDisplayItemCounter(
                    data_group.title, data_group, None, document_controller)
                self.__data_group_controllers.insert(before_index, controller)
                controller.on_title_changed = collections_changed
                collections_changed(str())

        def document_model_item_removed(key: str, value: typing.Any,
                                        index: int) -> None:
            if key == "data_groups":
                controller = self.__data_group_controllers.pop(index)
                controller.close()
                collections_changed(str())

        self.__document_model_item_inserted_listener = document_model.item_inserted_event.listen(
            document_model_item_inserted)
        self.__document_model_item_removed_listener = document_model.item_removed_event.listen(
            document_model_item_removed)

        data_group, filter_id = document_controller.get_data_group_and_filter_id(
        )
        filter_changed(data_group, filter_id)

        for index, data_group in enumerate(document_model.data_groups):
            document_model_item_inserted("data_groups", data_group, index)

        collections_changed(str())

        def collections_selection_changed(
                indexes: typing.AbstractSet[int]) -> None:
            if len(indexes) == 0:
                controller = collections_list_widget.items[0]
                document_controller.set_filter(controller.filter_id)
            elif len(indexes) == 1:
                controller = collections_list_widget.items[list(indexes)[0]]
                if controller.is_smart_collection:
                    document_controller.set_filter(controller.filter_id)
                    document_controller.set_data_group(None)
                else:
                    document_controller.set_filter(None)
                    document_controller.set_data_group(controller.data_group)

        collections_list_widget.on_selection_changed = collections_selection_changed

        collections_column = ui.create_column_widget()
        collections_column.add(collections_list_widget)

        collections_section = Widgets.SectionWidget(ui, _("Collections"),
                                                    collections_column)
        collections_section.expanded = True

        content_widget.add(collections_section)
        content_widget.add_stretch()

        # for testing
        self._collection_selection = collection_selection
Esempio n. 25
0
    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)