1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:37:46 +00:00

LibGUI: Make ItemListModel searchable

This commit is contained in:
Karol Kosek 2021-08-21 22:33:37 +02:00 committed by Andreas Kling
parent 8dbb996200
commit c2a89cac4e

View file

@ -77,6 +77,37 @@ public:
return {}; return {};
} }
virtual bool is_searchable() const override { return true; }
virtual Vector<GUI::ModelIndex, 1> matches(StringView const& searching, unsigned flags, GUI::ModelIndex const&) override
{
Vector<GUI::ModelIndex, 1> found_indices;
if constexpr (IsTwoDimensional) {
for (auto it = m_data.begin(); it != m_data.end(); ++it) {
for (auto it2d = (*it).begin(); it2d != (*it).end(); ++it2d) {
GUI::ModelIndex index = this->index(it.index(), it2d.index());
if (!string_matches(data(index, ModelRole::Display).as_string(), searching, flags))
continue;
found_indices.append(index);
if (flags & FirstMatchOnly)
return found_indices;
}
}
} else {
for (auto it = m_data.begin(); it != m_data.end(); ++it) {
GUI::ModelIndex index = this->index(it.index());
if (!string_matches(data(index, ModelRole::Display).as_string(), searching, flags))
continue;
found_indices.append(index);
if (flags & FirstMatchOnly)
return found_indices;
}
}
return found_indices;
}
protected: protected:
explicit ItemListModel(const Container& data, Optional<size_t> row_count = {}) requires(!IsTwoDimensional) explicit ItemListModel(const Container& data, Optional<size_t> row_count = {}) requires(!IsTwoDimensional)
: m_data(data) : m_data(data)