From 56eb45ccbc87e91da563e1b9597682e051fa8a9f Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 6 Feb 2024 16:57:02 +0000 Subject: [PATCH] HexEditor: Add toolbar and context menus to the Annotations panel The Annotations panel is the most obvious place to perform actions related to annotations, so let's make that possible. :^) The toolbar gets open/save/save-as actions for annotations, and one for adding an annotation. The table itself gets a context menu for editing or deleting the selected annotation. --- .../HexEditor/AnnotationsModel.cpp | 7 +++ .../Applications/HexEditor/AnnotationsModel.h | 1 + .../HexEditor/HexEditorWidget.cpp | 49 +++++++++++++++++-- .../HexEditor/HexEditorWidget.gml | 8 +++ .../Applications/HexEditor/HexEditorWidget.h | 6 +++ 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/Userland/Applications/HexEditor/AnnotationsModel.cpp b/Userland/Applications/HexEditor/AnnotationsModel.cpp index c717b0d169..df2b4ed163 100644 --- a/Userland/Applications/HexEditor/AnnotationsModel.cpp +++ b/Userland/Applications/HexEditor/AnnotationsModel.cpp @@ -69,6 +69,13 @@ Optional AnnotationsModel::closest_annotation_at(size_t position) return result; } +Optional AnnotationsModel::get_annotation(GUI::ModelIndex const& index) +{ + if (index.row() < 0 || index.row() >= row_count()) + return {}; + return m_annotations.at(index.row()); +} + ErrorOr AnnotationsModel::save_to_file(Core::File& file) const { JsonArray array {}; diff --git a/Userland/Applications/HexEditor/AnnotationsModel.h b/Userland/Applications/HexEditor/AnnotationsModel.h index d40f7fee53..1d4c87c9ad 100644 --- a/Userland/Applications/HexEditor/AnnotationsModel.h +++ b/Userland/Applications/HexEditor/AnnotationsModel.h @@ -64,6 +64,7 @@ public: void add_annotation(Annotation); void delete_annotation(Annotation const&); Optional closest_annotation_at(size_t position); + Optional get_annotation(GUI::ModelIndex const& index); ErrorOr save_to_file(Core::File&) const; ErrorOr load_from_file(Core::File&); diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index dff09fd358..b5cd648ea7 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -109,6 +109,36 @@ ErrorOr HexEditorWidget::setup() m_editor->update(); }; + m_annotations_context_menu = GUI::Menu::construct(); + m_edit_annotation_action = GUI::Action::create( + "&Edit Annotation", + TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/annotation.png"sv)), + [this](GUI::Action&) { + if (m_annotations->selection().is_empty()) + return; + auto index = m_annotations_sorting_model->map_to_source(m_annotations->selection().first()); + auto& annotation = m_editor->document().annotations().get_annotation(index).value(); + m_editor->show_edit_annotation_dialog(annotation); + }, + this); + m_annotations_context_menu->add_action(*m_edit_annotation_action); + m_delete_annotation_action = GUI::Action::create( + "&Delete Annotation", + TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/annotation-remove.png"sv)), + [this](GUI::Action&) { + if (m_annotations->selection().is_empty()) + return; + auto index = m_annotations_sorting_model->map_to_source(m_annotations->selection().first()); + auto& annotation = m_editor->document().annotations().get_annotation(index).value(); + m_editor->show_delete_annotation_dialog(annotation); + }, + this); + m_annotations_context_menu->add_action(*m_delete_annotation_action); + m_annotations->on_context_menu_request = [this](GUI::ModelIndex const& index, GUI::ContextMenuEvent const& event) { + if (index.is_valid()) + m_annotations_context_menu->popup(event.screen_position()); + }; + m_search_results->set_activates_on_selection(true); m_search_results->on_activation = [this](const GUI::ModelIndex& index) { if (!index.is_valid()) @@ -327,6 +357,17 @@ ErrorOr HexEditorWidget::setup() Config::write_bool("HexEditor"sv, "Layout"sv, "ShowValueInspector"sv, action.is_checked()); }); + auto& annotations_toolbar = *find_descendant_of_type_named("annotations_toolbar"); + annotations_toolbar.add_action(*m_open_annotations_action); + annotations_toolbar.add_action(*m_save_annotations_action); + annotations_toolbar.add_action(*m_save_annotations_as_action); + annotations_toolbar.add_separator(); + annotations_toolbar.add_action(GUI::Action::create( + "&Add Annotation", + TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/annotation-add.png"sv)), + [this](GUI::Action&) { m_editor->show_create_annotation_dialog(); }, + this)); + m_toolbar->add_action(*m_new_action); m_toolbar->add_action(*m_open_action); m_toolbar->add_action(*m_save_action); @@ -723,10 +764,10 @@ void HexEditorWidget::set_annotations_visible(bool visible) void HexEditorWidget::initialize_annotations_model() { - auto sorting_model = MUST(GUI::SortingProxyModel::create(m_editor->document().annotations())); - sorting_model->set_sort_role((GUI::ModelRole)AnnotationsModel::CustomRole::StartOffset); - sorting_model->sort(AnnotationsModel::Column::Start, GUI::SortOrder::Ascending); - m_annotations->set_model(sorting_model); + m_annotations_sorting_model = MUST(GUI::SortingProxyModel::create(m_editor->document().annotations())); + m_annotations_sorting_model->set_sort_role((GUI::ModelRole)AnnotationsModel::CustomRole::StartOffset); + m_annotations_sorting_model->sort(AnnotationsModel::Column::Start, GUI::SortOrder::Ascending); + m_annotations->set_model(m_annotations_sorting_model); } void HexEditorWidget::set_search_results_visible(bool visible) diff --git a/Userland/Applications/HexEditor/HexEditorWidget.gml b/Userland/Applications/HexEditor/HexEditorWidget.gml index f80073cc56..cce6468829 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.gml +++ b/Userland/Applications/HexEditor/HexEditorWidget.gml @@ -60,6 +60,14 @@ preferred_height: "grow" visible: false + @GUI::ToolbarContainer { + name: "annotations_toolbar_container" + + @GUI::Toolbar { + name: "annotations_toolbar" + } + } + @GUI::TableView { name: "annotations" } diff --git a/Userland/Applications/HexEditor/HexEditorWidget.h b/Userland/Applications/HexEditor/HexEditorWidget.h index 714ed76457..147ed3563d 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.h +++ b/Userland/Applications/HexEditor/HexEditorWidget.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -97,8 +98,13 @@ private: RefPtr m_side_panel_container; RefPtr m_value_inspector_container; RefPtr m_value_inspector; + RefPtr m_annotations_container; RefPtr m_annotations; + RefPtr m_annotations_sorting_model; + RefPtr m_annotations_context_menu; + RefPtr m_edit_annotation_action; + RefPtr m_delete_annotation_action; bool m_value_inspector_little_endian { true }; bool m_selecting_from_inspector { false };