summaryrefslogtreecommitdiff
path: root/Userland/Applications/HexEditor/SearchResultsModel.h
diff options
context:
space:
mode:
authorBrendan Coles <bcoles@gmail.com>2021-05-27 15:51:21 +0000
committerAndreas Kling <kling@serenityos.org>2021-05-27 22:57:17 +0200
commit6aa766f8ca861af1f47935b99d14662c24d7cbd7 (patch)
tree41e5d7b7c0c20908c85058593661aa085ba891ab /Userland/Applications/HexEditor/SearchResultsModel.h
parentd7797c8bf8888f00d7e8e9360939112a0dde5b8d (diff)
downloadserenity-6aa766f8ca861af1f47935b99d14662c24d7cbd7.zip
HexEditor: Add 'Find All' option to Find Dialog to find all matches
Diffstat (limited to 'Userland/Applications/HexEditor/SearchResultsModel.h')
-rw-r--r--Userland/Applications/HexEditor/SearchResultsModel.h82
1 files changed, 82 insertions, 0 deletions
diff --git a/Userland/Applications/HexEditor/SearchResultsModel.h b/Userland/Applications/HexEditor/SearchResultsModel.h
new file mode 100644
index 0000000000..c70e698bb1
--- /dev/null
+++ b/Userland/Applications/HexEditor/SearchResultsModel.h
@@ -0,0 +1,82 @@
+/*
+ * 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 {};
+ }
+
+ virtual void update() override { }
+
+private:
+ Vector<Match> m_matches;
+};