summaryrefslogtreecommitdiff
path: root/Userland/Applications/HexEditor/SearchResultsModel.h
blob: dff620df99b3dbd10032d17e2b40e5490b79d67a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * 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 {
    u64 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;
};