Skip to content

Commit

Permalink
Merge pull request #383 from livMatS/2024-11-04-adding-actions
Browse files Browse the repository at this point in the history
Adding actions
  • Loading branch information
jotelha authored Dec 16, 2024
2 parents 596f5c8 + f175cfa commit f884918
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ unpublished
- Sorting for dserver lookup results
- Add/delete tags in GUI
- Add annotations in GUI (key - value pairs)
- Delete annotations in GUI (required changes to dtoolcore)

0.6.2 (02Nov22)
---------------
Expand Down
10 changes: 4 additions & 6 deletions dtool_lookup_gui/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,10 @@ def delete_tag(self,tag):
if 'tags' in self._dataset_info:
self._dataset_info['tags'].remove(tag)

# delete annotation is not implemented in dtoolcore
# def delete_annotation(self,annotation_name , annotation):
# print("delete_annotation",annotation_name,annotation)
# _load_dataset(str(self)).delete_annotation(annotation_name, annotation)
# if 'annotations' in self._dataset_info:
# self._dataset_info['annotations'].remove(annotation_name)
def delete_annotation(self, annotation_name):
_load_dataset(str(self)).delete_annotation(annotation_name)
if 'annotations' in self._dataset_info and annotation_name in self._dataset_info['annotations']:
del self._dataset_info['annotations'][annotation_name]

async def get_readme(self):
if 'readme_content' in self._dataset_info:
Expand Down
133 changes: 106 additions & 27 deletions dtool_lookup_gui/views/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ class MainWindow(Gtk.ApplicationWindow):

show_tags_box = Gtk.Template.Child()
annotations_box = Gtk.Template.Child()
# add_tags_button = Gtk.Template.Child()
# enter_tags_entry = Gtk.Template.Child()

linting_errors_button = Gtk.Template.Child()

Expand Down Expand Up @@ -376,17 +374,46 @@ def __init__(self, *args, **kwargs):
get_item_action.connect("activate", self.do_get_item)
self.add_action(get_item_action)

# put tags
put_tags_variant = GLib.Variant.new_string('dummy')
put_tags_action = Gio.SimpleAction.new("put-tag", put_tags_variant.get_type())
put_tags_action.connect("activate", self.do_put_tag)
self.add_action(put_tags_action)

# put annotations
put_annotations_variant_type = GLib.VariantType.new("(ss)") # Tuple of two strings
put_annotations_action = Gio.SimpleAction.new("put-annotation", put_annotations_variant_type)
put_annotations_action.connect("activate", self.do_put_annotation)
self.add_action(put_annotations_action)
# put tag
put_tag_variant = GLib.Variant.new_string('dummy')
put_tag_action = Gio.SimpleAction.new("put-tag", put_tag_variant.get_type())
put_tag_action.connect("activate", self.do_put_tag)
self.add_action(put_tag_action)

# put annotation
put_annotation_variant_type = GLib.VariantType.new("(ss)") # Tuple of two strings
put_annotation_action = Gio.SimpleAction.new("put-annotation", put_annotation_variant_type)
put_annotation_action.connect("activate", self.do_put_annotation)
self.add_action(put_annotation_action)

# delete tag
delete_tag_variant = GLib.Variant.new_string('dummy')
delete_tag_action = Gio.SimpleAction.new("delete-tag", delete_tag_variant.get_type())
delete_tag_action.connect("activate", self.do_delete_tag)
self.add_action(delete_tag_action)

# delete annotation
delete_annotation_variant = GLib.Variant.new_string('dummy')
delete_annotation_action = Gio.SimpleAction.new("delete-annotation", delete_annotation_variant.get_type())
delete_annotation_action.connect("activate", self.do_delete_annotation)
self.add_action(delete_annotation_action)

# add item
add_item_variant = GLib.Variant.new_string("dummy")
add_item_action = Gio.SimpleAction.new("add-item", add_item_variant.get_type())
add_item_action.connect("activate", self.do_add_item)
self.add_action(add_item_action)

