mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:27:35 +00:00
LibGUI+VisualBuilder: Support custom editing widgets for property values.
Implemented this by letting GAbstractViews provide a GModelEditingDelegate for a given index, which then knows how to create and setup a custom widget appropriate for the data type being edited.
This commit is contained in:
parent
1d0ada32cc
commit
6a0011dcea
8 changed files with 170 additions and 7 deletions
|
@ -1,9 +1,55 @@
|
|||
#include "VBPropertiesWindow.h"
|
||||
#include "VBWidgetPropertyModel.h"
|
||||
#include <LibGUI/GBoxLayout.h>
|
||||
#include <LibGUI/GComboBox.h>
|
||||
#include <LibGUI/GModelEditingDelegate.h>
|
||||
#include <LibGUI/GTableView.h>
|
||||
#include <LibGUI/GTextBox.h>
|
||||
#include <LibGUI/GWidget.h>
|
||||
|
||||
class BoolValuesModel final : public GModel {
|
||||
public:
|
||||
virtual int row_count(const GModelIndex&) const override { return 2; }
|
||||
virtual int column_count(const GModelIndex&) const override { return 1; }
|
||||
virtual void update() override {}
|
||||
virtual GVariant data(const GModelIndex& index, Role role) const override
|
||||
{
|
||||
if (role != Role::Display)
|
||||
return {};
|
||||
switch (index.row()) {
|
||||
case 0:
|
||||
return "false";
|
||||
case 1:
|
||||
return "true";
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
};
|
||||
|
||||
class BoolModelEditingDelegate : public GModelEditingDelegate {
|
||||
public:
|
||||
BoolModelEditingDelegate() {}
|
||||
virtual ~BoolModelEditingDelegate() override {}
|
||||
|
||||
virtual GWidget* create_widget() override
|
||||
{
|
||||
auto* combo = new GComboBox(nullptr);
|
||||
combo->set_only_allow_values_from_model(true);
|
||||
combo->set_model(adopt(*new BoolValuesModel));
|
||||
combo->on_return_pressed = [this] { commit(); };
|
||||
combo->on_change = [this](auto&) { commit(); };
|
||||
return combo;
|
||||
}
|
||||
virtual GVariant value() const override { return static_cast<const GComboBox*>(widget())->text() == "true"; }
|
||||
virtual void set_value(const GVariant& value) override { static_cast<GComboBox*>(widget())->set_text(value.to_string()); }
|
||||
virtual void will_begin_editing() override
|
||||
{
|
||||
auto& combo = *static_cast<GComboBox*>(widget());
|
||||
combo.select_all();
|
||||
combo.open();
|
||||
}
|
||||
};
|
||||
|
||||
VBPropertiesWindow::VBPropertiesWindow()
|
||||
{
|
||||
set_title("Properties");
|
||||
|
@ -18,6 +64,19 @@ VBPropertiesWindow::VBPropertiesWindow()
|
|||
m_table_view = new GTableView(widget);
|
||||
m_table_view->set_headers_visible(false);
|
||||
m_table_view->set_editable(true);
|
||||
|
||||
m_table_view->aid_create_editing_delegate = [this](auto& index) -> OwnPtr<GModelEditingDelegate> {
|
||||
if (!m_table_view->model())
|
||||
return nullptr;
|
||||
auto type_index = m_table_view->model()->index(index.row(), VBWidgetPropertyModel::Column::Type);
|
||||
auto type = m_table_view->model()->data(type_index, GModel::Role::Custom).to_int();
|
||||
switch ((GVariant::Type)type) {
|
||||
case GVariant::Type::Bool:
|
||||
return make<BoolModelEditingDelegate>();
|
||||
default:
|
||||
return make<GStringModelEditingDelegate>();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
VBPropertiesWindow::~VBPropertiesWindow()
|
||||
|
|
|
@ -24,6 +24,8 @@ String VBWidgetPropertyModel::column_name(int column) const
|
|||
return "Name";
|
||||
case Column::Value:
|
||||
return "Value";
|
||||
case Column::Type:
|
||||
return "Type";
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
@ -39,6 +41,12 @@ GModel::ColumnMetadata VBWidgetPropertyModel::column_metadata(int column) const
|
|||
|
||||
GVariant VBWidgetPropertyModel::data(const GModelIndex& index, Role role) const
|
||||
{
|
||||
if (role == Role::Custom) {
|
||||
auto& property = *m_widget.m_properties[index.row()];
|
||||
if (index.column() == Column::Type)
|
||||
return (int)property.value().type();
|
||||
return {};
|
||||
}
|
||||
if (role == Role::Display) {
|
||||
auto& property = *m_widget.m_properties[index.row()];
|
||||
switch (index.column()) {
|
||||
|
@ -46,6 +54,8 @@ GVariant VBWidgetPropertyModel::data(const GModelIndex& index, Role role) const
|
|||
return property.name();
|
||||
case Column::Value:
|
||||
return property.value();
|
||||
case Column::Type:
|
||||
return to_string(property.value().type());
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
@ -54,6 +64,8 @@ GVariant VBWidgetPropertyModel::data(const GModelIndex& index, Role role) const
|
|||
switch (index.column()) {
|
||||
case Column::Name:
|
||||
return Color::Black;
|
||||
case Column::Type:
|
||||
return Color::Blue;
|
||||
case Column::Value:
|
||||
return property.is_readonly() ? Color(Color::MidGray) : Color(Color::Black);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ public:
|
|||
enum Column {
|
||||
Name = 0,
|
||||
Value,
|
||||
Type,
|
||||
__Count
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue