ladybird/Userland/Applications/HexEditor/SearchResultsModel.h
Linus Groh 6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00

81 lines
1.9 KiB
C++

/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/Hex.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <LibGUI/Model.h>
struct Match {
u64 offset;
DeprecatedString value;
};
class SearchResultsModel final : public GUI::Model {
public:
enum Column {
Offset,
Value
};
explicit SearchResultsModel(Vector<Match> const&& matches)
: m_matches(move(matches))
{
}
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override
{
return m_matches.size();
}
virtual int column_count(const GUI::ModelIndex&) const override
{
return 2;
}
DeprecatedString column_name(int column) const override
{
switch (column) {
case Column::Offset:
return "Offset";
case Column::Value:
return "Value";
}
VERIFY_NOT_REACHED();
}
virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
{
if (role == GUI::ModelRole::TextAlignment)
return Gfx::TextAlignment::CenterLeft;
if (role == GUI::ModelRole::Custom) {
auto& match = m_matches.at(index.row());
return match.offset;
}
if (role == GUI::ModelRole::Display) {
auto& match = m_matches.at(index.row());
switch (index.column()) {
case Column::Offset:
return DeprecatedString::formatted("{:#08X}", match.offset);
case Column::Value: {
Utf8View utf8_view(match.value);
if (!utf8_view.validate())
return {};
return StringView(match.value);
}
}
}
return {};
}
private:
Vector<Match> m_matches;
};