1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 14:54:59 +00:00
serenity/Userland/Applications/HexEditor/SearchResultsModel.h
sin-ack ca2c81251a Everywhere: Replace Model::update() with Model::invalidate()
Most of the models were just calling did_update anyway, which is
pointless since it can be unified to the base Model class. Instead, code
calling update() will now call invalidate(), which functions identically
and is more obvious in what it does.

Additionally, a default implementation is provided, which removes the
need to add empty implementations of update() for each model subclass.

Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
2021-08-06 19:14:31 +02:00

80 lines
1.9 KiB
C++

/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Hex.h>
#include <AK/NonnullRefPtr.h>
#include <AK/String.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <LibGUI/Model.h>
struct Match {
int offset;
String value;
};
class SearchResultsModel final : public GUI::Model {
public:
enum Column {
Offset,
Value
};
explicit SearchResultsModel(const Vector<Match>&& 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;
}
String 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 String::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;
};