From 8817d3ec58d74216bca80898da9359946e4d9299 Mon Sep 17 00:00:00 2001 From: Karol Kosek Date: Sun, 17 Apr 2022 14:25:14 +0200 Subject: [PATCH] ThemeEditor: Add generic RoleModel template class This is to simplify the code, as Color, Alignment, Flag, Metric and Path RoleModel classes looked exactly the same. Additionally, I've added a try_create() function for error propagation. :^) --- Userland/Applications/ThemeEditor/main.cpp | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Userland/Applications/ThemeEditor/main.cpp b/Userland/Applications/ThemeEditor/main.cpp index 7e068bfe6c..67704fc6a6 100644 --- a/Userland/Applications/ThemeEditor/main.cpp +++ b/Userland/Applications/ThemeEditor/main.cpp @@ -33,6 +33,31 @@ #include #include +template +class RoleModel final : public GUI::ItemListModel { +public: + static ErrorOr> try_create(Vector const& data) + { + return adopt_nonnull_ref_or_enomem(new (nothrow) RoleModel(data)); + } + + virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override + { + if (role == GUI::ModelRole::Display) + return Gfx::to_string(this->m_data[index.row()]); + if (role == GUI::ModelRole::Custom) + return this->m_data[index.row()]; + + return GUI::ItemListModel::data(index, role); + } + +private: + explicit RoleModel(Vector const& data) + : GUI::ItemListModel(data) + { + } +}; + class ColorRoleModel final : public GUI::ItemListModel { public: explicit ColorRoleModel(Vector const& data)