1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:48:11 +00:00
serenity/Userland/Applications/HexEditor/AnnotationsModel.h
Sam Atkins 56caee44e3 HexEditor: Save and load annotations to/from JSON files
This is a fairly simple JSON format: A single array, containing objects,
with the Annotation fields as key:value pairs.

When reading a file, we let invalid or missing keys fall back to the
default values. This is mostly intended to set a pattern so that if we
add new fields in the future, we won't fail to load old annotations
files. If loading the file fails though, we keep the previously loaded
set of annotations.
2024-01-31 17:38:56 +00:00

73 lines
1.8 KiB
C++

/*
* Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Optional.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibGUI/Model.h>
struct Annotation {
size_t start_offset { 0 };
size_t end_offset { 0 };
Gfx::Color background_color { Color::from_argb(0xfffce94f) };
String comments {};
bool operator==(Annotation const& other) const = default;
};
class AnnotationsModel final : public GUI::Model {
public:
enum Column {
Start,
End,
Comments,
};
enum class CustomRole {
StartOffset = to_underlying(GUI::ModelRole::Custom) + 1,
EndOffset,
Comments,
};
virtual int row_count(GUI::ModelIndex const& index = GUI::ModelIndex()) const override
{
if (!index.is_valid())
return m_annotations.size();
return 0;
}
virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override
{
return 3;
}
virtual ErrorOr<String> column_name(int column) const override
{
switch (column) {
case Column::Start:
return "Start"_string;
case Column::End:
return "End"_string;
case Column::Comments:
return "Comments"_string;
}
VERIFY_NOT_REACHED();
}
virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role = GUI::ModelRole::Display) const override;
void add_annotation(Annotation);
void delete_annotation(Annotation const&);
Optional<Annotation&> closest_annotation_at(size_t position);
ErrorOr<void> save_to_file(Core::File&) const;
ErrorOr<void> load_from_file(Core::File&);
private:
Vector<Annotation> m_annotations;
};