From d67be43eb0f220de65416a98cf871a805c93d24b Mon Sep 17 00:00:00 2001 From: "Martindale, Nathan" Date: Thu, 24 Aug 2023 08:10:56 -0400 Subject: [PATCH] Add anchor type removal --- icat/anchorlist.py | 67 ++++++++++++++++++++++++-- icat/data.py | 10 +++- notebooks/archive/Example3-Model.ipynb | 4 +- 3 files changed, 74 insertions(+), 7 deletions(-) diff --git a/icat/anchorlist.py b/icat/anchorlist.py index 081a894..94cfae1 100644 --- a/icat/anchorlist.py +++ b/icat/anchorlist.py @@ -551,7 +551,10 @@ def _handle_ipv_new_anchor_type_add_click( ): # must be assigned with partial and assigning type_ref and text_field_ref self.add_anchor_type(type_ref, name=text_field_ref.v_model) - self.refresh_anchor_types() + + def _handle_ipv_rm_anchor_type_btn_click(self, widget, event, data, type_ref: type): + # must be assigned with partial assigning type_ref + self.remove_anchor_type(type_ref) def _handle_ipv_color_picker_input(self, widget, event, data, anchor_type: type): # NOTE: need to set this up with a partial that specifies the anchor_type @@ -811,6 +814,36 @@ def _populate_anchor_types_col(self): ), ) + rm_btn = v.Btn( + small=True, + icon=True, + v_on="tooltip.on", + children=[v.Icon(children=["close"])], + style_="margin-top: 32px;", + ) + rm_btn.on_event( + "click", + partial( + self._handle_ipv_rm_anchor_type_btn_click, + type_ref=anchor_type_dict["ref"], + ), + ) + rm_btn_tooltip = v.Tooltip( + bottom=True, + open_delay=500, + max_width=400, + v_slots=[ + { + "name": "activator", + "variable": "tooltip", + "children": rm_btn, + } + ], + children=[ + "Remove this anchor type (will remove all current anchors of this type.)" + ], + ) + anchor_type_name = v.TextField(v_model=anchor_type_dict["name"], width=100) anchor_type_name.on_event( "change", @@ -835,7 +868,7 @@ def _populate_anchor_types_col(self): anchor_type_name, v.Html( tag="p", - style_="color: grey;", + style_="color: grey; margin-top: -18px; margin-bottom: 2px;", children=[ v.Html( tag="small", @@ -849,6 +882,7 @@ def _populate_anchor_types_col(self): ), ] ), + rm_btn_tooltip, v.Spacer(), color_picker, ] @@ -889,7 +923,7 @@ def _populate_anchor_types_col(self): new_anchor_type_name_text, v.Html( tag="p", - style_="color: grey;", + style_="color: grey; margin-top: -18px; margin-bottom: 2px;", children=[ v.Html( tag="small", @@ -997,6 +1031,33 @@ def modify_anchor_type(self, anchor_type: type, key: str, val: str): self.refresh_anchor_types() + def remove_anchor_type(self, anchor_type: type): + """Removes this anchor type from the current possible anchor types list, and + removes any corresponding anchors.""" + for anchor in self.anchors: + # NOTE: yes use ==, need strict check, not including inheritance. + if type(anchor) == anchor_type: + self.remove_anchor(anchor) + + remaining_anchor_types = [] + for anchor_type_dict in self.possible_anchor_types: + if anchor_type_dict["ref"] != anchor_type: + remaining_anchor_types.append(anchor_type_dict) + self.possible_anchor_types = remaining_anchor_types + self.fire_on_anchor_types_changed() + + # make sure to unset default exaxmple anchor type if it was this one. + # TODO: at some point it would be helpful if when this occurs, we automatically + # search through remaining anchor types and auto re-assign. Not MVP right now. + if ( + "ref" in self.default_example_anchor_type_dict + and self.default_example_anchor_type_dict["ref"] == anchor_type + ): + self.default_example_anchor_type_dict = {} + self.fire_on_default_example_anchor_type_changed() + + self.refresh_anchor_types() + def get_anchor_type_config(self, anchor_type: type): for anchor_type_dict in self.possible_anchor_types: if anchor_type_dict["ref"] == anchor_type: diff --git a/icat/data.py b/icat/data.py index b18930a..2a38f34 100644 --- a/icat/data.py +++ b/icat/data.py @@ -386,8 +386,14 @@ def _handle_label_changed(self, index: int | list[int], new_label: int | list[in def _handle_default_example_anchor_type_changed( self, anchor_type_dict: dict[str, any] ): - self.table.example_btn_color = anchor_type_dict["color"] - self.table.example_type_name = anchor_type_dict["name"] + if "name" in anchor_type_dict: + self.table.example_btn_color = anchor_type_dict["color"] + self.table.example_type_name = anchor_type_dict["name"] + else: + # if we get here, a blank dictionary was likely passed, which means the example + # anchor type was removed. + self.table.example_btn_color = "" + self.table_example_type_name = "similarity" # ============================================================ # EVENT SPAWNERS diff --git a/notebooks/archive/Example3-Model.ipynb b/notebooks/archive/Example3-Model.ipynb index 343b19d..b179bd5 100644 --- a/notebooks/archive/Example3-Model.ipynb +++ b/notebooks/archive/Example3-Model.ipynb @@ -89,13 +89,13 @@ { "cell_type": "code", "execution_count": null, - "id": "aaa79e7c-fb13-43df-9495-e1f94e7d094e", + "id": "e7870719-5d30-4882-9800-25e24e8a4425", "metadata": { "tags": [] }, "outputs": [], "source": [ - "model.anchor_list.default_example_anchor_type_dict" + "model.data.table.example_btn_color" ] }, {