# create dataset
create_dataset_variant = GLib.Variant.new_string("dummy")
create_dataset_action = Gio.SimpleAction.new("create-dataset", create_dataset_variant.get_type())
create_dataset_action.connect("activate", self.do_create_dataset)
self.add_action(create_dataset_action)

# freeze dataset
freeze_dataset_action = Gio.SimpleAction.new("freeze-dataset")
freeze_dataset_action.connect("activate", self.do_freeze_dataset)
self.add_action(freeze_dataset_action)

# refresh view
refresh_view_action = Gio.SimpleAction.new("refresh-view")
Expand Down Expand Up @@ -517,6 +544,33 @@ def do_put_annotation(self, action, parameter):
_logger.debug("Unpacked %s: %s key-value pair from tuple in do_put_annotation")
self._put_annotation(key, value)

def do_delete_tag(self, action, value):
"""Put tags on the selected dataset."""
tag = value.get_string()
self._delete_tag(tag)

# put annotations action
def do_delete_annotation(self, action, value):
"""Put annotations on the selected dataset."""
value = value.get_string()
self._delete_annotation(value)

# add item action
def do_add_item(self, action, value):
"""Add item to the selected dataset."""
item = value.get_string()
self._add_item(item)

# create dataset action
def do_create_dataset(self, action, value):
"""Create a new dataset."""
self._create_dataset(value.get_string())

# freeze dataset action
def do_freeze_dataset(self, action, value):
"""Freeze the selected dataset."""
self._freeze_dataset()

# other actions
def do_get_item(self, action, value):
""""Copy currently selected manifest item in currently selected dataset to specified destination."""
Expand Down Expand Up @@ -659,7 +713,8 @@ def on_open_local_directory_clicked(self, widget):
@Gtk.Template.Callback()
def on_create_dataset_clicked(self, widget):
"""Dataset creation button clicked."""
DatasetNameDialog(on_confirmation=self._create_dataset).show()
DatasetNameDialog(on_confirmation=lambda name:self.activate_action('create-dataset', GLib.Variant.new_string(name))
).show()

@Gtk.Template.Callback()
def on_refresh_clicked(self, widget):
Expand Down Expand Up @@ -697,7 +752,8 @@ def on_add_items_clicked(self, widget):
fpaths = dialog.get_filenames()
for fpath in fpaths:
# uri = urllib.parse.unquote(uri, encoding='utf-8', errors='replace')
self._add_item(fpath)
# self._add_item(fpath)
self.activate_action('add-item', GLib.Variant.new_string(fpath))
elif response == Gtk.ResponseType.CANCEL:
pass
dialog.destroy()
Expand Down Expand Up @@ -783,11 +839,12 @@ def on_freeze_clicked(self, widget):
response = dialog.run()
dialog.destroy()
if response == Gtk.ResponseType.OK:
uri = row.dataset.uri # URI won't change in freeze process
row.freeze()
# uri = row.dataset.uri # URI won't change in freeze process
# row.freeze()
self.activate_action('freeze-dataset')
self.dataset_list_box.show_all()
self.get_action_group("win").activate_action('select-dataset-by-uri', GLib.Variant.new_string(uri))
self.get_action_group("win").activate_action('show-dataset-by-uri', GLib.Variant.new_string(uri))
# self.get_action_group("win").activate_action('select-dataset-by-uri', GLib.Variant.new_string(uri))
# self.get_action_group("win").activate_action('show-dataset-by-uri', GLib.Variant.new_string(uri))

@Gtk.Template.Callback()
def on_error_bar_close(self, widget):
Expand Down Expand Up @@ -1151,6 +1208,18 @@ def _put_annotation(self, key, value):
dataset.put_annotation(annotation_name=key, annotation=value)
asyncio.create_task(self._update_dataset_view(dataset))

def _delete_tag(self, tag):
"""Put tags on the selected dataset."""
dataset = self.dataset_list_box.get_selected_row().dataset
dataset.delete_tag(tag)
asyncio.create_task(self._update_dataset_view(dataset))

