blob: 5d1cb6194c8f3661a892425c70835bca3960ae5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#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;
m_widget = create_widget()->make_weak_ptr();
}
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;
virtual void will_begin_editing() { }
protected:
virtual GWidget* create_widget() = 0;
void commit()
{
if (on_commit)
on_commit();
}
private:
RefPtr<GModel> m_model;
GModelIndex m_index;
WeakPtr<GWidget> m_widget;
};
class GStringModelEditingDelegate : public GModelEditingDelegate {
public:
GStringModelEditingDelegate() {}
virtual ~GStringModelEditingDelegate() override {}
virtual GWidget* create_widget() override
{
auto* textbox = new GTextBox(nullptr);
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()); }
};
|