mirror of
https://github.com/RGBCube/serenity
synced 2025-07-06 00:37:35 +00:00
LibGUI: Move editing logic from GTableView up to GAbstractView.
GAbstractView should be able to manage the high-level editing logic, as long as subclasses implement content_rect(GModelIndex) so we know where to put the editing widgets. :^)
This commit is contained in:
parent
9cab7a0707
commit
18785ba5c3
5 changed files with 46 additions and 39 deletions
|
@ -35,11 +35,15 @@ void GAbstractView::model_notification(const GModelNotification& notification)
|
|||
|
||||
void GAbstractView::did_update_model()
|
||||
{
|
||||
if (!model() || model()->selected_index() != m_edit_index)
|
||||
stop_editing();
|
||||
model_notification(GModelNotification(GModelNotification::ModelUpdated));
|
||||
}
|
||||
|
||||
void GAbstractView::did_update_selection()
|
||||
{
|
||||
if (!model() || model()->selected_index() != m_edit_index)
|
||||
stop_editing();
|
||||
}
|
||||
|
||||
void GAbstractView::did_scroll()
|
||||
|
@ -53,3 +57,34 @@ void GAbstractView::update_edit_widget_position()
|
|||
return;
|
||||
m_edit_widget->set_relative_rect(m_edit_widget_content_rect.translated(-horizontal_scrollbar().value(), -vertical_scrollbar().value()));
|
||||
}
|
||||
|
||||
void GAbstractView::begin_editing(const GModelIndex& index)
|
||||
{
|
||||
ASSERT(is_editable());
|
||||
ASSERT(model());
|
||||
if (m_edit_index == index)
|
||||
return;
|
||||
if (!model()->is_editable(index))
|
||||
return;
|
||||
if (m_edit_widget)
|
||||
delete m_edit_widget;
|
||||
m_edit_index = index;
|
||||
m_edit_widget = new GTextBox(this);
|
||||
m_edit_widget->move_to_back();
|
||||
m_edit_widget->set_text(model()->data(index, GModel::Role::Display).to_string());
|
||||
m_edit_widget_content_rect = content_rect(index);
|
||||
update_edit_widget_position();
|
||||
m_edit_widget->set_focus(true);
|
||||
m_edit_widget->on_return_pressed = [this] {
|
||||
ASSERT(model());
|
||||
model()->set_data(m_edit_index, m_edit_widget->text());
|
||||
stop_editing();
|
||||
};
|
||||
}
|
||||
|
||||
void GAbstractView::stop_editing()
|
||||
{
|
||||
m_edit_index = { };
|
||||
delete m_edit_widget;
|
||||
m_edit_widget = nullptr;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue