Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 def test_add_item_to_string_list_widget_causes_container_to_relayout(
         self) -> None:
     # ugly type casting
     from nion.ui import Widgets
     ui = TestUI.UserInterface()
     widget = Widgets.StringListWidget(ui)
     with contextlib.closing(widget):
         canvas_item = typing.cast(
             CanvasItem.CanvasItemComposition,
             typing.cast(
                 UserInterface.CanvasWidget,
                 typing.cast(
                     UserInterface.BoxWidget,
                     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 = typing.cast(
             CanvasItem.ScrollAreaCanvasItem,
             typing.cast(CanvasItem.CanvasItemComposition,
                         canvas_item.canvas_items[0]).canvas_items[0])
         canvas_item.layout_immediate(
             Geometry.IntSize(width=300, height=200))
         # check assumptions
         scroll_canvas_rect = scroll_area_canvas_item.canvas_rect or Geometry.IntRect.empty_rect(
         )
         scroll_content = scroll_area_canvas_item.content
         assert scroll_content
         self.assertEqual(scroll_canvas_rect.height, 200)
         scroll_content_rect = scroll_content.canvas_rect or Geometry.IntRect.empty_rect(
         )
         self.assertEqual(scroll_content_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)
         scroll_canvas_rect = scroll_area_canvas_item.canvas_rect or Geometry.IntRect.empty_rect(
         )
         scroll_content = scroll_area_canvas_item.content
         assert scroll_content
         scroll_content_rect = scroll_content.canvas_rect or Geometry.IntRect.empty_rect(
         )
         self.assertEqual(scroll_canvas_rect.height, 200)
         self.assertEqual(scroll_content_rect.height, 20)
Ejemplo n.º 3
0
    def __init__(self, ui, app):
        super().__init__(ui, _("Preferences"), app=app)

        self.ui = ui
        self.document_controller = self

        self._create_menus()

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

        preference_pane_delegates = list()
        preference_pane_delegate_id_ref = [None]

        content_stack = ui.create_stack_widget()

        def change_selection(indexes):
            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():
            content_stack.remove_all()
            preference_pane_delegates.clear()
            preference_pane_delegate_id = preference_pane_delegate_id_ref[0]
            items = list()
            selected_index = 0
            for index, preference_pane_delegate in enumerate(
                    PreferencesManager().preference_pane_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()
Ejemplo n.º 4
0
            def __init__(self, ui, app):
                super().__init__(ui, _("Choose Library"))

                current_item_ref = [None]

                def handle_choose():
                    current_item = current_item_ref[0]
                    if current_item:
                        app.switch_library(current_item)
                        return True
                    return False

                def handle_new():
                    workspace_dir = pose_open_library_dialog_fn()
                    if workspace_dir:
                        items.insert(0, (workspace_dir, datetime.datetime.now()))
                        list_widget.items = items
                        list_widget.set_selected_index(0)
                        app.switch_library(workspace_dir)
                        return True
                    return False

                self.add_button(_("New..."), handle_new)
                self.add_button(_("Other..."), handle_new)
                self.add_button(_("Cancel"), lambda: True)
                self.add_button(_("Choose"), handle_choose)

                path_label = ui.create_label_widget()

                prompt_row = ui.create_row_widget()
                prompt_row.add_spacing(13)
                prompt_row.add(ui.create_label_widget(_("Which library do you want Nion Swift to use?"), properties={"stylesheet": "font-weight: bold"}))
                prompt_row.add_spacing(13)
                prompt_row.add_stretch()

                explanation1_row = ui.create_row_widget()
                explanation1_row.add_spacing(13)
                explanation1_row.add(ui.create_label_widget(_("You can select a library from the list, find another library, or create a new library.")))
                explanation1_row.add_spacing(13)
                explanation1_row.add_stretch()

                explanation2_row = ui.create_row_widget()
                explanation2_row.add_spacing(13)
                explanation2_row.add(ui.create_label_widget(_("The same library will be used the next time you open Nion Swift.")))
                explanation2_row.add_spacing(13)
                explanation2_row.add_stretch()

                def selection_changed(indexes):
                    if len(indexes) == 1:
                        item = items[list(indexes)[0]]
                        current_item_ref[0] = os.path.dirname(item[0])
                        path_label.text = os.path.dirname(item[0])
                    else:
                        current_item_ref[0] = None
                        path_label.text = None

                def stringify_item(item):
                    date_utc = item[1]
                    tz_minutes = Utility.local_utcoffset_minutes(date_utc)
                    date_local = date_utc + datetime.timedelta(minutes=tz_minutes)
                    return str(os.path.basename(os.path.dirname(item[0]))) + " (" + date_local.strftime("%c") + ")"

                def item_selected(index):
                    item = items[index]
                    current_item_ref[0] = os.path.dirname(item[0])
                    path_label.text = os.path.dirname(item[0])
                    handle_choose()
                    self.request_close()

                list_widget = Widgets.StringListWidget(ui, items=items, selection_style=Selection.Style.single_or_none, item_getter=stringify_item, border_color="#888", properties={"min-height": 200, "min-width": 560})
                list_widget.on_selection_changed = selection_changed
                list_widget.on_item_selected = item_selected
                list_widget.on_cancel = self.request_close
                if len(items) > 0:
                    list_widget.set_selected_index(0)

                items_row = ui.create_row_widget()
                items_row.add_spacing(13)
                items_row.add(list_widget)
                items_row.add_spacing(13)
                items_row.add_stretch()

                path_row = ui.create_row_widget()
                path_row.add_spacing(13)
                path_row.add(path_label)
                path_row.add_spacing(13)
                path_row.add_stretch()

                column = ui.create_column_widget()
                column.add_spacing(18)
                column.add(prompt_row)
                column.add_spacing(6)
                column.add(explanation1_row)
                column.add(explanation2_row)
                column.add_spacing(12)
                column.add(items_row)
                column.add_spacing(6)
                column.add(path_row)
                column.add_spacing(6)
                column.add_stretch()
                self.content.add(column)

                self.__list_widget = list_widget
Ejemplo n.º 5
0
    def __init__(self, document_controller):
        ui = document_controller.ui
        super().__init__(ui,
                         _("Interactive Dialog"),
                         document_controller.app,
                         persistent_id="ScriptsDialog")

        self.ui = ui
        self.document_controller = document_controller

        self._create_menus()

        self.__cancelled = False

        self.__thread = None

        properties = dict()
        properties["min-height"] = 180
        properties["min-width"] = 540
        properties[
            "stylesheet"] = "background: white; font-family: Monaco, Courier, monospace"

        self.__output_widget = self.ui.create_text_edit_widget(properties)

        self.__message_column = ui.create_column_widget()

        self.__message_column.add(self.__make_cancel_row())

        items = self.ui.get_persistent_object("interactive_scripts_0", list())

        def selected_changed(indexes: AbstractSet[int]) -> None:
            run_button_widget.enabled = len(indexes) == 1

        def add_clicked() -> None:
            add_dir = self.ui.get_persistent_string("import_directory", "")
            file_paths, filter_str, directory = self.get_file_paths_dialog(
                _("Add Scripts"), add_dir, "Python Files (*.py)",
                "Python Files (*.py)")
            self.ui.set_persistent_string("import_directory", directory)
            items.extend(file_paths)
            items.sort()
            list_widget.items = items
            self.ui.set_persistent_object("interactive_scripts_0", items)

        def remove_clicked() -> None:
            indexes = list(list_widget.selected_items)
            for index in sorted(indexes, reverse=True):
                del items[index]
            list_widget.items = items
            self.ui.set_persistent_object("interactive_scripts_0", items)

        def run_clicked() -> None:
            indexes = list_widget.selected_items
            if len(indexes) == 1:
                script_path = items[list(indexes)[0]]
                self.run_script(script_path)

        def item_selected(index: int) -> bool:
            run_clicked()
            return True

        list_widget = Widgets.StringListWidget(
            ui,
            items=items,
            selection_style=Selection.Style.single_or_none,
            border_color="#888",
            properties={
                "min-height": 200,
                "min-width": 560
            })
        list_widget.on_selection_changed = selected_changed
        list_widget.on_item_selected = item_selected

        add_button_widget = ui.create_push_button_widget(_("Add..."))
        add_button_widget.on_clicked = add_clicked

        remove_button_widget = ui.create_push_button_widget(_("Remove"))
        remove_button_widget.on_clicked = remove_clicked

        run_button_widget = ui.create_push_button_widget(_("Run"))
        run_button_widget.on_clicked = run_clicked
        run_button_widget.enabled = False

        list_widget_row = ui.create_row_widget()
        list_widget_row.add_spacing(8)
        list_widget_row.add(list_widget)
        list_widget_row.add_spacing(8)

        close_button_widget = ui.create_push_button_widget(_("Close"))
        close_button_widget.on_clicked = self.request_close

        button_row = ui.create_row_widget()
        button_row.add_spacing(12)
        button_row.add(add_button_widget)
        button_row.add(remove_button_widget)
        button_row.add_stretch()
        button_row.add(run_button_widget)
        button_row.add_spacing(12)

        select_column = ui.create_column_widget()
        select_column.add_spacing(8)
        select_column.add(list_widget_row)
        select_column.add_spacing(8)
        select_column.add(button_row)
        select_column.add_spacing(8)

        run_column = ui.create_column_widget()
        run_column.add(self.__output_widget)
        run_column.add_spacing(6)
        run_column.add(self.__message_column)

        self.__stack = ui.create_stack_widget()

        self.__stack.add(select_column)
        self.__stack.add(run_column)

        self.content.add(self.__stack)

        self.__lock = threading.RLock()

        self.__q = collections.deque()
        self.__output_queue = collections.deque()