Esempio n. 1
0
 def __init__(self,
              document_controller: DocumentController.DocumentController,
              panel_id: str,
              properties: typing.Mapping[str, typing.Any]) -> None:
     super().__init__(document_controller, panel_id, _("Activity"))
     activity_controller = ActivityController(document_controller)
     self.widget = Declarative.DeclarativeWidget(
         document_controller.ui, document_controller.event_loop,
         activity_controller)
Esempio n. 2
0
    def __init__(self, document_controller: DocumentController.DocumentController, panel_id: str, properties: Persistence.PersistentDictType) -> None:
        super().__init__(document_controller, panel_id, _("Toolbar"))

        self.__component_registered_listener = Registry.listen_component_registered_event(self.__component_registered)

        self.widget = self.ui.create_column_widget()

        # note: "maximum" here means the size hint is maximum and the widget can be smaller. Qt layout is atrocious.
        self.__toolbar_widget_row = self.ui.create_row_widget(properties={"size-policy-horizontal": "maximum"})

        toolbar_row_widget = self.ui.create_row_widget()
        toolbar_row_widget.add(self.__toolbar_widget_row)
        toolbar_row_widget.add_stretch()

        self.widget.add(toolbar_row_widget)

        # make a map from widget_id to widget factory.
        widget_factories = dict()
        for component in Registry.get_components_by_type("toolbar-widget"):
            widget_factories[component.toolbar_widget_id] = component

        # define the order of widgets.
        # this part is hard coded for now; needs some work to make it dynamically order widgets as they become
        # available from packages.
        widget_id_list = [
            "nion.swift.toolbar-widget.tool-mode",
            "nion.swift.toolbar-widget.raster-zoom",
            "nion.swift.toolbar-widget.workspace"
        ]

        # add the widgets.
        for widget_id in widget_id_list:
            widget_factory = widget_factories[widget_id]
            widget_handler = widget_factory(document_controller=self.document_controller)
            widget = Declarative.DeclarativeWidget(self.ui, self.document_controller.event_loop, widget_handler)
            widget_section = self.ui.create_row_widget()
            widget_section.add(widget)
            section_bar = self.ui.create_canvas_widget(properties={"width": 9, "size_policy_vertical": "expanding"})

            def draw(drawing_context: DrawingContext.DrawingContext, canvas_size: Geometry.IntSize, *args: typing.Any, **kwargs: typing.Any) -> None:
                with drawing_context.saver():
                    drawing_context.rect(0, 0, canvas_size.width, canvas_size.height)
                    drawing_context.fill_style = "#DDD"
                    drawing_context.stroke_style = "#AAA"
                    drawing_context.fill()
                    drawing_context.stroke()

            section_bar.canvas_item.add_canvas_item(CanvasItem.DrawCanvasItem(draw))
            self.__toolbar_widget_row.add(section_bar)
            self.__toolbar_widget_row.add_spacing(8)
            self.__toolbar_widget_row.add(widget_section)
            self.__toolbar_widget_row.add_spacing(8)

        end_divider = self.ui.create_canvas_widget(properties={"width": 1, "size_policy_vertical": "expanding"})
        end_divider.canvas_item.add_canvas_item(CanvasItem.DividerCanvasItem(color="#888"))
        self.__toolbar_widget_row.add(end_divider)
Esempio n. 3
0
 def __init__(self,
              document_controller: DocumentController.DocumentController,
              panel_id: str, properties: dict):
     super().__init__(document_controller, panel_id, 'libertem-panel')
     panel_type = properties.get('panel_type')
     for component in Registry.get_components_by_type('libertem-panel'):
         if component.panel_type == panel_type:
             ui_handler = component.get_ui_handler(
                 api_broker=PlugInManager.APIBroker(),
                 event_loop=document_controller.event_loop)
             self.widget = Declarative.DeclarativeWidget(
                 document_controller.ui, document_controller.event_loop,
                 ui_handler)
Esempio n. 4
0
    def build(self, ui: UserInterface.UserInterface, event_loop: asyncio.AbstractEventLoop,
              **kwargs: typing.Any) -> Declarative.DeclarativeWidget:
        u = Declarative.DeclarativeUI()

        class Handler(Declarative.HandlerLike):
            def __init__(self, ui_view: Declarative.UIDescription) -> None:
                self.ui_view = ui_view

            def close(self) -> None:
                pass

        no_content_row = u.create_row(u.create_stretch(), u.create_label(text=_("No Preferences Available")),
                                      u.create_stretch())
        content = u.create_column(no_content_row)
        return Declarative.DeclarativeWidget(ui, event_loop, Handler(content))
