summaryrefslogtreecommitdiff
path: root/DevTools/VisualBuilder
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-06-23 08:18:28 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-06-23 08:18:28 +0200
commit6a0011dcea26bc687a6c114e62ec1496807d1115 (patch)
treeff52ed80b1544559e82b0ec55d7553d1b6551a86 /DevTools/VisualBuilder
parent1d0ada32cc33aac5ed9ad8fa65cb051512edd57a (diff)
downloadserenity-6a0011dcea26bc687a6c114e62ec1496807d1115.zip
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.
Diffstat (limited to 'DevTools/VisualBuilder')
-rw-r--r--DevTools/VisualBuilder/VBPropertiesWindow.cpp59
-rw-r--r--DevTools/VisualBuilder/VBWidgetPropertyModel.cpp12
-rw-r--r--DevTools/VisualBuilder/VBWidgetPropertyModel.h1
3 files changed, 72 insertions, 0 deletions
diff --git a/DevTools/VisualBuilder/VBPropertiesWindow.cpp b/DevTools/VisualBuilder/VBPropertiesWindow.cpp
index 146389a19e..c61cd56e89 100644
--- a/DevTools/VisualBuilder/VBPropertiesWindow.cpp
+++ b/DevTools/VisualBuilder/VBPropertiesWindow.cpp
@@ -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()
diff --git a/DevTools/VisualBuilder/VBWidgetPropertyModel.cpp b/DevTools/VisualBuilder/VBWidgetPropertyModel.cpp
index 5bee6870cb..eaed1184e3 100644
--- a/DevTools/VisualBuilder/VBWidgetPropertyModel.cpp
+++ b/DevTools/VisualBuilder/VBWidgetPropertyModel.cpp
@@ -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);
}
diff --git a/DevTools/VisualBuilder/VBWidgetPropertyModel.h b/DevTools/VisualBuilder/VBWidgetPropertyModel.h
index 0653e818ae..8d5cf17eae 100644
--- a/DevTools/VisualBuilder/VBWidgetPropertyModel.h
+++ b/DevTools/VisualBuilder/VBWidgetPropertyModel.h
@@ -10,6 +10,7 @@ public:
enum Column {
Name = 0,
Value,
+ Type,
__Count
};