def _delete_annotation(self, annotation_name):
"""Put annotations on the selected dataset."""
dataset = self.dataset_list_box.get_selected_row().dataset
dataset.delete_annotation(annotation_name)
asyncio.create_task(self._update_dataset_view(dataset))

def _refresh_datasets(self, on_show=None):
"""Reset dataset list, show spinner, and kick off async task for retrieving dataset entries."""
self.main_stack.set_visible_child(self.main_spinner)
Expand Down Expand Up @@ -1384,6 +1453,14 @@ def _create_dataset(self, name):
if base_uri is not None:
self.dataset_list_box.add_dataset(base_uri.base_uri.create_dataset(name))
self.dataset_list_box.show_all()

def _freeze_dataset(self):
row = self.dataset_list_box.get_selected_row()
uri = row.dataset.uri
row.freeze()
self.dataset_list_box.show_all()
self.get_action_group("win").activate_action('select-dataset-by-uri', GLib.Variant.new_string(uri))
self.get_action_group("win").activate_action('show-dataset-by-uri', GLib.Variant.new_string(uri))

async def _update_dataset_view(self, dataset):
_logger.debug("In _update_dataset_view.")
Expand Down Expand Up @@ -1424,10 +1501,9 @@ async def _get_manifest():
self.manifest_stack.set_visible_child(self.manifest_view)

def on_remove_tag(self, button, tag):
dataset.delete_tag(tag)
asyncio.create_task(self._update_dataset_view(dataset))
self.activate_action('delete-tag', GLib.Variant.new_string(tag))

def on_add_tag(self,button, entry):
def on_add_tag(self, button, entry):
tag = entry.get_text()
self.activate_action('put-tag', GLib.Variant.new_string(tag))

Expand All @@ -1446,7 +1522,8 @@ async def _get_tags():

# Remove button for the tag
button = Gtk.Button(label="-")
button.connect("clicked",lambda button, tag = tag : on_remove_tag(self,button,tag))
button.connect("clicked",
lambda button, tag = tag : on_remove_tag(self, button, tag))

# Adding the label and button to the box
box.pack_start(label, False, False, 0)
Expand All @@ -1465,7 +1542,8 @@ async def _get_tags():

# "+" button for adding the new tag
add_button = Gtk.Button(label="+")
add_button.connect("clicked", lambda button: on_add_tag(self , button, entry))
add_button.connect("clicked",
lambda button: on_add_tag(self , button, entry))

# Adding the entry and "+" button to the add_box
add_box.pack_start(entry, True, True, 0)
Expand Down Expand Up @@ -1514,9 +1592,10 @@ async def on_button_clicked(button):
current_label = button.get_label()
if current_label == "-":
# Delete annotation
self.annotations_box.remove(box)
# self.annotations_box.remove(box)
# Function to delete the annotation from the dataset not yet implemented
# dataset.delete_annotation(key)
self.activate_action('delete-annotation', GLib.Variant.new_string(key))
elif current_label == "+":
# Save new/updated annotation
new_key = key_widget.get_text() if is_new else key
Expand All @@ -1538,7 +1617,8 @@ def on_text_changed(entry):
value_entry.connect("changed", on_text_changed)
if is_new:
key_widget.connect("changed", on_text_changed) # Only for the new key entry
button.connect("clicked", lambda btn: asyncio.ensure_future(on_button_clicked(btn)))
button.connect("clicked",
lambda btn: asyncio.ensure_future(on_button_clicked(btn)))

# Add the button to the row
box.pack_start(button, expand=False, fill=False, padding=0)
Expand All @@ -1558,7 +1638,6 @@ def on_text_changed(entry):
# Re-render the UI
self.annotations_box.show_all()


_logger.debug("Get readme.")
asyncio.create_task(_get_readme())
_logger.debug("Get manifest.")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ classifiers = [
dynamic = ["version"]

dependencies = [
'dtoolcore>=3.17',
'dtoolcore>=3.19.0',
'dtool-create>=0.23.4',
'dtool-info>=0.16.2',
'dtool-lookup-api>=0.10.1',
Expand Down

0 comments on commit f884918

Please sign in to comment.