Esempio n. 5
0
    def build(self, ui, event_loop=None, **kwargs):
        u = Declarative.DeclarativeUI()

        video_device_factories = list(Registry.get_components_by_type("video_device_factory"))

        class Handler:
            def __init__(self, ui_view, video_sources):
                self.ui_view = ui_view
                self.video_sources = video_sources
                self.video_source_type_index = Model.PropertyModel(0)

            def create_new_video_device(self, widget):
                video_base.video_configuration.create_hardware_source(video_device_factories[self.video_source_type_index.value])

            def create_handler(self, component_id: str, container=None, item=None, **kwargs):

                class SectionHandler:

                    def __init__(self, container, hardware_source):
                        self.container = container
                        self.hardware_source = hardware_source
                        self.settings = video_base.video_configuration.get_settings_model(hardware_source)
                        self.settings_original = copy.deepcopy(self.settings)
                        self.needs_saving_model = Model.PropertyModel(False)
                        self.property_changed_event = Event.Event()
                        self.apply_button = None
                        self.revert_button = None

                        def settings_changed(property_name):
                            self.needs_saving_model.value = True

                        self.__settings_changed_event_listener = self.settings.property_changed_event.listen(settings_changed)

                        def needs_saving_model_changed(property_name):
                            if self.apply_button:
                                self.apply_button.enabled = self.needs_saving_model.value
                            if self.revert_button:
                                self.revert_button.enabled = self.needs_saving_model.value

                        self.__needs_saving_changed_event_listener = self.needs_saving_model.property_changed_event.listen(needs_saving_model_changed)

                    def close(self):
                        self.__settings_changed_event_listener.close()
                        self.__settings_changed_event_listener = None

                    def init_handler(self):
                        self.apply_button.enabled = self.needs_saving_model.value
                        self.revert_button.enabled = self.needs_saving_model.value

                    def create_handler(self, component_id: str, container=None, item=None, **kwargs):
                        if component_id == "edit_group":
                            if self.__video_device_factory:
                                return self.__video_device_factory.create_editor_handler(self.settings)
                            else:
                                return self

                    @property
                    def resources(self):
                        if self.__video_device_factory:
                            content = self.__video_device_factory.get_editor_description()
                        else:
                            content = u.create_label(text=_("Not Available"))

                        component = u.define_component(content=content, component_id="edit_group")

                        return {"edit_group": component}

                    @property
                    def __video_device_factory(self):
                        for video_device_factory in video_device_factories:
                            if video_device_factory.factory_id == self.settings.driver:
                                return video_device_factory
                        return None

                    @property
                    def driver_display_name(self) -> str:
                        video_device_factory = self.__video_device_factory
                        return video_device_factory.display_name if video_device_factory else _("Unknown")

                    def apply(self, widget):
                        video_base.video_configuration.set_settings_model(self.hardware_source, self.settings)
                        self.needs_saving_model.value = False

                    def revert(self, widget):
                        self.settings.copy_from(self.settings_original)
                        self.needs_saving_model.value = False

                    def remove(self, widget):
                        video_base.video_configuration.remove_hardware_source(self.hardware_source)

                if component_id == "section":
                    return SectionHandler(container, item)

            @property
            def resources(self):
                u = Declarative.DeclarativeUI()

                driver_display_name_label = u.create_label(text="@binding(driver_display_name)")

                device_id_field = u.create_line_edit(text="@binding(settings.device_id)", width=180)

                display_name_field = u.create_line_edit(text="@binding(settings.name)", width=240)

                edit_group_content = u.create_component_instance("edit_group")

                edit_group = u.create_group(edit_group_content, margin=8)

                edit_row = u.create_row(edit_group, u.create_stretch())

                apply_button = u.create_push_button(name="apply_button", text=_("Apply"), on_clicked="apply")
                revert_button = u.create_push_button(name="revert_button", text=_("Revert"), on_clicked="revert")
                remove_button = u.create_push_button(text=_("Remove"), on_clicked="remove")
                remove_row = u.create_row(apply_button, revert_button, remove_button, u.create_stretch())

                label_column = u.create_column(u.create_label(text=_("Driver:")), u.create_label(text=_("Device ID:")), u.create_label(text=_("Display Name:")), spacing=4)
                field_column = u.create_column(driver_display_name_label, device_id_field, display_name_field, spacing=4)

                content_row = u.create_row(label_column, field_column, u.create_stretch(), spacing=12)

                content = u.create_column(content_row, edit_row, remove_row, spacing=8)

                component = u.define_component(content=content, component_id="section")

                return {"section": component}

        sources_column = u.create_column(items="video_sources.items", item_component_id="section", spacing=8)

        sources_content = u.create_scroll_area(u.create_column(sources_column, u.create_stretch()))

        video_source_types = [video_device_factory.display_name for video_device_factory in video_device_factories]

        video_source_type_combo = u.create_combo_box(items=video_source_types, current_index="@binding(video_source_type_index.value)")

        button_row = u.create_row(u.create_stretch(), video_source_type_combo, u.create_push_button(text=_("New"), on_clicked="create_new_video_device"), spacing=8)

        content = u.create_column(sources_content, button_row)

        return Declarative.DeclarativeWidget(ui, event_loop, Handler(content, self.__video_configuration.video_sources))
