mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:07:36 +00:00
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. :^)
This commit is contained in:
parent
73552c1856
commit
8817d3ec58
1 changed files with 25 additions and 0 deletions
|
@ -33,6 +33,31 @@
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class RoleModel final : public GUI::ItemListModel<T> {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullRefPtr<RoleModel>> try_create(Vector<T> const& data)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_ref_or_enomem(new (nothrow) RoleModel<T>(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<T>::data(index, role);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit RoleModel(Vector<T> const& data)
|
||||||
|
: GUI::ItemListModel<T>(data)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class ColorRoleModel final : public GUI::ItemListModel<Gfx::ColorRole> {
|
class ColorRoleModel final : public GUI::ItemListModel<Gfx::ColorRole> {
|
||||||
public:
|
public:
|
||||||
explicit ColorRoleModel(Vector<Gfx::ColorRole> const& data)
|
explicit ColorRoleModel(Vector<Gfx::ColorRole> const& data)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue