#pragma once #include #include #include 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(); } GWidget* widget() { return m_widget; } const GWidget* widget() const { return m_widget; } Function on_commit; virtual GVariant value() const = 0; virtual void set_value(const GVariant&) = 0; virtual void will_begin_editing() { } protected: virtual ObjectPtr create_widget() = 0; void commit() { if (on_commit) on_commit(); } private: RefPtr m_model; GModelIndex m_index; ObjectPtr m_widget; }; class GStringModelEditingDelegate : public GModelEditingDelegate { public: GStringModelEditingDelegate() {} virtual ~GStringModelEditingDelegate() override {} virtual ObjectPtr create_widget() override { auto textbox = GTextBox::construct(nullptr); textbox->on_return_pressed = [this] { commit(); }; return textbox; } virtual GVariant value() const override { return static_cast(widget())->text(); } virtual void set_value(const GVariant& value) override { static_cast(widget())->set_text(value.to_string()); } };