Esempio n. 6
0
 def __component_registered(self, component: typing.Callable[..., Declarative.HandlerLike], component_types: typing.Set[str]) -> None:
     if "toolbar-widget" in component_types:
         self.__toolbar_widget_row.add_spacing(12)
         self.__toolbar_widget_row.add(Declarative.DeclarativeWidget(self.ui, self.document_controller.event_loop, component(document_controller=self.document_controller)))
    def build(self, ui: UserInterface.UserInterface,
              event_loop: asyncio.AbstractEventLoop,
              **kwargs: typing.Any) -> Declarative.DeclarativeWidget:
        u = Declarative.DeclarativeUI()

        video_device_factories: typing.List[
            video_base.VideoDeviceFactoryLike] = list(
                Registry.get_components_by_type("video_device_factory"))

        class Handler(Declarative.HandlerLike):
            def __init__(
                self, ui_view: Declarative.UIDescription,
                video_sources: ListModel.ListModel[
                    video_base.VideoHardwareSource]
            ) -> None:
                self.ui_view = ui_view
                self.video_sources = video_sources
                self.video_source_type_index = Model.PropertyModel(0)

            def close(self) -> None:
                pass

            def create_new_video_device(self,
                                        widget: UserInterface.Widget) -> None:
                video_base.video_configuration.create_hardware_source(
                    video_device_factories[self.video_source_type_index.value
                                           or 0])

            def create_handler(
                self,
                component_id: str,
                container: typing.Any = None,
                item: typing.Any = None,
                **kwargs: typing.Any
            ) -> typing.Optional[Declarative.HandlerLike]:
                class SectionHandler:
                    def __init__(
                        self, container: typing.Any,
                        hardware_source: video_base.VideoHardwareSource
                    ) -> None:
                        self.container = container
                        self.hardware_source = hardware_source
                        self.settings = video_base.video_configuration.get_settings_model(
                            hardware_source)
                        self.settings_original = copy.deepcopy(self.settings)
                        self.needs_saving_model = Model.PropertyModel(False)
                        self.property_changed_event = Event.Event()
                        # these will be assigned by declarative setup
                        self.apply_button: typing.Optional[
                            UserInterface.PushButtonWidget] = None
                        self.revert_button: typing.Optional[
                            UserInterface.PushButtonWidget] = None

                        def settings_changed(property_name: str) -> None:
                            self.needs_saving_model.value = True

                        self.__settings_changed_event_listener = self.settings.property_changed_event.listen(
                            settings_changed) if self.settings else None

                        def needs_saving_model_changed(
                                property_name: str) -> None:
                            if self.apply_button:
                                self.apply_button.enabled = self.needs_saving_model.value or False
                            if self.revert_button:
                                self.revert_button.enabled = self.needs_saving_model.value or False

                        self.__needs_saving_changed_event_listener = self.needs_saving_model.property_changed_event.listen(
                            needs_saving_model_changed)

                    def close(self) -> None:
                        if self.__settings_changed_event_listener:
                            self.__settings_changed_event_listener.close()
                            self.__settings_changed_event_listener = None

                    def init_handler(self) -> None:
                        if self.apply_button:
                            self.apply_button.enabled = self.needs_saving_model.value or False
                        if self.revert_button:
                            self.revert_button.enabled = self.needs_saving_model.value or False

                    def create_handler(
                        self,
                        component_id: str,
                        container: typing.Any = None,
                        item: typing.Any = None,
                        **kwargs: typing.Any
                    ) -> typing.Optional[Declarative.HandlerLike]:
                        if component_id == "edit_group":
                            if self.__video_device_factory:
                                return typing.cast(
                                    Declarative.HandlerLike,
                                    self.__video_device_factory.
                                    create_editor_handler(self.settings))
                            else:
                                return self
                        raise ValueError("Unable to create component.")

                    @property
                    def resources(self) -> typing.Mapping[str, typing.Any]:
                        if self.__video_device_factory:
                            content = self.__video_device_factory.get_editor_description(
                            )
                        else:
                            content = u.create_label(text=_("Not Available"))

                        component = u.define_component(
                            content=content, component_id="edit_group")

                        return {"edit_group": component}

                    @property
                    def __video_device_factory(
                        self
                    ) -> typing.Optional[video_base.VideoDeviceFactoryLike]:
                        for video_device_factory in video_device_factories:
                            if video_device_factory.factory_id == getattr(
                                    self.settings, "driver", None):
                                return video_device_factory
                        return None

                    @property
                    def driver_display_name(self) -> str:
                        video_device_factory = self.__video_device_factory
                        return video_device_factory.display_name if video_device_factory else _(
                            "Unknown")

                    def apply(self, widget: UserInterface.Widget) -> None:
                        settings = self.settings
                        if settings:
                            video_base.video_configuration.set_settings_model(
                                self.hardware_source, settings)
                            self.needs_saving_model.value = False

                    def revert(self, widget: UserInterface.Widget) -> None:
                        if self.settings:
                            self.settings.copy_from(self.settings_original)
                            self.needs_saving_model.value = False

                    def remove(self, widget: UserInterface.Widget) -> None:
                        video_base.video_configuration.remove_hardware_source(
                            self.hardware_source)

                if component_id == "section":
                    return SectionHandler(container, item)

                raise ValueError("Unable to create component.")

            @property
            def resources(self) -> typing.Mapping[str, typing.Any]:
                u = Declarative.DeclarativeUI()

                driver_display_name_label = u.create_label(
                    text="@binding(driver_display_name)")

                device_id_field = u.create_line_edit(
                    text="@binding(settings.device_id)", width=180)

                display_name_field = u.create_line_edit(
                    text="@binding(settings.name)", width=240)

                edit_group_content = u.create_component_instance("edit_group")

                edit_group = u.create_group(edit_group_content, margin=8)

                edit_row = u.create_row(edit_group, u.create_stretch())

                apply_button = u.create_push_button(name="apply_button",
                                                    text=_("Apply"),
                                                    on_clicked="apply")
                revert_button = u.create_push_button(name="revert_button",
                                                     text=_("Revert"),
                                                     on_clicked="revert")
                remove_button = u.create_push_button(text=_("Remove"),
                                                     on_clicked="remove")
                remove_row = u.create_row(apply_button, revert_button,
                                          remove_button, u.create_stretch())

                label_column = u.create_column(
                    u.create_label(text=_("Driver:")),
                    u.create_label(text=_("Device ID:")),
                    u.create_label(text=_("Display Name:")),
                    spacing=4)
                field_column = u.create_column(driver_display_name_label,
                                               device_id_field,
                                               display_name_field,
                                               spacing=4)

                content_row = u.create_row(label_column,
                                           field_column,
                                           u.create_stretch(),
                                           spacing=12)

                content = u.create_column(content_row,
                                          edit_row,
                                          remove_row,
                                          spacing=8)

                component = u.define_component(content=content,
                                               component_id="section")

                return {"section": component}

        sources_column = u.create_column(items="video_sources.items",
                                         item_component_id="section",
                                         spacing=8)

        sources_content = u.create_scroll_area(
            u.create_column(sources_column, u.create_stretch()))

        video_source_types = [
            video_device_factory.display_name
            for video_device_factory in video_device_factories
        ]

        video_source_type_combo = u.create_combo_box(
            items=video_source_types,
            current_index="@binding(video_source_type_index.value)")

        button_row = u.create_row(u.create_stretch(),
                                  video_source_type_combo,
                                  u.create_push_button(
                                      text=_("New"),
                                      on_clicked="create_new_video_device"),
                                  spacing=8)

        content = u.create_column(sources_content, button_row)

        return Declarative.DeclarativeWidget(
            ui, event_loop,
            Handler(content, self.__video_configuration.video_sources))