2019-03-23 01:04:31 +00:00
|
|
|
#include <LibGUI/GAbstractView.h>
|
|
|
|
#include <LibGUI/GModel.h>
|
|
|
|
#include <LibGUI/GScrollBar.h>
|
2019-03-28 16:19:56 +00:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-04-18 20:27:14 +00:00
|
|
|
#include <LibGUI/GTextBox.h>
|
2019-03-23 01:04:31 +00:00
|
|
|
#include <Kernel/KeyCode.h>
|
|
|
|
|
|
|
|
GAbstractView::GAbstractView(GWidget* parent)
|
|
|
|
: GScrollableWidget(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GAbstractView::~GAbstractView()
|
|
|
|
{
|
2019-04-18 20:27:14 +00:00
|
|
|
delete m_edit_widget;
|
2019-03-23 01:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GAbstractView::set_model(RetainPtr<GModel>&& model)
|
|
|
|
{
|
2019-04-14 00:36:06 +00:00
|
|
|
if (model == m_model)
|
2019-03-23 01:04:31 +00:00
|
|
|
return;
|
|
|
|
if (m_model)
|
|
|
|
m_model->unregister_view(Badge<GAbstractView>(), *this);
|
|
|
|
m_model = move(model);
|
|
|
|
if (m_model)
|
|
|
|
m_model->register_view(Badge<GAbstractView>(), *this);
|
|
|
|
did_update_model();
|
|
|
|
}
|
|
|
|
|
2019-03-23 02:53:51 +00:00
|
|
|
void GAbstractView::model_notification(const GModelNotification& notification)
|
2019-03-23 01:04:31 +00:00
|
|
|
{
|
2019-03-23 02:53:51 +00:00
|
|
|
if (on_model_notification)
|
|
|
|
on_model_notification(notification);
|
2019-03-23 01:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GAbstractView::did_update_model()
|
|
|
|
{
|
2019-04-18 22:07:33 +00:00
|
|
|
if (!model() || model()->selected_index() != m_edit_index)
|
|
|
|
stop_editing();
|
2019-03-23 01:04:31 +00:00
|
|
|
model_notification(GModelNotification(GModelNotification::ModelUpdated));
|
|
|
|
}
|
2019-03-30 02:27:25 +00:00
|
|
|
|
|
|
|
void GAbstractView::did_update_selection()
|
|
|
|
{
|
2019-04-18 22:07:33 +00:00
|
|
|
if (!model() || model()->selected_index() != m_edit_index)
|
|
|
|
stop_editing();
|
2019-03-30 02:27:25 +00:00
|
|
|
}
|
2019-04-18 21:57:07 +00:00
|
|
|
|
|
|
|
void GAbstractView::did_scroll()
|
|
|
|
{
|
|
|
|
update_edit_widget_position();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GAbstractView::update_edit_widget_position()
|
|
|
|
{
|
|
|
|
if (!m_edit_widget)
|
|
|
|
return;
|
|
|
|
m_edit_widget->set_relative_rect(m_edit_widget_content_rect.translated(-horizontal_scrollbar().value(), -vertical_scrollbar().value()));
|
|
|
|
}
|
2019-04-18 22:07:33 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-05-09 02:56:52 +00:00
|
|
|
|
|
|
|
void GAbstractView::activate(const GModelIndex& index)
|
|
|
|
{
|
|
|
|
if (on_activation)
|
|
|
|
on_activation(index);
|
|
|
|
}
|