1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

HexEditor: Store annotations in a Model

A model is necessary for displaying a list of them in the UI. We might
as well make that their home.
This commit is contained in:
Sam Atkins 2024-01-30 11:08:41 +00:00 committed by Sam Atkins
parent a54952795a
commit 8cac2e89a9
8 changed files with 157 additions and 46 deletions

View file

@ -0,0 +1,70 @@
/*
* 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);
private:
Vector<Annotation> m_annotations;
};