2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-06-23 08:18:28 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Model.h>
|
|
|
|
|
#include <LibGUI/TextBox.h>
|
|
|
|
|
#include <LibGUI/Widget.h>
|
2019-06-23 08:18:28 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
class ModelEditingDelegate {
|
2019-06-23 08:18:28 +02:00
|
|
|
public:
|
2021-07-11 21:46:34 +02:00
|
|
|
enum SelectionBehavior {
|
|
|
|
|
DoNotSelect,
|
|
|
|
|
SelectAll,
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-26 10:50:04 -07:00
|
|
|
virtual ~ModelEditingDelegate() = default;
|
2019-06-23 08:18:28 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void bind(Model& model, ModelIndex const& index)
|
2019-06-23 08:18:28 +02:00
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Widget* widget() { return m_widget; }
|
2022-04-01 20:58:27 +03:00
|
|
|
Widget const* widget() const { return m_widget; }
|
2019-06-23 08:18:28 +02:00
|
|
|
|
|
|
|
|
Function<void()> on_commit;
|
2020-08-28 20:50:12 +02:00
|
|
|
Function<void()> on_rollback;
|
2021-09-05 13:32:23 +02:00
|
|
|
Function<void()> on_change;
|
2019-06-23 08:18:28 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual Variant value() const = 0;
|
2021-07-11 21:46:34 +02:00
|
|
|
virtual void set_value(Variant const&, SelectionBehavior selection_behavior = SelectionBehavior::SelectAll) = 0;
|
2019-06-23 08:18:28 +02:00
|
|
|
|
2020-08-23 08:25:16 +04:30
|
|
|
virtual void will_begin_editing() { }
|
2019-06-23 08:18:28 +02:00
|
|
|
|
|
|
|
|
protected:
|
2022-02-26 10:50:04 -07:00
|
|
|
ModelEditingDelegate() = default;
|
2020-03-05 15:32:05 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual RefPtr<Widget> create_widget() = 0;
|
2019-06-23 08:18:28 +02:00
|
|
|
void commit()
|
|
|
|
|
{
|
|
|
|
|
if (on_commit)
|
|
|
|
|
on_commit();
|
|
|
|
|
}
|
2020-08-28 20:50:12 +02:00
|
|
|
void rollback()
|
|
|
|
|
{
|
|
|
|
|
if (on_rollback)
|
|
|
|
|
on_rollback();
|
|
|
|
|
}
|
2021-09-05 13:32:23 +02:00
|
|
|
void change()
|
|
|
|
|
{
|
|
|
|
|
if (on_change)
|
|
|
|
|
on_change();
|
|
|
|
|
}
|
2019-06-23 08:18:28 +02:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ModelIndex const& index() const { return m_index; }
|
2020-08-23 08:25:16 +04:30
|
|
|
|
2019-06-23 08:18:28 +02:00
|
|
|
private:
|
2020-02-02 15:07:41 +01:00
|
|
|
RefPtr<Model> m_model;
|
|
|
|
|
ModelIndex m_index;
|
|
|
|
|
RefPtr<Widget> m_widget;
|
2019-06-23 08:18:28 +02:00
|
|
|
};
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
class StringModelEditingDelegate : public ModelEditingDelegate {
|
2019-06-23 08:18:28 +02:00
|
|
|
public:
|
2022-02-26 10:50:04 -07:00
|
|
|
StringModelEditingDelegate() = default;
|
|
|
|
|
virtual ~StringModelEditingDelegate() override = default;
|
2019-06-23 08:18:28 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
virtual RefPtr<Widget> create_widget() override
|
2019-06-23 08:18:28 +02:00
|
|
|
{
|
2020-02-23 12:07:13 +01:00
|
|
|
auto textbox = TextBox::construct();
|
2023-04-29 16:47:52 -04:00
|
|
|
textbox->set_frame_style(Gfx::FrameStyle::NoFrame);
|
2021-09-05 13:32:23 +02:00
|
|
|
|
2019-06-23 08:18:28 +02:00
|
|
|
textbox->on_return_pressed = [this] {
|
|
|
|
|
commit();
|
|
|
|
|
};
|
2020-08-28 20:50:12 +02:00
|
|
|
textbox->on_escape_pressed = [this] {
|
|
|
|
|
rollback();
|
|
|
|
|
};
|
2021-09-05 13:32:23 +02:00
|
|
|
textbox->on_change = [this] {
|
|
|
|
|
change();
|
|
|
|
|
};
|
2019-06-23 08:18:28 +02:00
|
|
|
return textbox;
|
|
|
|
|
}
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual Variant value() const override { return static_cast<TextBox const*>(widget())->text(); }
|
2021-07-11 21:46:34 +02:00
|
|
|
virtual void set_value(Variant const& value, SelectionBehavior selection_behavior) override
|
2020-12-27 19:08:19 +01:00
|
|
|
{
|
|
|
|
|
auto& textbox = static_cast<TextBox&>(*widget());
|
2023-10-10 15:00:58 +03:30
|
|
|
if (value.is_valid())
|
2023-12-16 17:49:34 +03:30
|
|
|
textbox.set_text(value.to_byte_string());
|
2023-10-10 15:00:58 +03:30
|
|
|
else
|
|
|
|
|
textbox.clear();
|
2021-07-11 21:46:34 +02:00
|
|
|
if (selection_behavior == SelectionBehavior::SelectAll)
|
|
|
|
|
textbox.select_all();
|
2020-12-27 19:08:19 +01:00
|
|
|
}
|
2019-06-23 08:18:28 +02:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|