2019-06-23 08:18:28 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <LibGUI/GModel.h>
|
|
|
|
#include <LibGUI/GTextBox.h>
|
|
|
|
#include <LibGUI/GWidget.h>
|
|
|
|
|
|
|
|
class GModelEditingDelegate {
|
|
|
|
public:
|
|
|
|
GModelEditingDelegate() {}
|
|
|
|
virtual ~GModelEditingDelegate() {}
|
|
|
|
|
|
|
|
void bind(GModel& model, const GModelIndex& index)
|
|
|
|
{
|
|
|
|
if (m_model.ptr() == &model && m_index == index)
|
|
|
|
return;
|
|
|
|
m_model = model;
|
|
|
|
m_index = index;
|
2019-09-21 15:43:52 +02:00
|
|
|
m_widget = create_widget();
|
2019-06-23 08:18:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GWidget* widget() { return m_widget; }
|
|
|
|
const GWidget* widget() const { return m_widget; }
|
|
|
|
|
|
|
|
Function<void()> on_commit;
|
|
|
|
|
|
|
|
virtual GVariant value() const = 0;
|
|
|
|
virtual void set_value(const GVariant&) = 0;
|
|
|
|
|
2020-01-10 19:06:00 +03:00
|
|
|
virtual void will_begin_editing() {}
|
2019-06-23 08:18:28 +02:00
|
|
|
|
|
|
|
protected:
|
2019-09-22 00:31:54 +02:00
|
|
|
virtual RefPtr<GWidget> create_widget() = 0;
|
2019-06-23 08:18:28 +02:00
|
|
|
void commit()
|
|
|
|
{
|
|
|
|
if (on_commit)
|
|
|
|
on_commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<GModel> m_model;
|
|
|
|
GModelIndex m_index;
|
2019-09-22 00:31:54 +02:00
|
|
|
RefPtr<GWidget> m_widget;
|
2019-06-23 08:18:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class GStringModelEditingDelegate : public GModelEditingDelegate {
|
|
|
|
public:
|
|
|
|
GStringModelEditingDelegate() {}
|
|
|
|
virtual ~GStringModelEditingDelegate() override {}
|
|
|
|
|
2019-09-22 00:31:54 +02:00
|
|
|
virtual RefPtr<GWidget> create_widget() override
|
2019-06-23 08:18:28 +02:00
|
|
|
{
|
2019-09-21 15:43:52 +02:00
|
|
|
auto textbox = GTextBox::construct(nullptr);
|
2019-06-23 08:18:28 +02:00
|
|
|
textbox->on_return_pressed = [this] {
|
|
|
|
commit();
|
|
|
|
};
|
|
|
|
return textbox;
|
|
|
|
}
|
|
|
|
virtual GVariant value() const override { return static_cast<const GTextBox*>(widget())->text(); }
|
|
|
|
virtual void set_value(const GVariant& value) override { static_cast<GTextBox*>(widget())->set_text(value.to_string()); }
|
|
|
|
};
|