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.
This commit is contained in:
Sam Atkins 2024-02-06 16:57:02 +00:00 committed by Sam Atkins
parent a5d11c0a26
commit 56eb45ccbc
5 changed files with 67 additions and 4 deletions

View file

@ -69,6 +69,13 @@ Optional<Annotation&> AnnotationsModel::closest_annotation_at(size_t position)
return result;
}
Optional<Annotation&> AnnotationsModel::get_annotation(GUI::ModelIndex const& index)
{
if (index.row() < 0 || index.row() >= row_count())
return {};
return m_annotations.at(index.row());
}
ErrorOr<void> AnnotationsModel::save_to_file(Core::File& file) const
{
JsonArray array {};

View file

@ -64,6 +64,7 @@ public:
void add_annotation(Annotation);
void delete_annotation(Annotation const&);
Optional<Annotation&> closest_annotation_at(size_t position);
Optional<Annotation&> get_annotation(GUI::ModelIndex const& index);
ErrorOr<void> save_to_file(Core::File&) const;
ErrorOr<void> load_from_file(Core::File&);

View file

@ -109,6 +109,36 @@ ErrorOr<void> 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<void> HexEditorWidget::setup()
Config::write_bool("HexEditor"sv, "Layout"sv, "ShowValueInspector"sv, action.is_checked());
});
auto& annotations_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("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)

View file

@ -60,6 +60,14 @@
preferred_height: "grow"
visible: false
@GUI::ToolbarContainer {
name: "annotations_toolbar_container"
@GUI::Toolbar {
name: "annotations_toolbar"
}
}
@GUI::TableView {
name: "annotations"
}

View file

@ -17,6 +17,7 @@
#include <LibGUI/ActionGroup.h>
#include <LibGUI/Application.h>
#include <LibGUI/DynamicWidgetContainer.h>
#include <LibGUI/SortingProxyModel.h>
#include <LibGUI/TextEditor.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
@ -97,8 +98,13 @@ private:
RefPtr<GUI::DynamicWidgetContainer> m_side_panel_container;
RefPtr<GUI::Widget> m_value_inspector_container;
RefPtr<GUI::TableView> m_value_inspector;
RefPtr<GUI::Widget> m_annotations_container;
RefPtr<GUI::TableView> m_annotations;
RefPtr<GUI::SortingProxyModel> m_annotations_sorting_model;
RefPtr<GUI::Menu> m_annotations_context_menu;
RefPtr<GUI::Action> m_edit_annotation_action;
RefPtr<GUI::Action> m_delete_annotation_action;
bool m_value_inspector_little_endian { true };
bool m_selecting_from_inspector { false };